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