2 Copyright (C) 2001-2007, 2010-2021 Free Software Foundation, Inc.
3 Written by Bruno Haible and Simon Josefsson.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
10 This program 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
29 /* Get MB_LEN_MAX, CHAR_BIT. */
33 #include "c-strcase.h"
36 # define SIZE_MAX ((size_t) -1)
43 mem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
44 char **resultp, size_t *lengthp)
46 # define tmpbufsize 4096
50 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
51 # if defined _LIBICONV_VERSION \
52 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
54 /* Set to the initial state. */
55 iconv (cd, NULL, NULL, NULL, NULL);
58 /* Determine the length we need. */
61 /* The alignment is needed when converting e.g. to glibc's WCHAR_T or
62 libiconv's UCS-4-INTERNAL encoding. */
63 union { unsigned int align; char buf[tmpbufsize]; } tmp;
64 # define tmpbuf tmp.buf
65 const char *inptr = src;
66 size_t insize = srclen;
70 char *outptr = tmpbuf;
71 size_t outsize = tmpbufsize;
72 size_t res = iconv (cd,
73 (ICONV_CONST char **) &inptr, &insize,
76 if (res == (size_t)(-1))
80 else if (errno == EINVAL)
85 # if !defined _LIBICONV_VERSION && !(defined __GLIBC__ && !defined __UCLIBC__)
86 /* Irix iconv() inserts a NUL byte if it cannot convert.
87 NetBSD iconv() inserts a question mark if it cannot convert.
88 Only GNU libiconv and GNU libc are known to prefer to fail rather
89 than doing a lossy conversion. */
96 count += outptr - tmpbuf;
98 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
99 # if defined _LIBICONV_VERSION \
100 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
103 char *outptr = tmpbuf;
104 size_t outsize = tmpbufsize;
105 size_t res = iconv (cd, NULL, NULL, &outptr, &outsize);
107 if (res == (size_t)(-1))
109 count += outptr - tmpbuf;
121 if (*resultp != NULL && *lengthp >= length)
125 result = (char *) malloc (length);
133 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
134 # if defined _LIBICONV_VERSION \
135 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
137 /* Return to the initial state. */
138 iconv (cd, NULL, NULL, NULL, NULL);
141 /* Do the conversion for real. */
143 const char *inptr = src;
144 size_t insize = srclen;
145 char *outptr = result;
146 size_t outsize = length;
150 size_t res = iconv (cd,
151 (ICONV_CONST char **) &inptr, &insize,
154 if (res == (size_t)(-1))
161 # if !defined _LIBICONV_VERSION && !(defined __GLIBC__ && !defined __UCLIBC__)
162 /* Irix iconv() inserts a NUL byte if it cannot convert.
163 NetBSD iconv() inserts a question mark if it cannot convert.
164 Only GNU libiconv and GNU libc are known to prefer to fail rather
165 than doing a lossy conversion. */
173 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
174 # if defined _LIBICONV_VERSION \
175 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
178 size_t res = iconv (cd, NULL, NULL, &outptr, &outsize);
180 if (res == (size_t)(-1))
195 if (result != *resultp)
203 str_cd_iconv (const char *src, iconv_t cd)
205 /* For most encodings, a trailing NUL byte in the input will be converted
206 to a trailing NUL byte in the output. But not for UTF-7. So that this
207 function is usable for UTF-7, we have to exclude the NUL byte from the
208 conversion and add it by hand afterwards. */
209 # if !defined _LIBICONV_VERSION && !(defined __GLIBC__ && !defined __UCLIBC__)
210 /* Irix iconv() inserts a NUL byte if it cannot convert.
211 NetBSD iconv() inserts a question mark if it cannot convert.
212 Only GNU libiconv and GNU libc are known to prefer to fail rather
213 than doing a lossy conversion. For other iconv() implementations,
214 we have to look at the number of irreversible conversions returned;
215 but this information is lost when iconv() returns for an E2BIG reason.
216 Therefore we cannot use the second, faster algorithm. */
220 int retval = mem_cd_iconv (src, strlen (src), cd, &result, &length);
230 /* Add the terminating NUL byte. */
232 (result != NULL ? realloc (result, length + 1) : malloc (length + 1));
233 if (final_result == NULL)
239 final_result[length] = '\0';
244 /* This algorithm is likely faster than the one above. But it may produce
245 iconv() returns for an E2BIG reason, when the output size guess is too
246 small. Therefore it can only be used when we don't need the number of
247 irreversible conversions performed. */
251 const char *inptr = src;
252 size_t inbytes_remaining = strlen (src);
254 /* Make a guess for the worst-case output size, in order to avoid a
255 realloc. It's OK if the guess is wrong as long as it is not zero and
256 doesn't lead to an integer overflow. */
257 result_size = inbytes_remaining;
259 size_t approx_sqrt_SIZE_MAX = SIZE_MAX >> (sizeof (size_t) * CHAR_BIT / 2);
260 if (result_size <= approx_sqrt_SIZE_MAX / MB_LEN_MAX)
261 result_size *= MB_LEN_MAX;
263 result_size += 1; /* for the terminating NUL */
265 result = (char *) malloc (result_size);
272 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
273 # if defined _LIBICONV_VERSION \
274 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
276 /* Set to the initial state. */
277 iconv (cd, NULL, NULL, NULL, NULL);
280 /* Do the conversion. */
282 char *outptr = result;
283 size_t outbytes_remaining = result_size - 1;
287 /* Here inptr + inbytes_remaining = src + strlen (src),
288 outptr + outbytes_remaining = result + result_size - 1. */
289 size_t res = iconv (cd,
290 (ICONV_CONST char **) &inptr, &inbytes_remaining,
291 &outptr, &outbytes_remaining);
293 if (res == (size_t)(-1))
297 else if (errno == E2BIG)
299 size_t used = outptr - result;
300 size_t newsize = result_size * 2;
303 if (!(newsize > result_size))
308 newresult = (char *) realloc (result, newsize);
309 if (newresult == NULL)
315 result_size = newsize;
316 outptr = result + used;
317 outbytes_remaining = result_size - 1 - used;
325 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
326 # if defined _LIBICONV_VERSION \
327 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
331 /* Here outptr + outbytes_remaining = result + result_size - 1. */
332 size_t res = iconv (cd, NULL, NULL, &outptr, &outbytes_remaining);
334 if (res == (size_t)(-1))
338 size_t used = outptr - result;
339 size_t newsize = result_size * 2;
342 if (!(newsize > result_size))
347 newresult = (char *) realloc (result, newsize);
348 if (newresult == NULL)
354 result_size = newsize;
355 outptr = result + used;
356 outbytes_remaining = result_size - 1 - used;
366 /* Add the terminating NUL byte. */
369 length = outptr - result;
372 /* Give away unused memory. */
373 if (length < result_size)
375 char *smaller_result = (char *) realloc (result, length);
377 if (smaller_result != NULL)
378 result = smaller_result;
393 str_iconv (const char *src, const char *from_codeset, const char *to_codeset)
395 if (*src == '\0' || c_strcasecmp (from_codeset, to_codeset) == 0)
397 char *result = strdup (src);
409 /* Avoid glibc-2.1 bug with EUC-KR. */
410 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
411 && !defined _LIBICONV_VERSION
412 if (c_strcasecmp (from_codeset, "EUC-KR") == 0
413 || c_strcasecmp (to_codeset, "EUC-KR") == 0)
419 cd = iconv_open (to_codeset, from_codeset);
420 if (cd == (iconv_t) -1)
423 result = str_cd_iconv (src, cd);
427 /* Close cd, but preserve the errno from str_cd_iconv. */
428 int saved_errno = errno;
434 if (iconv_close (cd) < 0)
442 /* This is a different error code than if iconv_open existed but didn't
443 support from_codeset and to_codeset, so that the caller can emit
444 an error message such as
445 "iconv() is not supported. Installing GNU libiconv and
446 then reinstalling this package would fix this." */