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
00083
00084
00085 CREncHandler *
00086 cr_enc_handler_get_instance (enum CREncoding a_enc)
00087 {
00088 gulong i = 0;
00089
00090 for (i = 0; gv_default_enc_handlers[i].encoding; i++) {
00091 if (gv_default_enc_handlers[i].encoding == a_enc) {
00092 return (CREncHandler *)
00093 & gv_default_enc_handlers[i].encoding;
00094 }
00095 }
00096
00097 return NULL;
00098 }
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 enum CRStatus
00112 cr_enc_handler_resolve_enc_alias (const guchar * a_alias_name,
00113 enum CREncoding *a_enc)
00114 {
00115 gulong i = 0;
00116 guchar *alias_name_up = NULL;
00117 enum CRStatus status = CR_ENCODING_NOT_FOUND_ERROR;
00118
00119 g_return_val_if_fail (a_alias_name != NULL, CR_BAD_PARAM_ERROR);
00120
00121 alias_name_up = g_strdup (a_alias_name);
00122 g_ascii_strup (alias_name_up, -1);
00123
00124 for (i = 0; gv_default_aliases[i].name; i++) {
00125 if (!strcmp (gv_default_aliases[i].name, alias_name_up)) {
00126 *a_enc = gv_default_aliases[i].encoding;
00127 status = CR_OK;
00128 break;
00129 }
00130 }
00131
00132 return status;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150 enum CRStatus
00151 cr_enc_handler_convert_input (CREncHandler * a_this,
00152 const guchar * a_in,
00153 gulong * a_in_len,
00154 guchar ** a_out, gulong * a_out_len)
00155 {
00156 enum CRStatus status = CR_OK;
00157
00158 g_return_val_if_fail (a_this && a_in && a_in_len && a_out,
00159 CR_BAD_PARAM_ERROR);
00160
00161 if (a_this->decode_input == NULL)
00162 return CR_OK;
00163
00164 if (a_this->enc_str_len_as_utf8) {
00165 status = a_this->enc_str_len_as_utf8 (a_in,
00166 &a_in[*a_in_len - 1],
00167 a_out_len);
00168
00169 g_return_val_if_fail (status == CR_OK, status);
00170 } else {
00171 *a_out_len = *a_in_len;
00172 }
00173
00174 *a_out = g_malloc0 (*a_out_len);
00175
00176 status = a_this->decode_input (a_in, a_in_len, *a_out, a_out_len);
00177
00178 if (status != CR_OK) {
00179 g_free (*a_out);
00180 *a_out = NULL;
00181 }
00182
00183 g_return_val_if_fail (status == CR_OK, status);
00184
00185 return CR_OK;
00186 }