1 : /**********************************************************************
2 : utf16_le.c - Oniguruma (regular expression library)
3 : **********************************************************************/
4 : /*-
5 : * Copyright (c) 2002-2009 K.Kosako <sndgk393 AT ybb DOT ne DOT jp>
6 : * All rights reserved.
7 : *
8 : * Redistribution and use in source and binary forms, with or without
9 : * modification, are permitted provided that the following conditions
10 : * are met:
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.
16 : *
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
27 : * SUCH DAMAGE.
28 : */
29 :
30 : #include "regenc.h"
31 :
32 : #define UTF16_IS_SURROGATE_FIRST(c) (c >= 0xd8 && c <= 0xdb)
33 : #define UTF16_IS_SURROGATE_SECOND(c) (c >= 0xdc && c <= 0xdf)
34 :
35 : static const int EncLen_UTF16[] = {
36 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
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, 4, 4, 4, 4, 2, 2, 2, 2,
50 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
51 : 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
52 : };
53 :
54 : static int
55 : utf16le_code_to_mbclen(OnigCodePoint code)
56 0 : {
57 0 : return (code > 0xffff ? 4 : 2);
58 : }
59 :
60 : static int
61 : utf16le_mbc_enc_len(const UChar* p)
62 0 : {
63 0 : return EncLen_UTF16[*(p+1)];
64 : }
65 :
66 : static int
67 : utf16le_is_mbc_newline(const UChar* p, const UChar* end)
68 0 : {
69 0 : if (p + 1 < end) {
70 0 : if (*p == 0x0a && *(p+1) == 0x00)
71 0 : return 1;
72 : #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
73 : if ((*p == 0x0d || *p == 0x85) && *(p+1) == 0x00)
74 : return 1;
75 : if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28))
76 : return 1;
77 : #endif
78 : }
79 0 : return 0;
80 : }
81 :
82 : static OnigCodePoint
83 : utf16le_mbc_to_code(const UChar* p, const UChar* end)
84 0 : {
85 : OnigCodePoint code;
86 0 : UChar c0 = *p;
87 0 : UChar c1 = *(p+1);
88 :
89 0 : if (UTF16_IS_SURROGATE_FIRST(c1)) {
90 0 : code = ((((c1 - 0xd8) << 2) + ((c0 & 0xc0) >> 6) + 1) << 16)
91 : + ((((c0 & 0x3f) << 2) + (p[3] - 0xdc)) << 8)
92 : + p[2];
93 : }
94 : else {
95 0 : code = c1 * 256 + p[0];
96 : }
97 0 : return code;
98 : }
99 :
100 : static int
101 : utf16le_code_to_mbc(OnigCodePoint code, UChar *buf)
102 0 : {
103 0 : UChar* p = buf;
104 :
105 0 : if (code > 0xffff) {
106 : unsigned int plane, high;
107 :
108 0 : plane = code >> 16;
109 0 : high = (code & 0xff00) >> 8;
110 :
111 0 : *p++ = ((plane & 0x03) << 6) + (high >> 2);
112 0 : *p++ = (plane >> 2) + 0xd8;
113 0 : *p++ = (UChar )(code & 0xff);
114 0 : *p = (high & 0x02) + 0xdc;
115 0 : return 4;
116 : }
117 : else {
118 0 : *p++ = (UChar )(code & 0xff);
119 0 : *p++ = (UChar )((code & 0xff00) >> 8);
120 0 : return 2;
121 : }
122 : }
123 :
124 : static int
125 : utf16le_mbc_to_normalize(OnigAmbigType flag, const UChar** pp, const UChar* end,
126 : UChar* lower)
127 0 : {
128 0 : const UChar* p = *pp;
129 :
130 0 : if (*(p+1) == 0) {
131 0 : if (end > p + 3 &&
132 : (flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0 &&
133 : ((*p == 's' && *(p+2) == 's') ||
134 : ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
135 : (*p == 'S' && *(p+2) == 'S'))) &&
136 : *(p+3) == 0) {
137 0 : *lower++ = 0xdf;
138 0 : *lower = '\0';
139 0 : (*pp) += 4;
140 0 : return 2;
141 : }
142 :
143 0 : *(lower+1) = '\0';
144 0 : if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
145 : ONIGENC_IS_MBC_ASCII(p)) ||
146 : ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
147 : !ONIGENC_IS_MBC_ASCII(p))) {
148 0 : *lower = ONIGENC_ISO_8859_1_TO_LOWER_CASE(*p);
149 : }
150 : else {
151 0 : *lower = *p;
152 : }
153 0 : (*pp) += 2;
154 0 : return 2; /* return byte length of converted char to lower */
155 : }
156 : else {
157 0 : int len = EncLen_UTF16[*(p+1)];
158 0 : if (lower != p) {
159 : int i;
160 0 : for (i = 0; i < len; i++) {
161 0 : *lower++ = *p++;
162 : }
163 : }
164 0 : (*pp) += len;
165 0 : return len; /* return byte length of converted char to lower */
166 : }
167 : }
168 :
169 : static int
170 : utf16le_is_mbc_ambiguous(OnigAmbigType flag, const UChar** pp, const UChar* end)
171 0 : {
172 0 : const UChar* p = *pp;
173 :
174 0 : (*pp) += EncLen_UTF16[*(p+1)];
175 :
176 0 : if (*(p+1) == 0) {
177 : int c, v;
178 :
179 0 : if ((flag & ONIGENC_AMBIGUOUS_MATCH_COMPOUND) != 0) {
180 0 : if (end > p + 3 &&
181 : ((*p == 's' && *(p+2) == 's') ||
182 : ((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
183 : (*p == 'S' && *(p+2) == 'S'))) &&
184 : *(p+3) == 0) {
185 0 : (*pp) += 2;
186 0 : return TRUE;
187 : }
188 : }
189 :
190 0 : if (((flag & ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE) != 0 &&
191 : ONIGENC_IS_MBC_ASCII(p)) ||
192 : ((flag & ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE) != 0 &&
193 : !ONIGENC_IS_MBC_ASCII(p))) {
194 0 : c = *p;
195 0 : v = ONIGENC_IS_UNICODE_ISO_8859_1_CTYPE(c,
196 : (ONIGENC_CTYPE_UPPER | ONIGENC_CTYPE_LOWER));
197 : if ((v | ONIGENC_CTYPE_LOWER) != 0) {
198 : /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
199 0 : if (c >= 0xaa && c <= 0xba)
200 0 : return FALSE;
201 : else
202 0 : return TRUE;
203 : }
204 : return (v != 0 ? TRUE : FALSE);
205 : }
206 : }
207 :
208 0 : return FALSE;
209 : }
210 :
211 : static UChar*
212 : utf16le_left_adjust_char_head(const UChar* start, const UChar* s)
213 0 : {
214 0 : if (s <= start) return (UChar* )s;
215 :
216 0 : if ((s - start) % 2 == 1) {
217 0 : s--;
218 : }
219 :
220 0 : if (UTF16_IS_SURROGATE_SECOND(*(s+1)) && s > start + 1)
221 0 : s -= 2;
222 :
223 0 : return (UChar* )s;
224 : }
225 :
226 : OnigEncodingType OnigEncodingUTF16_LE = {
227 : utf16le_mbc_enc_len,
228 : "UTF-16LE", /* name */
229 : 4, /* max byte length */
230 : 2, /* min byte length */
231 : (ONIGENC_AMBIGUOUS_MATCH_ASCII_CASE |
232 : ONIGENC_AMBIGUOUS_MATCH_NONASCII_CASE |
233 : ONIGENC_AMBIGUOUS_MATCH_COMPOUND),
234 : {
235 : (OnigCodePoint )'\\' /* esc */
236 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar '.' */
237 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anytime '*' */
238 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* zero or one time '?' */
239 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* one or more time '+' */
240 : , (OnigCodePoint )ONIG_INEFFECTIVE_META_CHAR /* anychar anytime */
241 : },
242 : utf16le_is_mbc_newline,
243 : utf16le_mbc_to_code,
244 : utf16le_code_to_mbclen,
245 : utf16le_code_to_mbc,
246 : utf16le_mbc_to_normalize,
247 : utf16le_is_mbc_ambiguous,
248 : onigenc_iso_8859_1_get_all_pair_ambig_codes,
249 : onigenc_ess_tsett_get_all_comp_ambig_codes,
250 : onigenc_unicode_is_code_ctype,
251 : onigenc_unicode_get_ctype_code_range,
252 : utf16le_left_adjust_char_head,
253 : onigenc_always_false_is_allowed_reverse_match
254 : };
|