Update.
[platform/upstream/glibc.git] / iconv / gconv_open.c
1 /* Find matching transformation algorithms and initialize steps.
2    Copyright (C) 1997 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 <gconv.h>
23 #include <stdlib.h>
24
25
26 int
27 __gconv_open (const char *toset, const char *fromset, gconv_t *handle)
28 {
29   struct gconv_step *steps;
30   size_t nsteps;
31   gconv_t result = NULL;
32   size_t cnt = 0;
33   int res;
34
35   res = __gconv_find_transform (toset, fromset, &steps, &nsteps);
36   if (res == GCONV_OK)
37     {
38       /* Allocate room for handle.  */
39       result = (gconv_t) malloc (sizeof (struct gconv_info));
40       if (result == NULL)
41         res = GCONV_NOMEM;
42       else
43         {
44           /* Remember the list of steps.  */
45           result->steps = steps;
46           result->nsteps = nsteps;
47
48           /* Allocate array for the step data.  */
49           result->data = (struct gconv_step_data *)
50             calloc (nsteps, sizeof (struct gconv_step_data));
51
52           if (result->data == NULL)
53             res = GCONV_NOMEM;
54           else
55             {
56               /* Call all initialization functions for the transformation
57                  step implemenations.  */
58               struct gconv_step_data *data = result->data;
59
60               for (cnt = 0; cnt < nsteps; ++cnt)
61                 {
62                   /* If this is the last step we must not allocate an output
63                      buffer.  Signal this to the initializer.  */
64                   data[cnt].is_last = cnt == nsteps - 1;
65
66                   if (steps[cnt].init_fct != NULL)
67                     {
68                       res = (steps[cnt].init_fct) (&steps[cnt], &data[cnt]);
69                       if (res != GCONV_OK)
70                         break;
71                     }
72
73                   if (!data[cnt].is_last && data[cnt].outbuf == NULL)
74                     {
75                       data[cnt].outbufsize = GCONV_DEFAULT_BUFSIZE;
76                       data[cnt].outbuf =
77                         (char *) malloc (data[cnt].outbufsize);
78                       if (data[cnt].outbuf == NULL)
79                         {
80                           res = GCONV_NOMEM;
81                           break;
82                         }
83                       data[cnt].outbufavail = 0;
84                     }
85                 }
86             }
87         }
88     }
89
90   if (res != GCONV_OK)
91     {
92       /* Something went wrong.  Free all the resources.  */
93       int serrno = errno;
94
95       if (result != NULL)
96         {
97           if (result->data != NULL)
98             {
99               while (cnt-- > 0)
100                 if (steps[cnt].end_fct != NULL)
101                   (*steps[cnt].end_fct) (&result->data[cnt]);
102                 else
103                   {
104                     free (result->data[cnt].outbuf);
105                     if (result->data[cnt].data != NULL)
106                       free (result->data[cnt].data);
107                   }
108
109               free (result->data);
110             }
111
112           free (result);
113           result = NULL;
114         }
115
116       __gconv_close_transform (steps, nsteps);
117
118       __set_errno (serrno);
119     }
120
121   *handle = result;
122   return res;
123 }