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   int conv_flags = 0;
39   const char *errhand;
40
41   /* Find out whether any error handling method is specified.  */
42   errhand = strchr (toset, '/');
43   if (errhand != NULL)
44     errhand = strchr (errhand + 1, '/');
45   if (__builtin_expect (errhand != NULL, 1))
46     {
47       if (errhand[1] == '\0')
48         errhand = NULL;
49       else
50         {
51           /* Make copy without the error handling description.  */
52           char *newtoset = (char *) alloca (errhand - toset + 1);
53
54           newtoset[errhand - toset] = '\0';
55           toset = memcpy (newtoset, toset, errhand - toset);
56
57           flags = __GCONV_IGNORE_ERRORS;
58
59           if (strcasecmp (errhand, "IGNORE") == 0)
60             {
61               /* Found it.  This means we should ignore conversion errors.  */
62               flags = __GCONV_IGNORE_ERRORS;
63               errhand = NULL;
64             }
65         }
66     }
67
68   res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
69   if (res == __GCONV_OK)
70     {
71       const char **csnames = NULL;
72       size_t ncsnames = 0;
73       __gconv_trans_fct trans_fct = NULL;
74       __gconv_trans_context_fct trans_context_fct = NULL;
75       __gconv_trans_init_fct trans_init_fct = NULL;
76       __gconv_trans_end_fct trans_end_fct = NULL;
77
78       if (errhand != NULL)
79         {
80           /* Find the appropriate transliteration handling.  */
81           if (strcasecmp (errhand, "TRANSLIT") == 0)
82             {
83               /* It's the builtin transliteration handling.  We only
84                  suport for it working on the internal encoding.  */
85               static const char *internal_trans_names[1] = { "INTERNAL" };
86
87               csnames = internal_trans_names;
88               ncsnames = 1;
89               trans_fct = __gconv_transliterate;
90               /* No context, init, or end function.  */
91             }
92           else if (strcasecmp (errhand, "WORK AROUND A GCC BUG") == 0)
93             {
94               trans_init_fct = (__gconv_trans_init_fct) 1;
95             }
96         }
97
98       /* Allocate room for handle.  */
99       result = (__gconv_t) malloc (sizeof (struct __gconv_info)
100                                    + (nsteps
101                                       * sizeof (struct __gconv_step_data)));
102       if (result == NULL)
103         res = __GCONV_NOMEM;
104       else
105         {
106           size_t n;
107
108           /* Remember the list of steps.  */
109           result->__steps = steps;
110           result->__nsteps = nsteps;
111
112           /* Clear the array for the step data.  */
113           memset (result->__data, '\0',
114                   nsteps * sizeof (struct __gconv_step_data));
115
116           /* Call all initialization functions for the transformation
117              step implementations.  */
118           for (cnt = 0; cnt < nsteps - 1; ++cnt)
119             {
120               size_t size;
121
122               /* Would have to be done if we would not clear the whole
123                  array above.  */
124               /* If this is the last step we must not allocate an
125                  output buffer.  */
126               result->__data[cnt].__flags = conv_flags;
127
128 #if 0
129               /* Reset the counter.  */
130               result->__data[cnt].__invocation_counter = 0;
131
132               /* It's a regular use.  */
133               result->__data[cnt].__internal_use = 0;
134 #endif
135
136               /* We use the `mbstate_t' member in DATA.  */
137               result->__data[cnt].__statep = &result->__data[cnt].__state;
138
139               /* Allocate the buffer.  */
140               size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
141
142               result->__data[cnt].__outbuf = (char *) malloc (size);
143               if (result->__data[cnt].__outbuf == NULL)
144                 {
145                   res = __GCONV_NOMEM;
146                   break;
147                 }
148               result->__data[cnt].__outbufend =
149                 result->__data[cnt].__outbuf + size;
150
151               /* Now see whether we can use the transliteration module
152                  for this step.  */
153               for (n = 0; n < ncsnames; ++n)
154                 if (strcasecmp (steps[cnt].__from_name, csnames[n]) == 0)
155                   {
156                     /* Match!  Now try the initializer.  */
157                     if (trans_init_fct == NULL
158                         || (trans_init_fct (&result->__data[cnt].__trans.__data,
159                                             steps[cnt].__to_name)
160                             == __GCONV_OK))
161                       {
162                         result->__data[cnt].__trans.__trans_fct = trans_fct;
163                         result->__data[cnt].__trans.__trans_context_fct =
164                           trans_context_fct;
165                         result->__data[cnt].__trans.__trans_end_fct =
166                           trans_end_fct;
167                       }
168                     break;
169                   }
170             }
171
172           /* Now handle the last entry.  */
173           result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
174           /* Would have to be done if we would not clear the whole
175              array above.  */
176 #if 0
177           result->__data[cnt].__invocation_counter = 0;
178           result->__data[cnt].__internal_use = 0;
179 #endif
180           result->__data[cnt].__statep = &result->__data[cnt].__state;
181
182           /* Now see whether we can use the transliteration module
183              for this step.  */
184           for (n = 0; n < ncsnames; ++n)
185             if (strcasecmp (steps[cnt].__from_name, csnames[n]) == 0)
186               {
187                 /* Match!  Now try the initializer.  */
188                 if (trans_init_fct == NULL
189                     || trans_init_fct (&result->__data[cnt].__trans.__data,
190                                        steps[cnt].__to_name)
191                     == __GCONV_OK)
192                   {
193                     result->__data[cnt].__trans.__trans_fct = trans_fct;
194                     result->__data[cnt].__trans.__trans_context_fct =
195                       trans_context_fct;
196                     result->__data[cnt].__trans.__trans_end_fct =
197                       trans_end_fct;
198                   }
199                 break;
200               }
201         }
202
203       if (res != __GCONV_OK)
204         {
205           /* Something went wrong.  Free all the resources.  */
206           int serrno = errno;
207
208           if (result != NULL)
209             {
210               while (cnt-- > 0)
211                 free (result->__data[cnt].__outbuf);
212
213               free (result);
214               result = NULL;
215             }
216
217           __gconv_close_transform (steps, nsteps);
218
219           __set_errno (serrno);
220         }
221     }
222
223   *handle = result;
224   return res;
225 }