00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "cr-enc-handler.h"
00033 #include "cr-utils.h"
00034
00035 #include <string.h>
00036
00037 struct CREncAlias {
00038 const gchar *name;
00039 enum CREncoding encoding;
00040 };
00041
00042 static struct CREncAlias gv_default_aliases[] = {
00043 {"UTF-8", CR_UTF_8},
00044 {"UTF_8", CR_UTF_8},
00045 {"UTF8", CR_UTF_8},
00046 {"UTF-16", CR_UTF_16},
00047 {"UTF_16", CR_UTF_16},
00048 {"UTF16", CR_UTF_16},
00049 {"UCS1", CR_UCS_1},
00050 {"UCS-1", CR_UCS_1},
00051 {"UCS_1", CR_UCS_1},
00052 {"ISO-8859-1", CR_UCS_1},
00053 {"ISO_8859-1", CR_UCS_1},
00054 {"UCS-1", CR_UCS_1},
00055 {"UCS_1", CR_UCS_1},
00056 {"UCS4", CR_UCS_4},
00057 {"UCS-4", CR_UCS_4},
00058 {"UCS_4", CR_UCS_4},
00059 {"ASCII", CR_ASCII},
00060 {0, 0}
00061 };
00062
00063 static CREncHandler gv_default_enc_handlers[] = {
00064 {CR_UCS_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
00065 cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
00066
00067 {CR_ISO_8859_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
00068 cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
00069
00070 {CR_ASCII, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
00071 cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
00072
00073 {0, NULL, NULL, NULL, NULL}
00074 };
00075
00076
00077
00078
00079
00080
00081
00082 CREncHandler *
00083 cr_enc_handler_get_instance (enum CREncoding a_enc)
00084 {
00085 gulong i = 0;
00086
00087 for (i = 0; gv_default_enc_handlers[i].encoding; i++) {
00088 if (gv_default_enc_handlers[i].encoding == a_enc) {
00089 return (CREncHandler *)
00090 & gv_default_enc_handlers[i].encoding;
00091 }
00092 }
00093
00094 return NULL;
00095 }
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 enum CRStatus
00106 cr_enc_handler_resolve_enc_alias (const guchar * a_alias_name,
00107 enum CREncoding *a_enc)
00108 {
00109 gulong i = 0;
00110 guchar *alias_name_up = NULL;
00111 enum CRStatus status = CR_ENCODING_NOT_FOUND_ERROR;
00112
00113 g_return_val_if_fail (a_alias_name != NULL, CR_BAD_PARAM_ERROR);
00114
00115 alias_name_up = g_strdup (a_alias_name);
00116 g_ascii_strup (alias_name_up, -1);
00117
00118 for (i = 0; gv_default_aliases[i].name; i++) {
00119 if (!strcmp (gv_default_aliases[i].name, alias_name_up)) {
00120 *a_enc = gv_default_aliases[i].encoding;
00121 status = CR_OK;
00122 break;
00123 }
00124 }
00125
00126 return status;
00127 }
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141 enum CRStatus
00142 cr_enc_handler_convert_input (CREncHandler * a_this,
00143 const guchar * a_in,
00144 gulong * a_in_len,
00145 guchar ** a_out, gulong * a_out_len)
00146 {
00147 enum CRStatus status = CR_OK;
00148
00149 g_return_val_if_fail (a_this && a_in && a_in_len && a_out,
00150 CR_BAD_PARAM_ERROR);
00151
00152 if (a_this->decode_input == NULL)
00153 return CR_OK;
00154
00155 if (a_this->enc_str_len_as_utf8) {
00156 status = a_this->enc_str_len_as_utf8 (a_in,
00157 &a_in[*a_in_len - 1],
00158 a_out_len);
00159
00160 g_return_val_if_fail (status == CR_OK, status);
00161 } else {
00162 *a_out_len = *a_in_len;
00163 }
00164
00165 *a_out = g_malloc0 (*a_out_len);
00166
00167 status = a_this->decode_input (a_in, a_in_len, *a_out, a_out_len);
00168
00169 if (status != CR_OK) {
00170 g_free (*a_out);
00171 *a_out = NULL;
00172 }
00173
00174 g_return_val_if_fail (status == CR_OK, status);
00175
00176 return CR_OK;
00177 }