citrus_utf1632.c revision 1.12.34.2 1 /* $NetBSD: citrus_utf1632.c,v 1.12.34.2 2017/07/21 20:22:29 perseant Exp $ */
2
3 /*-
4 * Copyright (c)2003 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 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_utf1632.c,v 1.12.34.2 2017/07/21 20:22:29 perseant Exp $");
32 #endif /* LIBC_SCCS and not lint */
33
34 #include <assert.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <stddef.h>
40 #include <limits.h>
41 #include <wchar.h>
42 #include <sys/types.h>
43 #include <machine/endian.h>
44
45 #include "citrus_namespace.h"
46 #include "citrus_types.h"
47 #include "citrus_module.h"
48 #include "citrus_stdenc.h"
49 #include "citrus_bcs.h"
50
51 #include "citrus_utf1632.h"
52
53
54 /* ----------------------------------------------------------------------
55 * private stuffs used by templates
56 */
57
58 typedef struct {
59 u_int8_t ch[4];
60 int chlen;
61 int current_endian;
62 } _UTF1632State;
63
64 typedef struct {
65 int preffered_endian;
66 unsigned int cur_max;
67 #define _ENDIAN_UNKNOWN 0
68 #define _ENDIAN_BIG 1
69 #define _ENDIAN_LITTLE 2
70 u_int32_t mode;
71 #define _MODE_UTF32 0x00000001U
72 #define _MODE_FORCE_ENDIAN 0x00000002U
73 } _UTF1632EncodingInfo;
74
75 #define _FUNCNAME(m) _citrus_UTF1632_##m
76 #define _ENCODING_INFO _UTF1632EncodingInfo
77 #define _ENCODING_STATE _UTF1632State
78 #define _ENCODING_MB_CUR_MAX(_ei_) ((_ei_)->cur_max)
79 #define _ENCODING_IS_STATE_DEPENDENT 0
80 #define _STATE_NEEDS_EXPLICIT_INIT(_ps_) 0
81
82 #include "citrus_u2k_template.h"
83
84 static __inline void
85 /*ARGSUSED*/
86 _citrus_UTF1632_init_state(_UTF1632EncodingInfo *ei, _UTF1632State *s)
87 {
88 memset(s, 0, sizeof(*s));
89 }
90
91 static int
92 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo *ei, wchar_ucs4_t *pwc,
93 const char **s, size_t n, _UTF1632State *psenc,
94 size_t *nresult)
95 {
96 int chlenbak, endian, needlen;
97 wchar_kuten_t wc;
98 size_t result;
99 const char *s0;
100
101 _DIAGASSERT(nresult != 0);
102 _DIAGASSERT(ei != NULL);
103 _DIAGASSERT(s != NULL);
104 _DIAGASSERT(psenc != NULL);
105
106 s0 = *s;
107
108 if (s0 == NULL) {
109 _citrus_UTF1632_init_state(ei, psenc);
110 *nresult = 0; /* state independent */
111 return (0);
112 }
113
114 result = 0;
115 chlenbak = psenc->chlen;
116
117 refetch:
118 if ((ei->mode & _MODE_UTF32) != 0 || chlenbak>=2)
119 needlen = 4;
120 else
121 needlen = 2;
122
123 while (chlenbak < needlen) {
124 if (n==0)
125 goto restart;
126 psenc->ch[chlenbak++] = *s0++;
127 n--;
128 result++;
129 }
130
131 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
132 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
133 /* judge endian marker */
134 if ((ei->mode & _MODE_UTF32) == 0) {
135 /* UTF16 */
136 if (psenc->ch[0]==0xFE && psenc->ch[1]==0xFF) {
137 psenc->current_endian = _ENDIAN_BIG;
138 chlenbak = 0;
139 goto refetch;
140 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE) {
141 psenc->current_endian = _ENDIAN_LITTLE;
142 chlenbak = 0;
143 goto refetch;
144 }
145 } else {
146 /* UTF32 */
147 if (psenc->ch[0]==0x00 && psenc->ch[1]==0x00 &&
148 psenc->ch[2]==0xFE && psenc->ch[3]==0xFF) {
149 psenc->current_endian = _ENDIAN_BIG;
150 chlenbak = 0;
151 goto refetch;
152 } else if (psenc->ch[0]==0xFF && psenc->ch[1]==0xFE &&
153 psenc->ch[2]==0x00 && psenc->ch[3]==0x00) {
154 psenc->current_endian = _ENDIAN_LITTLE;
155 chlenbak = 0;
156 goto refetch;
157 }
158 }
159 }
160 psenc->current_endian = ei->preffered_endian;
161 }
162 endian = psenc->current_endian;
163
164 /* get wc */
165 if ((ei->mode & _MODE_UTF32) == 0) {
166 /* UTF16 */
167 if (needlen==2) {
168 switch (endian) {
169 case _ENDIAN_LITTLE:
170 wc = (psenc->ch[0] |
171 ((wchar_kuten_t)psenc->ch[1] << 8));
172 break;
173 case _ENDIAN_BIG:
174 wc = (psenc->ch[1] |
175 ((wchar_kuten_t)psenc->ch[0] << 8));
176 break;
177 default:
178 goto ilseq;
179 }
180 if (wc >= 0xD800 && wc <= 0xDBFF) {
181 /* surrogate high */
182 needlen=4;
183 goto refetch;
184 }
185 } else {
186 /* surrogate low */
187 wc -= 0xD800; /* wc : surrogate high (see above) */
188 wc <<= 10;
189 switch (endian) {
190 case _ENDIAN_LITTLE:
191 if (psenc->ch[3]<0xDC || psenc->ch[3]>0xDF)
192 goto ilseq;
193 wc |= psenc->ch[2];
194 wc |= (wchar_kuten_t)(psenc->ch[3] & 3) << 8;
195 break;
196 case _ENDIAN_BIG:
197 if (psenc->ch[2]<0xDC || psenc->ch[2]>0xDF)
198 goto ilseq;
199 wc |= psenc->ch[3];
200 wc |= (wchar_kuten_t)(psenc->ch[2] & 3) << 8;
201 break;
202 default:
203 goto ilseq;
204 }
205 wc += 0x10000;
206 }
207 } else {
208 /* UTF32 */
209 switch (endian) {
210 case _ENDIAN_LITTLE:
211 wc = (psenc->ch[0] |
212 ((wchar_kuten_t)psenc->ch[1] << 8) |
213 ((wchar_kuten_t)psenc->ch[2] << 16) |
214 ((wchar_kuten_t)psenc->ch[3] << 24));
215 break;
216 case _ENDIAN_BIG:
217 wc = (psenc->ch[3] |
218 ((wchar_kuten_t)psenc->ch[2] << 8) |
219 ((wchar_kuten_t)psenc->ch[1] << 16) |
220 ((wchar_kuten_t)psenc->ch[0] << 24));
221 break;
222 default:
223 goto ilseq;
224 }
225 if (wc >= 0xD800 && wc <= 0xDFFF)
226 goto ilseq;
227 }
228
229 _citrus_UTF1632_kt2ucs(ei, pwc, wc);
230 psenc->chlen = 0;
231 *nresult = result;
232 *s = s0;
233
234 return (0);
235
236 ilseq:
237 *nresult = (size_t)-1;
238 psenc->chlen = 0;
239 return (EILSEQ);
240
241 restart:
242 *nresult = (size_t)-2;
243 psenc->chlen = chlenbak;
244 *s = s0;
245 return (0);
246 }
247
248 static int
249 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo *ei, char *s, size_t n,
250 wchar_ucs4_t wc, _UTF1632State *psenc,
251 size_t *nresult)
252 {
253 wchar_kuten_t wc2;
254 static const char _bom[4] = {
255 #if BYTE_ORDER == BIG_ENDIAN
256 0x00, 0x00, 0xFE, 0xFF,
257 #else
258 0xFF, 0xFE, 0x00, 0x00,
259 #endif
260 };
261 const char *bom = &_bom[0];
262 size_t cnt;
263
264 _DIAGASSERT(ei != NULL);
265 _DIAGASSERT(nresult != 0);
266 _DIAGASSERT(s != NULL);
267
268 _citrus_UTF1632_ucs2kt(ei, &wc, wc);
269
270 cnt = (size_t)0;
271 if (psenc->current_endian == _ENDIAN_UNKNOWN) {
272 if ((ei->mode & _MODE_FORCE_ENDIAN) == 0) {
273 if (ei->mode & _MODE_UTF32) {
274 cnt = 4;
275 } else {
276 cnt = 2;
277 #if BYTE_ORDER == BIG_ENDIAN
278 bom += 2;
279 #endif
280 }
281 if (n < cnt)
282 goto e2big;
283 memcpy(s, bom, cnt);
284 s += cnt, n -= cnt;
285 }
286 psenc->current_endian = ei->preffered_endian;
287 }
288
289 wc2 = 0;
290 if ((ei->mode & _MODE_UTF32)==0) {
291 /* UTF16 */
292 if (wc>0xFFFF) {
293 /* surrogate */
294 if (wc>0x10FFFF)
295 goto ilseq;
296 if (n < 4)
297 goto e2big;
298 cnt += 4;
299 wc -= 0x10000;
300 wc2 = (wc & 0x3FF) | 0xDC00;
301 wc = (wc>>10) | 0xD800;
302 } else {
303 if (n < 2)
304 goto e2big;
305 cnt += 2;
306 }
307
308 surrogate:
309 switch (psenc->current_endian) {
310 case _ENDIAN_BIG:
311 s[1] = wc;
312 s[0] = (wc >>= 8);
313 break;
314 case _ENDIAN_LITTLE:
315 s[0] = wc;
316 s[1] = (wc >>= 8);
317 break;
318 }
319 if (wc2!=0) {
320 wc = wc2;
321 wc2 = 0;
322 s += 2;
323 goto surrogate;
324 }
325 } else {
326 /* UTF32 */
327 if (wc >= 0xD800 && wc <= 0xDFFF)
328 goto ilseq;
329 if (n < 4)
330 goto e2big;
331 cnt += 4;
332 switch (psenc->current_endian) {
333 case _ENDIAN_BIG:
334 s[3] = wc;
335 s[2] = (wc >>= 8);
336 s[1] = (wc >>= 8);
337 s[0] = (wc >>= 8);
338 break;
339 case _ENDIAN_LITTLE:
340 s[0] = wc;
341 s[1] = (wc >>= 8);
342 s[2] = (wc >>= 8);
343 s[3] = (wc >>= 8);
344 break;
345 }
346 }
347 *nresult = cnt;
348
349 return 0;
350
351 ilseq:
352 *nresult = (size_t)-1;
353 return EILSEQ;
354 e2big:
355 *nresult = (size_t)-1;
356 return E2BIG;
357 }
358
359 static void
360 parse_variable(_UTF1632EncodingInfo * __restrict ei,
361 const void * __restrict var, size_t lenvar)
362 {
363 #define MATCH(x, act) \
364 do { \
365 if (lenvar >= (sizeof(#x)-1) && \
366 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
367 act; \
368 lenvar -= sizeof(#x)-1; \
369 p += sizeof(#x)-1; \
370 } \
371 } while (/*CONSTCOND*/0)
372 const char *p;
373 p = var;
374 while (lenvar>0) {
375 switch (*p) {
376 case 'B':
377 case 'b':
378 MATCH(big, ei->preffered_endian = _ENDIAN_BIG);
379 break;
380 case 'L':
381 case 'l':
382 MATCH(little, ei->preffered_endian = _ENDIAN_LITTLE);
383 break;
384 case 'F':
385 case 'f':
386 MATCH(force, ei->mode |= _MODE_FORCE_ENDIAN);
387 break;
388 case 'U':
389 case 'u':
390 MATCH(utf32, ei->mode |= _MODE_UTF32);
391 break;
392 }
393 p++;
394 lenvar--;
395 }
396 }
397
398 static int
399 /*ARGSUSED*/
400 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo * __restrict ei,
401 const void * __restrict var,
402 size_t lenvar)
403 {
404 _DIAGASSERT(ei != NULL);
405
406 memset((void *)ei, 0, sizeof(*ei));
407
408 parse_variable(ei, var, lenvar);
409
410 if ((ei->mode&_MODE_UTF32)==0)
411 ei->cur_max = 6; /* endian + surrogate */
412 else
413 ei->cur_max = 8; /* endian + normal */
414
415 if (ei->preffered_endian == _ENDIAN_UNKNOWN) {
416 #if BYTE_ORDER == BIG_ENDIAN
417 ei->preffered_endian = _ENDIAN_BIG;
418 #else
419 ei->preffered_endian = _ENDIAN_LITTLE;
420 #endif
421 }
422
423 return (0);
424 }
425
426 static void
427 /*ARGSUSED*/
428 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo *ei)
429 {
430 }
431
432 static int
433 /*ARGSUSED*/
434 _citrus_UTF1632_stdenc_wctocs(struct _citrus_stdenc *ce,
435 _csid_t * __restrict csid,
436 _index_t * __restrict idx,
437 wchar_kuten_t wc)
438 {
439
440 _DIAGASSERT(csid != NULL && idx != NULL);
441
442 *csid = 0;
443 *idx = (_index_t)wc;
444
445 return (0);
446 }
447
448 static int
449 /*ARGSUSED*/
450 _citrus_UTF1632_stdenc_cstowc(struct _citrus_stdenc *ce,
451 wchar_kuten_t * __restrict wc,
452 _csid_t csid, _index_t idx)
453 {
454
455 _DIAGASSERT(wc != NULL);
456
457 if (csid != 0)
458 return (EILSEQ);
459
460 *wc = (wchar_kuten_t)idx;
461
462 return (0);
463 }
464
465 static __inline int
466 /*ARGSUSED*/
467 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo * __restrict ei,
468 _UTF1632State * __restrict psenc,
469 int * __restrict rstate)
470 {
471
472 if (psenc->chlen == 0)
473 *rstate = _STDENC_SDGEN_INITIAL;
474 else
475 *rstate = _STDENC_SDGEN_INCOMPLETE_CHAR;
476
477 return 0;
478 }
479
480 /* ----------------------------------------------------------------------
481 * public interface for stdenc
482 */
483
484 _CITRUS_STDENC_DECLS(UTF1632);
485 _CITRUS_STDENC_DEF_OPS(UTF1632);
486
487 #include "citrus_stdenc_template.h"
488