]>
Commit | Line | Data |
---|---|---|
6973fc01 UD |
1 | /* Convert characters in input buffer using conversion descriptor to |
2 | output buffer. | |
581c785b | 3 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
6973fc01 | 4 | This file is part of the GNU C Library. |
6973fc01 UD |
5 | |
6 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
6973fc01 UD |
10 | |
11 | The GNU C Library is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
6973fc01 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
6973fc01 | 19 | |
4360eafd | 20 | #include <stddef.h> /* for NULL */ |
e34b0f29 | 21 | #include <errno.h> |
6973fc01 | 22 | #include <iconv.h> |
e62c19f1 UD |
23 | |
24 | #include <gconv_int.h> | |
6973fc01 | 25 | |
e34b0f29 UD |
26 | #include <assert.h> |
27 | ||
6973fc01 UD |
28 | |
29 | size_t | |
0efb48a1 | 30 | iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, |
6973fc01 UD |
31 | size_t *outbytesleft) |
32 | { | |
d64b6ad0 | 33 | __gconv_t gcd = (__gconv_t) cd; |
8619129f | 34 | char *outstart = outbuf ? *outbuf : NULL; |
38677ace | 35 | size_t irreversible; |
e34b0f29 UD |
36 | int result; |
37 | ||
a1ffb40e | 38 | if (__glibc_unlikely (inbuf == NULL || *inbuf == NULL)) |
8619129f | 39 | { |
c63598bf | 40 | if (outbuf == NULL || *outbuf == NULL) |
38677ace | 41 | result = __gconv (gcd, NULL, NULL, NULL, NULL, &irreversible); |
c63598bf UD |
42 | else |
43 | result = __gconv (gcd, NULL, NULL, (unsigned char **) outbuf, | |
44 | (unsigned char *) (outstart + *outbytesleft), | |
38677ace | 45 | &irreversible); |
8619129f UD |
46 | } |
47 | else | |
48 | { | |
49 | const char *instart = *inbuf; | |
50 | ||
97e94e49 UD |
51 | result = __gconv (gcd, (const unsigned char **) inbuf, |
52 | (const unsigned char *) (*inbuf + *inbytesleft), | |
b117f744 UD |
53 | (unsigned char **) outbuf, |
54 | (unsigned char *) (*outbuf + *outbytesleft), | |
38677ace | 55 | &irreversible); |
8619129f UD |
56 | |
57 | *inbytesleft -= *inbuf - instart; | |
58 | } | |
59 | if (outstart != NULL) | |
60 | *outbytesleft -= *outbuf - outstart; | |
61 | ||
a711dd4b | 62 | switch (__builtin_expect (result, __GCONV_OK)) |
e34b0f29 | 63 | { |
d64b6ad0 | 64 | case __GCONV_ILLEGAL_DESCRIPTOR: |
e34b0f29 | 65 | __set_errno (EBADF); |
38677ace | 66 | irreversible = (size_t) -1L; |
e34b0f29 UD |
67 | break; |
68 | ||
d64b6ad0 | 69 | case __GCONV_ILLEGAL_INPUT: |
e34b0f29 | 70 | __set_errno (EILSEQ); |
38677ace | 71 | irreversible = (size_t) -1L; |
e34b0f29 UD |
72 | break; |
73 | ||
d64b6ad0 | 74 | case __GCONV_FULL_OUTPUT: |
e34b0f29 | 75 | __set_errno (E2BIG); |
38677ace | 76 | irreversible = (size_t) -1L; |
e34b0f29 UD |
77 | break; |
78 | ||
d64b6ad0 | 79 | case __GCONV_INCOMPLETE_INPUT: |
e34b0f29 | 80 | __set_errno (EINVAL); |
38677ace | 81 | irreversible = (size_t) -1L; |
e34b0f29 UD |
82 | break; |
83 | ||
d64b6ad0 UD |
84 | case __GCONV_EMPTY_INPUT: |
85 | case __GCONV_OK: | |
e34b0f29 UD |
86 | /* Nothing. */ |
87 | break; | |
6973fc01 | 88 | |
e34b0f29 UD |
89 | default: |
90 | assert (!"Nothing like this should happen"); | |
91 | } | |
6973fc01 | 92 | |
38677ace | 93 | return irreversible; |
6973fc01 | 94 | } |