xref: /freebsd/lib/libiconv_modules/UES/citrus_ues.c (revision 3416500aef140042c64bc149cb1ec6620483bc44)
1 /* $FreeBSD$ */
2 /* $NetBSD: citrus_ues.c,v 1.3 2012/02/12 13:51:29 wiz Exp $ */
3 
4 /*-
5  * Copyright (c)2006 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 #include <sys/cdefs.h>
31 
32 #include <assert.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <wchar.h>
40 
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_bcs.h"
44 #include "citrus_module.h"
45 #include "citrus_stdenc.h"
46 #include "citrus_ues.h"
47 
48 typedef struct {
49 	size_t	 mb_cur_max;
50 	int	 mode;
51 #define MODE_C99	1
52 } _UESEncodingInfo;
53 
54 typedef struct {
55 	int	 chlen;
56 	char	 ch[12];
57 } _UESState;
58 
59 #define _CEI_TO_EI(_cei_)               (&(_cei_)->ei)
60 #define _CEI_TO_STATE(_cei_, _func_)    (_cei_)->states.s_##_func_
61 
62 #define _FUNCNAME(m)			_citrus_UES_##m
63 #define _ENCODING_INFO			_UESEncodingInfo
64 #define _ENCODING_STATE			_UESState
65 #define _ENCODING_MB_CUR_MAX(_ei_)	(_ei_)->mb_cur_max
66 #define _ENCODING_IS_STATE_DEPENDENT		0
67 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_)	0
68 
69 static __inline void
70 /*ARGSUSED*/
71 _citrus_UES_init_state(_UESEncodingInfo * __restrict ei __unused,
72     _UESState * __restrict psenc)
73 {
74 
75 	psenc->chlen = 0;
76 }
77 
78 #if 0
79 static __inline void
80 /*ARGSUSED*/
81 _citrus_UES_pack_state(_UESEncodingInfo * __restrict ei __unused,
82     void *__restrict pspriv, const _UESState * __restrict psenc)
83 {
84 
85 	memcpy(pspriv, (const void *)psenc, sizeof(*psenc));
86 }
87 
88 static __inline void
89 /*ARGSUSED*/
90 _citrus_UES_unpack_state(_UESEncodingInfo * __restrict ei __unused,
91     _UESState * __restrict psenc, const void * __restrict pspriv)
92 {
93 
94 	memcpy((void *)psenc, pspriv, sizeof(*psenc));
95 }
96 #endif
97 
98 static __inline int
99 to_int(int ch)
100 {
101 
102 	if (ch >= '0' && ch <= '9')
103 		return (ch - '0');
104 	else if (ch >= 'A' && ch <= 'F')
105 		return ((ch - 'A') + 10);
106 	else if (ch >= 'a' && ch <= 'f')
107 		return ((ch - 'a') + 10);
108 	return (-1);
109 }
110 
111 #define ESCAPE		'\\'
112 #define UCS2_ESC	'u'
113 #define UCS4_ESC	'U'
114 
115 #define UCS2_BIT	16
116 #define UCS4_BIT	32
117 #define BMP_MAX		UINT32_C(0xFFFF)
118 #define UCS2_MAX	UINT32_C(0x10FFFF)
119 #define UCS4_MAX	UINT32_C(0x7FFFFFFF)
120 
121 static const char *xdig = "0123456789abcdef";
122 
123 static __inline int
124 to_str(char *s, wchar_t wc, int bit)
125 {
126 	char *p;
127 
128 	p = s;
129 	*p++ = ESCAPE;
130 	switch (bit) {
131 	case UCS2_BIT:
132 		*p++ = UCS2_ESC;
133 		break;
134 	case UCS4_BIT:
135 		*p++ = UCS4_ESC;
136 		break;
137 	default:
138 		abort();
139 	}
140 	do {
141 		*p++ = xdig[(wc >> (bit -= 4)) & 0xF];
142 	} while (bit > 0);
143 	return (p - s);
144 }
145 
146 static __inline bool
147 is_hi_surrogate(wchar_t wc)
148 {
149 
150 	return (wc >= 0xD800 && wc <= 0xDBFF);
151 }
152 
153 static __inline bool
154 is_lo_surrogate(wchar_t wc)
155 {
156 
157 	return (wc >= 0xDC00 && wc <= 0xDFFF);
158 }
159 
160 static __inline wchar_t
161 surrogate_to_ucs(wchar_t hi, wchar_t lo)
162 {
163 
164 	hi -= 0xD800;
165 	lo -= 0xDC00;
166 	return ((hi << 10 | lo) + 0x10000);
167 }
168 
169 static __inline void
170 ucs_to_surrogate(wchar_t wc, wchar_t * __restrict hi, wchar_t * __restrict lo)
171 {
172 
173 	wc -= 0x10000;
174 	*hi = (wc >> 10) + 0xD800;
175 	*lo = (wc & 0x3FF) + 0xDC00;
176 }
177 
178 static __inline bool
179 is_basic(wchar_t wc)
180 {
181 
182 	return ((uint32_t)wc <= 0x9F && wc != 0x24 && wc != 0x40 &&
183 	    wc != 0x60);
184 }
185 
186 static int
187 _citrus_UES_mbrtowc_priv(_UESEncodingInfo * __restrict ei,
188     wchar_t * __restrict pwc, char ** __restrict s, size_t n,
189     _UESState * __restrict psenc, size_t * __restrict nresult)
190 {
191 	char *s0;
192 	int ch, head, num, tail;
193 	wchar_t hi, wc;
194 
195 	if (*s == NULL) {
196 		_citrus_UES_init_state(ei, psenc);
197 		*nresult = 0;
198 		return (0);
199 	}
200 	s0 = *s;
201 
202 	hi = (wchar_t)0;
203 	tail = 0;
204 
205 surrogate:
206 	wc = (wchar_t)0;
207 	head = tail;
208 	if (psenc->chlen == head) {
209 		if (n-- < 1)
210 			goto restart;
211 		psenc->ch[psenc->chlen++] = *s0++;
212 	}
213 	ch = (unsigned char)psenc->ch[head++];
214 	if (ch == ESCAPE) {
215 		if (psenc->chlen == head) {
216 			if (n-- < 1)
217 				goto restart;
218 			psenc->ch[psenc->chlen++] = *s0++;
219 		}
220 		switch (psenc->ch[head]) {
221 		case UCS2_ESC:
222 			tail += 6;
223 			break;
224 		case UCS4_ESC:
225 			if (ei->mode & MODE_C99) {
226 				tail = 10;
227 				break;
228 			}
229 		/*FALLTHROUGH*/
230 		default:
231 			tail = 0;
232 		}
233 		++head;
234 	}
235 	for (; head < tail; ++head) {
236 		if (psenc->chlen == head) {
237 			if (n-- < 1) {
238 restart:
239 				*s = s0;
240 				*nresult = (size_t)-2;
241 				return (0);
242 			}
243 			psenc->ch[psenc->chlen++] = *s0++;
244 		}
245 		num = to_int((int)(unsigned char)psenc->ch[head]);
246 		if (num < 0) {
247 			tail = 0;
248 			break;
249 		}
250 		wc = (wc << 4) | num;
251 	}
252 	head = 0;
253 	switch (tail) {
254 	case 0:
255 		break;
256 	case 6:
257 		if (hi != (wchar_t)0)
258 			break;
259 		if ((ei->mode & MODE_C99) == 0) {
260 			if (is_hi_surrogate(wc) != 0) {
261 				hi = wc;
262 				goto surrogate;
263 			}
264 			if ((uint32_t)wc <= 0x7F /* XXX */ ||
265 			    is_lo_surrogate(wc) != 0)
266 				break;
267 			goto done;
268 		}
269 	/*FALLTHROUGH*/
270 	case 10:
271 		if (is_basic(wc) == 0 && (uint32_t)wc <= UCS4_MAX &&
272 		    is_hi_surrogate(wc) == 0 && is_lo_surrogate(wc) == 0)
273 			goto done;
274 		*nresult = (size_t)-1;
275 		return (EILSEQ);
276 	case 12:
277 		if (is_lo_surrogate(wc) == 0)
278 			break;
279 		wc = surrogate_to_ucs(hi, wc);
280 		goto done;
281 	}
282 	ch = (unsigned char)psenc->ch[0];
283 	head = psenc->chlen;
284 	if (--head > 0)
285 		memmove(&psenc->ch[0], &psenc->ch[1], head);
286 	wc = (wchar_t)ch;
287 done:
288 	psenc->chlen = head;
289 	if (pwc != NULL)
290 		*pwc = wc;
291 	*nresult = (size_t)((wc == 0) ? 0 : (s0 - *s));
292 	*s = s0;
293 
294 	return (0);
295 }
296 
297 static int
298 _citrus_UES_wcrtomb_priv(_UESEncodingInfo * __restrict ei,
299     char * __restrict s, size_t n, wchar_t wc,
300     _UESState * __restrict psenc, size_t * __restrict nresult)
301 {
302 	wchar_t hi, lo;
303 
304 	if (psenc->chlen != 0)
305 		return (EINVAL);
306 
307 	if ((ei->mode & MODE_C99) ? is_basic(wc) : (uint32_t)wc <= 0x7F) {
308 		if (n-- < 1)
309 			goto e2big;
310 		psenc->ch[psenc->chlen++] = (char)wc;
311 	} else if ((uint32_t)wc <= BMP_MAX) {
312 		if (n < 6)
313 			goto e2big;
314 		psenc->chlen = to_str(&psenc->ch[0], wc, UCS2_BIT);
315 	} else if ((ei->mode & MODE_C99) == 0 && (uint32_t)wc <= UCS2_MAX) {
316 		if (n < 12)
317 			goto e2big;
318 		ucs_to_surrogate(wc, &hi, &lo);
319 		psenc->chlen += to_str(&psenc->ch[0], hi, UCS2_BIT);
320 		psenc->chlen += to_str(&psenc->ch[6], lo, UCS2_BIT);
321 	} else if ((ei->mode & MODE_C99) && (uint32_t)wc <= UCS4_MAX) {
322 		if (n < 10)
323 			goto e2big;
324 		psenc->chlen = to_str(&psenc->ch[0], wc, UCS4_BIT);
325 	} else {
326 		*nresult = (size_t)-1;
327 		return (EILSEQ);
328 	}
329 	memcpy(s, psenc->ch, psenc->chlen);
330 	*nresult = psenc->chlen;
331 	psenc->chlen = 0;
332 
333 	return (0);
334 
335 e2big:
336 	*nresult = (size_t)-1;
337 	return (E2BIG);
338 }
339 
340 /*ARGSUSED*/
341 static int
342 _citrus_UES_stdenc_wctocs(_UESEncodingInfo * __restrict ei __unused,
343     _csid_t * __restrict csid, _index_t * __restrict idx, wchar_t wc)
344 {
345 
346 	*csid = 0;
347 	*idx = (_index_t)wc;
348 
349 	return (0);
350 }
351 
352 static __inline int
353 /*ARGSUSED*/
354 _citrus_UES_stdenc_cstowc(_UESEncodingInfo * __restrict ei __unused,
355     wchar_t * __restrict wc, _csid_t csid, _index_t idx)
356 {
357 
358 	if (csid != 0)
359 		return (EILSEQ);
360 	*wc = (wchar_t)idx;
361 
362 	return (0);
363 }
364 
365 static __inline int
366 /*ARGSUSED*/
367 _citrus_UES_stdenc_get_state_desc_generic(_UESEncodingInfo * __restrict ei __unused,
368     _UESState * __restrict psenc, int * __restrict rstate)
369 {
370 
371 	*rstate = (psenc->chlen == 0) ? _STDENC_SDGEN_INITIAL :
372 	    _STDENC_SDGEN_INCOMPLETE_CHAR;
373 	return (0);
374 }
375 
376 static void
377 /*ARGSUSED*/
378 _citrus_UES_encoding_module_uninit(_UESEncodingInfo *ei __unused)
379 {
380 
381 	/* ei seems to be unused */
382 }
383 
384 static int
385 /*ARGSUSED*/
386 _citrus_UES_encoding_module_init(_UESEncodingInfo * __restrict ei,
387     const void * __restrict var, size_t lenvar)
388 {
389 	const char *p;
390 
391 	p = var;
392 	memset((void *)ei, 0, sizeof(*ei));
393 	while (lenvar > 0) {
394 		switch (_bcs_toupper(*p)) {
395 		case 'C':
396 			MATCH(C99, ei->mode |= MODE_C99);
397 			break;
398 		}
399 		++p;
400 		--lenvar;
401 	}
402 	ei->mb_cur_max = (ei->mode & MODE_C99) ? 10 : 12;
403 
404 	return (0);
405 }
406 
407 /* ----------------------------------------------------------------------
408  * public interface for stdenc
409  */
410 
411 _CITRUS_STDENC_DECLS(UES);
412 _CITRUS_STDENC_DEF_OPS(UES);
413 
414 #include "citrus_stdenc_template.h"
415