Update.
[glibc.git] / iconvdata / euc-jp.c
blob3d97c01ce9aa5e74316552ba58236390a3886c97
1 /* Mapping tables for EUC-JP handling.
2 Copyright (C) 1998, 1999 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
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.
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. */
21 #include <stdint.h>
22 #include <gconv.h>
23 #include <jis0201.h>
24 #include <jis0208.h>
25 #include <jis0212.h>
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
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; \
47 if (ch <= 0x7f) \
48 ++inptr; \
49 else if ((ch <= 0xa0 || ch > 0xfe) && ch != 0x8e && ch != 0x8f) \
50 { \
51 /* This is illegal. */ \
52 result = __GCONV_ILLEGAL_INPUT; \
53 break; \
54 } \
55 else \
56 { \
57 /* Two or more byte character. First test whether the next \
58 character is also available. */ \
59 int ch2; \
61 if (NEED_LENGTH_TEST && inptr + 1 >= inend) \
62 { \
63 /* The second character is not available. Store the \
64 intermediate result. */ \
65 result = __GCONV_INCOMPLETE_INPUT; \
66 break; \
67 } \
69 ch2 = inptr[1]; \
71 /* All second bytes of a multibyte character must be >= 0xa1. */ \
72 if (ch2 < 0xa1) \
73 { \
74 /* This is an illegal character. */ \
75 result = __GCONV_ILLEGAL_INPUT; \
76 break; \
77 } \
79 if (ch == 0x8e) \
80 { \
81 /* This is code set 2: half-width katakana. */ \
82 ch = jisx0201_to_ucs4 (ch2); \
83 inptr += 2; \
84 } \
85 else \
86 { \
87 const unsigned char *endp; \
89 if (ch == 0x8f) \
90 { \
91 /* This is code set 3: JIS X 0212-1990. */ \
92 endp = inptr + 1; \
94 ch = jisx0212_to_ucs4 (&endp, \
95 NEED_LENGTH_TEST ? inend - endp : 2, \
96 0x80); \
97 } \
98 else \
99 { \
100 /* This is code set 1: JIS X 0208. */ \
101 endp = inptr; \
103 ch = jisx0208_to_ucs4 (&endp, \
104 NEED_LENGTH_TEST ? inend - inptr : 2, \
105 0x80); \
108 if (NEED_LENGTH_TEST && ch == 0) \
110 /* Not enough input available. */ \
111 result = __GCONV_INCOMPLETE_INPUT; \
112 break; \
114 if (ch == __UNKNOWN_10646_CHAR) \
116 /* Illegal character. */ \
117 result = __GCONV_ILLEGAL_INPUT; \
118 break; \
120 inptr = endp; \
124 *((uint32_t *) outptr)++ = ch; \
126 #include <iconv/loop.c>
129 /* Next, define the other direction. */
130 #define MIN_NEEDED_INPUT MIN_NEEDED_TO
131 #define MIN_NEEDED_OUTPUT MIN_NEEDED_FROM
132 #define MAX_NEEDED_OUTPUT MAX_NEEDED_FROM
133 #define LOOPFCT TO_LOOP
134 #define BODY \
136 uint32_t ch = *((uint32_t *) inptr); \
138 if (ch <= 0x7f) \
139 /* It's plain ASCII. */ \
140 *outptr++ = ch; \
141 else if (ch == 0xa5) \
142 /* YEN sign => backslash */ \
143 *outptr++ = 0x5c; \
144 else if (ch == 0x203e) \
145 /* overscore => asciitilde */ \
146 *outptr++ = 0x7e; \
147 else \
149 /* Try the JIS character sets. */ \
150 size_t found; \
152 /* See whether we have room for at least two characters. */ \
153 if (NEED_LENGTH_TEST && outptr + 1 >= outend) \
155 result = __GCONV_FULL_OUTPUT; \
156 break; \
159 found = ucs4_to_jisx0201 (ch, outptr + 1); \
160 if (found != __UNKNOWN_10646_CHAR) \
162 /* Yes, it's a JIS 0201 character. Store the shift byte. */ \
163 *outptr = 0x8e; \
164 outptr += 2; \
166 else \
168 /* No JIS 0201 character. */ \
169 found = ucs4_to_jisx0208 (ch, outptr, 2); \
170 /* Please note that we always have enough room for the output. */ \
171 if (found != __UNKNOWN_10646_CHAR) \
173 /* It's a JIS 0208 character, adjust it for EUC-JP. */ \
174 *outptr++ += 0x80; \
175 *outptr++ += 0x80; \
177 else \
179 /* No JIS 0208 character. */ \
180 found = ucs4_to_jisx0212 (ch, outptr + 1, \
181 (NEED_LENGTH_TEST \
182 ? outend - outptr - 1 : 2)); \
184 if (found == 0) \
186 /* We ran out of space. */ \
187 result = __GCONV_FULL_OUTPUT; \
188 break; \
190 else if (found != __UNKNOWN_10646_CHAR) \
192 /* It's a JIS 0212 character, adjust it for EUC-JP. */ \
193 *outptr++ = 0x8f; \
194 *outptr++ += 0x80; \
195 *outptr++ += 0x80; \
197 else \
199 /* Illegal character. */ \
200 result = __GCONV_ILLEGAL_INPUT; \
201 break; \
207 inptr += 4; \
209 #include <iconv/loop.c>
212 /* Now define the toplevel functions. */
213 #include <iconv/skeleton.c>