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 *runp;
40
41   /* Find out whether "IGNORE" is part of the options in the `toset'
42      name.  If yes, remove the string and remember this in the flag.  */
43   runp = __strchrnul (__strchrnul (toset, '/'), '/');
44   if (strcmp (runp, "IGNORE") == 0)
45     {
46       /* Found it.  This means we should ignore conversion errors.  */
47       char *newtoset = (char *) alloca (runp - toset + 1);
48
49       newtoset[runp - toset] = '\0';
50       toset = memcpy (newtoset, toset, runp - toset);
51
52       flags = __GCONV_IGNORE_ERRORS;
53     }
54
55   res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
56   if (res == __GCONV_OK)
57     {
58       /* Allocate room for handle.  */
59       result = (__gconv_t) malloc (sizeof (struct __gconv_info)
60                                    + (nsteps
61                                       * sizeof (struct __gconv_step_data)));
62       if (result == NULL)
63         res = __GCONV_NOMEM;
64       else
65         {
66           /* Remember the list of steps.  */
67           result->__steps = steps;
68           result->__nsteps = nsteps;
69
70           /* Clear the array for the step data.  */
71           memset (result->__data, '\0',
72                   nsteps * sizeof (struct __gconv_step_data));
73
74           /* Call all initialization functions for the transformation
75              step implementations.  */
76           for (cnt = 0; cnt < nsteps - 1; ++cnt)
77             {
78               size_t size;
79
80               /* Would have to be done if we would not clear the whole
81                  array above.  */
82               /* If this is the last step we must not allocate an
83                  output buffer.  */
84               result->__data[cnt].__flags = conv_flags;
85
86 #if 0
87               /* Reset the counter.  */
88               result->__data[cnt].__invocation_counter = 0;
89
90               /* It's a regular use.  */
91               result->__data[cnt].__internal_use = 0;
92 #endif
93
94               /* We use the `mbstate_t' member in DATA.  */
95               result->__data[cnt].__statep = &result->__data[cnt].__state;
96
97               /* Allocate the buffer.  */
98               size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
99
100               result->__data[cnt].__outbuf = (char *) malloc (size);
101               if (result->__data[cnt].__outbuf == NULL)
102                 {
103                   res = __GCONV_NOMEM;
104                   break;
105                 }
106               result->__data[cnt].__outbufend =
107                 result->__data[cnt].__outbuf + size;
108             }
109
110           /* Now handle the last entry.  */
111           result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
112           /* Would have to be done if we would not clear the whole
113              array above.  */
114 #if 0
115           result->__data[cnt].__invocation_counter = 0;
116           result->__data[cnt].__internal_use = 0;
117 #endif
118           result->__data[cnt].__statep = &result->__data[cnt].__state;
119         }
120
121       if (res != __GCONV_OK)
122         {
123           /* Something went wrong.  Free all the resources.  */
124           int serrno = errno;
125
126           if (result != NULL)
127             {
128               while (cnt-- > 0)
129                 free (result->__data[cnt].__outbuf);
130
131               free (result);
132               result = NULL;
133             }
134
135           __gconv_close_transform (steps, nsteps);
136
137           __set_errno (serrno);
138         }
139     }
140
141   *handle = result;
142   return res;
143 }