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