Update.
[glibc.git] / iconv / gconv_open.c
blob44cb5ccab55dd038fa6921bc4049ca4c2ab9f9ac
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.
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.
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.
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. */
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <gconv_int.h>
28 int
29 internal_function
30 __gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
31 int flags)
33 struct __gconv_step *steps;
34 size_t nsteps;
35 __gconv_t result = NULL;
36 size_t cnt = 0;
37 int res;
39 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
40 if (res == __GCONV_OK)
42 /* Allocate room for handle. */
43 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
44 + (nsteps
45 * sizeof (struct __gconv_step_data)));
46 if (result == NULL)
47 res = __GCONV_NOMEM;
48 else
50 /* Remember the list of steps. */
51 result->__steps = steps;
52 result->__nsteps = nsteps;
54 /* Clear the array for the step data. */
55 memset (result->__data, '\0',
56 nsteps * sizeof (struct __gconv_step_data));
58 /* Call all initialization functions for the transformation
59 step implemenations. */
60 for (cnt = 0; cnt < nsteps; ++cnt)
62 /* If this is the last step we must not allocate an
63 output buffer. */
64 result->__data[cnt].__is_last = cnt == nsteps - 1;
66 /* Reset the counter. */
67 result->__data[cnt].__invocation_counter = 0;
69 /* It's a regular use. */
70 result->__data[cnt].__internal_use = 0;
72 /* We use the `mbstate_t' member in DATA. */
73 result->__data[cnt].__statep = &result->__data[cnt].__state;
75 /* Allocate the buffer. */
76 if (!result->__data[cnt].__is_last)
78 size_t size = (GCONV_NCHAR_GOAL
79 * steps[cnt].__max_needed_to);
81 result->__data[cnt].__outbuf = (char *) malloc (size);
82 if (result->__data[cnt].__outbuf == NULL)
84 res = __GCONV_NOMEM;
85 break;
87 result->__data[cnt].__outbufend =
88 result->__data[cnt].__outbuf + size;
94 if (res != __GCONV_OK)
96 /* Something went wrong. Free all the resources. */
97 int serrno = errno;
99 if (result != NULL)
101 while (cnt-- > 0)
102 free (result->__data[cnt].__outbuf);
104 free (result);
105 result = NULL;
108 __gconv_close_transform (steps, nsteps);
110 __set_errno (serrno);
113 *handle = result;
114 return res;