Libcroco
cr-enc-handler.c
Go to the documentation of this file.
1 /* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
2 
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  */
22 
23 /*
24  *$Id$
25  */
26 
27 /**
28  *@file
29  *The definition of the #CREncHandler class.
30  */
31 
32 #include "cr-enc-handler.h"
33 #include "cr-utils.h"
34 
35 #include <string.h>
36 
37 struct CREncAlias {
38  const gchar *name;
40 };
41 
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 };
62 
63 static CREncHandler gv_default_enc_handlers[] = {
66 
69 
72 
73  {0, NULL, NULL, NULL, NULL}
74 };
75 
76 /**
77  * cr_enc_handler_get_instance:
78  *@a_enc: the encoding of the Handler.
79  *
80  *Gets the instance of encoding handler.
81  *This function implements a singleton pattern.
82  *
83  *Returns the instance of #CREncHandler.
84  */
87 {
88  gulong i = 0;
89 
90  for (i = 0; gv_default_enc_handlers[i].encoding; i++) {
91  if (gv_default_enc_handlers[i].encoding == a_enc) {
92  return (CREncHandler *) & gv_default_enc_handlers[i];
93  }
94  }
95 
96  return NULL;
97 }
98 
99 /**
100  * cr_enc_handler_resolve_enc_alias:
101  *@a_alias_name: the encoding name.
102  *@a_enc: output param. The returned encoding type
103  *or 0 if the alias is not supported.
104  *
105  *Given an encoding name (called an alias name)
106  *the function returns the matching encoding type.
107  *
108  *Returns CR_OK upon successfull completion, an error code otherwise.
109  */
110 enum CRStatus
111 cr_enc_handler_resolve_enc_alias (const guchar * a_alias_name,
112  enum CREncoding *a_enc)
113 {
114  gulong i = 0;
115  guchar *alias_name_up = NULL;
117 
118  g_return_val_if_fail (a_alias_name != NULL, CR_BAD_PARAM_ERROR);
119 
120  alias_name_up = (guchar *) g_ascii_strup ((const gchar *) a_alias_name, -1);
121 
122  for (i = 0; gv_default_aliases[i].name; i++) {
123  if (!strcmp (gv_default_aliases[i].name, (const gchar *) alias_name_up)) {
124  *a_enc = gv_default_aliases[i].encoding;
125  status = CR_OK;
126  break;
127  }
128  }
129 
130  return status;
131 }
132 
133 /**
134  * cr_enc_handler_convert_input:
135  *@a_this: the current instance of #CREncHandler.
136  *@a_in: the input buffer to convert.
137  *@a_in_len: in/out parameter. The len of the input
138  *buffer to convert. After return, contains the number of
139  *bytes actually consumed.
140  *@a_out: output parameter. The converted output buffer.
141  *Must be freed by the buffer.
142  *@a_out_len: output parameter. The length of the output buffer.
143  *
144  *Converts a raw input buffer into an utf8 buffer.
145  *
146  *Returns CR_OK upon successfull completion, an error code otherwise.
147  */
148 enum CRStatus
150  const guchar * a_in,
151  gulong * a_in_len,
152  guchar ** a_out, gulong * a_out_len)
153 {
154  enum CRStatus status = CR_OK;
155 
156  g_return_val_if_fail (a_this && a_in && a_in_len && a_out,
158 
159  if (a_this->decode_input == NULL)
160  return CR_OK;
161 
162  if (a_this->enc_str_len_as_utf8) {
163  status = a_this->enc_str_len_as_utf8 (a_in,
164  &a_in[*a_in_len - 1],
165  a_out_len);
166 
167  g_return_val_if_fail (status == CR_OK, status);
168  } else {
169  *a_out_len = *a_in_len;
170  }
171 
172  *a_out = g_malloc0 (*a_out_len);
173 
174  status = a_this->decode_input (a_in, a_in_len, *a_out, a_out_len);
175 
176  if (status != CR_OK) {
177  g_free (*a_out);
178  *a_out = NULL;
179  }
180 
181  g_return_val_if_fail (status == CR_OK, status);
182 
183  return CR_OK;
184 }
enum CRStatus cr_utils_ucs1_str_len_as_utf8(const guchar *a_in_start, const guchar *a_in_end, gulong *a_len)
Given an ucsA string, this function returns the size (in bytes) this string would have occupied if it...
Definition: cr-utils.c:230
typedefG_BEGIN_DECLS struct _CREncHandler CREncHandler
CREncHandler * cr_enc_handler_get_instance(enum CREncoding a_enc)
cr_enc_handler_get_instance: @a_enc: the encoding of the Handler.
The Croco library basic types definitions And global definitions.
CREncoding
Encoding values.
Definition: cr-utils.h:84
CRStatus
The status type returned by the methods of the croco library.
Definition: cr-utils.h:43
enum CREncoding encoding
const gchar * name
Definition: cr-utils.h:44
enum CRStatus cr_utils_utf8_to_ucs1(const guchar *a_in, gulong *a_in_len, guchar *a_out, gulong *a_out_len)
Converts an utf8 buffer into an ucs1 buffer.
Definition: cr-utils.c:995
enum CRStatus cr_utils_ucs1_to_utf8(const guchar *a_in, gulong *a_in_len, guchar *a_out, gulong *a_out_len)
Converts an ucs1 buffer into an utf8 buffer.
Definition: cr-utils.c:886
The declaration of the CREncHandler class.
enum CRStatus cr_enc_handler_resolve_enc_alias(const guchar *a_alias_name, enum CREncoding *a_enc)
cr_enc_handler_resolve_enc_alias: @a_alias_name: the encoding name.
enum CRStatus cr_utils_utf8_str_len_as_ucs1(const guchar *a_in_start, const guchar *a_in_end, gulong *a_len)
Definition: cr-utils.c:569
enum CRStatus cr_enc_handler_convert_input(CREncHandler *a_this, const guchar *a_in, gulong *a_in_len, guchar **a_out, gulong *a_out_len)
cr_enc_handler_convert_input: @a_this: the current instance of CREncHandler.