Update.
[glibc.git] / iconv / gconv_open.c
blob94a54f80089bad9f26ed3735adc5afed6c183f8f
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307 USA. */
21 #include <errno.h>
22 #include <locale.h>
23 #include "../locale/localeinfo.h"
24 #include <stdlib.h>
25 #include <string.h>
27 #include <gconv_int.h>
30 int
31 internal_function
32 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
33 int flags)
35 struct __gconv_step *steps;
36 size_t nsteps;
37 __gconv_t result = NULL;
38 size_t cnt = 0;
39 int res;
40 int conv_flags = 0;
41 const char *errhand;
42 const char *ignore;
43 struct trans_struct *trans = NULL;
45 /* Find out whether any error handling method is specified. */
46 errhand = strchr (toset, '/');
47 if (errhand != NULL)
48 errhand = strchr (errhand + 1, '/');
49 if (__builtin_expect (errhand != NULL, 1))
51 if (*++errhand == '\0')
52 errhand = NULL;
53 else
55 /* Make copy without the error handling description. */
56 char *newtoset = (char *) alloca (errhand - toset + 1);
57 char *tok;
58 char *ptr;
60 newtoset[errhand - toset] = '\0';
61 toset = memcpy (newtoset, toset, errhand - toset);
63 /* Find the appropriate transliteration handlers. */
64 tok = strdupa (errhand);
66 tok = __strtok_r (tok, ",", &ptr);
67 while (tok != NULL)
69 if (__strcasecmp_l (tok, "TRANSLIT", &_nl_C_locobj) == 0)
71 /* It's the builtin transliteration handling. We only
72 support it for working on the internal encoding. */
73 static const char *internal_trans_names[1] = { "INTERNAL" };
74 struct trans_struct *lastp = NULL;
75 struct trans_struct *runp;
77 for (runp = trans; runp != NULL; runp = runp->next)
78 if (runp->trans_fct == __gconv_transliterate)
79 break;
80 else
81 lastp = runp;
83 if (runp == NULL)
85 struct trans_struct *newp;
87 newp = (struct trans_struct *) alloca (sizeof (*newp));
88 memset (newp, '\0', sizeof (*newp));
90 /* We leave the `name' field zero to signal that
91 this is an internal transliteration step. */
92 newp->csnames = internal_trans_names;
93 newp->ncsnames = 1;
94 newp->trans_fct = __gconv_transliterate;
96 if (lastp == NULL)
97 trans = newp;
98 else
99 lastp->next = newp;
102 else if (__strcasecmp_l (tok, "IGNORE", &_nl_C_locobj) == 0)
103 /* Set the flag to ignore all errors. */
104 conv_flags |= __GCONV_IGNORE_ERRORS;
105 else
107 /* `tok' is possibly a module name. We'll see later
108 whether we can find it. But first see that we do
109 not already a module of this name. */
110 struct trans_struct *lastp = NULL;
111 struct trans_struct *runp;
113 for (runp = trans; runp != NULL; runp = runp->next)
114 if (runp->name != NULL
115 && __strcasecmp_l (tok, runp->name,
116 &_nl_C_locobj) == 0)
117 break;
118 else
119 lastp = runp;
121 if (runp == NULL)
123 struct trans_struct *newp;
125 newp = (struct trans_struct *) alloca (sizeof (*newp));
126 memset (newp, '\0', sizeof (*newp));
127 newp->name = tok;
129 if (lastp == NULL)
130 trans = newp;
131 else
132 lastp->next = newp;
136 tok = __strtok_r (NULL, ",", &ptr);
141 /* For the source character set we ignore the error handler specification.
142 XXX Is this really always the best? */
143 ignore = strchr (fromset, '/');
144 if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
145 && *++ignore != '\0')
147 char *newfromset = (char *) alloca (ignore - fromset + 1);
149 newfromset[ignore - fromset] = '\0';
150 fromset = memcpy (newfromset, fromset, ignore - fromset);
153 /* If the string is empty define this to mean the charset of the
154 currently selected locale. */
155 if (strcmp (toset, "//") == 0)
157 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
158 size_t len = strlen (codeset);
159 char *dest;
160 toset = dest = (char *) alloca (len + 3);
161 memcpy (__mempcpy (dest, codeset, len), "//", 3);
163 if (strcmp (fromset, "//") == 0)
165 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
166 size_t len = strlen (codeset);
167 char *dest;
168 fromset = dest = (char *) alloca (len + 3);
169 memcpy (__mempcpy (dest, codeset, len), "//", 3);
172 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
173 if (res == __GCONV_OK)
175 /* Find the modules. */
176 struct trans_struct *lastp = NULL;
177 struct trans_struct *runp;
179 for (runp = trans; runp != NULL; runp = runp->next)
181 if (runp->name == NULL
182 || __builtin_expect (__gconv_translit_find (runp), 0) == 0)
183 lastp = runp;
184 else
185 /* This means we haven't found the module. Remove it. */
186 (lastp == NULL ? trans : lastp->next) = runp->next;
189 /* Allocate room for handle. */
190 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
191 + (nsteps
192 * sizeof (struct __gconv_step_data)));
193 if (result == NULL)
194 res = __GCONV_NOMEM;
195 else
197 size_t n;
199 /* Remember the list of steps. */
200 result->__steps = steps;
201 result->__nsteps = nsteps;
203 /* Clear the array for the step data. */
204 memset (result->__data, '\0',
205 nsteps * sizeof (struct __gconv_step_data));
207 /* Call all initialization functions for the transformation
208 step implementations. */
209 for (cnt = 0; cnt < nsteps; ++cnt)
211 size_t size;
213 /* Would have to be done if we would not clear the whole
214 array above. */
215 #if 0
216 /* Reset the counter. */
217 result->__data[cnt].__invocation_counter = 0;
219 /* It's a regular use. */
220 result->__data[cnt].__internal_use = 0;
221 #endif
223 /* We use the `mbstate_t' member in DATA. */
224 result->__data[cnt].__statep = &result->__data[cnt].__state;
226 /* Now see whether we can use any of the transliteration
227 modules for this step. */
228 for (runp = trans; runp != NULL; runp = runp->next)
229 for (n = 0; n < runp->ncsnames; ++n)
230 if (__strcasecmp_l (steps[cnt].__from_name,
231 runp->csnames[n], &_nl_C_locobj) == 0)
233 void *data = NULL;
235 /* Match! Now try the initializer. */
236 if (runp->trans_init_fct == NULL
237 || (runp->trans_init_fct (&data,
238 steps[cnt].__to_name)
239 == __GCONV_OK))
241 /* Append at the end of the list. */
242 struct __gconv_trans_data *newp;
243 struct __gconv_trans_data **lastp;
245 newp = (struct __gconv_trans_data *)
246 malloc (sizeof (struct __gconv_trans_data));
247 if (newp == NULL)
249 res = __GCONV_NOMEM;
250 goto bail;
253 newp->__trans_fct = runp->trans_fct;
254 newp->__trans_context_fct = runp->trans_context_fct;
255 newp->__trans_end_fct = runp->trans_end_fct;
256 newp->__data = data;
257 newp->__next = NULL;
259 lastp = &result->__data[cnt].__trans;
260 while (*lastp != NULL)
261 lastp = &(*lastp)->__next;
263 *lastp = newp;
265 break;
268 /* If this is the last step we must not allocate an
269 output buffer. */
270 if (cnt < nsteps - 1)
272 result->__data[cnt].__flags = conv_flags;
274 /* Allocate the buffer. */
275 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
277 result->__data[cnt].__outbuf = (char *) malloc (size);
278 if (result->__data[cnt].__outbuf == NULL)
280 res = __GCONV_NOMEM;
281 goto bail;
284 result->__data[cnt].__outbufend =
285 result->__data[cnt].__outbuf + size;
287 else
289 /* Handle the last entry. */
290 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
292 break;
297 if (res != __GCONV_OK)
299 /* Something went wrong. Free all the resources. */
300 int serrno;
301 bail:
302 serrno = errno;
304 if (result != NULL)
306 while (cnt-- > 0)
308 struct __gconv_trans_data *transp;
310 transp = result->__data[cnt].__trans;
311 while (transp != NULL)
313 struct __gconv_trans_data *curp = transp;
314 transp = transp->__next;
316 if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
317 curp->__trans_end_fct (curp->__data);
319 free (curp);
322 free (result->__data[cnt].__outbuf);
325 free (result);
326 result = NULL;
329 __gconv_close_transform (steps, nsteps);
331 __set_errno (serrno);
335 *handle = result;
336 return res;