[BZ #284, BZ #721]
[platform/upstream/glibc.git] / iconv / gconv_open.c
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.
6
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.
11
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.
16
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.  */
21
22 #include <errno.h>
23 #include <locale.h>
24 #include "../locale/localeinfo.h"
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <gconv_int.h>
29
30
31 int
32 internal_function
33 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
34               int flags)
35 {
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;
45
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))
51     {
52       if (*++errhand == '\0')
53         errhand = NULL;
54       else
55         {
56           /* Make copy without the error handling description.  */
57           char *newtoset = (char *) alloca (errhand - toset + 1);
58           char *tok;
59           char *ptr;
60
61           newtoset[errhand - toset] = '\0';
62           toset = memcpy (newtoset, toset, errhand - toset);
63
64           /* Find the appropriate transliteration handlers.  */
65           tok = strdupa (errhand);
66
67           tok = __strtok_r (tok, ",", &ptr);
68           while (tok != NULL)
69             {
70               if (__strcasecmp_l (tok, "TRANSLIT", &_nl_C_locobj) == 0)
71                 {
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;
77
78                   for (runp = trans; runp != NULL; runp = runp->next)
79                     if (runp->trans_fct == __gconv_transliterate)
80                       break;
81                     else
82                       lastp = runp;
83
84                   if (runp == NULL)
85                     {
86                       struct trans_struct *newp;
87
88                       newp = (struct trans_struct *) alloca (sizeof (*newp));
89                       memset (newp, '\0', sizeof (*newp));
90
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;
96
97                       if (lastp == NULL)
98                         trans = newp;
99                       else
100                         lastp->next = newp;
101                     }
102                 }
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
107                 {
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;
113
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;
121
122                   if (runp == NULL)
123                     {
124                       struct trans_struct *newp;
125
126                       newp = (struct trans_struct *) alloca (sizeof (*newp));
127                       memset (newp, '\0', sizeof (*newp));
128                       newp->name = tok;
129
130                       if (lastp == NULL)
131                         trans = newp;
132                       else
133                         lastp->next = newp;
134                     }
135                 }
136
137               tok = __strtok_r (NULL, ",", &ptr);
138             }
139         }
140     }
141
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')
147     {
148       char *newfromset = (char *) alloca (ignore - fromset + 1);
149
150       newfromset[ignore - fromset] = '\0';
151       fromset = memcpy (newfromset, fromset, ignore - fromset);
152     }
153
154   /* If the string is empty define this to mean the charset of the
155      currently selected locale.  */
156   if (strcmp (toset, "//") == 0)
157     {
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);
163     }
164   if (strcmp (fromset, "//") == 0)
165     {
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);
171     }
172
173   res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
174   if (res == __GCONV_OK)
175     {
176       /* Find the modules.  */
177       struct trans_struct *lastp = NULL;
178       struct trans_struct *runp;
179
180       for (runp = trans; runp != NULL; runp = runp->next)
181         {
182           if (runp->name == NULL
183               || __builtin_expect (__gconv_translit_find (runp), 0) == 0)
184             lastp = runp;
185           else
186             {
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;
192             }
193         }
194
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
202         {
203           size_t n;
204
205           /* Remember the list of steps.  */
206           result->__steps = steps;
207           result->__nsteps = nsteps;
208
209           /* Clear the array for the step data.  */
210           memset (result->__data, '\0',
211                   nsteps * sizeof (struct __gconv_step_data));
212
213           /* Call all initialization functions for the transformation
214              step implementations.  */
215           for (cnt = 0; cnt < nsteps; ++cnt)
216             {
217               size_t size;
218
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;
224
225               /* It's a regular use.  */
226               result->__data[cnt].__internal_use = 0;
227 #endif
228
229               /* We use the `mbstate_t' member in DATA.  */
230               result->__data[cnt].__statep = &result->__data[cnt].__state;
231
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)
238                     {
239                       void *data = NULL;
240
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))
246                         {
247                           /* Append at the end of the list.  */
248                           struct __gconv_trans_data *newp;
249                           struct __gconv_trans_data **lastp;
250
251                           newp = (struct __gconv_trans_data *)
252                             malloc (sizeof (struct __gconv_trans_data));
253                           if (newp == NULL)
254                             {
255                               res = __GCONV_NOMEM;
256                               goto bail;
257                             }
258
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;
264
265                           lastp = &result->__data[cnt].__trans;
266                           while (*lastp != NULL)
267                             lastp = &(*lastp)->__next;
268
269                           *lastp = newp;
270                         }
271                       break;
272                     }
273
274               /* If this is the last step we must not allocate an
275                  output buffer.  */
276               if (cnt < nsteps - 1)
277                 {
278                   result->__data[cnt].__flags = conv_flags;
279
280                   /* Allocate the buffer.  */
281                   size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
282
283                   result->__data[cnt].__outbuf = malloc (size);
284                   if (result->__data[cnt].__outbuf == NULL)
285                     {
286                       res = __GCONV_NOMEM;
287                       goto bail;
288                     }
289
290                   result->__data[cnt].__outbufend =
291                     result->__data[cnt].__outbuf + size;
292                 }
293               else
294                 {
295                   /* Handle the last entry.  */
296                   result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
297
298                   break;
299                 }
300             }
301         }
302
303       if (res != __GCONV_OK)
304         {
305           /* Something went wrong.  Free all the resources.  */
306           int serrno;
307         bail:
308           serrno = errno;
309
310           if (result != NULL)
311             {
312               while (cnt-- > 0)
313                 {
314                   struct __gconv_trans_data *transp;
315
316                   transp = result->__data[cnt].__trans;
317                   while (transp != NULL)
318                     {
319                       struct __gconv_trans_data *curp = transp;
320                       transp = transp->__next;
321
322                       if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
323                         curp->__trans_end_fct (curp->__data);
324
325                       free (curp);
326                     }
327
328                   free (result->__data[cnt].__outbuf);
329                 }
330
331               free (result);
332               result = NULL;
333             }
334
335           __gconv_close_transform (steps, nsteps);
336
337           __set_errno (serrno);
338         }
339     }
340
341   *handle = result;
342   return res;
343 }