Debian Codegraph @

quickjs/libunicode.c

raw symbols SCIP
1 /*
2 * Unicode utilities
3 *
4 * Copyright (c) 2017-2018 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <assert.h>
29
30 #include "cutils.h"
31 #include "libunicode.h"
32 #include "libunicode-table.h"
33
34 enum {
35 RUN_TYPE_U,
36 RUN_TYPE_L,
37 RUN_TYPE_UF,
38 RUN_TYPE_LF,
39 RUN_TYPE_UL,
40 RUN_TYPE_LSU,
41 RUN_TYPE_U2L_399_EXT2,
42 RUN_TYPE_UF_D20,
43 RUN_TYPE_UF_D1_EXT,
44 RUN_TYPE_U_EXT,
45 RUN_TYPE_LF_EXT,
46 RUN_TYPE_UF_EXT2,
47 RUN_TYPE_LF_EXT2,
48 RUN_TYPE_UF_EXT3,
49 };
50
51 static int lre_case_conv1(uint32_t c, int conv_type)
52 {
53 uint32_t res[LRE_CC_RES_LEN_MAX];
54 lre_case_conv(res, c, conv_type);
55 return res[0];
56 }
57
58 /* case conversion using the table entry 'idx' with value 'v' */
59 static int lre_case_conv_entry(uint32_t *res, uint32_t c, int conv_type, uint32_t idx, uint32_t v)
60 {
61 uint32_t code, data, type, a, is_lower;
62 is_lower = (conv_type != 0);
63 type = (v >> (32 - 17 - 7 - 4)) & 0xf;
64 data = ((v & 0xf) << 8) | case_conv_table2[idx];
65 code = v >> (32 - 17);
66 switch(type) {
67 case RUN_TYPE_U:
68 case RUN_TYPE_L:
69 case RUN_TYPE_UF:
70 case RUN_TYPE_LF:
71 if (conv_type == (type & 1) ||
72 (type >= RUN_TYPE_UF && conv_type == 2)) {
73 c = c - code + (case_conv_table1[data] >> (32 - 17));
74 }
75 break;
76 case RUN_TYPE_UL:
77 a = c - code;
78 if ((a & 1) != (1 - is_lower))
79 break;
80 c = (a ^ 1) + code;
81 break;
82 case RUN_TYPE_LSU:
83 a = c - code;
84 if (a == 1) {
85 c += 2 * is_lower - 1;
86 } else if (a == (1 - is_lower) * 2) {
87 c += (2 * is_lower - 1) * 2;
88 }
89 break;
90 case RUN_TYPE_U2L_399_EXT2:
91 if (!is_lower) {
92 res[0] = c - code + case_conv_ext[data >> 6];
93 res[1] = 0x399;
94 return 2;
95 } else {
96 c = c - code + case_conv_ext[data & 0x3f];
97 }
98 break;
99 case RUN_TYPE_UF_D20:
100 if (conv_type == 1)
101 break;
102 c = data + (conv_type == 2) * 0x20;
103 break;
104 case RUN_TYPE_UF_D1_EXT:
105 if (conv_type == 1)
106 break;
107 c = case_conv_ext[data] + (conv_type == 2);
108 break;
109 case RUN_TYPE_U_EXT:
110 case RUN_TYPE_LF_EXT:
111 if (is_lower != (type - RUN_TYPE_U_EXT))
112 break;
113 c = case_conv_ext[data];
114 break;
115 case RUN_TYPE_LF_EXT2:
116 if (!is_lower)
117 break;
118 res[0] = c - code + case_conv_ext[data >> 6];
119 res[1] = case_conv_ext[data & 0x3f];
120 return 2;
121 case RUN_TYPE_UF_EXT2:
122 if (conv_type == 1)
123 break;
124 res[0] = c - code + case_conv_ext[data >> 6];
125 res[1] = case_conv_ext[data & 0x3f];
126 if (conv_type == 2) {
127 /* convert to lower */
128 res[0] = lre_case_conv1(res[0], 1);
129 res[1] = lre_case_conv1(res[1], 1);
130 }
131 return 2;
132 default:
133 case RUN_TYPE_UF_EXT3:
134 if (conv_type == 1)
135 break;
136 res[0] = case_conv_ext[data >> 8];
137 res[1] = case_conv_ext[(data >> 4) & 0xf];
138 res[2] = case_conv_ext[data & 0xf];
139 if (conv_type == 2) {
140 /* convert to lower */
141 res[0] = lre_case_conv1(res[0], 1);
142 res[1] = lre_case_conv1(res[1], 1);
143 res[2] = lre_case_conv1(res[2], 1);
144 }
145 return 3;
146 }
147 res[0] = c;
148 return 1;
149 }
150
151 /* conv_type:
152 0 = to upper
153 1 = to lower
154 2 = case folding (= to lower with modifications)
155 */
156 int lre_case_conv(uint32_t *res, uint32_t c, int conv_type)
157 {
158 if (c < 128) {
159 if (conv_type) {
160 if (c >= 'A' && c <= 'Z') {
161 c = c - 'A' + 'a';
162 }
163 } else {
164 if (c >= 'a' && c <= 'z') {
165 c = c - 'a' + 'A';
166 }
167 }
168 } else {
169 uint32_t v, code, len;
170 int idx, idx_min, idx_max;
171
172 idx_min = 0;
173 idx_max = countof(case_conv_table1) - 1;
174 while (idx_min <= idx_max) {
175 idx = (unsigned)(idx_max + idx_min) / 2;
176 v = case_conv_table1[idx];
177 code = v >> (32 - 17);
178 len = (v >> (32 - 17 - 7)) & 0x7f;
179 if (c < code) {
180 idx_max = idx - 1;
181 } else if (c >= code + len) {
182 idx_min = idx + 1;
183 } else {
184 return lre_case_conv_entry(res, c, conv_type, idx, v);
185 }
186 }
187 }
188 res[0] = c;
189 return 1;
190 }
191
192 static int lre_case_folding_entry(uint32_t c, uint32_t idx, uint32_t v, BOOL is_unicode)
193 {
194 uint32_t res[LRE_CC_RES_LEN_MAX];
195 int len;
196
197 if (is_unicode) {
198 len = lre_case_conv_entry(res, c, 2, idx, v);
199 if (len == 1) {
200 c = res[0];
201 } else {
202 /* handle the few specific multi-character cases (see
203 unicode_gen.c:dump_case_folding_special_cases()) */
204 if (c == 0xfb06) {
205 c = 0xfb05;
206 } else if (c == 0x01fd3) {
207 c = 0x390;
208 } else if (c == 0x01fe3) {
209 c = 0x3b0;
210 }
211 }
212 } else {
213 if (likely(c < 128)) {
214 if (c >= 'a' && c <= 'z')
215 c = c - 'a' + 'A';
216 } else {
217 /* legacy regexp: to upper case if single char >= 128 */
218 len = lre_case_conv_entry(res, c, FALSE, idx, v);
219 if (len == 1 && res[0] >= 128)
220 c = res[0];
221 }
222 }
223 return c;
224 }
225
226 /* JS regexp specific rules for case folding */
227 int lre_canonicalize(uint32_t c, BOOL is_unicode)
228 {
229 if (c < 128) {
230 /* fast case */
231 if (is_unicode) {
232 if (c >= 'A' && c <= 'Z') {
233 c = c - 'A' + 'a';
234 }
235 } else {
236 if (c >= 'a' && c <= 'z') {
237 c = c - 'a' + 'A';
238 }
239 }
240 } else {
241 uint32_t v, code, len;
242 int idx, idx_min, idx_max;
243
244 idx_min = 0;
245 idx_max = countof(case_conv_table1) - 1;
246 while (idx_min <= idx_max) {
247 idx = (unsigned)(idx_max + idx_min) / 2;
248 v = case_conv_table1[idx];
249 code = v >> (32 - 17);
250 len = (v >> (32 - 17 - 7)) & 0x7f;
251 if (c < code) {
252 idx_max = idx - 1;
253 } else if (c >= code + len) {
254 idx_min = idx + 1;
255 } else {
256 return lre_case_folding_entry(c, idx, v, is_unicode);
257 }
258 }
259 }
260 return c;
261 }
262
263 static uint32_t get_le24(const uint8_t *ptr)
264 {
265 #if defined(__x86__) || defined(__x86_64__)
266 return *(uint16_t *)ptr | (ptr[2] << 16);
267 #else
268 return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16);
269 #endif
270 }
271
272 #define UNICODE_INDEX_BLOCK_LEN 32
273
274 /* return -1 if not in table, otherwise the offset in the block */
275 static int get_index_pos(uint32_t *pcode, uint32_t c,
276 const uint8_t *index_table, int index_table_len)
277 {
278 uint32_t code, v;
279 int idx_min, idx_max, idx;
280
281 idx_min = 0;
282 v = get_le24(index_table);
283 code = v & ((1 << 21) - 1);
284 if (c < code) {
285 *pcode = 0;
286 return 0;
287 }
288 idx_max = index_table_len - 1;
289 code = get_le24(index_table + idx_max * 3);
290 if (c >= code)
291 return -1;
292 /* invariant: tab[idx_min] <= c < tab2[idx_max] */
293 while ((idx_max - idx_min) > 1) {
294 idx = (idx_max + idx_min) / 2;
295 v = get_le24(index_table + idx * 3);
296 code = v & ((1 << 21) - 1);
297 if (c < code) {
298 idx_max = idx;
299 } else {
300 idx_min = idx;
301 }
302 }
303 v = get_le24(index_table + idx_min * 3);
304 *pcode = v & ((1 << 21) - 1);
305 return (idx_min + 1) * UNICODE_INDEX_BLOCK_LEN + (v >> 21);
306 }
307
308 static BOOL lre_is_in_table(uint32_t c, const uint8_t *table,
309 const uint8_t *index_table, int index_table_len)
310 {
311 uint32_t code, b, bit;
312 int pos;
313 const uint8_t *p;
314
315 pos = get_index_pos(&code, c, index_table, index_table_len);
316 if (pos < 0)
317 return FALSE; /* outside the table */
318 p = table + pos;
319 bit = 0;
320 for(;;) {
321 b = *p++;
322 if (b < 64) {
323 code += (b >> 3) + 1;
324 if (c < code)
325 return bit;
326 bit ^= 1;
327 code += (b & 7) + 1;
328 } else if (b >= 0x80) {
329 code += b - 0x80 + 1;
330 } else if (b < 0x60) {
331 code += (((b - 0x40) << 8) | p[0]) + 1;
332 p++;
333 } else {
334 code += (((b - 0x60) << 16) | (p[0] << 8) | p[1]) + 1;
335 p += 2;
336 }
337 if (c < code)
338 return bit;
339 bit ^= 1;
340 }
341 }
342
343 BOOL lre_is_cased(uint32_t c)
344 {
345 uint32_t v, code, len;
346 int idx, idx_min, idx_max;
347
348 idx_min = 0;
349 idx_max = countof(case_conv_table1) - 1;
350 while (idx_min <= idx_max) {
351 idx = (unsigned)(idx_max + idx_min) / 2;
352 v = case_conv_table1[idx];
353 code = v >> (32 - 17);
354 len = (v >> (32 - 17 - 7)) & 0x7f;
355 if (c < code) {
356 idx_max = idx - 1;
357 } else if (c >= code + len) {
358 idx_min = idx + 1;
359 } else {
360 return TRUE;
361 }
362 }
363 return lre_is_in_table(c, unicode_prop_Cased1_table,
364 unicode_prop_Cased1_index,
365 sizeof(unicode_prop_Cased1_index) / 3);
366 }
367
368 BOOL lre_is_case_ignorable(uint32_t c)
369 {
370 return lre_is_in_table(c, unicode_prop_Case_Ignorable_table,
371 unicode_prop_Case_Ignorable_index,
372 sizeof(unicode_prop_Case_Ignorable_index) / 3);
373 }
374
375 /* character range */
376
377 static __maybe_unused void cr_dump(CharRange *cr)
378 {
379 int i;
380 for(i = 0; i < cr->len; i++)
381 printf("%d: 0x%04x\n", i, cr->points[i]);
382 }
383
384 static void *cr_default_realloc(void *opaque, void *ptr, size_t size)
385 {
386 return realloc(ptr, size);
387 }
388
389 void cr_init(CharRange *cr, void *mem_opaque, DynBufReallocFunc *realloc_func)
390 {
391 cr->len = cr->size = 0;
392 cr->points = NULL;
393 cr->mem_opaque = mem_opaque;
394 cr->realloc_func = realloc_func ? realloc_func : cr_default_realloc;
395 }
396
397 void cr_free(CharRange *cr)
398 {
399 cr->realloc_func(cr->mem_opaque, cr->points, 0);
400 }
401
402 int cr_realloc(CharRange *cr, int size)
403 {
404 int new_size;
405 uint32_t *new_buf;
406
407 if (size > cr->size) {
408 new_size = max_int(size, cr->size * 3 / 2);
409 new_buf = cr->realloc_func(cr->mem_opaque, cr->points,
410 new_size * sizeof(cr->points[0]));
411 if (!new_buf)
412 return -1;
413 cr->points = new_buf;
414 cr->size = new_size;
415 }
416 return 0;
417 }
418
419 int cr_copy(CharRange *cr, const CharRange *cr1)
420 {
421 if (cr_realloc(cr, cr1->len))
422 return -1;
423 memcpy(cr->points, cr1->points, sizeof(cr->points[0]) * cr1->len);
424 cr->len = cr1->len;
425 return 0;
426 }
427
428 /* merge consecutive intervals and remove empty intervals */
429 static void cr_compress(CharRange *cr)
430 {
431 int i, j, k, len;
432 uint32_t *pt;
433
434 pt = cr->points;
435 len = cr->len;
436 i = 0;
437 j = 0;
438 k = 0;
439 while ((i + 1) < len) {
440 if (pt[i] == pt[i + 1]) {
441 /* empty interval */
442 i += 2;
443 } else {
444 j = i;
445 while ((j + 3) < len && pt[j + 1] == pt[j + 2])
446 j += 2;
447 /* just copy */
448 pt[k] = pt[i];
449 pt[k + 1] = pt[j + 1];
450 k += 2;
451 i = j + 2;
452 }
453 }
454 cr->len = k;
455 }
456
457 /* union or intersection */
458 int cr_op(CharRange *cr, const uint32_t *a_pt, int a_len,
459 const uint32_t *b_pt, int b_len, int op)
460 {
461 int a_idx, b_idx, is_in;
462 uint32_t v;
463
464 a_idx = 0;
465 b_idx = 0;
466 for(;;) {
467 /* get one more point from a or b in increasing order */
468 if (a_idx < a_len && b_idx < b_len) {
469 if (a_pt[a_idx] < b_pt[b_idx]) {
470 goto a_add;
471 } else if (a_pt[a_idx] == b_pt[b_idx]) {
472 v = a_pt[a_idx];
473 a_idx++;
474 b_idx++;
475 } else {
476 goto b_add;
477 }
478 } else if (a_idx < a_len) {
479 a_add:
480 v = a_pt[a_idx++];
481 } else if (b_idx < b_len) {
482 b_add:
483 v = b_pt[b_idx++];
484 } else {
485 break;
486 }
487 /* add the point if the in/out status changes */
488 switch(op) {
489 case CR_OP_UNION:
490 is_in = (a_idx & 1) | (b_idx & 1);
491 break;
492 case CR_OP_INTER:
493 is_in = (a_idx & 1) & (b_idx & 1);
494 break;
495 case CR_OP_XOR:
496 is_in = (a_idx & 1) ^ (b_idx & 1);
497 break;
498 default:
499 abort();
500 }
501 if (is_in != (cr->len & 1)) {
502 if (cr_add_point(cr, v))
503 return -1;
504 }
505 }
506 cr_compress(cr);
507 return 0;
508 }
509
510 int cr_union1(CharRange *cr, const uint32_t *b_pt, int b_len)
511 {
512 CharRange a = *cr;
513 int ret;
514 cr->len = 0;
515 cr->size = 0;
516 cr->points = NULL;
517 ret = cr_op(cr, a.points, a.len, b_pt, b_len, CR_OP_UNION);
518 cr_free(&a);
519 return ret;
520 }
521
522 int cr_invert(CharRange *cr)
523 {
524 int len;
525 len = cr->len;
526 if (cr_realloc(cr, len + 2))
527 return -1;
528 memmove(cr->points + 1, cr->points, len * sizeof(cr->points[0]));
529 cr->points[0] = 0;
530 cr->points[len + 1] = UINT32_MAX;
531 cr->len = len + 2;
532 cr_compress(cr);
533 return 0;
534 }
535
536 #ifdef CONFIG_ALL_UNICODE
537
538 BOOL lre_is_id_start(uint32_t c)
539 {
540 return lre_is_in_table(c, unicode_prop_ID_Start_table,
541 unicode_prop_ID_Start_index,
542 sizeof(unicode_prop_ID_Start_index) / 3);
543 }
544
545 BOOL lre_is_id_continue(uint32_t c)
546 {
547 return lre_is_id_start(c) ||
548 lre_is_in_table(c, unicode_prop_ID_Continue1_table,
549 unicode_prop_ID_Continue1_index,
550 sizeof(unicode_prop_ID_Continue1_index) / 3);
551 }
552
553 #define UNICODE_DECOMP_LEN_MAX 18
554
555 typedef enum {
556 DECOMP_TYPE_C1, /* 16 bit char */
557 DECOMP_TYPE_L1, /* 16 bit char table */
558 DECOMP_TYPE_L2,
559 DECOMP_TYPE_L3,
560 DECOMP_TYPE_L4,
561 DECOMP_TYPE_L5, /* XXX: not used */
562 DECOMP_TYPE_L6, /* XXX: could remove */
563 DECOMP_TYPE_L7, /* XXX: could remove */
564 DECOMP_TYPE_LL1, /* 18 bit char table */
565 DECOMP_TYPE_LL2,
566 DECOMP_TYPE_S1, /* 8 bit char table */
567 DECOMP_TYPE_S2,
568 DECOMP_TYPE_S3,
569 DECOMP_TYPE_S4,
570 DECOMP_TYPE_S5,
571 DECOMP_TYPE_I1, /* increment 16 bit char value */
572 DECOMP_TYPE_I2_0,
573 DECOMP_TYPE_I2_1,
574 DECOMP_TYPE_I3_1,
575 DECOMP_TYPE_I3_2,
576 DECOMP_TYPE_I4_1,
577 DECOMP_TYPE_I4_2,
578 DECOMP_TYPE_B1, /* 16 bit base + 8 bit offset */
579 DECOMP_TYPE_B2,
580 DECOMP_TYPE_B3,
581 DECOMP_TYPE_B4,
582 DECOMP_TYPE_B5,
583 DECOMP_TYPE_B6,
584 DECOMP_TYPE_B7,
585 DECOMP_TYPE_B8,
586 DECOMP_TYPE_B18,
587 DECOMP_TYPE_LS2,
588 DECOMP_TYPE_PAT3,
589 DECOMP_TYPE_S2_UL,
590 DECOMP_TYPE_LS2_UL,
591 } DecompTypeEnum;
592
593 static uint32_t unicode_get_short_code(uint32_t c)
594 {
595 static const uint16_t unicode_short_table[2] = { 0x2044, 0x2215 };
596
597 if (c < 0x80)
598 return c;
599 else if (c < 0x80 + 0x50)
600 return c - 0x80 + 0x300;
601 else
602 return unicode_short_table[c - 0x80 - 0x50];
603 }
604
605 static uint32_t unicode_get_lower_simple(uint32_t c)
606 {
607 if (c < 0x100 || (c >= 0x410 && c <= 0x42f))
608 c += 0x20;
609 else
610 c++;
611 return c;
612 }
613
614 static uint16_t unicode_get16(const uint8_t *p)
615 {
616 return p[0] | (p[1] << 8);
617 }
618
619 static int unicode_decomp_entry(uint32_t *res, uint32_t c,
620 int idx, uint32_t code, uint32_t len,
621 uint32_t type)
622 {
623 uint32_t c1;
624 int l, i, p;
625 const uint8_t *d;
626
627 if (type == DECOMP_TYPE_C1) {
628 res[0] = unicode_decomp_table2[idx];
629 return 1;
630 } else {
631 d = unicode_decomp_data + unicode_decomp_table2[idx];
632 switch(type) {
633 case DECOMP_TYPE_L1:
634 case DECOMP_TYPE_L2:
635 case DECOMP_TYPE_L3:
636 case DECOMP_TYPE_L4:
637 case DECOMP_TYPE_L5:
638 case DECOMP_TYPE_L6:
639 case DECOMP_TYPE_L7:
640 l = type - DECOMP_TYPE_L1 + 1;
641 d += (c - code) * l * 2;
642 for(i = 0; i < l; i++) {
643 if ((res[i] = unicode_get16(d + 2 * i)) == 0)
644 return 0;
645 }
646 return l;
647 case DECOMP_TYPE_LL1:
648 case DECOMP_TYPE_LL2:
649 {
650 uint32_t k, p;
651 l = type - DECOMP_TYPE_LL1 + 1;
652 k = (c - code) * l;
653 p = len * l * 2;
654 for(i = 0; i < l; i++) {
655 c1 = unicode_get16(d + 2 * k) |
656 (((d[p + (k / 4)] >> ((k % 4) * 2)) & 3) << 16);
657 if (!c1)
658 return 0;
659 res[i] = c1;
660 k++;
661 }
662 }
663 return l;
664 case DECOMP_TYPE_S1:
665 case DECOMP_TYPE_S2:
666 case DECOMP_TYPE_S3:
667 case DECOMP_TYPE_S4:
668 case DECOMP_TYPE_S5:
669 l = type - DECOMP_TYPE_S1 + 1;
670 d += (c - code) * l;
671 for(i = 0; i < l; i++) {
672 if ((res[i] = unicode_get_short_code(d[i])) == 0)
673 return 0;
674 }
675 return l;
676 case DECOMP_TYPE_I1:
677 l = 1;
678 p = 0;
679 goto decomp_type_i;
680 case DECOMP_TYPE_I2_0:
681 case DECOMP_TYPE_I2_1:
682 case DECOMP_TYPE_I3_1:
683 case DECOMP_TYPE_I3_2:
684 case DECOMP_TYPE_I4_1:
685 case DECOMP_TYPE_I4_2:
686 l = 2 + ((type - DECOMP_TYPE_I2_0) >> 1);
687 p = ((type - DECOMP_TYPE_I2_0) & 1) + (l > 2);
688 decomp_type_i:
689 for(i = 0; i < l; i++) {
690 c1 = unicode_get16(d + 2 * i);
691 if (i == p)
692 c1 += c - code;
693 res[i] = c1;
694 }
695 return l;
696 case DECOMP_TYPE_B18:
697 l = 18;
698 goto decomp_type_b;
699 case DECOMP_TYPE_B1:
700 case DECOMP_TYPE_B2:
701 case DECOMP_TYPE_B3:
702 case DECOMP_TYPE_B4:
703 case DECOMP_TYPE_B5:
704 case DECOMP_TYPE_B6:
705 case DECOMP_TYPE_B7:
706 case DECOMP_TYPE_B8:
707 l = type - DECOMP_TYPE_B1 + 1;
708 decomp_type_b:
709 {
710 uint32_t c_min;
711 c_min = unicode_get16(d);
712 d += 2 + (c - code) * l;
713 for(i = 0; i < l; i++) {
714 c1 = d[i];
715 if (c1 == 0xff)
716 c1 = 0x20;
717 else
718 c1 += c_min;
719 res[i] = c1;
720 }
721 }
722 return l;
723 case DECOMP_TYPE_LS2:
724 d += (c - code) * 3;
725 if (!(res[0] = unicode_get16(d)))
726 return 0;
727 res[1] = unicode_get_short_code(d[2]);
728 return 2;
729 case DECOMP_TYPE_PAT3:
730 res[0] = unicode_get16(d);
731 res[2] = unicode_get16(d + 2);
732 d += 4 + (c - code) * 2;
733 res[1] = unicode_get16(d);
734 return 3;
735 case DECOMP_TYPE_S2_UL:
736 case DECOMP_TYPE_LS2_UL:
737 c1 = c - code;
738 if (type == DECOMP_TYPE_S2_UL) {
739 d += c1 & ~1;
740 c = unicode_get_short_code(*d);
741 d++;
742 } else {
743 d += (c1 >> 1) * 3;
744 c = unicode_get16(d);
745 d += 2;
746 }
747 if (c1 & 1)
748 c = unicode_get_lower_simple(c);
749 res[0] = c;
750 res[1] = unicode_get_short_code(*d);
751 return 2;
752 }
753 }
754 return 0;
755 }
756
757
758 /* return the length of the decomposition (length <=
759 UNICODE_DECOMP_LEN_MAX) or 0 if no decomposition */
760 static int unicode_decomp_char(uint32_t *res, uint32_t c, BOOL is_compat1)
761 {
762 uint32_t v, type, is_compat, code, len;
763 int idx_min, idx_max, idx;
764
765 idx_min = 0;
766 idx_max = countof(unicode_decomp_table1) - 1;
767 while (idx_min <= idx_max) {
768 idx = (idx_max + idx_min) / 2;
769 v = unicode_decomp_table1[idx];
770 code = v >> (32 - 18);
771 len = (v >> (32 - 18 - 7)) & 0x7f;
772 // printf("idx=%d code=%05x len=%d\n", idx, code, len);
773 if (c < code) {
774 idx_max = idx - 1;
775 } else if (c >= code + len) {
776 idx_min = idx + 1;
777 } else {
778 is_compat = v & 1;
779 if (is_compat1 < is_compat)
780 break;
781 type = (v >> (32 - 18 - 7 - 6)) & 0x3f;
782 return unicode_decomp_entry(res, c, idx, code, len, type);
783 }
784 }
785 return 0;
786 }
787
788 /* return 0 if no pair found */
789 static int unicode_compose_pair(uint32_t c0, uint32_t c1)
790 {
791 uint32_t code, len, type, v, idx1, d_idx, d_offset, ch;
792 int idx_min, idx_max, idx, d;
793 uint32_t pair[2];
794
795 idx_min = 0;
796 idx_max = countof(unicode_comp_table) - 1;
797 while (idx_min <= idx_max) {
798 idx = (idx_max + idx_min) / 2;
799 idx1 = unicode_comp_table[idx];
800
801 /* idx1 represent an entry of the decomposition table */
802 d_idx = idx1 >> 6;
803 d_offset = idx1 & 0x3f;
804 v = unicode_decomp_table1[d_idx];
805 code = v >> (32 - 18);
806 len = (v >> (32 - 18 - 7)) & 0x7f;
807 type = (v >> (32 - 18 - 7 - 6)) & 0x3f;
808 ch = code + d_offset;
809 unicode_decomp_entry(pair, ch, d_idx, code, len, type);
810 d = c0 - pair[0];
811 if (d == 0)
812 d = c1 - pair[1];
813 if (d < 0) {
814 idx_max = idx - 1;
815 } else if (d > 0) {
816 idx_min = idx + 1;
817 } else {
818 return ch;
819 }
820 }
821 return 0;
822 }
823
824 /* return the combining class of character c (between 0 and 255) */
825 static int unicode_get_cc(uint32_t c)
826 {
827 uint32_t code, n, type, cc, c1, b;
828 int pos;
829 const uint8_t *p;
830
831 pos = get_index_pos(&code, c,
832 unicode_cc_index, sizeof(unicode_cc_index) / 3);
833 if (pos < 0)
834 return 0;
835 p = unicode_cc_table + pos;
836 for(;;) {
837 b = *p++;
838 type = b >> 6;
839 n = b & 0x3f;
840 if (n < 48) {
841 } else if (n < 56) {
842 n = (n - 48) << 8;
843 n |= *p++;
844 n += 48;
845 } else {
846 n = (n - 56) << 8;
847 n |= *p++ << 8;
848 n |= *p++;
849 n += 48 + (1 << 11);
850 }
851 if (type <= 1)
852 p++;
853 c1 = code + n + 1;
854 if (c < c1) {
855 switch(type) {
856 case 0:
857 cc = p[-1];
858 break;
859 case 1:
860 cc = p[-1] + c - code;
861 break;
862 case 2:
863 cc = 0;
864 break;
865 default:
866 case 3:
867 cc = 230;
868 break;
869 }
870 return cc;
871 }
872 code = c1;
873 }
874 }
875
876 static void sort_cc(int *buf, int len)
877 {
878 int i, j, k, cc, cc1, start, ch1;
879
880 for(i = 0; i < len; i++) {
881 cc = unicode_get_cc(buf[i]);
882 if (cc != 0) {
883 start = i;
884 j = i + 1;
885 while (j < len) {
886 ch1 = buf[j];
887 cc1 = unicode_get_cc(ch1);
888 if (cc1 == 0)
889 break;
890 k = j - 1;
891 while (k >= start) {
892 if (unicode_get_cc(buf[k]) <= cc1)
893 break;
894 buf[k + 1] = buf[k];
895 k--;
896 }
897 buf[k + 1] = ch1;
898 j++;
899 }
900 #if 0
901 printf("cc:");
902 for(k = start; k < j; k++) {
903 printf(" %3d", unicode_get_cc(buf[k]));
904 }
905 printf("\n");
906 #endif
907 i = j;
908 }
909 }
910 }
911
912 static void to_nfd_rec(DynBuf *dbuf,
913 const int *src, int src_len, int is_compat)
914 {
915 uint32_t c, v;
916 int i, l;
917 uint32_t res[UNICODE_DECOMP_LEN_MAX];
918
919 for(i = 0; i < src_len; i++) {
920 c = src[i];
921 if (c >= 0xac00 && c < 0xd7a4) {
922 /* Hangul decomposition */
923 c -= 0xac00;
924 dbuf_put_u32(dbuf, 0x1100 + c / 588);
925 dbuf_put_u32(dbuf, 0x1161 + (c % 588) / 28);
926 v = c % 28;
927 if (v != 0)
928 dbuf_put_u32(dbuf, 0x11a7 + v);
929 } else {
930 l = unicode_decomp_char(res, c, is_compat);
931 if (l) {
932 to_nfd_rec(dbuf, (int *)res, l, is_compat);
933 } else {
934 dbuf_put_u32(dbuf, c);
935 }
936 }
937 }
938 }
939
940 /* return 0 if not found */
941 static int compose_pair(uint32_t c0, uint32_t c1)
942 {
943 /* Hangul composition */
944 if (c0 >= 0x1100 && c0 < 0x1100 + 19 &&
945 c1 >= 0x1161 && c1 < 0x1161 + 21) {
946 return 0xac00 + (c0 - 0x1100) * 588 + (c1 - 0x1161) * 28;
947 } else if (c0 >= 0xac00 && c0 < 0xac00 + 11172 &&
948 (c0 - 0xac00) % 28 == 0 &&
949 c1 >= 0x11a7 && c1 < 0x11a7 + 28) {
950 return c0 + c1 - 0x11a7;
951 } else {
952 return unicode_compose_pair(c0, c1);
953 }
954 }
955
956 int unicode_normalize(uint32_t **pdst, const uint32_t *src, int src_len,
957 UnicodeNormalizationEnum n_type,
958 void *opaque, DynBufReallocFunc *realloc_func)
959 {
960 int *buf, buf_len, i, p, starter_pos, cc, last_cc, out_len;
961 BOOL is_compat;
962 DynBuf dbuf_s, *dbuf = &dbuf_s;
963
964 is_compat = n_type >> 1;
965
966 dbuf_init2(dbuf, opaque, realloc_func);
967 if (dbuf_realloc(dbuf, sizeof(int) * src_len))
968 goto fail;
969
970 /* common case: latin1 is unaffected by NFC */
971 if (n_type == UNICODE_NFC) {
972 for(i = 0; i < src_len; i++) {
973 if (src[i] >= 0x100)
974 goto not_latin1;
975 }
976 buf = (int *)dbuf->buf;
977 memcpy(buf, src, src_len * sizeof(int));
978 *pdst = (uint32_t *)buf;
979 return src_len;
980 not_latin1: ;
981 }
982
983 to_nfd_rec(dbuf, (const int *)src, src_len, is_compat);
984 if (dbuf_error(dbuf)) {
985 fail:
986 *pdst = NULL;
987 return -1;
988 }
989 buf = (int *)dbuf->buf;
990 buf_len = dbuf->size / sizeof(int);
991
992 sort_cc(buf, buf_len);
993
994 if (buf_len <= 1 || (n_type & 1) != 0) {
995 /* NFD / NFKD */
996 *pdst = (uint32_t *)buf;
997 return buf_len;
998 }
999
1000 i = 1;
1001 out_len = 1;
1002 while (i < buf_len) {
1003 /* find the starter character and test if it is blocked from
1004 the character at 'i' */
1005 last_cc = unicode_get_cc(buf[i]);
1006 starter_pos = out_len - 1;
1007 while (starter_pos >= 0) {
1008 cc = unicode_get_cc(buf[starter_pos]);
1009 if (cc == 0)
1010 break;
1011 if (cc >= last_cc)
1012 goto next;
1013 last_cc = 256;
1014 starter_pos--;
1015 }
1016 if (starter_pos >= 0 &&
1017 (p = compose_pair(buf[starter_pos], buf[i])) != 0) {
1018 buf[starter_pos] = p;
1019 i++;
1020 } else {
1021 next:
1022 buf[out_len++] = buf[i++];
1023 }
1024 }
1025 *pdst = (uint32_t *)buf;
1026 return out_len;
1027 }
1028
1029 /* char ranges for various unicode properties */
1030
1031 static int unicode_find_name(const char *name_table, const char *name)
1032 {
1033 const char *p, *r;
1034 int pos;
1035 size_t name_len, len;
1036
1037 p = name_table;
1038 pos = 0;
1039 name_len = strlen(name);
1040 while (*p) {
1041 for(;;) {
1042 r = strchr(p, ',');
1043 if (!r)
1044 len = strlen(p);
1045 else
1046 len = r - p;
1047 if (len == name_len && !memcmp(p, name, name_len))
1048 return pos;
1049 p += len + 1;
1050 if (!r)
1051 break;
1052 }
1053 pos++;
1054 }
1055 return -1;
1056 }
1057
1058 /* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2
1059 if not found */
1060 int unicode_script(CharRange *cr,
1061 const char *script_name, BOOL is_ext)
1062 {
1063 int script_idx;
1064 const uint8_t *p, *p_end;
1065 uint32_t c, c1, b, n, v, v_len, i, type;
1066 CharRange cr1_s, *cr1;
1067 CharRange cr2_s, *cr2 = &cr2_s;
1068 BOOL is_common;
1069
1070 script_idx = unicode_find_name(unicode_script_name_table, script_name);
1071 if (script_idx < 0)
1072 return -2;
1073 /* Note: we remove the "Unknown" Script */
1074 script_idx += UNICODE_SCRIPT_Unknown + 1;
1075
1076 is_common = (script_idx == UNICODE_SCRIPT_Common ||
1077 script_idx == UNICODE_SCRIPT_Inherited);
1078 if (is_ext) {
1079 cr1 = &cr1_s;
1080 cr_init(cr1, cr->mem_opaque, cr->realloc_func);
1081 cr_init(cr2, cr->mem_opaque, cr->realloc_func);
1082 } else {
1083 cr1 = cr;
1084 }
1085
1086 p = unicode_script_table;
1087 p_end = unicode_script_table + countof(unicode_script_table);
1088 c = 0;
1089 while (p < p_end) {
1090 b = *p++;
1091 type = b >> 7;
1092 n = b & 0x7f;
1093 if (n < 96) {
1094 } else if (n < 112) {
1095 n = (n - 96) << 8;
1096 n |= *p++;
1097 n += 96;
1098 } else {
1099 n = (n - 112) << 16;
1100 n |= *p++ << 8;
1101 n |= *p++;
1102 n += 96 + (1 << 12);
1103 }
1104 if (type == 0)
1105 v = 0;
1106 else
1107 v = *p++;
1108 c1 = c + n + 1;
1109 if (v == script_idx) {
1110 if (cr_add_interval(cr1, c, c1))
1111 goto fail;
1112 }
1113 c = c1;
1114 }
1115
1116 if (is_ext) {
1117 /* add the script extensions */
1118 p = unicode_script_ext_table;
1119 p_end = unicode_script_ext_table + countof(unicode_script_ext_table);
1120 c = 0;
1121 while (p < p_end) {
1122 b = *p++;
1123 if (b < 128) {
1124 n = b;
1125 } else if (b < 128 + 64) {
1126 n = (b - 128) << 8;
1127 n |= *p++;
1128 n += 128;
1129 } else {
1130 n = (b - 128 - 64) << 16;
1131 n |= *p++ << 8;
1132 n |= *p++;
1133 n += 128 + (1 << 14);
1134 }
1135 c1 = c + n + 1;
1136 v_len = *p++;
1137 if (is_common) {
1138 if (v_len != 0) {
1139 if (cr_add_interval(cr2, c, c1))
1140 goto fail;
1141 }
1142 } else {
1143 for(i = 0; i < v_len; i++) {
1144 if (p[i] == script_idx) {
1145 if (cr_add_interval(cr2, c, c1))
1146 goto fail;
1147 break;
1148 }
1149 }
1150 }
1151 p += v_len;
1152 c = c1;
1153 }
1154 if (is_common) {
1155 /* remove all the characters with script extensions */
1156 if (cr_invert(cr2))
1157 goto fail;
1158 if (cr_op(cr, cr1->points, cr1->len, cr2->points, cr2->len,
1159 CR_OP_INTER))
1160 goto fail;
1161 } else {
1162 if (cr_op(cr, cr1->points, cr1->len, cr2->points, cr2->len,
1163 CR_OP_UNION))
1164 goto fail;
1165 }
1166 cr_free(cr1);
1167 cr_free(cr2);
1168 }
1169 return 0;
1170 fail:
1171 if (is_ext) {
1172 cr_free(cr1);
1173 cr_free(cr2);
1174 }
1175 goto fail;
1176 }
1177
1178 #define M(id) (1U << UNICODE_GC_ ## id)
1179
1180 static int unicode_general_category1(CharRange *cr, uint32_t gc_mask)
1181 {
1182 const uint8_t *p, *p_end;
1183 uint32_t c, c0, b, n, v;
1184
1185 p = unicode_gc_table;
1186 p_end = unicode_gc_table + countof(unicode_gc_table);
1187 c = 0;
1188 while (p < p_end) {
1189 b = *p++;
1190 n = b >> 5;
1191 v = b & 0x1f;
1192 if (n == 7) {
1193 n = *p++;
1194 if (n < 128) {
1195 n += 7;
1196 } else if (n < 128 + 64) {
1197 n = (n - 128) << 8;
1198 n |= *p++;
1199 n += 7 + 128;
1200 } else {
1201 n = (n - 128 - 64) << 16;
1202 n |= *p++ << 8;
1203 n |= *p++;
1204 n += 7 + 128 + (1 << 14);
1205 }
1206 }
1207 c0 = c;
1208 c += n + 1;
1209 if (v == 31) {
1210 /* run of Lu / Ll */
1211 b = gc_mask & (M(Lu) | M(Ll));
1212 if (b != 0) {
1213 if (b == (M(Lu) | M(Ll))) {
1214 goto add_range;
1215 } else {
1216 c0 += ((gc_mask & M(Ll)) != 0);
1217 for(; c0 < c; c0 += 2) {
1218 if (cr_add_interval(cr, c0, c0 + 1))
1219 return -1;
1220 }
1221 }
1222 }
1223 } else if ((gc_mask >> v) & 1) {
1224 add_range:
1225 if (cr_add_interval(cr, c0, c))
1226 return -1;
1227 }
1228 }
1229 return 0;
1230 }
1231
1232 static int unicode_prop1(CharRange *cr, int prop_idx)
1233 {
1234 const uint8_t *p, *p_end;
1235 uint32_t c, c0, b, bit;
1236
1237 p = unicode_prop_table[prop_idx];
1238 p_end = p + unicode_prop_len_table[prop_idx];
1239 c = 0;
1240 bit = 0;
1241 while (p < p_end) {
1242 c0 = c;
1243 b = *p++;
1244 if (b < 64) {
1245 c += (b >> 3) + 1;
1246 if (bit) {
1247 if (cr_add_interval(cr, c0, c))
1248 return -1;
1249 }
1250 bit ^= 1;
1251 c0 = c;
1252 c += (b & 7) + 1;
1253 } else if (b >= 0x80) {
1254 c += b - 0x80 + 1;
1255 } else if (b < 0x60) {
1256 c += (((b - 0x40) << 8) | p[0]) + 1;
1257 p++;
1258 } else {
1259 c += (((b - 0x60) << 16) | (p[0] << 8) | p[1]) + 1;
1260 p += 2;
1261 }
1262 if (bit) {
1263 if (cr_add_interval(cr, c0, c))
1264 return -1;
1265 }
1266 bit ^= 1;
1267 }
1268 return 0;
1269 }
1270
1271 #define CASE_U (1 << 0)
1272 #define CASE_L (1 << 1)
1273 #define CASE_F (1 << 2)
1274
1275 /* use the case conversion table to generate range of characters.
1276 CASE_U: set char if modified by uppercasing,
1277 CASE_L: set char if modified by lowercasing,
1278 CASE_F: set char if modified by case folding,
1279 */
1280 static int unicode_case1(CharRange *cr, int case_mask)
1281 {
1282 #define MR(x) (1 << RUN_TYPE_ ## x)
1283 const uint32_t tab_run_mask[3] = {
1284 MR(U) | MR(UF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(UF_D20) |
1285 MR(UF_D1_EXT) | MR(U_EXT) | MR(UF_EXT2) | MR(UF_EXT3),
1286
1287 MR(L) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(LF_EXT2),
1288
1289 MR(UF) | MR(LF) | MR(UL) | MR(LSU) | MR(U2L_399_EXT2) | MR(LF_EXT) | MR(LF_EXT2) | MR(UF_D20) | MR(UF_D1_EXT) | MR(LF_EXT) | MR(UF_EXT2) | MR(UF_EXT3),
1290 };
1291 #undef MR
1292 uint32_t mask, v, code, type, len, i, idx;
1293
1294 if (case_mask == 0)
1295 return 0;
1296 mask = 0;
1297 for(i = 0; i < 3; i++) {
1298 if ((case_mask >> i) & 1)
1299 mask |= tab_run_mask[i];
1300 }
1301 for(idx = 0; idx < countof(case_conv_table1); idx++) {
1302 v = case_conv_table1[idx];
1303 type = (v >> (32 - 17 - 7 - 4)) & 0xf;
1304 code = v >> (32 - 17);
1305 len = (v >> (32 - 17 - 7)) & 0x7f;
1306 if ((mask >> type) & 1) {
1307 // printf("%d: type=%d %04x %04x\n", idx, type, code, code + len - 1);
1308 switch(type) {
1309 case RUN_TYPE_UL:
1310 if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F)))
1311 goto def_case;
1312 code += ((case_mask & CASE_U) != 0);
1313 for(i = 0; i < len; i += 2) {
1314 if (cr_add_interval(cr, code + i, code + i + 1))
1315 return -1;
1316 }
1317 break;
1318 case RUN_TYPE_LSU:
1319 if ((case_mask & CASE_U) && (case_mask & (CASE_L | CASE_F)))
1320 goto def_case;
1321 if (!(case_mask & CASE_U)) {
1322 if (cr_add_interval(cr, code, code + 1))
1323 return -1;
1324 }
1325 if (cr_add_interval(cr, code + 1, code + 2))
1326 return -1;
1327 if (case_mask & CASE_U) {
1328 if (cr_add_interval(cr, code + 2, code + 3))
1329 return -1;
1330 }
1331 break;
1332 default:
1333 def_case:
1334 if (cr_add_interval(cr, code, code + len))
1335 return -1;
1336 break;
1337 }
1338 }
1339 }
1340 return 0;
1341 }
1342
1343 static int point_cmp(const void *p1, const void *p2, void *arg)
1344 {
1345 uint32_t v1 = *(uint32_t *)p1;
1346 uint32_t v2 = *(uint32_t *)p2;
1347 return (v1 > v2) - (v1 < v2);
1348 }
1349
1350 static void cr_sort_and_remove_overlap(CharRange *cr)
1351 {
1352 uint32_t start, end, start1, end1, i, j;
1353
1354 /* the resulting ranges are not necessarily sorted and may overlap */
1355 rqsort(cr->points, cr->len / 2, sizeof(cr->points[0]) * 2, point_cmp, NULL);
1356 j = 0;
1357 for(i = 0; i < cr->len; ) {
1358 start = cr->points[i];
1359 end = cr->points[i + 1];
1360 i += 2;
1361 while (i < cr->len) {
1362 start1 = cr->points[i];
1363 end1 = cr->points[i + 1];
1364 if (start1 > end) {
1365 /* |------|
1366 * |-------| */
1367 break;
1368 } else if (end1 <= end) {
1369 /* |------|
1370 * |--| */
1371 i += 2;
1372 } else {
1373 /* |------|
1374 * |-------| */
1375 end = end1;
1376 i += 2;
1377 }
1378 }
1379 cr->points[j] = start;
1380 cr->points[j + 1] = end;
1381 j += 2;
1382 }
1383 cr->len = j;
1384 }
1385
1386 /* canonicalize a character set using the JS regex case folding rules
1387 (see lre_canonicalize()) */
1388 int cr_regexp_canonicalize(CharRange *cr, BOOL is_unicode)
1389 {
1390 CharRange cr_inter, cr_mask, cr_result, cr_sub;
1391 uint32_t v, code, len, i, idx, start, end, c, d_start, d_end, d;
1392
1393 cr_init(&cr_mask, cr->mem_opaque, cr->realloc_func);
1394 cr_init(&cr_inter, cr->mem_opaque, cr->realloc_func);
1395 cr_init(&cr_result, cr->mem_opaque, cr->realloc_func);
1396 cr_init(&cr_sub, cr->mem_opaque, cr->realloc_func);
1397
1398 if (unicode_case1(&cr_mask, is_unicode ? CASE_F : CASE_U))
1399 goto fail;
1400 if (cr_op(&cr_inter, cr_mask.points, cr_mask.len, cr->points, cr->len, CR_OP_INTER))
1401 goto fail;
1402
1403 if (cr_invert(&cr_mask))
1404 goto fail;
1405 if (cr_op(&cr_sub, cr_mask.points, cr_mask.len, cr->points, cr->len, CR_OP_INTER))
1406 goto fail;
1407
1408 /* cr_inter = cr & cr_mask */
1409 /* cr_sub = cr & ~cr_mask */
1410
1411 /* use the case conversion table to compute the result */
1412 d_start = -1;
1413 d_end = -1;
1414 idx = 0;
1415 v = case_conv_table1[idx];
1416 code = v >> (32 - 17);
1417 len = (v >> (32 - 17 - 7)) & 0x7f;
1418 for(i = 0; i < cr_inter.len; i += 2) {
1419 start = cr_inter.points[i];
1420 end = cr_inter.points[i + 1];
1421
1422 for(c = start; c < end; c++) {
1423 for(;;) {
1424 if (c >= code && c < code + len)
1425 break;
1426 idx++;
1427 assert(idx < countof(case_conv_table1));
1428 v = case_conv_table1[idx];
1429 code = v >> (32 - 17);
1430 len = (v >> (32 - 17 - 7)) & 0x7f;
1431 }
1432 d = lre_case_folding_entry(c, idx, v, is_unicode);
1433 /* try to merge with the current interval */
1434 if (d_start == -1) {
1435 d_start = d;
1436 d_end = d + 1;
1437 } else if (d_end == d) {
1438 d_end++;
1439 } else {
1440 cr_add_interval(&cr_result, d_start, d_end);
1441 d_start = d;
1442 d_end = d + 1;
1443 }
1444 }
1445 }
1446 if (d_start != -1) {
1447 if (cr_add_interval(&cr_result, d_start, d_end))
1448 goto fail;
1449 }
1450
1451 /* the resulting ranges are not necessarily sorted and may overlap */
1452 cr_sort_and_remove_overlap(&cr_result);
1453
1454 /* or with the character not affected by the case folding */
1455 cr->len = 0;
1456 if (cr_op(cr, cr_result.points, cr_result.len, cr_sub.points, cr_sub.len, CR_OP_UNION))
1457 goto fail;
1458
1459 cr_free(&cr_inter);
1460 cr_free(&cr_mask);
1461 cr_free(&cr_result);
1462 cr_free(&cr_sub);
1463 return 0;
1464 fail:
1465 cr_free(&cr_inter);
1466 cr_free(&cr_mask);
1467 cr_free(&cr_result);
1468 cr_free(&cr_sub);
1469 return -1;
1470 }
1471
1472 typedef enum {
1473 POP_GC,
1474 POP_PROP,
1475 POP_CASE,
1476 POP_UNION,
1477 POP_INTER,
1478 POP_XOR,
1479 POP_INVERT,
1480 POP_END,
1481 } PropOPEnum;
1482
1483 #define POP_STACK_LEN_MAX 4
1484
1485 static int unicode_prop_ops(CharRange *cr, ...)
1486 {
1487 va_list ap;
1488 CharRange stack[POP_STACK_LEN_MAX];
1489 int stack_len, op, ret, i;
1490 uint32_t a;
1491
1492 va_start(ap, cr);
1493 stack_len = 0;
1494 for(;;) {
1495 op = va_arg(ap, int);
1496 switch(op) {
1497 case POP_GC:
1498 assert(stack_len < POP_STACK_LEN_MAX);
1499 a = va_arg(ap, int);
1500 cr_init(&stack[stack_len++], cr->mem_opaque, cr->realloc_func);
1501 if (unicode_general_category1(&stack[stack_len - 1], a))
1502 goto fail;
1503 break;
1504 case POP_PROP:
1505 assert(stack_len < POP_STACK_LEN_MAX);
1506 a = va_arg(ap, int);
1507 cr_init(&stack[stack_len++], cr->mem_opaque, cr->realloc_func);
1508 if (unicode_prop1(&stack[stack_len - 1], a))
1509 goto fail;
1510 break;
1511 case POP_CASE:
1512 assert(stack_len < POP_STACK_LEN_MAX);
1513 a = va_arg(ap, int);
1514 cr_init(&stack[stack_len++], cr->mem_opaque, cr->realloc_func);
1515 if (unicode_case1(&stack[stack_len - 1], a))
1516 goto fail;
1517 break;
1518 case POP_UNION:
1519 case POP_INTER:
1520 case POP_XOR:
1521 {
1522 CharRange *cr1, *cr2, *cr3;
1523 assert(stack_len >= 2);
1524 assert(stack_len < POP_STACK_LEN_MAX);
1525 cr1 = &stack[stack_len - 2];
1526 cr2 = &stack[stack_len - 1];
1527 cr3 = &stack[stack_len++];
1528 cr_init(cr3, cr->mem_opaque, cr->realloc_func);
1529 if (cr_op(cr3, cr1->points, cr1->len,
1530 cr2->points, cr2->len, op - POP_UNION + CR_OP_UNION))
1531 goto fail;
1532 cr_free(cr1);
1533 cr_free(cr2);
1534 *cr1 = *cr3;
1535 stack_len -= 2;
1536 }
1537 break;
1538 case POP_INVERT:
1539 assert(stack_len >= 1);
1540 if (cr_invert(&stack[stack_len - 1]))
1541 goto fail;
1542 break;
1543 case POP_END:
1544 goto done;
1545 default:
1546 abort();
1547 }
1548 }
1549 done:
1550 assert(stack_len == 1);
1551 ret = cr_copy(cr, &stack[0]);
1552 cr_free(&stack[0]);
1553 return ret;
1554 fail:
1555 for(i = 0; i < stack_len; i++)
1556 cr_free(&stack[i]);
1557 return -1;
1558 }
1559
1560 static const uint32_t unicode_gc_mask_table[] = {
1561 M(Lu) | M(Ll) | M(Lt), /* LC */
1562 M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo), /* L */
1563 M(Mn) | M(Mc) | M(Me), /* M */
1564 M(Nd) | M(Nl) | M(No), /* N */
1565 M(Sm) | M(Sc) | M(Sk) | M(So), /* S */
1566 M(Pc) | M(Pd) | M(Ps) | M(Pe) | M(Pi) | M(Pf) | M(Po), /* P */
1567 M(Zs) | M(Zl) | M(Zp), /* Z */
1568 M(Cc) | M(Cf) | M(Cs) | M(Co) | M(Cn), /* C */
1569 };
1570
1571 /* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2
1572 if not found */
1573 int unicode_general_category(CharRange *cr, const char *gc_name)
1574 {
1575 int gc_idx;
1576 uint32_t gc_mask;
1577
1578 gc_idx = unicode_find_name(unicode_gc_name_table, gc_name);
1579 if (gc_idx < 0)
1580 return -2;
1581 if (gc_idx <= UNICODE_GC_Co) {
1582 gc_mask = (uint64_t)1 << gc_idx;
1583 } else {
1584 gc_mask = unicode_gc_mask_table[gc_idx - UNICODE_GC_LC];
1585 }
1586 return unicode_general_category1(cr, gc_mask);
1587 }
1588
1589
1590 /* 'cr' must be initialized and empty. Return 0 if OK, -1 if error, -2
1591 if not found */
1592 int unicode_prop(CharRange *cr, const char *prop_name)
1593 {
1594 int prop_idx, ret;
1595
1596 prop_idx = unicode_find_name(unicode_prop_name_table, prop_name);
1597 if (prop_idx < 0)
1598 return -2;
1599 prop_idx += UNICODE_PROP_ASCII_Hex_Digit;
1600
1601 ret = 0;
1602 switch(prop_idx) {
1603 case UNICODE_PROP_ASCII:
1604 if (cr_add_interval(cr, 0x00, 0x7f + 1))
1605 return -1;
1606 break;
1607 case UNICODE_PROP_Any:
1608 if (cr_add_interval(cr, 0x00000, 0x10ffff + 1))
1609 return -1;
1610 break;
1611 case UNICODE_PROP_Assigned:
1612 ret = unicode_prop_ops(cr,
1613 POP_GC, M(Cn),
1614 POP_INVERT,
1615 POP_END);
1616 break;
1617 case UNICODE_PROP_Math:
1618 ret = unicode_prop_ops(cr,
1619 POP_GC, M(Sm),
1620 POP_PROP, UNICODE_PROP_Other_Math,
1621 POP_UNION,
1622 POP_END);
1623 break;
1624 case UNICODE_PROP_Lowercase:
1625 ret = unicode_prop_ops(cr,
1626 POP_GC, M(Ll),
1627 POP_PROP, UNICODE_PROP_Other_Lowercase,
1628 POP_UNION,
1629 POP_END);
1630 break;
1631 case UNICODE_PROP_Uppercase:
1632 ret = unicode_prop_ops(cr,
1633 POP_GC, M(Lu),
1634 POP_PROP, UNICODE_PROP_Other_Uppercase,
1635 POP_UNION,
1636 POP_END);
1637 break;
1638 case UNICODE_PROP_Cased:
1639 ret = unicode_prop_ops(cr,
1640 POP_GC, M(Lu) | M(Ll) | M(Lt),
1641 POP_PROP, UNICODE_PROP_Other_Uppercase,
1642 POP_UNION,
1643 POP_PROP, UNICODE_PROP_Other_Lowercase,
1644 POP_UNION,
1645 POP_END);
1646 break;
1647 case UNICODE_PROP_Alphabetic:
1648 ret = unicode_prop_ops(cr,
1649 POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl),
1650 POP_PROP, UNICODE_PROP_Other_Uppercase,
1651 POP_UNION,
1652 POP_PROP, UNICODE_PROP_Other_Lowercase,
1653 POP_UNION,
1654 POP_PROP, UNICODE_PROP_Other_Alphabetic,
1655 POP_UNION,
1656 POP_END);
1657 break;
1658 case UNICODE_PROP_Grapheme_Base:
1659 ret = unicode_prop_ops(cr,
1660 POP_GC, M(Cc) | M(Cf) | M(Cs) | M(Co) | M(Cn) | M(Zl) | M(Zp) | M(Me) | M(Mn),
1661 POP_PROP, UNICODE_PROP_Other_Grapheme_Extend,
1662 POP_UNION,
1663 POP_INVERT,
1664 POP_END);
1665 break;
1666 case UNICODE_PROP_Grapheme_Extend:
1667 ret = unicode_prop_ops(cr,
1668 POP_GC, M(Me) | M(Mn),
1669 POP_PROP, UNICODE_PROP_Other_Grapheme_Extend,
1670 POP_UNION,
1671 POP_END);
1672 break;
1673 case UNICODE_PROP_XID_Start:
1674 ret = unicode_prop_ops(cr,
1675 POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl),
1676 POP_PROP, UNICODE_PROP_Other_ID_Start,
1677 POP_UNION,
1678 POP_PROP, UNICODE_PROP_Pattern_Syntax,
1679 POP_PROP, UNICODE_PROP_Pattern_White_Space,
1680 POP_UNION,
1681 POP_PROP, UNICODE_PROP_XID_Start1,
1682 POP_UNION,
1683 POP_INVERT,
1684 POP_INTER,
1685 POP_END);
1686 break;
1687 case UNICODE_PROP_XID_Continue:
1688 ret = unicode_prop_ops(cr,
1689 POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl) |
1690 M(Mn) | M(Mc) | M(Nd) | M(Pc),
1691 POP_PROP, UNICODE_PROP_Other_ID_Start,
1692 POP_UNION,
1693 POP_PROP, UNICODE_PROP_Other_ID_Continue,
1694 POP_UNION,
1695 POP_PROP, UNICODE_PROP_Pattern_Syntax,
1696 POP_PROP, UNICODE_PROP_Pattern_White_Space,
1697 POP_UNION,
1698 POP_PROP, UNICODE_PROP_XID_Continue1,
1699 POP_UNION,
1700 POP_INVERT,
1701 POP_INTER,
1702 POP_END);
1703 break;
1704 case UNICODE_PROP_Changes_When_Uppercased:
1705 ret = unicode_case1(cr, CASE_U);
1706 break;
1707 case UNICODE_PROP_Changes_When_Lowercased:
1708 ret = unicode_case1(cr, CASE_L);
1709 break;
1710 case UNICODE_PROP_Changes_When_Casemapped:
1711 ret = unicode_case1(cr, CASE_U | CASE_L | CASE_F);
1712 break;
1713 case UNICODE_PROP_Changes_When_Titlecased:
1714 ret = unicode_prop_ops(cr,
1715 POP_CASE, CASE_U,
1716 POP_PROP, UNICODE_PROP_Changes_When_Titlecased1,
1717 POP_XOR,
1718 POP_END);
1719 break;
1720 case UNICODE_PROP_Changes_When_Casefolded:
1721 ret = unicode_prop_ops(cr,
1722 POP_CASE, CASE_F,
1723 POP_PROP, UNICODE_PROP_Changes_When_Casefolded1,
1724 POP_XOR,
1725 POP_END);
1726 break;
1727 case UNICODE_PROP_Changes_When_NFKC_Casefolded:
1728 ret = unicode_prop_ops(cr,
1729 POP_CASE, CASE_F,
1730 POP_PROP, UNICODE_PROP_Changes_When_NFKC_Casefolded1,
1731 POP_XOR,
1732 POP_END);
1733 break;
1734 #if 0
1735 case UNICODE_PROP_ID_Start:
1736 ret = unicode_prop_ops(cr,
1737 POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl),
1738 POP_PROP, UNICODE_PROP_Other_ID_Start,
1739 POP_UNION,
1740 POP_PROP, UNICODE_PROP_Pattern_Syntax,
1741 POP_PROP, UNICODE_PROP_Pattern_White_Space,
1742 POP_UNION,
1743 POP_INVERT,
1744 POP_INTER,
1745 POP_END);
1746 break;
1747 case UNICODE_PROP_ID_Continue:
1748 ret = unicode_prop_ops(cr,
1749 POP_GC, M(Lu) | M(Ll) | M(Lt) | M(Lm) | M(Lo) | M(Nl) |
1750 M(Mn) | M(Mc) | M(Nd) | M(Pc),
1751 POP_PROP, UNICODE_PROP_Other_ID_Start,
1752 POP_UNION,
1753 POP_PROP, UNICODE_PROP_Other_ID_Continue,
1754 POP_UNION,
1755 POP_PROP, UNICODE_PROP_Pattern_Syntax,
1756 POP_PROP, UNICODE_PROP_Pattern_White_Space,
1757 POP_UNION,
1758 POP_INVERT,
1759 POP_INTER,
1760 POP_END);
1761 break;
1762 case UNICODE_PROP_Case_Ignorable:
1763 ret = unicode_prop_ops(cr,
1764 POP_GC, M(Mn) | M(Cf) | M(Lm) | M(Sk),
1765 POP_PROP, UNICODE_PROP_Case_Ignorable1,
1766 POP_XOR,
1767 POP_END);
1768 break;
1769 #else
1770 /* we use the existing tables */
1771 case UNICODE_PROP_ID_Continue:
1772 ret = unicode_prop_ops(cr,
1773 POP_PROP, UNICODE_PROP_ID_Start,
1774 POP_PROP, UNICODE_PROP_ID_Continue1,
1775 POP_XOR,
1776 POP_END);
1777 break;
1778 #endif
1779 default:
1780 if (prop_idx >= countof(unicode_prop_table))
1781 return -2;
1782 ret = unicode_prop1(cr, prop_idx);
1783 break;
1784 }
1785 return ret;
1786 }
1787
1788 #endif /* CONFIG_ALL_UNICODE */