1 /* $NetBSD: citrus_viqr.c,v 1.5 2011/11/19 18:20:13 tnozaki Exp $ */
4 * Copyright (c)2006 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/cdefs.h>
31 #if defined(LIBC_SCCS) && !defined(lint)
32 __RCSID("$NetBSD: citrus_viqr.c,v 1.5 2011/11/19 18:20:13 tnozaki Exp $");
33 #endif /* LIBC_SCCS and not lint */
35 #include <sys/queue.h>
36 #include <sys/types.h>
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"
57 * this table generated from RFC 1456.
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+~",
99 static const mnemonic_def_t mnemonic_ext
[] = {
100 /* add extra mnemonic here (should be sorted by wchar_t order). */
102 static const size_t mnemonic_ext_size
=
103 sizeof(mnemonic_ext
) / sizeof(mnemonic_def_t
);
106 mnemonic_ext_find(wchar_t wc
, const mnemonic_def_t
*head
, size_t n
)
108 const mnemonic_def_t
*mid
;
110 _DIAGASSERT(head
!= NULL
);
112 for (; n
> 0; n
>>= 1) {
113 mid
= head
+ (n
>> 1);
114 if (mid
->value
== wc
) {
116 } else if (mid
->value
< wc
) {
125 typedef TAILQ_HEAD(mnemonic_list_t
, mnemonic_t
) mnemonic_list_t
;
126 typedef struct mnemonic_t
{
127 TAILQ_ENTRY(mnemonic_t
) entry
;
129 struct mnemonic_t
*parent
;
130 mnemonic_list_t child
;
135 mnemonic_list_find(mnemonic_list_t
*ml
, int ch
)
139 _DIAGASSERT(ml
!= NULL
);
141 TAILQ_FOREACH(m
, ml
, entry
) {
150 mnemonic_create(mnemonic_t
*parent
, int ascii
, wchar_t value
)
154 _DIAGASSERT(parent
!= NULL
);
156 m
= malloc(sizeof(*m
));
161 TAILQ_INIT(&m
->child
);
168 mnemonic_append_child(mnemonic_t
*m
, const char *s
,
169 wchar_t value
, wchar_t invalid
)
174 _DIAGASSERT(m
!= NULL
);
175 _DIAGASSERT(s
!= NULL
);
177 ch
= (unsigned char)*s
++;
180 m0
= mnemonic_list_find(&m
->child
, ch
);
182 m0
= mnemonic_create(m
, ch
, (wchar_t)ch
);
185 TAILQ_INSERT_TAIL(&m
->child
, m0
, entry
);
188 for (m0
= NULL
; (ch
= (unsigned char)*s
) != '\0'; ++s
) {
189 m0
= mnemonic_list_find(&m
->child
, ch
);
191 m0
= mnemonic_create(m
, ch
, invalid
);
194 TAILQ_INSERT_TAIL(&m
->child
, m0
, entry
);
206 mnemonic_destroy(mnemonic_t
*m
)
210 _DIAGASSERT(m
!= NULL
);
212 TAILQ_FOREACH(m0
, &m
->child
, entry
)
213 mnemonic_destroy(m0
);
229 _VIQREncodingInfo ei
;
231 /* for future multi-locale facility */
234 _VIQRState s_mbrtowc
;
236 _VIQRState s_mbsrtowcs
;
237 _VIQRState s_wcrtomb
;
238 _VIQRState s_wcsrtombs
;
243 #define _CEI_TO_EI(_cei_) (&(_cei_)->ei)
244 #define _CEI_TO_STATE(_cei_, _func_) (_cei_)->states.s_##_func_
246 #define _FUNCNAME(m) _citrus_VIQR_##m
247 #define _ENCODING_INFO _VIQREncodingInfo
248 #define _CTYPE_INFO _VIQRCTypeInfo
249 #define _ENCODING_STATE _VIQRState
250 #define _ENCODING_MB_CUR_MAX(_ei_) (_ei_)->mb_cur_max
251 #define _ENCODING_IS_STATE_DEPENDENT 1
252 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
256 _citrus_VIQR_init_state(_VIQREncodingInfo
* __restrict ei
,
257 _VIQRState
* __restrict psenc
)
259 /* ei may be unused */
260 _DIAGASSERT(psenc
!= NULL
);
267 _citrus_VIQR_pack_state(_VIQREncodingInfo
* __restrict ei
,
268 void *__restrict pspriv
, const _VIQRState
* __restrict psenc
)
270 /* ei may be unused */
271 _DIAGASSERT(pspriv
!= NULL
);
272 _DIAGASSERT(psenc
!= NULL
);
274 memcpy(pspriv
, (const void *)psenc
, sizeof(*psenc
));
279 _citrus_VIQR_unpack_state(_VIQREncodingInfo
* __restrict ei
,
280 _VIQRState
* __restrict psenc
, const void * __restrict pspriv
)
282 /* ei may be unused */
283 _DIAGASSERT(psenc
!= NULL
);
284 _DIAGASSERT(pspriv
!= NULL
);
286 memcpy((void *)psenc
, pspriv
, sizeof(*psenc
));
290 _citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo
* __restrict ei
,
291 wchar_t * __restrict pwc
, const char ** __restrict s
, size_t n
,
292 _VIQRState
* __restrict psenc
, size_t * __restrict nresult
)
300 _DIAGASSERT(ei
!= NULL
);
301 /* pwc may be null */
302 _DIAGASSERT(s
!= NULL
);
303 _DIAGASSERT(psenc
!= NULL
);
304 _DIAGASSERT(nresult
!= NULL
);
307 _citrus_VIQR_init_state(ei
, psenc
);
308 *nresult
= (size_t)_ENCODING_IS_STATE_DEPENDENT
;
316 if (psenc
->chlen
== i
) {
319 *nresult
= (size_t)-2;
322 psenc
->ch
[psenc
->chlen
++] = *s0
++;
324 ch
= (unsigned char)psenc
->ch
[i
++];
333 m0
= mnemonic_list_find(&m
->child
, ch
);
338 while (m
!= ei
->mroot
) {
340 if (m
->value
!= ei
->invalid
)
344 if (ch
== ESCAPE
&& m
!= ei
->mroot
)
347 memmove(&psenc
->ch
[0], &psenc
->ch
[i
], psenc
->chlen
);
348 wc
= (m
== ei
->mroot
) ? (wchar_t)ch
: m
->value
;
351 *nresult
= (size_t)(wc
== 0 ? 0 : s0
- *s
);
358 _citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo
* __restrict ei
,
359 char * __restrict s
, size_t n
, wchar_t wc
,
360 _VIQRState
* __restrict psenc
, size_t * __restrict nresult
)
366 _DIAGASSERT(ei
!= NULL
);
367 _DIAGASSERT(s
!= NULL
);
368 _DIAGASSERT(psenc
!= NULL
);
369 _DIAGASSERT(nresult
!= NULL
);
371 switch (psenc
->chlen
) {
378 if ((uint32_t)wc
<= 0xFF) {
379 p
= mnemonic_rfc1456
[wc
& 0xFF];
384 ch
= (unsigned int)wc
;
386 if (psenc
->chlen
> 0) {
387 m
= mnemonic_list_find(&m
->child
, psenc
->ch
[0]);
390 psenc
->ch
[0] = ESCAPE
;
392 if (mnemonic_list_find(&m
->child
, ch
) == NULL
) {
396 psenc
->ch
[psenc
->chlen
++] = ch
;
398 p
= mnemonic_ext_find(wc
, &mnemonic_ext
[0], mnemonic_ext_size
);
400 *nresult
= (size_t)-1;
408 psenc
->ch
[psenc
->chlen
++] = *p
++;
412 memcpy(s
, psenc
->ch
, psenc
->chlen
);
413 *nresult
= psenc
->chlen
;
414 if (m
== ei
->mroot
) {
424 *nresult
= (size_t)-1;
430 _citrus_VIQR_put_state_reset(_VIQREncodingInfo
* __restrict ei
,
431 char * __restrict s
, size_t n
, _VIQRState
* __restrict psenc
,
432 size_t * __restrict nresult
)
434 /* ei may be unused */
435 _DIAGASSERT(s
!= NULL
);
436 _DIAGASSERT(psenc
!= NULL
);
437 _DIAGASSERT(nresult
!= NULL
);
439 switch (psenc
->chlen
) {
453 _citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo
* __restrict ei
,
454 _csid_t
* __restrict csid
, _index_t
* __restrict idx
, wchar_t wc
)
456 /* ei may be unused */
457 _DIAGASSERT(csid
!= NULL
);
458 _DIAGASSERT(idx
!= NULL
);
468 _citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo
* __restrict ei
,
469 wchar_t * __restrict pwc
, _csid_t csid
, _index_t idx
)
471 /* ei may be unused */
472 _DIAGASSERT(pwc
!= NULL
);
482 _citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo
*ei
)
484 _DIAGASSERT(ei
!= NULL
);
486 mnemonic_destroy(ei
->mroot
);
491 _citrus_VIQR_encoding_module_init(_VIQREncodingInfo
* __restrict ei
,
492 const void * __restrict var
, size_t lenvar
)
497 const mnemonic_def_t
*p
;
499 _DIAGASSERT(ei
!= NULL
);
500 /* var may be unused */
503 ei
->invalid
= (wchar_t)-1;
504 ei
->mroot
= mnemonic_create(NULL
, '\0', ei
->invalid
);
505 if (ei
->mroot
== NULL
)
507 for (i
= 0; i
< sizeof(mnemonic_rfc1456
) / sizeof(const char *); ++i
) {
508 s
= mnemonic_rfc1456
[i
];
512 _DIAGASSERT(n
<= MB_LEN_MAX
);
513 if (ei
->mb_cur_max
< n
)
515 errnum
= mnemonic_append_child(ei
->mroot
,
516 s
, (wchar_t)i
, ei
->invalid
);
518 _citrus_VIQR_encoding_module_uninit(ei
);
522 for (i
= 0; i
< mnemonic_ext_size
; ++i
) {
523 p
= &mnemonic_ext
[i
];
524 _DIAGASSERT(p
!= NULL
&& p
->name
!= NULL
);
526 _DIAGASSERT(n
<= MB_LEN_MAX
);
527 if (ei
->mb_cur_max
< n
)
529 errnum
= mnemonic_append_child(ei
->mroot
,
530 p
->name
, p
->value
, ei
->invalid
);
532 _citrus_VIQR_encoding_module_uninit(ei
);
542 _citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo
* __restrict ei
,
543 _VIQRState
* __restrict psenc
, int * __restrict rstate
)
545 /* ei may be unused */
546 _DIAGASSERT(psenc
!= NULL
);
547 _DIAGASSERT(rstate
!= NULL
);
549 *rstate
= (psenc
->chlen
== 0)
550 ? _STDENC_SDGEN_INITIAL
551 : _STDENC_SDGEN_INCOMPLETE_CHAR
;
556 /* ----------------------------------------------------------------------
557 * public interface for ctype
560 _CITRUS_CTYPE_DECLS(VIQR
);
561 _CITRUS_CTYPE_DEF_OPS(VIQR
);
563 #include "citrus_ctype_template.h"
565 /* ----------------------------------------------------------------------
566 * public interface for stdenc
569 _CITRUS_STDENC_DECLS(VIQR
);
570 _CITRUS_STDENC_DEF_OPS(VIQR
);
572 #include "citrus_stdenc_template.h"