/src/dcmtk/oficonv/libsrc/citrus_mskanji.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c)2002 Citrus Project, |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
15 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
16 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
17 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
18 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
19 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
20 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
21 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
22 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
23 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
24 | | * SUCH DAMAGE. |
25 | | */ |
26 | | |
27 | | /* |
28 | | * ja_JP.SJIS locale table for BSD4.4/rune |
29 | | * version 1.0 |
30 | | * (C) Sin'ichiro MIYATANI / Phase One, Inc |
31 | | * May 12, 1995 |
32 | | * |
33 | | * Redistribution and use in source and binary forms, with or without |
34 | | * modification, are permitted provided that the following conditions |
35 | | * are met: |
36 | | * 1. Redistributions of source code must retain the above copyright |
37 | | * notice, this list of conditions and the following disclaimer. |
38 | | * 2. Redistributions in binary form must reproduce the above copyright |
39 | | * notice, this list of conditions and the following disclaimer in the |
40 | | * documentation and/or other materials provided with the distribution. |
41 | | * 3. All advertising materials mentioning features or use of this software |
42 | | * must display the following acknowledgement: |
43 | | * This product includes software developed by Phase One, Inc. |
44 | | * 4. The name of Phase One, Inc. may be used to endorse or promote products |
45 | | * derived from this software without specific prior written permission. |
46 | | * |
47 | | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
48 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
49 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
50 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
51 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
52 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
53 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
54 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
55 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
56 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
57 | | * SUCH DAMAGE. |
58 | | */ |
59 | | |
60 | | #include "dcmtk/config/osconfig.h" |
61 | | #include "citrus_mskanji.h" |
62 | | |
63 | | #include <sys/types.h> |
64 | | #include <errno.h> |
65 | | #include <limits.h> |
66 | | #include <stdbool.h> |
67 | | #include <stddef.h> |
68 | | #include <stdio.h> |
69 | | #include <stdlib.h> |
70 | | #include <string.h> |
71 | | #include <wchar.h> |
72 | | |
73 | | #include "citrus_bcs.h" |
74 | | #include "citrus_types.h" |
75 | | #include "citrus_module.h" |
76 | | #include "citrus_stdenc.h" |
77 | | |
78 | | |
79 | | /* ---------------------------------------------------------------------- |
80 | | * private stuffs used by templates |
81 | | */ |
82 | | |
83 | | typedef struct _MSKanjiState { |
84 | | int chlen; |
85 | | char ch[2]; |
86 | | } _MSKanjiState; |
87 | | |
88 | | typedef struct { |
89 | | int mode; |
90 | 0 | #define MODE_JIS2004 1 |
91 | | } _MSKanjiEncodingInfo; |
92 | | |
93 | | #define _CEI_TO_EI(_cei_) (&(_cei_)->ei) |
94 | | #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_ |
95 | | |
96 | 0 | #define _FUNCNAME(m) _citrus_MSKanji_##m |
97 | 0 | #define _ENCODING_INFO _MSKanjiEncodingInfo |
98 | | #define _ENCODING_STATE _MSKanjiState |
99 | 0 | #define _ENCODING_MB_CUR_MAX(_ei_) 2 |
100 | | #define _ENCODING_IS_STATE_DEPENDENT 0 |
101 | | #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0 |
102 | | |
103 | | |
104 | | static bool |
105 | | _mskanji1(int c) |
106 | 0 | { |
107 | |
|
108 | 0 | return ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)); |
109 | 0 | } |
110 | | |
111 | | static bool |
112 | | _mskanji2(int c) |
113 | 0 | { |
114 | |
|
115 | 0 | return ((c >= 0x40 && c <= 0x7e) || (c >= 0x80 && c <= 0xfc)); |
116 | 0 | } |
117 | | |
118 | | static __inline void |
119 | | /*ARGSUSED*/ |
120 | | _citrus_MSKanji_init_state(_MSKanjiEncodingInfo * ei , |
121 | | _MSKanjiState * s) |
122 | 0 | { |
123 | 0 | (void) ei; |
124 | |
|
125 | 0 | s->chlen = 0; |
126 | 0 | } |
127 | | |
128 | | static int |
129 | | /*ARGSUSED*/ |
130 | | _citrus_MSKanji_mbrtowc_priv(_MSKanjiEncodingInfo * ei, |
131 | | _citrus_wc_t * pwc, char ** s, size_t n, |
132 | | _MSKanjiState * psenc, size_t * nresult) |
133 | 0 | { |
134 | 0 | char *s0; |
135 | 0 | _citrus_wc_t wchar; |
136 | 0 | int chlenbak, len; |
137 | |
|
138 | 0 | s0 = *s; |
139 | |
|
140 | 0 | if (s0 == NULL) { |
141 | 0 | _citrus_MSKanji_init_state(ei, psenc); |
142 | 0 | *nresult = 0; /* state independent */ |
143 | 0 | return (0); |
144 | 0 | } |
145 | | |
146 | 0 | chlenbak = psenc->chlen; |
147 | | |
148 | | /* make sure we have the first byte in the buffer */ |
149 | 0 | switch (psenc->chlen) { |
150 | 0 | case 0: |
151 | 0 | if (n < 1) |
152 | 0 | goto restart; |
153 | 0 | psenc->ch[0] = *s0++; |
154 | 0 | psenc->chlen = 1; |
155 | 0 | n--; |
156 | 0 | break; |
157 | 0 | case 1: |
158 | 0 | break; |
159 | 0 | default: |
160 | | /* illegal state */ |
161 | 0 | goto encoding_error; |
162 | 0 | } |
163 | | |
164 | 0 | len = _mskanji1(psenc->ch[0] & 0xff) ? 2 : 1; |
165 | 0 | while (psenc->chlen < len) { |
166 | 0 | if (n < 1) |
167 | 0 | goto restart; |
168 | 0 | psenc->ch[psenc->chlen] = *s0++; |
169 | 0 | psenc->chlen++; |
170 | 0 | n--; |
171 | 0 | } |
172 | | |
173 | 0 | *s = s0; |
174 | |
|
175 | 0 | switch (len) { |
176 | 0 | case 1: |
177 | 0 | wchar = psenc->ch[0] & 0xff; |
178 | 0 | break; |
179 | 0 | case 2: |
180 | 0 | if (!_mskanji2(psenc->ch[1] & 0xff)) |
181 | 0 | goto encoding_error; |
182 | 0 | wchar = ((psenc->ch[0] & 0xff) << 8) | (psenc->ch[1] & 0xff); |
183 | 0 | break; |
184 | 0 | default: |
185 | | /* illegal state */ |
186 | 0 | goto encoding_error; |
187 | 0 | } |
188 | | |
189 | 0 | psenc->chlen = 0; |
190 | |
|
191 | 0 | if (pwc) |
192 | 0 | *pwc = wchar; |
193 | 0 | *nresult = wchar ? len - chlenbak : 0; |
194 | 0 | return (0); |
195 | | |
196 | 0 | encoding_error: |
197 | 0 | psenc->chlen = 0; |
198 | 0 | *nresult = (size_t)-1; |
199 | 0 | return (EILSEQ); |
200 | | |
201 | 0 | restart: |
202 | 0 | *nresult = (size_t)-2; |
203 | 0 | *s = s0; |
204 | 0 | return (0); |
205 | 0 | } |
206 | | |
207 | | |
208 | | static int |
209 | | _citrus_MSKanji_wcrtomb_priv(_MSKanjiEncodingInfo * ei , |
210 | | char * s, size_t n, _citrus_wc_t wc, |
211 | | _MSKanjiState * psenc , size_t * nresult) |
212 | 0 | { |
213 | 0 | int ret; |
214 | 0 | (void) ei; |
215 | 0 | (void) psenc; |
216 | | |
217 | | /* check invalid sequence */ |
218 | 0 | if (wc & ~0xffff) { |
219 | 0 | ret = EILSEQ; |
220 | 0 | goto err; |
221 | 0 | } |
222 | | |
223 | 0 | if (wc & 0xff00) { |
224 | 0 | if (n < 2) { |
225 | 0 | ret = E2BIG; |
226 | 0 | goto err; |
227 | 0 | } |
228 | | |
229 | 0 | s[0] = (wc >> 8) & 0xff; |
230 | 0 | s[1] = wc & 0xff; |
231 | 0 | if (!_mskanji1(s[0] & 0xff) || !_mskanji2(s[1] & 0xff)) { |
232 | 0 | ret = EILSEQ; |
233 | 0 | goto err; |
234 | 0 | } |
235 | | |
236 | 0 | *nresult = 2; |
237 | 0 | return (0); |
238 | 0 | } else { |
239 | 0 | if (n < 1) { |
240 | 0 | ret = E2BIG; |
241 | 0 | goto err; |
242 | 0 | } |
243 | | |
244 | 0 | s[0] = wc & 0xff; |
245 | 0 | if (_mskanji1(s[0] & 0xff)) { |
246 | 0 | ret = EILSEQ; |
247 | 0 | goto err; |
248 | 0 | } |
249 | | |
250 | 0 | *nresult = 1; |
251 | 0 | return (0); |
252 | 0 | } |
253 | | |
254 | 0 | err: |
255 | 0 | *nresult = (size_t)-1; |
256 | 0 | return (ret); |
257 | 0 | } |
258 | | |
259 | | |
260 | | static __inline int |
261 | | /*ARGSUSED*/ |
262 | | _citrus_MSKanji_stdenc_wctocs(_MSKanjiEncodingInfo * ei, |
263 | | _citrus_csid_t * csid, _citrus_index_t * idx, _citrus_wc_t wc) |
264 | 0 | { |
265 | 0 | _citrus_index_t col, row; |
266 | 0 | int offset; |
267 | |
|
268 | 0 | if ((_citrus_wc_t)wc < 0x80) { |
269 | | /* ISO-646 */ |
270 | 0 | *csid = 0; |
271 | 0 | *idx = (_citrus_index_t)wc; |
272 | 0 | } else if ((_citrus_wc_t)wc < 0x100) { |
273 | | /* KANA */ |
274 | 0 | *csid = 1; |
275 | 0 | *idx = (_citrus_index_t)wc & 0x7F; |
276 | 0 | } else { |
277 | | /* Kanji (containing Gaiji zone) */ |
278 | | /* |
279 | | * 94^2 zone (contains a part of Gaiji (0xED40 - 0xEEFC)): |
280 | | * 0x8140 - 0x817E -> 0x2121 - 0x215F |
281 | | * 0x8180 - 0x819E -> 0x2160 - 0x217E |
282 | | * 0x819F - 0x81FC -> 0x2221 - 0x227E |
283 | | * |
284 | | * 0x8240 - 0x827E -> 0x2321 - 0x235F |
285 | | * ... |
286 | | * 0x9F9F - 0x9FFc -> 0x5E21 - 0x5E7E |
287 | | * |
288 | | * 0xE040 - 0xE07E -> 0x5F21 - 0x5F5F |
289 | | * ... |
290 | | * 0xEF9F - 0xEFFC -> 0x7E21 - 0x7E7E |
291 | | * |
292 | | * extended Gaiji zone: |
293 | | * 0xF040 - 0xFCFC |
294 | | * |
295 | | * JIS X0213-plane2: |
296 | | * 0xF040 - 0xF09E -> 0x2121 - 0x217E |
297 | | * 0xF140 - 0xF19E -> 0x2321 - 0x237E |
298 | | * ... |
299 | | * 0xF240 - 0xF29E -> 0x2521 - 0x257E |
300 | | * |
301 | | * 0xF09F - 0xF0FC -> 0x2821 - 0x287E |
302 | | * 0xF29F - 0xF2FC -> 0x2C21 - 0x2C7E |
303 | | * ... |
304 | | * 0xF44F - 0xF49E -> 0x2F21 - 0x2F7E |
305 | | * |
306 | | * 0xF49F - 0xF4FC -> 0x6E21 - 0x6E7E |
307 | | * ... |
308 | | * 0xFC9F - 0xFCFC -> 0x7E21 - 0x7E7E |
309 | | */ |
310 | 0 | row = ((_citrus_wc_t)wc >> 8) & 0xFF; |
311 | 0 | col = (_citrus_wc_t)wc & 0xFF; |
312 | 0 | if (!_mskanji1(row) || !_mskanji2(col)) |
313 | 0 | return (EILSEQ); |
314 | 0 | if ((ei->mode & MODE_JIS2004) == 0 || row < 0xF0) { |
315 | 0 | *csid = 2; |
316 | 0 | offset = 0x81; |
317 | 0 | } else { |
318 | 0 | *csid = 3; |
319 | 0 | if ((_citrus_wc_t)wc <= 0xF49E) { |
320 | 0 | offset = (_citrus_wc_t)wc >= 0xF29F || |
321 | 0 | ((_citrus_wc_t)wc >= 0xF09F && |
322 | 0 | (_citrus_wc_t)wc <= 0xF0FC) ? 0xED : 0xF0; |
323 | 0 | } else |
324 | 0 | offset = 0xCE; |
325 | 0 | } |
326 | 0 | row -= offset; |
327 | 0 | if (row >= 0x5F) |
328 | 0 | row -= 0x40; |
329 | 0 | row = row * 2 + 0x21; |
330 | 0 | col -= 0x1F; |
331 | 0 | if (col >= 0x61) |
332 | 0 | col -= 1; |
333 | 0 | if (col > 0x7E) { |
334 | 0 | row += 1; |
335 | 0 | col -= 0x5E; |
336 | 0 | } |
337 | 0 | *idx = ((_citrus_index_t)row << 8) | col; |
338 | 0 | } |
339 | | |
340 | 0 | return (0); |
341 | 0 | } |
342 | | |
343 | | static __inline int |
344 | | /*ARGSUSED*/ |
345 | | _citrus_MSKanji_stdenc_cstowc(_MSKanjiEncodingInfo * ei, |
346 | | _citrus_wc_t * wc, _citrus_csid_t csid, _citrus_index_t idx) |
347 | 0 | { |
348 | 0 | uint32_t col, row; |
349 | 0 | int offset; |
350 | |
|
351 | 0 | switch (csid) { |
352 | 0 | case 0: |
353 | | /* ISO-646 */ |
354 | 0 | if (idx >= 0x80) |
355 | 0 | return (EILSEQ); |
356 | 0 | *wc = (_citrus_wc_t)idx; |
357 | 0 | break; |
358 | 0 | case 1: |
359 | | /* kana */ |
360 | 0 | if (idx >= 0x80) |
361 | 0 | return (EILSEQ); |
362 | 0 | *wc = (_citrus_wc_t)idx + 0x80; |
363 | 0 | break; |
364 | 0 | case 3: |
365 | 0 | if ((ei->mode & MODE_JIS2004) == 0) |
366 | 0 | return (EILSEQ); |
367 | | /*FALLTHROUGH*/ |
368 | 0 | case 2: |
369 | | /* kanji */ |
370 | 0 | row = (idx >> 8); |
371 | 0 | if (row < 0x21) |
372 | 0 | return (EILSEQ); |
373 | 0 | if (csid == 3) { |
374 | 0 | if (row <= 0x2F) |
375 | 0 | offset = (row == 0x22 || row >= 0x26) ? |
376 | 0 | 0xED : 0xF0; |
377 | 0 | else if (row >= 0x4D && row <= 0x7E) |
378 | 0 | offset = 0xCE; |
379 | 0 | else |
380 | 0 | return (EILSEQ); |
381 | 0 | } else { |
382 | 0 | if (row > 0x97) |
383 | 0 | return (EILSEQ); |
384 | 0 | offset = (row < 0x5F) ? 0x81 : 0xC1; |
385 | 0 | } |
386 | 0 | col = idx & 0xFF; |
387 | 0 | if (col < 0x21 || col > 0x7E) |
388 | 0 | return (EILSEQ); |
389 | 0 | row -= 0x21; col -= 0x21; |
390 | 0 | if ((row & 1) == 0) { |
391 | 0 | col += 0x40; |
392 | 0 | if (col >= 0x7F) |
393 | 0 | col += 1; |
394 | 0 | } else |
395 | 0 | col += 0x9F; |
396 | 0 | row = row / 2 + offset; |
397 | 0 | *wc = ((_citrus_wc_t)row << 8) | col; |
398 | 0 | break; |
399 | 0 | default: |
400 | 0 | return (EILSEQ); |
401 | 0 | } |
402 | | |
403 | 0 | return (0); |
404 | 0 | } |
405 | | |
406 | | static __inline int |
407 | | /*ARGSUSED*/ |
408 | | _citrus_MSKanji_stdenc_get_state_desc_generic(_MSKanjiEncodingInfo * ei , |
409 | | _MSKanjiState * psenc, int * rstate) |
410 | 0 | { |
411 | 0 | (void) ei; |
412 | |
|
413 | 0 | *rstate = (psenc->chlen == 0) ? _CITRUS_STDENC_SDGEN_INITIAL : |
414 | 0 | _CITRUS_STDENC_SDGEN_INCOMPLETE_CHAR; |
415 | 0 | return (0); |
416 | 0 | } |
417 | | |
418 | | static int |
419 | | /*ARGSUSED*/ |
420 | | _citrus_MSKanji_encoding_module_init(_MSKanjiEncodingInfo * ei, |
421 | | const void * var, size_t lenvar) |
422 | 0 | { |
423 | 0 | const char *p; |
424 | |
|
425 | 0 | p = var; |
426 | 0 | memset((void *)ei, 0, sizeof(*ei)); |
427 | 0 | while (lenvar > 0) { |
428 | 0 | switch (_citrus_bcs_toupper(*p)) { |
429 | 0 | case 'J': |
430 | 0 | MATCH(JIS2004, ei->mode |= MODE_JIS2004); |
431 | 0 | break; |
432 | 0 | } |
433 | 0 | ++p; |
434 | 0 | --lenvar; |
435 | 0 | } |
436 | | |
437 | 0 | return (0); |
438 | 0 | } |
439 | | |
440 | | static void |
441 | | _citrus_MSKanji_encoding_module_uninit(_MSKanjiEncodingInfo *ei ) |
442 | 0 | { |
443 | 0 | (void) ei; |
444 | 0 | } |
445 | | |
446 | | /* ---------------------------------------------------------------------- |
447 | | * public interface for stdenc |
448 | | */ |
449 | | |
450 | | _CITRUS_STDENC_DECLS(MSKanji); |
451 | | _CITRUS_STDENC_DEF_OPS(MSKanji); |
452 | | |
453 | | #include "citrus_stdenc_template.h" |