]> git.proxmox.com Git - mirror_edk2.git/blob - MdeModulePkg/Universal/RegularExpressionDxe/Oniguruma/utf16_le.c
ArmVirtPkg: Include NVMe support in ArmVirtQemu*
[mirror_edk2.git] / MdeModulePkg / Universal / RegularExpressionDxe / Oniguruma / utf16_le.c
1 /**********************************************************************
2 utf16_le.c - Oniguruma (regular expression library)
3 **********************************************************************/
4 /*-
5 * Copyright (c) 2002-2019 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 #include "regint.h" /* for USE_CALLOUT */
30
31 static int
32 init(void)
33 {
34 #ifdef USE_CALLOUT
35
36 int id;
37 OnigEncoding enc;
38 char* name;
39 unsigned int args[4];
40 OnigValue opts[4];
41
42 enc = ONIG_ENCODING_UTF16_LE;
43
44 name = "F\000A\000I\000L\000\000\000"; BC0_P(name, fail);
45 name = "M\000I\000S\000M\000A\000T\000C\000H\000\000\000"; BC0_P(name, mismatch);
46
47 name = "M\000A\000X\000\000\000";
48 args[0] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
49 args[1] = ONIG_TYPE_CHAR;
50 opts[0].c = 'X';
51 BC_B_O(name, max, 2, args, 1, opts);
52
53 name = "E\000R\000R\000O\000R\000\000\000";
54 args[0] = ONIG_TYPE_LONG; opts[0].l = ONIG_ABORT;
55 BC_P_O(name, error, 1, args, 1, opts);
56
57 name = "C\000O\000U\000N\000T\000\000\000";
58 args[0] = ONIG_TYPE_CHAR; opts[0].c = '>';
59 BC_B_O(name, count, 1, args, 1, opts);
60
61 name = "T\000O\000T\000A\000L\000_\000C\000O\000U\000N\000T\000\000\000";
62 args[0] = ONIG_TYPE_CHAR; opts[0].c = '>';
63 BC_B_O(name, total_count, 1, args, 1, opts);
64
65 name = "C\000M\000P\000\000\000";
66 args[0] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
67 args[1] = ONIG_TYPE_STRING;
68 args[2] = ONIG_TYPE_TAG | ONIG_TYPE_LONG;
69 BC_P(name, cmp, 3, args);
70
71 #endif /* USE_CALLOUT */
72
73 return ONIG_NORMAL;
74 }
75
76 static const int EncLen_UTF16[] = {
77 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
78 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
79 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
80 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
81 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
82 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
83 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
84 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
85 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
86 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
87 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
88 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
89 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
90 2, 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 2, 2, 2,
91 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
92 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
93 };
94
95 static int
96 utf16le_code_to_mbclen(OnigCodePoint code)
97 {
98 if (code > 0xffff) {
99 if (code > 0x10ffff)
100 return ONIGERR_INVALID_CODE_POINT_VALUE;
101 else
102 return 4;
103 }
104 else {
105 return 2;
106 }
107 }
108
109 static int
110 utf16le_mbc_enc_len(const UChar* p)
111 {
112 return EncLen_UTF16[*(p+1)];
113 }
114
115 static int
116 is_valid_mbc_string(const UChar* p, const UChar* end)
117 {
118 const UChar* end1 = end - 1;
119
120 while (p < end1) {
121 int len = utf16le_mbc_enc_len(p);
122 if (len == 4) {
123 if (p + 3 < end && ! UTF16_IS_SURROGATE_SECOND(*(p + 3)))
124 return FALSE;
125 }
126 else
127 if (UTF16_IS_SURROGATE_SECOND(*(p + 1)))
128 return FALSE;
129
130 p += len;
131 }
132
133 if (p != end)
134 return FALSE;
135 else
136 return TRUE;
137 }
138
139 static int
140 utf16le_is_mbc_newline(const UChar* p, const UChar* end)
141 {
142 if (p + 1 < end) {
143 if (*p == 0x0a && *(p+1) == 0x00)
144 return 1;
145 #ifdef USE_UNICODE_ALL_LINE_TERMINATORS
146 if ((
147 #ifndef USE_CRNL_AS_LINE_TERMINATOR
148 *p == 0x0d ||
149 #endif
150 *p == 0x85) && *(p+1) == 0x00)
151 return 1;
152
153 if (*(p+1) == 0x20 && (*p == 0x29 || *p == 0x28))
154 return 1;
155 #endif
156 }
157 return 0;
158 }
159
160 static OnigCodePoint
161 utf16le_mbc_to_code(const UChar* p, const UChar* end ARG_UNUSED)
162 {
163 OnigCodePoint code;
164 UChar c0 = *p;
165 UChar c1 = *(p+1);
166
167 if (UTF16_IS_SURROGATE_FIRST(c1)) {
168 code = ((((c1 - 0xd8) << 2) + ((c0 & 0xc0) >> 6) + 1) << 16)
169 + ((((c0 & 0x3f) << 2) + (p[3] - 0xdc)) << 8)
170 + p[2];
171 }
172 else {
173 code = c1 * 256 + p[0];
174 }
175 return code;
176 }
177
178 static int
179 utf16le_code_to_mbc(OnigCodePoint code, UChar *buf)
180 {
181 UChar* p = buf;
182
183 if (code > 0xffff) {
184 unsigned int plane, high;
185
186 plane = (code >> 16) - 1;
187 high = (code & 0xff00) >> 8;
188
189 *p++ = ((plane & 0x03) << 6) + (high >> 2);
190 *p++ = (plane >> 2) + 0xd8;
191 *p++ = (UChar )(code & 0xff);
192 *p = (high & 0x03) + 0xdc;
193 return 4;
194 }
195 else {
196 *p++ = (UChar )(code & 0xff);
197 *p++ = (UChar )((code & 0xff00) >> 8);
198 return 2;
199 }
200 }
201
202 static int
203 utf16le_mbc_case_fold(OnigCaseFoldType flag,
204 const UChar** pp, const UChar* end, UChar* fold)
205 {
206 const UChar* p = *pp;
207
208 if (ONIGENC_IS_ASCII_CODE(*p) && *(p+1) == 0) {
209 #ifdef USE_UNICODE_CASE_FOLD_TURKISH_AZERI
210 if ((flag & ONIGENC_CASE_FOLD_TURKISH_AZERI) != 0) {
211 if (*p == 0x49) {
212 *fold++ = 0x31;
213 *fold = 0x01;
214 (*pp) += 2;
215 return 2;
216 }
217 }
218 #endif
219
220 *fold++ = ONIGENC_ASCII_CODE_TO_LOWER_CASE(*p);
221 *fold = 0;
222 *pp += 2;
223 return 2;
224 }
225 else
226 return onigenc_unicode_mbc_case_fold(ONIG_ENCODING_UTF16_LE, flag, pp, end,
227 fold);
228 }
229
230 #if 0
231 static int
232 utf16le_is_mbc_ambiguous(OnigCaseFoldType flag, const UChar** pp,
233 const UChar* end)
234 {
235 const UChar* p = *pp;
236
237 (*pp) += EncLen_UTF16[*(p+1)];
238
239 if (*(p+1) == 0) {
240 int c, v;
241
242 if (*p == 0xdf && (flag & INTERNAL_ONIGENC_CASE_FOLD_MULTI_CHAR) != 0) {
243 return TRUE;
244 }
245
246 c = *p;
247 v = ONIGENC_IS_UNICODE_ISO_8859_1_BIT_CTYPE(c,
248 (BIT_CTYPE_UPPER | BIT_CTYPE_LOWER));
249 if ((v | BIT_CTYPE_LOWER) != 0) {
250 /* 0xaa, 0xb5, 0xba are lower case letter, but can't convert. */
251 if (c >= 0xaa && c <= 0xba)
252 return FALSE;
253 else
254 return TRUE;
255 }
256 return (v != 0 ? TRUE : FALSE);
257 }
258
259 return FALSE;
260 }
261 #endif
262
263 static UChar*
264 utf16le_left_adjust_char_head(const UChar* start, const UChar* s)
265 {
266 if (s <= start) return (UChar* )s;
267
268 if ((s - start) % 2 == 1) {
269 s--;
270 }
271
272 if (UTF16_IS_SURROGATE_SECOND(*(s+1)) && s > start + 1 &&
273 UTF16_IS_SURROGATE_FIRST(*(s-1)))
274 s -= 2;
275
276 return (UChar* )s;
277 }
278
279 static int
280 utf16le_get_case_fold_codes_by_str(OnigCaseFoldType flag,
281 const OnigUChar* p, const OnigUChar* end, OnigCaseFoldCodeItem items[])
282 {
283 return onigenc_unicode_get_case_fold_codes_by_str(ONIG_ENCODING_UTF16_LE,
284 flag, p, end, items);
285 }
286
287 OnigEncodingType OnigEncodingUTF16_LE = {
288 utf16le_mbc_enc_len,
289 "UTF-16LE", /* name */
290 4, /* max enc length */
291 2, /* min enc length */
292 utf16le_is_mbc_newline,
293 utf16le_mbc_to_code,
294 utf16le_code_to_mbclen,
295 utf16le_code_to_mbc,
296 utf16le_mbc_case_fold,
297 onigenc_unicode_apply_all_case_fold,
298 utf16le_get_case_fold_codes_by_str,
299 onigenc_unicode_property_name_to_ctype,
300 onigenc_unicode_is_code_ctype,
301 onigenc_utf16_32_get_ctype_code_range,
302 utf16le_left_adjust_char_head,
303 onigenc_always_false_is_allowed_reverse_match,
304 init,
305 0, /* is_initialized */
306 is_valid_mbc_string,
307 ENC_FLAG_UNICODE|ENC_FLAG_SKIP_OFFSET_1,
308 0, 0
309 };