Fix macro file 'serial' numbers for 'aclocal --install'.
[libiconv.git] / lib / euc_jp.h
blobf147c47afda0eea4b2399a7c2ebb86d4ad7f3da4
1 /*
2 * Copyright (C) 1999-2001, 2005, 2016 Free Software Foundation, Inc.
3 * This file is part of the GNU LIBICONV Library.
5 * The GNU LIBICONV Library is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either version 2.1
8 * of the License, or (at your option) any later version.
10 * The GNU LIBICONV Library is distributed in the hope that it will be
11 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
17 * If not, see <https://www.gnu.org/licenses/>.
21 * EUC-JP
24 static int
25 euc_jp_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
27 unsigned char c = *s;
28 /* Code set 0 (ASCII or JIS X 0201-1976 Roman) */
29 if (c < 0x80)
30 return ascii_mbtowc(conv,pwc,s,n);
31 /* Code set 1 (JIS X 0208) */
32 if (c >= 0xa1 && c < 0xff) {
33 if (n < 2)
34 return RET_TOOFEW(0);
35 if (c < 0xf5) {
36 unsigned char c2 = s[1];
37 if (c2 >= 0xa1 && c2 < 0xff) {
38 unsigned char buf[2];
39 buf[0] = c-0x80; buf[1] = c2-0x80;
40 return jisx0208_mbtowc(conv,pwc,buf,2);
41 } else
42 return RET_ILSEQ;
43 } else {
44 /* User-defined range. See
45 * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
46 unsigned char c2 = s[1];
47 if (c2 >= 0xa1 && c2 < 0xff) {
48 *pwc = 0xe000 + 94*(c-0xf5) + (c2-0xa1);
49 return 2;
50 } else
51 return RET_ILSEQ;
54 /* Code set 2 (half-width katakana) */
55 if (c == 0x8e) {
56 if (n < 2)
57 return RET_TOOFEW(0);
59 unsigned char c2 = s[1];
60 if (c2 >= 0xa1 && c2 < 0xe0) {
61 int ret = jisx0201_mbtowc(conv,pwc,s+1,n-1);
62 if (ret == RET_ILSEQ)
63 return RET_ILSEQ;
64 if (ret != 1) abort();
65 return 2;
66 } else
67 return RET_ILSEQ;
70 /* Code set 3 (JIS X 0212-1990) */
71 if (c == 0x8f) {
72 if (n < 2)
73 return RET_TOOFEW(0);
75 unsigned char c2 = s[1];
76 if (c2 >= 0xa1 && c2 < 0xff) {
77 if (n < 3)
78 return RET_TOOFEW(0);
79 if (c2 < 0xf5) {
80 unsigned char c3 = s[2];
81 if (c3 >= 0xa1 && c3 < 0xff) {
82 unsigned char buf[2];
83 int ret;
84 buf[0] = c2-0x80; buf[1] = c3-0x80;
85 ret = jisx0212_mbtowc(conv,pwc,buf,2);
86 if (ret == RET_ILSEQ)
87 return RET_ILSEQ;
88 if (ret != 2) abort();
89 return 3;
90 } else
91 return RET_ILSEQ;
92 } else {
93 /* User-defined range. See
94 * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
95 unsigned char c3 = s[2];
96 if (c3 >= 0xa1 && c3 < 0xff) {
97 *pwc = 0xe3ac + 94*(c2-0xf5) + (c3-0xa1);
98 return 3;
99 } else
100 return RET_ILSEQ;
102 } else
103 return RET_ILSEQ;
106 return RET_ILSEQ;
109 static int
110 euc_jp_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
112 unsigned char buf[2];
113 int ret;
115 /* Code set 0 (ASCII or JIS X 0201-1976 Roman) */
116 ret = ascii_wctomb(conv,r,wc,n);
117 if (ret != RET_ILUNI)
118 return ret;
120 /* Code set 1 (JIS X 0208) */
121 ret = jisx0208_wctomb(conv,buf,wc,2);
122 if (ret != RET_ILUNI) {
123 if (ret != 2) abort();
124 if (n < 2)
125 return RET_TOOSMALL;
126 r[0] = buf[0]+0x80;
127 r[1] = buf[1]+0x80;
128 return 2;
131 /* Code set 2 (half-width katakana) */
132 ret = jisx0201_wctomb(conv,buf,wc,1);
133 if (ret != RET_ILUNI && buf[0] >= 0x80) {
134 if (ret != 1) abort();
135 if (n < 2)
136 return RET_TOOSMALL;
137 r[0] = 0x8e;
138 r[1] = buf[0];
139 return 2;
142 /* Code set 3 (JIS X 0212-1990) */
143 ret = jisx0212_wctomb(conv,buf,wc,2);
144 if (ret != RET_ILUNI) {
145 if (ret != 2) abort();
146 if (n < 3)
147 return RET_TOOSMALL;
148 r[0] = 0x8f;
149 r[1] = buf[0]+0x80;
150 r[2] = buf[1]+0x80;
151 return 3;
154 /* Extra compatibility with Shift_JIS. */
155 if (wc == 0x00a5) {
156 r[0] = 0x5c;
157 return 1;
159 if (wc == 0x203e) {
160 r[0] = 0x7e;
161 return 1;
164 /* User-defined range. See
165 * Ken Lunde's "CJKV Information Processing", table 4-66, p. 206. */
166 if (wc >= 0xe000 && wc < 0xe758) {
167 if (wc < 0xe3ac) {
168 unsigned char c1, c2;
169 if (n < 2)
170 return RET_TOOSMALL;
171 c1 = (unsigned int) (wc - 0xe000) / 94;
172 c2 = (unsigned int) (wc - 0xe000) % 94;
173 r[0] = c1+0xf5;
174 r[1] = c2+0xa1;
175 return 2;
176 } else {
177 unsigned char c1, c2;
178 if (n < 3)
179 return RET_TOOSMALL;
180 c1 = (unsigned int) (wc - 0xe3ac) / 94;
181 c2 = (unsigned int) (wc - 0xe3ac) % 94;
182 r[0] = 0x8f;
183 r[1] = c1+0xf5;
184 r[2] = c2+0xa1;
185 return 3;
189 return RET_ILUNI;