Update.
[platform/upstream/glibc.git] / iconv / gconv_open.c
1 /* Find matching transformation algorithms and initialize steps.
2    Copyright (C) 1997, 1998, 1999 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
24 #include <gconv_int.h>
25
26
27 int
28 internal_function
29 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle)
30 {
31   struct __gconv_step *steps;
32   size_t nsteps;
33   __gconv_t result = NULL;
34   size_t cnt = 0;
35   int res;
36
37   res = __gconv_find_transform (toset, fromset, &steps, &nsteps);
38   if (res == __GCONV_OK)
39     {
40       /* Allocate room for handle.  */
41       result = (__gconv_t) malloc (sizeof (struct __gconv_info)
42                                    + (nsteps
43                                       * sizeof (struct __gconv_step_data)));
44       if (result == NULL)
45         res = __GCONV_NOMEM;
46       else
47         {
48           /* Remember the list of steps.  */
49           result->__steps = steps;
50           result->__nsteps = nsteps;
51
52           /* Clear the array for the step data.  */
53           memset (result->__data, '\0',
54                   nsteps * sizeof (struct __gconv_step_data));
55
56           /* Call all initialization functions for the transformation
57              step implemenations.  */
58           for (cnt = 0; cnt < nsteps; ++cnt)
59             {
60               /* If this is the last step we must not allocate an
61                  output buffer.  */
62               result->__data[cnt].__is_last = cnt == nsteps - 1;
63
64               /* Reset the counter.  */
65               result->__data[cnt].__invocation_counter = 0;
66
67               /* It's a regular use.  */
68               result->__data[cnt].__internal_use = 0;
69
70               /* We use the `mbstate_t' member in DATA.  */
71               result->__data[cnt].__statep = &result->__data[cnt].__state;
72
73               /* Allocate the buffer.  */
74               if (!result->__data[cnt].__is_last)
75                 {
76                   size_t size = (GCONV_NCHAR_GOAL
77                                  * steps[cnt].__max_needed_to);
78
79                   result->__data[cnt].__outbuf = (char *) malloc (size);
80                   if (result->__data[cnt].__outbuf == NULL)
81                     {
82                       res = __GCONV_NOMEM;
83                       break;
84                     }
85                   result->__data[cnt].__outbufend =
86                     result->__data[cnt].__outbuf + size;
87                 }
88             }
89         }
90     }
91
92   if (res != __GCONV_OK)
93     {
94       /* Something went wrong.  Free all the resources.  */
95       int serrno = errno;
96
97       if (result != NULL)
98         {
99           while (cnt-- > 0)
100             free (result->__data[cnt].__outbuf);
101
102           free (result);
103           result = NULL;
104         }
105
106       __gconv_close_transform (steps, nsteps);
107
108       __set_errno (serrno);
109     }
110
111   *handle = result;
112   return res;
113 }