1219019Sgabor/* $FreeBSD$ */
2219019Sgabor/*	$NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $	*/
3219019Sgabor
4219019Sgabor/*-
5219019Sgabor * Copyright (c)2004, 2005 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
33219019Sgabor#include <assert.h>
34219019Sgabor#include <errno.h>
35219019Sgabor#include <limits.h>
36219019Sgabor#include <stdio.h>
37219019Sgabor#include <stdint.h>
38219019Sgabor#include <stdlib.h>
39219019Sgabor#include <string.h>
40219019Sgabor#include <wchar.h>
41219019Sgabor
42219019Sgabor#include "citrus_namespace.h"
43219019Sgabor#include "citrus_types.h"
44219019Sgabor#include "citrus_module.h"
45219019Sgabor#include "citrus_stdenc.h"
46219019Sgabor#include "citrus_utf7.h"
47219019Sgabor
48219019Sgabor/* ----------------------------------------------------------------------
49219019Sgabor * private stuffs used by templates
50219019Sgabor */
51219019Sgabor
52219019Sgabor#define EI_MASK		UINT16_C(0xff)
53219019Sgabor#define EI_DIRECT	UINT16_C(0x100)
54219019Sgabor#define EI_OPTION	UINT16_C(0x200)
55219019Sgabor#define EI_SPACE	UINT16_C(0x400)
56219019Sgabor
57219019Sgabortypedef struct {
58219019Sgabor	uint16_t	 cell[0x80];
59219019Sgabor} _UTF7EncodingInfo;
60219019Sgabor
61219019Sgabortypedef struct {
62219019Sgabor	unsigned int
63219019Sgabor		mode: 1,	/* whether base64 mode */
64219019Sgabor		bits: 4,	/* need to hold 0 - 15 */
65219019Sgabor		cache: 22,	/* 22 = BASE64_BIT + UTF16_BIT */
66219019Sgabor		surrogate: 1;	/* whether surrogate pair or not */
67219019Sgabor	int chlen;
68219019Sgabor	char ch[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */
69219019Sgabor} _UTF7State;
70219019Sgabor
71219019Sgabor#define	_CEI_TO_EI(_cei_)		(&(_cei_)->ei)
72219019Sgabor#define	_CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
73219019Sgabor
74219019Sgabor#define	_FUNCNAME(m)			_citrus_UTF7_##m
75219019Sgabor#define	_ENCODING_INFO			_UTF7EncodingInfo
76219019Sgabor#define	_ENCODING_STATE			_UTF7State
77219019Sgabor#define	_ENCODING_MB_CUR_MAX(_ei_)		4
78219019Sgabor#define	_ENCODING_IS_STATE_DEPENDENT		1
79219019Sgabor#define	_STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
80219019Sgabor
81219019Sgaborstatic __inline void
82219019Sgabor/*ARGSUSED*/
83219019Sgabor_citrus_UTF7_init_state(_UTF7EncodingInfo * __restrict ei __unused,
84219019Sgabor    _UTF7State * __restrict s)
85219019Sgabor{
86219019Sgabor
87219019Sgabor	memset((void *)s, 0, sizeof(*s));
88219019Sgabor}
89219019Sgabor
90260264Sdim#if 0
91219019Sgaborstatic __inline void
92219019Sgabor/*ARGSUSED*/
93219019Sgabor_citrus_UTF7_pack_state(_UTF7EncodingInfo * __restrict ei __unused,
94219019Sgabor    void *__restrict pspriv, const _UTF7State * __restrict s)
95219019Sgabor{
96219019Sgabor
97219019Sgabor	memcpy(pspriv, (const void *)s, sizeof(*s));
98219019Sgabor}
99219019Sgabor
100219019Sgaborstatic __inline void
101219019Sgabor/*ARGSUSED*/
102219019Sgabor_citrus_UTF7_unpack_state(_UTF7EncodingInfo * __restrict ei __unused,
103219019Sgabor    _UTF7State * __restrict s, const void * __restrict pspriv)
104219019Sgabor{
105219019Sgabor
106219019Sgabor	memcpy((void *)s, pspriv, sizeof(*s));
107219019Sgabor}
108260264Sdim#endif
109219019Sgabor
110219019Sgaborstatic const char base64[] =
111219019Sgabor	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
112219019Sgabor	"abcdefghijklmnopqrstuvwxyz"
113219019Sgabor	"0123456789+/";
114219019Sgabor
115219019Sgaborstatic const char direct[] =
116219019Sgabor	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
117219019Sgabor	"abcdefghijklmnopqrstuvwxyz"
118219019Sgabor	"0123456789(),-./:?";
119219019Sgabor
120219019Sgaborstatic const char option[] = "!\"#$%&';<=>@[]^_`{|}";
121219019Sgaborstatic const char spaces[] = " \t\r\n";
122219019Sgabor
123219019Sgabor#define	BASE64_BIT	6
124219019Sgabor#define	UTF16_BIT	16
125219019Sgabor
126219019Sgabor#define	BASE64_MAX	0x3f
127219019Sgabor#define	UTF16_MAX	UINT16_C(0xffff)
128219019Sgabor#define	UTF32_MAX	UINT32_C(0x10ffff)
129219019Sgabor
130219019Sgabor#define	BASE64_IN	'+'
131219019Sgabor#define	BASE64_OUT	'-'
132219019Sgabor
133219019Sgabor#define	SHIFT7BIT(c)	((c) >> 7)
134219019Sgabor#define	ISSPECIAL(c)	((c) == '\0' || (c) == BASE64_IN)
135219019Sgabor
136219019Sgabor#define	FINDLEN(ei, c) \
137219019Sgabor	(SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
138219019Sgabor
139219019Sgabor#define	ISDIRECT(ei, c)	(!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
140219019Sgabor	ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
141219019Sgabor
142219019Sgabor#define	ISSAFE(ei, c)	(!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
143219019Sgabor	(c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
144219019Sgabor
145219019Sgabor/* surrogate pair */
146219019Sgabor#define	SRG_BASE	UINT32_C(0x10000)
147219019Sgabor#define	HISRG_MIN	UINT16_C(0xd800)
148219019Sgabor#define	HISRG_MAX	UINT16_C(0xdbff)
149219019Sgabor#define	LOSRG_MIN	UINT16_C(0xdc00)
150219019Sgabor#define	LOSRG_MAX	UINT16_C(0xdfff)
151219019Sgabor
152219019Sgaborstatic int
153219019Sgabor_citrus_UTF7_mbtoutf16(_UTF7EncodingInfo * __restrict ei,
154219019Sgabor    uint16_t * __restrict u16, char ** __restrict s, size_t n,
155219019Sgabor    _UTF7State * __restrict psenc, size_t * __restrict nresult)
156219019Sgabor{
157219019Sgabor	_UTF7State sv;
158219019Sgabor	char *s0;
159219019Sgabor	int done, i, len;
160219019Sgabor
161219019Sgabor	s0 = *s;
162219019Sgabor	sv = *psenc;
163219019Sgabor
164219019Sgabor	for (i = 0, done = 0; done == 0; i++) {
165219019Sgabor		if (i == psenc->chlen) {
166219019Sgabor			if (n-- < 1) {
167219019Sgabor				*nresult = (size_t)-2;
168219019Sgabor				*s = s0;
169219019Sgabor				sv.chlen = psenc->chlen;
170219019Sgabor				*psenc = sv;
171219019Sgabor				return (0);
172219019Sgabor			}
173219019Sgabor			psenc->ch[psenc->chlen++] = *s0++;
174219019Sgabor		}
175219019Sgabor		if (SHIFT7BIT((int)psenc->ch[i]))
176219019Sgabor			goto ilseq;
177219019Sgabor		if (!psenc->mode) {
178219019Sgabor			if (psenc->bits > 0 || psenc->cache > 0)
179219019Sgabor				return (EINVAL);
180219019Sgabor			if (psenc->ch[i] == BASE64_IN)
181219019Sgabor				psenc->mode = 1;
182219019Sgabor			else {
183219019Sgabor				if (!ISDIRECT(ei, (int)psenc->ch[i]))
184219019Sgabor					goto ilseq;
185219019Sgabor				*u16 = (uint16_t)psenc->ch[i];
186219019Sgabor				done = 1;
187219019Sgabor				continue;
188219019Sgabor			}
189219019Sgabor		} else {
190219019Sgabor			if (psenc->ch[i] == BASE64_OUT && psenc->cache == 0) {
191219019Sgabor				psenc->mode = 0;
192219019Sgabor				*u16 = (uint16_t)BASE64_IN;
193219019Sgabor				done = 1;
194219019Sgabor				continue;
195219019Sgabor			}
196219019Sgabor			len = FINDLEN(ei, (int)psenc->ch[i]);
197219019Sgabor			if (len < 0) {
198219019Sgabor				if (psenc->bits >= BASE64_BIT)
199219019Sgabor					return (EINVAL);
200219019Sgabor				psenc->mode = 0;
201219019Sgabor				psenc->bits = psenc->cache = 0;
202219019Sgabor				if (psenc->ch[i] != BASE64_OUT) {
203219019Sgabor					if (!ISDIRECT(ei, (int)psenc->ch[i]))
204219019Sgabor						goto ilseq;
205219019Sgabor					*u16 = (uint16_t)psenc->ch[i];
206219019Sgabor					done = 1;
207219019Sgabor				}
208219019Sgabor			} else {
209219019Sgabor				psenc->cache =
210219019Sgabor				    (psenc->cache << BASE64_BIT) | len;
211219019Sgabor				switch (psenc->bits) {
212219019Sgabor				case 0: case 2: case 4: case 6: case 8:
213219019Sgabor					psenc->bits += BASE64_BIT;
214219019Sgabor					break;
215219019Sgabor				case 10: case 12: case 14:
216219019Sgabor					psenc->bits -= (UTF16_BIT - BASE64_BIT);
217219019Sgabor					*u16 = (psenc->cache >> psenc->bits) &
218219019Sgabor					    UTF16_MAX;
219219019Sgabor					done = 1;
220219019Sgabor					break;
221219019Sgabor				default:
222219019Sgabor					return (EINVAL);
223219019Sgabor				}
224219019Sgabor			}
225219019Sgabor		}
226219019Sgabor	}
227219019Sgabor
228219019Sgabor	if (psenc->chlen > i)
229219019Sgabor		return (EINVAL);
230219019Sgabor	psenc->chlen = 0;
231219019Sgabor	*nresult = (size_t)((*u16 == 0) ? 0 : s0 - *s);
232219019Sgabor	*s = s0;
233219019Sgabor
234219019Sgabor	return (0);
235219019Sgabor
236219019Sgaborilseq:
237219019Sgabor	*nresult = (size_t)-1;
238219019Sgabor	return (EILSEQ);
239219019Sgabor}
240219019Sgabor
241219019Sgaborstatic int
242219019Sgabor_citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo * __restrict ei,
243219019Sgabor    wchar_t * __restrict pwc, char ** __restrict s, size_t n,
244219019Sgabor    _UTF7State * __restrict psenc, size_t * __restrict nresult)
245219019Sgabor{
246219019Sgabor	char *s0;
247219019Sgabor	uint32_t u32;
248219019Sgabor	uint16_t hi, lo;
249219019Sgabor	size_t nr, siz;
250219019Sgabor	int err;
251219019Sgabor
252219019Sgabor	if (*s == NULL) {
253219019Sgabor		_citrus_UTF7_init_state(ei, psenc);
254219019Sgabor		*nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
255219019Sgabor		return (0);
256219019Sgabor	}
257219019Sgabor	s0 = *s;
258219019Sgabor	if (psenc->surrogate) {
259219019Sgabor		hi = (psenc->cache >> 2) & UTF16_MAX;
260219019Sgabor		if (hi < HISRG_MIN || hi > HISRG_MAX)
261219019Sgabor			return (EINVAL);
262219019Sgabor		siz = 0;
263219019Sgabor	} else {
264219019Sgabor		err = _citrus_UTF7_mbtoutf16(ei, &hi, &s0, n, psenc, &nr);
265219019Sgabor		if (nr == (size_t)-1 || nr == (size_t)-2) {
266219019Sgabor			*nresult = nr;
267219019Sgabor			return (err);
268219019Sgabor		}
269219019Sgabor		if (err != 0)
270219019Sgabor			return (err);
271219019Sgabor		n -= nr;
272219019Sgabor		siz = nr;
273219019Sgabor		if (hi < HISRG_MIN || hi > HISRG_MAX) {
274219019Sgabor			u32 = (uint32_t)hi;
275219019Sgabor			goto done;
276219019Sgabor		}
277219019Sgabor		psenc->surrogate = 1;
278219019Sgabor	}
279219019Sgabor	err = _citrus_UTF7_mbtoutf16(ei, &lo, &s0, n, psenc, &nr);
280219019Sgabor	if (nr == (size_t)-1 || nr == (size_t)-2) {
281219019Sgabor		*nresult = nr;
282219019Sgabor		return (err);
283219019Sgabor	}
284219019Sgabor	if (err != 0)
285219019Sgabor		return (err);
286219019Sgabor	hi -= HISRG_MIN;
287219019Sgabor	lo -= LOSRG_MIN;
288219019Sgabor	u32 = (hi << 10 | lo) + SRG_BASE;
289219019Sgabor	siz += nr;
290219019Sgabordone:
291219019Sgabor	*s = s0;
292219019Sgabor	if (pwc != NULL)
293219019Sgabor		*pwc = (wchar_t)u32;
294219019Sgabor	if (u32 == (uint32_t)0) {
295219019Sgabor		*nresult = (size_t)0;
296219019Sgabor		_citrus_UTF7_init_state(ei, psenc);
297219019Sgabor	} else {
298219019Sgabor		*nresult = siz;
299219019Sgabor		psenc->surrogate = 0;
300219019Sgabor	}
301219019Sgabor	return (err);
302219019Sgabor}
303219019Sgabor
304219019Sgaborstatic int
305219019Sgabor_citrus_UTF7_utf16tomb(_UTF7EncodingInfo * __restrict ei,
306219019Sgabor    char * __restrict s, size_t n __unused, uint16_t u16,
307219019Sgabor    _UTF7State * __restrict psenc, size_t * __restrict nresult)
308219019Sgabor{
309219019Sgabor	int bits, i;
310219019Sgabor
311219019Sgabor	if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
312219019Sgabor		return (EINVAL);
313219019Sgabor
314219019Sgabor	if (ISSAFE(ei, u16)) {
315219019Sgabor		if (psenc->mode) {
316219019Sgabor			if (psenc->bits > 0) {
317219019Sgabor				bits = BASE64_BIT - psenc->bits;
318219019Sgabor				i = (psenc->cache << bits) & BASE64_MAX;
319219019Sgabor				psenc->ch[psenc->chlen++] = base64[i];
320219019Sgabor				psenc->bits = psenc->cache = 0;
321219019Sgabor			}
322219019Sgabor			if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0)
323219019Sgabor				psenc->ch[psenc->chlen++] = BASE64_OUT;
324219019Sgabor			psenc->mode = 0;
325219019Sgabor		}
326219019Sgabor		if (psenc->bits != 0)
327219019Sgabor			return (EINVAL);
328219019Sgabor		psenc->ch[psenc->chlen++] = (char)u16;
329219019Sgabor		if (u16 == BASE64_IN)
330219019Sgabor			psenc->ch[psenc->chlen++] = BASE64_OUT;
331219019Sgabor	} else {
332219019Sgabor		if (!psenc->mode) {
333219019Sgabor			if (psenc->bits > 0)
334219019Sgabor				return (EINVAL);
335219019Sgabor			psenc->ch[psenc->chlen++] = BASE64_IN;
336219019Sgabor			psenc->mode = 1;
337219019Sgabor		}
338219019Sgabor		psenc->cache = (psenc->cache << UTF16_BIT) | u16;
339219019Sgabor		bits = UTF16_BIT + psenc->bits;
340219019Sgabor		psenc->bits = bits % BASE64_BIT;
341219019Sgabor		while ((bits -= BASE64_BIT) >= 0) {
342219019Sgabor			i = (psenc->cache >> bits) & BASE64_MAX;
343219019Sgabor			psenc->ch[psenc->chlen++] = base64[i];
344219019Sgabor		}
345219019Sgabor	}
346219019Sgabor	memcpy(s, psenc->ch, psenc->chlen);
347219019Sgabor	*nresult = psenc->chlen;
348219019Sgabor	psenc->chlen = 0;
349219019Sgabor
350219019Sgabor	return (0);
351219019Sgabor}
352219019Sgabor
353219019Sgaborstatic int
354219019Sgabor_citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei,
355219019Sgabor    char * __restrict s, size_t n, wchar_t wchar,
356219019Sgabor    _UTF7State * __restrict psenc, size_t * __restrict nresult)
357219019Sgabor{
358219019Sgabor	uint32_t u32;
359219019Sgabor	uint16_t u16[2];
360219019Sgabor	int err, i, len;
361219019Sgabor	size_t nr, siz;
362219019Sgabor
363219019Sgabor	u32 = (uint32_t)wchar;
364219019Sgabor	if (u32 <= UTF16_MAX) {
365219019Sgabor		u16[0] = (uint16_t)u32;
366219019Sgabor		len = 1;
367219019Sgabor	} else if (u32 <= UTF32_MAX) {
368219019Sgabor		u32 -= SRG_BASE;
369219019Sgabor		u16[0] = (u32 >> 10) + HISRG_MIN;
370219019Sgabor		u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN;
371219019Sgabor		len = 2;
372219019Sgabor	} else {
373219019Sgabor		*nresult = (size_t)-1;
374219019Sgabor		return (EILSEQ);
375219019Sgabor	}
376219019Sgabor	siz = 0;
377219019Sgabor	for (i = 0; i < len; ++i) {
378219019Sgabor		err = _citrus_UTF7_utf16tomb(ei, s, n, u16[i], psenc, &nr);
379219019Sgabor		if (err != 0)
380219019Sgabor			return (err); /* XXX: state has been modified */
381219019Sgabor		s += nr;
382219019Sgabor		n -= nr;
383219019Sgabor		siz += nr;
384219019Sgabor	}
385219019Sgabor	*nresult = siz;
386219019Sgabor
387219019Sgabor	return (0);
388219019Sgabor}
389219019Sgabor
390219019Sgaborstatic int
391219019Sgabor/* ARGSUSED */
392219019Sgabor_citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei __unused,
393219019Sgabor    char * __restrict s, size_t n, _UTF7State * __restrict psenc,
394219019Sgabor    size_t * __restrict nresult)
395219019Sgabor{
396219019Sgabor	int bits, pos;
397219019Sgabor
398219019Sgabor	if (psenc->chlen != 0 || psenc->bits > BASE64_BIT || psenc->surrogate)
399219019Sgabor		return (EINVAL);
400219019Sgabor
401219019Sgabor	if (psenc->mode) {
402219019Sgabor		if (psenc->bits > 0) {
403219019Sgabor			if (n-- < 1)
404219019Sgabor				return (E2BIG);
405219019Sgabor			bits = BASE64_BIT - psenc->bits;
406219019Sgabor			pos = (psenc->cache << bits) & BASE64_MAX;
407219019Sgabor			psenc->ch[psenc->chlen++] = base64[pos];
408219019Sgabor			psenc->ch[psenc->chlen++] = BASE64_OUT;
409219019Sgabor			psenc->bits = psenc->cache = 0;
410219019Sgabor		}
411219019Sgabor		psenc->mode = 0;
412219019Sgabor	}
413219019Sgabor	if (psenc->bits != 0)
414219019Sgabor		return (EINVAL);
415219019Sgabor	if (n-- < 1)
416219019Sgabor		return (E2BIG);
417219019Sgabor
418219019Sgabor	*nresult = (size_t)psenc->chlen;
419219019Sgabor	if (psenc->chlen > 0) {
420219019Sgabor		memcpy(s, psenc->ch, psenc->chlen);
421219019Sgabor		psenc->chlen = 0;
422219019Sgabor	}
423219019Sgabor
424219019Sgabor	return (0);
425219019Sgabor}
426219019Sgabor
427219019Sgaborstatic __inline int
428219019Sgabor/*ARGSUSED*/
429219019Sgabor_citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei __unused,
430219019Sgabor    _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
431219019Sgabor{
432219019Sgabor
433219019Sgabor	*csid = 0;
434219019Sgabor	*idx = (_index_t)wc;
435219019Sgabor
436219019Sgabor	return (0);
437219019Sgabor}
438219019Sgabor
439219019Sgaborstatic __inline int
440219019Sgabor/*ARGSUSED*/
441219019Sgabor_citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei __unused,
442219019Sgabor    wchar_t * __restrict wc, _csid_t csid, _index_t idx)
443219019Sgabor{
444219019Sgabor
445219019Sgabor	if (csid != 0)
446219019Sgabor		return (EILSEQ);
447219019Sgabor	*wc = (wchar_t)idx;
448219019Sgabor
449219019Sgabor	return (0);
450219019Sgabor}
451219019Sgabor
452219019Sgaborstatic __inline int
453219019Sgabor/*ARGSUSED*/
454219019Sgabor_citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo * __restrict ei __unused,
455219019Sgabor    _UTF7State * __restrict psenc, int * __restrict rstate)
456219019Sgabor{
457219019Sgabor
458219019Sgabor	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
459219019Sgabor	    _STDENC_SDGEN_INCOMPLETE_CHAR;
460219019Sgabor	return (0);
461219019Sgabor}
462219019Sgabor
463219019Sgaborstatic void
464219019Sgabor/*ARGSUSED*/
465219019Sgabor_citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei __unused)
466219019Sgabor{
467219019Sgabor
468219019Sgabor	/* ei seems to be unused */
469219019Sgabor}
470219019Sgabor
471219019Sgaborstatic int
472219019Sgabor/*ARGSUSED*/
473219019Sgabor_citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei,
474219019Sgabor    const void * __restrict var __unused, size_t lenvar __unused)
475219019Sgabor{
476219019Sgabor	const char *s;
477219019Sgabor
478219019Sgabor	memset(ei, 0, sizeof(*ei));
479219019Sgabor
480219019Sgabor#define FILL(str, flag)				\
481219019Sgabordo {						\
482219019Sgabor	for (s = str; *s != '\0'; s++)		\
483219019Sgabor		ei->cell[*s & 0x7f] |= flag;	\
484219019Sgabor} while (/*CONSTCOND*/0)
485219019Sgabor
486219019Sgabor	FILL(base64, (s - base64) + 1);
487219019Sgabor	FILL(direct, EI_DIRECT);
488219019Sgabor	FILL(option, EI_OPTION);
489219019Sgabor	FILL(spaces, EI_SPACE);
490219019Sgabor
491219019Sgabor	return (0);
492219019Sgabor}
493219019Sgabor
494219019Sgabor/* ----------------------------------------------------------------------
495219019Sgabor * public interface for stdenc
496219019Sgabor */
497219019Sgabor
498219019Sgabor_CITRUS_STDENC_DECLS(UTF7);
499219019Sgabor_CITRUS_STDENC_DEF_OPS(UTF7);
500219019Sgabor
501219019Sgabor#include "citrus_stdenc_template.h"
502