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.
5
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.
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 Lesser General Public License for more details.
15
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/>. */
19
20#include <errno.h>
21#include <locale.h>
22#include "../locale/localeinfo.h"
23#include <stdlib.h>
24#include <string.h>
25
26#include <gconv_int.h>
27
28
29int
30internal_function
31__gconv_open (const char *toset, const char *fromset, __gconv_t *handle,
32 int flags)
33{
34 struct __gconv_step *steps;
35 size_t nsteps;
36 __gconv_t result = NULL;
37 size_t cnt = 0;
38 int res;
39 int conv_flags = 0;
40 const char *errhand;
41 const char *ignore;
42 bool translit = false;
43
44 /* Find out whether any error handling method is specified. */
45 errhand = strchr (toset, '/');
46 if (errhand != NULL)
47 errhand = strchr (errhand + 1, '/');
48 if (__glibc_likely (errhand != NULL))
49 {
50 if (*++errhand == '\0')
51 errhand = NULL;
52 else
53 {
54 /* Make copy without the error handling description. */
55 char *newtoset = (char *) alloca (errhand - toset + 1);
56 char *tok;
57 char *ptr = NULL /* Work around a bogus warning */;
58
59 newtoset[errhand - toset] = '\0';
60 toset = memcpy (newtoset, toset, errhand - toset);
61
62 /* Find the appropriate transliteration handlers. */
63 tok = strdupa (errhand);
64
65 tok = __strtok_r (tok, ",", &ptr);
66 while (tok != NULL)
67 {
68 if (__strcasecmp_l (tok, "TRANSLIT", _nl_C_locobj_ptr) == 0)
69 translit = true;
70 else if (__strcasecmp_l (tok, "IGNORE", _nl_C_locobj_ptr) == 0)
71 /* Set the flag to ignore all errors. */
72 conv_flags |= __GCONV_IGNORE_ERRORS;
73
74 tok = __strtok_r (NULL, ",", &ptr);
75 }
76 }
77 }
78
79 /* For the source character set we ignore the error handler specification.
80 XXX Is this really always the best? */
81 ignore = strchr (fromset, '/');
82 if (ignore != NULL && (ignore = strchr (ignore + 1, '/')) != NULL
83 && *++ignore != '\0')
84 {
85 char *newfromset = (char *) alloca (ignore - fromset + 1);
86
87 newfromset[ignore - fromset] = '\0';
88 fromset = memcpy (newfromset, fromset, ignore - fromset);
89 }
90
91 /* If the string is empty define this to mean the charset of the
92 currently selected locale. */
93 if (strcmp (toset, "//") == 0)
94 {
95 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
96 size_t len = strlen (codeset);
97 char *dest;
98 toset = dest = (char *) alloca (len + 3);
99 memcpy (__mempcpy (dest, codeset, len), "//", 3);
100 }
101 if (strcmp (fromset, "//") == 0)
102 {
103 const char *codeset = _NL_CURRENT (LC_CTYPE, CODESET);
104 size_t len = strlen (codeset);
105 char *dest;
106 fromset = dest = (char *) alloca (len + 3);
107 memcpy (__mempcpy (dest, codeset, len), "//", 3);
108 }
109
110 res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags);
111 if (res == __GCONV_OK)
112 {
113 /* Allocate room for handle. */
114 result = (__gconv_t) malloc (sizeof (struct __gconv_info)
115 + (nsteps
116 * sizeof (struct __gconv_step_data)));
117 if (result == NULL)
118 res = __GCONV_NOMEM;
119 else
120 {
121 /* Remember the list of steps. */
122 result->__steps = steps;
123 result->__nsteps = nsteps;
124
125 /* Clear the array for the step data. */
126 memset (result->__data, '\0',
127 nsteps * sizeof (struct __gconv_step_data));
128
129 /* Call all initialization functions for the transformation
130 step implementations. */
131 for (cnt = 0; cnt < nsteps; ++cnt)
132 {
133 size_t size;
134
135 /* Would have to be done if we would not clear the whole
136 array above. */
137#if 0
138 /* Reset the counter. */
139 result->__data[cnt].__invocation_counter = 0;
140
141 /* It's a regular use. */
142 result->__data[cnt].__internal_use = 0;
143#endif
144
145 /* We use the `mbstate_t' member in DATA. */
146 result->__data[cnt].__statep = &result->__data[cnt].__state;
147
148 /* The builtin transliteration handling only
149 supports the internal encoding. */
150 if (translit
151 && __strcasecmp_l (steps[cnt].__from_name,
152 "INTERNAL", _nl_C_locobj_ptr) == 0)
153 conv_flags |= __GCONV_TRANSLIT;
154
155 /* If this is the last step we must not allocate an
156 output buffer. */
157 if (cnt < nsteps - 1)
158 {
159 result->__data[cnt].__flags = conv_flags;
160
161 /* Allocate the buffer. */
162 size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to);
163
164 result->__data[cnt].__outbuf = malloc (size);
165 if (result->__data[cnt].__outbuf == NULL)
166 {
167 res = __GCONV_NOMEM;
168 goto bail;
169 }
170
171 result->__data[cnt].__outbufend =
172 result->__data[cnt].__outbuf + size;
173 }
174 else
175 {
176 /* Handle the last entry. */
177 result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST;
178
179 break;
180 }
181 }
182 }
183
184 if (res != __GCONV_OK)
185 {
186 /* Something went wrong. Free all the resources. */
187 int serrno;
188 bail:
189 serrno = errno;
190
191 if (result != NULL)
192 {
193 while (cnt-- > 0)
194 free (result->__data[cnt].__outbuf);
195
196 free (result);
197 result = NULL;
198 }
199
200 __gconv_close_transform (steps, nsteps);
201
202 __set_errno (serrno);
203 }
204 }
205
206 *handle = result;
207 return res;
208}
209