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