is_IS locale: Base collation on iso14651_t1 [BZ #22519]
[glibc.git] / iconv / gconv_open.c
blobd3495278083cc691e3e3b98f6c4a95a54e88a1ee
1 /* Find matching transformation algorithms and initialize steps.
2 Copyright (C) 1997-2017 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 <http://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 int
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;
38 int conv_flags = 0;
39 const char *errhand;
40 const char *ignore;
41 bool translit = false;
43 /* Find out whether any error handling method is specified. */
44 errhand = strchr (toset, '/');
45 if (errhand != NULL)
46 errhand = strchr (errhand + 1, '/');
47 if (__glibc_likely (errhand != NULL))
49 if (*++errhand == '\0')
50 errhand = NULL;
51 else
53 /* Make copy without the error handling description. */
54 char *newtoset = (char *) alloca (errhand - toset + 1);
55 char *tok;
56 char *ptr = NULL /* Work around a bogus warning */;
58 newtoset[errhand - toset] = '\0';
59 toset = memcpy (newtoset, toset, errhand - toset);
61 /* Find the appropriate transliteration handlers. */
62 tok = strdupa (errhand);
64 tok = __strtok_r (tok, ",", &ptr);
65 while (tok != NULL)
67 if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
68 translit = true;
69 else if (__strcasecmp_l (tok, "IGNORE", _nl_C_locobj_ptr) == 0)
70 /* Set the flag to ignore all errors. */
71 conv_flags |= __GCONV_IGNORE_ERRORS;
73 tok = __strtok_r (NULL, ",", &ptr);
78 /* For the source character set we ignore the error handler specification.
79 XXX Is this really always the best? */
80 ignore = strchr (fromset, '/');
81 if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
82 && *++ignore != '\0')
84 char *newfromset = (char *) alloca (ignore - fromset + 1);
86 newfromset[ignore - fromset] = '\0';
87 fromset = memcpy (newfromset, fromset, ignore - fromset);
90 /* If the string is empty define this to mean the charset of the
91 currently selected locale. */
92 if (strcmp (toset, "//") == 0)
94 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
95 size_t len = strlen (codeset);
96 char *dest;
97 toset = dest = (char *) alloca (len + 3);
98 memcpy (__mempcpy (dest, codeset, len), "//", 3);
100 if (strcmp (fromset, "//") == 0)
102 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
103 size_t len = strlen (codeset);
104 char *dest;
105 fromset = dest = (char *) alloca (len + 3);
106 memcpy (__mempcpy (dest, codeset, len), "//", 3);
109 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
110 if (res == __GCONV_OK)
112 /* Allocate room for handle. */
113 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
114 + (nsteps
115 * sizeof (struct __gconv_step_data)));
116 if (result == NULL)
117 res = __GCONV_NOMEM;
118 else
120 /* Remember the list of steps. */
121 result->__steps = steps;
122 result->__nsteps = nsteps;
124 /* Clear the array for the step data. */
125 memset (result->__data, '\0',
126 nsteps * sizeof (struct __gconv_step_data));
128 /* Call all initialization functions for the transformation
129 step implementations. */
130 for (cnt = 0; cnt < nsteps; ++cnt)
132 size_t size;
134 /* Would have to be done if we would not clear the whole
135 array above. */
136 #if 0
137 /* Reset the counter. */
138 result->__data[cnt].__invocation_counter = 0;
140 /* It's a regular use. */
141 result->__data[cnt].__internal_use = 0;
142 #endif
144 /* We use the `mbstate_t' member in DATA. */
145 result->__data[cnt].__statep = &result->__data[cnt].__state;
147 /* The builtin transliteration handling only
148 supports the internal encoding. */
149 if (translit
150 && __strcasecmp_l (steps[cnt].__from_name,
151 "INTERNAL", _nl_C_locobj_ptr) == 0)
152 conv_flags |= __GCONV_TRANSLIT;
154 /* If this is the last step we must not allocate an
155 output buffer. */
156 if (cnt < nsteps - 1)
158 result->__data[cnt].__flags = conv_flags;
160 /* Allocate the buffer. */
161 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
163 result->__data[cnt].__outbuf = malloc (size);
164 if (result->__data[cnt].__outbuf == NULL)
166 res = __GCONV_NOMEM;
167 goto bail;
170 result->__data[cnt].__outbufend =
171 result->__data[cnt].__outbuf + size;
173 else
175 /* Handle the last entry. */
176 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
178 break;
183 if (res != __GCONV_OK)
185 /* Something went wrong. Free all the resources. */
186 int serrno;
187 bail:
188 serrno = errno;
190 if (result != NULL)
192 while (cnt-- > 0)
193 free (result->__data[cnt].__outbuf);
195 free (result);
196 result = NULL;
199 __gconv_close_transform (steps, nsteps);
201 __set_errno (serrno);
205 *handle = result;
206 return res;