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