xref: /freebsd/lib/libiconv_modules/VIQR/citrus_viqr.c (revision 641a6cfb86023499caafe26a4d821a0b885cf00b)
1 /* $FreeBSD$ */
2 /* $NetBSD: citrus_viqr.c,v 1.4 2008/06/14 16:01:08 tnozaki Exp $ */
3 
4 /*-
5  * Copyright (c)2006 Citrus Project,
6  * All rights reserved.
7  *
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.
16  *
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.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 
35 #include <assert.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stddef.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <wchar.h>
43 
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_bcs.h"
47 #include "citrus_module.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_viqr.h"
50 
51 #define ESCAPE	'\\'
52 
53 /*
54  * this table generated from RFC 1456.
55  */
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+~",
89 };
90 
91 typedef struct {
92 	const char	*name;
93 	wchar_t		 value;
94 } mnemonic_def_t;
95 
96 static const mnemonic_def_t mnemonic_ext[] = {
97 /* add extra mnemonic here (should be sorted by wchar_t order). */
98 };
99 static const size_t mnemonic_ext_size =
100 	sizeof(mnemonic_ext) / sizeof(mnemonic_def_t);
101 
102 static const char *
103 mnemonic_ext_find(wchar_t wc, const mnemonic_def_t *head, size_t n)
104 {
105 	const mnemonic_def_t *mid;
106 
107 	for (; n > 0; n >>= 1) {
108 		mid = head + (n >> 1);
109 		if (mid->value == wc)
110 			return (mid->name);
111 		else if (mid->value < wc) {
112 			head = mid + 1;
113 			--n;
114 		}
115 	}
116 	return (NULL);
117 }
118 
119 struct mnemonic_t;
120 typedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t;
121 typedef struct mnemonic_t {
122 	TAILQ_ENTRY(mnemonic_t)	 entry;
123 	struct mnemonic_t	*parent;
124 	mnemonic_list_t		 child;
125 	wchar_t			 value;
126 	int			 ascii;
127 } mnemonic_t;
128 
129 static mnemonic_t *
130 mnemonic_list_find(mnemonic_list_t *ml, int ch)
131 {
132 	mnemonic_t *m;
133 
134 	TAILQ_FOREACH(m, ml, entry) {
135 		if (m->ascii == ch)
136 			return (m);
137 	}
138 
139 	return (NULL);
140 }
141 
142 static mnemonic_t *
143 mnemonic_create(mnemonic_t *parent, int ascii, wchar_t value)
144 {
145 	mnemonic_t *m;
146 
147 	m = malloc(sizeof(*m));
148 	if (m != NULL) {
149 		m->parent = parent;
150 		m->ascii = ascii;
151 		m->value = value;
152 		TAILQ_INIT(&m->child);
153 	}
154 
155 	return (m);
156 }
157 
158 static int
159 mnemonic_append_child(mnemonic_t *m, const char *s,
160     wchar_t value, wchar_t invalid)
161 {
162 	mnemonic_t *m0;
163 	int ch;
164 
165 	ch = (unsigned char)*s++;
166 	if (ch == '\0')
167 		return (EINVAL);
168 	m0 = mnemonic_list_find(&m->child, ch);
169 	if (m0 == NULL) {
170 		m0 = mnemonic_create(m, ch, (wchar_t)ch);
171 		if (m0 == NULL)
172 			return (ENOMEM);
173 		TAILQ_INSERT_TAIL(&m->child, m0, entry);
174 	}
175 	m = m0;
176 	for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) {
177 		m0 = mnemonic_list_find(&m->child, ch);
178 		if (m0 == NULL) {
179 			m0 = mnemonic_create(m, ch, invalid);
180 			if (m0 == NULL)
181 				return (ENOMEM);
182 			TAILQ_INSERT_TAIL(&m->child, m0, entry);
183 		}
184 		m = m0;
185 	}
186 	if (m0 == NULL)
187 		return (EINVAL);
188 	m0->value = value;
189 
190 	return (0);
191 }
192 
193 static void
194 mnemonic_destroy(mnemonic_t *m)
195 {
196 	mnemonic_t *m0;
197 
198 	TAILQ_FOREACH(m0, &m->child, entry)
199 		mnemonic_destroy(m0);
200 	free(m);
201 }
202 
203 typedef struct {
204 	mnemonic_t	*mroot;
205 	wchar_t		 invalid;
206 	size_t		 mb_cur_max;
207 } _VIQREncodingInfo;
208 
209 typedef struct {
210 	int	 chlen;
211 	char	 ch[MB_LEN_MAX];
212 } _VIQRState;
213 
214 #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
215 #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
216 
217 #define _FUNCNAME(m)			_citrus_VIQR_##m
218 #define _ENCODING_INFO			_VIQREncodingInfo
219 #define _ENCODING_STATE			_VIQRState
220 #define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
221 #define _ENCODING_IS_STATE_DEPENDENT		1
222 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
223 
224 static __inline void
225 /*ARGSUSED*/
226 _citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei __unused,
227     _VIQRState * __restrict psenc)
228 {
229 
230 	psenc->chlen = 0;
231 }
232 
233 static __inline void
234 /*ARGSUSED*/
235 _citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei __unused,
236     void *__restrict pspriv, const _VIQRState * __restrict psenc)
237 {
238 
239 	memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
240 }
241 
242 static __inline void
243 /*ARGSUSED*/
244 _citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei __unused,
245     _VIQRState * __restrict psenc, const void * __restrict pspriv)
246 {
247 
248 	memcpy((void *)psenc, pspriv, sizeof(*psenc));
249 }
250 
251 static int
252 _citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei,
253     wchar_t * __restrict pwc, char ** __restrict s, size_t n,
254     _VIQRState * __restrict psenc, size_t * __restrict nresult)
255 {
256 	mnemonic_t *m, *m0;
257 	char *s0;
258 	wchar_t wc;
259 	ssize_t i;
260 	int ch, escape;
261 
262 	if (*s == NULL) {
263 		_citrus_VIQR_init_state(ei, psenc);
264 		*nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
265 		return (0);
266 	}
267 	s0 = *s;
268 
269 	i = 0;
270 	m = ei->mroot;
271 	for (escape = 0;;) {
272 		if (psenc->chlen == i) {
273 			if (n-- < 1) {
274 				*s = s0;
275 				*nresult = (size_t)-2;
276 				return (0);
277 			}
278 			psenc->ch[psenc->chlen++] = *s0++;
279 		}
280 		ch = (unsigned char)psenc->ch[i++];
281 		if (ch == ESCAPE) {
282 			if (m != ei->mroot)
283 				break;
284 			escape = 1;
285 			continue;
286 		}
287 		if (escape != 0)
288 			break;
289 		m0 = mnemonic_list_find(&m->child, ch);
290 		if (m0 == NULL)
291 			break;
292 		m = m0;
293 	}
294 	while (m != ei->mroot) {
295 		--i;
296 		if (m->value != ei->invalid)
297 			break;
298 		m = m->parent;
299 	}
300 	if (ch == ESCAPE && m != ei->mroot)
301 		++i;
302 	psenc->chlen -= i;
303 	memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen);
304 	wc = (m == ei->mroot) ? (wchar_t)ch : m->value;
305 	if (pwc != NULL)
306 		*pwc = wc;
307 	*nresult = (size_t)(wc == 0 ? 0 : s0 - *s);
308 	*s = s0;
309 
310 	return (0);
311 }
312 
313 static int
314 _citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei,
315     char * __restrict s, size_t n, wchar_t wc,
316     _VIQRState * __restrict psenc, size_t * __restrict nresult)
317 {
318 	mnemonic_t *m;
319 	const char *p;
320 	int ch = 0;
321 
322 	switch (psenc->chlen) {
323 	case 0: case 1:
324 		break;
325 	default:
326 		return (EINVAL);
327 	}
328 	m = NULL;
329 	if ((uint32_t)wc <= 0xFF) {
330 		p = mnemonic_rfc1456[wc & 0xFF];
331 		if (p != NULL)
332 			goto mnemonic_found;
333 		if (n-- < 1)
334 			goto e2big;
335 		ch = (unsigned int)wc;
336 		m = ei->mroot;
337 		if (psenc->chlen > 0) {
338 			m = mnemonic_list_find(&m->child, psenc->ch[0]);
339 			if (m == NULL)
340 				return (EINVAL);
341 			psenc->ch[0] = ESCAPE;
342 		}
343 		if (mnemonic_list_find(&m->child, ch) == NULL) {
344 			psenc->chlen = 0;
345 			m = NULL;
346 		}
347 		psenc->ch[psenc->chlen++] = ch;
348 	} else {
349 		p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size);
350 		if (p == NULL) {
351 			*nresult = (size_t)-1;
352 			return (EILSEQ);
353 		} else {
354 mnemonic_found:
355 			psenc->chlen = 0;
356 			while (*p != '\0') {
357 				if (n-- < 1)
358 					goto e2big;
359 				psenc->ch[psenc->chlen++] = *p++;
360 			}
361 		}
362 	}
363 	memcpy(s, psenc->ch, psenc->chlen);
364 	*nresult = psenc->chlen;
365 	if (m == ei->mroot) {
366 		psenc->ch[0] = ch;
367 		psenc->chlen = 1;
368 	} else
369 		psenc->chlen = 0;
370 
371 	return (0);
372 
373 e2big:
374 	*nresult = (size_t)-1;
375 	return (E2BIG);
376 }
377 
378 static int
379 /* ARGSUSED */
380 _citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei __unused,
381     char * __restrict s __unused, size_t n __unused,
382     _VIQRState * __restrict psenc, size_t * __restrict nresult)
383 {
384 
385 	switch (psenc->chlen) {
386 	case 0: case 1:
387 		break;
388 	default:
389 		return (EINVAL);
390 	}
391 	*nresult = 0;
392 	psenc->chlen = 0;
393 
394 	return (0);
395 }
396 
397 static __inline int
398 /*ARGSUSED*/
399 _citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei __unused,
400     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
401 {
402 
403 	*csid = 0;
404 	*idx = (_index_t)wc;
405 
406 	return (0);
407 }
408 
409 static __inline int
410 /*ARGSUSED*/
411 _citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei __unused,
412     wchar_t * __restrict pwc, _csid_t csid, _index_t idx)
413 {
414 
415 	if (csid != 0)
416 		return (EILSEQ);
417 	*pwc = (wchar_t)idx;
418 
419 	return (0);
420 }
421 
422 static void
423 _citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei)
424 {
425 
426 	mnemonic_destroy(ei->mroot);
427 }
428 
429 static int
430 /*ARGSUSED*/
431 _citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei,
432     const void * __restrict var __unused, size_t lenvar __unused)
433 {
434 	const mnemonic_def_t *p;
435 	const char *s;
436 	size_t i, n;
437 	int errnum;
438 
439 	ei->mb_cur_max = 1;
440 	ei->invalid = (wchar_t)-1;
441 	ei->mroot = mnemonic_create(NULL, '\0', ei->invalid);
442 	if (ei->mroot == NULL)
443 		return (ENOMEM);
444 	for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) {
445 		s = mnemonic_rfc1456[i];
446 		if (s == NULL)
447 			continue;
448 		n = strlen(s);
449 		if (ei->mb_cur_max < n)
450 			ei->mb_cur_max = n;
451 		errnum = mnemonic_append_child(ei->mroot,
452 		    s, (wchar_t)i, ei->invalid);
453 		if (errnum != 0) {
454 			_citrus_VIQR_encoding_module_uninit(ei);
455 			return (errnum);
456 		}
457 	}
458 	for (i = 0;; ++i) {
459 		p = &mnemonic_ext[i];
460 		n = strlen(p->name);
461 		if (ei->mb_cur_max < n)
462 			ei->mb_cur_max = n;
463 		errnum = mnemonic_append_child(ei->mroot,
464 		    p->name, p->value, ei->invalid);
465 		if (errnum != 0) {
466 			_citrus_VIQR_encoding_module_uninit(ei);
467 			return (errnum);
468 		}
469 	}
470 
471 	return (0);
472 }
473 
474 static __inline int
475 /*ARGSUSED*/
476 _citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei __unused,
477     _VIQRState * __restrict psenc, int * __restrict rstate)
478 {
479 
480 	*rstate = (psenc->chlen == 0) ?
481 	    _STDENC_SDGEN_INITIAL :
482 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
483 
484 	return (0);
485 }
486 
487 /* ----------------------------------------------------------------------
488  * public interface for stdenc
489  */
490 
491 _CITRUS_STDENC_DECLS(VIQR);
492 _CITRUS_STDENC_DEF_OPS(VIQR);
493 
494 #include "citrus_stdenc_template.h"
495