2d66f8b6662f16ef4f2678c8367884758d3f57e6
[platform/upstream/glibc.git] / iconvdata / 8bit-gap.c
1 /* Generic conversion to and from 8bit charsets,
2    converting from UCS using gaps.
3    Copyright (C) 1997, 1998, 1999, 2000 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 Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    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    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the GNU C Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.  */
21
22 #include <stdint.h>
23
24 struct gap
25 {
26   uint16_t start;
27   uint16_t end;
28   int32_t idx;
29 };
30
31 /* Now we can include the tables.  */
32 #include TABLES
33
34
35 #define FROM_LOOP               from_gap
36 #define TO_LOOP                 to_gap
37 #define DEFINE_INIT             1
38 #define DEFINE_FINI             1
39 #define MIN_NEEDED_FROM         1
40 #define MIN_NEEDED_TO           4
41
42
43 /* First define the conversion function from the 8bit charset to UCS4.  */
44 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
45 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
46 #define LOOPFCT                 FROM_LOOP
47 #define BODY \
48   {                                                                           \
49     uint32_t ch = to_ucs4[*inptr];                                            \
50                                                                               \
51     if (HAS_HOLES && __builtin_expect (ch, L'\1') == L'\0' && *inptr != '\0') \
52       {                                                                       \
53         /* This is an illegal character.  */                                  \
54         if (! ignore_errors_p ())                                             \
55           {                                                                   \
56             result = __GCONV_ILLEGAL_INPUT;                                   \
57             break;                                                            \
58           }                                                                   \
59                                                                               \
60         ++*irreversible;                                                      \
61       }                                                                       \
62     else                                                                      \
63       {                                                                       \
64         put32 (outptr, ch);                                                   \
65         outptr += 4;                                                          \
66       }                                                                       \
67                                                                               \
68     ++inptr;                                                                  \
69   }
70 #include <iconv/loop.c>
71
72
73 /* Next, define the other direction.  */
74 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
75 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
76 #define LOOPFCT                 TO_LOOP
77 #define BODY \
78   {                                                                           \
79     const struct gap *rp = from_idx;                                          \
80     uint32_t ch = get32 (inptr);                                              \
81     unsigned char res;                                                        \
82                                                                               \
83     if (__builtin_expect (ch, 0) >= 0xffff)                                   \
84       {                                                                       \
85         /* This is an illegal character.  */                                  \
86         if (! ignore_errors_p ())                                             \
87           {                                                                   \
88             result = __GCONV_ILLEGAL_INPUT;                                   \
89             break;                                                            \
90           }                                                                   \
91                                                                               \
92         ++*irreversible;                                                      \
93         inptr += 4;                                                           \
94         continue;                                                             \
95       }                                                                       \
96     while (ch > rp->end)                                                      \
97       ++rp;                                                                   \
98     if (__builtin_expect (ch < rp->start, 0))                                 \
99       {                                                                       \
100         /* This is an illegal character.  */                                  \
101         if (! ignore_errors_p ())                                             \
102           {                                                                   \
103             result = __GCONV_ILLEGAL_INPUT;                                   \
104             break;                                                            \
105           }                                                                   \
106                                                                               \
107         ++*irreversible;                                                      \
108         inptr += 4;                                                           \
109         continue;                                                             \
110       }                                                                       \
111                                                                               \
112     res = from_ucs4[ch + rp->idx];                                            \
113     if (__builtin_expect (res, '\1') == '\0' && ch != 0)                      \
114       {                                                                       \
115         /* This is an illegal character.  */                                  \
116         if (! ignore_errors_p ())                                             \
117           {                                                                   \
118             result = __GCONV_ILLEGAL_INPUT;                                   \
119             break;                                                            \
120           }                                                                   \
121                                                                               \
122         ++*irreversible;                                                      \
123         inptr += 4;                                                           \
124         continue;                                                             \
125       }                                                                       \
126                                                                               \
127     *outptr++ = res;                                                          \
128     inptr += 4;                                                               \
129   }
130 #include <iconv/loop.c>
131
132
133 /* Now define the toplevel functions.  */
134 #include <iconv/skeleton.c>