kernel - Handle spinlock indefinite wait edge case
[dragonfly.git] / contrib / grep / lib / striconv.c
blob6f7dad8e773e06f6f8086ee72d525a9a48639182
1 /* Charset conversion.
2 Copyright (C) 2001-2007, 2010-2015 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)
8 any later version.
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 <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 /* Specification. */
21 #include "striconv.h"
23 #include <errno.h>
24 #include <stdlib.h>
25 #include <string.h>
27 #if HAVE_ICONV
28 # include <iconv.h>
29 /* Get MB_LEN_MAX, CHAR_BIT. */
30 # include <limits.h>
31 #endif
33 #include "c-strcase.h"
35 #ifndef SIZE_MAX
36 # define SIZE_MAX ((size_t) -1)
37 #endif
40 #if HAVE_ICONV
42 int
43 mem_cd_iconv (const char *src, size_t srclen, iconv_t cd,
44 char **resultp, size_t *lengthp)
46 # define tmpbufsize 4096
47 size_t length;
48 char *result;
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__) \
53 || defined __sun)
54 /* Set to the initial state. */
55 iconv (cd, NULL, NULL, NULL, NULL);
56 # endif
58 /* Determine the length we need. */
60 size_t count = 0;
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;
68 while (insize > 0)
70 char *outptr = tmpbuf;
71 size_t outsize = tmpbufsize;
72 size_t res = iconv (cd,
73 (ICONV_CONST char **) &inptr, &insize,
74 &outptr, &outsize);
76 if (res == (size_t)(-1))
78 if (errno == E2BIG)
80 else if (errno == EINVAL)
81 break;
82 else
83 return -1;
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. */
90 else if (res > 0)
92 errno = EILSEQ;
93 return -1;
95 # endif
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__) \
101 || defined __sun)
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))
108 return -1;
109 count += outptr - tmpbuf;
111 # endif
112 length = count;
113 # undef tmpbuf
116 if (length == 0)
118 *lengthp = 0;
119 return 0;
121 if (*resultp != NULL && *lengthp >= length)
122 result = *resultp;
123 else
125 result = (char *) malloc (length);
126 if (result == NULL)
128 errno = ENOMEM;
129 return -1;
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__) \
136 || defined __sun)
137 /* Return to the initial state. */
138 iconv (cd, NULL, NULL, NULL, NULL);
139 # endif
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;
148 while (insize > 0)
150 size_t res = iconv (cd,
151 (ICONV_CONST char **) &inptr, &insize,
152 &outptr, &outsize);
154 if (res == (size_t)(-1))
156 if (errno == EINVAL)
157 break;
158 else
159 goto fail;
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. */
166 else if (res > 0)
168 errno = EILSEQ;
169 goto fail;
171 # endif
173 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
174 # if defined _LIBICONV_VERSION \
175 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
176 || defined __sun)
178 size_t res = iconv (cd, NULL, NULL, &outptr, &outsize);
180 if (res == (size_t)(-1))
181 goto fail;
183 # endif
184 if (outsize != 0)
185 abort ();
188 *resultp = result;
189 *lengthp = length;
191 return 0;
193 fail:
195 if (result != *resultp)
197 int saved_errno = errno;
198 free (result);
199 errno = saved_errno;
201 return -1;
203 # undef tmpbufsize
206 char *
207 str_cd_iconv (const char *src, iconv_t cd)
209 /* For most encodings, a trailing NUL byte in the input will be converted
210 to a trailing NUL byte in the output. But not for UTF-7. So that this
211 function is usable for UTF-7, we have to exclude the NUL byte from the
212 conversion and add it by hand afterwards. */
213 # if !defined _LIBICONV_VERSION && !(defined __GLIBC__ && !defined __UCLIBC__)
214 /* Irix iconv() inserts a NUL byte if it cannot convert.
215 NetBSD iconv() inserts a question mark if it cannot convert.
216 Only GNU libiconv and GNU libc are known to prefer to fail rather
217 than doing a lossy conversion. For other iconv() implementations,
218 we have to look at the number of irreversible conversions returned;
219 but this information is lost when iconv() returns for an E2BIG reason.
220 Therefore we cannot use the second, faster algorithm. */
222 char *result = NULL;
223 size_t length = 0;
224 int retval = mem_cd_iconv (src, strlen (src), cd, &result, &length);
225 char *final_result;
227 if (retval < 0)
229 if (result != NULL)
230 abort ();
231 return NULL;
234 /* Add the terminating NUL byte. */
235 final_result =
236 (result != NULL ? realloc (result, length + 1) : malloc (length + 1));
237 if (final_result == NULL)
239 free (result);
240 errno = ENOMEM;
241 return NULL;
243 final_result[length] = '\0';
245 return final_result;
247 # else
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. */
252 char *result;
253 size_t result_size;
254 size_t length;
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);
270 if (result == NULL)
272 errno = ENOMEM;
273 return NULL;
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__) \
279 || defined __sun)
280 /* Set to the initial state. */
281 iconv (cd, NULL, NULL, NULL, NULL);
282 # endif
284 /* Do the conversion. */
286 char *outptr = result;
287 size_t outbytes_remaining = result_size - 1;
289 for (;;)
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))
299 if (errno == EINVAL)
300 break;
301 else if (errno == E2BIG)
303 size_t used = outptr - result;
304 size_t newsize = result_size * 2;
305 char *newresult;
307 if (!(newsize > result_size))
309 errno = ENOMEM;
310 goto failed;
312 newresult = (char *) realloc (result, newsize);
313 if (newresult == NULL)
315 errno = ENOMEM;
316 goto failed;
318 result = newresult;
319 result_size = newsize;
320 outptr = result + used;
321 outbytes_remaining = result_size - 1 - used;
323 else
324 goto failed;
326 else
327 break;
329 /* Avoid glibc-2.1 bug and Solaris 2.7 bug. */
330 # if defined _LIBICONV_VERSION \
331 || !(((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
332 || defined __sun)
333 for (;;)
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))
340 if (errno == E2BIG)
342 size_t used = outptr - result;
343 size_t newsize = result_size * 2;
344 char *newresult;
346 if (!(newsize > result_size))
348 errno = ENOMEM;
349 goto failed;
351 newresult = (char *) realloc (result, newsize);
352 if (newresult == NULL)
354 errno = ENOMEM;
355 goto failed;
357 result = newresult;
358 result_size = newsize;
359 outptr = result + used;
360 outbytes_remaining = result_size - 1 - used;
362 else
363 goto failed;
365 else
366 break;
368 # endif
370 /* Add the terminating NUL byte. */
371 *outptr++ = '\0';
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;
385 return result;
387 failed:
389 int saved_errno = errno;
390 free (result);
391 errno = saved_errno;
392 return NULL;
395 # endif
398 #endif
400 char *
401 str_iconv (const char *src, const char *from_codeset, const char *to_codeset)
403 if (*src == '\0' || c_strcasecmp (from_codeset, to_codeset) == 0)
405 char *result = strdup (src);
407 if (result == NULL)
408 errno = ENOMEM;
409 return result;
411 else
413 #if HAVE_ICONV
414 iconv_t cd;
415 char *result;
417 /* Avoid glibc-2.1 bug with EUC-KR. */
418 # if ((__GLIBC__ == 2 && __GLIBC_MINOR__ <= 1) && !defined __UCLIBC__) \
419 && !defined _LIBICONV_VERSION
420 if (c_strcasecmp (from_codeset, "EUC-KR") == 0
421 || c_strcasecmp (to_codeset, "EUC-KR") == 0)
423 errno = EINVAL;
424 return NULL;
426 # endif
427 cd = iconv_open (to_codeset, from_codeset);
428 if (cd == (iconv_t) -1)
429 return NULL;
431 result = str_cd_iconv (src, cd);
433 if (result == NULL)
435 /* Close cd, but preserve the errno from str_cd_iconv. */
436 int saved_errno = errno;
437 iconv_close (cd);
438 errno = saved_errno;
440 else
442 if (iconv_close (cd) < 0)
444 /* Return NULL, but free the allocated memory, and while doing
445 that, preserve the errno from iconv_close. */
446 int saved_errno = errno;
447 free (result);
448 errno = saved_errno;
449 return NULL;
452 return result;
453 #else
454 /* This is a different error code than if iconv_open existed but didn't
455 support from_codeset and to_codeset, so that the caller can emit
456 an error message such as
457 "iconv() is not supported. Installing GNU libiconv and
458 then reinstalling this package would fix this." */
459 errno = ENOSYS;
460 return NULL;
461 #endif