1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2014 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, see
18 <http://www.gnu.org/licenses/>. */
22 #include "../locale/localeinfo.h"
26 #include <gconv_int.h>
31 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
34 struct __gconv_step *steps;
36 __gconv_t result = NULL;
42 struct trans_struct *trans = NULL;
44 /* Find out whether any error handling method is specified. */
45 errhand = strchr (toset, '/');
47 errhand = strchr (errhand + 1, '/');
48 if (__builtin_expect (errhand != NULL, 1))
50 if (*++errhand == '\0')
54 /* Make copy without the error handling description. */
55 char *newtoset = (char *) alloca (errhand - toset + 1);
57 char *ptr = NULL /* Work around a bogus warning */;
59 newtoset[errhand - toset] = '\0';
60 toset = memcpy (newtoset, toset, errhand - toset);
62 /* Find the appropriate transliteration handlers. */
63 tok = strdupa (errhand);
65 tok = __strtok_r (tok, ",", &ptr);
68 if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
70 /* It's the builtin transliteration handling. We only
71 support it for working on the internal encoding. */
72 static const char *const internal_trans_names[1]
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)
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 = (const char **) internal_trans_names;
94 newp->trans_fct = __gconv_transliterate;
102 else if (__strcasecmp_l (tok, "IGNORE", _nl_C_locobj_ptr) == 0)
103 /* Set the flag to ignore all errors. */
104 conv_flags |= __GCONV_IGNORE_ERRORS;
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_ptr) == 0)
123 struct trans_struct *newp;
125 newp = (struct trans_struct *) alloca (sizeof (*newp));
126 memset (newp, '\0', sizeof (*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);
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);
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)
186 /* This means we haven't found the module. Remove it. */
190 lastp->next = runp->next;
194 /* Allocate room for handle. */
195 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
197 * sizeof (struct __gconv_step_data)));
204 /* Remember the list of steps. */
205 result->__steps = steps;
206 result->__nsteps = nsteps;
208 /* Clear the array for the step data. */
209 memset (result->__data, '\0',
210 nsteps * sizeof (struct __gconv_step_data));
212 /* Call all initialization functions for the transformation
213 step implementations. */
214 for (cnt = 0; cnt < nsteps; ++cnt)
218 /* Would have to be done if we would not clear the whole
221 /* Reset the counter. */
222 result->__data[cnt].__invocation_counter = 0;
224 /* It's a regular use. */
225 result->__data[cnt].__internal_use = 0;
228 /* We use the `mbstate_t' member in DATA. */
229 result->__data[cnt].__statep = &result->__data[cnt].__state;
231 /* Now see whether we can use any of the transliteration
232 modules for this step. */
233 for (runp = trans; runp != NULL; runp = runp->next)
234 for (n = 0; n < runp->ncsnames; ++n)
235 if (__strcasecmp_l (steps[cnt].__from_name,
236 runp->csnames[n], _nl_C_locobj_ptr) == 0)
240 /* Match! Now try the initializer. */
241 if (runp->trans_init_fct == NULL
242 || (runp->trans_init_fct (&data,
243 steps[cnt].__to_name)
246 /* Append at the end of the list. */
247 struct __gconv_trans_data *newp;
248 struct __gconv_trans_data **lastp;
250 newp = (struct __gconv_trans_data *)
251 malloc (sizeof (struct __gconv_trans_data));
258 newp->__trans_fct = runp->trans_fct;
259 newp->__trans_context_fct = runp->trans_context_fct;
260 newp->__trans_end_fct = runp->trans_end_fct;
264 lastp = &result->__data[cnt].__trans;
265 while (*lastp != NULL)
266 lastp = &(*lastp)->__next;
273 /* If this is the last step we must not allocate an
275 if (cnt < nsteps - 1)
277 result->__data[cnt].__flags = conv_flags;
279 /* Allocate the buffer. */
280 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
282 result->__data[cnt].__outbuf = malloc (size);
283 if (result->__data[cnt].__outbuf == NULL)
289 result->__data[cnt].__outbufend =
290 result->__data[cnt].__outbuf + size;
294 /* Handle the last entry. */
295 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
302 if (res != __GCONV_OK)
304 /* Something went wrong. Free all the resources. */
313 struct __gconv_trans_data *transp;
315 transp = result->__data[cnt].__trans;
316 while (transp != NULL)
318 struct __gconv_trans_data *curp = transp;
319 transp = transp->__next;
321 if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
322 curp->__trans_end_fct (curp->__data);
327 free (result->__data[cnt].__outbuf);
334 __gconv_close_transform (steps, nsteps);
336 __set_errno (serrno);