xref: /freebsd/lib/libiconv_modules/UTF7/citrus_utf7.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1 /*	$NetBSD: citrus_utf7.c,v 1.5 2006/08/23 12:57:24 tnozaki Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause
5  *
6  * Copyright (c)2004, 2005 Citrus Project,
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  */
31 
32 #include <sys/cdefs.h>
33 
34 #include <assert.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <stdio.h>
38 #include <stdint.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <wchar.h>
42 
43 #include "citrus_namespace.h"
44 #include "citrus_types.h"
45 #include "citrus_module.h"
46 #include "citrus_stdenc.h"
47 #include "citrus_utf7.h"
48 
49 /* ----------------------------------------------------------------------
50  * private stuffs used by templates
51  */
52 
53 #define EI_MASK		UINT16_C(0xff)
54 #define EI_DIRECT	UINT16_C(0x100)
55 #define EI_OPTION	UINT16_C(0x200)
56 #define EI_SPACE	UINT16_C(0x400)
57 
58 typedef struct {
59 	uint16_t	 cell[0x80];
60 } _UTF7EncodingInfo;
61 
62 typedef struct {
63 	unsigned int
64 		mode: 1,	/* whether base64 mode */
65 		bits: 4,	/* need to hold 0 - 15 */
66 		cache: 22;	/* 22 = BASE64_BIT + UTF16_BIT */
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 	err = _citrus_UTF7_mbtoutf16(ei, &hi, s, n, psenc, &nr);
257 	if (nr == (size_t)-1 || nr == (size_t)-2) {
258 		*nresult = nr;
259 		return (err);
260 	}
261 	if (err != 0)
262 		return (err);
263 	n -= nr;
264 	siz = nr;
265 	if (hi < HISRG_MIN || hi > HISRG_MAX) {
266 		u32 = (uint32_t)hi;
267 		goto done;
268 	}
269 	err = _citrus_UTF7_mbtoutf16(ei, &lo, s, n, psenc, &nr);
270 	if (nr == (size_t)-1 || nr == (size_t)-2) {
271 		psenc->chlen = 1; /* make get_state_desc return incomplete */
272 		*nresult = nr;
273 		return (err);
274 	}
275 	if (err != 0)
276 		return (err);
277 	if (lo < LOSRG_MIN || lo > LOSRG_MAX) {
278 		*nresult = (size_t)-1;
279 		return (EILSEQ);
280 	}
281 	hi -= HISRG_MIN;
282 	lo -= LOSRG_MIN;
283 	u32 = (hi << 10 | lo) + SRG_BASE;
284 	siz += nr;
285 done:
286 	if (pwc != NULL)
287 		*pwc = (wchar_t)u32;
288 	if (u32 == (uint32_t)0) {
289 		*nresult = (size_t)0;
290 		_citrus_UTF7_init_state(ei, psenc);
291 	} else {
292 		*nresult = siz;
293 	}
294 	return (err);
295 }
296 
297 static int
298 _citrus_UTF7_utf16tomb(_UTF7EncodingInfo * __restrict ei,
299     char * __restrict s, size_t n __unused, uint16_t u16,
300     _UTF7State * __restrict psenc, size_t * __restrict nresult)
301 {
302 	int bits, i;
303 
304 	if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
305 		return (EINVAL);
306 
307 	if (ISSAFE(ei, u16)) {
308 		if (psenc->mode) {
309 			if (psenc->bits > 0) {
310 				bits = BASE64_BIT - psenc->bits;
311 				i = (psenc->cache << bits) & BASE64_MAX;
312 				psenc->ch[psenc->chlen++] = base64[i];
313 				psenc->bits = psenc->cache = 0;
314 			}
315 			if (u16 == BASE64_OUT || FINDLEN(ei, u16) >= 0)
316 				psenc->ch[psenc->chlen++] = BASE64_OUT;
317 			psenc->mode = 0;
318 		}
319 		if (psenc->bits != 0)
320 			return (EINVAL);
321 		psenc->ch[psenc->chlen++] = (char)u16;
322 		if (u16 == BASE64_IN)
323 			psenc->ch[psenc->chlen++] = BASE64_OUT;
324 	} else {
325 		if (!psenc->mode) {
326 			if (psenc->bits > 0)
327 				return (EINVAL);
328 			psenc->ch[psenc->chlen++] = BASE64_IN;
329 			psenc->mode = 1;
330 		}
331 		psenc->cache = (psenc->cache << UTF16_BIT) | u16;
332 		bits = UTF16_BIT + psenc->bits;
333 		psenc->bits = bits % BASE64_BIT;
334 		while ((bits -= BASE64_BIT) >= 0) {
335 			i = (psenc->cache >> bits) & BASE64_MAX;
336 			psenc->ch[psenc->chlen++] = base64[i];
337 		}
338 	}
339 	memcpy(s, psenc->ch, psenc->chlen);
340 	*nresult = psenc->chlen;
341 	psenc->chlen = 0;
342 
343 	return (0);
344 }
345 
346 static int
347 _citrus_UTF7_wcrtomb_priv(_UTF7EncodingInfo * __restrict ei,
348     char * __restrict s, size_t n, wchar_t wchar,
349     _UTF7State * __restrict psenc, size_t * __restrict nresult)
350 {
351 	uint32_t u32;
352 	uint16_t u16[2];
353 	int err, i, len;
354 	size_t nr, siz;
355 
356 	u32 = (uint32_t)wchar;
357 	if (u32 <= UTF16_MAX) {
358 		u16[0] = (uint16_t)u32;
359 		len = 1;
360 	} else if (u32 <= UTF32_MAX) {
361 		u32 -= SRG_BASE;
362 		u16[0] = (u32 >> 10) + HISRG_MIN;
363 		u16[1] = ((uint16_t)(u32 & UINT32_C(0x3ff))) + LOSRG_MIN;
364 		len = 2;
365 	} else {
366 		*nresult = (size_t)-1;
367 		return (EILSEQ);
368 	}
369 	siz = 0;
370 	for (i = 0; i < len; ++i) {
371 		err = _citrus_UTF7_utf16tomb(ei, s, n, u16[i], psenc, &nr);
372 		if (err != 0)
373 			return (err); /* XXX: state has been modified */
374 		s += nr;
375 		n -= nr;
376 		siz += nr;
377 	}
378 	*nresult = siz;
379 
380 	return (0);
381 }
382 
383 static int
384 /* ARGSUSED */
385 _citrus_UTF7_put_state_reset(_UTF7EncodingInfo * __restrict ei __unused,
386     char * __restrict s, size_t n, _UTF7State * __restrict psenc,
387     size_t * __restrict nresult)
388 {
389 	int bits, pos;
390 
391 	if (psenc->chlen != 0 || psenc->bits > BASE64_BIT)
392 		return (EINVAL);
393 
394 	if (psenc->mode) {
395 		if (psenc->bits > 0) {
396 			if (n-- < 1)
397 				return (E2BIG);
398 			bits = BASE64_BIT - psenc->bits;
399 			pos = (psenc->cache << bits) & BASE64_MAX;
400 			psenc->ch[psenc->chlen++] = base64[pos];
401 			psenc->ch[psenc->chlen++] = BASE64_OUT;
402 			psenc->bits = psenc->cache = 0;
403 		}
404 		psenc->mode = 0;
405 	}
406 	if (psenc->bits != 0)
407 		return (EINVAL);
408 	if (n-- < 1)
409 		return (E2BIG);
410 
411 	*nresult = (size_t)psenc->chlen;
412 	if (psenc->chlen > 0) {
413 		memcpy(s, psenc->ch, psenc->chlen);
414 		psenc->chlen = 0;
415 	}
416 
417 	return (0);
418 }
419 
420 static __inline int
421 /*ARGSUSED*/
422 _citrus_UTF7_stdenc_wctocs(_UTF7EncodingInfo * __restrict ei __unused,
423     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
424 {
425 
426 	*csid = 0;
427 	*idx = (_index_t)wc;
428 
429 	return (0);
430 }
431 
432 static __inline int
433 /*ARGSUSED*/
434 _citrus_UTF7_stdenc_cstowc(_UTF7EncodingInfo * __restrict ei __unused,
435     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
436 {
437 
438 	if (csid != 0)
439 		return (EILSEQ);
440 	*wc = (wchar_t)idx;
441 
442 	return (0);
443 }
444 
445 static __inline int
446 /*ARGSUSED*/
447 _citrus_UTF7_stdenc_get_state_desc_generic(_UTF7EncodingInfo * __restrict ei __unused,
448     _UTF7State * __restrict psenc, int * __restrict rstate)
449 {
450 
451 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
452 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
453 	return (0);
454 }
455 
456 static void
457 /*ARGSUSED*/
458 _citrus_UTF7_encoding_module_uninit(_UTF7EncodingInfo *ei __unused)
459 {
460 
461 	/* ei seems to be unused */
462 }
463 
464 static int
465 /*ARGSUSED*/
466 _citrus_UTF7_encoding_module_init(_UTF7EncodingInfo * __restrict ei,
467     const void * __restrict var __unused, size_t lenvar __unused)
468 {
469 	const char *s;
470 
471 	memset(ei, 0, sizeof(*ei));
472 
473 #define FILL(str, flag)				\
474 do {						\
475 	for (s = str; *s != '\0'; s++)		\
476 		ei->cell[*s & 0x7f] |= flag;	\
477 } while (/*CONSTCOND*/0)
478 
479 	FILL(base64, (s - base64) + 1);
480 	FILL(direct, EI_DIRECT);
481 	FILL(option, EI_OPTION);
482 	FILL(spaces, EI_SPACE);
483 
484 	return (0);
485 }
486 
487 /* ----------------------------------------------------------------------
488  * public interface for stdenc
489  */
490 
491 _CITRUS_STDENC_DECLS(UTF7);
492 _CITRUS_STDENC_DEF_OPS(UTF7);
493 
494 #include "citrus_stdenc_template.h"
495