1/* Convert characters in input buffer using conversion descriptor to
2 output buffer.
3 Copyright (C) 1997-2016 Free Software Foundation, Inc.
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
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.
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
15 Lesser General Public License for more details.
16
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/>. */
20
21#include <stddef.h> /* for NULL */
22#include <errno.h>
23#include <iconv.h>
24
25#include <gconv_int.h>
26
27#include <assert.h>
28
29
30size_t
31iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
32 size_t *outbytesleft)
33{
34 __gconv_t gcd = (__gconv_t) cd;
35 char *outstart = outbuf ? *outbuf : NULL;
36 size_t irreversible;
37 int result;
38
39 if (__glibc_unlikely (inbuf == NULL || *inbuf == NULL))
40 {
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);
47 }
48 else
49 {
50 const char *instart = *inbuf;
51
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);
57
58 *inbytesleft -= *inbuf - instart;
59 }
60 if (outstart != NULL)
61 *outbytesleft -= *outbuf - outstart;
62
63 switch (__builtin_expect (result, __GCONV_OK))
64 {
65 case __GCONV_ILLEGAL_DESCRIPTOR:
66 __set_errno (EBADF);
67 irreversible = (size_t) -1L;
68 break;
69
70 case __GCONV_ILLEGAL_INPUT:
71 __set_errno (EILSEQ);
72 irreversible = (size_t) -1L;
73 break;
74
75 case __GCONV_FULL_OUTPUT:
76 __set_errno (E2BIG);
77 irreversible = (size_t) -1L;
78 break;
79
80 case __GCONV_INCOMPLETE_INPUT:
81 __set_errno (EINVAL);
82 irreversible = (size_t) -1L;
83 break;
84
85 case __GCONV_EMPTY_INPUT:
86 case __GCONV_OK:
87 /* Nothing. */
88 break;
89
90 default:
91 assert (!"Nothing like this should happen");
92 }
93
94 return irreversible;
95}
96