Update.
[platform/upstream/glibc.git] / iconv / gconv_open.c
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.
5
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.
10
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.
15
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.  */
20
21 #include <errno.h>
22 #include <locale.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <gconv_int.h>
27
28
29 int
30 internal_function
31 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
32               int flags)
33 {
34   struct __gconv_step *steps;
35   size_t nsteps;
36   __gconv_t result = NULL;
37   size_t cnt = 0;
38   int res;
39   int conv_flags = 0;
40   const char *errhand;
41   const char *ignore;
42   struct trans_struct *trans = NULL;
43
44   /* Find out whether any error handling method is specified.  */
45   errhand = strchr (toset, '/');
46   if (errhand != NULL)
47     errhand = strchr (errhand + 1, '/');
48   if (__builtin_expect (errhand != NULL, 1))
49     {
50       if (*++errhand == '\0')
51         errhand = NULL;
52       else
53         {
54           /* Make copy without the error handling description.  */
55           char *newtoset = (char *) alloca (errhand - toset + 1);
56           char *tok;
57           char *ptr;
58
59           newtoset[errhand - toset] = '\0';
60           toset = memcpy (newtoset, toset, errhand - toset);
61
62           /* Find the appropriate transliteration handlers.  */
63           tok = strdupa (errhand);
64
65           tok = __strtok_r (tok, ",", &ptr);
66           while (tok != NULL)
67             {
68               if (__strcasecmp_l (tok, "TRANSLIT", &_nl_C_locobj) == 0)
69                 {
70                   /* It's the builtin transliteration handling.  We only
71                      support it for working on the internal encoding.  */
72                   static const char *internal_trans_names[1] = { "INTERNAL" };
73                   struct trans_struct *lastp = NULL;
74                   struct trans_struct *runp;
75
76                   for (runp = trans; runp != NULL; runp = runp->next)
77                     if (runp->trans_fct == __gconv_transliterate)
78                       break;
79                     else
80                       lastp = runp;
81
82                   if (runp == NULL)
83                     {
84                       struct trans_struct *newp;
85
86                       newp = (struct trans_struct *) alloca (sizeof (*newp));
87                       memset (newp, '\0', sizeof (*newp));
88
89                       /* We leave the `name' field zero to signal that
90                          this is an internal transliteration step.  */
91                       newp->csnames = internal_trans_names;
92                       newp->ncsnames = 1;
93                       newp->trans_fct = __gconv_transliterate;
94
95                       if (lastp == NULL)
96                         trans = newp;
97                       else
98                         lastp->next = newp;
99                     }
100                 }
101               else if (__strcasecmp_l (tok, "IGNORE", &_nl_C_locobj) == 0)
102                 /* Set the flag to ignore all errors.  */
103                 conv_flags |= __GCONV_IGNORE_ERRORS;
104               else
105                 {
106                   /* `tok' is possibly a module name.  We'll see later
107                      whether we can find it.  But first see that we do
108                      not already a module of this name.  */
109                   struct trans_struct *lastp = NULL;
110                   struct trans_struct *runp;
111
112                   for (runp = trans; runp != NULL; runp = runp->next)
113                     if (runp->name != NULL
114                         && __strcasecmp_l (tok, runp->name,
115                                            &_nl_C_locobj) == 0)
116                       break;
117                     else
118                       lastp = runp;
119
120                   if (runp == NULL)
121                     {
122                       struct trans_struct *newp;
123
124                       newp = (struct trans_struct *) alloca (sizeof (*newp));
125                       memset (newp, '\0', sizeof (*newp));
126                       newp->name = tok;
127
128                       if (lastp == NULL)
129                         trans = newp;
130                       else
131                         lastp->next = newp;
132                     }
133                 }
134
135               tok = __strtok_r (NULL, ",", &ptr);
136             }
137         }
138     }
139
140   /* For the source character set we ignore the error handler specification.
141      XXX Is this really always the best?  */
142   ignore = strchr (fromset, '/');
143   if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
144       && *++ignore != '\0')
145     {
146       char *newfromset = (char *) alloca (ignore - fromset + 1);
147
148       newfromset[ignore - fromset] = '\0';
149       fromset = memcpy (newfromset, fromset, ignore - fromset);
150     }
151
152   res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
153   if (res == __GCONV_OK)
154     {
155       /* Find the modules.  */
156       struct trans_struct *lastp = NULL;
157       struct trans_struct *runp;
158
159       for (runp = trans; runp != NULL; runp = runp->next)
160         {
161           if (runp->name == NULL
162               || __builtin_expect (__gconv_translit_find (runp), 0) == 0)
163             lastp = runp;
164           else
165             /* This means we haven't found the module.  Remove it.  */
166             (lastp == NULL ? trans : lastp->next) = runp->next;
167         }
168
169       /* Allocate room for handle.  */
170       result = (__gconv_t) malloc (sizeof (struct __gconv_info)
171                                    + (nsteps
172                                       * sizeof (struct __gconv_step_data)));
173       if (result == NULL)
174         res = __GCONV_NOMEM;
175       else
176         {
177           size_t n;
178
179           /* Remember the list of steps.  */
180           result->__steps = steps;
181           result->__nsteps = nsteps;
182
183           /* Clear the array for the step data.  */
184           memset (result->__data, '\0',
185                   nsteps * sizeof (struct __gconv_step_data));
186
187           /* Call all initialization functions for the transformation
188              step implementations.  */
189           for (cnt = 0; cnt < nsteps; ++cnt)
190             {
191               size_t size;
192
193               /* Would have to be done if we would not clear the whole
194                  array above.  */
195 #if 0
196               /* Reset the counter.  */
197               result->__data[cnt].__invocation_counter = 0;
198
199               /* It's a regular use.  */
200               result->__data[cnt].__internal_use = 0;
201 #endif
202
203               /* We use the `mbstate_t' member in DATA.  */
204               result->__data[cnt].__statep = &result->__data[cnt].__state;
205
206               /* Now see whether we can use any of the transliteration
207                  modules for this step.  */
208               for (runp = trans; runp != NULL; runp = runp->next)
209                 for (n = 0; n < runp->ncsnames; ++n)
210                   if (__strcasecmp_l (steps[cnt].__from_name,
211                                       runp->csnames[n], &_nl_C_locobj) == 0)
212                     {
213                       void *data = NULL;
214
215                       /* Match!  Now try the initializer.  */
216                       if (runp->trans_init_fct == NULL
217                           || (runp->trans_init_fct (&data,
218                                                     steps[cnt].__to_name)
219                               == __GCONV_OK))
220                         {
221                           /* Append at the end of the list.  */
222                           struct __gconv_trans_data *newp;
223                           struct __gconv_trans_data **lastp;
224
225                           newp = (struct __gconv_trans_data *)
226                             malloc (sizeof (struct __gconv_trans_data));
227                           if (newp == NULL)
228                             {
229                               res = __GCONV_NOMEM;
230                               goto bail;
231                             }
232
233                           newp->__trans_fct = runp->trans_fct;
234                           newp->__trans_context_fct = runp->trans_context_fct;
235                           newp->__trans_end_fct = runp->trans_end_fct;
236                           newp->__data = data;
237                           newp->__next = NULL;
238
239                           lastp = &result->__data[cnt].__trans;
240                           while (*lastp != NULL)
241                             lastp = &(*lastp)->__next;
242
243                           *lastp = newp;
244                         }
245                       break;
246                     }
247
248               /* If this is the last step we must not allocate an
249                  output buffer.  */
250               if (cnt < nsteps - 1)
251                 {
252                   result->__data[cnt].__flags = conv_flags;
253
254                   /* Allocate the buffer.  */
255                   size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
256
257                   result->__data[cnt].__outbuf = (char *) malloc (size);
258                   if (result->__data[cnt].__outbuf == NULL)
259                     {
260                       res = __GCONV_NOMEM;
261                       goto bail;
262                     }
263
264                   result->__data[cnt].__outbufend =
265                     result->__data[cnt].__outbuf + size;
266                 }
267               else
268                 {
269                   /* Handle the last entry.  */
270                   result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
271
272                   break;
273                 }
274             }
275         }
276
277       if (res != __GCONV_OK)
278         {
279           /* Something went wrong.  Free all the resources.  */
280           int serrno;
281         bail:
282           serrno = errno;
283
284           if (result != NULL)
285             {
286               while (cnt-- > 0)
287                 {
288                   struct __gconv_trans_data *transp;
289
290                   transp = result->__data[cnt].__trans;
291                   while (transp != NULL)
292                     {
293                       struct __gconv_trans_data *curp = transp;
294                       transp = transp->__next;
295
296                       if (__builtin_expect (curp->__trans_end_fct != NULL, 0))
297                         curp->__trans_end_fct (curp->__data);
298
299                       free (curp);
300                     }
301
302                   free (result->__data[cnt].__outbuf);
303                 }
304
305               free (result);
306               result = NULL;
307             }
308
309           __gconv_close_transform (steps, nsteps);
310
311           __set_errno (serrno);
312         }
313     }
314
315   *handle = result;
316   return res;
317 }