citrus_mskanji.c revision 1.14.22.5 1 /* $NetBSD: citrus_mskanji.c,v 1.14.22.5 2018/01/29 22:08:59 perseant Exp $ */
2
3 /*-
4 * Copyright (c)2002 Citrus Project,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * ja_JP.SJIS locale table for BSD4.4/rune
31 * version 1.0
32 * (C) Sin'ichiro MIYATANI / Phase One, Inc
33 * May 12, 1995
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by Phase One, Inc.
46 * 4. The name of Phase One, Inc. may be used to endorse or promote products
47 * derived from this software without specific prior written permission.
48 *
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 * SUCH DAMAGE.
60 */
61
62
63 #include <sys/cdefs.h>
64 #if defined(LIBC_SCCS) && !defined(lint)
65 __RCSID("$NetBSD: citrus_mskanji.c,v 1.14.22.5 2018/01/29 22:08:59 perseant Exp $");
66 #endif /* LIBC_SCCS and not lint */
67
68 #include <assert.h>
69 #include <errno.h>
70 #include <string.h>
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <stddef.h>
74 #include <wchar.h>
75 #include <sys/types.h>
76 #include <limits.h>
77
78 #include "citrus_namespace.h"
79 #include "citrus_types.h"
80 #include "citrus_bcs.h"
81 #include "citrus_module.h"
82 #include "citrus_ctype.h"
83 #include "citrus_stdenc.h"
84 #include "citrus_mskanji.h"
85
86
87 /* ----------------------------------------------------------------------
88 * private stuffs used by templates
89 */
90
91 typedef struct _MSKanjiState {
92 char ch[2];
93 int chlen;
94 } _MSKanjiState;
95
96 typedef struct {
97 int mode;
98 #define MODE_JIS2004 1
99 } _MSKanjiEncodingInfo;
100
101 typedef struct {
102 _MSKanjiEncodingInfo ei;
103 struct {
104 /* for future multi-locale facility */
105 _MSKanjiState s_mblen;
106 _MSKanjiState s_mbrlen;
107 _MSKanjiState s_mbrtowc;
108 _MSKanjiState s_mbtowc;
109 _MSKanjiState s_mbsrtowcs;
110 _MSKanjiState s_mbsnrtowcs;
111 _MSKanjiState s_wcrtomb;
112 _MSKanjiState s_wcsrtombs;
113 _MSKanjiState s_wcsnrtombs;
114 _MSKanjiState s_wctomb;
115 } states;
116 } _MSKanjiCTypeInfo;
117
118 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
119 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
120
121 #define _FUNCNAME(m) _citrus_MSKanji_##m
122 #define _ENCODING_INFO _MSKanjiEncodingInfo
123 #define _CTYPE_INFO _MSKanjiCTypeInfo
124 #define _ENCODING_STATE _MSKanjiState
125 #define _ENCODING_MB_CUR_MAX(_ei_) 2
126 #define _ENCODING_IS_STATE_DEPENDENT 0
127 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
128
129 #ifdef __STDC_ISO_10646__
130 #include "citrus_mskanji_k2u.h"
131 #include "citrus_mskanji_u2k.h"
132
133 static __inline int
134 /*ARGSUSED*/
135 _FUNCNAME(ucs2kt)(_ENCODING_INFO * __restrict ei,
136 wchar_kuten_t * __restrict ktp, wchar_ucs4_t wc)
137 {
138 struct unicode2kuten_lookup *uk;
139
140 _DIAGASSERT(ktp != NULL);
141
142 /* US-ASCII are not in the list */
143 if (wc < 0x80) {
144 *ktp = wc;
145 return 0;
146 }
147
148 *ktp = citrus_trie_lookup(&__mskanji_u2k_header, wc);
149 return 0;
150 }
151
152 static __inline int
153 /*ARGSUSED*/
154 _FUNCNAME(kt2ucs)(_ENCODING_INFO * __restrict ei,
155 wchar_ucs4_t * __restrict up, wchar_kuten_t kt)
156 {
157 _csid_t csid;
158 _index_t idx;
159 struct unicode2kuten_lookup *uk, *table;
160
161 _DIAGASSERT(up != NULL);
162
163 table = NULL;
164
165 /* Special cases */
166 if (kt == 0x5c) { /* backslash -> Yen sign */
167 *up = 0x00A5;
168 return 0;
169 }
170 if (kt == 0x7E) { /* tilde -> overline */
171 *up = 0x203E;
172 return 0;
173 }
174 if (kt < 0x80) {
175 *up = kt;
176 return 0;
177 }
178
179 *up = citrus_trie_lookup(&__mskanji_k2u_header, kt);
180 return 0;
181 }
182 #else
183 #include "citrus_u2k_template.h"
184 #endif
185
186 static int
187 _mskanji1(int c)
188 {
189
190 if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc))
191 return 1;
192 else
193 return 0;
194 }
195
196 static int
197 _mskanji2(int c)
198 {
199
200 if ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc))
201 return 1;
202 else
203 return 0;
204 }
205
206 static __inline void
207 /*ARGSUSED*/
208 _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * __restrict ei,
209 _MSKanjiState * __restrict s)
210 {
211 s->chlen = 0;
212 }
213
214 static __inline void
215 /*ARGSUSED*/
216 _citrus_MSKanji_pack_state(_MSKanjiEncodingInfo * __restrict ei,
217 void * __restrict pspriv,
218 const _MSKanjiState * __restrict s)
219 {
220 memcpy(pspriv, (const void *)s, sizeof(*s));
221 }
222
223 static __inline void
224 /*ARGSUSED*/
225 _citrus_MSKanji_unpack_state(_MSKanjiEncodingInfo * __restrict ei,
226 _MSKanjiState * __restrict s,
227 const void * __restrict pspriv)
228 {
229 memcpy((void *)s, pspriv, sizeof(*s));
230 }
231
232 static int
233 /*ARGSUSED*/
234 _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * __restrict ei,
235 wchar_ucs4_t * __restrict pwc,
236 const char ** __restrict s, size_t n,
237 _MSKanjiState * __restrict psenc,
238 size_t * __restrict nresult)
239 {
240 wchar_kuten_t wchar;
241 int len;
242 int chlenbak;
243 const char *s0;
244
245 _DIAGASSERT(nresult != 0);
246 _DIAGASSERT(ei != NULL);
247 _DIAGASSERT(s != NULL);
248 _DIAGASSERT(psenc != NULL);
249
250 s0 = *s;
251
252 if (s0 == NULL) {
253 _citrus_MSKanji_init_state(ei, psenc);
254 *nresult = 0; /* state independent */
255 return (0);
256 }
257
258 chlenbak = psenc->chlen;
259
260 /* make sure we have the first byte in the buffer */
261 switch (psenc->chlen) {
262 case 0:
263 if (n < 1)
264 goto restart;
265 psenc->ch[0] = *s0++;
266 psenc->chlen = 1;
267 n--;
268 break;
269 case 1:
270 break;
271 default:
272 /* illegal state */
273 goto encoding_error;
274 }
275
276 len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1;
277 while (psenc->chlen < len) {
278 if (n < 1)
279 goto restart;
280 psenc->ch[psenc->chlen] = *s0++;
281 psenc->chlen++;
282 n--;
283 }
284
285 *s = s0;
286
287 switch (len) {
288 case 1:
289 wchar = psenc->ch[0] & 0xff;
290 break;
291 case 2:
292 if (!_mskanji2(psenc->ch[1] & 0xff))
293 goto encoding_error;
294 wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff);
295 break;
296 default:
297 /* illegal state */
298 goto encoding_error;
299 }
300
301 psenc->chlen = 0;
302
303 if (pwc)
304 _citrus_MSKanji_kt2ucs(ei, pwc, wchar);
305
306 if (!wchar)
307 *nresult = 0;
308 else
309 *nresult = len - chlenbak;
310
311 return (0);
312
313 encoding_error:
314 psenc->chlen = 0;
315 *nresult = (size_t)-1;
316 return (EILSEQ);
317
318 restart:
319 *nresult = (size_t)-2;
320 *s = s0;
321 return (0);
322 }
323
324
325 static int
326 _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * __restrict ei,
327 char * __restrict s, size_t n, wchar_ucs4_t wc,
328 _MSKanjiState * __restrict psenc,
329 size_t * __restrict nresult)
330 {
331 int ret;
332
333 _DIAGASSERT(ei != NULL);
334 _DIAGASSERT(psenc != NULL);
335 _DIAGASSERT(s != NULL);
336
337 _citrus_MSKanji_ucs2kt(ei, &wc, wc);
338
339 /* check invalid sequence */
340 if (wc & ~0xffff) {
341 ret = EILSEQ;
342 goto err;
343 }
344
345 if (wc & 0xff00) {
346 if (n < 2) {
347 ret = E2BIG;
348 goto err;
349 }
350
351 s[0] = (wc >> 8) & 0xff;
352 s[1] = wc & 0xff;
353 if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) {
354 ret = EILSEQ;
355 goto err;
356 }
357
358 *nresult = 2;
359 return 0;
360 } else {
361 if (n < 1) {
362 ret = E2BIG;
363 goto err;
364 }
365
366 s[0] = wc & 0xff;
367 if (_mskanji1(s[0] & 0xff)) {
368 ret = EILSEQ;
369 goto err;
370 }
371
372 *nresult = 1;
373 return 0;
374 }
375
376 err:
377 *nresult = (size_t)-1;
378 return ret;
379 }
380
381
382 static int
383 /*ARGSUSED*/
384 _citrus_MSKanji_stdenc_wctocs(struct _citrus_stdenc *ce,
385 _csid_t * __restrict csid,
386 _index_t * __restrict idx, wchar_kuten_t wc)
387 {
388 _MSKanjiEncodingInfo *ei;
389 _index_t row, col;
390 int offset;
391
392 _DIAGASSERT(ce != NULL && csid != NULL && idx != NULL);
393
394 ei = (_ENCODING_INFO *)(ce->ce_closure);
395 _DIAGASSERT(ei != NULL);
396
397 if ((_wc_t)wc < 0x80) {
398 /* ISO-646 */
399 *csid = 0;
400 *idx = (_index_t)wc;
401 } else if ((_wc_t)wc < 0x100) {
402 /* KANA */
403 *csid = 1;
404 *idx = (_index_t)wc & 0x7F;
405 } else {
406 /* Kanji (containing Gaiji zone) */
407 /*
408 * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)):
409 * 0x8140 - 0x817E -> 0x2121 - 0x215F
410 * 0x8180 - 0x819E -> 0x2160 - 0x217E
411 * 0x819F - 0x81FC -> 0x2221 - 0x227E
412 *
413 * 0x8240 - 0x827E -> 0x2321 - 0x235F
414 * ...
415 * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E
416 *
417 * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F
418 * ...
419 * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E
420 *
421 * extended Gaiji zone:
422 * 0xF040 - 0xFCFC
423 *
424 * JIS X0213-plane2:
425 * 0xF040 - 0xF09E -> 0x2121 - 0x217E
426 * 0xF140 - 0xF19E -> 0x2321 - 0x237E
427 * ...
428 * 0xF240 - 0xF29E -> 0x2521 - 0x257E
429 *
430 * 0xF09F - 0xF0FC -> 0x2821 - 0x287E
431 * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E
432 * ...
433 * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E
434 *
435 * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E
436 * ...
437 * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E
438 */
439 row = ((_wc_t)wc >> 8) & 0xFF;
440 col = (_wc_t)wc & 0xFF;
441 if (!_mskanji1(row) || !_mskanji2(col))
442 return EILSEQ;
443 if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) {
444 *csid = 2;
445 offset = 0x81;
446 } else {
447 *csid = 3;
448 if ((_wc_t)wc <= 0xF49E) {
449 offset = (_wc_t)wc >= 0xF29F ||
450 ((_wc_t)wc >= 0xF09F && (_wc_t)wc <= 0xF0FC)
451 ? 0xED : 0xF0;
452 } else
453 offset = 0xCE;
454 }
455 row -= offset;
456 if (row >= 0x5F)
457 row -= 0x40;
458 row = row * 2 + 0x21;
459 col -= 0x1F;
460 if (col >= 0x61)
461 col -= 1;
462 if (col > 0x7E) {
463 row += 1;
464 col -= 0x5E;
465 }
466 *idx = ((_index_t)row << 8) | col;
467 }
468
469 return 0;
470 }
471
472 static int
473 /*ARGSUSED*/
474 _citrus_MSKanji_stdenc_cstowc(struct _citrus_stdenc *ce,
475 wchar_kuten_t * __restrict wc,
476 _csid_t csid, _index_t idx)
477 {
478 u_int32_t row, col;
479 int offset;
480 _MSKanjiEncodingInfo *ei;
481
482 _DIAGASSERT(wc != NULL && ce != NULL);
483
484 ei = (_ENCODING_INFO *)(ce->ce_closure);
485 _DIAGASSERT(ei != NULL);
486
487 switch (csid) {
488 case 0:
489 /* ISO-646 */
490 if (idx >= 0x80)
491 return EILSEQ;
492 *wc = (wchar_kuten_t)idx;
493 break;
494 case 1:
495 /* kana */
496 if (idx >= 0x80)
497 return EILSEQ;
498 *wc = (wchar_kuten_t)idx + 0x80;
499 break;
500 case 3:
501 if ((ei->mode & MODE_JIS2004) == 0)
502 return EILSEQ;
503 /*FALLTHROUGH*/
504 case 2:
505 /* kanji */
506 row = (idx >> 8);
507 if (row < 0x21)
508 return EILSEQ;
509 if (csid == 3) {
510 if (row <= 0x2F)
511 offset = (row == 0x22 || row >= 0x26)
512 ? 0xED : 0xF0;
513 else if (row >= 0x4D && row <= 0x7E)
514 offset = 0xCE;
515 else
516 return EILSEQ;
517 } else {
518 if (row > 0x97)
519 return EILSEQ;
520 offset = (row < 0x5F) ? 0x81 : 0xC1;
521 }
522 col = idx & 0xFF;
523 if (col < 0x21 || col > 0x7E)
524 return EILSEQ;
525 row -= 0x21; col -= 0x21;
526 if ((row & 1) == 0) {
527 col += 0x40;
528 if (col >= 0x7F)
529 col += 1;
530 } else
531 col += 0x9F;
532 row = row / 2 + offset;
533 *wc = ((wchar_kuten_t)row << 8) | col;
534 break;
535 default:
536 return EILSEQ;
537 }
538
539 return 0;
540 }
541
542 static __inline int
543 /*ARGSUSED*/
544 _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * __restrict ei,
545 _MSKanjiState * __restrict psenc,
546 int * __restrict rstate)
547 {
548
549 if (psenc->chlen == 0)
550 *rstate = _STDENC_SDGEN_INITIAL;
551 else
552 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
553
554 return 0;
555 }
556
557 static int
558 /*ARGSUSED*/
559 _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * __restrict ei,
560 const void * __restrict var,
561 size_t lenvar)
562 {
563 const char *p;
564
565 _DIAGASSERT(ei != NULL);
566
567 #ifdef __STDC_ISO_10646__
568 citrus_trie_init(&__mskanji_u2k_header, __mskanji_u2k_sizes, __mskanji_u2k_data);
569 citrus_trie_init(&__mskanji_k2u_header, __mskanji_k2u_sizes, __mskanji_k2u_data);
570 #endif
571
572 p = var;
573 #define MATCH(x, act) \
574 do { \
575 if (lenvar >= (sizeof(#x)-1) && \
576 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
577 act; \
578 lenvar -= sizeof(#x)-1; \
579 p += sizeof(#x)-1; \
580 } \
581 } while (/*CONSTCOND*/0)
582 memset((void *)ei, 0, sizeof(*ei));
583 while (lenvar > 0) {
584 switch (_bcs_toupper(*p)) {
585 case 'J':
586 MATCH(JIS2004, ei->mode |= MODE_JIS2004);
587 break;
588 }
589 ++p;
590 --lenvar;
591 }
592
593 return 0;
594 }
595
596 static void
597 _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei)
598 {
599 }
600
601 /* ----------------------------------------------------------------------
602 * public interface for ctype
603 */
604
605 _CITRUS_CTYPE_DECLS(MSKanji);
606 _CITRUS_CTYPE_DEF_OPS(MSKanji);
607
608 #include "citrus_ctype_template.h"
609
610 /* ----------------------------------------------------------------------
611 * public interface for stdenc
612 */
613
614 _CITRUS_STDENC_DECLS(MSKanji);
615 _CITRUS_STDENC_DEF_OPS(MSKanji);
616
617 #include "citrus_stdenc_template.h"
618