Rename converted variable to correcter name irreversible.
[kopensolaris-gnu/glibc.git] / iconvdata / euc-jp.c
1 /* Mapping tables for EUC-JP handling.
2    Copyright (C) 1998, 1999, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <stdint.h>
22 #include <gconv.h>
23 #include <jis0201.h>
24 #include <jis0208.h>
25 #include <jis0212.h>
26
27 /* Definitions used in the body of the `gconv' function.  */
28 #define CHARSET_NAME            "EUC-JP//"
29 #define FROM_LOOP               from_euc_jp
30 #define TO_LOOP                 to_euc_jp
31 #define DEFINE_INIT             1
32 #define DEFINE_FINI             1
33 #define MIN_NEEDED_FROM         1
34 #define MAX_NEEDED_FROM         3
35 #define MIN_NEEDED_TO           4
36
37
38 /* First define the conversion function from EUC-JP to UCS4.  */
39 #define MIN_NEEDED_INPUT        MIN_NEEDED_FROM
40 #define MAX_NEEDED_INPUT        MAX_NEEDED_FROM
41 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_TO
42 #define LOOPFCT                 FROM_LOOP
43 #define BODY \
44   {                                                                           \
45     uint32_t ch = *inptr;                                                     \
46                                                                               \
47     if (ch <= 0x7f)                                                           \
48       ++inptr;                                                                \
49     else if ((__builtin_expect (ch, 0xa1) <= 0xa0 && ch != 0x8e && ch != 0x8f)\
50              || __builtin_expect (ch, 0xfe) > 0xfe)                           \
51       {                                                                       \
52         /* This is illegal.  */                                               \
53         if (! ignore_errors_p ())                                             \
54           {                                                                   \
55             result = __GCONV_ILLEGAL_INPUT;                                   \
56             break;                                                            \
57           }                                                                   \
58                                                                               \
59         ++inptr;                                                              \
60         ++*irreversible;                                                      \
61         continue;                                                             \
62       }                                                                       \
63     else                                                                      \
64       {                                                                       \
65         /* Two or more byte character.  First test whether the next           \
66            character is also available.  */                                   \
67         int ch2;                                                              \
68                                                                               \
69         if (NEED_LENGTH_TEST && __builtin_expect (inptr + 1 >= inend, 0))     \
70           {                                                                   \
71             /* The second character is not available.  Store the              \
72                intermediate result.  */                                       \
73             result = __GCONV_INCOMPLETE_INPUT;                                \
74             break;                                                            \
75           }                                                                   \
76                                                                               \
77         ch2 = inptr[1];                                                       \
78                                                                               \
79         /* All second bytes of a multibyte character must be >= 0xa1. */      \
80         if (__builtin_expect (ch2, 0xa1) < 0xa1)                              \
81           {                                                                   \
82             /* This is an illegal character.  */                              \
83             if (! ignore_errors_p ())                                         \
84               {                                                               \
85                 result = __GCONV_ILLEGAL_INPUT;                               \
86                 break;                                                        \
87               }                                                               \
88                                                                               \
89             ++inptr;                                                          \
90             ++*irreversible;                                                  \
91             continue;                                                         \
92           }                                                                   \
93                                                                               \
94         if (ch == 0x8e)                                                       \
95           {                                                                   \
96             /* This is code set 2: half-width katakana.  */                   \
97             ch = jisx0201_to_ucs4 (ch2);                                      \
98             inptr += 2;                                                       \
99           }                                                                   \
100         else                                                                  \
101           {                                                                   \
102             const unsigned char *endp;                                        \
103                                                                               \
104             if (ch == 0x8f)                                                   \
105               {                                                               \
106                 /* This is code set 3: JIS X 0212-1990.  */                   \
107                 endp = inptr + 1;                                             \
108                                                                               \
109                 ch = jisx0212_to_ucs4 (&endp,                                 \
110                                        NEED_LENGTH_TEST ? inend - endp : 2,   \
111                                        0x80);                                 \
112               }                                                               \
113             else                                                              \
114               {                                                               \
115                 /* This is code set 1: JIS X 0208.  */                        \
116                 endp = inptr;                                                 \
117                                                                               \
118                 ch = jisx0208_to_ucs4 (&endp,                                 \
119                                        NEED_LENGTH_TEST ? inend - inptr : 2,  \
120                                        0x80);                                 \
121               }                                                               \
122                                                                               \
123             if (NEED_LENGTH_TEST && __builtin_expect (ch, 1) == 0)            \
124               {                                                               \
125                 /* Not enough input available.  */                            \
126                 result = __GCONV_INCOMPLETE_INPUT;                            \
127                 break;                                                        \
128               }                                                               \
129             if (__builtin_expect (ch, 0) == __UNKNOWN_10646_CHAR)             \
130               {                                                               \
131                 /* Illegal character.  */                                     \
132                 if (! ignore_errors_p ())                                     \
133                   {                                                           \
134                     /* This is an illegal character.  */                      \
135                     result = __GCONV_ILLEGAL_INPUT;                           \
136                     break;                                                    \
137                   }                                                           \
138                                                                               \
139                 inptr += 2;                                                   \
140                 ++*irreversible;                                              \
141                 continue;                                                     \
142               }                                                               \
143             inptr = endp;                                                     \
144           }                                                                   \
145       }                                                                       \
146                                                                               \
147     put32 (outptr, ch);                                                       \
148     outptr += 4;                                                              \
149   }
150 #include <iconv/loop.c>
151
152
153 /* Next, define the other direction.  */
154 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
155 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
156 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
157 #define LOOPFCT                 TO_LOOP
158 #define BODY \
159   {                                                                           \
160     uint32_t ch = get32 (inptr);                                              \
161                                                                               \
162     if (ch <= 0x7f)                                                           \
163       /* It's plain ASCII.  */                                                \
164       *outptr++ = ch;                                                         \
165     else if (ch == 0xa5)                                                      \
166       /* YEN sign => backslash  */                                            \
167       *outptr++ = 0x5c;                                                       \
168     else if (ch == 0x203e)                                                    \
169       /* overscore => asciitilde */                                           \
170       *outptr++ = 0x7e;                                                       \
171     else                                                                      \
172       {                                                                       \
173         /* Try the JIS character sets.  */                                    \
174         size_t found;                                                         \
175                                                                               \
176         /* See whether we have room for at least two characters.  */          \
177         if (NEED_LENGTH_TEST && __builtin_expect (outptr + 1 >= outend, 0))   \
178           {                                                                   \
179             result = __GCONV_FULL_OUTPUT;                                     \
180             break;                                                            \
181           }                                                                   \
182                                                                               \
183         found = ucs4_to_jisx0201 (ch, outptr + 1);                            \
184         if (found != __UNKNOWN_10646_CHAR)                                    \
185           {                                                                   \
186             /* Yes, it's a JIS 0201 character.  Store the shift byte.  */     \
187             *outptr = 0x8e;                                                   \
188             outptr += 2;                                                      \
189           }                                                                   \
190         else                                                                  \
191           {                                                                   \
192             /* No JIS 0201 character.  */                                     \
193             found = ucs4_to_jisx0208 (ch, outptr, 2);                         \
194             /* Please note that we always have enough room for the output. */ \
195             if (found != __UNKNOWN_10646_CHAR)                                \
196               {                                                               \
197                 /* It's a JIS 0208 character, adjust it for EUC-JP.  */       \
198                 *outptr++ += 0x80;                                            \
199                 *outptr++ += 0x80;                                            \
200               }                                                               \
201             else                                                              \
202               {                                                               \
203                 /* No JIS 0208 character.  */                                 \
204                 found = ucs4_to_jisx0212 (ch, outptr + 1,                     \
205                                           (NEED_LENGTH_TEST                   \
206                                            ? outend - outptr - 1 : 2));       \
207                                                                               \
208                 if (__builtin_expect (found, 1) == 0)                         \
209                   {                                                           \
210                     /* We ran out of space.  */                               \
211                     result = __GCONV_FULL_OUTPUT;                             \
212                     break;                                                    \
213                   }                                                           \
214                 else if (__builtin_expect (found, 0) != __UNKNOWN_10646_CHAR) \
215                   {                                                           \
216                     /* It's a JIS 0212 character, adjust it for EUC-JP.  */   \
217                     *outptr++ = 0x8f;                                         \
218                     *outptr++ += 0x80;                                        \
219                     *outptr++ += 0x80;                                        \
220                   }                                                           \
221                 else                                                          \
222                   {                                                           \
223                     /* Illegal character.  */                                 \
224                     if (! ignore_errors_p ())                                 \
225                       {                                                       \
226                         result = __GCONV_ILLEGAL_INPUT;                       \
227                         break;                                                \
228                       }                                                       \
229                                                                               \
230                     inptr += 4;                                               \
231                     ++*irreversible;                                          \
232                     continue;                                                 \
233                   }                                                           \
234               }                                                               \
235           }                                                                   \
236       }                                                                       \
237                                                                               \
238     inptr += 4;                                                               \
239   }
240 #include <iconv/loop.c>
241
242
243 /* Now define the toplevel functions.  */
244 #include <iconv/skeleton.c>