1 /* $NetBSD: citrus_utf1632.c,v 1.12 2012/02/12 13:51:29 wiz Exp $ */
4 * Copyright (c)2003 Citrus Project,
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 #if defined(LIBC_SCCS) && !defined(lint)
31 __RCSID("$NetBSD: citrus_utf1632.c,v 1.12 2012/02/12 13:51:29 wiz Exp $");
32 #endif /* LIBC_SCCS and not lint */
42 #include <sys/types.h>
43 #include <machine/endian.h>
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"
51 #include "citrus_utf1632.h"
54 /* ----------------------------------------------------------------------
55 * private stuffs used by templates
67 #define _ENDIAN_UNKNOWN 0
69 #define _ENDIAN_LITTLE 2
71 #define _MODE_UTF32 0x00000001U
72 #define _MODE_FORCE_ENDIAN 0x00000002U
73 } _UTF1632EncodingInfo
;
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
85 _citrus_UTF1632_init_state(_UTF1632EncodingInfo
*ei
, _UTF1632State
*s
)
87 memset(s
, 0, sizeof(*s
));
91 _citrus_UTF1632_mbrtowc_priv(_UTF1632EncodingInfo
*ei
, wchar_t *pwc
,
92 const char **s
, size_t n
, _UTF1632State
*psenc
,
95 int chlenbak
, endian
, needlen
;
100 _DIAGASSERT(nresult
!= 0);
101 _DIAGASSERT(ei
!= NULL
);
102 _DIAGASSERT(s
!= NULL
);
103 _DIAGASSERT(psenc
!= NULL
);
108 _citrus_UTF1632_init_state(ei
, psenc
);
109 *nresult
= 0; /* state independent */
114 chlenbak
= psenc
->chlen
;
117 if ((ei
->mode
& _MODE_UTF32
) != 0 || chlenbak
>=2)
122 while (chlenbak
< needlen
) {
125 psenc
->ch
[chlenbak
++] = *s0
++;
130 if (psenc
->current_endian
== _ENDIAN_UNKNOWN
) {
131 if ((ei
->mode
& _MODE_FORCE_ENDIAN
) == 0) {
132 /* judge endian marker */
133 if ((ei
->mode
& _MODE_UTF32
) == 0) {
135 if (psenc
->ch
[0]==0xFE && psenc
->ch
[1]==0xFF) {
136 psenc
->current_endian
= _ENDIAN_BIG
;
139 } else if (psenc
->ch
[0]==0xFF && psenc
->ch
[1]==0xFE) {
140 psenc
->current_endian
= _ENDIAN_LITTLE
;
146 if (psenc
->ch
[0]==0x00 && psenc
->ch
[1]==0x00 &&
147 psenc
->ch
[2]==0xFE && psenc
->ch
[3]==0xFF) {
148 psenc
->current_endian
= _ENDIAN_BIG
;
151 } else if (psenc
->ch
[0]==0xFF && psenc
->ch
[1]==0xFE &&
152 psenc
->ch
[2]==0x00 && psenc
->ch
[3]==0x00) {
153 psenc
->current_endian
= _ENDIAN_LITTLE
;
159 psenc
->current_endian
= ei
->preffered_endian
;
161 endian
= psenc
->current_endian
;
164 if ((ei
->mode
& _MODE_UTF32
) == 0) {
170 ((wchar_t)psenc
->ch
[1] << 8));
174 ((wchar_t)psenc
->ch
[0] << 8));
179 if (wc
>= 0xD800 && wc
<= 0xDBFF) {
186 wc
-= 0xD800; /* wc : surrogate high (see above) */
190 if (psenc
->ch
[3]<0xDC || psenc
->ch
[3]>0xDF)
193 wc
|= (wchar_t)(psenc
->ch
[3] & 3) << 8;
196 if (psenc
->ch
[2]<0xDC || psenc
->ch
[2]>0xDF)
199 wc
|= (wchar_t)(psenc
->ch
[2] & 3) << 8;
211 ((wchar_t)psenc
->ch
[1] << 8) |
212 ((wchar_t)psenc
->ch
[2] << 16) |
213 ((wchar_t)psenc
->ch
[3] << 24));
217 ((wchar_t)psenc
->ch
[2] << 8) |
218 ((wchar_t)psenc
->ch
[1] << 16) |
219 ((wchar_t)psenc
->ch
[0] << 24));
224 if (wc
>= 0xD800 && wc
<= 0xDFFF)
237 *nresult
= (size_t)-1;
242 *nresult
= (size_t)-2;
243 psenc
->chlen
= chlenbak
;
249 _citrus_UTF1632_wcrtomb_priv(_UTF1632EncodingInfo
*ei
, char *s
, size_t n
,
250 wchar_t wc
, _UTF1632State
*psenc
,
254 static const char _bom
[4] = {
255 #if BYTE_ORDER == BIG_ENDIAN
256 0x00, 0x00, 0xFE, 0xFF,
258 0xFF, 0xFE, 0x00, 0x00,
261 const char *bom
= &_bom
[0];
264 _DIAGASSERT(ei
!= NULL
);
265 _DIAGASSERT(nresult
!= 0);
266 _DIAGASSERT(s
!= NULL
);
269 if (psenc
->current_endian
== _ENDIAN_UNKNOWN
) {
270 if ((ei
->mode
& _MODE_FORCE_ENDIAN
) == 0) {
271 if (ei
->mode
& _MODE_UTF32
) {
275 #if BYTE_ORDER == BIG_ENDIAN
284 psenc
->current_endian
= ei
->preffered_endian
;
288 if ((ei
->mode
& _MODE_UTF32
)==0) {
298 wc2
= (wc
& 0x3FF) | 0xDC00;
299 wc
= (wc
>>10) | 0xD800;
307 switch (psenc
->current_endian
) {
325 if (wc
>= 0xD800 && wc
<= 0xDFFF)
330 switch (psenc
->current_endian
) {
350 *nresult
= (size_t)-1;
353 *nresult
= (size_t)-1;
358 parse_variable(_UTF1632EncodingInfo
* __restrict ei
,
359 const void * __restrict var
, size_t lenvar
)
361 #define MATCH(x, act) \
363 if (lenvar >= (sizeof(#x)-1) && \
364 _bcs_strncasecmp(p, #x, sizeof(#x)-1) == 0) { \
366 lenvar -= sizeof(#x)-1; \
369 } while (/*CONSTCOND*/0)
376 MATCH(big
, ei
->preffered_endian
= _ENDIAN_BIG
);
380 MATCH(little
, ei
->preffered_endian
= _ENDIAN_LITTLE
);
384 MATCH(force
, ei
->mode
|= _MODE_FORCE_ENDIAN
);
388 MATCH(utf32
, ei
->mode
|= _MODE_UTF32
);
398 _citrus_UTF1632_encoding_module_init(_UTF1632EncodingInfo
* __restrict ei
,
399 const void * __restrict var
,
402 _DIAGASSERT(ei
!= NULL
);
404 memset((void *)ei
, 0, sizeof(*ei
));
406 parse_variable(ei
, var
, lenvar
);
408 if ((ei
->mode
&_MODE_UTF32
)==0)
409 ei
->cur_max
= 6; /* endian + surrogate */
411 ei
->cur_max
= 8; /* endian + normal */
413 if (ei
->preffered_endian
== _ENDIAN_UNKNOWN
) {
414 #if BYTE_ORDER == BIG_ENDIAN
415 ei
->preffered_endian
= _ENDIAN_BIG
;
417 ei
->preffered_endian
= _ENDIAN_LITTLE
;
426 _citrus_UTF1632_encoding_module_uninit(_UTF1632EncodingInfo
*ei
)
432 _citrus_UTF1632_stdenc_wctocs(_UTF1632EncodingInfo
* __restrict ei
,
433 _csid_t
* __restrict csid
,
434 _index_t
* __restrict idx
,
438 _DIAGASSERT(csid
!= NULL
&& idx
!= NULL
);
448 _citrus_UTF1632_stdenc_cstowc(_UTF1632EncodingInfo
* __restrict ei
,
449 _wc_t
* __restrict wc
,
450 _csid_t csid
, _index_t idx
)
453 _DIAGASSERT(wc
!= NULL
);
465 _citrus_UTF1632_stdenc_get_state_desc_generic(_UTF1632EncodingInfo
* __restrict ei
,
466 _UTF1632State
* __restrict psenc
,
467 int * __restrict rstate
)
470 if (psenc
->chlen
== 0)
471 *rstate
= _STDENC_SDGEN_INITIAL
;
473 *rstate
= _STDENC_SDGEN_INCOMPLETE_CHAR
;
478 /* ----------------------------------------------------------------------
479 * public interface for stdenc
482 _CITRUS_STDENC_DECLS(UTF1632
);
483 _CITRUS_STDENC_DEF_OPS(UTF1632
);
485 #include "citrus_stdenc_template.h"