1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/* $NetBSD: citrus_viqr.c,v 1.4 2008/06/14 16:01:08 tnozaki Exp $ */
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c)2006 Citrus Project,
6219019Sgabor * All rights reserved.
7219019Sgabor *
8219019Sgabor * Redistribution and use in source and binary forms, with or without
9219019Sgabor * modification, are permitted provided that the following conditions
10219019Sgabor * are met:
11219019Sgabor * 1. Redistributions of source code must retain the above copyright
12219019Sgabor *    notice, this list of conditions and the following disclaimer.
13219019Sgabor * 2. Redistributions in binary form must reproduce the above copyright
14219019Sgabor *    notice, this list of conditions and the following disclaimer in the
15219019Sgabor *    documentation and/or other materials provided with the distribution.
16219019Sgabor *
17219019Sgabor * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18219019Sgabor * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19219019Sgabor * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20219019Sgabor * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21219019Sgabor * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22219019Sgabor * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23219019Sgabor * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24219019Sgabor * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25219019Sgabor * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26219019Sgabor * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27219019Sgabor * SUCH DAMAGE.
28219019Sgabor *
29219019Sgabor */
30219019Sgabor
31219019Sgabor#include <sys/cdefs.h>
32219019Sgabor#include <sys/queue.h>
33219019Sgabor#include <sys/types.h>
34219019Sgabor
35219019Sgabor#include <assert.h>
36219019Sgabor#include <errno.h>
37219019Sgabor#include <limits.h>
38219019Sgabor#include <stddef.h>
39219019Sgabor#include <stdint.h>
40219019Sgabor#include <stdlib.h>
41219019Sgabor#include <string.h>
42219019Sgabor#include <wchar.h>
43219019Sgabor
44219019Sgabor#include "citrus_namespace.h"
45219019Sgabor#include "citrus_types.h"
46219019Sgabor#include "citrus_bcs.h"
47219019Sgabor#include "citrus_module.h"
48219019Sgabor#include "citrus_stdenc.h"
49219019Sgabor#include "citrus_viqr.h"
50219019Sgabor
51219019Sgabor#define ESCAPE	'\\'
52219019Sgabor
53219019Sgabor/*
54219019Sgabor * this table generated from RFC 1456.
55219019Sgabor */
56219019Sgaborstatic const char *mnemonic_rfc1456[0x100] = {
57219019Sgabor  NULL , NULL , "A(?", NULL , NULL , "A(~", "A^~", NULL ,
58219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
59219019Sgabor  NULL , NULL , NULL , NULL , "Y?" , NULL , NULL , NULL ,
60219019Sgabor  NULL , "Y~" , NULL , NULL , NULL , NULL , "Y." , NULL ,
61219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
62219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
63219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
64219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
65219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
66219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
67219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
68219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
69219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
70219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
71219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
72219019Sgabor  NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ,
73219019Sgabor  "A." , "A('", "A(`", "A(.", "A^'", "A^`", "A^?", "A^.",
74219019Sgabor  "E~" , "E." , "E^'", "E^`", "E^?", "E^~", "E^.", "O^'",
75219019Sgabor  "O^`", "O^?", "O^~", "O^.", "O+.", "O+'", "O+`", "O+?",
76219019Sgabor  "I." , "O?" , "O." , "I?" , "U?" , "U~" , "U." , "Y`" ,
77219019Sgabor  "O~" , "a('", "a(`", "a(.", "a^'", "a^`", "a^?", "a^.",
78219019Sgabor  "e~" , "e." , "e^'", "e^`", "e^?", "e^~", "e^.", "o^'",
79219019Sgabor  "o^`", "o^?", "o^~", "O+~", "O+" , "o^.", "o+`", "o+?",
80219019Sgabor  "i." , "U+.", "U+'", "U+`", "U+?", "o+" , "o+'", "U+" ,
81219019Sgabor  "A`" , "A'" , "A^" , "A~" , "A?" , "A(" , "a(?", "a(~",
82219019Sgabor  "E`" , "E'" , "E^" , "E?" , "I`" , "I'" , "I~" , "y`" ,
83219019Sgabor  "DD" , "u+'", "O`" , "O'" , "O^" , "a." , "y?" , "u+`",
84219019Sgabor  "u+?", "U`" , "U'" , "y~" , "y." , "Y'" , "o+~", "u+" ,
85219019Sgabor  "a`" , "a'" , "a^" , "a~" , "a?" , "a(" , "u+~", "a^~",
86219019Sgabor  "e`" , "e'" , "e^" , "e?" , "i`" , "i'" , "i~" , "i?" ,
87219019Sgabor  "dd" , "u+.", "o`" , "o'" , "o^" , "o~" , "o?" , "o." ,
88219019Sgabor  "u." , "u`" , "u'" , "u~" , "u?" , "y'" , "o+.", "U+~",
89219019Sgabor};
90219019Sgabor
91219019Sgabortypedef struct {
92219019Sgabor	const char	*name;
93219019Sgabor	wchar_t		 value;
94219019Sgabor} mnemonic_def_t;
95219019Sgabor
96219019Sgaborstatic const mnemonic_def_t mnemonic_ext[] = {
97219019Sgabor/* add extra mnemonic here (should be sorted by wchar_t order). */
98219019Sgabor};
99219019Sgaborstatic const size_t mnemonic_ext_size =
100219019Sgabor	sizeof(mnemonic_ext) / sizeof(mnemonic_def_t);
101219019Sgabor
102219019Sgaborstatic const char *
103219019Sgabormnemonic_ext_find(wchar_t wc, const mnemonic_def_t *head, size_t n)
104219019Sgabor{
105219019Sgabor	const mnemonic_def_t *mid;
106219019Sgabor
107219019Sgabor	for (; n > 0; n >>= 1) {
108219019Sgabor		mid = head + (n >> 1);
109219019Sgabor		if (mid->value == wc)
110219019Sgabor			return (mid->name);
111219019Sgabor		else if (mid->value < wc) {
112219019Sgabor			head = mid + 1;
113219019Sgabor			--n;
114219019Sgabor		}
115219019Sgabor	}
116219019Sgabor	return (NULL);
117219019Sgabor}
118219019Sgabor
119219019Sgaborstruct mnemonic_t;
120219019Sgabortypedef TAILQ_HEAD(mnemonic_list_t, mnemonic_t) mnemonic_list_t;
121219019Sgabortypedef struct mnemonic_t {
122219019Sgabor	TAILQ_ENTRY(mnemonic_t)	 entry;
123219019Sgabor	struct mnemonic_t	*parent;
124219019Sgabor	mnemonic_list_t		 child;
125219019Sgabor	wchar_t			 value;
126219019Sgabor	int			 ascii;
127219019Sgabor} mnemonic_t;
128219019Sgabor
129219019Sgaborstatic mnemonic_t *
130219019Sgabormnemonic_list_find(mnemonic_list_t *ml, int ch)
131219019Sgabor{
132219019Sgabor	mnemonic_t *m;
133219019Sgabor
134219019Sgabor	TAILQ_FOREACH(m, ml, entry) {
135219019Sgabor		if (m->ascii == ch)
136219019Sgabor			return (m);
137219019Sgabor	}
138219019Sgabor
139219019Sgabor	return (NULL);
140219019Sgabor}
141219019Sgabor
142219019Sgaborstatic mnemonic_t *
143219019Sgabormnemonic_create(mnemonic_t *parent, int ascii, wchar_t value)
144219019Sgabor{
145219019Sgabor	mnemonic_t *m;
146219019Sgabor
147219019Sgabor	m = malloc(sizeof(*m));
148219019Sgabor	if (m != NULL) {
149219019Sgabor		m->parent = parent;
150219019Sgabor		m->ascii = ascii;
151219019Sgabor		m->value = value;
152219019Sgabor		TAILQ_INIT(&m->child);
153219019Sgabor	}
154219019Sgabor
155219019Sgabor	return (m);
156219019Sgabor}
157219019Sgabor
158219019Sgaborstatic int
159219019Sgabormnemonic_append_child(mnemonic_t *m, const char *s,
160219019Sgabor    wchar_t value, wchar_t invalid)
161219019Sgabor{
162219019Sgabor	mnemonic_t *m0;
163219019Sgabor	int ch;
164219019Sgabor
165219019Sgabor	ch = (unsigned char)*s++;
166219019Sgabor	if (ch == '\0')
167219019Sgabor		return (EINVAL);
168219019Sgabor	m0 = mnemonic_list_find(&m->child, ch);
169219019Sgabor	if (m0 == NULL) {
170219019Sgabor		m0 = mnemonic_create(m, ch, (wchar_t)ch);
171219019Sgabor		if (m0 == NULL)
172219019Sgabor			return (ENOMEM);
173219019Sgabor		TAILQ_INSERT_TAIL(&m->child, m0, entry);
174219019Sgabor	}
175219019Sgabor	m = m0;
176219019Sgabor	for (m0 = NULL; (ch = (unsigned char)*s) != '\0'; ++s) {
177219019Sgabor		m0 = mnemonic_list_find(&m->child, ch);
178219019Sgabor		if (m0 == NULL) {
179219019Sgabor			m0 = mnemonic_create(m, ch, invalid);
180219019Sgabor			if (m0 == NULL)
181219019Sgabor				return (ENOMEM);
182219019Sgabor			TAILQ_INSERT_TAIL(&m->child, m0, entry);
183219019Sgabor		}
184219019Sgabor		m = m0;
185219019Sgabor	}
186219019Sgabor	if (m0 == NULL)
187219019Sgabor		return (EINVAL);
188219019Sgabor	m0->value = value;
189219019Sgabor
190219019Sgabor	return (0);
191219019Sgabor}
192219019Sgabor
193219019Sgaborstatic void
194219019Sgabormnemonic_destroy(mnemonic_t *m)
195219019Sgabor{
196219019Sgabor	mnemonic_t *m0;
197219019Sgabor
198219019Sgabor	TAILQ_FOREACH(m0, &m->child, entry)
199219019Sgabor		mnemonic_destroy(m0);
200219019Sgabor	free(m);
201219019Sgabor}
202219019Sgabor
203219019Sgabortypedef struct {
204219019Sgabor	mnemonic_t	*mroot;
205219019Sgabor	wchar_t		 invalid;
206219019Sgabor	size_t		 mb_cur_max;
207219019Sgabor} _VIQREncodingInfo;
208219019Sgabor
209219019Sgabortypedef struct {
210219019Sgabor	int	 chlen;
211219019Sgabor	char	 ch[MB_LEN_MAX];
212219019Sgabor} _VIQRState;
213219019Sgabor
214219019Sgabor#define _CEI_TO_EI(_cei_)		(&(_cei_)->ei)
215219019Sgabor#define _CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
216219019Sgabor
217219019Sgabor#define _FUNCNAME(m)			_citrus_VIQR_##m
218219019Sgabor#define _ENCODING_INFO			_VIQREncodingInfo
219219019Sgabor#define _ENCODING_STATE			_VIQRState
220219019Sgabor#define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
221219019Sgabor#define _ENCODING_IS_STATE_DEPENDENT		1
222219019Sgabor#define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
223219019Sgabor
224219019Sgaborstatic __inline void
225219019Sgabor/*ARGSUSED*/
226219019Sgabor_citrus_VIQR_init_state(_VIQREncodingInfo * __restrict ei __unused,
227219019Sgabor    _VIQRState * __restrict psenc)
228219019Sgabor{
229219019Sgabor
230219019Sgabor	psenc->chlen = 0;
231219019Sgabor}
232219019Sgabor
233219019Sgaborstatic __inline void
234219019Sgabor/*ARGSUSED*/
235219019Sgabor_citrus_VIQR_pack_state(_VIQREncodingInfo * __restrict ei __unused,
236219019Sgabor    void *__restrict pspriv, const _VIQRState * __restrict psenc)
237219019Sgabor{
238219019Sgabor
239219019Sgabor	memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
240219019Sgabor}
241219019Sgabor
242219019Sgaborstatic __inline void
243219019Sgabor/*ARGSUSED*/
244219019Sgabor_citrus_VIQR_unpack_state(_VIQREncodingInfo * __restrict ei __unused,
245219019Sgabor    _VIQRState * __restrict psenc, const void * __restrict pspriv)
246219019Sgabor{
247219019Sgabor
248219019Sgabor	memcpy((void *)psenc, pspriv, sizeof(*psenc));
249219019Sgabor}
250219019Sgabor
251219019Sgaborstatic int
252219019Sgabor_citrus_VIQR_mbrtowc_priv(_VIQREncodingInfo * __restrict ei,
253252583Speter    wchar_t * __restrict pwc, const char ** __restrict s, size_t n,
254219019Sgabor    _VIQRState * __restrict psenc, size_t * __restrict nresult)
255219019Sgabor{
256219019Sgabor	mnemonic_t *m, *m0;
257252583Speter	const char *s0;
258219019Sgabor	wchar_t wc;
259219019Sgabor	ssize_t i;
260219019Sgabor	int ch, escape;
261219019Sgabor
262219019Sgabor	if (*s == NULL) {
263219019Sgabor		_citrus_VIQR_init_state(ei, psenc);
264219019Sgabor		*nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
265219019Sgabor		return (0);
266219019Sgabor	}
267219019Sgabor	s0 = *s;
268219019Sgabor
269219019Sgabor	i = 0;
270219019Sgabor	m = ei->mroot;
271219019Sgabor	for (escape = 0;;) {
272219019Sgabor		if (psenc->chlen == i) {
273219019Sgabor			if (n-- < 1) {
274219019Sgabor				*s = s0;
275219019Sgabor				*nresult = (size_t)-2;
276219019Sgabor				return (0);
277219019Sgabor			}
278219019Sgabor			psenc->ch[psenc->chlen++] = *s0++;
279219019Sgabor		}
280219019Sgabor		ch = (unsigned char)psenc->ch[i++];
281219019Sgabor		if (ch == ESCAPE) {
282219019Sgabor			if (m != ei->mroot)
283219019Sgabor				break;
284219019Sgabor			escape = 1;
285219019Sgabor			continue;
286219019Sgabor		}
287219019Sgabor		if (escape != 0)
288219019Sgabor			break;
289219019Sgabor		m0 = mnemonic_list_find(&m->child, ch);
290219019Sgabor		if (m0 == NULL)
291219019Sgabor			break;
292219019Sgabor		m = m0;
293219019Sgabor	}
294219019Sgabor	while (m != ei->mroot) {
295219019Sgabor		--i;
296219019Sgabor		if (m->value != ei->invalid)
297219019Sgabor			break;
298219019Sgabor		m = m->parent;
299219019Sgabor	}
300219019Sgabor	if (ch == ESCAPE && m != ei->mroot)
301219019Sgabor		++i;
302219019Sgabor	psenc->chlen -= i;
303219019Sgabor	memmove(&psenc->ch[0], &psenc->ch[i], psenc->chlen);
304219019Sgabor	wc = (m == ei->mroot) ? (wchar_t)ch : m->value;
305219019Sgabor	if (pwc != NULL)
306219019Sgabor		*pwc = wc;
307219019Sgabor	*nresult = (size_t)(wc == 0 ? 0 : s0 - *s);
308219019Sgabor	*s = s0;
309219019Sgabor
310219019Sgabor	return (0);
311219019Sgabor}
312219019Sgabor
313219019Sgaborstatic int
314219019Sgabor_citrus_VIQR_wcrtomb_priv(_VIQREncodingInfo * __restrict ei,
315219019Sgabor    char * __restrict s, size_t n, wchar_t wc,
316219019Sgabor    _VIQRState * __restrict psenc, size_t * __restrict nresult)
317219019Sgabor{
318219019Sgabor	mnemonic_t *m;
319219019Sgabor	const char *p;
320219019Sgabor	int ch = 0;
321219019Sgabor
322219019Sgabor	switch (psenc->chlen) {
323219019Sgabor	case 0: case 1:
324219019Sgabor		break;
325219019Sgabor	default:
326219019Sgabor		return (EINVAL);
327219019Sgabor	}
328219019Sgabor	m = NULL;
329219019Sgabor	if ((uint32_t)wc <= 0xFF) {
330219019Sgabor		p = mnemonic_rfc1456[wc & 0xFF];
331219019Sgabor		if (p != NULL)
332219019Sgabor			goto mnemonic_found;
333219019Sgabor		if (n-- < 1)
334219019Sgabor			goto e2big;
335219019Sgabor		ch = (unsigned int)wc;
336219019Sgabor		m = ei->mroot;
337219019Sgabor		if (psenc->chlen > 0) {
338219019Sgabor			m = mnemonic_list_find(&m->child, psenc->ch[0]);
339219019Sgabor			if (m == NULL)
340219019Sgabor				return (EINVAL);
341219019Sgabor			psenc->ch[0] = ESCAPE;
342219019Sgabor		}
343219019Sgabor		if (mnemonic_list_find(&m->child, ch) == NULL) {
344219019Sgabor			psenc->chlen = 0;
345219019Sgabor			m = NULL;
346219019Sgabor		}
347219019Sgabor		psenc->ch[psenc->chlen++] = ch;
348219019Sgabor	} else {
349219019Sgabor		p = mnemonic_ext_find(wc, &mnemonic_ext[0], mnemonic_ext_size);
350219019Sgabor		if (p == NULL) {
351219019Sgabor			*nresult = (size_t)-1;
352219019Sgabor			return (EILSEQ);
353219019Sgabor		} else {
354219019Sgabormnemonic_found:
355219019Sgabor			psenc->chlen = 0;
356219019Sgabor			while (*p != '\0') {
357219019Sgabor				if (n-- < 1)
358219019Sgabor					goto e2big;
359219019Sgabor				psenc->ch[psenc->chlen++] = *p++;
360219019Sgabor			}
361219019Sgabor		}
362219019Sgabor	}
363219019Sgabor	memcpy(s, psenc->ch, psenc->chlen);
364219019Sgabor	*nresult = psenc->chlen;
365219019Sgabor	if (m == ei->mroot) {
366219019Sgabor		psenc->ch[0] = ch;
367219019Sgabor		psenc->chlen = 1;
368219019Sgabor	} else
369219019Sgabor		psenc->chlen = 0;
370219019Sgabor
371219019Sgabor	return (0);
372219019Sgabor
373219019Sgabore2big:
374219019Sgabor	*nresult = (size_t)-1;
375219019Sgabor	return (E2BIG);
376219019Sgabor}
377219019Sgabor
378219019Sgaborstatic int
379219019Sgabor/* ARGSUSED */
380219019Sgabor_citrus_VIQR_put_state_reset(_VIQREncodingInfo * __restrict ei __unused,
381219019Sgabor    char * __restrict s __unused, size_t n __unused,
382219019Sgabor    _VIQRState * __restrict psenc, size_t * __restrict nresult)
383219019Sgabor{
384219019Sgabor
385219019Sgabor	switch (psenc->chlen) {
386219019Sgabor	case 0: case 1:
387219019Sgabor		break;
388219019Sgabor	default:
389219019Sgabor		return (EINVAL);
390219019Sgabor	}
391219019Sgabor	*nresult = 0;
392219019Sgabor	psenc->chlen = 0;
393219019Sgabor
394219019Sgabor	return (0);
395219019Sgabor}
396219019Sgabor
397219019Sgaborstatic __inline int
398219019Sgabor/*ARGSUSED*/
399219019Sgabor_citrus_VIQR_stdenc_wctocs(_VIQREncodingInfo * __restrict ei __unused,
400219019Sgabor    _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
401219019Sgabor{
402219019Sgabor
403219019Sgabor	*csid = 0;
404219019Sgabor	*idx = (_index_t)wc;
405219019Sgabor
406219019Sgabor	return (0);
407219019Sgabor}
408219019Sgabor
409219019Sgaborstatic __inline int
410219019Sgabor/*ARGSUSED*/
411219019Sgabor_citrus_VIQR_stdenc_cstowc(_VIQREncodingInfo * __restrict ei __unused,
412219019Sgabor    wchar_t * __restrict pwc, _csid_t csid, _index_t idx)
413219019Sgabor{
414219019Sgabor
415219019Sgabor	if (csid != 0)
416219019Sgabor		return (EILSEQ);
417219019Sgabor	*pwc = (wchar_t)idx;
418219019Sgabor
419219019Sgabor	return (0);
420219019Sgabor}
421219019Sgabor
422219019Sgaborstatic void
423219019Sgabor_citrus_VIQR_encoding_module_uninit(_VIQREncodingInfo *ei)
424219019Sgabor{
425219019Sgabor
426219019Sgabor	mnemonic_destroy(ei->mroot);
427219019Sgabor}
428219019Sgabor
429219019Sgaborstatic int
430219019Sgabor/*ARGSUSED*/
431219019Sgabor_citrus_VIQR_encoding_module_init(_VIQREncodingInfo * __restrict ei,
432219019Sgabor    const void * __restrict var __unused, size_t lenvar __unused)
433219019Sgabor{
434219019Sgabor	const char *s;
435219019Sgabor	size_t i, n;
436219019Sgabor	int errnum;
437219019Sgabor
438219019Sgabor	ei->mb_cur_max = 1;
439219019Sgabor	ei->invalid = (wchar_t)-1;
440219019Sgabor	ei->mroot = mnemonic_create(NULL, '\0', ei->invalid);
441219019Sgabor	if (ei->mroot == NULL)
442219019Sgabor		return (ENOMEM);
443219019Sgabor	for (i = 0; i < sizeof(mnemonic_rfc1456) / sizeof(const char *); ++i) {
444219019Sgabor		s = mnemonic_rfc1456[i];
445219019Sgabor		if (s == NULL)
446219019Sgabor			continue;
447219019Sgabor		n = strlen(s);
448219019Sgabor		if (ei->mb_cur_max < n)
449219019Sgabor			ei->mb_cur_max = n;
450219019Sgabor		errnum = mnemonic_append_child(ei->mroot,
451219019Sgabor		    s, (wchar_t)i, ei->invalid);
452219019Sgabor		if (errnum != 0) {
453219019Sgabor			_citrus_VIQR_encoding_module_uninit(ei);
454219019Sgabor			return (errnum);
455219019Sgabor		}
456219019Sgabor	}
457267829Sdelphij	/* a + 1 < b + 1 here to silence gcc warning about unsigned < 0. */
458267829Sdelphij	for (i = 0; i + 1 < mnemonic_ext_size + 1; ++i) {
459267829Sdelphij		const mnemonic_def_t *p;
460267829Sdelphij
461219019Sgabor		p = &mnemonic_ext[i];
462219019Sgabor		n = strlen(p->name);
463219019Sgabor		if (ei->mb_cur_max < n)
464219019Sgabor			ei->mb_cur_max = n;
465219019Sgabor		errnum = mnemonic_append_child(ei->mroot,
466219019Sgabor		    p->name, p->value, ei->invalid);
467219019Sgabor		if (errnum != 0) {
468219019Sgabor			_citrus_VIQR_encoding_module_uninit(ei);
469219019Sgabor			return (errnum);
470219019Sgabor		}
471219019Sgabor	}
472219019Sgabor
473219019Sgabor	return (0);
474219019Sgabor}
475219019Sgabor
476219019Sgaborstatic __inline int
477219019Sgabor/*ARGSUSED*/
478219019Sgabor_citrus_VIQR_stdenc_get_state_desc_generic(_VIQREncodingInfo * __restrict ei __unused,
479219019Sgabor    _VIQRState * __restrict psenc, int * __restrict rstate)
480219019Sgabor{
481219019Sgabor
482219019Sgabor	*rstate = (psenc->chlen == 0) ?
483219019Sgabor	    _STDENC_SDGEN_INITIAL :
484219019Sgabor	    _STDENC_SDGEN_INCOMPLETE_CHAR;
485219019Sgabor
486219019Sgabor	return (0);
487219019Sgabor}
488219019Sgabor
489219019Sgabor/* ----------------------------------------------------------------------
490219019Sgabor * public interface for stdenc
491219019Sgabor */
492219019Sgabor
493219019Sgabor_CITRUS_STDENC_DECLS(VIQR);
494219019Sgabor_CITRUS_STDENC_DEF_OPS(VIQR);
495219019Sgabor
496219019Sgabor#include "citrus_stdenc_template.h"
497