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