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