1 | n/a | /* |
---|
2 | n/a | * _codecs_jp.c: Codecs collection for Japanese encodings |
---|
3 | n/a | * |
---|
4 | n/a | * Written by Hye-Shik Chang <perky@FreeBSD.org> |
---|
5 | n/a | */ |
---|
6 | n/a | |
---|
7 | n/a | #define USING_BINARY_PAIR_SEARCH |
---|
8 | n/a | #define EMPBASE 0x20000 |
---|
9 | n/a | |
---|
10 | n/a | #include "cjkcodecs.h" |
---|
11 | n/a | #include "mappings_jp.h" |
---|
12 | n/a | #include "mappings_jisx0213_pair.h" |
---|
13 | n/a | #include "alg_jisx0201.h" |
---|
14 | n/a | #include "emu_jisx0213_2000.h" |
---|
15 | n/a | |
---|
16 | n/a | /* |
---|
17 | n/a | * CP932 codec |
---|
18 | n/a | */ |
---|
19 | n/a | |
---|
20 | n/a | ENCODER(cp932) |
---|
21 | n/a | { |
---|
22 | n/a | while (*inpos < inlen) { |
---|
23 | n/a | Py_UCS4 c = INCHAR1; |
---|
24 | n/a | DBCHAR code; |
---|
25 | n/a | unsigned char c1, c2; |
---|
26 | n/a | |
---|
27 | n/a | if (c <= 0x80) { |
---|
28 | n/a | WRITEBYTE1((unsigned char)c); |
---|
29 | n/a | NEXT(1, 1); |
---|
30 | n/a | continue; |
---|
31 | n/a | } |
---|
32 | n/a | else if (c >= 0xff61 && c <= 0xff9f) { |
---|
33 | n/a | WRITEBYTE1(c - 0xfec0); |
---|
34 | n/a | NEXT(1, 1); |
---|
35 | n/a | continue; |
---|
36 | n/a | } |
---|
37 | n/a | else if (c >= 0xf8f0 && c <= 0xf8f3) { |
---|
38 | n/a | /* Windows compatibility */ |
---|
39 | n/a | REQUIRE_OUTBUF(1); |
---|
40 | n/a | if (c == 0xf8f0) |
---|
41 | n/a | OUTBYTE1(0xa0); |
---|
42 | n/a | else |
---|
43 | n/a | OUTBYTE1(c - 0xfef1 + 0xfd); |
---|
44 | n/a | NEXT(1, 1); |
---|
45 | n/a | continue; |
---|
46 | n/a | } |
---|
47 | n/a | |
---|
48 | n/a | if (c > 0xFFFF) |
---|
49 | n/a | return 1; |
---|
50 | n/a | REQUIRE_OUTBUF(2); |
---|
51 | n/a | |
---|
52 | n/a | if (TRYMAP_ENC(cp932ext, code, c)) { |
---|
53 | n/a | OUTBYTE1(code >> 8); |
---|
54 | n/a | OUTBYTE2(code & 0xff); |
---|
55 | n/a | } |
---|
56 | n/a | else if (TRYMAP_ENC(jisxcommon, code, c)) { |
---|
57 | n/a | if (code & 0x8000) /* MSB set: JIS X 0212 */ |
---|
58 | n/a | return 1; |
---|
59 | n/a | |
---|
60 | n/a | /* JIS X 0208 */ |
---|
61 | n/a | c1 = code >> 8; |
---|
62 | n/a | c2 = code & 0xff; |
---|
63 | n/a | c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21); |
---|
64 | n/a | c1 = (c1 - 0x21) >> 1; |
---|
65 | n/a | OUTBYTE1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1); |
---|
66 | n/a | OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41); |
---|
67 | n/a | } |
---|
68 | n/a | else if (c >= 0xe000 && c < 0xe758) { |
---|
69 | n/a | /* User-defined area */ |
---|
70 | n/a | c1 = (Py_UCS4)(c - 0xe000) / 188; |
---|
71 | n/a | c2 = (Py_UCS4)(c - 0xe000) % 188; |
---|
72 | n/a | OUTBYTE1(c1 + 0xf0); |
---|
73 | n/a | OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41); |
---|
74 | n/a | } |
---|
75 | n/a | else |
---|
76 | n/a | return 1; |
---|
77 | n/a | |
---|
78 | n/a | NEXT(1, 2); |
---|
79 | n/a | } |
---|
80 | n/a | |
---|
81 | n/a | return 0; |
---|
82 | n/a | } |
---|
83 | n/a | |
---|
84 | n/a | DECODER(cp932) |
---|
85 | n/a | { |
---|
86 | n/a | while (inleft > 0) { |
---|
87 | n/a | unsigned char c = INBYTE1, c2; |
---|
88 | n/a | Py_UCS4 decoded; |
---|
89 | n/a | |
---|
90 | n/a | if (c <= 0x80) { |
---|
91 | n/a | OUTCHAR(c); |
---|
92 | n/a | NEXT_IN(1); |
---|
93 | n/a | continue; |
---|
94 | n/a | } |
---|
95 | n/a | else if (c >= 0xa0 && c <= 0xdf) { |
---|
96 | n/a | if (c == 0xa0) |
---|
97 | n/a | OUTCHAR(0xf8f0); /* half-width katakana */ |
---|
98 | n/a | else |
---|
99 | n/a | OUTCHAR(0xfec0 + c); |
---|
100 | n/a | NEXT_IN(1); |
---|
101 | n/a | continue; |
---|
102 | n/a | } |
---|
103 | n/a | else if (c >= 0xfd/* && c <= 0xff*/) { |
---|
104 | n/a | /* Windows compatibility */ |
---|
105 | n/a | OUTCHAR(0xf8f1 - 0xfd + c); |
---|
106 | n/a | NEXT_IN(1); |
---|
107 | n/a | continue; |
---|
108 | n/a | } |
---|
109 | n/a | |
---|
110 | n/a | REQUIRE_INBUF(2); |
---|
111 | n/a | c2 = INBYTE2; |
---|
112 | n/a | |
---|
113 | n/a | if (TRYMAP_DEC(cp932ext, decoded, c, c2)) |
---|
114 | n/a | OUTCHAR(decoded); |
---|
115 | n/a | else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)){ |
---|
116 | n/a | if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc) |
---|
117 | n/a | return 1; |
---|
118 | n/a | |
---|
119 | n/a | c = (c < 0xe0 ? c - 0x81 : c - 0xc1); |
---|
120 | n/a | c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41); |
---|
121 | n/a | c = (2 * c + (c2 < 0x5e ? 0 : 1) + 0x21); |
---|
122 | n/a | c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21; |
---|
123 | n/a | |
---|
124 | n/a | if (TRYMAP_DEC(jisx0208, decoded, c, c2)) |
---|
125 | n/a | OUTCHAR(decoded); |
---|
126 | n/a | else |
---|
127 | n/a | return 1; |
---|
128 | n/a | } |
---|
129 | n/a | else if (c >= 0xf0 && c <= 0xf9) { |
---|
130 | n/a | if ((c2 >= 0x40 && c2 <= 0x7e) || |
---|
131 | n/a | (c2 >= 0x80 && c2 <= 0xfc)) |
---|
132 | n/a | OUTCHAR(0xe000 + 188 * (c - 0xf0) + |
---|
133 | n/a | (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41)); |
---|
134 | n/a | else |
---|
135 | n/a | return 1; |
---|
136 | n/a | } |
---|
137 | n/a | else |
---|
138 | n/a | return 1; |
---|
139 | n/a | |
---|
140 | n/a | NEXT_IN(2); |
---|
141 | n/a | } |
---|
142 | n/a | |
---|
143 | n/a | return 0; |
---|
144 | n/a | } |
---|
145 | n/a | |
---|
146 | n/a | |
---|
147 | n/a | /* |
---|
148 | n/a | * EUC-JIS-2004 codec |
---|
149 | n/a | */ |
---|
150 | n/a | |
---|
151 | n/a | ENCODER(euc_jis_2004) |
---|
152 | n/a | { |
---|
153 | n/a | while (*inpos < inlen) { |
---|
154 | n/a | Py_UCS4 c = INCHAR1; |
---|
155 | n/a | DBCHAR code; |
---|
156 | n/a | Py_ssize_t insize; |
---|
157 | n/a | |
---|
158 | n/a | if (c < 0x80) { |
---|
159 | n/a | WRITEBYTE1(c); |
---|
160 | n/a | NEXT(1, 1); |
---|
161 | n/a | continue; |
---|
162 | n/a | } |
---|
163 | n/a | |
---|
164 | n/a | insize = 1; |
---|
165 | n/a | |
---|
166 | n/a | if (c <= 0xFFFF) { |
---|
167 | n/a | EMULATE_JISX0213_2000_ENCODE_BMP(code, c) |
---|
168 | n/a | else if (TRYMAP_ENC(jisx0213_bmp, code, c)) { |
---|
169 | n/a | if (code == MULTIC) { |
---|
170 | n/a | if (inlen - *inpos < 2) { |
---|
171 | n/a | if (flags & MBENC_FLUSH) { |
---|
172 | n/a | code = find_pairencmap( |
---|
173 | n/a | (ucs2_t)c, 0, |
---|
174 | n/a | jisx0213_pair_encmap, |
---|
175 | n/a | JISX0213_ENCPAIRS); |
---|
176 | n/a | if (code == DBCINV) |
---|
177 | n/a | return 1; |
---|
178 | n/a | } |
---|
179 | n/a | else |
---|
180 | n/a | return MBERR_TOOFEW; |
---|
181 | n/a | } |
---|
182 | n/a | else { |
---|
183 | n/a | Py_UCS4 c2 = INCHAR2; |
---|
184 | n/a | code = find_pairencmap( |
---|
185 | n/a | (ucs2_t)c, c2, |
---|
186 | n/a | jisx0213_pair_encmap, |
---|
187 | n/a | JISX0213_ENCPAIRS); |
---|
188 | n/a | if (code == DBCINV) { |
---|
189 | n/a | code = find_pairencmap( |
---|
190 | n/a | (ucs2_t)c, 0, |
---|
191 | n/a | jisx0213_pair_encmap, |
---|
192 | n/a | JISX0213_ENCPAIRS); |
---|
193 | n/a | if (code == DBCINV) |
---|
194 | n/a | return 1; |
---|
195 | n/a | } else |
---|
196 | n/a | insize = 2; |
---|
197 | n/a | } |
---|
198 | n/a | } |
---|
199 | n/a | } |
---|
200 | n/a | else if (TRYMAP_ENC(jisxcommon, code, c)) |
---|
201 | n/a | ; |
---|
202 | n/a | else if (c >= 0xff61 && c <= 0xff9f) { |
---|
203 | n/a | /* JIS X 0201 half-width katakana */ |
---|
204 | n/a | WRITEBYTE2(0x8e, c - 0xfec0); |
---|
205 | n/a | NEXT(1, 2); |
---|
206 | n/a | continue; |
---|
207 | n/a | } |
---|
208 | n/a | else if (c == 0xff3c) |
---|
209 | n/a | /* F/W REVERSE SOLIDUS (see NOTES) */ |
---|
210 | n/a | code = 0x2140; |
---|
211 | n/a | else if (c == 0xff5e) |
---|
212 | n/a | /* F/W TILDE (see NOTES) */ |
---|
213 | n/a | code = 0x2232; |
---|
214 | n/a | else |
---|
215 | n/a | return 1; |
---|
216 | n/a | } |
---|
217 | n/a | else if (c >> 16 == EMPBASE >> 16) { |
---|
218 | n/a | EMULATE_JISX0213_2000_ENCODE_EMP(code, c) |
---|
219 | n/a | else if (TRYMAP_ENC(jisx0213_emp, code, c & 0xffff)) |
---|
220 | n/a | ; |
---|
221 | n/a | else |
---|
222 | n/a | return insize; |
---|
223 | n/a | } |
---|
224 | n/a | else |
---|
225 | n/a | return insize; |
---|
226 | n/a | |
---|
227 | n/a | if (code & 0x8000) { |
---|
228 | n/a | /* Codeset 2 */ |
---|
229 | n/a | WRITEBYTE3(0x8f, code >> 8, (code & 0xFF) | 0x80); |
---|
230 | n/a | NEXT(insize, 3); |
---|
231 | n/a | } else { |
---|
232 | n/a | /* Codeset 1 */ |
---|
233 | n/a | WRITEBYTE2((code >> 8) | 0x80, (code & 0xFF) | 0x80); |
---|
234 | n/a | NEXT(insize, 2); |
---|
235 | n/a | } |
---|
236 | n/a | } |
---|
237 | n/a | |
---|
238 | n/a | return 0; |
---|
239 | n/a | } |
---|
240 | n/a | |
---|
241 | n/a | DECODER(euc_jis_2004) |
---|
242 | n/a | { |
---|
243 | n/a | while (inleft > 0) { |
---|
244 | n/a | unsigned char c = INBYTE1; |
---|
245 | n/a | Py_UCS4 code, decoded; |
---|
246 | n/a | |
---|
247 | n/a | if (c < 0x80) { |
---|
248 | n/a | OUTCHAR(c); |
---|
249 | n/a | NEXT_IN(1); |
---|
250 | n/a | continue; |
---|
251 | n/a | } |
---|
252 | n/a | |
---|
253 | n/a | if (c == 0x8e) { |
---|
254 | n/a | /* JIS X 0201 half-width katakana */ |
---|
255 | n/a | unsigned char c2; |
---|
256 | n/a | |
---|
257 | n/a | REQUIRE_INBUF(2); |
---|
258 | n/a | c2 = INBYTE2; |
---|
259 | n/a | if (c2 >= 0xa1 && c2 <= 0xdf) { |
---|
260 | n/a | OUTCHAR(0xfec0 + c2); |
---|
261 | n/a | NEXT_IN(2); |
---|
262 | n/a | } |
---|
263 | n/a | else |
---|
264 | n/a | return 1; |
---|
265 | n/a | } |
---|
266 | n/a | else if (c == 0x8f) { |
---|
267 | n/a | unsigned char c2, c3; |
---|
268 | n/a | |
---|
269 | n/a | REQUIRE_INBUF(3); |
---|
270 | n/a | c2 = INBYTE2 ^ 0x80; |
---|
271 | n/a | c3 = INBYTE3 ^ 0x80; |
---|
272 | n/a | |
---|
273 | n/a | /* JIS X 0213 Plane 2 or JIS X 0212 (see NOTES) */ |
---|
274 | n/a | EMULATE_JISX0213_2000_DECODE_PLANE2(writer, c2, c3) |
---|
275 | n/a | else if (TRYMAP_DEC(jisx0213_2_bmp, decoded, c2, c3)) |
---|
276 | n/a | OUTCHAR(decoded); |
---|
277 | n/a | else if (TRYMAP_DEC(jisx0213_2_emp, code, c2, c3)) { |
---|
278 | n/a | OUTCHAR(EMPBASE | code); |
---|
279 | n/a | NEXT_IN(3); |
---|
280 | n/a | continue; |
---|
281 | n/a | } |
---|
282 | n/a | else if (TRYMAP_DEC(jisx0212, decoded, c2, c3)) |
---|
283 | n/a | OUTCHAR(decoded); |
---|
284 | n/a | else |
---|
285 | n/a | return 1; |
---|
286 | n/a | NEXT_IN(3); |
---|
287 | n/a | } |
---|
288 | n/a | else { |
---|
289 | n/a | unsigned char c2; |
---|
290 | n/a | |
---|
291 | n/a | REQUIRE_INBUF(2); |
---|
292 | n/a | c ^= 0x80; |
---|
293 | n/a | c2 = INBYTE2 ^ 0x80; |
---|
294 | n/a | |
---|
295 | n/a | /* JIS X 0213 Plane 1 */ |
---|
296 | n/a | EMULATE_JISX0213_2000_DECODE_PLANE1(writer, c, c2) |
---|
297 | n/a | else if (c == 0x21 && c2 == 0x40) |
---|
298 | n/a | OUTCHAR(0xff3c); |
---|
299 | n/a | else if (c == 0x22 && c2 == 0x32) |
---|
300 | n/a | OUTCHAR(0xff5e); |
---|
301 | n/a | else if (TRYMAP_DEC(jisx0208, decoded, c, c2)) |
---|
302 | n/a | OUTCHAR(decoded); |
---|
303 | n/a | else if (TRYMAP_DEC(jisx0213_1_bmp, decoded, c, c2)) |
---|
304 | n/a | OUTCHAR(decoded); |
---|
305 | n/a | else if (TRYMAP_DEC(jisx0213_1_emp, code, c, c2)) { |
---|
306 | n/a | OUTCHAR(EMPBASE | code); |
---|
307 | n/a | NEXT_IN(2); |
---|
308 | n/a | continue; |
---|
309 | n/a | } |
---|
310 | n/a | else if (TRYMAP_DEC(jisx0213_pair, code, c, c2)) { |
---|
311 | n/a | OUTCHAR2(code >> 16, code & 0xffff); |
---|
312 | n/a | NEXT_IN(2); |
---|
313 | n/a | continue; |
---|
314 | n/a | } |
---|
315 | n/a | else |
---|
316 | n/a | return 1; |
---|
317 | n/a | NEXT_IN(2); |
---|
318 | n/a | } |
---|
319 | n/a | } |
---|
320 | n/a | |
---|
321 | n/a | return 0; |
---|
322 | n/a | } |
---|
323 | n/a | |
---|
324 | n/a | |
---|
325 | n/a | /* |
---|
326 | n/a | * EUC-JP codec |
---|
327 | n/a | */ |
---|
328 | n/a | |
---|
329 | n/a | ENCODER(euc_jp) |
---|
330 | n/a | { |
---|
331 | n/a | while (*inpos < inlen) { |
---|
332 | n/a | Py_UCS4 c = INCHAR1; |
---|
333 | n/a | DBCHAR code; |
---|
334 | n/a | |
---|
335 | n/a | if (c < 0x80) { |
---|
336 | n/a | WRITEBYTE1((unsigned char)c); |
---|
337 | n/a | NEXT(1, 1); |
---|
338 | n/a | continue; |
---|
339 | n/a | } |
---|
340 | n/a | |
---|
341 | n/a | if (c > 0xFFFF) |
---|
342 | n/a | return 1; |
---|
343 | n/a | |
---|
344 | n/a | if (TRYMAP_ENC(jisxcommon, code, c)) |
---|
345 | n/a | ; |
---|
346 | n/a | else if (c >= 0xff61 && c <= 0xff9f) { |
---|
347 | n/a | /* JIS X 0201 half-width katakana */ |
---|
348 | n/a | WRITEBYTE2(0x8e, c - 0xfec0); |
---|
349 | n/a | NEXT(1, 2); |
---|
350 | n/a | continue; |
---|
351 | n/a | } |
---|
352 | n/a | #ifndef STRICT_BUILD |
---|
353 | n/a | else if (c == 0xff3c) /* FULL-WIDTH REVERSE SOLIDUS */ |
---|
354 | n/a | code = 0x2140; |
---|
355 | n/a | else if (c == 0xa5) { /* YEN SIGN */ |
---|
356 | n/a | WRITEBYTE1(0x5c); |
---|
357 | n/a | NEXT(1, 1); |
---|
358 | n/a | continue; |
---|
359 | n/a | } else if (c == 0x203e) { /* OVERLINE */ |
---|
360 | n/a | WRITEBYTE1(0x7e); |
---|
361 | n/a | NEXT(1, 1); |
---|
362 | n/a | continue; |
---|
363 | n/a | } |
---|
364 | n/a | #endif |
---|
365 | n/a | else |
---|
366 | n/a | return 1; |
---|
367 | n/a | |
---|
368 | n/a | if (code & 0x8000) { |
---|
369 | n/a | /* JIS X 0212 */ |
---|
370 | n/a | WRITEBYTE3(0x8f, code >> 8, (code & 0xFF) | 0x80); |
---|
371 | n/a | NEXT(1, 3); |
---|
372 | n/a | } else { |
---|
373 | n/a | /* JIS X 0208 */ |
---|
374 | n/a | WRITEBYTE2((code >> 8) | 0x80, (code & 0xFF) | 0x80); |
---|
375 | n/a | NEXT(1, 2); |
---|
376 | n/a | } |
---|
377 | n/a | } |
---|
378 | n/a | |
---|
379 | n/a | return 0; |
---|
380 | n/a | } |
---|
381 | n/a | |
---|
382 | n/a | DECODER(euc_jp) |
---|
383 | n/a | { |
---|
384 | n/a | while (inleft > 0) { |
---|
385 | n/a | unsigned char c = INBYTE1; |
---|
386 | n/a | Py_UCS4 decoded; |
---|
387 | n/a | |
---|
388 | n/a | if (c < 0x80) { |
---|
389 | n/a | OUTCHAR(c); |
---|
390 | n/a | NEXT_IN(1); |
---|
391 | n/a | continue; |
---|
392 | n/a | } |
---|
393 | n/a | |
---|
394 | n/a | if (c == 0x8e) { |
---|
395 | n/a | /* JIS X 0201 half-width katakana */ |
---|
396 | n/a | unsigned char c2; |
---|
397 | n/a | |
---|
398 | n/a | REQUIRE_INBUF(2); |
---|
399 | n/a | c2 = INBYTE2; |
---|
400 | n/a | if (c2 >= 0xa1 && c2 <= 0xdf) { |
---|
401 | n/a | OUTCHAR(0xfec0 + c2); |
---|
402 | n/a | NEXT_IN(2); |
---|
403 | n/a | } |
---|
404 | n/a | else |
---|
405 | n/a | return 1; |
---|
406 | n/a | } |
---|
407 | n/a | else if (c == 0x8f) { |
---|
408 | n/a | unsigned char c2, c3; |
---|
409 | n/a | |
---|
410 | n/a | REQUIRE_INBUF(3); |
---|
411 | n/a | c2 = INBYTE2; |
---|
412 | n/a | c3 = INBYTE3; |
---|
413 | n/a | /* JIS X 0212 */ |
---|
414 | n/a | if (TRYMAP_DEC(jisx0212, decoded, c2 ^ 0x80, c3 ^ 0x80)) { |
---|
415 | n/a | OUTCHAR(decoded); |
---|
416 | n/a | NEXT_IN(3); |
---|
417 | n/a | } |
---|
418 | n/a | else |
---|
419 | n/a | return 1; |
---|
420 | n/a | } |
---|
421 | n/a | else { |
---|
422 | n/a | unsigned char c2; |
---|
423 | n/a | |
---|
424 | n/a | REQUIRE_INBUF(2); |
---|
425 | n/a | c2 = INBYTE2; |
---|
426 | n/a | /* JIS X 0208 */ |
---|
427 | n/a | #ifndef STRICT_BUILD |
---|
428 | n/a | if (c == 0xa1 && c2 == 0xc0) |
---|
429 | n/a | /* FULL-WIDTH REVERSE SOLIDUS */ |
---|
430 | n/a | OUTCHAR(0xff3c); |
---|
431 | n/a | else |
---|
432 | n/a | #endif |
---|
433 | n/a | if (TRYMAP_DEC(jisx0208, decoded, c ^ 0x80, c2 ^ 0x80)) |
---|
434 | n/a | OUTCHAR(decoded); |
---|
435 | n/a | else |
---|
436 | n/a | return 1; |
---|
437 | n/a | NEXT_IN(2); |
---|
438 | n/a | } |
---|
439 | n/a | } |
---|
440 | n/a | |
---|
441 | n/a | return 0; |
---|
442 | n/a | } |
---|
443 | n/a | |
---|
444 | n/a | |
---|
445 | n/a | /* |
---|
446 | n/a | * SHIFT_JIS codec |
---|
447 | n/a | */ |
---|
448 | n/a | |
---|
449 | n/a | ENCODER(shift_jis) |
---|
450 | n/a | { |
---|
451 | n/a | while (*inpos < inlen) { |
---|
452 | n/a | Py_UCS4 c = INCHAR1; |
---|
453 | n/a | DBCHAR code; |
---|
454 | n/a | unsigned char c1, c2; |
---|
455 | n/a | |
---|
456 | n/a | #ifdef STRICT_BUILD |
---|
457 | n/a | JISX0201_R_ENCODE(c, code) |
---|
458 | n/a | #else |
---|
459 | n/a | if (c < 0x80) |
---|
460 | n/a | code = c; |
---|
461 | n/a | else if (c == 0x00a5) |
---|
462 | n/a | code = 0x5c; /* YEN SIGN */ |
---|
463 | n/a | else if (c == 0x203e) |
---|
464 | n/a | code = 0x7e; /* OVERLINE */ |
---|
465 | n/a | #endif |
---|
466 | n/a | else JISX0201_K_ENCODE(c, code) |
---|
467 | n/a | else if (c > 0xFFFF) |
---|
468 | n/a | return 1; |
---|
469 | n/a | else |
---|
470 | n/a | code = NOCHAR; |
---|
471 | n/a | |
---|
472 | n/a | if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) { |
---|
473 | n/a | REQUIRE_OUTBUF(1); |
---|
474 | n/a | |
---|
475 | n/a | OUTBYTE1((unsigned char)code); |
---|
476 | n/a | NEXT(1, 1); |
---|
477 | n/a | continue; |
---|
478 | n/a | } |
---|
479 | n/a | |
---|
480 | n/a | REQUIRE_OUTBUF(2); |
---|
481 | n/a | |
---|
482 | n/a | if (code == NOCHAR) { |
---|
483 | n/a | if (TRYMAP_ENC(jisxcommon, code, c)) |
---|
484 | n/a | ; |
---|
485 | n/a | #ifndef STRICT_BUILD |
---|
486 | n/a | else if (c == 0xff3c) |
---|
487 | n/a | code = 0x2140; /* FULL-WIDTH REVERSE SOLIDUS */ |
---|
488 | n/a | #endif |
---|
489 | n/a | else |
---|
490 | n/a | return 1; |
---|
491 | n/a | |
---|
492 | n/a | if (code & 0x8000) /* MSB set: JIS X 0212 */ |
---|
493 | n/a | return 1; |
---|
494 | n/a | } |
---|
495 | n/a | |
---|
496 | n/a | c1 = code >> 8; |
---|
497 | n/a | c2 = code & 0xff; |
---|
498 | n/a | c2 = (((c1 - 0x21) & 1) ? 0x5e : 0) + (c2 - 0x21); |
---|
499 | n/a | c1 = (c1 - 0x21) >> 1; |
---|
500 | n/a | OUTBYTE1(c1 < 0x1f ? c1 + 0x81 : c1 + 0xc1); |
---|
501 | n/a | OUTBYTE2(c2 < 0x3f ? c2 + 0x40 : c2 + 0x41); |
---|
502 | n/a | NEXT(1, 2); |
---|
503 | n/a | } |
---|
504 | n/a | |
---|
505 | n/a | return 0; |
---|
506 | n/a | } |
---|
507 | n/a | |
---|
508 | n/a | DECODER(shift_jis) |
---|
509 | n/a | { |
---|
510 | n/a | while (inleft > 0) { |
---|
511 | n/a | unsigned char c = INBYTE1; |
---|
512 | n/a | Py_UCS4 decoded; |
---|
513 | n/a | |
---|
514 | n/a | #ifdef STRICT_BUILD |
---|
515 | n/a | JISX0201_R_DECODE(c, writer) |
---|
516 | n/a | #else |
---|
517 | n/a | if (c < 0x80) |
---|
518 | n/a | OUTCHAR(c); |
---|
519 | n/a | #endif |
---|
520 | n/a | else JISX0201_K_DECODE(c, writer) |
---|
521 | n/a | else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xea)){ |
---|
522 | n/a | unsigned char c1, c2; |
---|
523 | n/a | |
---|
524 | n/a | REQUIRE_INBUF(2); |
---|
525 | n/a | c2 = INBYTE2; |
---|
526 | n/a | if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc) |
---|
527 | n/a | return 1; |
---|
528 | n/a | |
---|
529 | n/a | c1 = (c < 0xe0 ? c - 0x81 : c - 0xc1); |
---|
530 | n/a | c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41); |
---|
531 | n/a | c1 = (2 * c1 + (c2 < 0x5e ? 0 : 1) + 0x21); |
---|
532 | n/a | c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21; |
---|
533 | n/a | |
---|
534 | n/a | #ifndef STRICT_BUILD |
---|
535 | n/a | if (c1 == 0x21 && c2 == 0x40) { |
---|
536 | n/a | /* FULL-WIDTH REVERSE SOLIDUS */ |
---|
537 | n/a | OUTCHAR(0xff3c); |
---|
538 | n/a | NEXT_IN(2); |
---|
539 | n/a | continue; |
---|
540 | n/a | } |
---|
541 | n/a | #endif |
---|
542 | n/a | if (TRYMAP_DEC(jisx0208, decoded, c1, c2)) { |
---|
543 | n/a | OUTCHAR(decoded); |
---|
544 | n/a | NEXT_IN(2); |
---|
545 | n/a | continue; |
---|
546 | n/a | } |
---|
547 | n/a | else |
---|
548 | n/a | return 1; |
---|
549 | n/a | } |
---|
550 | n/a | else |
---|
551 | n/a | return 1; |
---|
552 | n/a | |
---|
553 | n/a | NEXT_IN(1); /* JIS X 0201 */ |
---|
554 | n/a | } |
---|
555 | n/a | |
---|
556 | n/a | return 0; |
---|
557 | n/a | } |
---|
558 | n/a | |
---|
559 | n/a | |
---|
560 | n/a | /* |
---|
561 | n/a | * SHIFT_JIS-2004 codec |
---|
562 | n/a | */ |
---|
563 | n/a | |
---|
564 | n/a | ENCODER(shift_jis_2004) |
---|
565 | n/a | { |
---|
566 | n/a | while (*inpos < inlen) { |
---|
567 | n/a | Py_UCS4 c = INCHAR1; |
---|
568 | n/a | DBCHAR code = NOCHAR; |
---|
569 | n/a | int c1, c2; |
---|
570 | n/a | Py_ssize_t insize; |
---|
571 | n/a | |
---|
572 | n/a | JISX0201_ENCODE(c, code) |
---|
573 | n/a | |
---|
574 | n/a | if (code < 0x80 || (code >= 0xa1 && code <= 0xdf)) { |
---|
575 | n/a | WRITEBYTE1((unsigned char)code); |
---|
576 | n/a | NEXT(1, 1); |
---|
577 | n/a | continue; |
---|
578 | n/a | } |
---|
579 | n/a | |
---|
580 | n/a | REQUIRE_OUTBUF(2); |
---|
581 | n/a | insize = 1; |
---|
582 | n/a | |
---|
583 | n/a | if (code == NOCHAR) { |
---|
584 | n/a | if (c <= 0xffff) { |
---|
585 | n/a | EMULATE_JISX0213_2000_ENCODE_BMP(code, c) |
---|
586 | n/a | else if (TRYMAP_ENC(jisx0213_bmp, code, c)) { |
---|
587 | n/a | if (code == MULTIC) { |
---|
588 | n/a | if (inlen - *inpos < 2) { |
---|
589 | n/a | if (flags & MBENC_FLUSH) { |
---|
590 | n/a | code = find_pairencmap |
---|
591 | n/a | ((ucs2_t)c, 0, |
---|
592 | n/a | jisx0213_pair_encmap, |
---|
593 | n/a | JISX0213_ENCPAIRS); |
---|
594 | n/a | if (code == DBCINV) |
---|
595 | n/a | return 1; |
---|
596 | n/a | } |
---|
597 | n/a | else |
---|
598 | n/a | return MBERR_TOOFEW; |
---|
599 | n/a | } |
---|
600 | n/a | else { |
---|
601 | n/a | Py_UCS4 ch2 = INCHAR2; |
---|
602 | n/a | code = find_pairencmap( |
---|
603 | n/a | (ucs2_t)c, ch2, |
---|
604 | n/a | jisx0213_pair_encmap, |
---|
605 | n/a | JISX0213_ENCPAIRS); |
---|
606 | n/a | if (code == DBCINV) { |
---|
607 | n/a | code = find_pairencmap( |
---|
608 | n/a | (ucs2_t)c, 0, |
---|
609 | n/a | jisx0213_pair_encmap, |
---|
610 | n/a | JISX0213_ENCPAIRS); |
---|
611 | n/a | if (code == DBCINV) |
---|
612 | n/a | return 1; |
---|
613 | n/a | } |
---|
614 | n/a | else |
---|
615 | n/a | insize = 2; |
---|
616 | n/a | } |
---|
617 | n/a | } |
---|
618 | n/a | } |
---|
619 | n/a | else if (TRYMAP_ENC(jisxcommon, code, c)) { |
---|
620 | n/a | /* abandon JIS X 0212 codes */ |
---|
621 | n/a | if (code & 0x8000) |
---|
622 | n/a | return 1; |
---|
623 | n/a | } |
---|
624 | n/a | else |
---|
625 | n/a | return 1; |
---|
626 | n/a | } |
---|
627 | n/a | else if (c >> 16 == EMPBASE >> 16) { |
---|
628 | n/a | EMULATE_JISX0213_2000_ENCODE_EMP(code, c) |
---|
629 | n/a | else if (TRYMAP_ENC(jisx0213_emp, code, c&0xffff)) |
---|
630 | n/a | ; |
---|
631 | n/a | else |
---|
632 | n/a | return insize; |
---|
633 | n/a | } |
---|
634 | n/a | else |
---|
635 | n/a | return insize; |
---|
636 | n/a | } |
---|
637 | n/a | |
---|
638 | n/a | c1 = code >> 8; |
---|
639 | n/a | c2 = (code & 0xff) - 0x21; |
---|
640 | n/a | |
---|
641 | n/a | if (c1 & 0x80) { |
---|
642 | n/a | /* Plane 2 */ |
---|
643 | n/a | if (c1 >= 0xee) |
---|
644 | n/a | c1 -= 0x87; |
---|
645 | n/a | else if (c1 >= 0xac || c1 == 0xa8) |
---|
646 | n/a | c1 -= 0x49; |
---|
647 | n/a | else |
---|
648 | n/a | c1 -= 0x43; |
---|
649 | n/a | } |
---|
650 | n/a | else { |
---|
651 | n/a | /* Plane 1 */ |
---|
652 | n/a | c1 -= 0x21; |
---|
653 | n/a | } |
---|
654 | n/a | |
---|
655 | n/a | if (c1 & 1) |
---|
656 | n/a | c2 += 0x5e; |
---|
657 | n/a | c1 >>= 1; |
---|
658 | n/a | OUTBYTE1(c1 + (c1 < 0x1f ? 0x81 : 0xc1)); |
---|
659 | n/a | OUTBYTE2(c2 + (c2 < 0x3f ? 0x40 : 0x41)); |
---|
660 | n/a | |
---|
661 | n/a | NEXT(insize, 2); |
---|
662 | n/a | } |
---|
663 | n/a | |
---|
664 | n/a | return 0; |
---|
665 | n/a | } |
---|
666 | n/a | |
---|
667 | n/a | DECODER(shift_jis_2004) |
---|
668 | n/a | { |
---|
669 | n/a | while (inleft > 0) { |
---|
670 | n/a | unsigned char c = INBYTE1; |
---|
671 | n/a | |
---|
672 | n/a | JISX0201_DECODE(c, writer) |
---|
673 | n/a | else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)){ |
---|
674 | n/a | unsigned char c1, c2; |
---|
675 | n/a | Py_UCS4 code, decoded; |
---|
676 | n/a | |
---|
677 | n/a | REQUIRE_INBUF(2); |
---|
678 | n/a | c2 = INBYTE2; |
---|
679 | n/a | if (c2 < 0x40 || (c2 > 0x7e && c2 < 0x80) || c2 > 0xfc) |
---|
680 | n/a | return 1; |
---|
681 | n/a | |
---|
682 | n/a | c1 = (c < 0xe0 ? c - 0x81 : c - 0xc1); |
---|
683 | n/a | c2 = (c2 < 0x80 ? c2 - 0x40 : c2 - 0x41); |
---|
684 | n/a | c1 = (2 * c1 + (c2 < 0x5e ? 0 : 1)); |
---|
685 | n/a | c2 = (c2 < 0x5e ? c2 : c2 - 0x5e) + 0x21; |
---|
686 | n/a | |
---|
687 | n/a | if (c1 < 0x5e) { /* Plane 1 */ |
---|
688 | n/a | c1 += 0x21; |
---|
689 | n/a | EMULATE_JISX0213_2000_DECODE_PLANE1(writer, |
---|
690 | n/a | c1, c2) |
---|
691 | n/a | else if (TRYMAP_DEC(jisx0208, decoded, c1, c2)) |
---|
692 | n/a | OUTCHAR(decoded); |
---|
693 | n/a | else if (TRYMAP_DEC(jisx0213_1_bmp, decoded, c1, c2)) |
---|
694 | n/a | OUTCHAR(decoded); |
---|
695 | n/a | else if (TRYMAP_DEC(jisx0213_1_emp, code, c1, c2)) |
---|
696 | n/a | OUTCHAR(EMPBASE | code); |
---|
697 | n/a | else if (TRYMAP_DEC(jisx0213_pair, code, c1, c2)) |
---|
698 | n/a | OUTCHAR2(code >> 16, code & 0xffff); |
---|
699 | n/a | else |
---|
700 | n/a | return 1; |
---|
701 | n/a | NEXT_IN(2); |
---|
702 | n/a | } |
---|
703 | n/a | else { /* Plane 2 */ |
---|
704 | n/a | if (c1 >= 0x67) |
---|
705 | n/a | c1 += 0x07; |
---|
706 | n/a | else if (c1 >= 0x63 || c1 == 0x5f) |
---|
707 | n/a | c1 -= 0x37; |
---|
708 | n/a | else |
---|
709 | n/a | c1 -= 0x3d; |
---|
710 | n/a | |
---|
711 | n/a | EMULATE_JISX0213_2000_DECODE_PLANE2(writer, |
---|
712 | n/a | c1, c2) |
---|
713 | n/a | else if (TRYMAP_DEC(jisx0213_2_bmp, decoded, c1, c2)) |
---|
714 | n/a | OUTCHAR(decoded); |
---|
715 | n/a | else if (TRYMAP_DEC(jisx0213_2_emp, code, c1, c2)) { |
---|
716 | n/a | OUTCHAR(EMPBASE | code); |
---|
717 | n/a | NEXT_IN(2); |
---|
718 | n/a | continue; |
---|
719 | n/a | } |
---|
720 | n/a | else |
---|
721 | n/a | return 1; |
---|
722 | n/a | NEXT_IN(2); |
---|
723 | n/a | } |
---|
724 | n/a | continue; |
---|
725 | n/a | } |
---|
726 | n/a | else |
---|
727 | n/a | return 1; |
---|
728 | n/a | |
---|
729 | n/a | NEXT_IN(1); /* JIS X 0201 */ |
---|
730 | n/a | } |
---|
731 | n/a | |
---|
732 | n/a | return 0; |
---|
733 | n/a | } |
---|
734 | n/a | |
---|
735 | n/a | |
---|
736 | n/a | BEGIN_MAPPINGS_LIST |
---|
737 | n/a | MAPPING_DECONLY(jisx0208) |
---|
738 | n/a | MAPPING_DECONLY(jisx0212) |
---|
739 | n/a | MAPPING_ENCONLY(jisxcommon) |
---|
740 | n/a | MAPPING_DECONLY(jisx0213_1_bmp) |
---|
741 | n/a | MAPPING_DECONLY(jisx0213_2_bmp) |
---|
742 | n/a | MAPPING_ENCONLY(jisx0213_bmp) |
---|
743 | n/a | MAPPING_DECONLY(jisx0213_1_emp) |
---|
744 | n/a | MAPPING_DECONLY(jisx0213_2_emp) |
---|
745 | n/a | MAPPING_ENCONLY(jisx0213_emp) |
---|
746 | n/a | MAPPING_ENCDEC(jisx0213_pair) |
---|
747 | n/a | MAPPING_ENCDEC(cp932ext) |
---|
748 | n/a | END_MAPPINGS_LIST |
---|
749 | n/a | |
---|
750 | n/a | BEGIN_CODECS_LIST |
---|
751 | n/a | CODEC_STATELESS(shift_jis) |
---|
752 | n/a | CODEC_STATELESS(cp932) |
---|
753 | n/a | CODEC_STATELESS(euc_jp) |
---|
754 | n/a | CODEC_STATELESS(shift_jis_2004) |
---|
755 | n/a | CODEC_STATELESS(euc_jis_2004) |
---|
756 | n/a | { "euc_jisx0213", (void *)2000, NULL, _STATELESS_METHODS(euc_jis_2004) }, |
---|
757 | n/a | { "shift_jisx0213", (void *)2000, NULL, _STATELESS_METHODS(shift_jis_2004) }, |
---|
758 | n/a | END_CODECS_LIST |
---|
759 | n/a | |
---|
760 | n/a | I_AM_A_MODULE_FOR(jp) |
---|