]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/iconv.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / iconv / iconv.c
CommitLineData
6973fc01
UD
1/* Convert characters in input buffer using conversion descriptor to
2 output buffer.
2b778ceb 3 Copyright (C) 1997-2021 Free Software Foundation, Inc.
6973fc01
UD
4 This file is part of the GNU C Library.
5 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
6
7 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
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.
6973fc01
UD
11
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
41bdb6e2 15 Lesser General Public License for more details.
6973fc01 16
41bdb6e2 17 You should have received a copy of the GNU Lesser General Public
59ba27a6 18 License along with the GNU C Library; if not, see
5a82c748 19 <https://www.gnu.org/licenses/>. */
6973fc01 20
4360eafd 21#include <stddef.h> /* for NULL */
e34b0f29 22#include <errno.h>
6973fc01 23#include <iconv.h>
e62c19f1
UD
24
25#include <gconv_int.h>
6973fc01 26
e34b0f29
UD
27#include <assert.h>
28
6973fc01
UD
29
30size_t
0efb48a1 31iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
6973fc01
UD
32 size_t *outbytesleft)
33{
d64b6ad0 34 __gconv_t gcd = (__gconv_t) cd;
8619129f 35 char *outstart = outbuf ? *outbuf : NULL;
38677ace 36 size_t irreversible;
e34b0f29
UD
37 int result;
38
a1ffb40e 39 if (__glibc_unlikely (inbuf == NULL || *inbuf == NULL))
8619129f 40 {
c63598bf 41 if (outbuf == NULL || *outbuf == NULL)
38677ace 42 result = __gconv (gcd, NULL, NULL, NULL, NULL, &irreversible);
c63598bf
UD
43 else
44 result = __gconv (gcd, NULL, NULL, (unsigned char **) outbuf,
45 (unsigned char *) (outstart + *outbytesleft),
38677ace 46 &irreversible);
8619129f
UD
47 }
48 else
49 {
50 const char *instart = *inbuf;
51
97e94e49
UD
52 result = __gconv (gcd, (const unsigned char **) inbuf,
53 (const unsigned char *) (*inbuf + *inbytesleft),
b117f744
UD
54 (unsigned char **) outbuf,
55 (unsigned char *) (*outbuf + *outbytesleft),
38677ace 56 &irreversible);
8619129f
UD
57
58 *inbytesleft -= *inbuf - instart;
59 }
60 if (outstart != NULL)
61 *outbytesleft -= *outbuf - outstart;
62
a711dd4b 63 switch (__builtin_expect (result, __GCONV_OK))
e34b0f29 64 {
d64b6ad0 65 case __GCONV_ILLEGAL_DESCRIPTOR:
e34b0f29 66 __set_errno (EBADF);
38677ace 67 irreversible = (size_t) -1L;
e34b0f29
UD
68 break;
69
d64b6ad0 70 case __GCONV_ILLEGAL_INPUT:
e34b0f29 71 __set_errno (EILSEQ);
38677ace 72 irreversible = (size_t) -1L;
e34b0f29
UD
73 break;
74
d64b6ad0 75 case __GCONV_FULL_OUTPUT:
e34b0f29 76 __set_errno (E2BIG);
38677ace 77 irreversible = (size_t) -1L;
e34b0f29
UD
78 break;
79
d64b6ad0 80 case __GCONV_INCOMPLETE_INPUT:
e34b0f29 81 __set_errno (EINVAL);
38677ace 82 irreversible = (size_t) -1L;
e34b0f29
UD
83 break;
84
d64b6ad0
UD
85 case __GCONV_EMPTY_INPUT:
86 case __GCONV_OK:
e34b0f29
UD
87 /* Nothing. */
88 break;
6973fc01 89
e34b0f29
UD
90 default:
91 assert (!"Nothing like this should happen");
92 }
6973fc01 93
38677ace 94 return irreversible;
6973fc01 95}