1 /**********************************************************************
2 utf_16be.c - Oniguruma (regular expression library)
3 **********************************************************************/
5 * Copyright (c) 2002-2007 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
32 #define UTF16_IS_SURROGATE_FIRST(c) (((c) & 0xfc) == 0xd8)
33 #define UTF16_IS_SURROGATE_SECOND(c) (((c) & 0xfc) == 0xdc)
34 #define UTF16_IS_SURROGATE(c) (((c) & 0xf8) == 0xd8)
36 static const int EncLen_UTF16
[] = {
37 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
38 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
39 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
40 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
41 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
42 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
43 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
44 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
45 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
46 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
47 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
48 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
49 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
50 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2,
51 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
52 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
56 utf16be_mbc_enc_len(const UChar
* p
, const OnigUChar
* e ARG_UNUSED
,
57 OnigEncoding enc ARG_UNUSED
)
60 if (!UTF16_IS_SURROGATE(byte
)) {
62 return ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(2);
64 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
66 if (UTF16_IS_SURROGATE_FIRST(byte
)) {
68 case 1: return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(3);
69 case 2: return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(2);
71 if (UTF16_IS_SURROGATE_SECOND(p
[2]))
72 return ONIGENC_CONSTRUCT_MBCLEN_NEEDMORE(1);
75 if (UTF16_IS_SURROGATE_SECOND(p
[2]))
76 return ONIGENC_CONSTRUCT_MBCLEN_CHARFOUND(4);
80 return ONIGENC_CONSTRUCT_MBCLEN_INVALID();
84 utf16be_is_mbc_newline(const UChar
* p
, const UChar
* end
,
88 if (*(p
+1) == 0x0a && *p
== 0x00)
90 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
92 #ifndef USE_CRNL_AS_LINE_TERMINATOR
95 *(p
+1) == 0x85) && *p
== 0x00)
97 if (*p
== 0x20 && (*(p
+1) == 0x29 || *(p
+1) == 0x28))
105 utf16be_mbc_to_code(const UChar
* p
, const UChar
* end ARG_UNUSED
,
110 if (UTF16_IS_SURROGATE_FIRST(*p
)) {
111 code
= ((((p
[0] - 0xd8) << 2) + ((p
[1] & 0xc0) >> 6) + 1) << 16)
112 + ((((p
[1] & 0x3f) << 2) + (p
[2] - 0xdc)) << 8)
116 code
= p
[0] * 256 + p
[1];
122 utf16be_code_to_mbclen(OnigCodePoint code
,
125 return (code
> 0xffff ? 4 : 2);
129 utf16be_code_to_mbc(OnigCodePoint code
, UChar
*buf
,
135 unsigned int high
= (code
>> 10) + 0xD7C0;
136 unsigned int low
= (code
& 0x3FF) + 0xDC00;
137 *p
++ = (high
>> 8) & 0xFF;
139 *p
++ = (low
>> 8) & 0xFF;
144 *p
++ = (UChar
)((code
& 0xff00) >> 8);
145 *p
++ = (UChar
)(code
& 0xff);
151 utf16be_mbc_case_fold(OnigCaseFoldType flag
,
152 const UChar
** pp
, const UChar
* end
, UChar
* fold
,
155 const UChar
* p
= *pp
;
157 if (ONIGENC_IS_ASCII_CODE(*(p
+1)) && *p
== 0) {
159 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
160 if ((flag
& ONIGENC_CASE_FOLD_TURKISH_AZERI
) != 0) {
171 *fold
= ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p
);
176 return onigenc_unicode_mbc_case_fold(enc
, flag
,
182 utf16be_is_mbc_ambiguous(OnigCaseFoldType flag
, const UChar
** pp
, const UChar
* end
)
184 const UChar
* p
= *pp
;
186 (*pp
) += EncLen_UTF16
[*p
];
192 if (*p
== 0xdf && (flag
& INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR
) != 0) {
197 v
= ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c
,
198 (BIT_CTYPE_UPPER
| BIT_CTYPE_LOWER
));
200 if ((v
| BIT_CTYPE_LOWER
) != 0) {
201 /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
202 if (c
>= 0xaa && c
<= 0xba)
207 return (v
!= 0 ? TRUE
: FALSE
);
215 utf16be_left_adjust_char_head(const UChar
* start
, const UChar
* s
,
216 OnigEncoding enc ARG_UNUSED
)
218 if (s
<= start
) return (UChar
* )s
;
220 if ((s
- start
) % 2 == 1) {
224 if (UTF16_IS_SURROGATE_SECOND(*s
) && s
> start
+ 1)
231 utf16be_get_case_fold_codes_by_str(OnigCaseFoldType flag
,
232 const OnigUChar
* p
, const OnigUChar
* end
,
233 OnigCaseFoldCodeItem items
[],
236 return onigenc_unicode_get_case_fold_codes_by_str(enc
,
237 flag
, p
, end
, items
);
240 OnigEncodingDefine(utf_16be
, UTF_16BE
) = {
242 "UTF-16BE", /* name */
243 4, /* max byte length */
244 2, /* min byte length */
245 utf16be_is_mbc_newline
,
247 utf16be_code_to_mbclen
,
249 utf16be_mbc_case_fold
,
250 onigenc_unicode_apply_all_case_fold
,
251 utf16be_get_case_fold_codes_by_str
,
252 onigenc_unicode_property_name_to_ctype
,
253 onigenc_unicode_is_code_ctype
,
254 onigenc_utf16_32_get_ctype_code_range
,
255 utf16be_left_adjust_char_head
,
256 onigenc_always_false_is_allowed_reverse_match
258 ENC_ALIAS("UCS-2BE", "UTF-16BE")