1/* $FreeBSD$ */
2/*	$NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $	*/
3
4/*-
5 * SPDX-License-Identifier: BSD-2-Clause
6 *
7 * Copyright (c)2004, 2005 Citrus Project,
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 */
32
33#include <sys/cdefs.h>
34
35#include <assert.h>
36#include <errno.h>
37#include <limits.h>
38#include <stdio.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_module.h"
47#include "citrus_stdenc.h"
48#include "citrus_utf7.h"
49
50/* ----------------------------------------------------------------------
51 * private stuffs used by templates
52 */
53
54#define EI_MASK		UINT16_C(0xff)
55#define EI_DIRECT	UINT16_C(0x100)
56#define EI_OPTION	UINT16_C(0x200)
57#define EI_SPACE	UINT16_C(0x400)
58
59typedef struct {
60	uint16_t	 cell[0x80];
61} _UTF7EncodingInfo;
62
63typedef struct {
64	unsigned int
65		mode: 1,	/* whether base64 mode */
66		bits: 4,	/* need to hold 0 - 15 */
67		cache: 22;	/* 22 = BASE64_BIT + UTF16_BIT */
68	int chlen;
69	char ch[4]; /* BASE64_IN, 3 * 6 = 18, most closed to UTF16_BIT */
70} _UTF7State;
71
72#define	_CEI_TO_EI(_cei_)		(&(_cei_)->ei)
73#define	_CEI_TO_STATE(_cei_, _func_)	(_cei_)->states.s_##_func_
74
75#define	_FUNCNAME(m)			_citrus_UTF7_##m
76#define	_ENCODING_INFO			_UTF7EncodingInfo
77#define	_ENCODING_STATE			_UTF7State
78#define	_ENCODING_MB_CUR_MAX(_ei_)		4
79#define	_ENCODING_IS_STATE_DEPENDENT		1
80#define	_STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
81
82static __inline void
83/*ARGSUSED*/
84_citrus_UTF7_init_state(_UTF7EncodingInfo * __restrict ei __unused,
85    _UTF7State * __restrict s)
86{
87
88	memset((void *)s, 0, sizeof(*s));
89}
90
91#if 0
92static __inline void
93/*ARGSUSED*/
94_citrus_UTF7_pack_state(_UTF7EncodingInfo * __restrict ei __unused,
95    void *__restrict pspriv, const _UTF7State * __restrict s)
96{
97
98	memcpy(pspriv, (const void *)s, sizeof(*s));
99}
100
101static __inline void
102/*ARGSUSED*/
103_citrus_UTF7_unpack_state(_UTF7EncodingInfo * __restrict ei __unused,
104    _UTF7State * __restrict s, const void * __restrict pspriv)
105{
106
107	memcpy((void *)s, pspriv, sizeof(*s));
108}
109#endif
110
111static const char base64[] =
112	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
113	"abcdefghijklmnopqrstuvwxyz"
114	"0123456789+/";
115
116static const char direct[] =
117	"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
118	"abcdefghijklmnopqrstuvwxyz"
119	"0123456789'(),-./:?";
120
121static const char option[] = "!\"#$%&*;<=>@[]^_`{|}";
122static const char spaces[] = " \t\r\n";
123
124#define	BASE64_BIT	6
125#define	UTF16_BIT	16
126
127#define	BASE64_MAX	0x3f
128#define	UTF16_MAX	UINT16_C(0xffff)
129#define	UTF32_MAX	UINT32_C(0x10ffff)
130
131#define	BASE64_IN	'+'
132#define	BASE64_OUT	'-'
133
134#define	SHIFT7BIT(c)	((c) >> 7)
135#define	ISSPECIAL(c)	((c) == '\0' || (c) == BASE64_IN)
136
137#define	FINDLEN(ei, c) \
138	(SHIFT7BIT((c)) ? -1 : (((ei)->cell[(c)] & EI_MASK) - 1))
139
140#define	ISDIRECT(ei, c)	(!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
141	ei->cell[(c)] & (EI_DIRECT | EI_OPTION | EI_SPACE)))
142
143#define	ISSAFE(ei, c)	(!SHIFT7BIT((c)) && (ISSPECIAL((c)) || \
144	(c < 0x80 && ei->cell[(c)] & (EI_DIRECT | EI_SPACE))))
145
146/* surrogate pair */
147#define	SRG_BASE	UINT32_C(0x10000)
148#define	HISRG_MIN	UINT16_C(0xd800)
149#define	HISRG_MAX	UINT16_C(0xdbff)
150#define	LOSRG_MIN	UINT16_C(0xdc00)
151#define	LOSRG_MAX	UINT16_C(0xdfff)
152
153static int
154_citrus_UTF7_mbtoutf16(_UTF7EncodingInfo * __restrict ei,
155    uint16_t * __restrict u16, char ** __restrict s, size_t n,
156    _UTF7State * __restrict psenc, size_t * __restrict nresult)
157{
158	char *s0;
159	int done, i, len;
160
161	*nresult = 0;
162	s0 = *s;
163
164	for (i = 0, done = 0; done == 0; i++) {
165		if (i == psenc->chlen) {
166			if (n-- < 1) {
167				*nresult = (size_t)-2;
168				*s = s0;
169				return (0);
170			}
171			psenc->ch[psenc->chlen++] = *s0++;
172		}
173		if (SHIFT7BIT((int)psenc->ch[i]))
174			goto ilseq;
175		if (!psenc->mode) {
176			if (psenc->bits > 0 || psenc->cache > 0)
177				return (EINVAL);
178			if (psenc->ch[i] == BASE64_IN)
179				psenc->mode = 1;
180			else {
181				if (!ISDIRECT(ei, (int)psenc->ch[i]))
182					goto ilseq;
183				*u16 = (uint16_t)psenc->ch[i];
184				done = 1;
185				continue;
186			}
187		} else {
188			if (psenc->ch[i] == BASE64_OUT && psenc->cache == 0) {
189				psenc->mode = 0;
190				*u16 = (uint16_t)BASE64_IN;
191				done = 1;
192				continue;
193			}
194			len = FINDLEN(ei, (int)psenc->ch[i]);
195			if (len < 0) {
196				if (psenc->bits >= BASE64_BIT)
197					return (EINVAL);
198				psenc->mode = 0;
199				psenc->bits = psenc->cache = 0;
200				if (psenc->ch[i] != BASE64_OUT) {
201					if (!ISDIRECT(ei, (int)psenc->ch[i]))
202						goto ilseq;
203					*u16 = (uint16_t)psenc->ch[i];
204					done = 1;
205				} else {
206					psenc->chlen--;
207					i--;
208				}
209			} else {
210				psenc->cache =
211				    (psenc->cache << BASE64_BIT) | len;
212				switch (psenc->bits) {
213				case 0: case 2: case 4: case 6: case 8:
214					psenc->bits += BASE64_BIT;
215					break;
216				case 10: case 12: case 14:
217					psenc->bits -= (UTF16_BIT - BASE64_BIT);
218					*u16 = (psenc->cache >> psenc->bits) &
219					    UTF16_MAX;
220					done = 1;
221					break;
222				default:
223					return (EINVAL);
224				}
225			}
226		}
227	}
228
229	if (psenc->chlen > i)
230		return (EINVAL);
231	psenc->chlen = 0;
232	*nresult = (size_t)((*u16 == 0) ? 0 : s0 - *s);
233	*s = s0;
234
235	return (0);
236
237ilseq:
238	*nresult = (size_t)-1;
239	return (EILSEQ);
240}
241
242static int
243_citrus_UTF7_mbrtowc_priv(_UTF7EncodingInfo * __restrict ei,
244    wchar_t * __restrict pwc, char ** __restrict s, size_t n,
245    _UTF7State * __restrict psenc, size_t * __restrict nresult)
246{
247	uint32_t u32;
248	uint16_t hi, lo;
249	size_t nr, siz;
250	int err;
251
252	if (*s == NULL) {
253		_citrus_UTF7_init_state(ei, psenc);
254		*nresult = (size_t)_ENCODING_IS_STATE_DEPENDENT;
255		return (0);
256	}
257	err = _citrus_UTF7_mbtoutf16(ei, &hi, s, n, psenc, &nr);
258	if (nr == (size_t)-1 || nr == (size_t)-2) {
259		*nresult = nr;
260		return (err);
261	}
262	if (err != 0)
263		return (err);
264	n -= nr;
265	siz = nr;
266	if (hi < HISRG_MIN || hi > HISRG_MAX) {
267		u32 = (uint32_t)hi;
268		goto done;
269	}
270	err = _citrus_UTF7_mbtoutf16(ei, &lo, s, n, psenc, &nr);
271	if (nr == (size_t)-1 || nr == (size_t)-2) {
272		psenc->chlen = 1; /* make get_state_desc return incomplete */
273		*nresult = nr;
274		return (err);
275	}
276	if (err != 0)
277		return (err);
278	if (lo < LOSRG_MIN || lo > LOSRG_MAX) {
279		*nresult = (size_t)-1;
280		return (EILSEQ);
281	}
282	hi -= HISRG_MIN;
283	lo -= LOSRG_MIN;
284	u32 = (hi << 10 | lo) + SRG_BASE;
285	siz += nr;
286done:
287	if (pwc != NULL)
288		*pwc = (wchar_t)u32;
289	if (u32 == (uint32_t)0) {
290		*nresult = (size_t)0;
291		_citrus_UTF7_init_state(ei, psenc);
292	} else {
293		*nresult = siz;
294	}
295	return (err);
296}
297
298static int
299_citrus_UTF7_utf16tomb(_UTF7EncodingInfo * __restrict ei,
300    char * __restrict s, size_t n __unused, uint16_t u16,
301    _UTF7State * __restrict psenc, size_t * __restrict nresult)
302{
303	int bits, i;
304
305	if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
306		return (EINVAL);
307
308	if (ISSAFE(ei, u16)) {
309		if (psenc->mode) {
310			if (psenc->bits > 0) {
311				bits = BASE64_BIT - psenc->bits;
312				i = (psenc->cache << bits) & BASE64_MAX;
313				psenc->ch[psenc->chlen++] = base64[i];
314				psenc->bits = psenc->cache = 0;
315			}
316			if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0)
317				psenc->ch[psenc->chlen++] = BASE64_OUT;
318			psenc->mode = 0;
319		}
320		if (psenc->bits != 0)
321			return (EINVAL);
322		psenc->ch[psenc->chlen++] = (char)u16;
323		if (u16 == BASE64_IN)
324			psenc->ch[psenc->chlen++] = BASE64_OUT;
325	} else {
326		if (!psenc->mode) {
327			if (psenc->bits > 0)
328				return (EINVAL);
329			psenc->ch[psenc->chlen++] = BASE64_IN;
330			psenc->mode = 1;
331		}
332		psenc->cache = (psenc->cache << UTF16_BIT) | u16;
333		bits = UTF16_BIT + psenc->bits;
334		psenc->bits = bits % BASE64_BIT;
335		while ((bits -= BASE64_BIT) >= 0) {
336			i = (psenc->cache >> bits) & BASE64_MAX;
337			psenc->ch[psenc->chlen++] = base64[i];
338		}
339	}
340	memcpy(s, psenc->ch, psenc->chlen);
341	*nresult = psenc->chlen;
342	psenc->chlen = 0;
343
344	return (0);
345}
346
347static int
348_citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei,
349    char * __restrict s, size_t n, wchar_t wchar,
350    _UTF7State * __restrict psenc, size_t * __restrict nresult)
351{
352	uint32_t u32;
353	uint16_t u16[2];
354	int err, i, len;
355	size_t nr, siz;
356
357	u32 = (uint32_t)wchar;
358	if (u32 <= UTF16_MAX) {
359		u16[0] = (uint16_t)u32;
360		len = 1;
361	} else if (u32 <= UTF32_MAX) {
362		u32 -= SRG_BASE;
363		u16[0] = (u32 >> 10) + HISRG_MIN;
364		u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN;
365		len = 2;
366	} else {
367		*nresult = (size_t)-1;
368		return (EILSEQ);
369	}
370	siz = 0;
371	for (i = 0; i < len; ++i) {
372		err = _citrus_UTF7_utf16tomb(ei, s, n, u16[i], psenc, &nr);
373		if (err != 0)
374			return (err); /* XXX: state has been modified */
375		s += nr;
376		n -= nr;
377		siz += nr;
378	}
379	*nresult = siz;
380
381	return (0);
382}
383
384static int
385/* ARGSUSED */
386_citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei __unused,
387    char * __restrict s, size_t n, _UTF7State * __restrict psenc,
388    size_t * __restrict nresult)
389{
390	int bits, pos;
391
392	if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
393		return (EINVAL);
394
395	if (psenc->mode) {
396		if (psenc->bits > 0) {
397			if (n-- < 1)
398				return (E2BIG);
399			bits = BASE64_BIT - psenc->bits;
400			pos = (psenc->cache << bits) & BASE64_MAX;
401			psenc->ch[psenc->chlen++] = base64[pos];
402			psenc->ch[psenc->chlen++] = BASE64_OUT;
403			psenc->bits = psenc->cache = 0;
404		}
405		psenc->mode = 0;
406	}
407	if (psenc->bits != 0)
408		return (EINVAL);
409	if (n-- < 1)
410		return (E2BIG);
411
412	*nresult = (size_t)psenc->chlen;
413	if (psenc->chlen > 0) {
414		memcpy(s, psenc->ch, psenc->chlen);
415		psenc->chlen = 0;
416	}
417
418	return (0);
419}
420
421static __inline int
422/*ARGSUSED*/
423_citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei __unused,
424    _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
425{
426
427	*csid = 0;
428	*idx = (_index_t)wc;
429
430	return (0);
431}
432
433static __inline int
434/*ARGSUSED*/
435_citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei __unused,
436    wchar_t * __restrict wc, _csid_t csid, _index_t idx)
437{
438
439	if (csid != 0)
440		return (EILSEQ);
441	*wc = (wchar_t)idx;
442
443	return (0);
444}
445
446static __inline int
447/*ARGSUSED*/
448_citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo * __restrict ei __unused,
449    _UTF7State * __restrict psenc, int * __restrict rstate)
450{
451
452	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
453	    _STDENC_SDGEN_INCOMPLETE_CHAR;
454	return (0);
455}
456
457static void
458/*ARGSUSED*/
459_citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei __unused)
460{
461
462	/* ei seems to be unused */
463}
464
465static int
466/*ARGSUSED*/
467_citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei,
468    const void * __restrict var __unused, size_t lenvar __unused)
469{
470	const char *s;
471
472	memset(ei, 0, sizeof(*ei));
473
474#define FILL(str, flag)				\
475do {						\
476	for (s = str; *s != '\0'; s++)		\
477		ei->cell[*s & 0x7f] |= flag;	\
478} while (/*CONSTCOND*/0)
479
480	FILL(base64, (s - base64) + 1);
481	FILL(direct, EI_DIRECT);
482	FILL(option, EI_OPTION);
483	FILL(spaces, EI_SPACE);
484
485	return (0);
486}
487
488/* ----------------------------------------------------------------------
489 * public interface for stdenc
490 */
491
492_CITRUS_STDENC_DECLS(UTF7);
493_CITRUS_STDENC_DEF_OPS(UTF7);
494
495#include "citrus_stdenc_template.h"
496