]>
git.saurik.com Git - apple/libc.git/blob - locale/FreeBSD/gb18030.c
2 * Copyright (c) 2002-2004 Tim J. Robbins
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * PRC National Standard GB 18030-2000 encoding of Chinese text.
29 * See gb18030(5) for details.
32 #include <sys/param.h>
33 __FBSDID("$FreeBSD: src/lib/libc/locale/gb18030.c,v 1.8 2007/10/13 16:28:21 ache Exp $");
35 #include "xlocale_private.h"
44 #define GB18030_MB_CUR_MAX 4
46 static size_t _GB18030_mbrtowc(wchar_t * __restrict
, const char * __restrict
,
47 size_t, mbstate_t * __restrict
, locale_t
);
48 static int _GB18030_mbsinit(const mbstate_t *, locale_t
);
49 static size_t _GB18030_wcrtomb(char * __restrict
, wchar_t,
50 mbstate_t * __restrict
, locale_t
);
58 _GB18030_init(struct __xlocale_st_runelocale
*xrl
)
61 xrl
->__mbrtowc
= _GB18030_mbrtowc
;
62 xrl
->__wcrtomb
= _GB18030_wcrtomb
;
63 xrl
->__mbsinit
= _GB18030_mbsinit
;
64 xrl
->__mb_cur_max
= GB18030_MB_CUR_MAX
;
65 xrl
->__mb_sb_limit
= 128;
71 _GB18030_mbsinit(const mbstate_t *ps
, locale_t loc __unused
)
74 return (ps
== NULL
|| ((const _GB18030State
*)ps
)->count
== 0);
78 _GB18030_mbrtowc(wchar_t * __restrict pwc
, const char * __restrict s
,
79 size_t n
, mbstate_t * __restrict ps
, locale_t loc __unused
)
86 gs
= (_GB18030State
*)ps
;
88 if (gs
->count
< 0 || gs
->count
> sizeof(gs
->bytes
)) {
99 ncopy
= MIN(MIN(n
, GB18030_MB_CUR_MAX
), sizeof(gs
->bytes
) - gs
->count
);
100 memcpy(gs
->bytes
+ gs
->count
, s
, ncopy
);
103 s
= (char *)gs
->bytes
;
107 /* Incomplete multibyte sequence */
111 * Single byte: [00-7f]
112 * Two byte: [81-fe][40-7e,80-fe]
113 * Four byte: [81-fe][30-39][81-fe][30-39]
115 ch
= (unsigned char)*s
++;
119 } else if (ch
>= 0x81 && ch
<= 0xfe) {
123 ch
= (unsigned char)*s
++;
124 if ((ch
>= 0x40 && ch
<= 0x7e) || (ch
>= 0x80 && ch
<= 0xfe)) {
125 wch
= (wch
<< 8) | ch
;
127 } else if (ch
>= 0x30 && ch
<= 0x39) {
129 * Strip high bit off the wide character we will
130 * eventually output so that it is positive when
131 * cast to wint_t on 32-bit twos-complement machines.
133 wch
= ((wch
& 0x7f) << 8) | ch
;
136 ch
= (unsigned char)*s
++;
137 if (ch
< 0x81 || ch
> 0xfe)
139 wch
= (wch
<< 8) | ch
;
142 ch
= (unsigned char)*s
++;
143 if (ch
< 0x30 || ch
> 0x39)
145 wch
= (wch
<< 8) | ch
;
155 return (wch
== L
'\0' ? 0 : len
- ocount
);
162 _GB18030_wcrtomb(char * __restrict s
, wchar_t wc
, mbstate_t * __restrict ps
, locale_t loc __unused
)
168 gs
= (_GB18030State
*)ps
;
170 if (gs
->count
!= 0) {
176 /* Reset to initial shift state (no-op) */
178 if ((wc
& ~0x7fffffff) != 0)
180 if (wc
& 0x7f000000) {
181 /* Replace high bit that mbrtowc() removed. */
183 c
= (wc
>> 24) & 0xff;
184 if (c
< 0x81 || c
> 0xfe)
187 c
= (wc
>> 16) & 0xff;
188 if (c
< 0x30 || c
> 0x39)
191 c
= (wc
>> 8) & 0xff;
192 if (c
< 0x81 || c
> 0xfe)
196 if (c
< 0x30 || c
> 0x39)
200 } else if (wc
& 0x00ff0000)
202 else if (wc
& 0x0000ff00) {
203 c
= (wc
>> 8) & 0xff;
204 if (c
< 0x81 || c
> 0xfe)
208 if (c
< 0x40 || c
== 0x7f || c
== 0xff)
212 } else if (wc
<= 0x7f) {