Don't return error for invald error if ignore flag is set.
[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 ((ch <= 0xa0 && ch != 0x8e && ch != 0x8f) || ch > 0xfe)           \
50       {                                                                       \
51         /* This is illegal.  */                                               \
52         if (! ignore_errors_p ())                                             \
53           {                                                                   \
54             result = __GCONV_ILLEGAL_INPUT;                                   \
55             break;                                                            \
56           }                                                                   \
57                                                                               \
58         ++inptr;                                                              \
59         ++*converted;                                                         \
60         continue;                                                             \
61       }                                                                       \
62     else                                                                      \
63       {                                                                       \
64         /* Two or more byte character.  First test whether the next           \
65            character is also available.  */                                   \
66         int ch2;                                                              \
67                                                                               \
68         if (NEED_LENGTH_TEST && inptr + 1 >= inend)                           \
69           {                                                                   \
70             /* The second character is not available.  Store the              \
71                intermediate result.  */                                       \
72             result = __GCONV_INCOMPLETE_INPUT;                                \
73             break;                                                            \
74           }                                                                   \
75                                                                               \
76         ch2 = inptr[1];                                                       \
77                                                                               \
78         /* All second bytes of a multibyte character must be >= 0xa1. */      \
79         if (ch2 < 0xa1)                                                       \
80           {                                                                   \
81             /* This is an illegal character.  */                              \
82             if (! ignore_errors_p ())                                         \
83               {                                                               \
84                 result = __GCONV_ILLEGAL_INPUT;                               \
85                 break;                                                        \
86               }                                                               \
87                                                                               \
88             ++inptr;                                                          \
89             ++*converted;                                                     \
90             continue;                                                         \
91           }                                                                   \
92                                                                               \
93         if (ch == 0x8e)                                                       \
94           {                                                                   \
95             /* This is code set 2: half-width katakana.  */                   \
96             ch = jisx0201_to_ucs4 (ch2);                                      \
97             inptr += 2;                                                       \
98           }                                                                   \
99         else                                                                  \
100           {                                                                   \
101             const unsigned char *endp;                                        \
102                                                                               \
103             if (ch == 0x8f)                                                   \
104               {                                                               \
105                 /* This is code set 3: JIS X 0212-1990.  */                   \
106                 endp = inptr + 1;                                             \
107                                                                               \
108                 ch = jisx0212_to_ucs4 (&endp,                                 \
109                                        NEED_LENGTH_TEST ? inend - endp : 2,   \
110                                        0x80);                                 \
111               }                                                               \
112             else                                                              \
113               {                                                               \
114                 /* This is code set 1: JIS X 0208.  */                        \
115                 endp = inptr;                                                 \
116                                                                               \
117                 ch = jisx0208_to_ucs4 (&endp,                                 \
118                                        NEED_LENGTH_TEST ? inend - inptr : 2,  \
119                                        0x80);                                 \
120               }                                                               \
121                                                                               \
122             if (NEED_LENGTH_TEST && ch == 0)                                  \
123               {                                                               \
124                 /* Not enough input available.  */                            \
125                 result = __GCONV_INCOMPLETE_INPUT;                            \
126                 break;                                                        \
127               }                                                               \
128             if (ch == __UNKNOWN_10646_CHAR)                                   \
129               {                                                               \
130                 /* Illegal character.  */                                     \
131                 if (! ignore_errors_p ())                                     \
132                   {                                                           \
133                     /* This is an illegal character.  */                      \
134                     result = __GCONV_ILLEGAL_INPUT;                           \
135                     break;                                                    \
136                   }                                                           \
137                                                                               \
138                 inptr += 2;                                                   \
139                 ++*converted;                                                 \
140                 continue;                                                     \
141               }                                                               \
142             inptr = endp;                                                     \
143           }                                                                   \
144       }                                                                       \
145                                                                               \
146     put32 (outptr, ch);                                                       \
147     outptr += 4;                                                              \
148   }
149 #include <iconv/loop.c>
150
151
152 /* Next, define the other direction.  */
153 #define MIN_NEEDED_INPUT        MIN_NEEDED_TO
154 #define MIN_NEEDED_OUTPUT       MIN_NEEDED_FROM
155 #define MAX_NEEDED_OUTPUT       MAX_NEEDED_FROM
156 #define LOOPFCT                 TO_LOOP
157 #define BODY \
158   {                                                                           \
159     uint32_t ch = get32 (inptr);                                              \
160                                                                               \
161     if (ch <= 0x7f)                                                           \
162       /* It's plain ASCII.  */                                                \
163       *outptr++ = ch;                                                         \
164     else if (ch == 0xa5)                                                      \
165       /* YEN sign => backslash  */                                            \
166       *outptr++ = 0x5c;                                                       \
167     else if (ch == 0x203e)                                                    \
168       /* overscore => asciitilde */                                           \
169       *outptr++ = 0x7e;                                                       \
170     else                                                                      \
171       {                                                                       \
172         /* Try the JIS character sets.  */                                    \
173         size_t found;                                                         \
174                                                                               \
175         /* See whether we have room for at least two characters.  */          \
176         if (NEED_LENGTH_TEST && outptr + 1 >= outend)                         \
177           {                                                                   \
178             result = __GCONV_FULL_OUTPUT;                                     \
179             break;                                                            \
180           }                                                                   \
181                                                                               \
182         found = ucs4_to_jisx0201 (ch, outptr + 1);                            \
183         if (found != __UNKNOWN_10646_CHAR)                                    \
184           {                                                                   \
185             /* Yes, it's a JIS 0201 character.  Store the shift byte.  */     \
186             *outptr = 0x8e;                                                   \
187             outptr += 2;                                                      \
188           }                                                                   \
189         else                                                                  \
190           {                                                                   \
191             /* No JIS 0201 character.  */                                     \
192             found = ucs4_to_jisx0208 (ch, outptr, 2);                         \
193             /* Please note that we always have enough room for the output. */ \
194             if (found != __UNKNOWN_10646_CHAR)                                \
195               {                                                               \
196                 /* It's a JIS 0208 character, adjust it for EUC-JP.  */       \
197                 *outptr++ += 0x80;                                            \
198                 *outptr++ += 0x80;                                            \
199               }                                                               \
200             else                                                              \
201               {                                                               \
202                 /* No JIS 0208 character.  */                                 \
203                 found = ucs4_to_jisx0212 (ch, outptr + 1,                     \
204                                           (NEED_LENGTH_TEST                   \
205                                            ? outend - outptr - 1 : 2));       \
206                                                                               \
207                 if (found == 0)                                               \
208                   {                                                           \
209                     /* We ran out of space.  */                               \
210                     result = __GCONV_FULL_OUTPUT;                             \
211                     break;                                                    \
212                   }                                                           \
213                 else if (found != __UNKNOWN_10646_CHAR)                       \
214                   {                                                           \
215                     /* It's a JIS 0212 character, adjust it for EUC-JP.  */   \
216                     *outptr++ = 0x8f;                                         \
217                     *outptr++ += 0x80;                                        \
218                     *outptr++ += 0x80;                                        \
219                   }                                                           \
220                 else                                                          \
221                   {                                                           \
222                     /* Illegal character.  */                                 \
223                     if (! ignore_errors_p ())                                 \
224                       {                                                       \
225                         result = __GCONV_ILLEGAL_INPUT;                       \
226                         break;                                                \
227                       }                                                       \
228                                                                               \
229                     inptr += 4;                                               \
230                     ++*converted;                                             \
231                     continue;                                                 \
232                   }                                                           \
233               }                                                               \
234           }                                                                   \
235       }                                                                       \
236                                                                               \
237     inptr += 4;                                                               \
238   }
239 #include <iconv/loop.c>
240
241
242 /* Now define the toplevel functions.  */
243 #include <iconv/skeleton.c>