2 Copyright (C) 2001-2007, 2010-2024 Free Software Foundation, Inc.
3 Written by Bruno Haible and Simon Josefsson.
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.
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.
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/>. */
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 && !(_LIBICONV_VERSION == 0x10b && defined __APPLE__)) \
86 && !(defined __GLIBC__ && !defined __UCLIBC__)
87 /* Irix iconv() inserts a NUL byte if it cannot convert.
88 NetBSD iconv() inserts a question mark if it cannot convert.
89 Only GNU libiconv (excluding the bastard Apple iconv) and GNU libc
90 are known to prefer to fail rather than doing a lossy conversion. */
97 count
+= outptr
- tmpbuf
;
99 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
100 # if defined _LIBICONV_VERSION \
101 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
104 char *outptr
= tmpbuf
;
105 size_t outsize
= tmpbufsize
;
106 size_t res
= iconv (cd
, NULL
, NULL
, &outptr
, &outsize
);
108 if (res
== (size_t)(-1))
110 count
+= outptr
- tmpbuf
;
122 if (*resultp
!= NULL
&& *lengthp
>= length
)
126 result
= (char *) malloc (length
);
134 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
135 # if defined _LIBICONV_VERSION \
136 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
138 /* Return to the initial state. */
139 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
142 /* Do the conversion for real. */
144 const char *inptr
= src
;
145 size_t insize
= srclen
;
146 char *outptr
= result
;
147 size_t outsize
= length
;
151 size_t res
= iconv (cd
,
152 (ICONV_CONST
char **) &inptr
, &insize
,
155 if (res
== (size_t)(-1))
162 # if !(defined _LIBICONV_VERSION && !(_LIBICONV_VERSION == 0x10b && defined __APPLE__)) \
163 && !(defined __GLIBC__ && !defined __UCLIBC__)
164 /* Irix iconv() inserts a NUL byte if it cannot convert.
165 NetBSD iconv() inserts a question mark if it cannot convert.
166 Only GNU libiconv (excluding the bastard Apple iconv) and GNU libc
167 are known to prefer to fail rather than doing a lossy conversion. */
175 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
176 # if defined _LIBICONV_VERSION \
177 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
180 size_t res
= iconv (cd
, NULL
, NULL
, &outptr
, &outsize
);
182 if (res
== (size_t)(-1))
197 if (result
!= *resultp
)
205 str_cd_iconv (const char *src
, iconv_t cd
)
207 /* For most encodings, a trailing NUL byte in the input will be converted
208 to a trailing NUL byte in the output. But not for UTF-7. So that this
209 function is usable for UTF-7, we have to exclude the NUL byte from the
210 conversion and add it by hand afterwards. */
211 # if !(defined _LIBICONV_VERSION && !(_LIBICONV_VERSION == 0x10b && defined __APPLE__)) \
212 && !(defined __GLIBC__ && !defined __UCLIBC__)
213 /* Irix iconv() inserts a NUL byte if it cannot convert.
214 NetBSD iconv() inserts a question mark if it cannot convert.
215 Only GNU libiconv (excluding the bastard Apple iconv) and GNU libc are
216 known to prefer to fail rather than doing a lossy conversion. For other
217 iconv() implementations, we have to look at the number of irreversible
218 conversions returned; but this information is lost when iconv() returns
219 for an E2BIG reason. Therefore we cannot use the second, faster
224 int retval
= mem_cd_iconv (src
, strlen (src
), cd
, &result
, &length
);
234 /* Add the terminating NUL byte. */
236 (result
!= NULL
? realloc (result
, length
+ 1) : malloc (length
+ 1));
237 if (final_result
== NULL
)
243 final_result
[length
] = '\0';
248 /* This algorithm is likely faster than the one above. But it may produce
249 iconv() returns for an E2BIG reason, when the output size guess is too
250 small. Therefore it can only be used when we don't need the number of
251 irreversible conversions performed. */
255 const char *inptr
= src
;
256 size_t inbytes_remaining
= strlen (src
);
258 /* Make a guess for the worst-case output size, in order to avoid a
259 realloc. It's OK if the guess is wrong as long as it is not zero and
260 doesn't lead to an integer overflow. */
261 result_size
= inbytes_remaining
;
263 size_t approx_sqrt_SIZE_MAX
= SIZE_MAX
>> (sizeof (size_t) * CHAR_BIT
/ 2);
264 if (result_size
<= approx_sqrt_SIZE_MAX
/ MB_LEN_MAX
)
265 result_size
*= MB_LEN_MAX
;
267 result_size
+= 1; /* for the terminating NUL */
269 result
= (char *) malloc (result_size
);
276 /* Avoid glibc-2.1 bug and Solaris 2.7-2.9 bug. */
277 # if defined _LIBICONV_VERSION \
278 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
280 /* Set to the initial state. */
281 iconv (cd
, NULL
, NULL
, NULL
, NULL
);
284 /* Do the conversion. */
286 char *outptr
= result
;
287 size_t outbytes_remaining
= result_size
- 1;
291 /* Here inptr + inbytes_remaining = src + strlen (src),
292 outptr + outbytes_remaining = result + result_size - 1. */
293 size_t res
= iconv (cd
,
294 (ICONV_CONST
char **) &inptr
, &inbytes_remaining
,
295 &outptr
, &outbytes_remaining
);
297 if (res
== (size_t)(-1))
301 else if (errno
== E2BIG
)
303 size_t used
= outptr
- result
;
304 size_t newsize
= result_size
* 2;
307 if (!(newsize
> result_size
))
312 newresult
= (char *) realloc (result
, newsize
);
313 if (newresult
== NULL
)
319 result_size
= newsize
;
320 outptr
= result
+ used
;
321 outbytes_remaining
= result_size
- 1 - used
;
329 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
330 # if defined _LIBICONV_VERSION \
331 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
335 /* Here outptr + outbytes_remaining = result + result_size - 1. */
336 size_t res
= iconv (cd
, NULL
, NULL
, &outptr
, &outbytes_remaining
);
338 if (res
== (size_t)(-1))
342 size_t used
= outptr
- result
;
343 size_t newsize
= result_size
* 2;
346 if (!(newsize
> result_size
))
351 newresult
= (char *) realloc (result
, newsize
);
352 if (newresult
== NULL
)
358 result_size
= newsize
;
359 outptr
= result
+ used
;
360 outbytes_remaining
= result_size
- 1 - used
;
370 /* Add the terminating NUL byte. */
373 length
= outptr
- result
;
376 /* Give away unused memory. */
377 if (length
< result_size
)
379 char *smaller_result
= (char *) realloc (result
, length
);
381 if (smaller_result
!= NULL
)
382 result
= smaller_result
;
397 str_iconv (const char *src
, const char *from_codeset
, const char *to_codeset
)
399 if (*src
== '\0' || c_strcasecmp (from_codeset
, to_codeset
) == 0)
401 char *result
= strdup (src
);
413 /* Avoid glibc-2.1 bug with EUC-KR. */
414 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
415 && !defined _LIBICONV_VERSION
416 if (c_strcasecmp (from_codeset
, "EUC-KR") == 0
417 || c_strcasecmp (to_codeset
, "EUC-KR") == 0)
423 cd
= iconv_open (to_codeset
, from_codeset
);
424 if (cd
== (iconv_t
) -1)
427 result
= str_cd_iconv (src
, cd
);
431 /* Close cd, but preserve the errno from str_cd_iconv. */
432 int saved_errno
= errno
;
438 if (iconv_close (cd
) < 0)
446 /* This is a different error code than if iconv_open existed but didn't
447 support from_codeset and to_codeset, so that the caller can emit
448 an error message such as
449 "iconv() is not supported. Installing GNU libiconv and
450 then reinstalling this package would fix this." */