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