Update install.texi, and regenerate INSTALL.
[glibc.git] / iconv / gconv_open.c
blob56bc390d73ce336bdc374b78bea51b6cfe82f5a6
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2021 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 Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the 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 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <https://www.gnu.org/licenses/>. */
20 #include <errno.h>
21 #include <locale.h>
22 #include "../locale/localeinfo.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include <gconv_int.h>
29 /* How many character should be converted in one call? */
30 #define GCONV_NCHAR_GOAL 8160
33 int
34 __gconv_open (struct gconv_spec *conv_spec, __gconv_t *handle,
35 int flags)
37 struct __gconv_step *steps;
38 size_t nsteps;
39 __gconv_t result = NULL;
40 size_t cnt = 0;
41 int res;
42 int conv_flags = 0;
43 bool translit = false;
44 char *tocode, *fromcode;
46 /* Find out whether any error handling method is specified. */
47 translit = conv_spec->translit;
49 if (conv_spec->ignore)
50 conv_flags |= __GCONV_IGNORE_ERRORS;
52 tocode = conv_spec->tocode;
53 fromcode = conv_spec->fromcode;
55 /* If the string is empty define this to mean the charset of the
56 currently selected locale. */
57 if (strcmp (tocode, "//") == 0)
59 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
60 size_t len = strlen (codeset);
61 char *dest;
62 tocode = dest = (char *) alloca (len + 3);
63 memcpy (__mempcpy (dest, codeset, len), "//", 3);
65 if (strcmp (fromcode, "//") == 0)
67 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
68 size_t len = strlen (codeset);
69 char *dest;
70 fromcode = dest = (char *) alloca (len + 3);
71 memcpy (__mempcpy (dest, codeset, len), "//", 3);
74 res = __gconv_find_transform (tocode, fromcode, &steps, &nsteps, flags);
75 if (res == __GCONV_OK)
77 /* Allocate room for handle. */
78 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
79 + (nsteps
80 * sizeof (struct __gconv_step_data)));
81 if (result == NULL)
82 res = __GCONV_NOMEM;
83 else
85 /* Remember the list of steps. */
86 result->__steps = steps;
87 result->__nsteps = nsteps;
89 /* Clear the array for the step data. */
90 memset (result->__data, '\0',
91 nsteps * sizeof (struct __gconv_step_data));
93 /* Call all initialization functions for the transformation
94 step implementations. */
95 for (cnt = 0; cnt < nsteps; ++cnt)
97 size_t size;
99 /* Would have to be done if we would not clear the whole
100 array above. */
101 #if 0
102 /* Reset the counter. */
103 result->__data[cnt].__invocation_counter = 0;
105 /* It's a regular use. */
106 result->__data[cnt].__internal_use = 0;
107 #endif
109 /* We use the `mbstate_t' member in DATA. */
110 result->__data[cnt].__statep = &result->__data[cnt].__state;
112 /* The builtin transliteration handling only
113 supports the internal encoding. */
114 if (translit
115 && __strcasecmp_l (steps[cnt].__from_name,
116 "INTERNAL", _nl_C_locobj_ptr) == 0)
117 conv_flags |= __GCONV_TRANSLIT;
119 /* If this is the last step we must not allocate an
120 output buffer. */
121 if (cnt < nsteps - 1)
123 result->__data[cnt].__flags = conv_flags;
125 /* Allocate the buffer. */
126 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
128 result->__data[cnt].__outbuf = malloc (size);
129 if (result->__data[cnt].__outbuf == NULL)
131 res = __GCONV_NOMEM;
132 goto bail;
135 result->__data[cnt].__outbufend =
136 result->__data[cnt].__outbuf + size;
138 else
140 /* Handle the last entry. */
141 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
143 break;
148 if (res != __GCONV_OK)
150 /* Something went wrong. Free all the resources. */
151 int serrno;
152 bail:
153 serrno = errno;
155 if (result != NULL)
157 while (cnt-- > 0)
158 free (result->__data[cnt].__outbuf);
160 free (result);
161 result = NULL;
164 __gconv_close_transform (steps, nsteps);
166 __set_errno (serrno);
170 *handle = result;
171 return res;
173 libc_hidden_def (__gconv_open)