Updated to fedora-glibc-20050504T0728
[glibc.git] / iconv / gconv_open.c
blobb23ab44e4e272b4385b99f78ef993183ff8d90bb
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997,1998,1999,2000,2001,2004,2005
3 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library 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 GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
22 #include <errno.h>
23 #include <locale.h>
24 #include "../locale/localeinfo.h"
25 #include <stdlib.h>
26 #include <string.h>
28 #include <gconv_int.h>
31 int
32 internal_function
33 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
34 int flags)
36 struct __gconv_step *steps;
37 size_t nsteps;
38 __gconv_t result = NULL;
39 size_t cnt = 0;
40 int res;
41 int conv_flags = 0;
42 const char *errhand;
43 const char *ignore;
44 struct trans_struct *trans = NULL;
46 /* Find out whether any error handling method is specified. */
47 errhand = strchr (toset, '/');
48 if (errhand != NULL)
49 errhand = strchr (errhand + 1, '/');
50 if (__builtin_expect (errhand != NULL, 1))
52 if (*++errhand == '\0')
53 errhand = NULL;
54 else
56 /* Make copy without the error handling description. */
57 char *newtoset = (char *) alloca (errhand - toset + 1);
58 char *tok;
59 char *ptr;
61 newtoset[errhand - toset] = '\0';
62 toset = memcpy (newtoset, toset, errhand - toset);
64 /* Find the appropriate transliteration handlers. */
65 tok = strdupa (errhand);
67 tok = __strtok_r (tok, ",", &ptr);
68 while (tok != NULL)
70 if (__strcasecmp_l (tok, "TRANSLIT", &_nl_C_locobj) == 0)
72 /* It's the builtin transliteration handling. We only
73 support it for working on the internal encoding. */
74 static const char *internal_trans_names[1] = { "INTERNAL" };
75 struct trans_struct *lastp = NULL;
76 struct trans_struct *runp;
78 for (runp = trans; runp != NULL; runp = runp->next)
79 if (runp->trans_fct == __gconv_transliterate)
80 break;
81 else
82 lastp = runp;
84 if (runp == NULL)
86 struct trans_struct *newp;
88 newp = (struct trans_struct *) alloca (sizeof (*newp));
89 memset (newp, '\0', sizeof (*newp));
91 /* We leave the `name' field zero to signal that
92 this is an internal transliteration step. */
93 newp->csnames = internal_trans_names;
94 newp->ncsnames = 1;
95 newp->trans_fct = __gconv_transliterate;
97 if (lastp == NULL)
98 trans = newp;
99 else
100 lastp->next = newp;
103 else if (__strcasecmp_l (tok, "IGNORE", &_nl_C_locobj) == 0)
104 /* Set the flag to ignore all errors. */
105 conv_flags |= __GCONV_IGNORE_ERRORS;
106 else
108 /* `tok' is possibly a module name. We'll see later
109 whether we can find it. But first see that we do
110 not already a module of this name. */
111 struct trans_struct *lastp = NULL;
112 struct trans_struct *runp;
114 for (runp = trans; runp != NULL; runp = runp->next)
115 if (runp->name != NULL
116 && __strcasecmp_l (tok, runp->name,
117 &_nl_C_locobj) == 0)
118 break;
119 else
120 lastp = runp;
122 if (runp == NULL)
124 struct trans_struct *newp;
126 newp = (struct trans_struct *) alloca (sizeof (*newp));
127 memset (newp, '\0', sizeof (*newp));
128 newp->name = tok;
130 if (lastp == NULL)
131 trans = newp;
132 else
133 lastp->next = newp;
137 tok = __strtok_r (NULL, ",", &ptr);
142 /* For the source character set we ignore the error handler specification.
143 XXX Is this really always the best? */
144 ignore = strchr (fromset, '/');
145 if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
146 && *++ignore != '\0')
148 char *newfromset = (char *) alloca (ignore - fromset + 1);
150 newfromset[ignore - fromset] = '\0';
151 fromset = memcpy (newfromset, fromset, ignore - fromset);
154 /* If the string is empty define this to mean the charset of the
155 currently selected locale. */
156 if (strcmp (toset, "//") == 0)
158 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
159 size_t len = strlen (codeset);
160 char *dest;
161 toset = dest = (char *) alloca (len + 3);
162 memcpy (__mempcpy (dest, codeset, len), "//", 3);
164 if (strcmp (fromset, "//") == 0)
166 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
167 size_t len = strlen (codeset);
168 char *dest;
169 fromset = dest = (char *) alloca (len + 3);
170 memcpy (__mempcpy (dest, codeset, len), "//", 3);
173 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
174 if (res == __GCONV_OK)
176 /* Find the modules. */
177 struct trans_struct *lastp = NULL;
178 struct trans_struct *runp;
180 for (runp = trans; runp != NULL; runp = runp->next)
182 if (runp->name == NULL
183 || __builtin_expect (__gconv_translit_find (runp), 0) == 0)
184 lastp = runp;
185 else
187 /* This means we haven't found the module. Remove it. */
188 if (lastp == NULL)
189 trans = runp->next;
190 else
191 lastp->next = runp->next;
195 /* Allocate room for handle. */
196 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
197 + (nsteps
198 * sizeof (struct __gconv_step_data)));
199 if (result == NULL)
200 res = __GCONV_NOMEM;
201 else
203 size_t n;
205 /* Remember the list of steps. */
206 result->__steps = steps;
207 result->__nsteps = nsteps;
209 /* Clear the array for the step data. */
210 memset (result->__data, '\0',
211 nsteps * sizeof (struct __gconv_step_data));
213 /* Call all initialization functions for the transformation
214 step implementations. */
215 for (cnt = 0; cnt < nsteps; ++cnt)
217 size_t size;
219 /* Would have to be done if we would not clear the whole
220 array above. */
221 #if 0
222 /* Reset the counter. */
223 result->__data[cnt].__invocation_counter = 0;
225 /* It's a regular use. */
226 result->__data[cnt].__internal_use = 0;
227 #endif
229 /* We use the `mbstate_t' member in DATA. */
230 result->__data[cnt].__statep = &result->__data[cnt].__state;
232 /* Now see whether we can use any of the transliteration
233 modules for this step. */
234 for (runp = trans; runp != NULL; runp = runp->next)
235 for (n = 0; n < runp->ncsnames; ++n)
236 if (__strcasecmp_l (steps[cnt].__from_name,
237 runp->csnames[n], &_nl_C_locobj) == 0)
239 void *data = NULL;
241 /* Match! Now try the initializer. */
242 if (runp->trans_init_fct == NULL
243 || (runp->trans_init_fct (&data,
244 steps[cnt].__to_name)
245 == __GCONV_OK))
247 /* Append at the end of the list. */
248 struct __gconv_trans_data *newp;
249 struct __gconv_trans_data **lastp;
251 newp = (struct __gconv_trans_data *)
252 malloc (sizeof (struct __gconv_trans_data));
253 if (newp == NULL)
255 res = __GCONV_NOMEM;
256 goto bail;
259 newp->__trans_fct = runp->trans_fct;
260 newp->__trans_context_fct = runp->trans_context_fct;
261 newp->__trans_end_fct = runp->trans_end_fct;
262 newp->__data = data;
263 newp->__next = NULL;
265 lastp = &result->__data[cnt].__trans;
266 while (*lastp != NULL)
267 lastp = &(*lastp)->__next;
269 *lastp = newp;
271 break;
274 /* If this is the last step we must not allocate an
275 output buffer. */
276 if (cnt < nsteps - 1)
278 result->__data[cnt].__flags = conv_flags;
280 /* Allocate the buffer. */
281 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
283 result->__data[cnt].__outbuf = malloc (size);
284 if (result->__data[cnt].__outbuf == NULL)
286 res = __GCONV_NOMEM;
287 goto bail;
290 result->__data[cnt].__outbufend =
291 result->__data[cnt].__outbuf + size;
293 else
295 /* Handle the last entry. */
296 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
298 break;
303 if (res != __GCONV_OK)
305 /* Something went wrong. Free all the resources. */
306 int serrno;
307 bail:
308 serrno = errno;
310 if (result != NULL)
312 while (cnt-- > 0)
314 struct __gconv_trans_data *transp;
316 transp = result->__data[cnt].__trans;
317 while (transp != NULL)
319 struct __gconv_trans_data *curp = transp;
320 transp = transp->__next;
322 if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
323 curp->__trans_end_fct (curp->__data);
325 free (curp);
328 free (result->__data[cnt].__outbuf);
331 free (result);
332 result = NULL;
335 __gconv_close_transform (steps, nsteps);
337 __set_errno (serrno);
341 *handle = result;
342 return res;