Sync Citrus iconv support with NetBSD.
[dragonfly/netmp.git] / lib / libc / citrus / modules / citrus_viqr.c
blobf76ce7be67caa8a344c8cf1073e121f69051bf33
1 /* $NetBSD: citrus_viqr.c,v 1.3 2006/11/22 20:11:03 tnozaki Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/modules/citrus_viqr.c,v 1.1 2008/04/10 10:21:02 hasso Exp $ */
4 /*-
5 * Copyright (c)2006 Citrus Project,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
31 #include <sys/queue.h>
32 #include <sys/types.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <stddef.h>
39 #include <locale.h>
40 #include <wchar.h>
41 #include <limits.h>
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_bcs.h"
46 #include "citrus_module.h"
47 #include "citrus_ctype.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_viqr.h"
51 #define ESCAPE '\\'
54 * this table generated from RFC 1456.
56 static const char *mnemonic_rfc1456[0x100] = {
57 NULL , NULL , "A(?", NULL , NULL , "A(~", "A^~", NULL ,
58 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
59 NULL , NULL , NULL , NULL , "Y?" , NULL , NULL , NULL ,
60 NULL , "Y~" , NULL , NULL , NULL , NULL , "Y." , NULL ,
61 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
62 NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
63 NULL , NULL , NULL , NULL , NULL , NULL , NULL , 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 "A." , "A('", "A(`", "A(.", "A^'", "A^`", "A^?", "A^.",
74 "E~" , "E." , "E^'", "E^`", "E^?", "E^~", "E^.", "O^'",
75 "O^`", "O^?", "O^~", "O^.", "O+.", "O+'", "O+`", "O+?",
76 "I." , "O?" , "O." , "I?" , "U?" , "U~" , "U." , "Y`" ,
77 "O~" , "a('", "a(`", "a(.", "a^'", "a^`", "a^?", "a^.",
78 "e~" , "e." , "e^'", "e^`", "e^?", "e^~", "e^.", "o^'",
79 "o^`", "o^?", "o^~", "O+~", "O+" , "o^.", "o+`", "o+?",
80 "i." , "U+.", "U+'", "U+`", "U+?", "o+" , "o+'", "U+" ,
81 "A`" , "A'" , "A^" , "A~" , "A?" , "A(" , "a(?", "a(~",
82 "E`" , "E'" , "E^" , "E?" , "I`" , "I'" , "I~" , "y`" ,
83 "DD" , "u+'", "O`" , "O'" , "O^" , "a." , "y?" , "u+`",
84 "u+?", "U`" , "U'" , "y~" , "y." , "Y'" , "o+~", "u+" ,
85 "a`" , "a'" , "a^" , "a~" , "a?" , "a(" , "u+~", "a^~",
86 "e`" , "e'" , "e^" , "e?" , "i`" , "i'" , "i~" , "i?" ,
87 "dd" , "u+.", "o`" , "o'" , "o^" , "o~" , "o?" , "o." ,
88 "u." , "u`" , "u'" , "u~" , "u?" , "y'" , "o+.", "U+~",
91 typedef struct {
92 const char *name;
93 wchar_t value;
94 } mnemonic_def_t;
96 static const mnemonic_def_t mnemonic_ext[] = {
97 /* add extra mnemonic here (should be sorted by wchar_t order). */
99 static const size_t mnemonic_ext_size =
100 sizeof(mnemonic_ext) / sizeof(mnemonic_def_t);
102 static const char *
103 mnemonic_ext_find(wchar_t wc, const mnemonic_def_t *head, size_t n)
105 const mnemonic_def_t *mid;
107 _DIAGASSERT(head != NULL);
109 for (; n > 0; n >>= 1) {
110 mid = head + (n >> 1);
111 if (mid->value == wc) {
112 return mid->name;
113 } else if (mid->value < wc) {
114 head = mid + 1;
115 --n;
118 return NULL;
121 struct mnemonic_t;
122 typedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t;
123 typedef struct mnemonic_t {
124 TAILQ_ENTRY(mnemonic_t) entry;
125 int ascii;
126 struct mnemonic_t *parent;
127 mnemonic_list_t child;
128 wchar_t value;
129 } mnemonic_t;
131 static mnemonic_t *
132 mnemonic_list_find(mnemonic_list_t *ml, int ch)
134 mnemonic_t *m;
136 _DIAGASSERT(ml != NULL);
138 TAILQ_FOREACH(m, ml, entry) {
139 if (m->ascii == ch)
140 return m;
143 return NULL;
146 static mnemonic_t *
147 mnemonic_create(mnemonic_t *parent, int ascii, wchar_t value)
149 mnemonic_t *m;
151 _DIAGASSERT(parent != NULL);
153 m = malloc(sizeof(*m));
154 if (m != NULL) {
155 m->parent = parent;
156 m->ascii = ascii;
157 m->value = value;
158 TAILQ_INIT(&m->child);
161 return m;
164 static int
165 mnemonic_append_child(mnemonic_t *m, const char *s,
166 wchar_t value, wchar_t invalid)
168 int ch;
169 mnemonic_t *m0;
171 _DIAGASSERT(m != NULL);
172 _DIAGASSERT(s != NULL);
174 ch = (unsigned char)*s++;
175 if (ch == '\0')
176 return EINVAL;
177 m0 = mnemonic_list_find(&m->child, ch);
178 if (m0 == NULL) {
179 m0 = mnemonic_create(m, ch, (wchar_t)ch);
180 if (m0 == NULL)
181 return ENOMEM;
182 TAILQ_INSERT_TAIL(&m->child, m0, entry);
184 m = m0;
185 for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) {
186 m0 = mnemonic_list_find(&m->child, ch);
187 if (m0 == NULL) {
188 m0 = mnemonic_create(m, ch, invalid);
189 if (m0 == NULL)
190 return ENOMEM;
191 TAILQ_INSERT_TAIL(&m->child, m0, entry);
193 m = m0;
195 if (m0 == NULL)
196 return EINVAL;
197 m0->value = value;
199 return 0;
202 static void
203 mnemonic_destroy(mnemonic_t *m)
205 mnemonic_t *m0;
207 _DIAGASSERT(m != NULL);
209 TAILQ_FOREACH(m0, &m->child, entry)
210 mnemonic_destroy(m0);
211 free(m);
214 typedef struct {
215 size_t mb_cur_max;
216 wchar_t invalid;
217 mnemonic_t *mroot;
218 } _VIQREncodingInfo;
220 typedef struct {
221 int chlen;
222 char ch[MB_LEN_MAX];
223 } _VIQRState;
225 typedef struct {
226 _VIQREncodingInfo ei;
227 struct {
228 /* for future multi-locale facility */
229 _VIQRState s_mblen;
230 _VIQRState s_mbrlen;
231 _VIQRState s_mbrtowc;
232 _VIQRState s_mbtowc;
233 _VIQRState s_mbsrtowcs;
234 _VIQRState s_wcrtomb;
235 _VIQRState s_wcsrtombs;
236 _VIQRState s_wctomb;
237 } states;
238 } _VIQRCTypeInfo;
240 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
241 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
243 #define _FUNCNAME(m) _citrus_VIQR_##m
244 #define _ENCODING_INFO _VIQREncodingInfo
245 #define _CTYPE_INFO _VIQRCTypeInfo
246 #define _ENCODING_STATE _VIQRState
247 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
248 #define _ENCODING_IS_STATE_DEPENDENT 1
249 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
251 static __inline void
252 /*ARGSUSED*/
253 _citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei,
254 _VIQRState * __restrict psenc)
256 /* ei may be unused */
257 _DIAGASSERT(psenc != NULL);
259 psenc->chlen = 0;
262 static __inline void
263 /*ARGSUSED*/
264 _citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei,
265 void *__restrict pspriv, const _VIQRState * __restrict psenc)
267 /* ei may be unused */
268 _DIAGASSERT(pspriv != NULL);
269 _DIAGASSERT(psenc != NULL);
271 memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
274 static __inline void
275 /*ARGSUSED*/
276 _citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei,
277 _VIQRState * __restrict psenc, const void * __restrict pspriv)
279 /* ei may be unused */
280 _DIAGASSERT(psenc != NULL);
281 _DIAGASSERT(pspriv != NULL);
283 memcpy((void *)psenc, pspriv, sizeof(*psenc));
286 static int
287 _citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei,
288 wchar_t * __restrict pwc, const char ** __restrict s, size_t n,
289 _VIQRState * __restrict psenc, size_t * __restrict nresult)
291 const char *s0;
292 wchar_t wc;
293 mnemonic_t *m, *m0;
294 size_t i;
295 int ch, escape;
297 _DIAGASSERT(ei != NULL);
298 /* pwc may be null */
299 _DIAGASSERT(s != NULL);
300 _DIAGASSERT(psenc != NULL);
301 _DIAGASSERT(nresult != NULL);
303 if (*s == NULL) {
304 _citrus_VIQR_init_state(ei, psenc);
305 *nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
306 return 0;
308 s0 = *s;
310 i = 0;
311 m = ei->mroot;
312 for (escape = 0;;) {
313 if (psenc->chlen == i) {
314 if (n-- < 1) {
315 *s = s0;
316 *nresult = (size_t)-2;
317 return 0;
319 psenc->ch[psenc->chlen++] = *s0++;
321 ch = (unsigned char)psenc->ch[i++];
322 if (ch == ESCAPE) {
323 if (m != ei->mroot)
324 break;
325 escape = 1;
326 continue;
328 if (escape != 0)
329 break;
330 m0 = mnemonic_list_find(&m->child, ch);
331 if (m0 == NULL)
332 break;
333 m = m0;
335 while (m != ei->mroot) {
336 --i;
337 if (m->value != ei->invalid)
338 break;
339 m = m->parent;
341 if (ch == ESCAPE && m != ei->mroot)
342 ++i;
343 psenc->chlen -= i;
344 memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen);
345 wc = (m == ei->mroot) ? (wchar_t)ch : m->value;
346 if (pwc != NULL)
347 *pwc = wc;
348 *nresult = (size_t)(wc == 0 ? 0 : s0 - *s);
349 *s = s0;
351 return 0;
354 static int
355 _citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei,
356 char * __restrict s, size_t n, wchar_t wc,
357 _VIQRState * __restrict psenc, size_t * __restrict nresult)
359 mnemonic_t *m;
360 int ch, escape;
361 const char *p;
363 _DIAGASSERT(ei != NULL);
364 _DIAGASSERT(s != NULL);
365 _DIAGASSERT(psenc != NULL);
366 _DIAGASSERT(nresult != NULL);
368 switch (psenc->chlen) {
369 case 0: case 1:
370 break;
371 default:
372 return EINVAL;
374 m = NULL;
375 if ((uint32_t)wc <= 0xFF) {
376 p = mnemonic_rfc1456[wc & 0xFF];
377 if (p != NULL)
378 goto mnemonic_found;
379 if (n-- < 1)
380 goto e2big;
381 ch = (unsigned int)wc;
382 m = ei->mroot;
383 if (psenc->chlen > 0) {
384 m = mnemonic_list_find(&m->child, psenc->ch[0]);
385 if (m == NULL)
386 return EINVAL;
387 psenc->ch[0] = ESCAPE;
389 if (mnemonic_list_find(&m->child, ch) == NULL) {
390 psenc->chlen = 0;
391 m = NULL;
393 psenc->ch[psenc->chlen++] = ch;
394 } else {
395 p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size);
396 if (p == NULL) {
397 *nresult = (size_t)-1;
398 return EILSEQ;
399 } else {
400 mnemonic_found:
401 psenc->chlen = 0;
402 while (*p != '\0') {
403 if (n-- < 1)
404 goto e2big;
405 psenc->ch[psenc->chlen++] = *p++;
409 memcpy(s, psenc->ch, psenc->chlen);
410 *nresult = psenc->chlen;
411 if (m == ei->mroot) {
412 psenc->ch[0] = ch;
413 psenc->chlen = 1;
414 } else {
415 psenc->chlen = 0;
418 return 0;
420 e2big:
421 *nresult = (size_t)-1;
422 return E2BIG;
425 static int
426 /* ARGSUSED */
427 _citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei,
428 char * __restrict s, size_t n, _VIQRState * __restrict psenc,
429 size_t * __restrict nresult)
431 /* ei may be unused */
432 _DIAGASSERT(s != NULL);
433 _DIAGASSERT(psenc != NULL);
434 _DIAGASSERT(nresult != NULL);
436 switch (psenc->chlen) {
437 case 0: case 1:
438 break;
439 default:
440 return EINVAL;
442 *nresult = 0;
443 psenc->chlen = 0;
445 return 0;
448 static __inline int
449 /*ARGSUSED*/
450 _citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei,
451 _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
453 /* ei may be unused */
454 _DIAGASSERT(csid != NULL);
455 _DIAGASSERT(idx != NULL);
457 *csid = 0;
458 *idx = (_index_t)wc;
460 return 0;
463 static __inline int
464 /*ARGSUSED*/
465 _citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei,
466 wchar_t * __restrict pwc, _csid_t csid, _index_t idx)
468 /* ei may be unused */
469 _DIAGASSERT(pwc != NULL);
471 if (csid != 0)
472 return EILSEQ;
473 *pwc = (wchar_t)idx;
475 return 0;
478 static void
479 _citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei)
481 _DIAGASSERT(ei != NULL);
483 mnemonic_destroy(ei->mroot);
486 static int
487 /*ARGSUSED*/
488 _citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei,
489 const void * __restrict var, size_t lenvar)
491 int errnum;
492 const char *s;
493 size_t i, n;
494 const mnemonic_def_t *p;
496 _DIAGASSERT(ei != NULL);
497 /* var may be unused */
499 ei->mb_cur_max = 1;
500 ei->invalid = (wchar_t)-1;
501 ei->mroot = mnemonic_create(NULL, '\0', ei->invalid);
502 if (ei->mroot == NULL)
503 return ENOMEM;
504 for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) {
505 s = mnemonic_rfc1456[i];
506 if (s == NULL)
507 continue;
508 n = strlen(s);
509 _DIAGASSERT(n <= MB_LEN_MAX);
510 if (ei->mb_cur_max < n)
511 ei->mb_cur_max = n;
512 errnum = mnemonic_append_child(ei->mroot,
513 s, (wchar_t)i, ei->invalid);
514 if (errnum != 0) {
515 _citrus_VIQR_encoding_module_uninit(ei);
516 return errnum;
519 for (i = 0; i < mnemonic_ext_size; ++i) {
520 p = &mnemonic_ext[i];
521 _DIAGASSERT(p != NULL && p->name != NULL);
522 n = strlen(p->name);
523 _DIAGASSERT(n <= MB_LEN_MAX);
524 if (ei->mb_cur_max < n)
525 ei->mb_cur_max = n;
526 errnum = mnemonic_append_child(ei->mroot,
527 p->name, p->value, ei->invalid);
528 if (errnum != 0) {
529 _citrus_VIQR_encoding_module_uninit(ei);
530 return errnum;
534 return 0;
537 static __inline int
538 /*ARGSUSED*/
539 _citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei,
540 _VIQRState * __restrict psenc, int * __restrict rstate)
542 /* ei may be unused */
543 _DIAGASSERT(psenc != NULL);
544 _DIAGASSERT(rstate != NULL);
546 *rstate = (psenc->chlen == 0)
547 ? _STDENC_SDGEN_INITIAL
548 : _STDENC_SDGEN_INCOMPLETE_CHAR;
550 return 0;
553 /* ----------------------------------------------------------------------
554 * public interface for ctype
557 _CITRUS_CTYPE_DECLS(VIQR);
558 _CITRUS_CTYPE_DEF_OPS(VIQR);
560 #include "citrus_ctype_template.h"
562 /* ----------------------------------------------------------------------
563 * public interface for stdenc
566 _CITRUS_STDENC_DECLS(VIQR);
567 _CITRUS_STDENC_DEF_OPS(VIQR);
569 #include "citrus_stdenc_template.h"