1 | /* Find matching transformation algorithms and initialize steps. |
2 | Copyright (C) 1997-2018 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 | |
29 | int |
30 | __gconv_open (const char *toset, const char *fromset, __gconv_t *handle, |
31 | int flags) |
32 | { |
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; |
42 | |
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)) |
48 | { |
49 | if (*++errhand == '\0') |
50 | errhand = NULL; |
51 | else |
52 | { |
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 */; |
57 | |
58 | newtoset[errhand - toset] = '\0'; |
59 | toset = memcpy (newtoset, toset, errhand - toset); |
60 | |
61 | /* Find the appropriate transliteration handlers. */ |
62 | tok = strdupa (errhand); |
63 | |
64 | tok = __strtok_r (tok, "," , &ptr); |
65 | while (tok != NULL) |
66 | { |
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; |
72 | |
73 | tok = __strtok_r (NULL, "," , &ptr); |
74 | } |
75 | } |
76 | } |
77 | |
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') |
83 | { |
84 | char *newfromset = (char *) alloca (ignore - fromset + 1); |
85 | |
86 | newfromset[ignore - fromset] = '\0'; |
87 | fromset = memcpy (newfromset, fromset, ignore - fromset); |
88 | } |
89 | |
90 | /* If the string is empty define this to mean the charset of the |
91 | currently selected locale. */ |
92 | if (strcmp (toset, "//" ) == 0) |
93 | { |
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); |
99 | } |
100 | if (strcmp (fromset, "//" ) == 0) |
101 | { |
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); |
107 | } |
108 | |
109 | res = __gconv_find_transform (toset, fromset, &steps, &nsteps, flags); |
110 | if (res == __GCONV_OK) |
111 | { |
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 |
119 | { |
120 | /* Remember the list of steps. */ |
121 | result->__steps = steps; |
122 | result->__nsteps = nsteps; |
123 | |
124 | /* Clear the array for the step data. */ |
125 | memset (result->__data, '\0', |
126 | nsteps * sizeof (struct __gconv_step_data)); |
127 | |
128 | /* Call all initialization functions for the transformation |
129 | step implementations. */ |
130 | for (cnt = 0; cnt < nsteps; ++cnt) |
131 | { |
132 | size_t size; |
133 | |
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; |
139 | |
140 | /* It's a regular use. */ |
141 | result->__data[cnt].__internal_use = 0; |
142 | #endif |
143 | |
144 | /* We use the `mbstate_t' member in DATA. */ |
145 | result->__data[cnt].__statep = &result->__data[cnt].__state; |
146 | |
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; |
153 | |
154 | /* If this is the last step we must not allocate an |
155 | output buffer. */ |
156 | if (cnt < nsteps - 1) |
157 | { |
158 | result->__data[cnt].__flags = conv_flags; |
159 | |
160 | /* Allocate the buffer. */ |
161 | size = (GCONV_NCHAR_GOAL * steps[cnt].__max_needed_to); |
162 | |
163 | result->__data[cnt].__outbuf = malloc (size); |
164 | if (result->__data[cnt].__outbuf == NULL) |
165 | { |
166 | res = __GCONV_NOMEM; |
167 | goto bail; |
168 | } |
169 | |
170 | result->__data[cnt].__outbufend = |
171 | result->__data[cnt].__outbuf + size; |
172 | } |
173 | else |
174 | { |
175 | /* Handle the last entry. */ |
176 | result->__data[cnt].__flags = conv_flags | __GCONV_IS_LAST; |
177 | |
178 | break; |
179 | } |
180 | } |
181 | } |
182 | |
183 | if (res != __GCONV_OK) |
184 | { |
185 | /* Something went wrong. Free all the resources. */ |
186 | int serrno; |
187 | bail: |
188 | serrno = errno; |
189 | |
190 | if (result != NULL) |
191 | { |
192 | while (cnt-- > 0) |
193 | free (result->__data[cnt].__outbuf); |
194 | |
195 | free (result); |
196 | result = NULL; |
197 | } |
198 | |
199 | __gconv_close_transform (steps, nsteps); |
200 | |
201 | __set_errno (serrno); |
202 | } |
203 | } |
204 | |
205 | *handle = result; |
206 | return res; |
207 | } |
208 | |