Debian Codegraph @

lib/striconv.c

raw
1 /* Charset conversion.
2 Copyright (C) 2001-2007, 2010-2025 Free Software Foundation, Inc.
3 Written by Bruno Haible and Simon Josefsson.
4
5 This file is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 This file is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #include <config.h>
19
20 /* Specification. */
21 #include "striconv.h"
22
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #if HAVE_ICONV
28 # include <iconv.h>
29 /* Get MB_LEN_MAX, CHAR_BIT. */
30 # include <limits.h>
31 #endif
32
33 #include "c-strcase.h"
34
35 #ifndef SIZE_MAX
36 # define SIZE_MAX ((size_t) -1)
37 #endif
38
39
40 #if HAVE_ICONV
41
42 int
43 mem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
44 char **resultp, size_t *lengthp)
45 {
46 # define tmpbufsize 4096
47 size_t length;
48 char *result;
49
50 /* Set to the initial state. */
51 iconv (cd, NULL, NULL, NULL, NULL);
52
53 /* Determine the length we need. */
54 {
55 size_t count = 0;
56 /* The alignment is needed when converting e.g. to glibc's WCHAR_T or
57 libiconv's UCS-4-INTERNAL encoding. */
58 union { unsigned int align; char buf[tmpbufsize]; } tmp;
59 # define tmpbuf tmp.buf
60 const char *inptr = src;
61 size_t insize = srclen;
62
63 while (insize > 0)
64 {
65 char *outptr = tmpbuf;
66 size_t outsize = tmpbufsize;
67 size_t res = iconv (cd,
68 (ICONV_CONST char **) &inptr, &insize,
69 &outptr, &outsize);
70
71 if (res == (size_t)(-1))
72 {
73 if (errno == E2BIG)
74 ;
75 else if (errno == EINVAL)
76 break;
77 else
78 return -1;
79 }
80 # if !(defined _LIBICONV_VERSION && !(_LIBICONV_VERSION == 0x10b && defined __APPLE__)) \
81 && !(defined __GLIBC__ && !defined __UCLIBC__)
82 /* Irix iconv() inserts a NUL byte if it cannot convert.
83 NetBSD iconv() inserts a question mark if it cannot convert.
84 Only GNU libiconv (excluding the bastard Apple iconv) and GNU libc
85 are known to prefer to fail rather than doing a lossy conversion. */
86 else if (res > 0)
87 {
88 errno = EILSEQ;
89 return -1;
90 }
91 # endif
92 count += outptr - tmpbuf;
93 }
94 {
95 char *outptr = tmpbuf;
96 size_t outsize = tmpbufsize;
97 size_t res = iconv (cd, NULL, NULL, &outptr, &outsize);
98
99 if (res == (size_t)(-1))
100 return -1;
101 count += outptr - tmpbuf;
102 }
103 length = count;
104 # undef tmpbuf
105 }
106
107 if (length == 0)
108 {
109 *lengthp = 0;
110 return 0;
111 }
112 if (*resultp != NULL && *lengthp >= length)
113 result = *resultp;
114 else
115 {
116 result = (char *) malloc (length);
117 if (result == NULL)
118 {
119 errno = ENOMEM;
120 return -1;
121 }
122 }
123
124 /* Return to the initial state. */
125 iconv (cd, NULL, NULL, NULL, NULL);
126
127 /* Do the conversion for real. */
128 {
129 const char *inptr = src;
130 size_t insize = srclen;
131 char *outptr = result;
132 size_t outsize = length;
133
134 while (insize > 0)
135 {
136 size_t res = iconv (cd,
137 (ICONV_CONST char **) &inptr, &insize,
138 &outptr, &outsize);
139
140 if (res == (size_t)(-1))
141 {
142 if (errno == EINVAL)
143 break;
144 else
145 goto fail;
146 }
147 # if !(defined _LIBICONV_VERSION && !(_LIBICONV_VERSION == 0x10b && defined __APPLE__)) \
148 && !(defined __GLIBC__ && !defined __UCLIBC__)
149 /* Irix iconv() inserts a NUL byte if it cannot convert.
150 NetBSD iconv() inserts a question mark if it cannot convert.
151 Only GNU libiconv (excluding the bastard Apple iconv) and GNU libc
152 are known to prefer to fail rather than doing a lossy conversion. */
153 else if (res > 0)
154 {
155 errno = EILSEQ;
156 goto fail;
157 }
158 # endif
159 }
160 {
161 size_t res = iconv (cd, NULL, NULL, &outptr, &outsize);
162
163 if (res == (size_t)(-1))
164 goto fail;
165 }
166 if (outsize != 0)
167 abort ();
168 }
169
170 *resultp = result;
171 *lengthp = length;
172
173 return 0;
174
175 fail:
176 {
177 if (result != *resultp)
178 free (result);
179 return -1;
180 }
181 # undef tmpbufsize
182 }
183
184 char *
185 str_cd_iconv (const char *src, iconv_t cd)
186 {
187 /* For most encodings, a trailing NUL byte in the input will be converted
188 to a trailing NUL byte in the output. But not for UTF-7. So that this
189 function is usable for UTF-7, we have to exclude the NUL byte from the
190 conversion and add it by hand afterwards. */
191 # if !(defined _LIBICONV_VERSION && !(_LIBICONV_VERSION == 0x10b && defined __APPLE__)) \
192 && !(defined __GLIBC__ && !defined __UCLIBC__)
193 /* Irix iconv() inserts a NUL byte if it cannot convert.
194 NetBSD iconv() inserts a question mark if it cannot convert.
195 Only GNU libiconv (excluding the bastard Apple iconv) and GNU libc are
196 known to prefer to fail rather than doing a lossy conversion. For other
197 iconv() implementations, we have to look at the number of irreversible
198 conversions returned; but this information is lost when iconv() returns
199 for an E2BIG reason. Therefore we cannot use the second, faster
200 algorithm. */
201
202 char *result = NULL;
203 size_t length = 0;
204 int retval = mem_cd_iconv (src, strlen (src), cd, &result, &length);
205 char *final_result;
206
207 if (retval < 0)
208 {
209 if (result != NULL)
210 abort ();
211 return NULL;
212 }
213
214 /* Add the terminating NUL byte. */
215 final_result =
216 (result != NULL ? realloc (result, length + 1) : malloc (length + 1));
217 if (final_result == NULL)
218 {
219 free (result);
220 errno = ENOMEM;
221 return NULL;
222 }
223 final_result[length] = '\0';
224
225 return final_result;
226
227 # else
228 /* This algorithm is likely faster than the one above. But it may produce
229 iconv() returns for an E2BIG reason, when the output size guess is too
230 small. Therefore it can only be used when we don't need the number of
231 irreversible conversions performed. */
232 char *result;
233 size_t result_size;
234 size_t length;
235 const char *inptr = src;
236 size_t inbytes_remaining = strlen (src);
237
238 /* Make a guess for the worst-case output size, in order to avoid a
239 realloc. It's OK if the guess is wrong as long as it is not zero and
240 doesn't lead to an integer overflow. */
241 result_size = inbytes_remaining;
242 {
243 size_t approx_sqrt_SIZE_MAX = SIZE_MAX >> (sizeof (size_t) * CHAR_BIT / 2);
244 if (result_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
245 result_size *= MB_LEN_MAX;
246 }
247 result_size += 1; /* for the terminating NUL */
248
249 result = (char *) malloc (result_size);
250 if (result == NULL)
251 {
252 errno = ENOMEM;
253 return NULL;
254 }
255
256 /* Set to the initial state. */
257 iconv (cd, NULL, NULL, NULL, NULL);
258
259 /* Do the conversion. */
260 {
261 char *outptr = result;
262 size_t outbytes_remaining = result_size - 1;
263
264 for (;;)
265 {
266 /* Here inptr + inbytes_remaining = src + strlen (src),
267 outptr + outbytes_remaining = result + result_size - 1. */
268 size_t res = iconv (cd,
269 (ICONV_CONST char **) &inptr, &inbytes_remaining,
270 &outptr, &outbytes_remaining);
271
272 if (res == (size_t)(-1))
273 {
274 if (errno == EINVAL)
275 break;
276 else if (errno == E2BIG)
277 {
278 size_t used = outptr - result;
279 size_t newsize = result_size * 2;
280 char *newresult;
281
282 if (!(newsize > result_size))
283 {
284 errno = ENOMEM;
285 goto failed;
286 }
287 newresult = (char *) realloc (result, newsize);
288 if (newresult == NULL)
289 {
290 errno = ENOMEM;
291 goto failed;
292 }
293 result = newresult;
294 result_size = newsize;
295 outptr = result + used;
296 outbytes_remaining = result_size - 1 - used;
297 }
298 else
299 goto failed;
300 }
301 else
302 break;
303 }
304 for (;;)
305 {
306 /* Here outptr + outbytes_remaining = result + result_size - 1. */
307 size_t res = iconv (cd, NULL, NULL, &outptr, &outbytes_remaining);
308
309 if (res == (size_t)(-1))
310 {
311 if (errno == E2BIG)
312 {
313 size_t used = outptr - result;
314 size_t newsize = result_size * 2;
315 char *newresult;
316
317 if (!(newsize > result_size))
318 {
319 errno = ENOMEM;
320 goto failed;
321 }
322 newresult = (char *) realloc (result, newsize);
323 if (newresult == NULL)
324 {
325 errno = ENOMEM;
326 goto failed;
327 }
328 result = newresult;
329 result_size = newsize;
330 outptr = result + used;
331 outbytes_remaining = result_size - 1 - used;
332 }
333 else
334 goto failed;
335 }
336 else
337 break;
338 }
339
340 /* Add the terminating NUL byte. */
341 *outptr++ = '\0';
342
343 length = outptr - result;
344 }
345
346 /* Give away unused memory. */
347 if (length < result_size)
348 {
349 char *smaller_result = (char *) realloc (result, length);
350
351 if (smaller_result != NULL)
352 result = smaller_result;
353 }
354
355 return result;
356
357 failed:
358 free (result);
359 return NULL;
360
361 # endif
362 }
363
364 #endif
365
366 char *
367 str_iconv (const char *src, const char *from_codeset, const char *to_codeset)
368 {
369 if (*src == '\0' || c_strcasecmp (from_codeset, to_codeset) == 0)
370 {
371 char *result = strdup (src);
372
373 if (result == NULL)
374 errno = ENOMEM;
375 return result;
376 }
377 else
378 {
379 #if HAVE_ICONV
380 iconv_t cd;
381 char *result;
382
383 cd = iconv_open (to_codeset, from_codeset);
384 if (cd == (iconv_t) -1)
385 return NULL;
386
387 result = str_cd_iconv (src, cd);
388
389 if (result == NULL)
390 {
391 /* Close cd, but preserve the errno from str_cd_iconv. */
392 int saved_errno = errno;
393 iconv_close (cd);
394 errno = saved_errno;
395 }
396 else
397 {
398 if (iconv_close (cd) < 0)
399 {
400 free (result);
401 return NULL;
402 }
403 }
404 return result;
405 #else
406 /* This is a different error code than if iconv_open existed but didn't
407 support from_codeset and to_codeset, so that the caller can emit
408 an error message such as
409 "iconv() is not supported. Installing GNU libiconv and
410 then reinstalling this package would fix this." */
411 errno = ENOSYS;
412 return NULL;
413 #endif
414 }
415 }