1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
3 /*
4 * This file is part of The Croco Library
5 *
6 * Copyright (C) 2002-2003 Dodji Seketeli <dodji@seketeli.org>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of version 2.1 of the GNU Lesser General Public
10 * License as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 * USA
21 */
23 /*
24 *$Id: cr-enc-handler.c,v 1.7 2004/03/07 13:22:47 dodji Exp $
25 */
27 /**
28 *@file
29 *The definition of the #CREncHandler class.
30 */
32 #include "cr-enc-handler.h"
33 #include "cr-utils.h"
35 #include <string.h>
37 struct CREncAlias {
38 const gchar *name;
39 enum CREncoding encoding;
40 };
42 static struct CREncAlias gv_default_aliases[] = {
43 {"UTF-8", CR_UTF_8},
44 {"UTF_8", CR_UTF_8},
45 {"UTF8", CR_UTF_8},
46 {"UTF-16", CR_UTF_16},
47 {"UTF_16", CR_UTF_16},
48 {"UTF16", CR_UTF_16},
49 {"UCS1", CR_UCS_1},
50 {"UCS-1", CR_UCS_1},
51 {"UCS_1", CR_UCS_1},
52 {"ISO-8859-1", CR_UCS_1},
53 {"ISO_8859-1", CR_UCS_1},
54 {"UCS-1", CR_UCS_1},
55 {"UCS_1", CR_UCS_1},
56 {"UCS4", CR_UCS_4},
57 {"UCS-4", CR_UCS_4},
58 {"UCS_4", CR_UCS_4},
59 {"ASCII", CR_ASCII},
60 {0, 0}
61 };
63 static CREncHandler gv_default_enc_handlers[] = {
64 {CR_UCS_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
65 cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
67 {CR_ISO_8859_1, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
68 cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
70 {CR_ASCII, cr_utils_ucs1_to_utf8, cr_utils_utf8_to_ucs1,
71 cr_utils_ucs1_str_len_as_utf8, cr_utils_utf8_str_len_as_ucs1},
73 {0, NULL, NULL, NULL, NULL}
74 };
76 /**
77 *Gets the instance of encoding handler.
78 *This function implements a singleton pattern.
79 *@param a_enc the encoding of the Handler.
80 *@return the instance of #CREncHandler.
81 */
82 CREncHandler *
83 cr_enc_handler_get_instance (enum CREncoding a_enc)
84 {
85 gulong i = 0;
87 for (i = 0; gv_default_enc_handlers[i].encoding; i++) {
88 if (gv_default_enc_handlers[i].encoding == a_enc) {
89 return (CREncHandler *)
90 & gv_default_enc_handlers[i].encoding;
91 }
92 }
94 return NULL;
95 }
97 /**
98 *Given an encoding name (called an alias name)
99 *the function returns the matching encoding type.
100 *@param a_alias_name the encoding name
101 *@param a_enc output param. The returned encoding type
102 *or 0 if the alias is not supported.
103 *@return CR_OK upon successfull completion, an error code otherwise.
104 */
105 enum CRStatus
106 cr_enc_handler_resolve_enc_alias (const guchar * a_alias_name,
107 enum CREncoding *a_enc)
108 {
109 gulong i = 0;
110 guchar *alias_name_up = NULL;
111 enum CRStatus status = CR_ENCODING_NOT_FOUND_ERROR;
113 g_return_val_if_fail (a_alias_name != NULL, CR_BAD_PARAM_ERROR);
115 alias_name_up = g_strdup (a_alias_name);
116 g_ascii_strup (alias_name_up, -1);
118 for (i = 0; gv_default_aliases[i].name; i++) {
119 if (!strcmp (gv_default_aliases[i].name, alias_name_up)) {
120 *a_enc = gv_default_aliases[i].encoding;
121 status = CR_OK;
122 break;
123 }
124 }
126 return status;
127 }
129 /**
130 *Converts a raw input buffer into an utf8 buffer.
131 *@param a_this the current instance of #CREncHandler.
132 *@param a_in the input buffer to convert.
133 *@param a_in_len in/out parameter. The len of the input
134 *buffer to convert. After return, contains the number of
135 *bytes actually consumed.
136 *@param @a_out output parameter. The converted output buffer.
137 *Must be freed by the buffer.
138 *@param a_out_len output parameter. The length of the output buffer.
139 *@return CR_OK upon successfull completion, an error code otherwise.
140 */
141 enum CRStatus
142 cr_enc_handler_convert_input (CREncHandler * a_this,
143 const guchar * a_in,
144 gulong * a_in_len,
145 guchar ** a_out, gulong * a_out_len)
146 {
147 enum CRStatus status = CR_OK;
149 g_return_val_if_fail (a_this && a_in && a_in_len && a_out,
150 CR_BAD_PARAM_ERROR);
152 if (a_this->decode_input == NULL)
153 return CR_OK;
155 if (a_this->enc_str_len_as_utf8) {
156 status = a_this->enc_str_len_as_utf8 (a_in,
157 &a_in[*a_in_len - 1],
158 a_out_len);
160 g_return_val_if_fail (status == CR_OK, status);
161 } else {
162 *a_out_len = *a_in_len;
163 }
165 *a_out = g_malloc0 (*a_out_len);
167 status = a_this->decode_input (a_in, a_in_len, *a_out, a_out_len);
169 if (status != CR_OK) {
170 g_free (*a_out);
171 *a_out = NULL;
172 }
174 g_return_val_if_fail (status == CR_OK, status);
176 return CR_OK;
177 }