]> git.ipfire.org Git - thirdparty/glibc.git/blame - iconv/iconv.c
Fix typos in comments.
[thirdparty/glibc.git] / iconv / iconv.c
CommitLineData
6973fc01
UD
1/* Convert characters in input buffer using conversion descriptor to
2 output buffer.
a711dd4b 3 Copyright (C) 1997, 1998, 1999, 2000 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
AJ
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; if not, write to the Free
19 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307 USA. */
6973fc01 21
4360eafd 22#include <stddef.h> /* for NULL */
e34b0f29 23#include <errno.h>
6973fc01 24#include <iconv.h>
e62c19f1
UD
25
26#include <gconv_int.h>
6973fc01 27
e34b0f29
UD
28#include <assert.h>
29
6973fc01
UD
30
31size_t
0efb48a1 32iconv (iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf,
6973fc01
UD
33 size_t *outbytesleft)
34{
d64b6ad0 35 __gconv_t gcd = (__gconv_t) cd;
8619129f 36 char *outstart = outbuf ? *outbuf : NULL;
38677ace 37 size_t irreversible;
e34b0f29
UD
38 int result;
39
ec92d937 40 if (__builtin_expect (inbuf == NULL || *inbuf == NULL, 0))
8619129f 41 {
c63598bf 42 if (outbuf == NULL || *outbuf == NULL)
38677ace 43 result = __gconv (gcd, NULL, NULL, NULL, NULL, &irreversible);
c63598bf
UD
44 else
45 result = __gconv (gcd, NULL, NULL, (unsigned char **) outbuf,
46 (unsigned char *) (outstart + *outbytesleft),
38677ace 47 &irreversible);
8619129f
UD
48 }
49 else
50 {
51 const char *instart = *inbuf;
52
97e94e49
UD
53 result = __gconv (gcd, (const unsigned char **) inbuf,
54 (const unsigned char *) (*inbuf + *inbytesleft),
b117f744
UD
55 (unsigned char **) outbuf,
56 (unsigned char *) (*outbuf + *outbytesleft),
38677ace 57 &irreversible);
8619129f
UD
58
59 *inbytesleft -= *inbuf - instart;
60 }
61 if (outstart != NULL)
62 *outbytesleft -= *outbuf - outstart;
63
a711dd4b 64 switch (__builtin_expect (result, __GCONV_OK))
e34b0f29 65 {
d64b6ad0 66 case __GCONV_ILLEGAL_DESCRIPTOR:
e34b0f29 67 __set_errno (EBADF);
38677ace 68 irreversible = (size_t) -1L;
e34b0f29
UD
69 break;
70
d64b6ad0 71 case __GCONV_ILLEGAL_INPUT:
e34b0f29 72 __set_errno (EILSEQ);
38677ace 73 irreversible = (size_t) -1L;
e34b0f29
UD
74 break;
75
d64b6ad0 76 case __GCONV_FULL_OUTPUT:
e34b0f29 77 __set_errno (E2BIG);
38677ace 78 irreversible = (size_t) -1L;
e34b0f29
UD
79 break;
80
d64b6ad0 81 case __GCONV_INCOMPLETE_INPUT:
e34b0f29 82 __set_errno (EINVAL);
38677ace 83 irreversible = (size_t) -1L;
e34b0f29
UD
84 break;
85
d64b6ad0
UD
86 case __GCONV_EMPTY_INPUT:
87 case __GCONV_OK:
e34b0f29
UD
88 /* Nothing. */
89 break;
6973fc01 90
e34b0f29
UD
91 default:
92 assert (!"Nothing like this should happen");
93 }
6973fc01 94
38677ace 95 return irreversible;
6973fc01 96}