xref: /freebsd/lib/libiconv_modules/VIQR/citrus_viqr.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
19ca40936STijl Coosemans /* $NetBSD: citrus_viqr.c,v 1.5 2011/11/19 18:20:13 tnozaki Exp $ */
2ad30f8e7SGabor Kovesdan 
3ad30f8e7SGabor Kovesdan /*-
45e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause
55e53a4f9SPedro F. Giffuni  *
6ad30f8e7SGabor Kovesdan  * Copyright (c)2006 Citrus Project,
7ad30f8e7SGabor Kovesdan  * All rights reserved.
8ad30f8e7SGabor Kovesdan  *
9ad30f8e7SGabor Kovesdan  * Redistribution and use in source and binary forms, with or without
10ad30f8e7SGabor Kovesdan  * modification, are permitted provided that the following conditions
11ad30f8e7SGabor Kovesdan  * are met:
12ad30f8e7SGabor Kovesdan  * 1. Redistributions of source code must retain the above copyright
13ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer.
14ad30f8e7SGabor Kovesdan  * 2. Redistributions in binary form must reproduce the above copyright
15ad30f8e7SGabor Kovesdan  *    notice, this list of conditions and the following disclaimer in the
16ad30f8e7SGabor Kovesdan  *    documentation and/or other materials provided with the distribution.
17ad30f8e7SGabor Kovesdan  *
18ad30f8e7SGabor Kovesdan  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19ad30f8e7SGabor Kovesdan  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20ad30f8e7SGabor Kovesdan  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ad30f8e7SGabor Kovesdan  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22ad30f8e7SGabor Kovesdan  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23ad30f8e7SGabor Kovesdan  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24ad30f8e7SGabor Kovesdan  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25ad30f8e7SGabor Kovesdan  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26ad30f8e7SGabor Kovesdan  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27ad30f8e7SGabor Kovesdan  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28ad30f8e7SGabor Kovesdan  * SUCH DAMAGE.
29ad30f8e7SGabor Kovesdan  *
30ad30f8e7SGabor Kovesdan  */
31ad30f8e7SGabor Kovesdan 
32ad30f8e7SGabor Kovesdan #include <sys/cdefs.h>
33ad30f8e7SGabor Kovesdan #include <sys/queue.h>
34ad30f8e7SGabor Kovesdan #include <sys/types.h>
35ad30f8e7SGabor Kovesdan 
36ad30f8e7SGabor Kovesdan #include <assert.h>
37ad30f8e7SGabor Kovesdan #include <errno.h>
38ad30f8e7SGabor Kovesdan #include <limits.h>
39ad30f8e7SGabor Kovesdan #include <stddef.h>
40ad30f8e7SGabor Kovesdan #include <stdint.h>
41ad30f8e7SGabor Kovesdan #include <stdlib.h>
42ad30f8e7SGabor Kovesdan #include <string.h>
43ad30f8e7SGabor Kovesdan #include <wchar.h>
44ad30f8e7SGabor Kovesdan 
45ad30f8e7SGabor Kovesdan #include "citrus_namespace.h"
46ad30f8e7SGabor Kovesdan #include "citrus_types.h"
47ad30f8e7SGabor Kovesdan #include "citrus_bcs.h"
48ad30f8e7SGabor Kovesdan #include "citrus_module.h"
49ad30f8e7SGabor Kovesdan #include "citrus_stdenc.h"
50ad30f8e7SGabor Kovesdan #include "citrus_viqr.h"
51ad30f8e7SGabor Kovesdan 
52ad30f8e7SGabor Kovesdan #define ESCAPE	'\\'
53ad30f8e7SGabor Kovesdan 
54ad30f8e7SGabor Kovesdan /*
55ad30f8e7SGabor Kovesdan  * this table generated from RFC 1456.
56ad30f8e7SGabor Kovesdan  */
57ad30f8e7SGabor Kovesdan static const char *mnemonic_rfc1456[0x100] = {
58ad30f8e7SGabor Kovesdan   NULL , NULL , "A(?", NULL , NULL , "A(~", "A^~", NULL ,
59ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
60ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , "Y?" , NULL , NULL , NULL ,
61ad30f8e7SGabor Kovesdan   NULL , "Y~" , NULL , NULL , NULL , NULL , "Y." , NULL ,
62ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
63ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
64ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
65ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
66ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
67ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
68ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
69ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
70ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
71ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
72ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
73ad30f8e7SGabor Kovesdan   NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
74ad30f8e7SGabor Kovesdan   "A." , "A('", "A(`", "A(.", "A^'", "A^`", "A^?", "A^.",
75ad30f8e7SGabor Kovesdan   "E~" , "E." , "E^'", "E^`", "E^?", "E^~", "E^.", "O^'",
76ad30f8e7SGabor Kovesdan   "O^`", "O^?", "O^~", "O^.", "O+.", "O+'", "O+`", "O+?",
77ad30f8e7SGabor Kovesdan   "I." , "O?" , "O." , "I?" , "U?" , "U~" , "U." , "Y`" ,
78ad30f8e7SGabor Kovesdan   "O~" , "a('", "a(`", "a(.", "a^'", "a^`", "a^?", "a^.",
79ad30f8e7SGabor Kovesdan   "e~" , "e." , "e^'", "e^`", "e^?", "e^~", "e^.", "o^'",
80ad30f8e7SGabor Kovesdan   "o^`", "o^?", "o^~", "O+~", "O+" , "o^.", "o+`", "o+?",
81ad30f8e7SGabor Kovesdan   "i." , "U+.", "U+'", "U+`", "U+?", "o+" , "o+'", "U+" ,
82ad30f8e7SGabor Kovesdan   "A`" , "A'" , "A^" , "A~" , "A?" , "A(" , "a(?", "a(~",
83ad30f8e7SGabor Kovesdan   "E`" , "E'" , "E^" , "E?" , "I`" , "I'" , "I~" , "y`" ,
84ad30f8e7SGabor Kovesdan   "DD" , "u+'", "O`" , "O'" , "O^" , "a." , "y?" , "u+`",
85ad30f8e7SGabor Kovesdan   "u+?", "U`" , "U'" , "y~" , "y." , "Y'" , "o+~", "u+" ,
86ad30f8e7SGabor Kovesdan   "a`" , "a'" , "a^" , "a~" , "a?" , "a(" , "u+~", "a^~",
87ad30f8e7SGabor Kovesdan   "e`" , "e'" , "e^" , "e?" , "i`" , "i'" , "i~" , "i?" ,
88ad30f8e7SGabor Kovesdan   "dd" , "u+.", "o`" , "o'" , "o^" , "o~" , "o?" , "o." ,
89ad30f8e7SGabor Kovesdan   "u." , "u`" , "u'" , "u~" , "u?" , "y'" , "o+.", "U+~",
90ad30f8e7SGabor Kovesdan };
91ad30f8e7SGabor Kovesdan 
92ad30f8e7SGabor Kovesdan typedef struct {
93ad30f8e7SGabor Kovesdan 	const char	*name;
94ad30f8e7SGabor Kovesdan 	wchar_t		 value;
95ad30f8e7SGabor Kovesdan } mnemonic_def_t;
96ad30f8e7SGabor Kovesdan 
97ad30f8e7SGabor Kovesdan static const mnemonic_def_t mnemonic_ext[] = {
98ad30f8e7SGabor Kovesdan /* add extra mnemonic here (should be sorted by wchar_t order). */
99ad30f8e7SGabor Kovesdan };
100ad30f8e7SGabor Kovesdan static const size_t mnemonic_ext_size =
101ad30f8e7SGabor Kovesdan 	sizeof(mnemonic_ext) / sizeof(mnemonic_def_t);
102ad30f8e7SGabor Kovesdan 
103ad30f8e7SGabor Kovesdan static const char *
mnemonic_ext_find(wchar_t wc,const mnemonic_def_t * head,size_t n)104ad30f8e7SGabor Kovesdan mnemonic_ext_find(wchar_t wc, const mnemonic_def_t *head, size_t n)
105ad30f8e7SGabor Kovesdan {
106ad30f8e7SGabor Kovesdan 	const mnemonic_def_t *mid;
107ad30f8e7SGabor Kovesdan 
108ad30f8e7SGabor Kovesdan 	for (; n > 0; n >>= 1) {
109ad30f8e7SGabor Kovesdan 		mid = head + (n >> 1);
110ad30f8e7SGabor Kovesdan 		if (mid->value == wc)
111ad30f8e7SGabor Kovesdan 			return (mid->name);
112ad30f8e7SGabor Kovesdan 		else if (mid->value < wc) {
113ad30f8e7SGabor Kovesdan 			head = mid + 1;
114ad30f8e7SGabor Kovesdan 			--n;
115ad30f8e7SGabor Kovesdan 		}
116ad30f8e7SGabor Kovesdan 	}
117ad30f8e7SGabor Kovesdan 	return (NULL);
118ad30f8e7SGabor Kovesdan }
119ad30f8e7SGabor Kovesdan 
120ad30f8e7SGabor Kovesdan struct mnemonic_t;
121ad30f8e7SGabor Kovesdan typedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t;
122ad30f8e7SGabor Kovesdan typedef struct mnemonic_t {
123ad30f8e7SGabor Kovesdan 	TAILQ_ENTRY(mnemonic_t)	 entry;
124ad30f8e7SGabor Kovesdan 	struct mnemonic_t	*parent;
125ad30f8e7SGabor Kovesdan 	mnemonic_list_t		 child;
126ad30f8e7SGabor Kovesdan 	wchar_t			 value;
127ad30f8e7SGabor Kovesdan 	int			 ascii;
128ad30f8e7SGabor Kovesdan } mnemonic_t;
129ad30f8e7SGabor Kovesdan 
130ad30f8e7SGabor Kovesdan static mnemonic_t *
mnemonic_list_find(mnemonic_list_t * ml,int ch)131ad30f8e7SGabor Kovesdan mnemonic_list_find(mnemonic_list_t *ml, int ch)
132ad30f8e7SGabor Kovesdan {
133ad30f8e7SGabor Kovesdan 	mnemonic_t *m;
134ad30f8e7SGabor Kovesdan 
135ad30f8e7SGabor Kovesdan 	TAILQ_FOREACH(m, ml, entry) {
136ad30f8e7SGabor Kovesdan 		if (m->ascii == ch)
137ad30f8e7SGabor Kovesdan 			return (m);
138ad30f8e7SGabor Kovesdan 	}
139ad30f8e7SGabor Kovesdan 
140ad30f8e7SGabor Kovesdan 	return (NULL);
141ad30f8e7SGabor Kovesdan }
142ad30f8e7SGabor Kovesdan 
143ad30f8e7SGabor Kovesdan static mnemonic_t *
mnemonic_create(mnemonic_t * parent,int ascii,wchar_t value)144ad30f8e7SGabor Kovesdan mnemonic_create(mnemonic_t *parent, int ascii, wchar_t value)
145ad30f8e7SGabor Kovesdan {
146ad30f8e7SGabor Kovesdan 	mnemonic_t *m;
147ad30f8e7SGabor Kovesdan 
148ad30f8e7SGabor Kovesdan 	m = malloc(sizeof(*m));
149ad30f8e7SGabor Kovesdan 	if (m != NULL) {
150ad30f8e7SGabor Kovesdan 		m->parent = parent;
151ad30f8e7SGabor Kovesdan 		m->ascii = ascii;
152ad30f8e7SGabor Kovesdan 		m->value = value;
153ad30f8e7SGabor Kovesdan 		TAILQ_INIT(&m->child);
154ad30f8e7SGabor Kovesdan 	}
155ad30f8e7SGabor Kovesdan 
156ad30f8e7SGabor Kovesdan 	return (m);
157ad30f8e7SGabor Kovesdan }
158ad30f8e7SGabor Kovesdan 
159ad30f8e7SGabor Kovesdan static int
mnemonic_append_child(mnemonic_t * m,const char * s,wchar_t value,wchar_t invalid)160ad30f8e7SGabor Kovesdan mnemonic_append_child(mnemonic_t *m, const char *s,
161ad30f8e7SGabor Kovesdan     wchar_t value, wchar_t invalid)
162ad30f8e7SGabor Kovesdan {
163ad30f8e7SGabor Kovesdan 	mnemonic_t *m0;
164ad30f8e7SGabor Kovesdan 	int ch;
165ad30f8e7SGabor Kovesdan 
166ad30f8e7SGabor Kovesdan 	ch = (unsigned char)*s++;
167ad30f8e7SGabor Kovesdan 	if (ch == '\0')
168ad30f8e7SGabor Kovesdan 		return (EINVAL);
169ad30f8e7SGabor Kovesdan 	m0 = mnemonic_list_find(&m->child, ch);
170ad30f8e7SGabor Kovesdan 	if (m0 == NULL) {
171ad30f8e7SGabor Kovesdan 		m0 = mnemonic_create(m, ch, (wchar_t)ch);
172ad30f8e7SGabor Kovesdan 		if (m0 == NULL)
173ad30f8e7SGabor Kovesdan 			return (ENOMEM);
174ad30f8e7SGabor Kovesdan 		TAILQ_INSERT_TAIL(&m->child, m0, entry);
175ad30f8e7SGabor Kovesdan 	}
176ad30f8e7SGabor Kovesdan 	m = m0;
177ad30f8e7SGabor Kovesdan 	for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) {
178ad30f8e7SGabor Kovesdan 		m0 = mnemonic_list_find(&m->child, ch);
179ad30f8e7SGabor Kovesdan 		if (m0 == NULL) {
180ad30f8e7SGabor Kovesdan 			m0 = mnemonic_create(m, ch, invalid);
181ad30f8e7SGabor Kovesdan 			if (m0 == NULL)
182ad30f8e7SGabor Kovesdan 				return (ENOMEM);
183ad30f8e7SGabor Kovesdan 			TAILQ_INSERT_TAIL(&m->child, m0, entry);
184ad30f8e7SGabor Kovesdan 		}
185ad30f8e7SGabor Kovesdan 		m = m0;
186ad30f8e7SGabor Kovesdan 	}
187ad30f8e7SGabor Kovesdan 	if (m0 == NULL)
188ad30f8e7SGabor Kovesdan 		return (EINVAL);
189ad30f8e7SGabor Kovesdan 	m0->value = value;
190ad30f8e7SGabor Kovesdan 
191ad30f8e7SGabor Kovesdan 	return (0);
192ad30f8e7SGabor Kovesdan }
193ad30f8e7SGabor Kovesdan 
194ad30f8e7SGabor Kovesdan static void
mnemonic_destroy(mnemonic_t * m)195ad30f8e7SGabor Kovesdan mnemonic_destroy(mnemonic_t *m)
196ad30f8e7SGabor Kovesdan {
197*8f27c9d1SJohn Baldwin 	mnemonic_t *m0, *n;
198ad30f8e7SGabor Kovesdan 
199*8f27c9d1SJohn Baldwin 	TAILQ_FOREACH_SAFE(m0, &m->child, entry, n)
200ad30f8e7SGabor Kovesdan 		mnemonic_destroy(m0);
201ad30f8e7SGabor Kovesdan 	free(m);
202ad30f8e7SGabor Kovesdan }
203ad30f8e7SGabor Kovesdan 
204ad30f8e7SGabor Kovesdan typedef struct {
205ad30f8e7SGabor Kovesdan 	mnemonic_t	*mroot;
206ad30f8e7SGabor Kovesdan 	wchar_t		 invalid;
207ad30f8e7SGabor Kovesdan 	size_t		 mb_cur_max;
208ad30f8e7SGabor Kovesdan } _VIQREncodingInfo;
209ad30f8e7SGabor Kovesdan 
210ad30f8e7SGabor Kovesdan typedef struct {
211ad30f8e7SGabor Kovesdan 	int	 chlen;
212ad30f8e7SGabor Kovesdan 	char	 ch[MB_LEN_MAX];
213ad30f8e7SGabor Kovesdan } _VIQRState;
214ad30f8e7SGabor Kovesdan 
215ad30f8e7SGabor Kovesdan #define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
216ad30f8e7SGabor Kovesdan #define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
217ad30f8e7SGabor Kovesdan 
218ad30f8e7SGabor Kovesdan #define _FUNCNAME(m)			_citrus_VIQR_##m
219ad30f8e7SGabor Kovesdan #define _ENCODING_INFO			_VIQREncodingInfo
220ad30f8e7SGabor Kovesdan #define _ENCODING_STATE			_VIQRState
221ad30f8e7SGabor Kovesdan #define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
222ad30f8e7SGabor Kovesdan #define _ENCODING_IS_STATE_DEPENDENT		1
223ad30f8e7SGabor Kovesdan #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
224ad30f8e7SGabor Kovesdan 
225ad30f8e7SGabor Kovesdan static __inline void
226ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei __unused,_VIQRState * __restrict psenc)227ad30f8e7SGabor Kovesdan _citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei __unused,
228ad30f8e7SGabor Kovesdan     _VIQRState * __restrict psenc)
229ad30f8e7SGabor Kovesdan {
230ad30f8e7SGabor Kovesdan 
231ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
232ad30f8e7SGabor Kovesdan }
233ad30f8e7SGabor Kovesdan 
234b61949ddSDimitry Andric #if 0
235ad30f8e7SGabor Kovesdan static __inline void
236ad30f8e7SGabor Kovesdan /*ARGSUSED*/
237ad30f8e7SGabor Kovesdan _citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei __unused,
238ad30f8e7SGabor Kovesdan     void *__restrict pspriv, const _VIQRState * __restrict psenc)
239ad30f8e7SGabor Kovesdan {
240ad30f8e7SGabor Kovesdan 
241ad30f8e7SGabor Kovesdan 	memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
242ad30f8e7SGabor Kovesdan }
243ad30f8e7SGabor Kovesdan 
244ad30f8e7SGabor Kovesdan static __inline void
245ad30f8e7SGabor Kovesdan /*ARGSUSED*/
246ad30f8e7SGabor Kovesdan _citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei __unused,
247ad30f8e7SGabor Kovesdan     _VIQRState * __restrict psenc, const void * __restrict pspriv)
248ad30f8e7SGabor Kovesdan {
249ad30f8e7SGabor Kovesdan 
250ad30f8e7SGabor Kovesdan 	memcpy((void *)psenc, pspriv, sizeof(*psenc));
251ad30f8e7SGabor Kovesdan }
252b61949ddSDimitry Andric #endif
253ad30f8e7SGabor Kovesdan 
254ad30f8e7SGabor Kovesdan static int
_citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei,wchar_t * __restrict pwc,char ** __restrict s,size_t n,_VIQRState * __restrict psenc,size_t * __restrict nresult)255ad30f8e7SGabor Kovesdan _citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei,
2561243a98eSTijl Coosemans     wchar_t * __restrict pwc, char ** __restrict s, size_t n,
257ad30f8e7SGabor Kovesdan     _VIQRState * __restrict psenc, size_t * __restrict nresult)
258ad30f8e7SGabor Kovesdan {
259ad30f8e7SGabor Kovesdan 	mnemonic_t *m, *m0;
2601243a98eSTijl Coosemans 	char *s0;
261ad30f8e7SGabor Kovesdan 	wchar_t wc;
262ad30f8e7SGabor Kovesdan 	ssize_t i;
263ad30f8e7SGabor Kovesdan 	int ch, escape;
264ad30f8e7SGabor Kovesdan 
265ad30f8e7SGabor Kovesdan 	if (*s == NULL) {
266ad30f8e7SGabor Kovesdan 		_citrus_VIQR_init_state(ei, psenc);
267ad30f8e7SGabor Kovesdan 		*nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
268ad30f8e7SGabor Kovesdan 		return (0);
269ad30f8e7SGabor Kovesdan 	}
270ad30f8e7SGabor Kovesdan 	s0 = *s;
271ad30f8e7SGabor Kovesdan 
272ad30f8e7SGabor Kovesdan 	i = 0;
273ad30f8e7SGabor Kovesdan 	m = ei->mroot;
274ad30f8e7SGabor Kovesdan 	for (escape = 0;;) {
275ad30f8e7SGabor Kovesdan 		if (psenc->chlen == i) {
276ad30f8e7SGabor Kovesdan 			if (n-- < 1) {
277ad30f8e7SGabor Kovesdan 				*s = s0;
278ad30f8e7SGabor Kovesdan 				*nresult = (size_t)-2;
279ad30f8e7SGabor Kovesdan 				return (0);
280ad30f8e7SGabor Kovesdan 			}
281ad30f8e7SGabor Kovesdan 			psenc->ch[psenc->chlen++] = *s0++;
282ad30f8e7SGabor Kovesdan 		}
283ad30f8e7SGabor Kovesdan 		ch = (unsigned char)psenc->ch[i++];
284ad30f8e7SGabor Kovesdan 		if (ch == ESCAPE) {
285ad30f8e7SGabor Kovesdan 			if (m != ei->mroot)
286ad30f8e7SGabor Kovesdan 				break;
287ad30f8e7SGabor Kovesdan 			escape = 1;
288ad30f8e7SGabor Kovesdan 			continue;
289ad30f8e7SGabor Kovesdan 		}
290ad30f8e7SGabor Kovesdan 		if (escape != 0)
291ad30f8e7SGabor Kovesdan 			break;
292ad30f8e7SGabor Kovesdan 		m0 = mnemonic_list_find(&m->child, ch);
293ad30f8e7SGabor Kovesdan 		if (m0 == NULL)
294ad30f8e7SGabor Kovesdan 			break;
295ad30f8e7SGabor Kovesdan 		m = m0;
296ad30f8e7SGabor Kovesdan 	}
297ad30f8e7SGabor Kovesdan 	while (m != ei->mroot) {
298ad30f8e7SGabor Kovesdan 		--i;
299ad30f8e7SGabor Kovesdan 		if (m->value != ei->invalid)
300ad30f8e7SGabor Kovesdan 			break;
301ad30f8e7SGabor Kovesdan 		m = m->parent;
302ad30f8e7SGabor Kovesdan 	}
303ad30f8e7SGabor Kovesdan 	if (ch == ESCAPE && m != ei->mroot)
304ad30f8e7SGabor Kovesdan 		++i;
305ad30f8e7SGabor Kovesdan 	psenc->chlen -= i;
306ad30f8e7SGabor Kovesdan 	memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen);
307ad30f8e7SGabor Kovesdan 	wc = (m == ei->mroot) ? (wchar_t)ch : m->value;
308ad30f8e7SGabor Kovesdan 	if (pwc != NULL)
309ad30f8e7SGabor Kovesdan 		*pwc = wc;
310ad30f8e7SGabor Kovesdan 	*nresult = (size_t)(wc == 0 ? 0 : s0 - *s);
311ad30f8e7SGabor Kovesdan 	*s = s0;
312ad30f8e7SGabor Kovesdan 
313ad30f8e7SGabor Kovesdan 	return (0);
314ad30f8e7SGabor Kovesdan }
315ad30f8e7SGabor Kovesdan 
316ad30f8e7SGabor Kovesdan static int
_citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei,char * __restrict s,size_t n,wchar_t wc,_VIQRState * __restrict psenc,size_t * __restrict nresult)317ad30f8e7SGabor Kovesdan _citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei,
318ad30f8e7SGabor Kovesdan     char * __restrict s, size_t n, wchar_t wc,
319ad30f8e7SGabor Kovesdan     _VIQRState * __restrict psenc, size_t * __restrict nresult)
320ad30f8e7SGabor Kovesdan {
321ad30f8e7SGabor Kovesdan 	mnemonic_t *m;
322ad30f8e7SGabor Kovesdan 	const char *p;
323ad30f8e7SGabor Kovesdan 	int ch = 0;
324ad30f8e7SGabor Kovesdan 
325ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
326ad30f8e7SGabor Kovesdan 	case 0: case 1:
327ad30f8e7SGabor Kovesdan 		break;
328ad30f8e7SGabor Kovesdan 	default:
329ad30f8e7SGabor Kovesdan 		return (EINVAL);
330ad30f8e7SGabor Kovesdan 	}
331ad30f8e7SGabor Kovesdan 	m = NULL;
332ad30f8e7SGabor Kovesdan 	if ((uint32_t)wc <= 0xFF) {
333ad30f8e7SGabor Kovesdan 		p = mnemonic_rfc1456[wc & 0xFF];
334ad30f8e7SGabor Kovesdan 		if (p != NULL)
335ad30f8e7SGabor Kovesdan 			goto mnemonic_found;
336ad30f8e7SGabor Kovesdan 		if (n-- < 1)
337ad30f8e7SGabor Kovesdan 			goto e2big;
338ad30f8e7SGabor Kovesdan 		ch = (unsigned int)wc;
339ad30f8e7SGabor Kovesdan 		m = ei->mroot;
340ad30f8e7SGabor Kovesdan 		if (psenc->chlen > 0) {
341ad30f8e7SGabor Kovesdan 			m = mnemonic_list_find(&m->child, psenc->ch[0]);
342ad30f8e7SGabor Kovesdan 			if (m == NULL)
343ad30f8e7SGabor Kovesdan 				return (EINVAL);
344ad30f8e7SGabor Kovesdan 			psenc->ch[0] = ESCAPE;
345ad30f8e7SGabor Kovesdan 		}
346ad30f8e7SGabor Kovesdan 		if (mnemonic_list_find(&m->child, ch) == NULL) {
347ad30f8e7SGabor Kovesdan 			psenc->chlen = 0;
348ad30f8e7SGabor Kovesdan 			m = NULL;
349ad30f8e7SGabor Kovesdan 		}
350ad30f8e7SGabor Kovesdan 		psenc->ch[psenc->chlen++] = ch;
351ad30f8e7SGabor Kovesdan 	} else {
352ad30f8e7SGabor Kovesdan 		p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size);
353ad30f8e7SGabor Kovesdan 		if (p == NULL) {
354ad30f8e7SGabor Kovesdan 			*nresult = (size_t)-1;
355ad30f8e7SGabor Kovesdan 			return (EILSEQ);
356ad30f8e7SGabor Kovesdan 		} else {
357ad30f8e7SGabor Kovesdan mnemonic_found:
358ad30f8e7SGabor Kovesdan 			psenc->chlen = 0;
359ad30f8e7SGabor Kovesdan 			while (*p != '\0') {
360ad30f8e7SGabor Kovesdan 				if (n-- < 1)
361ad30f8e7SGabor Kovesdan 					goto e2big;
362ad30f8e7SGabor Kovesdan 				psenc->ch[psenc->chlen++] = *p++;
363ad30f8e7SGabor Kovesdan 			}
364ad30f8e7SGabor Kovesdan 		}
365ad30f8e7SGabor Kovesdan 	}
366ad30f8e7SGabor Kovesdan 	memcpy(s, psenc->ch, psenc->chlen);
367ad30f8e7SGabor Kovesdan 	*nresult = psenc->chlen;
368ad30f8e7SGabor Kovesdan 	if (m == ei->mroot) {
369ad30f8e7SGabor Kovesdan 		psenc->ch[0] = ch;
370ad30f8e7SGabor Kovesdan 		psenc->chlen = 1;
371ad30f8e7SGabor Kovesdan 	} else
372ad30f8e7SGabor Kovesdan 		psenc->chlen = 0;
373ad30f8e7SGabor Kovesdan 
374ad30f8e7SGabor Kovesdan 	return (0);
375ad30f8e7SGabor Kovesdan 
376ad30f8e7SGabor Kovesdan e2big:
377ad30f8e7SGabor Kovesdan 	*nresult = (size_t)-1;
378ad30f8e7SGabor Kovesdan 	return (E2BIG);
379ad30f8e7SGabor Kovesdan }
380ad30f8e7SGabor Kovesdan 
381ad30f8e7SGabor Kovesdan static int
382ad30f8e7SGabor Kovesdan /* ARGSUSED */
_citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei __unused,char * __restrict s __unused,size_t n __unused,_VIQRState * __restrict psenc,size_t * __restrict nresult)383ad30f8e7SGabor Kovesdan _citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei __unused,
384ad30f8e7SGabor Kovesdan     char * __restrict s __unused, size_t n __unused,
385ad30f8e7SGabor Kovesdan     _VIQRState * __restrict psenc, size_t * __restrict nresult)
386ad30f8e7SGabor Kovesdan {
387ad30f8e7SGabor Kovesdan 
388ad30f8e7SGabor Kovesdan 	switch (psenc->chlen) {
389ad30f8e7SGabor Kovesdan 	case 0: case 1:
390ad30f8e7SGabor Kovesdan 		break;
391ad30f8e7SGabor Kovesdan 	default:
392ad30f8e7SGabor Kovesdan 		return (EINVAL);
393ad30f8e7SGabor Kovesdan 	}
394ad30f8e7SGabor Kovesdan 	*nresult = 0;
395ad30f8e7SGabor Kovesdan 	psenc->chlen = 0;
396ad30f8e7SGabor Kovesdan 
397ad30f8e7SGabor Kovesdan 	return (0);
398ad30f8e7SGabor Kovesdan }
399ad30f8e7SGabor Kovesdan 
400ad30f8e7SGabor Kovesdan static __inline int
401ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei __unused,_csid_t * __restrict csid,_index_t * __restrict idx,wchar_t wc)402ad30f8e7SGabor Kovesdan _citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei __unused,
403ad30f8e7SGabor Kovesdan     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
404ad30f8e7SGabor Kovesdan {
405ad30f8e7SGabor Kovesdan 
406ad30f8e7SGabor Kovesdan 	*csid = 0;
407ad30f8e7SGabor Kovesdan 	*idx = (_index_t)wc;
408ad30f8e7SGabor Kovesdan 
409ad30f8e7SGabor Kovesdan 	return (0);
410ad30f8e7SGabor Kovesdan }
411ad30f8e7SGabor Kovesdan 
412ad30f8e7SGabor Kovesdan static __inline int
413ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei __unused,wchar_t * __restrict pwc,_csid_t csid,_index_t idx)414ad30f8e7SGabor Kovesdan _citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei __unused,
415ad30f8e7SGabor Kovesdan     wchar_t * __restrict pwc, _csid_t csid, _index_t idx)
416ad30f8e7SGabor Kovesdan {
417ad30f8e7SGabor Kovesdan 
418ad30f8e7SGabor Kovesdan 	if (csid != 0)
419ad30f8e7SGabor Kovesdan 		return (EILSEQ);
420ad30f8e7SGabor Kovesdan 	*pwc = (wchar_t)idx;
421ad30f8e7SGabor Kovesdan 
422ad30f8e7SGabor Kovesdan 	return (0);
423ad30f8e7SGabor Kovesdan }
424ad30f8e7SGabor Kovesdan 
425ad30f8e7SGabor Kovesdan static void
_citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo * ei)426ad30f8e7SGabor Kovesdan _citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei)
427ad30f8e7SGabor Kovesdan {
428ad30f8e7SGabor Kovesdan 
429ad30f8e7SGabor Kovesdan 	mnemonic_destroy(ei->mroot);
430ad30f8e7SGabor Kovesdan }
431ad30f8e7SGabor Kovesdan 
432ad30f8e7SGabor Kovesdan static int
433ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei,const void * __restrict var __unused,size_t lenvar __unused)434ad30f8e7SGabor Kovesdan _citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei,
435ad30f8e7SGabor Kovesdan     const void * __restrict var __unused, size_t lenvar __unused)
436ad30f8e7SGabor Kovesdan {
437ad30f8e7SGabor Kovesdan 	const char *s;
438ad30f8e7SGabor Kovesdan 	size_t i, n;
439ad30f8e7SGabor Kovesdan 	int errnum;
440ad30f8e7SGabor Kovesdan 
441ad30f8e7SGabor Kovesdan 	ei->mb_cur_max = 1;
442ad30f8e7SGabor Kovesdan 	ei->invalid = (wchar_t)-1;
443ad30f8e7SGabor Kovesdan 	ei->mroot = mnemonic_create(NULL, '\0', ei->invalid);
444ad30f8e7SGabor Kovesdan 	if (ei->mroot == NULL)
445ad30f8e7SGabor Kovesdan 		return (ENOMEM);
446ad30f8e7SGabor Kovesdan 	for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) {
447ad30f8e7SGabor Kovesdan 		s = mnemonic_rfc1456[i];
448ad30f8e7SGabor Kovesdan 		if (s == NULL)
449ad30f8e7SGabor Kovesdan 			continue;
450ad30f8e7SGabor Kovesdan 		n = strlen(s);
451ad30f8e7SGabor Kovesdan 		if (ei->mb_cur_max < n)
452ad30f8e7SGabor Kovesdan 			ei->mb_cur_max = n;
453ad30f8e7SGabor Kovesdan 		errnum = mnemonic_append_child(ei->mroot,
454ad30f8e7SGabor Kovesdan 		    s, (wchar_t)i, ei->invalid);
455ad30f8e7SGabor Kovesdan 		if (errnum != 0) {
456ad30f8e7SGabor Kovesdan 			_citrus_VIQR_encoding_module_uninit(ei);
457ad30f8e7SGabor Kovesdan 			return (errnum);
458ad30f8e7SGabor Kovesdan 		}
459ad30f8e7SGabor Kovesdan 	}
4604b6e82c5STijl Coosemans 	/* a + 1 < b + 1 here to silence gcc warning about unsigned < 0. */
4614b6e82c5STijl Coosemans 	for (i = 0; i + 1 < mnemonic_ext_size + 1; ++i) {
462b37cb981SXin LI 		const mnemonic_def_t *p;
463b37cb981SXin LI 
464ad30f8e7SGabor Kovesdan 		p = &mnemonic_ext[i];
465ad30f8e7SGabor Kovesdan 		n = strlen(p->name);
466ad30f8e7SGabor Kovesdan 		if (ei->mb_cur_max < n)
467ad30f8e7SGabor Kovesdan 			ei->mb_cur_max = n;
468ad30f8e7SGabor Kovesdan 		errnum = mnemonic_append_child(ei->mroot,
469ad30f8e7SGabor Kovesdan 		    p->name, p->value, ei->invalid);
470ad30f8e7SGabor Kovesdan 		if (errnum != 0) {
471ad30f8e7SGabor Kovesdan 			_citrus_VIQR_encoding_module_uninit(ei);
472ad30f8e7SGabor Kovesdan 			return (errnum);
473ad30f8e7SGabor Kovesdan 		}
474ad30f8e7SGabor Kovesdan 	}
475ad30f8e7SGabor Kovesdan 
476ad30f8e7SGabor Kovesdan 	return (0);
477ad30f8e7SGabor Kovesdan }
478ad30f8e7SGabor Kovesdan 
479ad30f8e7SGabor Kovesdan static __inline int
480ad30f8e7SGabor Kovesdan /*ARGSUSED*/
_citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei __unused,_VIQRState * __restrict psenc,int * __restrict rstate)481ad30f8e7SGabor Kovesdan _citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei __unused,
482ad30f8e7SGabor Kovesdan     _VIQRState * __restrict psenc, int * __restrict rstate)
483ad30f8e7SGabor Kovesdan {
484ad30f8e7SGabor Kovesdan 
485ad30f8e7SGabor Kovesdan 	*rstate = (psenc->chlen == 0) ?
486ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INITIAL :
487ad30f8e7SGabor Kovesdan 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
488ad30f8e7SGabor Kovesdan 
489ad30f8e7SGabor Kovesdan 	return (0);
490ad30f8e7SGabor Kovesdan }
491ad30f8e7SGabor Kovesdan 
492ad30f8e7SGabor Kovesdan /* ----------------------------------------------------------------------
493ad30f8e7SGabor Kovesdan  * public interface for stdenc
494ad30f8e7SGabor Kovesdan  */
495ad30f8e7SGabor Kovesdan 
496ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DECLS(VIQR);
497ad30f8e7SGabor Kovesdan _CITRUS_STDENC_DEF_OPS(VIQR);
498ad30f8e7SGabor Kovesdan 
499ad30f8e7SGabor Kovesdan #include "citrus_stdenc_template.h"
500