citrus_viqr.c revision 1.6.22.2 1 /* $NetBSD: citrus_viqr.c,v 1.6.22.2 2017/07/21 20:22:29 perseant Exp $ */
2
3 /*-
4 * Copyright (c)2006 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 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
32 __RCSID("$NetBSD: citrus_viqr.c,v 1.6.22.2 2017/07/21 20:22:29 perseant Exp $");
33 #endif /* LIBC_SCCS and not lint */
34
35 #include <sys/queue.h>
36 #include <sys/types.h>
37 #include <assert.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <stdint.h>
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <wchar.h>
44 #include <limits.h>
45
46 #include "citrus_namespace.h"
47 #include "citrus_types.h"
48 #include "citrus_bcs.h"
49 #include "citrus_module.h"
50 #include "citrus_ctype.h"
51 #include "citrus_stdenc.h"
52 #include "citrus_viqr.h"
53
54 #define ESCAPE '\\'
55
56 /*
57 * this table generated from RFC 1456.
58 */
59 static const char *mnemonic_rfc1456[0x100] = {
60 NULL , NULL , "A(?", NULL , NULL , "A(~", "A^~", NULL ,
61 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
62 NULL , NULL , NULL , NULL , "Y?" , NULL , NULL , NULL ,
63 NULL , "Y~" , NULL , NULL , NULL , NULL , "Y." , NULL ,
64 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
65 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
66 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
67 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
68 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
69 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
70 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
71 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
72 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
73 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
74 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
75 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
76 "A." , "A('", "A(`", "A(.", "A^'", "A^`", "A^?", "A^.",
77 "E~" , "E." , "E^'", "E^`", "E^?", "E^~", "E^.", "O^'",
78 "O^`", "O^?", "O^~", "O^.", "O+.", "O+'", "O+`", "O+?",
79 "I." , "O?" , "O." , "I?" , "U?" , "U~" , "U." , "Y`" ,
80 "O~" , "a('", "a(`", "a(.", "a^'", "a^`", "a^?", "a^.",
81 "e~" , "e." , "e^'", "e^`", "e^?", "e^~", "e^.", "o^'",
82 "o^`", "o^?", "o^~", "O+~", "O+" , "o^.", "o+`", "o+?",
83 "i." , "U+.", "U+'", "U+`", "U+?", "o+" , "o+'", "U+" ,
84 "A`" , "A'" , "A^" , "A~" , "A?" , "A(" , "a(?", "a(~",
85 "E`" , "E'" , "E^" , "E?" , "I`" , "I'" , "I~" , "y`" ,
86 "DD" , "u+'", "O`" , "O'" , "O^" , "a." , "y?" , "u+`",
87 "u+?", "U`" , "U'" , "y~" , "y." , "Y'" , "o+~", "u+" ,
88 "a`" , "a'" , "a^" , "a~" , "a?" , "a(" , "u+~", "a^~",
89 "e`" , "e'" , "e^" , "e?" , "i`" , "i'" , "i~" , "i?" ,
90 "dd" , "u+.", "o`" , "o'" , "o^" , "o~" , "o?" , "o." ,
91 "u." , "u`" , "u'" , "u~" , "u?" , "y'" , "o+.", "U+~",
92 };
93
94 typedef struct {
95 const char *name;
96 wchar_kuten_t value;
97 } mnemonic_def_t;
98
99 static const mnemonic_def_t mnemonic_ext[] = {
100 /* add extra mnemonic here (should be sorted by wchar_kuten_t order). */
101 };
102 static const size_t mnemonic_ext_size =
103 sizeof(mnemonic_ext) / sizeof(mnemonic_def_t);
104
105 static const char *
106 mnemonic_ext_find(wchar_kuten_t wc, const mnemonic_def_t *head, size_t n)
107 {
108 const mnemonic_def_t *mid;
109
110 _DIAGASSERT(head != NULL);
111
112 for (; n > 0; n >>= 1) {
113 mid = head + (n >> 1);
114 if (mid->value == wc) {
115 return mid->name;
116 } else if (mid->value < wc) {
117 head = mid + 1;
118 --n;
119 }
120 }
121 return NULL;
122 }
123
124 struct mnemonic_t;
125 typedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t;
126 typedef struct mnemonic_t {
127 TAILQ_ENTRY(mnemonic_t) entry;
128 int ascii;
129 struct mnemonic_t *parent;
130 mnemonic_list_t child;
131 wchar_kuten_t value;
132 } mnemonic_t;
133
134 static mnemonic_t *
135 mnemonic_list_find(mnemonic_list_t *ml, int ch)
136 {
137 mnemonic_t *m;
138
139 _DIAGASSERT(ml != NULL);
140
141 TAILQ_FOREACH(m, ml, entry) {
142 if (m->ascii == ch)
143 return m;
144 }
145
146 return NULL;
147 }
148
149 static mnemonic_t *
150 mnemonic_create(mnemonic_t *parent, int ascii, wchar_kuten_t value)
151 {
152 mnemonic_t *m;
153
154 _DIAGASSERT(parent != NULL);
155
156 m = malloc(sizeof(*m));
157 if (m != NULL) {
158 m->parent = parent;
159 m->ascii = ascii;
160 m->value = value;
161 TAILQ_INIT(&m->child);
162 }
163
164 return m;
165 }
166
167 static int
168 mnemonic_append_child(mnemonic_t *m, const char *s,
169 wchar_kuten_t value, wchar_kuten_t invalid)
170 {
171 int ch;
172 mnemonic_t *m0;
173
174 _DIAGASSERT(m != NULL);
175 _DIAGASSERT(s != NULL);
176
177 ch = (unsigned char)*s++;
178 if (ch == '\0')
179 return EINVAL;
180 m0 = mnemonic_list_find(&m->child, ch);
181 if (m0 == NULL) {
182 m0 = mnemonic_create(m, ch, (wchar_kuten_t)ch);
183 if (m0 == NULL)
184 return ENOMEM;
185 TAILQ_INSERT_TAIL(&m->child, m0, entry);
186 }
187 m = m0;
188 for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) {
189 m0 = mnemonic_list_find(&m->child, ch);
190 if (m0 == NULL) {
191 m0 = mnemonic_create(m, ch, invalid);
192 if (m0 == NULL)
193 return ENOMEM;
194 TAILQ_INSERT_TAIL(&m->child, m0, entry);
195 }
196 m = m0;
197 }
198 if (m0 == NULL)
199 return EINVAL;
200 m0->value = value;
201
202 return 0;
203 }
204
205 static void
206 mnemonic_destroy(mnemonic_t *m)
207 {
208 mnemonic_t *m0;
209
210 _DIAGASSERT(m != NULL);
211
212 TAILQ_FOREACH(m0, &m->child, entry)
213 mnemonic_destroy(m0);
214 free(m);
215 }
216
217 typedef struct {
218 size_t mb_cur_max;
219 wchar_kuten_t invalid;
220 mnemonic_t *mroot;
221 } _VIQREncodingInfo;
222
223 typedef struct {
224 int chlen;
225 char ch[MB_LEN_MAX];
226 } _VIQRState;
227
228 typedef struct {
229 _VIQREncodingInfo ei;
230 struct {
231 /* for future multi-locale facility */
232 _VIQRState s_mblen;
233 _VIQRState s_mbrlen;
234 _VIQRState s_mbrtowc;
235 _VIQRState s_mbtowc;
236 _VIQRState s_mbsrtowcs;
237 _VIQRState s_mbsnrtowcs;
238 _VIQRState s_wcrtomb;
239 _VIQRState s_wcsrtombs;
240 _VIQRState s_wcsnrtombs;
241 _VIQRState s_wctomb;
242 } states;
243 } _VIQRCTypeInfo;
244
245 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
246 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
247
248 #define _FUNCNAME(m) _citrus_VIQR_##m
249 #define _ENCODING_INFO _VIQREncodingInfo
250 #define _CTYPE_INFO _VIQRCTypeInfo
251 #define _ENCODING_STATE _VIQRState
252 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
253 #define _ENCODING_IS_STATE_DEPENDENT 1
254 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
255
256 #include "citrus_u2k_template.h"
257
258 static void
259 /*ARGSUSED*/
260 _citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei,
261 _VIQRState * __restrict psenc)
262 {
263 /* ei may be unused */
264 _DIAGASSERT(psenc != NULL);
265
266 psenc->chlen = 0;
267 }
268
269 static void
270 /*ARGSUSED*/
271 _citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei,
272 void *__restrict pspriv, const _VIQRState * __restrict psenc)
273 {
274 /* ei may be unused */
275 _DIAGASSERT(pspriv != NULL);
276 _DIAGASSERT(psenc != NULL);
277
278 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
279 }
280
281 static __inline void
282 /*ARGSUSED*/
283 _citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei,
284 _VIQRState * __restrict psenc, const void * __restrict pspriv)
285 {
286 /* ei may be unused */
287 _DIAGASSERT(psenc != NULL);
288 _DIAGASSERT(pspriv != NULL);
289
290 memcpy((void *)psenc, pspriv, sizeof(*psenc));
291 }
292
293 static int
294 _citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei,
295 wchar_ucs4_t * __restrict pwc, const char ** __restrict s, size_t n,
296 _VIQRState * __restrict psenc, size_t * __restrict nresult)
297 {
298 const char *s0;
299 wchar_kuten_t wc;
300 mnemonic_t *m, *m0;
301 size_t i;
302 int ch, escape;
303
304 _DIAGASSERT(ei != NULL);
305 /* pwc may be null */
306 _DIAGASSERT(s != NULL);
307 _DIAGASSERT(psenc != NULL);
308 _DIAGASSERT(nresult != NULL);
309
310 if (*s == NULL) {
311 _citrus_VIQR_init_state(ei, psenc);
312 *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
313 return 0;
314 }
315 s0 = *s;
316
317 i = 0;
318 m = ei->mroot;
319 for (escape = 0;;) {
320 if (psenc->chlen == i) {
321 if (n-- < 1) {
322 *s = s0;
323 *nresult = (size_t)-2;
324 return 0;
325 }
326 psenc->ch[psenc->chlen++] = *s0++;
327 }
328 ch = (unsigned char)psenc->ch[i++];
329 if (ch == ESCAPE) {
330 if (m != ei->mroot)
331 break;
332 escape = 1;
333 continue;
334 }
335 if (escape != 0)
336 break;
337 m0 = mnemonic_list_find(&m->child, ch);
338 if (m0 == NULL)
339 break;
340 m = m0;
341 }
342 while (m != ei->mroot) {
343 --i;
344 if (m->value != ei->invalid)
345 break;
346 m = m->parent;
347 }
348 if (ch == ESCAPE && m != ei->mroot)
349 ++i;
350 psenc->chlen -= i;
351 memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen);
352 wc = (m == ei->mroot) ? (wchar_kuten_t)ch : m->value;
353 if (pwc != NULL)
354 _citrus_VIQR_kt2ucs(ei, pwc, wc);
355 *nresult = (size_t)(wc == 0 ? 0 : s0 - *s);
356 *s = s0;
357
358 return 0;
359 }
360
361 static int
362 _citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei,
363 char * __restrict s, size_t n, wchar_ucs4_t wc,
364 _VIQRState * __restrict psenc, size_t * __restrict nresult)
365 {
366 mnemonic_t *m;
367 int ch;
368 const char *p;
369
370 _DIAGASSERT(ei != NULL);
371 _DIAGASSERT(s != NULL);
372 _DIAGASSERT(psenc != NULL);
373 _DIAGASSERT(nresult != NULL);
374
375 _citrus_VIQR_ucs2kt(ei, &wc, wc);
376
377 switch (psenc->chlen) {
378 case 0: case 1:
379 break;
380 default:
381 return EINVAL;
382 }
383 m = NULL;
384 if ((uint32_t)wc <= 0xFF) {
385 p = mnemonic_rfc1456[wc & 0xFF];
386 if (p != NULL)
387 goto mnemonic_found;
388 if (n-- < 1)
389 goto e2big;
390 ch = (unsigned int)wc;
391 m = ei->mroot;
392 if (psenc->chlen > 0) {
393 m = mnemonic_list_find(&m->child, psenc->ch[0]);
394 if (m == NULL)
395 return EINVAL;
396 psenc->ch[0] = ESCAPE;
397 }
398 if (mnemonic_list_find(&m->child, ch) == NULL) {
399 psenc->chlen = 0;
400 m = NULL;
401 }
402 psenc->ch[psenc->chlen++] = ch;
403 } else {
404 p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size);
405 if (p == NULL) {
406 *nresult = (size_t)-1;
407 return EILSEQ;
408 } else {
409 mnemonic_found:
410 psenc->chlen = 0;
411 while (*p != '\0') {
412 if (n-- < 1)
413 goto e2big;
414 psenc->ch[psenc->chlen++] = *p++;
415 }
416 }
417 }
418 memcpy(s, psenc->ch, psenc->chlen);
419 *nresult = psenc->chlen;
420 if (m == ei->mroot) {
421 psenc->ch[0] = ch;
422 psenc->chlen = 1;
423 } else {
424 psenc->chlen = 0;
425 }
426
427 return 0;
428
429 e2big:
430 *nresult = (size_t)-1;
431 return E2BIG;
432 }
433
434 static int
435 /* ARGSUSED */
436 _citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei,
437 char * __restrict s, size_t n, _VIQRState * __restrict psenc,
438 size_t * __restrict nresult)
439 {
440 /* ei may be unused */
441 _DIAGASSERT(s != NULL);
442 _DIAGASSERT(psenc != NULL);
443 _DIAGASSERT(nresult != NULL);
444
445 switch (psenc->chlen) {
446 case 0: case 1:
447 break;
448 default:
449 return EINVAL;
450 }
451 *nresult = 0;
452 psenc->chlen = 0;
453
454 return 0;
455 }
456
457 static __inline int
458 /*ARGSUSED*/
459 _citrus_VIQR_stdenc_wctocs(struct _citrus_stdenc *ce,
460 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_kuten_t wc)
461 {
462 /* ei may be unused */
463 _DIAGASSERT(csid != NULL);
464 _DIAGASSERT(idx != NULL);
465
466 *csid = 0;
467 *idx = (_index_t)wc;
468
469 return 0;
470 }
471
472 static __inline int
473 /*ARGSUSED*/
474 _citrus_VIQR_stdenc_cstowc(struct _citrus_stdenc *ce,
475 wchar_kuten_t * __restrict pwc, _csid_t csid, _index_t idx)
476 {
477 /* ei may be unused */
478 _DIAGASSERT(pwc != NULL);
479
480 if (csid != 0)
481 return EILSEQ;
482 *pwc = (wchar_kuten_t)idx;
483
484 return 0;
485 }
486
487 static void
488 _citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei)
489 {
490 _DIAGASSERT(ei != NULL);
491
492 mnemonic_destroy(ei->mroot);
493 }
494
495 static int
496 /*ARGSUSED*/
497 _citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei,
498 const void * __restrict var, size_t lenvar)
499 {
500 int errnum;
501 const char *s;
502 size_t i, n;
503 const mnemonic_def_t *p;
504
505 _DIAGASSERT(ei != NULL);
506 /* var may be unused */
507
508 ei->mb_cur_max = 1;
509 ei->invalid = (wchar_kuten_t)-1;
510 ei->mroot = mnemonic_create(NULL, '\0', ei->invalid);
511 if (ei->mroot == NULL)
512 return ENOMEM;
513 for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) {
514 s = mnemonic_rfc1456[i];
515 if (s == NULL)
516 continue;
517 n = strlen(s);
518 _DIAGASSERT(n <= MB_LEN_MAX);
519 if (ei->mb_cur_max < n)
520 ei->mb_cur_max = n;
521 errnum = mnemonic_append_child(ei->mroot,
522 s, (wchar_kuten_t)i, ei->invalid);
523 if (errnum != 0) {
524 _citrus_VIQR_encoding_module_uninit(ei);
525 return errnum;
526 }
527 }
528 for (i = 0; i < mnemonic_ext_size; ++i) {
529 p = &mnemonic_ext[i];
530 _DIAGASSERT(p != NULL && p->name != NULL);
531 n = strlen(p->name);
532 _DIAGASSERT(n <= MB_LEN_MAX);
533 if (ei->mb_cur_max < n)
534 ei->mb_cur_max = n;
535 errnum = mnemonic_append_child(ei->mroot,
536 p->name, p->value, ei->invalid);
537 if (errnum != 0) {
538 _citrus_VIQR_encoding_module_uninit(ei);
539 return errnum;
540 }
541 }
542
543 return 0;
544 }
545
546 static __inline int
547 /*ARGSUSED*/
548 _citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei,
549 _VIQRState * __restrict psenc, int * __restrict rstate)
550 {
551 /* ei may be unused */
552 _DIAGASSERT(psenc != NULL);
553 _DIAGASSERT(rstate != NULL);
554
555 *rstate = (psenc->chlen == 0)
556 ? _STDENC_SDGEN_INITIAL
557 : _STDENC_SDGEN_INCOMPLETE_CHAR;
558
559 return 0;
560 }
561
562 /* ----------------------------------------------------------------------
563 * public interface for ctype
564 */
565
566 _CITRUS_CTYPE_DECLS(VIQR);
567 _CITRUS_CTYPE_DEF_OPS(VIQR);
568
569 #include "citrus_ctype_template.h"
570
571 /* ----------------------------------------------------------------------
572 * public interface for stdenc
573 */
574
575 _CITRUS_STDENC_DECLS(VIQR);
576 _CITRUS_STDENC_DEF_OPS(VIQR);
577
578 #include "citrus_stdenc_template.h"
579