Fix buffer overrun in regexp matcher
[glibc.git] / iconv / iconv.c
blob93ca171434260a77748e3e3b772bef0bc26ffcdc
1 /* Convert characters in input buffer using conversion descriptor to
2 output buffer.
3 Copyright (C) 1997-2013 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, see
19 <http://www.gnu.org/licenses/>. */
21 #include <stddef.h> /* for NULL */
22 #include <errno.h>
23 #include <iconv.h>
25 #include <gconv_int.h>
27 #include <assert.h>
30 size_t
31 iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
32 size_t *outbytesleft)
34 __gconv_t gcd = (__gconv_t) cd;
35 char *outstart = outbuf ? *outbuf : NULL;
36 size_t irreversible;
37 int result;
39 if (__builtin_expect (inbuf == NULL || *inbuf == NULL, 0))
41 if (outbuf == NULL || *outbuf == NULL)
42 result = __gconv (gcd, NULL, NULL, NULL, NULL, &irreversible);
43 else
44 result = __gconv (gcd, NULL, NULL, (unsigned char **) outbuf,
45 (unsigned char *) (outstart + *outbytesleft),
46 &irreversible);
48 else
50 const char *instart = *inbuf;
52 result = __gconv (gcd, (const unsigned char **) inbuf,
53 (const unsigned char *) (*inbuf + *inbytesleft),
54 (unsigned char **) outbuf,
55 (unsigned char *) (*outbuf + *outbytesleft),
56 &irreversible);
58 *inbytesleft -= *inbuf - instart;
60 if (outstart != NULL)
61 *outbytesleft -= *outbuf - outstart;
63 switch (__builtin_expect (result, __GCONV_OK))
65 case __GCONV_ILLEGAL_DESCRIPTOR:
66 __set_errno (EBADF);
67 irreversible = (size_t) -1L;
68 break;
70 case __GCONV_ILLEGAL_INPUT:
71 __set_errno (EILSEQ);
72 irreversible = (size_t) -1L;
73 break;
75 case __GCONV_FULL_OUTPUT:
76 __set_errno (E2BIG);
77 irreversible = (size_t) -1L;
78 break;
80 case __GCONV_INCOMPLETE_INPUT:
81 __set_errno (EINVAL);
82 irreversible = (size_t) -1L;
83 break;
85 case __GCONV_EMPTY_INPUT:
86 case __GCONV_OK:
87 /* Nothing. */
88 break;
90 default:
91 assert (!"Nothing like this should happen");
94 return irreversible;