1 /* Convert characters in input buffer using conversion descriptor to
3 Copyright (C) 1997-2018 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 */
25 #include <gconv_int.h>
31 iconv (iconv_t cd
, char **inbuf
, size_t *inbytesleft
, char **outbuf
,
34 __gconv_t gcd
= (__gconv_t
) cd
;
35 char *outstart
= outbuf
? *outbuf
: NULL
;
39 if (__glibc_unlikely (inbuf
== NULL
|| *inbuf
== NULL
))
41 if (outbuf
== NULL
|| *outbuf
== NULL
)
42 result
= __gconv (gcd
, NULL
, NULL
, NULL
, NULL
, &irreversible
);
44 result
= __gconv (gcd
, NULL
, NULL
, (unsigned char **) outbuf
,
45 (unsigned char *) (outstart
+ *outbytesleft
),
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
),
58 *inbytesleft
-= *inbuf
- instart
;
61 *outbytesleft
-= *outbuf
- outstart
;
63 switch (__builtin_expect (result
, __GCONV_OK
))
65 case __GCONV_ILLEGAL_DESCRIPTOR
:
67 irreversible
= (size_t) -1L;
70 case __GCONV_ILLEGAL_INPUT
:
72 irreversible
= (size_t) -1L;
75 case __GCONV_FULL_OUTPUT
:
77 irreversible
= (size_t) -1L;
80 case __GCONV_INCOMPLETE_INPUT
:
82 irreversible
= (size_t) -1L;
85 case __GCONV_EMPTY_INPUT
:
91 assert (!"Nothing like this should happen");