Coverage Report

Created: 2025-11-15 06:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/quickjs/libregexp.c
Line
Count
Source
1
/*
2
 * Regular Expression Engine
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 <inttypes.h>
28
#include <string.h>
29
#include <assert.h>
30
31
#include "cutils.h"
32
#include "libregexp.h"
33
#include "libunicode.h"
34
35
/*
36
  TODO:
37
38
  - Add a lock step execution mode (=linear time execution guaranteed)
39
    when the regular expression is "simple" i.e. no backreference nor
40
    complicated lookahead. The opcodes are designed for this execution
41
    model.
42
*/
43
44
#if defined(TEST)
45
#define DUMP_REOP
46
#endif
47
48
typedef enum {
49
#define DEF(id, size) REOP_ ## id,
50
#include "libregexp-opcode.h"
51
#undef DEF
52
    REOP_COUNT,
53
} REOPCodeEnum;
54
55
0
#define CAPTURE_COUNT_MAX 255
56
0
#define STACK_SIZE_MAX 255
57
/* must be large enough to have a negligible runtime cost and small
58
   enough to call the interrupt callback often. */
59
3.02M
#define INTERRUPT_COUNTER_INIT 10000
60
61
/* unicode code points */
62
3.03M
#define CP_LS   0x2028
63
2.87k
#define CP_PS   0x2029
64
65
#define TMP_BUF_SIZE 128
66
67
typedef struct {
68
    DynBuf byte_code;
69
    const uint8_t *buf_ptr;
70
    const uint8_t *buf_end;
71
    const uint8_t *buf_start;
72
    int re_flags;
73
    BOOL is_unicode;
74
    BOOL unicode_sets; /* if set, is_unicode is also set */
75
    BOOL ignore_case;
76
    BOOL multi_line;
77
    BOOL dotall;
78
    int capture_count;
79
    int total_capture_count; /* -1 = not computed yet */
80
    int has_named_captures; /* -1 = don't know, 0 = no, 1 = yes */
81
    void *opaque;
82
    DynBuf group_names;
83
    union {
84
        char error_msg[TMP_BUF_SIZE];
85
        char tmp_buf[TMP_BUF_SIZE];
86
    } u;
87
} REParseState;
88
89
typedef struct {
90
#ifdef DUMP_REOP
91
    const char *name;
92
#endif
93
    uint8_t size;
94
} REOpCode;
95
96
static const REOpCode reopcode_info[REOP_COUNT] = {
97
#ifdef DUMP_REOP
98
#define DEF(id, size) { #id, size },
99
#else
100
#define DEF(id, size) { size },
101
#endif
102
#include "libregexp-opcode.h"
103
#undef DEF
104
};
105
106
6.05M
#define RE_HEADER_FLAGS         0
107
6.05M
#define RE_HEADER_CAPTURE_COUNT 2
108
3.02M
#define RE_HEADER_STACK_SIZE    3
109
9
#define RE_HEADER_BYTECODE_LEN  4
110
111
3.02M
#define RE_HEADER_LEN 8
112
113
0
static inline int is_digit(int c) {
114
0
    return c >= '0' && c <= '9';
115
0
}
116
117
/* insert 'len' bytes at position 'pos'. Return < 0 if error. */
118
static int dbuf_insert(DynBuf *s, int pos, int len)
119
0
{
120
0
    if (dbuf_claim(s, len))
121
0
        return -1;
122
0
    memmove(s->buf + pos + len, s->buf + pos, s->size - pos);
123
0
    s->size += len;
124
0
    return 0;
125
0
}
126
127
typedef struct REString {
128
    struct REString *next;
129
    uint32_t hash;
130
    uint32_t len;
131
    uint32_t buf[];
132
} REString;
133
134
typedef struct {
135
    /* the string list is the union of 'char_range' and of the strings
136
       in hash_table[]. The strings in hash_table[] have a length !=
137
       1. */
138
    CharRange cr;
139
    uint32_t n_strings;
140
    uint32_t hash_size;
141
    int hash_bits;
142
    REString **hash_table;
143
} REStringList;
144
145
static uint32_t re_string_hash(int len, const uint32_t *buf)
146
0
{
147
0
    int i;
148
0
    uint32_t h;
149
0
    h = 1;
150
0
    for(i = 0; i < len; i++)
151
0
        h = h * 263 + buf[i];
152
0
    return h * 0x61C88647;
153
0
}
154
155
static void re_string_list_init(REParseState *s1, REStringList *s)
156
0
{
157
0
    cr_init(&s->cr, s1->opaque, lre_realloc);
158
0
    s->n_strings = 0;
159
0
    s->hash_size = 0;
160
0
    s->hash_bits = 0;
161
0
    s->hash_table = NULL;
162
0
}
163
164
static void re_string_list_free(REStringList *s)
165
0
{
166
0
    REString *p, *p_next;
167
0
    int i;
168
0
    for(i = 0; i < s->hash_size; i++) {
169
0
        for(p = s->hash_table[i]; p != NULL; p = p_next) {
170
0
            p_next = p->next;
171
0
            lre_realloc(s->cr.mem_opaque, p, 0);
172
0
        }
173
0
    }
174
0
    lre_realloc(s->cr.mem_opaque, s->hash_table, 0);
175
176
0
    cr_free(&s->cr);
177
0
}
178
179
static void lre_print_char(int c, BOOL is_range)
180
0
{
181
0
    if (c == '\'' || c == '\\' ||
182
0
        (is_range && (c == '-' || c == ']'))) {
183
0
        printf("\\%c", c);
184
0
    } else if (c >= ' ' && c <= 126) {
185
0
        printf("%c", c);
186
0
    } else {
187
0
        printf("\\u{%04x}", c);
188
0
    }
189
0
}
190
191
static __maybe_unused void re_string_list_dump(const char *str, const REStringList *s)
192
0
{
193
0
    REString *p;
194
0
    const CharRange *cr;
195
0
    int i, j, k;
196
0
197
0
    printf("%s:\n", str);
198
0
    printf("  ranges: [");
199
0
    cr = &s->cr;
200
0
    for(i = 0; i < cr->len; i += 2) {
201
0
        lre_print_char(cr->points[i], TRUE);
202
0
        if (cr->points[i] != cr->points[i + 1] - 1) {
203
0
            printf("-");
204
0
            lre_print_char(cr->points[i + 1] - 1, TRUE);
205
0
        }
206
0
    }
207
0
    printf("]\n");
208
0
    
209
0
    j = 0;
210
0
    for(i = 0; i < s->hash_size; i++) {
211
0
        for(p = s->hash_table[i]; p != NULL; p = p->next) {
212
0
            printf("  %d/%d: '", j, s->n_strings);
213
0
            for(k = 0; k < p->len; k++) {
214
0
                lre_print_char(p->buf[k], FALSE);
215
0
            }
216
0
            printf("'\n");
217
0
            j++;
218
0
        }
219
0
    }
220
0
}
221
222
static int re_string_find2(REStringList *s, int len, const uint32_t *buf,
223
                           uint32_t h0, BOOL add_flag)
224
0
{
225
0
    uint32_t h = 0; /* avoid warning */
226
0
    REString *p;
227
0
    if (s->n_strings != 0) {
228
0
        h = h0 >> (32 - s->hash_bits);
229
0
        for(p = s->hash_table[h]; p != NULL; p = p->next) {
230
0
            if (p->hash == h0 && p->len == len &&
231
0
                !memcmp(p->buf, buf, len * sizeof(buf[0]))) {
232
0
                return 1;
233
0
            }
234
0
        }
235
0
    }
236
    /* not found */
237
0
    if (!add_flag)
238
0
        return 0;
239
    /* increase the size of the hash table if needed */
240
0
    if (unlikely((s->n_strings + 1) > s->hash_size)) {
241
0
        REString **new_hash_table, *p_next;
242
0
        int new_hash_bits, i;
243
0
        uint32_t new_hash_size;
244
0
        new_hash_bits = max_int(s->hash_bits + 1, 4);
245
0
        new_hash_size = 1 << new_hash_bits;
246
0
        new_hash_table = lre_realloc(s->cr.mem_opaque, NULL,
247
0
                                     sizeof(new_hash_table[0]) * new_hash_size);
248
0
        if (!new_hash_table)
249
0
            return -1;
250
0
        memset(new_hash_table, 0, sizeof(new_hash_table[0]) * new_hash_size);
251
0
        for(i = 0; i < s->hash_size; i++) {
252
0
            for(p = s->hash_table[i]; p != NULL; p = p_next) {
253
0
                p_next = p->next;
254
0
                h = p->hash >> (32 - new_hash_bits);
255
0
                p->next = new_hash_table[h];
256
0
                new_hash_table[h] = p;
257
0
            }
258
0
        }
259
0
        lre_realloc(s->cr.mem_opaque, s->hash_table, 0);
260
0
        s->hash_bits = new_hash_bits;
261
0
        s->hash_size = new_hash_size;
262
0
        s->hash_table = new_hash_table;
263
0
        h = h0 >> (32 - s->hash_bits);
264
0
    }
265
266
0
    p = lre_realloc(s->cr.mem_opaque, NULL, sizeof(REString) + len * sizeof(buf[0]));
267
0
    if (!p)
268
0
        return -1;
269
0
    p->next = s->hash_table[h];
270
0
    s->hash_table[h] = p;
271
0
    s->n_strings++;
272
0
    p->hash = h0;
273
0
    p->len = len;
274
0
    memcpy(p->buf, buf, sizeof(buf[0]) * len);
275
0
    return 1;
276
0
}
277
278
static int re_string_find(REStringList *s, int len, const uint32_t *buf,
279
                          BOOL add_flag)
280
0
{
281
0
    uint32_t h0;
282
0
    h0 = re_string_hash(len, buf);
283
0
    return re_string_find2(s, len, buf, h0, add_flag);
284
0
}
285
286
/* return -1 if memory error, 0 if OK */
287
static int re_string_add(REStringList *s, int len, const uint32_t *buf)
288
0
{
289
0
    if (len == 1) {
290
0
        return cr_union_interval(&s->cr, buf[0], buf[0]);
291
0
    }
292
0
    if (re_string_find(s, len, buf, TRUE) < 0)
293
0
        return -1;
294
0
    return 0;
295
0
}
296
297
/* a = a op b */
298
static int re_string_list_op(REStringList *a, REStringList *b, int op)
299
0
{
300
0
    int i, ret;
301
0
    REString *p, **pp;
302
303
0
    if (cr_op1(&a->cr, b->cr.points, b->cr.len, op))
304
0
        return -1;
305
306
0
    switch(op) {
307
0
    case CR_OP_UNION:
308
0
        if (b->n_strings != 0) {
309
0
            for(i = 0; i < b->hash_size; i++) {
310
0
                for(p = b->hash_table[i]; p != NULL; p = p->next) {
311
0
                    if (re_string_find2(a, p->len, p->buf, p->hash, TRUE) < 0)
312
0
                        return -1;
313
0
                }
314
0
            }
315
0
        }
316
0
        break;
317
0
    case CR_OP_INTER:
318
0
    case CR_OP_SUB:
319
0
        for(i = 0; i < a->hash_size; i++) {
320
0
            pp = &a->hash_table[i];
321
0
            for(;;) {
322
0
                p = *pp;
323
0
                if (p == NULL)
324
0
                    break;
325
0
                ret = re_string_find2(b, p->len, p->buf, p->hash, FALSE);
326
0
                if (op == CR_OP_SUB)
327
0
                    ret = !ret;
328
0
                if (!ret) {
329
                    /* remove it */
330
0
                    *pp = p->next;
331
0
                    a->n_strings--;
332
0
                    lre_realloc(a->cr.mem_opaque, p, 0);
333
0
                } else {
334
                    /* keep it */
335
0
                    pp = &p->next;
336
0
                }
337
0
            }
338
0
        }
339
0
        break;
340
0
    default:
341
0
        abort();
342
0
    }
343
0
    return 0;
344
0
}
345
346
static int re_string_list_canonicalize(REParseState *s1,
347
                                       REStringList *s, BOOL is_unicode)
348
0
{
349
0
    if (cr_regexp_canonicalize(&s->cr, is_unicode))
350
0
        return -1;
351
0
    if (s->n_strings != 0) {
352
0
        REStringList a_s, *a = &a_s;
353
0
        int i, j;
354
0
        REString *p;
355
        
356
        /* XXX: simplify */
357
0
        re_string_list_init(s1, a);
358
359
0
        a->n_strings = s->n_strings;
360
0
        a->hash_size = s->hash_size;
361
0
        a->hash_bits = s->hash_bits;
362
0
        a->hash_table = s->hash_table;
363
        
364
0
        s->n_strings = 0;
365
0
        s->hash_size = 0;
366
0
        s->hash_bits = 0;
367
0
        s->hash_table = NULL;
368
369
0
        for(i = 0; i < a->hash_size; i++) {
370
0
            for(p = a->hash_table[i]; p != NULL; p = p->next) {
371
0
                for(j = 0; j < p->len; j++) {
372
0
                    p->buf[j] = lre_canonicalize(p->buf[j], is_unicode);
373
0
                }
374
0
                if (re_string_add(s, p->len, p->buf)) {
375
0
                    re_string_list_free(a);
376
0
                    return -1;
377
0
                }
378
0
            }
379
0
        }
380
0
        re_string_list_free(a);
381
0
    }
382
0
    return 0;
383
0
}
384
385
static const uint16_t char_range_d[] = {
386
    1,
387
    0x0030, 0x0039 + 1,
388
};
389
390
/* code point ranges for Zs,Zl or Zp property */
391
static const uint16_t char_range_s[] = {
392
    10,
393
    0x0009, 0x000D + 1,
394
    0x0020, 0x0020 + 1,
395
    0x00A0, 0x00A0 + 1,
396
    0x1680, 0x1680 + 1,
397
    0x2000, 0x200A + 1,
398
    /* 2028;LINE SEPARATOR;Zl;0;WS;;;;;N;;;;; */
399
    /* 2029;PARAGRAPH SEPARATOR;Zp;0;B;;;;;N;;;;; */
400
    0x2028, 0x2029 + 1,
401
    0x202F, 0x202F + 1,
402
    0x205F, 0x205F + 1,
403
    0x3000, 0x3000 + 1,
404
    /* FEFF;ZERO WIDTH NO-BREAK SPACE;Cf;0;BN;;;;;N;BYTE ORDER MARK;;;; */
405
    0xFEFF, 0xFEFF + 1,
406
};
407
408
static const uint16_t char_range_w[] = {
409
    4,
410
    0x0030, 0x0039 + 1,
411
    0x0041, 0x005A + 1,
412
    0x005F, 0x005F + 1,
413
    0x0061, 0x007A + 1,
414
};
415
416
9
#define CLASS_RANGE_BASE 0x40000000
417
418
typedef enum {
419
    CHAR_RANGE_d,
420
    CHAR_RANGE_D,
421
    CHAR_RANGE_s,
422
    CHAR_RANGE_S,
423
    CHAR_RANGE_w,
424
    CHAR_RANGE_W,
425
} CharRangeEnum;
426
427
static const uint16_t * const char_range_table[] = {
428
    char_range_d,
429
    char_range_s,
430
    char_range_w,
431
};
432
433
static int cr_init_char_range(REParseState *s, REStringList *cr, uint32_t c)
434
0
{
435
0
    BOOL invert;
436
0
    const uint16_t *c_pt;
437
0
    int len, i;
438
439
0
    invert = c & 1;
440
0
    c_pt = char_range_table[c >> 1];
441
0
    len = *c_pt++;
442
0
    re_string_list_init(s, cr);
443
0
    for(i = 0; i < len * 2; i++) {
444
0
        if (cr_add_point(&cr->cr, c_pt[i]))
445
0
            goto fail;
446
0
    }
447
0
    if (invert) {
448
0
        if (cr_invert(&cr->cr))
449
0
            goto fail;
450
0
    }
451
0
    return 0;
452
0
 fail:
453
0
    re_string_list_free(cr);
454
0
    return -1;
455
0
}
456
457
#ifdef DUMP_REOP
458
static __maybe_unused void lre_dump_bytecode(const uint8_t *buf,
459
                                                     int buf_len)
460
{
461
    int pos, len, opcode, bc_len, re_flags, i;
462
    uint32_t val;
463
464
    assert(buf_len >= RE_HEADER_LEN);
465
466
    re_flags = lre_get_flags(buf);
467
    bc_len = get_u32(buf + RE_HEADER_BYTECODE_LEN);
468
    assert(bc_len + RE_HEADER_LEN <= buf_len);
469
    printf("flags: 0x%x capture_count=%d stack_size=%d\n",
470
           re_flags, buf[RE_HEADER_CAPTURE_COUNT], buf[RE_HEADER_STACK_SIZE]);
471
    if (re_flags & LRE_FLAG_NAMED_GROUPS) {
472
        const char *p;
473
        p = (char *)buf + RE_HEADER_LEN + bc_len;
474
        printf("named groups: ");
475
        for(i = 1; i < buf[RE_HEADER_CAPTURE_COUNT]; i++) {
476
            if (i != 1)
477
                printf(",");
478
            printf("<%s>", p);
479
            p += strlen(p) + 1;
480
        }
481
        printf("\n");
482
        assert(p == (char *)(buf + buf_len));
483
    }
484
    printf("bytecode_len=%d\n", bc_len);
485
486
    buf += RE_HEADER_LEN;
487
    pos = 0;
488
    while (pos < bc_len) {
489
        printf("%5u: ", pos);
490
        opcode = buf[pos];
491
        len = reopcode_info[opcode].size;
492
        if (opcode >= REOP_COUNT) {
493
            printf(" invalid opcode=0x%02x\n", opcode);
494
            break;
495
        }
496
        if ((pos + len) > bc_len) {
497
            printf(" buffer overflow (opcode=0x%02x)\n", opcode);
498
            break;
499
        }
500
        printf("%s", reopcode_info[opcode].name);
501
        switch(opcode) {
502
        case REOP_char:
503
        case REOP_char_i:
504
            val = get_u16(buf + pos + 1);
505
            if (val >= ' ' && val <= 126)
506
                printf(" '%c'", val);
507
            else
508
                printf(" 0x%04x", val);
509
            break;
510
        case REOP_char32:
511
        case REOP_char32_i:
512
            val = get_u32(buf + pos + 1);
513
            if (val >= ' ' && val <= 126)
514
                printf(" '%c'", val);
515
            else
516
                printf(" 0x%08x", val);
517
            break;
518
        case REOP_goto:
519
        case REOP_split_goto_first:
520
        case REOP_split_next_first:
521
        case REOP_loop:
522
        case REOP_lookahead:
523
        case REOP_negative_lookahead:
524
            val = get_u32(buf + pos + 1);
525
            val += (pos + 5);
526
            printf(" %u", val);
527
            break;
528
        case REOP_simple_greedy_quant:
529
            printf(" %u %u %u %u",
530
                   get_u32(buf + pos + 1) + (pos + 17),
531
                   get_u32(buf + pos + 1 + 4),
532
                   get_u32(buf + pos + 1 + 8),
533
                   get_u32(buf + pos + 1 + 12));
534
            break;
535
        case REOP_save_start:
536
        case REOP_save_end:
537
        case REOP_back_reference:
538
        case REOP_back_reference_i:
539
        case REOP_backward_back_reference:
540
        case REOP_backward_back_reference_i:
541
            printf(" %u", buf[pos + 1]);
542
            break;
543
        case REOP_save_reset:
544
            printf(" %u %u", buf[pos + 1], buf[pos + 2]);
545
            break;
546
        case REOP_push_i32:
547
            val = get_u32(buf + pos + 1);
548
            printf(" %d", val);
549
            break;
550
        case REOP_range:
551
        case REOP_range_i:
552
            {
553
                int n, i;
554
                n = get_u16(buf + pos + 1);
555
                len += n * 4;
556
                for(i = 0; i < n * 2; i++) {
557
                    val = get_u16(buf + pos + 3 + i * 2);
558
                    printf(" 0x%04x", val);
559
                }
560
            }
561
            break;
562
        case REOP_range32:
563
        case REOP_range32_i:
564
            {
565
                int n, i;
566
                n = get_u16(buf + pos + 1);
567
                len += n * 8;
568
                for(i = 0; i < n * 2; i++) {
569
                    val = get_u32(buf + pos + 3 + i * 4);
570
                    printf(" 0x%08x", val);
571
                }
572
            }
573
            break;
574
        default:
575
            break;
576
        }
577
        printf("\n");
578
        pos += len;
579
    }
580
}
581
#endif
582
583
static void re_emit_op(REParseState *s, int op)
584
21
{
585
21
    dbuf_putc(&s->byte_code, op);
586
21
}
587
588
/* return the offset of the u32 value */
589
static int re_emit_op_u32(REParseState *s, int op, uint32_t val)
590
6
{
591
6
    int pos;
592
6
    dbuf_putc(&s->byte_code, op);
593
6
    pos = s->byte_code.size;
594
6
    dbuf_put_u32(&s->byte_code, val);
595
6
    return pos;
596
6
}
597
598
static int re_emit_goto(REParseState *s, int op, uint32_t val)
599
0
{
600
0
    int pos;
601
0
    dbuf_putc(&s->byte_code, op);
602
0
    pos = s->byte_code.size;
603
0
    dbuf_put_u32(&s->byte_code, val - (pos + 4));
604
0
    return pos;
605
0
}
606
607
static void re_emit_op_u8(REParseState *s, int op, uint32_t val)
608
18
{
609
18
    dbuf_putc(&s->byte_code, op);
610
18
    dbuf_putc(&s->byte_code, val);
611
18
}
612
613
static void re_emit_op_u16(REParseState *s, int op, uint32_t val)
614
9
{
615
9
    dbuf_putc(&s->byte_code, op);
616
9
    dbuf_put_u16(&s->byte_code, val);
617
9
}
618
619
static int __attribute__((format(printf, 2, 3))) re_parse_error(REParseState *s, const char *fmt, ...)
620
0
{
621
0
    va_list ap;
622
0
    va_start(ap, fmt);
623
0
    vsnprintf(s->u.error_msg, sizeof(s->u.error_msg), fmt, ap);
624
0
    va_end(ap);
625
0
    return -1;
626
0
}
627
628
static int re_parse_out_of_memory(REParseState *s)
629
0
{
630
0
    return re_parse_error(s, "out of memory");
631
0
}
632
633
/* If allow_overflow is false, return -1 in case of
634
   overflow. Otherwise return INT32_MAX. */
635
static int parse_digits(const uint8_t **pp, BOOL allow_overflow)
636
0
{
637
0
    const uint8_t *p;
638
0
    uint64_t v;
639
0
    int c;
640
641
0
    p = *pp;
642
0
    v = 0;
643
0
    for(;;) {
644
0
        c = *p;
645
0
        if (c < '0' || c > '9')
646
0
            break;
647
0
        v = v * 10 + c - '0';
648
0
        if (v >= INT32_MAX) {
649
0
            if (allow_overflow)
650
0
                v = INT32_MAX;
651
0
            else
652
0
                return -1;
653
0
        }
654
0
        p++;
655
0
    }
656
0
    *pp = p;
657
0
    return v;
658
0
}
659
660
static int re_parse_expect(REParseState *s, const uint8_t **pp, int c)
661
0
{
662
0
    const uint8_t *p;
663
0
    p = *pp;
664
0
    if (*p != c)
665
0
        return re_parse_error(s, "expecting '%c'", c);
666
0
    p++;
667
0
    *pp = p;
668
0
    return 0;
669
0
}
670
671
/* Parse an escape sequence, *pp points after the '\':
672
   allow_utf16 value:
673
   0 : no UTF-16 escapes allowed
674
   1 : UTF-16 escapes allowed
675
   2 : UTF-16 escapes allowed and escapes of surrogate pairs are
676
   converted to a unicode character (unicode regexp case).
677
678
   Return the unicode char and update *pp if recognized,
679
   return -1 if malformed escape,
680
   return -2 otherwise. */
681
int lre_parse_escape(const uint8_t **pp, int allow_utf16)
682
64.8k
{
683
64.8k
    const uint8_t *p;
684
64.8k
    uint32_t c;
685
686
64.8k
    p = *pp;
687
64.8k
    c = *p++;
688
64.8k
    switch(c) {
689
7.99k
    case 'b':
690
7.99k
        c = '\b';
691
7.99k
        break;
692
0
    case 'f':
693
0
        c = '\f';
694
0
        break;
695
0
    case 'n':
696
0
        c = '\n';
697
0
        break;
698
0
    case 'r':
699
0
        c = '\r';
700
0
        break;
701
0
    case 't':
702
0
        c = '\t';
703
0
        break;
704
638
    case 'v':
705
638
        c = '\v';
706
638
        break;
707
0
    case 'x':
708
3
    case 'u':
709
3
        {
710
3
            int h, n, i;
711
3
            uint32_t c1;
712
713
3
            if (*p == '{' && allow_utf16) {
714
0
                p++;
715
0
                c = 0;
716
0
                for(;;) {
717
0
                    h = from_hex(*p++);
718
0
                    if (h < 0)
719
0
                        return -1;
720
0
                    c = (c << 4) | h;
721
0
                    if (c > 0x10FFFF)
722
0
                        return -1;
723
0
                    if (*p == '}')
724
0
                        break;
725
0
                }
726
0
                p++;
727
3
            } else {
728
3
                if (c == 'x') {
729
0
                    n = 2;
730
3
                } else {
731
3
                    n = 4;
732
3
                }
733
734
3
                c = 0;
735
15
                for(i = 0; i < n; i++) {
736
12
                    h = from_hex(*p++);
737
12
                    if (h < 0) {
738
0
                        return -1;
739
0
                    }
740
12
                    c = (c << 4) | h;
741
12
                }
742
3
                if (is_hi_surrogate(c) &&
743
3
                    allow_utf16 == 2 && p[0] == '\\' && p[1] == 'u') {
744
                    /* convert an escaped surrogate pair into a
745
                       unicode char */
746
0
                    c1 = 0;
747
0
                    for(i = 0; i < 4; i++) {
748
0
                        h = from_hex(p[2 + i]);
749
0
                        if (h < 0)
750
0
                            break;
751
0
                        c1 = (c1 << 4) | h;
752
0
                    }
753
0
                    if (i == 4 && is_lo_surrogate(c1)) {
754
0
                        p += 6;
755
0
                        c = from_surrogate(c, c1);
756
0
                    }
757
0
                }
758
3
            }
759
3
        }
760
3
        break;
761
3
    case '0': case '1': case '2': case '3':
762
0
    case '4': case '5': case '6': case '7':
763
0
        c -= '0';
764
0
        if (allow_utf16 == 2) {
765
            /* only accept \0 not followed by digit */
766
0
            if (c != 0 || is_digit(*p))
767
0
                return -1;
768
0
        } else {
769
            /* parse a legacy octal sequence */
770
0
            uint32_t v;
771
0
            v = *p - '0';
772
0
            if (v > 7)
773
0
                break;
774
0
            c = (c << 3) | v;
775
0
            p++;
776
0
            if (c >= 32)
777
0
                break;
778
0
            v = *p - '0';
779
0
            if (v > 7)
780
0
                break;
781
0
            c = (c << 3) | v;
782
0
            p++;
783
0
        }
784
0
        break;
785
56.2k
    default:
786
56.2k
        return -2;
787
64.8k
    }
788
8.63k
    *pp = p;
789
8.63k
    return c;
790
64.8k
}
791
792
#ifdef CONFIG_ALL_UNICODE
793
/* XXX: we use the same chars for name and value */
794
static BOOL is_unicode_char(int c)
795
0
{
796
0
    return ((c >= '0' && c <= '9') ||
797
0
            (c >= 'A' && c <= 'Z') ||
798
0
            (c >= 'a' && c <= 'z') ||
799
0
            (c == '_'));
800
0
}
801
802
/* XXX: memory error test */
803
static void seq_prop_cb(void *opaque, const uint32_t *seq, int seq_len)
804
0
{
805
0
    REStringList *sl = opaque;
806
0
    re_string_add(sl, seq_len, seq);
807
0
}
808
809
static int parse_unicode_property(REParseState *s, REStringList *cr,
810
                                  const uint8_t **pp, BOOL is_inv,
811
                                  BOOL allow_sequence_prop)
812
0
{
813
0
    const uint8_t *p;
814
0
    char name[64], value[64];
815
0
    char *q;
816
0
    BOOL script_ext;
817
0
    int ret;
818
819
0
    p = *pp;
820
0
    if (*p != '{')
821
0
        return re_parse_error(s, "expecting '{' after \\p");
822
0
    p++;
823
0
    q = name;
824
0
    while (is_unicode_char(*p)) {
825
0
        if ((q - name) >= sizeof(name) - 1)
826
0
            goto unknown_property_name;
827
0
        *q++ = *p++;
828
0
    }
829
0
    *q = '\0';
830
0
    q = value;
831
0
    if (*p == '=') {
832
0
        p++;
833
0
        while (is_unicode_char(*p)) {
834
0
            if ((q - value) >= sizeof(value) - 1)
835
0
                return re_parse_error(s, "unknown unicode property value");
836
0
            *q++ = *p++;
837
0
        }
838
0
    }
839
0
    *q = '\0';
840
0
    if (*p != '}')
841
0
        return re_parse_error(s, "expecting '}'");
842
0
    p++;
843
    //    printf("name=%s value=%s\n", name, value);
844
845
0
    if (!strcmp(name, "Script") || !strcmp(name, "sc")) {
846
0
        script_ext = FALSE;
847
0
        goto do_script;
848
0
    } else if (!strcmp(name, "Script_Extensions") || !strcmp(name, "scx")) {
849
0
        script_ext = TRUE;
850
0
    do_script:
851
0
        re_string_list_init(s, cr);
852
0
        ret = unicode_script(&cr->cr, value, script_ext);
853
0
        if (ret) {
854
0
            re_string_list_free(cr);
855
0
            if (ret == -2)
856
0
                return re_parse_error(s, "unknown unicode script");
857
0
            else
858
0
                goto out_of_memory;
859
0
        }
860
0
    } else if (!strcmp(name, "General_Category") || !strcmp(name, "gc")) {
861
0
        re_string_list_init(s, cr);
862
0
        ret = unicode_general_category(&cr->cr, value);
863
0
        if (ret) {
864
0
            re_string_list_free(cr);
865
0
            if (ret == -2)
866
0
                return re_parse_error(s, "unknown unicode general category");
867
0
            else
868
0
                goto out_of_memory;
869
0
        }
870
0
    } else if (value[0] == '\0') {
871
0
        re_string_list_init(s, cr);
872
0
        ret = unicode_general_category(&cr->cr, name);
873
0
        if (ret == -1) {
874
0
            re_string_list_free(cr);
875
0
            goto out_of_memory;
876
0
        }
877
0
        if (ret < 0) {
878
0
            ret = unicode_prop(&cr->cr, name);
879
0
            if (ret == -1) {
880
0
                re_string_list_free(cr);
881
0
                goto out_of_memory;
882
0
            }
883
0
        }
884
0
        if (ret < 0 && !is_inv && allow_sequence_prop) {
885
0
            CharRange cr_tmp;
886
0
            cr_init(&cr_tmp, s->opaque, lre_realloc);
887
0
            ret = unicode_sequence_prop(name, seq_prop_cb, cr, &cr_tmp);
888
0
            cr_free(&cr_tmp);
889
0
            if (ret == -1) {
890
0
                re_string_list_free(cr);
891
0
                goto out_of_memory;
892
0
            }
893
0
        }
894
0
        if (ret < 0)
895
0
            goto unknown_property_name;
896
0
    } else {
897
0
    unknown_property_name:
898
0
        return re_parse_error(s, "unknown unicode property name");
899
0
    }
900
901
    /* the ordering of case folding and inversion  differs with
902
       unicode_sets. 'unicode_sets' ordering is more consistent */
903
    /* XXX: the spec seems incorrect, we do it as the other engines
904
       seem to do it. */
905
0
    if (s->ignore_case && s->unicode_sets) {
906
0
        if (re_string_list_canonicalize(s, cr, s->is_unicode)) {
907
0
            re_string_list_free(cr);
908
0
            goto out_of_memory;
909
0
        }
910
0
    }
911
0
    if (is_inv) {
912
0
        if (cr_invert(&cr->cr)) {
913
0
            re_string_list_free(cr);
914
0
            goto out_of_memory;
915
0
        }
916
0
    }
917
0
    if (s->ignore_case && !s->unicode_sets) {
918
0
        if (re_string_list_canonicalize(s, cr, s->is_unicode)) {
919
0
            re_string_list_free(cr);
920
0
            goto out_of_memory;
921
0
        }
922
0
    }
923
0
    *pp = p;
924
0
    return 0;
925
0
 out_of_memory:
926
0
    return re_parse_out_of_memory(s);
927
0
}
928
#endif /* CONFIG_ALL_UNICODE */
929
930
static int get_class_atom(REParseState *s, REStringList *cr,
931
                          const uint8_t **pp, BOOL inclass);
932
933
static int parse_class_string_disjunction(REParseState *s, REStringList *cr,
934
                                          const uint8_t **pp)
935
0
{
936
0
    const uint8_t *p;
937
0
    DynBuf str;
938
0
    int c;
939
    
940
0
    p = *pp;
941
0
    if (*p != '{')
942
0
        return re_parse_error(s, "expecting '{' after \\q");
943
944
0
    dbuf_init2(&str, s->opaque, lre_realloc);
945
0
    re_string_list_init(s, cr);
946
    
947
0
    p++;
948
0
    for(;;) {
949
0
        str.size = 0;
950
0
        while (*p != '}' && *p != '|') {
951
0
            c = get_class_atom(s, NULL, &p, FALSE);
952
0
            if (c < 0)
953
0
                goto fail;
954
0
            if (dbuf_put_u32(&str, c)) {
955
0
                re_parse_out_of_memory(s);
956
0
                goto fail;
957
0
            }
958
0
        }
959
0
        if (re_string_add(cr, str.size / 4, (uint32_t *)str.buf)) {
960
0
            re_parse_out_of_memory(s);
961
0
            goto fail;
962
0
        }
963
0
        if (*p == '}')
964
0
            break;
965
0
        p++;
966
0
    }
967
0
    if (s->ignore_case) {
968
0
        if (re_string_list_canonicalize(s, cr, TRUE))
969
0
            goto fail;
970
0
    }
971
0
    p++; /* skip the '}' */
972
0
    dbuf_free(&str);
973
0
    *pp = p;
974
0
    return 0;
975
0
 fail:
976
0
    dbuf_free(&str);
977
0
    re_string_list_free(cr);
978
0
    return -1;
979
0
}
980
981
/* return -1 if error otherwise the character or a class range
982
   (CLASS_RANGE_BASE) if cr != NULL. In case of class range, 'cr' is
983
   initialized. Otherwise, it is ignored. */
984
static int get_class_atom(REParseState *s, REStringList *cr,
985
                          const uint8_t **pp, BOOL inclass)
986
9
{
987
9
    const uint8_t *p;
988
9
    uint32_t c;
989
9
    int ret;
990
991
9
    p = *pp;
992
993
9
    c = *p;
994
9
    switch(c) {
995
0
    case '\\':
996
0
        p++;
997
0
        if (p >= s->buf_end)
998
0
            goto unexpected_end;
999
0
        c = *p++;
1000
0
        switch(c) {
1001
0
        case 'd':
1002
0
            c = CHAR_RANGE_d;
1003
0
            goto class_range;
1004
0
        case 'D':
1005
0
            c = CHAR_RANGE_D;
1006
0
            goto class_range;
1007
0
        case 's':
1008
0
            c = CHAR_RANGE_s;
1009
0
            goto class_range;
1010
0
        case 'S':
1011
0
            c = CHAR_RANGE_S;
1012
0
            goto class_range;
1013
0
        case 'w':
1014
0
            c = CHAR_RANGE_w;
1015
0
            goto class_range;
1016
0
        case 'W':
1017
0
            c = CHAR_RANGE_W;
1018
0
        class_range:
1019
0
            if (!cr)
1020
0
                goto default_escape;
1021
0
            if (cr_init_char_range(s, cr, c))
1022
0
                return -1;
1023
0
            c = CLASS_RANGE_BASE;
1024
0
            break;
1025
0
        case 'c':
1026
0
            c = *p;
1027
0
            if ((c >= 'a' && c <= 'z') ||
1028
0
                (c >= 'A' && c <= 'Z') ||
1029
0
                (((c >= '0' && c <= '9') || c == '_') &&
1030
0
                 inclass && !s->is_unicode)) {   /* Annex B.1.4 */
1031
0
                c &= 0x1f;
1032
0
                p++;
1033
0
            } else if (s->is_unicode) {
1034
0
                goto invalid_escape;
1035
0
            } else {
1036
                /* otherwise return '\' and 'c' */
1037
0
                p--;
1038
0
                c = '\\';
1039
0
            }
1040
0
            break;
1041
0
        case '-':
1042
0
            if (!inclass && s->is_unicode)
1043
0
                goto invalid_escape;
1044
0
            break;
1045
0
        case '^':
1046
0
        case '$':
1047
0
        case '\\':
1048
0
        case '.':
1049
0
        case '*':
1050
0
        case '+':
1051
0
        case '?':
1052
0
        case '(':
1053
0
        case ')':
1054
0
        case '[':
1055
0
        case ']':
1056
0
        case '{':
1057
0
        case '}':
1058
0
        case '|':
1059
0
        case '/':
1060
            /* always valid to escape these characters */
1061
0
            break;
1062
0
#ifdef CONFIG_ALL_UNICODE
1063
0
        case 'p':
1064
0
        case 'P':
1065
0
            if (s->is_unicode && cr) {
1066
0
                if (parse_unicode_property(s, cr, &p, (c == 'P'), s->unicode_sets))
1067
0
                    return -1;
1068
0
                c = CLASS_RANGE_BASE;
1069
0
                break;
1070
0
            }
1071
0
            goto default_escape;
1072
0
#endif
1073
0
        case 'q':
1074
0
            if (s->unicode_sets && cr && inclass) {
1075
0
                if (parse_class_string_disjunction(s, cr, &p))
1076
0
                    return -1;
1077
0
                c = CLASS_RANGE_BASE;
1078
0
                break;
1079
0
            }
1080
0
            goto default_escape;
1081
0
        default:
1082
0
        default_escape:
1083
0
            p--;
1084
0
            ret = lre_parse_escape(&p, s->is_unicode * 2);
1085
0
            if (ret >= 0) {
1086
0
                c = ret;
1087
0
            } else {
1088
0
                if (s->is_unicode) {
1089
0
                invalid_escape:
1090
0
                    return re_parse_error(s, "invalid escape sequence in regular expression");
1091
0
                } else {
1092
                    /* just ignore the '\' */
1093
0
                    goto normal_char;
1094
0
                }
1095
0
            }
1096
0
            break;
1097
0
        }
1098
0
        break;
1099
0
    case '\0':
1100
0
        if (p >= s->buf_end) {
1101
0
        unexpected_end:
1102
0
            return re_parse_error(s, "unexpected end");
1103
0
        }
1104
        /* fall thru */
1105
0
        goto normal_char;
1106
1107
0
    case '&':
1108
0
    case '!':
1109
0
    case '#':
1110
0
    case '$':
1111
0
    case '%':
1112
0
    case '*':
1113
0
    case '+':
1114
9
    case ',':
1115
9
    case '.':
1116
9
    case ':':
1117
9
    case ';':
1118
9
    case '<':
1119
9
    case '=':
1120
9
    case '>':
1121
9
    case '?':
1122
9
    case '@':
1123
9
    case '^':
1124
9
    case '`':
1125
9
    case '~':
1126
9
        if (s->unicode_sets && p[1] == c) {
1127
            /* forbidden double characters */
1128
0
            return re_parse_error(s, "invalid class set operation in regular expression");
1129
0
        }
1130
9
        goto normal_char;
1131
1132
9
    case '(':
1133
0
    case ')':
1134
0
    case '[':
1135
0
    case ']':
1136
0
    case '{':
1137
0
    case '}':
1138
0
    case '/':
1139
0
    case '-':
1140
0
    case '|':
1141
0
        if (s->unicode_sets) {
1142
            /* invalid characters in unicode sets */
1143
0
            return re_parse_error(s, "invalid character in class in regular expression");
1144
0
        }
1145
0
        goto normal_char;
1146
1147
0
    default:
1148
9
    normal_char:
1149
        /* normal char */
1150
9
        if (c >= 128) {
1151
0
            c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
1152
0
            if ((unsigned)c > 0xffff && !s->is_unicode) {
1153
                /* XXX: should handle non BMP-1 code points */
1154
0
                return re_parse_error(s, "malformed unicode char");
1155
0
            }
1156
9
        } else {
1157
9
            p++;
1158
9
        }
1159
9
        break;
1160
9
    }
1161
9
    *pp = p;
1162
9
    return c;
1163
9
}
1164
1165
static int re_emit_range(REParseState *s, const CharRange *cr)
1166
0
{
1167
0
    int len, i;
1168
0
    uint32_t high;
1169
1170
0
    len = (unsigned)cr->len / 2;
1171
0
    if (len >= 65535)
1172
0
        return re_parse_error(s, "too many ranges");
1173
0
    if (len == 0) {
1174
0
        re_emit_op_u32(s, REOP_char32, -1);
1175
0
    } else {
1176
0
        high = cr->points[cr->len - 1];
1177
0
        if (high == UINT32_MAX)
1178
0
            high = cr->points[cr->len - 2];
1179
0
        if (high <= 0xffff) {
1180
            /* can use 16 bit ranges with the conversion that 0xffff =
1181
               infinity */
1182
0
            re_emit_op_u16(s, s->ignore_case ? REOP_range_i : REOP_range, len);
1183
0
            for(i = 0; i < cr->len; i += 2) {
1184
0
                dbuf_put_u16(&s->byte_code, cr->points[i]);
1185
0
                high = cr->points[i + 1] - 1;
1186
0
                if (high == UINT32_MAX - 1)
1187
0
                    high = 0xffff;
1188
0
                dbuf_put_u16(&s->byte_code, high);
1189
0
            }
1190
0
        } else {
1191
0
            re_emit_op_u16(s, s->ignore_case ? REOP_range32_i : REOP_range32, len);
1192
0
            for(i = 0; i < cr->len; i += 2) {
1193
0
                dbuf_put_u32(&s->byte_code, cr->points[i]);
1194
0
                dbuf_put_u32(&s->byte_code, cr->points[i + 1] - 1);
1195
0
            }
1196
0
        }
1197
0
    }
1198
0
    return 0;
1199
0
}
1200
1201
static int re_string_cmp_len(const void *a, const void *b, void *arg)
1202
0
{
1203
0
    REString *p1 = *(REString **)a;
1204
0
    REString *p2 = *(REString **)b;
1205
0
    return (p1->len < p2->len) - (p1->len > p2->len);
1206
0
}
1207
1208
static void re_emit_char(REParseState *s, int c)
1209
9
{
1210
9
    if (c <= 0xffff)
1211
9
        re_emit_op_u16(s, s->ignore_case ? REOP_char_i : REOP_char, c);
1212
0
    else
1213
0
        re_emit_op_u32(s, s->ignore_case ? REOP_char32_i : REOP_char32, c);
1214
9
}
1215
1216
static int re_emit_string_list(REParseState *s, const REStringList *sl)
1217
0
{
1218
0
    REString **tab, *p;
1219
0
    int i, j, split_pos, last_match_pos, n;
1220
0
    BOOL has_empty_string, is_last;
1221
    
1222
    //    re_string_list_dump("sl", sl);
1223
0
    if (sl->n_strings == 0) {
1224
        /* simple case: only characters */
1225
0
        if (re_emit_range(s, &sl->cr))
1226
0
            return -1;
1227
0
    } else {
1228
        /* at least one string list is present : match the longest ones first */
1229
        /* XXX: add a new op_switch opcode to compile as a trie */
1230
0
        tab = lre_realloc(s->opaque, NULL, sizeof(tab[0]) * sl->n_strings);
1231
0
        if (!tab) {
1232
0
            re_parse_out_of_memory(s);
1233
0
            return -1;
1234
0
        }
1235
0
        has_empty_string = FALSE;
1236
0
        n = 0;
1237
0
        for(i = 0; i < sl->hash_size; i++) {
1238
0
            for(p = sl->hash_table[i]; p != NULL; p = p->next) {
1239
0
                if (p->len == 0) {
1240
0
                    has_empty_string = TRUE;
1241
0
                } else {
1242
0
                    tab[n++] = p;
1243
0
                }
1244
0
            }
1245
0
        }
1246
0
        assert(n <= sl->n_strings);
1247
        
1248
0
        rqsort(tab, n, sizeof(tab[0]), re_string_cmp_len, NULL);
1249
1250
0
        last_match_pos = -1;
1251
0
        for(i = 0; i < n; i++) {
1252
0
            p = tab[i];
1253
0
            is_last = !has_empty_string && sl->cr.len == 0 && i == (n - 1);
1254
0
            if (!is_last)
1255
0
                split_pos = re_emit_op_u32(s, REOP_split_next_first, 0);
1256
0
            else
1257
0
                split_pos = 0;
1258
0
            for(j = 0; j < p->len; j++) {
1259
0
                re_emit_char(s, p->buf[j]);
1260
0
            }
1261
0
            if (!is_last) {
1262
0
                last_match_pos = re_emit_op_u32(s, REOP_goto, last_match_pos);
1263
0
                put_u32(s->byte_code.buf + split_pos, s->byte_code.size - (split_pos + 4));
1264
0
            }
1265
0
        }
1266
1267
0
        if (sl->cr.len != 0) {
1268
            /* char range */
1269
0
            is_last = !has_empty_string;
1270
0
            if (!is_last)
1271
0
                split_pos = re_emit_op_u32(s, REOP_split_next_first, 0);
1272
0
            else
1273
0
                split_pos = 0; /* not used */
1274
0
            if (re_emit_range(s, &sl->cr)) {
1275
0
                lre_realloc(s->opaque, tab, 0);
1276
0
                return -1;
1277
0
            }
1278
0
            if (!is_last)
1279
0
                put_u32(s->byte_code.buf + split_pos, s->byte_code.size - (split_pos + 4));
1280
0
        }
1281
1282
        /* patch the 'goto match' */
1283
0
        while (last_match_pos != -1) {
1284
0
            int next_pos = get_u32(s->byte_code.buf + last_match_pos);
1285
0
            put_u32(s->byte_code.buf + last_match_pos, s->byte_code.size - (last_match_pos + 4));
1286
0
            last_match_pos = next_pos;
1287
0
        }
1288
        
1289
0
        lre_realloc(s->opaque, tab, 0);
1290
0
    }
1291
0
    return 0;
1292
0
}
1293
1294
static int re_parse_nested_class(REParseState *s, REStringList *cr, const uint8_t **pp);
1295
1296
static int re_parse_class_set_operand(REParseState *s, REStringList *cr, const uint8_t **pp)
1297
0
{
1298
0
    int c1;
1299
0
    const uint8_t *p = *pp;
1300
    
1301
0
    if (*p == '[') {
1302
0
        if (re_parse_nested_class(s, cr, pp))
1303
0
            return -1;
1304
0
    } else {
1305
0
        c1 = get_class_atom(s, cr, pp, TRUE);
1306
0
        if (c1 < 0)
1307
0
            return -1;
1308
0
        if (c1 < CLASS_RANGE_BASE) {
1309
            /* create a range with a single character */
1310
0
            re_string_list_init(s, cr);
1311
0
            if (s->ignore_case)
1312
0
                c1 = lre_canonicalize(c1, s->is_unicode);
1313
0
            if (cr_union_interval(&cr->cr, c1, c1)) {
1314
0
                re_string_list_free(cr);
1315
0
                return -1;
1316
0
            }
1317
0
        }
1318
0
    }
1319
0
    return 0;
1320
0
}
1321
1322
static int re_parse_nested_class(REParseState *s, REStringList *cr, const uint8_t **pp)
1323
0
{
1324
0
    const uint8_t *p;
1325
0
    uint32_t c1, c2;
1326
0
    int ret;
1327
0
    REStringList cr1_s, *cr1 = &cr1_s;
1328
0
    BOOL invert, is_first;
1329
1330
0
    if (lre_check_stack_overflow(s->opaque, 0))
1331
0
        return re_parse_error(s, "stack overflow");
1332
1333
0
    re_string_list_init(s, cr);
1334
0
    p = *pp;
1335
0
    p++;    /* skip '[' */
1336
1337
0
    invert = FALSE;
1338
0
    if (*p == '^') {
1339
0
        p++;
1340
0
        invert = TRUE;
1341
0
    }
1342
    
1343
    /* handle unions */
1344
0
    is_first = TRUE;
1345
0
    for(;;) {
1346
0
        if (*p == ']')
1347
0
            break;
1348
0
        if (*p == '[' && s->unicode_sets) {
1349
0
            if (re_parse_nested_class(s, cr1, &p))
1350
0
                goto fail;
1351
0
            goto class_union;
1352
0
        } else {
1353
0
            c1 = get_class_atom(s, cr1, &p, TRUE);
1354
0
            if ((int)c1 < 0)
1355
0
                goto fail;
1356
0
            if (*p == '-' && p[1] != ']') {
1357
0
                const uint8_t *p0 = p + 1;
1358
0
                if (p[1] == '-' && s->unicode_sets && is_first)
1359
0
                    goto class_atom; /* first character class followed by '--' */
1360
0
                if (c1 >= CLASS_RANGE_BASE) {
1361
0
                    if (s->is_unicode) {
1362
0
                        re_string_list_free(cr1);
1363
0
                        goto invalid_class_range;
1364
0
                    }
1365
                    /* Annex B: match '-' character */
1366
0
                    goto class_atom;
1367
0
                }
1368
0
                c2 = get_class_atom(s, cr1, &p0, TRUE);
1369
0
                if ((int)c2 < 0)
1370
0
                    goto fail;
1371
0
                if (c2 >= CLASS_RANGE_BASE) {
1372
0
                    re_string_list_free(cr1);
1373
0
                    if (s->is_unicode) {
1374
0
                        goto invalid_class_range;
1375
0
                    }
1376
                    /* Annex B: match '-' character */
1377
0
                    goto class_atom;
1378
0
                }
1379
0
                p = p0;
1380
0
                if (c2 < c1) {
1381
0
                invalid_class_range:
1382
0
                    re_parse_error(s, "invalid class range");
1383
0
                    goto fail;
1384
0
                }
1385
0
                if (s->ignore_case) {
1386
0
                    CharRange cr2_s, *cr2 = &cr2_s;
1387
0
                    cr_init(cr2, s->opaque, lre_realloc);
1388
0
                    if (cr_add_interval(cr2, c1, c2 + 1) ||
1389
0
                        cr_regexp_canonicalize(cr2, s->is_unicode) ||
1390
0
                        cr_op1(&cr->cr, cr2->points, cr2->len, CR_OP_UNION)) {
1391
0
                        cr_free(cr2);
1392
0
                        goto memory_error;
1393
0
                    }
1394
0
                    cr_free(cr2);
1395
0
                } else {
1396
0
                    if (cr_union_interval(&cr->cr, c1, c2))
1397
0
                        goto memory_error;
1398
0
                }
1399
0
                is_first = FALSE; /* union operation */
1400
0
            } else {
1401
0
            class_atom:
1402
0
                if (c1 >= CLASS_RANGE_BASE) {
1403
0
                class_union:
1404
0
                    ret = re_string_list_op(cr, cr1, CR_OP_UNION);
1405
0
                    re_string_list_free(cr1);
1406
0
                    if (ret)
1407
0
                        goto memory_error;
1408
0
                } else {
1409
0
                    if (s->ignore_case)
1410
0
                        c1 = lre_canonicalize(c1, s->is_unicode);
1411
0
                    if (cr_union_interval(&cr->cr, c1, c1))
1412
0
                        goto memory_error;
1413
0
                }
1414
0
            }
1415
0
        }
1416
0
        if (s->unicode_sets && is_first) {
1417
0
            if (*p == '&' && p[1] == '&' && p[2] != '&') {
1418
                /* handle '&&' */
1419
0
                for(;;) {
1420
0
                    if (*p == ']') {
1421
0
                        break;
1422
0
                    } else if (*p == '&' && p[1] == '&' && p[2] != '&') {
1423
0
                        p += 2;
1424
0
                    } else {
1425
0
                        goto invalid_operation;
1426
0
                    }
1427
0
                    if (re_parse_class_set_operand(s, cr1, &p))
1428
0
                        goto fail;
1429
0
                    ret = re_string_list_op(cr, cr1, CR_OP_INTER);
1430
0
                    re_string_list_free(cr1);
1431
0
                    if (ret)
1432
0
                        goto memory_error;
1433
0
                }
1434
0
            } else if (*p == '-' && p[1] == '-') {
1435
                /* handle '--' */
1436
0
                for(;;) {
1437
0
                    if (*p == ']') {
1438
0
                        break;
1439
0
                    } else if (*p == '-' && p[1] == '-') {
1440
0
                        p += 2;
1441
0
                    } else {
1442
0
                    invalid_operation:
1443
0
                        re_parse_error(s, "invalid operation in regular expression");
1444
0
                        goto fail;
1445
0
                    }
1446
0
                    if (re_parse_class_set_operand(s, cr1, &p))
1447
0
                        goto fail;
1448
0
                    ret = re_string_list_op(cr, cr1, CR_OP_SUB);
1449
0
                    re_string_list_free(cr1);
1450
0
                    if (ret)
1451
0
                        goto memory_error;
1452
0
                }
1453
0
            }
1454
0
        }
1455
0
        is_first = FALSE;
1456
0
    }
1457
1458
0
    p++;    /* skip ']' */
1459
0
    *pp = p;
1460
0
    if (invert) {
1461
        /* XXX: add may_contain_string syntax check to be fully
1462
           compliant. The test here accepts more input than the
1463
           spec. */
1464
0
        if (cr->n_strings != 0) {
1465
0
            re_parse_error(s, "negated character class with strings in regular expression debugger eval code");
1466
0
            goto fail;
1467
0
        }
1468
0
        if (cr_invert(&cr->cr))
1469
0
            goto memory_error;
1470
0
    }
1471
0
    return 0;
1472
0
 memory_error:
1473
0
    re_parse_out_of_memory(s);
1474
0
 fail:
1475
0
    re_string_list_free(cr);
1476
0
    return -1;
1477
0
}
1478
1479
static int re_parse_char_class(REParseState *s, const uint8_t **pp)
1480
0
{
1481
0
    REStringList cr_s, *cr = &cr_s;
1482
1483
0
    if (re_parse_nested_class(s, cr, pp))
1484
0
        return -1;
1485
0
    if (re_emit_string_list(s, cr))
1486
0
        goto fail;
1487
0
    re_string_list_free(cr);
1488
0
    return 0;
1489
0
 fail:
1490
0
    re_string_list_free(cr);
1491
0
    return -1;
1492
0
}
1493
1494
/* Return:
1495
   - true if the opcodes may not advance the char pointer
1496
   - false if the opcodes always advance the char pointer
1497
*/
1498
static BOOL re_need_check_advance(const uint8_t *bc_buf, int bc_buf_len)
1499
0
{
1500
0
    int pos, opcode, len;
1501
0
    uint32_t val;
1502
0
    BOOL ret;
1503
1504
0
    ret = TRUE;
1505
0
    pos = 0;
1506
0
    while (pos < bc_buf_len) {
1507
0
        opcode = bc_buf[pos];
1508
0
        len = reopcode_info[opcode].size;
1509
0
        switch(opcode) {
1510
0
        case REOP_range:
1511
0
        case REOP_range_i:
1512
0
            val = get_u16(bc_buf + pos + 1);
1513
0
            len += val * 4;
1514
0
            goto simple_char;
1515
0
        case REOP_range32:
1516
0
        case REOP_range32_i:
1517
0
            val = get_u16(bc_buf + pos + 1);
1518
0
            len += val * 8;
1519
0
            goto simple_char;
1520
0
        case REOP_char:
1521
0
        case REOP_char_i:
1522
0
        case REOP_char32:
1523
0
        case REOP_char32_i:
1524
0
        case REOP_dot:
1525
0
        case REOP_any:
1526
0
        simple_char:
1527
0
            ret = FALSE;
1528
0
            break;
1529
0
        case REOP_line_start:
1530
0
        case REOP_line_start_m:
1531
0
        case REOP_line_end:
1532
0
        case REOP_line_end_m:
1533
0
        case REOP_push_i32:
1534
0
        case REOP_push_char_pos:
1535
0
        case REOP_drop:
1536
0
        case REOP_word_boundary:
1537
0
        case REOP_word_boundary_i:
1538
0
        case REOP_not_word_boundary:
1539
0
        case REOP_not_word_boundary_i:
1540
0
        case REOP_prev:
1541
            /* no effect */
1542
0
            break;
1543
0
        case REOP_save_start:
1544
0
        case REOP_save_end:
1545
0
        case REOP_save_reset:
1546
0
        case REOP_back_reference:
1547
0
        case REOP_back_reference_i:
1548
0
        case REOP_backward_back_reference:
1549
0
        case REOP_backward_back_reference_i:
1550
0
            break;
1551
0
        default:
1552
            /* safe behavior: we cannot predict the outcome */
1553
0
            return TRUE;
1554
0
        }
1555
0
        pos += len;
1556
0
    }
1557
0
    return ret;
1558
0
}
1559
1560
/* return -1 if a simple quantifier cannot be used. Otherwise return
1561
   the number of characters in the atom. */
1562
static int re_is_simple_quantifier(const uint8_t *bc_buf, int bc_buf_len)
1563
0
{
1564
0
    int pos, opcode, len, count;
1565
0
    uint32_t val;
1566
1567
0
    count = 0;
1568
0
    pos = 0;
1569
0
    while (pos < bc_buf_len) {
1570
0
        opcode = bc_buf[pos];
1571
0
        len = reopcode_info[opcode].size;
1572
0
        switch(opcode) {
1573
0
        case REOP_range:
1574
0
        case REOP_range_i:
1575
0
            val = get_u16(bc_buf + pos + 1);
1576
0
            len += val * 4;
1577
0
            goto simple_char;
1578
0
        case REOP_range32:
1579
0
        case REOP_range32_i:
1580
0
            val = get_u16(bc_buf + pos + 1);
1581
0
            len += val * 8;
1582
0
            goto simple_char;
1583
0
        case REOP_char:
1584
0
        case REOP_char_i:
1585
0
        case REOP_char32:
1586
0
        case REOP_char32_i:
1587
0
        case REOP_dot:
1588
0
        case REOP_any:
1589
0
        simple_char:
1590
0
            count++;
1591
0
            break;
1592
0
        case REOP_line_start:
1593
0
        case REOP_line_start_m:
1594
0
        case REOP_line_end:
1595
0
        case REOP_line_end_m:
1596
0
        case REOP_word_boundary:
1597
0
        case REOP_word_boundary_i:
1598
0
        case REOP_not_word_boundary:
1599
0
        case REOP_not_word_boundary_i:
1600
0
            break;
1601
0
        default:
1602
0
            return -1;
1603
0
        }
1604
0
        pos += len;
1605
0
    }
1606
0
    return count;
1607
0
}
1608
1609
/* '*pp' is the first char after '<' */
1610
static int re_parse_group_name(char *buf, int buf_size, const uint8_t **pp)
1611
0
{
1612
0
    const uint8_t *p, *p1;
1613
0
    uint32_t c, d;
1614
0
    char *q;
1615
1616
0
    p = *pp;
1617
0
    q = buf;
1618
0
    for(;;) {
1619
0
        c = *p;
1620
0
        if (c == '\\') {
1621
0
            p++;
1622
0
            if (*p != 'u')
1623
0
                return -1;
1624
0
            c = lre_parse_escape(&p, 2); // accept surrogate pairs
1625
0
        } else if (c == '>') {
1626
0
            break;
1627
0
        } else if (c >= 128) {
1628
0
            c = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p);
1629
0
            if (is_hi_surrogate(c)) {
1630
0
                d = unicode_from_utf8(p, UTF8_CHAR_LEN_MAX, &p1);
1631
0
                if (is_lo_surrogate(d)) {
1632
0
                    c = from_surrogate(c, d);
1633
0
                    p = p1;
1634
0
                }
1635
0
            }
1636
0
        } else {
1637
0
            p++;
1638
0
        }
1639
0
        if (c > 0x10FFFF)
1640
0
            return -1;
1641
0
        if (q == buf) {
1642
0
            if (!lre_js_is_ident_first(c))
1643
0
                return -1;
1644
0
        } else {
1645
0
            if (!lre_js_is_ident_next(c))
1646
0
                return -1;
1647
0
        }
1648
0
        if ((q - buf + UTF8_CHAR_LEN_MAX + 1) > buf_size)
1649
0
            return -1;
1650
0
        if (c < 128) {
1651
0
            *q++ = c;
1652
0
        } else {
1653
0
            q += unicode_to_utf8((uint8_t*)q, c);
1654
0
        }
1655
0
    }
1656
0
    if (q == buf)
1657
0
        return -1;
1658
0
    *q = '\0';
1659
0
    p++;
1660
0
    *pp = p;
1661
0
    return 0;
1662
0
}
1663
1664
/* if capture_name = NULL: return the number of captures + 1.
1665
   Otherwise, return the capture index corresponding to capture_name
1666
   or -1 if none */
1667
static int re_parse_captures(REParseState *s, int *phas_named_captures,
1668
                             const char *capture_name)
1669
0
{
1670
0
    const uint8_t *p;
1671
0
    int capture_index;
1672
0
    char name[TMP_BUF_SIZE];
1673
1674
0
    capture_index = 1;
1675
0
    *phas_named_captures = 0;
1676
0
    for (p = s->buf_start; p < s->buf_end; p++) {
1677
0
        switch (*p) {
1678
0
        case '(':
1679
0
            if (p[1] == '?') {
1680
0
                if (p[2] == '<' && p[3] != '=' && p[3] != '!') {
1681
0
                    *phas_named_captures = 1;
1682
                    /* potential named capture */
1683
0
                    if (capture_name) {
1684
0
                        p += 3;
1685
0
                        if (re_parse_group_name(name, sizeof(name), &p) == 0) {
1686
0
                            if (!strcmp(name, capture_name))
1687
0
                                return capture_index;
1688
0
                        }
1689
0
                    }
1690
0
                    capture_index++;
1691
0
                    if (capture_index >= CAPTURE_COUNT_MAX)
1692
0
                        goto done;
1693
0
                }
1694
0
            } else {
1695
0
                capture_index++;
1696
0
                if (capture_index >= CAPTURE_COUNT_MAX)
1697
0
                    goto done;
1698
0
            }
1699
0
            break;
1700
0
        case '\\':
1701
0
            p++;
1702
0
            break;
1703
0
        case '[':
1704
0
            for (p += 1 + (*p == ']'); p < s->buf_end && *p != ']'; p++) {
1705
0
                if (*p == '\\')
1706
0
                    p++;
1707
0
            }
1708
0
            break;
1709
0
        }
1710
0
    }
1711
0
 done:
1712
0
    if (capture_name)
1713
0
        return -1;
1714
0
    else
1715
0
        return capture_index;
1716
0
}
1717
1718
static int re_count_captures(REParseState *s)
1719
0
{
1720
0
    if (s->total_capture_count < 0) {
1721
0
        s->total_capture_count = re_parse_captures(s, &s->has_named_captures,
1722
0
                                                   NULL);
1723
0
    }
1724
0
    return s->total_capture_count;
1725
0
}
1726
1727
static BOOL re_has_named_captures(REParseState *s)
1728
0
{
1729
0
    if (s->has_named_captures < 0)
1730
0
        re_count_captures(s);
1731
0
    return s->has_named_captures;
1732
0
}
1733
1734
static int find_group_name(REParseState *s, const char *name)
1735
0
{
1736
0
    const char *p, *buf_end;
1737
0
    size_t len, name_len;
1738
0
    int capture_index;
1739
1740
0
    p = (char *)s->group_names.buf;
1741
0
    if (!p) return -1;
1742
0
    buf_end = (char *)s->group_names.buf + s->group_names.size;
1743
0
    name_len = strlen(name);
1744
0
    capture_index = 1;
1745
0
    while (p < buf_end) {
1746
0
        len = strlen(p);
1747
0
        if (len == name_len && memcmp(name, p, name_len) == 0)
1748
0
            return capture_index;
1749
0
        p += len + 1;
1750
0
        capture_index++;
1751
0
    }
1752
0
    return -1;
1753
0
}
1754
1755
static int re_parse_disjunction(REParseState *s, BOOL is_backward_dir);
1756
1757
static int re_parse_modifiers(REParseState *s, const uint8_t **pp)
1758
0
{
1759
0
    const uint8_t *p = *pp;
1760
0
    int mask = 0;
1761
0
    int val;
1762
1763
0
    for(;;) {
1764
0
        if (*p == 'i') {
1765
0
            val = LRE_FLAG_IGNORECASE;
1766
0
        } else if (*p == 'm') {
1767
0
            val = LRE_FLAG_MULTILINE;
1768
0
        } else if (*p == 's') {
1769
0
            val = LRE_FLAG_DOTALL;
1770
0
        } else {
1771
0
            break;
1772
0
        }
1773
0
        if (mask & val)
1774
0
            return re_parse_error(s, "duplicate modifier: '%c'", *p);
1775
0
        mask |= val;
1776
0
        p++;
1777
0
    }
1778
0
    *pp = p;
1779
0
    return mask;
1780
0
}
1781
1782
static BOOL update_modifier(BOOL val, int add_mask, int remove_mask,
1783
                            int mask)
1784
0
{
1785
0
    if (add_mask & mask)
1786
0
        val = TRUE;
1787
0
    if (remove_mask & mask)
1788
0
        val = FALSE;
1789
0
    return val;
1790
0
}
1791
1792
static int re_parse_term(REParseState *s, BOOL is_backward_dir)
1793
18
{
1794
18
    const uint8_t *p;
1795
18
    int c, last_atom_start, quant_min, quant_max, last_capture_count;
1796
18
    BOOL greedy, add_zero_advance_check, is_neg, is_backward_lookahead;
1797
18
    REStringList cr_s, *cr = &cr_s;
1798
1799
18
    last_atom_start = -1;
1800
18
    last_capture_count = 0;
1801
18
    p = s->buf_ptr;
1802
18
    c = *p;
1803
18
    switch(c) {
1804
0
    case '^':
1805
0
        p++;
1806
0
        re_emit_op(s, s->multi_line ? REOP_line_start_m : REOP_line_start);
1807
0
        break;
1808
0
    case '$':
1809
0
        p++;
1810
0
        re_emit_op(s, s->multi_line ? REOP_line_end_m : REOP_line_end);
1811
0
        break;
1812
9
    case '.':
1813
9
        p++;
1814
9
        last_atom_start = s->byte_code.size;
1815
9
        last_capture_count = s->capture_count;
1816
9
        if (is_backward_dir)
1817
0
            re_emit_op(s, REOP_prev);
1818
9
        re_emit_op(s, s->dotall ? REOP_any : REOP_dot);
1819
9
        if (is_backward_dir)
1820
0
            re_emit_op(s, REOP_prev);
1821
9
        break;
1822
0
    case '{':
1823
0
        if (s->is_unicode) {
1824
0
            return re_parse_error(s, "syntax error");
1825
0
        } else if (!is_digit(p[1])) {
1826
            /* Annex B: we accept '{' not followed by digits as a
1827
               normal atom */
1828
0
            goto parse_class_atom;
1829
0
        } else {
1830
0
            const uint8_t *p1 = p + 1;
1831
            /* Annex B: error if it is like a repetition count */
1832
0
            parse_digits(&p1, TRUE);
1833
0
            if (*p1 == ',') {
1834
0
                p1++;
1835
0
                if (is_digit(*p1)) {
1836
0
                    parse_digits(&p1, TRUE);
1837
0
                }
1838
0
            }
1839
0
            if (*p1 != '}') {
1840
0
                goto parse_class_atom;
1841
0
            }
1842
0
        }
1843
        /* fall thru */
1844
0
    case '*':
1845
0
    case '+':
1846
0
    case '?':
1847
0
        return re_parse_error(s, "nothing to repeat");
1848
0
    case '(':
1849
0
        if (p[1] == '?') {
1850
0
            if (p[2] == ':') {
1851
0
                p += 3;
1852
0
                last_atom_start = s->byte_code.size;
1853
0
                last_capture_count = s->capture_count;
1854
0
                s->buf_ptr = p;
1855
0
                if (re_parse_disjunction(s, is_backward_dir))
1856
0
                    return -1;
1857
0
                p = s->buf_ptr;
1858
0
                if (re_parse_expect(s, &p, ')'))
1859
0
                    return -1;
1860
0
            } else if (p[2] == 'i' || p[2] == 'm' || p[2] == 's' || p[2] == '-') {
1861
0
                BOOL saved_ignore_case, saved_multi_line, saved_dotall;
1862
0
                int add_mask, remove_mask;
1863
0
                p += 2;
1864
0
                remove_mask = 0;
1865
0
                add_mask = re_parse_modifiers(s, &p);
1866
0
                if (add_mask < 0)
1867
0
                    return -1;
1868
0
                if (*p == '-') {
1869
0
                    p++;
1870
0
                    remove_mask = re_parse_modifiers(s, &p);
1871
0
                    if (remove_mask < 0)
1872
0
                        return -1;
1873
0
                }
1874
0
                if ((add_mask == 0 && remove_mask == 0) ||
1875
0
                    (add_mask & remove_mask) != 0) {
1876
0
                    return re_parse_error(s, "invalid modifiers");
1877
0
                }
1878
0
                if (re_parse_expect(s, &p, ':'))
1879
0
                    return -1;
1880
0
                saved_ignore_case = s->ignore_case;
1881
0
                saved_multi_line = s->multi_line;
1882
0
                saved_dotall = s->dotall;
1883
0
                s->ignore_case = update_modifier(s->ignore_case, add_mask, remove_mask, LRE_FLAG_IGNORECASE);
1884
0
                s->multi_line = update_modifier(s->multi_line, add_mask, remove_mask, LRE_FLAG_MULTILINE);
1885
0
                s->dotall = update_modifier(s->dotall, add_mask, remove_mask, LRE_FLAG_DOTALL);
1886
1887
0
                last_atom_start = s->byte_code.size;
1888
0
                last_capture_count = s->capture_count;
1889
0
                s->buf_ptr = p;
1890
0
                if (re_parse_disjunction(s, is_backward_dir))
1891
0
                    return -1;
1892
0
                p = s->buf_ptr;
1893
0
                if (re_parse_expect(s, &p, ')'))
1894
0
                    return -1;
1895
0
                s->ignore_case = saved_ignore_case;
1896
0
                s->multi_line = saved_multi_line;
1897
0
                s->dotall = saved_dotall;
1898
0
            } else if ((p[2] == '=' || p[2] == '!')) {
1899
0
                is_neg = (p[2] == '!');
1900
0
                is_backward_lookahead = FALSE;
1901
0
                p += 3;
1902
0
                goto lookahead;
1903
0
            } else if (p[2] == '<' &&
1904
0
                       (p[3] == '=' || p[3] == '!')) {
1905
0
                int pos;
1906
0
                is_neg = (p[3] == '!');
1907
0
                is_backward_lookahead = TRUE;
1908
0
                p += 4;
1909
                /* lookahead */
1910
0
            lookahead:
1911
                /* Annex B allows lookahead to be used as an atom for
1912
                   the quantifiers */
1913
0
                if (!s->is_unicode && !is_backward_lookahead)  {
1914
0
                    last_atom_start = s->byte_code.size;
1915
0
                    last_capture_count = s->capture_count;
1916
0
                }
1917
0
                pos = re_emit_op_u32(s, REOP_lookahead + is_neg, 0);
1918
0
                s->buf_ptr = p;
1919
0
                if (re_parse_disjunction(s, is_backward_lookahead))
1920
0
                    return -1;
1921
0
                p = s->buf_ptr;
1922
0
                if (re_parse_expect(s, &p, ')'))
1923
0
                    return -1;
1924
0
                re_emit_op(s, REOP_match);
1925
                /* jump after the 'match' after the lookahead is successful */
1926
0
                if (dbuf_error(&s->byte_code))
1927
0
                    return -1;
1928
0
                put_u32(s->byte_code.buf + pos, s->byte_code.size - (pos + 4));
1929
0
            } else if (p[2] == '<') {
1930
0
                p += 3;
1931
0
                if (re_parse_group_name(s->u.tmp_buf, sizeof(s->u.tmp_buf),
1932
0
                                        &p)) {
1933
0
                    return re_parse_error(s, "invalid group name");
1934
0
                }
1935
0
                if (find_group_name(s, s->u.tmp_buf) > 0) {
1936
0
                    return re_parse_error(s, "duplicate group name");
1937
0
                }
1938
                /* group name with a trailing zero */
1939
0
                dbuf_put(&s->group_names, (uint8_t *)s->u.tmp_buf,
1940
0
                         strlen(s->u.tmp_buf) + 1);
1941
0
                s->has_named_captures = 1;
1942
0
                goto parse_capture;
1943
0
            } else {
1944
0
                return re_parse_error(s, "invalid group");
1945
0
            }
1946
0
        } else {
1947
0
            int capture_index;
1948
0
            p++;
1949
            /* capture without group name */
1950
0
            dbuf_putc(&s->group_names, 0);
1951
0
        parse_capture:
1952
0
            if (s->capture_count >= CAPTURE_COUNT_MAX)
1953
0
                return re_parse_error(s, "too many captures");
1954
0
            last_atom_start = s->byte_code.size;
1955
0
            last_capture_count = s->capture_count;
1956
0
            capture_index = s->capture_count++;
1957
0
            re_emit_op_u8(s, REOP_save_start + is_backward_dir,
1958
0
                          capture_index);
1959
1960
0
            s->buf_ptr = p;
1961
0
            if (re_parse_disjunction(s, is_backward_dir))
1962
0
                return -1;
1963
0
            p = s->buf_ptr;
1964
1965
0
            re_emit_op_u8(s, REOP_save_start + 1 - is_backward_dir,
1966
0
                          capture_index);
1967
1968
0
            if (re_parse_expect(s, &p, ')'))
1969
0
                return -1;
1970
0
        }
1971
0
        break;
1972
0
    case '\\':
1973
0
        switch(p[1]) {
1974
0
        case 'b':
1975
0
        case 'B':
1976
0
            if (p[1] != 'b') {
1977
0
                re_emit_op(s, s->ignore_case ? REOP_not_word_boundary_i : REOP_not_word_boundary);
1978
0
            } else {
1979
0
                re_emit_op(s, s->ignore_case ? REOP_word_boundary_i : REOP_word_boundary);
1980
0
            }
1981
0
            p += 2;
1982
0
            break;
1983
0
        case 'k':
1984
0
            {
1985
0
                const uint8_t *p1;
1986
0
                int dummy_res;
1987
1988
0
                p1 = p;
1989
0
                if (p1[2] != '<') {
1990
                    /* annex B: we tolerate invalid group names in non
1991
                       unicode mode if there is no named capture
1992
                       definition */
1993
0
                    if (s->is_unicode || re_has_named_captures(s))
1994
0
                        return re_parse_error(s, "expecting group name");
1995
0
                    else
1996
0
                        goto parse_class_atom;
1997
0
                }
1998
0
                p1 += 3;
1999
0
                if (re_parse_group_name(s->u.tmp_buf, sizeof(s->u.tmp_buf),
2000
0
                                        &p1)) {
2001
0
                    if (s->is_unicode || re_has_named_captures(s))
2002
0
                        return re_parse_error(s, "invalid group name");
2003
0
                    else
2004
0
                        goto parse_class_atom;
2005
0
                }
2006
0
                c = find_group_name(s, s->u.tmp_buf);
2007
0
                if (c < 0) {
2008
                    /* no capture name parsed before, try to look
2009
                       after (inefficient, but hopefully not common */
2010
0
                    c = re_parse_captures(s, &dummy_res, s->u.tmp_buf);
2011
0
                    if (c < 0) {
2012
0
                        if (s->is_unicode || re_has_named_captures(s))
2013
0
                            return re_parse_error(s, "group name not defined");
2014
0
                        else
2015
0
                            goto parse_class_atom;
2016
0
                    }
2017
0
                }
2018
0
                p = p1;
2019
0
            }
2020
0
            goto emit_back_reference;
2021
0
        case '0':
2022
0
            p += 2;
2023
0
            c = 0;
2024
0
            if (s->is_unicode) {
2025
0
                if (is_digit(*p)) {
2026
0
                    return re_parse_error(s, "invalid decimal escape in regular expression");
2027
0
                }
2028
0
            } else {
2029
                /* Annex B.1.4: accept legacy octal */
2030
0
                if (*p >= '0' && *p <= '7') {
2031
0
                    c = *p++ - '0';
2032
0
                    if (*p >= '0' && *p <= '7') {
2033
0
                        c = (c << 3) + *p++ - '0';
2034
0
                    }
2035
0
                }
2036
0
            }
2037
0
            goto normal_char;
2038
0
        case '1': case '2': case '3': case '4':
2039
0
        case '5': case '6': case '7': case '8':
2040
0
        case '9':
2041
0
            {
2042
0
                const uint8_t *q = ++p;
2043
2044
0
                c = parse_digits(&p, FALSE);
2045
0
                if (c < 0 || (c >= s->capture_count && c >= re_count_captures(s))) {
2046
0
                    if (!s->is_unicode) {
2047
                        /* Annex B.1.4: accept legacy octal */
2048
0
                        p = q;
2049
0
                        if (*p <= '7') {
2050
0
                            c = 0;
2051
0
                            if (*p <= '3')
2052
0
                                c = *p++ - '0';
2053
0
                            if (*p >= '0' && *p <= '7') {
2054
0
                                c = (c << 3) + *p++ - '0';
2055
0
                                if (*p >= '0' && *p <= '7') {
2056
0
                                    c = (c << 3) + *p++ - '0';
2057
0
                                }
2058
0
                            }
2059
0
                        } else {
2060
0
                            c = *p++;
2061
0
                        }
2062
0
                        goto normal_char;
2063
0
                    }
2064
0
                    return re_parse_error(s, "back reference out of range in regular expression");
2065
0
                }
2066
0
            emit_back_reference:
2067
0
                last_atom_start = s->byte_code.size;
2068
0
                last_capture_count = s->capture_count;
2069
                
2070
0
                re_emit_op_u8(s, REOP_back_reference + 2 * is_backward_dir + s->ignore_case, c);
2071
0
            }
2072
0
            break;
2073
0
        default:
2074
0
            goto parse_class_atom;
2075
0
        }
2076
0
        break;
2077
0
    case '[':
2078
0
        last_atom_start = s->byte_code.size;
2079
0
        last_capture_count = s->capture_count;
2080
0
        if (is_backward_dir)
2081
0
            re_emit_op(s, REOP_prev);
2082
0
        if (re_parse_char_class(s, &p))
2083
0
            return -1;
2084
0
        if (is_backward_dir)
2085
0
            re_emit_op(s, REOP_prev);
2086
0
        break;
2087
0
    case ']':
2088
0
    case '}':
2089
0
        if (s->is_unicode)
2090
0
            return re_parse_error(s, "syntax error");
2091
0
        goto parse_class_atom;
2092
9
    default:
2093
9
    parse_class_atom:
2094
9
        c = get_class_atom(s, cr, &p, FALSE);
2095
9
        if ((int)c < 0)
2096
0
            return -1;
2097
9
    normal_char:
2098
9
        last_atom_start = s->byte_code.size;
2099
9
        last_capture_count = s->capture_count;
2100
9
        if (is_backward_dir)
2101
0
            re_emit_op(s, REOP_prev);
2102
9
        if (c >= CLASS_RANGE_BASE) {
2103
0
            int ret;
2104
0
            ret = re_emit_string_list(s, cr);
2105
0
            re_string_list_free(cr);
2106
0
            if (ret)
2107
0
                return -1;
2108
9
        } else {
2109
9
            if (s->ignore_case)
2110
0
                c = lre_canonicalize(c, s->is_unicode);
2111
9
            re_emit_char(s, c);
2112
9
        }
2113
9
        if (is_backward_dir)
2114
0
            re_emit_op(s, REOP_prev);
2115
9
        break;
2116
18
    }
2117
2118
    /* quantifier */
2119
18
    if (last_atom_start >= 0) {
2120
18
        c = *p;
2121
18
        switch(c) {
2122
0
        case '*':
2123
0
            p++;
2124
0
            quant_min = 0;
2125
0
            quant_max = INT32_MAX;
2126
0
            goto quantifier;
2127
0
        case '+':
2128
0
            p++;
2129
0
            quant_min = 1;
2130
0
            quant_max = INT32_MAX;
2131
0
            goto quantifier;
2132
0
        case '?':
2133
0
            p++;
2134
0
            quant_min = 0;
2135
0
            quant_max = 1;
2136
0
            goto quantifier;
2137
0
        case '{':
2138
0
            {
2139
0
                const uint8_t *p1 = p;
2140
                /* As an extension (see ES6 annex B), we accept '{' not
2141
                   followed by digits as a normal atom */
2142
0
                if (!is_digit(p[1])) {
2143
0
                    if (s->is_unicode)
2144
0
                        goto invalid_quant_count;
2145
0
                    break;
2146
0
                }
2147
0
                p++;
2148
0
                quant_min = parse_digits(&p, TRUE);
2149
0
                quant_max = quant_min;
2150
0
                if (*p == ',') {
2151
0
                    p++;
2152
0
                    if (is_digit(*p)) {
2153
0
                        quant_max = parse_digits(&p, TRUE);
2154
0
                        if (quant_max < quant_min) {
2155
0
                        invalid_quant_count:
2156
0
                            return re_parse_error(s, "invalid repetition count");
2157
0
                        }
2158
0
                    } else {
2159
0
                        quant_max = INT32_MAX; /* infinity */
2160
0
                    }
2161
0
                }
2162
0
                if (*p != '}' && !s->is_unicode) {
2163
                    /* Annex B: normal atom if invalid '{' syntax */
2164
0
                    p = p1;
2165
0
                    break;
2166
0
                }
2167
0
                if (re_parse_expect(s, &p, '}'))
2168
0
                    return -1;
2169
0
            }
2170
0
        quantifier:
2171
0
            greedy = TRUE;
2172
0
            if (*p == '?') {
2173
0
                p++;
2174
0
                greedy = FALSE;
2175
0
            }
2176
0
            if (last_atom_start < 0) {
2177
0
                return re_parse_error(s, "nothing to repeat");
2178
0
            }
2179
0
            if (greedy) {
2180
0
                int len, pos;
2181
2182
0
                if (quant_max > 0) {
2183
                    /* specific optimization for simple quantifiers */
2184
0
                    if (dbuf_error(&s->byte_code))
2185
0
                        goto out_of_memory;
2186
0
                    len = re_is_simple_quantifier(s->byte_code.buf + last_atom_start,
2187
0
                                                 s->byte_code.size - last_atom_start);
2188
0
                    if (len > 0) {
2189
0
                        re_emit_op(s, REOP_match);
2190
2191
0
                        if (dbuf_insert(&s->byte_code, last_atom_start, 17))
2192
0
                            goto out_of_memory;
2193
0
                        pos = last_atom_start;
2194
0
                        s->byte_code.buf[pos++] = REOP_simple_greedy_quant;
2195
0
                        put_u32(&s->byte_code.buf[pos],
2196
0
                                s->byte_code.size - last_atom_start - 17);
2197
0
                        pos += 4;
2198
0
                        put_u32(&s->byte_code.buf[pos], quant_min);
2199
0
                        pos += 4;
2200
0
                        put_u32(&s->byte_code.buf[pos], quant_max);
2201
0
                        pos += 4;
2202
0
                        put_u32(&s->byte_code.buf[pos], len);
2203
0
                        pos += 4;
2204
0
                        goto done;
2205
0
                    }
2206
0
                }
2207
2208
0
                if (dbuf_error(&s->byte_code))
2209
0
                    goto out_of_memory;
2210
0
            }
2211
            /* the spec tells that if there is no advance when
2212
               running the atom after the first quant_min times,
2213
               then there is no match. We remove this test when we
2214
               are sure the atom always advances the position. */
2215
0
            add_zero_advance_check = re_need_check_advance(s->byte_code.buf + last_atom_start,
2216
0
                                                           s->byte_code.size - last_atom_start);
2217
2218
0
            {
2219
0
                int len, pos;
2220
0
                len = s->byte_code.size - last_atom_start;
2221
0
                if (quant_min == 0) {
2222
                    /* need to reset the capture in case the atom is
2223
                       not executed */
2224
0
                    if (last_capture_count != s->capture_count) {
2225
0
                        if (dbuf_insert(&s->byte_code, last_atom_start, 3))
2226
0
                            goto out_of_memory;
2227
0
                        s->byte_code.buf[last_atom_start++] = REOP_save_reset;
2228
0
                        s->byte_code.buf[last_atom_start++] = last_capture_count;
2229
0
                        s->byte_code.buf[last_atom_start++] = s->capture_count - 1;
2230
0
                    }
2231
0
                    if (quant_max == 0) {
2232
0
                        s->byte_code.size = last_atom_start;
2233
0
                    } else if (quant_max == 1 || quant_max == INT32_MAX) {
2234
0
                        BOOL has_goto = (quant_max == INT32_MAX);
2235
0
                        if (dbuf_insert(&s->byte_code, last_atom_start, 5 + add_zero_advance_check))
2236
0
                            goto out_of_memory;
2237
0
                        s->byte_code.buf[last_atom_start] = REOP_split_goto_first +
2238
0
                            greedy;
2239
0
                        put_u32(s->byte_code.buf + last_atom_start + 1,
2240
0
                                len + 5 * has_goto + add_zero_advance_check * 2);
2241
0
                        if (add_zero_advance_check) {
2242
0
                            s->byte_code.buf[last_atom_start + 1 + 4] = REOP_push_char_pos;
2243
0
                            re_emit_op(s, REOP_check_advance);
2244
0
                        }
2245
0
                        if (has_goto)
2246
0
                            re_emit_goto(s, REOP_goto, last_atom_start);
2247
0
                    } else {
2248
0
                        if (dbuf_insert(&s->byte_code, last_atom_start, 10 + add_zero_advance_check))
2249
0
                            goto out_of_memory;
2250
0
                        pos = last_atom_start;
2251
0
                        s->byte_code.buf[pos++] = REOP_push_i32;
2252
0
                        put_u32(s->byte_code.buf + pos, quant_max);
2253
0
                        pos += 4;
2254
0
                        s->byte_code.buf[pos++] = REOP_split_goto_first + greedy;
2255
0
                        put_u32(s->byte_code.buf + pos, len + 5 + add_zero_advance_check * 2);
2256
0
                        pos += 4;
2257
0
                        if (add_zero_advance_check) {
2258
0
                            s->byte_code.buf[pos++] = REOP_push_char_pos;
2259
0
                            re_emit_op(s, REOP_check_advance);
2260
0
                        }
2261
0
                        re_emit_goto(s, REOP_loop, last_atom_start + 5);
2262
0
                        re_emit_op(s, REOP_drop);
2263
0
                    }
2264
0
                } else if (quant_min == 1 && quant_max == INT32_MAX &&
2265
0
                           !add_zero_advance_check) {
2266
0
                    re_emit_goto(s, REOP_split_next_first - greedy,
2267
0
                                 last_atom_start);
2268
0
                } else {
2269
0
                    if (quant_min == 1) {
2270
                        /* nothing to add */
2271
0
                    } else {
2272
0
                        if (dbuf_insert(&s->byte_code, last_atom_start, 5))
2273
0
                            goto out_of_memory;
2274
0
                        s->byte_code.buf[last_atom_start] = REOP_push_i32;
2275
0
                        put_u32(s->byte_code.buf + last_atom_start + 1,
2276
0
                                quant_min);
2277
0
                        last_atom_start += 5;
2278
0
                        re_emit_goto(s, REOP_loop, last_atom_start);
2279
0
                        re_emit_op(s, REOP_drop);
2280
0
                    }
2281
0
                    if (quant_max == INT32_MAX) {
2282
0
                        pos = s->byte_code.size;
2283
0
                        re_emit_op_u32(s, REOP_split_goto_first + greedy,
2284
0
                                       len + 5 + add_zero_advance_check * 2);
2285
0
                        if (add_zero_advance_check)
2286
0
                            re_emit_op(s, REOP_push_char_pos);
2287
                        /* copy the atom */
2288
0
                        dbuf_put_self(&s->byte_code, last_atom_start, len);
2289
0
                        if (add_zero_advance_check)
2290
0
                            re_emit_op(s, REOP_check_advance);
2291
0
                        re_emit_goto(s, REOP_goto, pos);
2292
0
                    } else if (quant_max > quant_min) {
2293
0
                        re_emit_op_u32(s, REOP_push_i32, quant_max - quant_min);
2294
0
                        pos = s->byte_code.size;
2295
0
                        re_emit_op_u32(s, REOP_split_goto_first + greedy,
2296
0
                                       len + 5 + add_zero_advance_check * 2);
2297
0
                        if (add_zero_advance_check)
2298
0
                            re_emit_op(s, REOP_push_char_pos);
2299
                        /* copy the atom */
2300
0
                        dbuf_put_self(&s->byte_code, last_atom_start, len);
2301
0
                        if (add_zero_advance_check)
2302
0
                            re_emit_op(s, REOP_check_advance);
2303
0
                        re_emit_goto(s, REOP_loop, pos);
2304
0
                        re_emit_op(s, REOP_drop);
2305
0
                    }
2306
0
                }
2307
0
                last_atom_start = -1;
2308
0
            }
2309
0
            break;
2310
18
        default:
2311
18
            break;
2312
18
        }
2313
18
    }
2314
18
 done:
2315
18
    s->buf_ptr = p;
2316
18
    return 0;
2317
0
 out_of_memory:
2318
0
    return re_parse_out_of_memory(s);
2319
18
}
2320
2321
static int re_parse_alternative(REParseState *s, BOOL is_backward_dir)
2322
9
{
2323
9
    const uint8_t *p;
2324
9
    int ret;
2325
9
    size_t start, term_start, end, term_size;
2326
2327
9
    start = s->byte_code.size;
2328
27
    for(;;) {
2329
27
        p = s->buf_ptr;
2330
27
        if (p >= s->buf_end)
2331
9
            break;
2332
18
        if (*p == '|' || *p == ')')
2333
0
            break;
2334
18
        term_start = s->byte_code.size;
2335
18
        ret = re_parse_term(s, is_backward_dir);
2336
18
        if (ret)
2337
0
            return ret;
2338
18
        if (is_backward_dir) {
2339
            /* reverse the order of the terms (XXX: inefficient, but
2340
               speed is not really critical here) */
2341
0
            end = s->byte_code.size;
2342
0
            term_size = end - term_start;
2343
0
            if (dbuf_claim(&s->byte_code, term_size))
2344
0
                return -1;
2345
0
            memmove(s->byte_code.buf + start + term_size,
2346
0
                    s->byte_code.buf + start,
2347
0
                    end - start);
2348
0
            memcpy(s->byte_code.buf + start, s->byte_code.buf + end,
2349
0
                   term_size);
2350
0
        }
2351
18
    }
2352
9
    return 0;
2353
9
}
2354
2355
static int re_parse_disjunction(REParseState *s, BOOL is_backward_dir)
2356
9
{
2357
9
    int start, len, pos;
2358
2359
9
    if (lre_check_stack_overflow(s->opaque, 0))
2360
0
        return re_parse_error(s, "stack overflow");
2361
2362
9
    start = s->byte_code.size;
2363
9
    if (re_parse_alternative(s, is_backward_dir))
2364
0
        return -1;
2365
9
    while (*s->buf_ptr == '|') {
2366
0
        s->buf_ptr++;
2367
2368
0
        len = s->byte_code.size - start;
2369
2370
        /* insert a split before the first alternative */
2371
0
        if (dbuf_insert(&s->byte_code, start, 5)) {
2372
0
            return re_parse_out_of_memory(s);
2373
0
        }
2374
0
        s->byte_code.buf[start] = REOP_split_next_first;
2375
0
        put_u32(s->byte_code.buf + start + 1, len + 5);
2376
2377
0
        pos = re_emit_op_u32(s, REOP_goto, 0);
2378
2379
0
        if (re_parse_alternative(s, is_backward_dir))
2380
0
            return -1;
2381
2382
        /* patch the goto */
2383
0
        len = s->byte_code.size - (pos + 4);
2384
0
        put_u32(s->byte_code.buf + pos, len);
2385
0
    }
2386
9
    return 0;
2387
9
}
2388
2389
/* the control flow is recursive so the analysis can be linear */
2390
static int compute_stack_size(const uint8_t *bc_buf, int bc_buf_len)
2391
9
{
2392
9
    int stack_size, stack_size_max, pos, opcode, len;
2393
9
    uint32_t val;
2394
2395
9
    stack_size = 0;
2396
9
    stack_size_max = 0;
2397
9
    bc_buf += RE_HEADER_LEN;
2398
9
    bc_buf_len -= RE_HEADER_LEN;
2399
9
    pos = 0;
2400
63
    while (pos < bc_buf_len) {
2401
54
        opcode = bc_buf[pos];
2402
54
        len = reopcode_info[opcode].size;
2403
54
        assert(opcode < REOP_COUNT);
2404
54
        assert((pos + len) <= bc_buf_len);
2405
54
        switch(opcode) {
2406
0
        case REOP_push_i32:
2407
0
        case REOP_push_char_pos:
2408
0
            stack_size++;
2409
0
            if (stack_size > stack_size_max) {
2410
0
                if (stack_size > STACK_SIZE_MAX)
2411
0
                    return -1;
2412
0
                stack_size_max = stack_size;
2413
0
            }
2414
0
            break;
2415
0
        case REOP_drop:
2416
0
        case REOP_check_advance:
2417
0
            assert(stack_size > 0);
2418
0
            stack_size--;
2419
0
            break;
2420
0
        case REOP_range:
2421
0
        case REOP_range_i:
2422
0
            val = get_u16(bc_buf + pos + 1);
2423
0
            len += val * 4;
2424
0
            break;
2425
0
        case REOP_range32:
2426
0
        case REOP_range32_i:
2427
0
            val = get_u16(bc_buf + pos + 1);
2428
0
            len += val * 8;
2429
0
            break;
2430
54
        }
2431
54
        pos += len;
2432
54
    }
2433
9
    return stack_size_max;
2434
9
}
2435
2436
static void *lre_bytecode_realloc(void *opaque, void *ptr, size_t size)
2437
60
{
2438
60
    if (size > (INT32_MAX / 2)) {
2439
        /* the bytecode cannot be larger than 2G. Leave some slack to 
2440
           avoid some overflows. */
2441
0
        return NULL;
2442
60
    } else {
2443
60
        return lre_realloc(opaque, ptr, size);
2444
60
    }
2445
60
}
2446
2447
/* 'buf' must be a zero terminated UTF-8 string of length buf_len.
2448
   Return NULL if error and allocate an error message in *perror_msg,
2449
   otherwise the compiled bytecode and its length in plen.
2450
*/
2451
uint8_t *lre_compile(int *plen, char *error_msg, int error_msg_size,
2452
                     const char *buf, size_t buf_len, int re_flags,
2453
                     void *opaque)
2454
9
{
2455
9
    REParseState s_s, *s = &s_s;
2456
9
    int stack_size;
2457
9
    BOOL is_sticky;
2458
2459
9
    memset(s, 0, sizeof(*s));
2460
9
    s->opaque = opaque;
2461
9
    s->buf_ptr = (const uint8_t *)buf;
2462
9
    s->buf_end = s->buf_ptr + buf_len;
2463
9
    s->buf_start = s->buf_ptr;
2464
9
    s->re_flags = re_flags;
2465
9
    s->is_unicode = ((re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) != 0);
2466
9
    is_sticky = ((re_flags & LRE_FLAG_STICKY) != 0);
2467
9
    s->ignore_case = ((re_flags & LRE_FLAG_IGNORECASE) != 0);
2468
9
    s->multi_line = ((re_flags & LRE_FLAG_MULTILINE) != 0);
2469
9
    s->dotall = ((re_flags & LRE_FLAG_DOTALL) != 0);
2470
9
    s->unicode_sets = ((re_flags & LRE_FLAG_UNICODE_SETS) != 0);
2471
9
    s->capture_count = 1;
2472
9
    s->total_capture_count = -1;
2473
9
    s->has_named_captures = -1;
2474
2475
9
    dbuf_init2(&s->byte_code, opaque, lre_bytecode_realloc);
2476
9
    dbuf_init2(&s->group_names, opaque, lre_realloc);
2477
2478
9
    dbuf_put_u16(&s->byte_code, re_flags); /* first element is the flags */
2479
9
    dbuf_putc(&s->byte_code, 0); /* second element is the number of captures */
2480
9
    dbuf_putc(&s->byte_code, 0); /* stack size */
2481
9
    dbuf_put_u32(&s->byte_code, 0); /* bytecode length */
2482
2483
9
    if (!is_sticky) {
2484
        /* iterate thru all positions (about the same as .*?( ... ) )
2485
           .  We do it without an explicit loop so that lock step
2486
           thread execution will be possible in an optimized
2487
           implementation */
2488
3
        re_emit_op_u32(s, REOP_split_goto_first, 1 + 5);
2489
3
        re_emit_op(s, REOP_any);
2490
3
        re_emit_op_u32(s, REOP_goto, -(5 + 1 + 5));
2491
3
    }
2492
9
    re_emit_op_u8(s, REOP_save_start, 0);
2493
2494
9
    if (re_parse_disjunction(s, FALSE)) {
2495
0
    error:
2496
0
        dbuf_free(&s->byte_code);
2497
0
        dbuf_free(&s->group_names);
2498
0
        pstrcpy(error_msg, error_msg_size, s->u.error_msg);
2499
0
        *plen = 0;
2500
0
        return NULL;
2501
0
    }
2502
2503
9
    re_emit_op_u8(s, REOP_save_end, 0);
2504
2505
9
    re_emit_op(s, REOP_match);
2506
2507
9
    if (*s->buf_ptr != '\0') {
2508
0
        re_parse_error(s, "extraneous characters at the end");
2509
0
        goto error;
2510
0
    }
2511
2512
9
    if (dbuf_error(&s->byte_code)) {
2513
0
        re_parse_out_of_memory(s);
2514
0
        goto error;
2515
0
    }
2516
2517
9
    stack_size = compute_stack_size(s->byte_code.buf, s->byte_code.size);
2518
9
    if (stack_size < 0) {
2519
0
        re_parse_error(s, "too many imbricated quantifiers");
2520
0
        goto error;
2521
0
    }
2522
2523
9
    s->byte_code.buf[RE_HEADER_CAPTURE_COUNT] = s->capture_count;
2524
9
    s->byte_code.buf[RE_HEADER_STACK_SIZE] = stack_size;
2525
9
    put_u32(s->byte_code.buf + RE_HEADER_BYTECODE_LEN,
2526
9
            s->byte_code.size - RE_HEADER_LEN);
2527
2528
    /* add the named groups if needed */
2529
9
    if (s->group_names.size > (s->capture_count - 1)) {
2530
0
        dbuf_put(&s->byte_code, s->group_names.buf, s->group_names.size);
2531
0
        put_u16(s->byte_code.buf + RE_HEADER_FLAGS,
2532
0
                lre_get_flags(s->byte_code.buf) | LRE_FLAG_NAMED_GROUPS);
2533
0
    }
2534
9
    dbuf_free(&s->group_names);
2535
2536
#ifdef DUMP_REOP
2537
    lre_dump_bytecode(s->byte_code.buf, s->byte_code.size);
2538
#endif
2539
2540
9
    error_msg[0] = '\0';
2541
9
    *plen = s->byte_code.size;
2542
9
    return s->byte_code.buf;
2543
9
}
2544
2545
static BOOL is_line_terminator(uint32_t c)
2546
3.02M
{
2547
3.02M
    return (c == '\n' || c == '\r' || c == CP_LS || c == CP_PS);
2548
3.02M
}
2549
2550
static BOOL is_word_char(uint32_t c)
2551
0
{
2552
0
    return ((c >= '0' && c <= '9') ||
2553
0
            (c >= 'a' && c <= 'z') ||
2554
0
            (c >= 'A' && c <= 'Z') ||
2555
0
            (c == '_'));
2556
0
}
2557
2558
#define GET_CHAR(c, cptr, cbuf_end, cbuf_type)                          \
2559
3.03M
    do {                                                                \
2560
3.03M
        if (cbuf_type == 0) {                                           \
2561
0
            c = *cptr++;                                                \
2562
3.03M
        } else {                                                        \
2563
3.03M
            const uint16_t *_p = (const uint16_t *)cptr;                \
2564
3.03M
            const uint16_t *_end = (const uint16_t *)cbuf_end;          \
2565
3.03M
            c = *_p++;                                                  \
2566
3.03M
            if (is_hi_surrogate(c) && cbuf_type == 2) {                 \
2567
6
                if (_p < _end && is_lo_surrogate(*_p)) {                \
2568
0
                    c = from_surrogate(c, *_p++);                       \
2569
0
                }                                                       \
2570
6
            }                                                           \
2571
3.03M
            cptr = (const void *)_p;                                    \
2572
3.03M
        }                                                               \
2573
3.03M
    } while (0)
2574
2575
#define PEEK_CHAR(c, cptr, cbuf_end, cbuf_type)                         \
2576
0
    do {                                                                \
2577
0
        if (cbuf_type == 0) {                                           \
2578
0
            c = cptr[0];                                                \
2579
0
        } else {                                                        \
2580
0
            const uint16_t *_p = (const uint16_t *)cptr;                \
2581
0
            const uint16_t *_end = (const uint16_t *)cbuf_end;          \
2582
0
            c = *_p++;                                                  \
2583
0
            if (is_hi_surrogate(c) && cbuf_type == 2) {                 \
2584
0
                if (_p < _end && is_lo_surrogate(*_p)) {                \
2585
0
                    c = from_surrogate(c, *_p);                         \
2586
0
                }                                                       \
2587
0
            }                                                           \
2588
0
        }                                                               \
2589
0
    } while (0)
2590
2591
#define PEEK_PREV_CHAR(c, cptr, cbuf_start, cbuf_type)                  \
2592
0
    do {                                                                \
2593
0
        if (cbuf_type == 0) {                                           \
2594
0
            c = cptr[-1];                                               \
2595
0
        } else {                                                        \
2596
0
            const uint16_t *_p = (const uint16_t *)cptr - 1;            \
2597
0
            const uint16_t *_start = (const uint16_t *)cbuf_start;      \
2598
0
            c = *_p;                                                    \
2599
0
            if (is_lo_surrogate(c) && cbuf_type == 2) {                 \
2600
0
                if (_p > _start && is_hi_surrogate(_p[-1])) {           \
2601
0
                    c = from_surrogate(*--_p, c);                       \
2602
0
                }                                                       \
2603
0
            }                                                           \
2604
0
        }                                                               \
2605
0
    } while (0)
2606
2607
#define GET_PREV_CHAR(c, cptr, cbuf_start, cbuf_type)                   \
2608
0
    do {                                                                \
2609
0
        if (cbuf_type == 0) {                                           \
2610
0
            cptr--;                                                     \
2611
0
            c = cptr[0];                                                \
2612
0
        } else {                                                        \
2613
0
            const uint16_t *_p = (const uint16_t *)cptr - 1;            \
2614
0
            const uint16_t *_start = (const uint16_t *)cbuf_start;      \
2615
0
            c = *_p;                                                    \
2616
0
            if (is_lo_surrogate(c) && cbuf_type == 2) {                 \
2617
0
                if (_p > _start && is_hi_surrogate(_p[-1])) {           \
2618
0
                    c = from_surrogate(*--_p, c);                       \
2619
0
                }                                                       \
2620
0
            }                                                           \
2621
0
            cptr = (const void *)_p;                                    \
2622
0
        }                                                               \
2623
0
    } while (0)
2624
2625
#define PREV_CHAR(cptr, cbuf_start, cbuf_type)                          \
2626
0
    do {                                                                \
2627
0
        if (cbuf_type == 0) {                                           \
2628
0
            cptr--;                                                     \
2629
0
        } else {                                                        \
2630
0
            const uint16_t *_p = (const uint16_t *)cptr - 1;            \
2631
0
            const uint16_t *_start = (const uint16_t *)cbuf_start;      \
2632
0
            if (is_lo_surrogate(*_p) && cbuf_type == 2) {               \
2633
0
                if (_p > _start && is_hi_surrogate(_p[-1])) {           \
2634
0
                    --_p;                                               \
2635
0
                }                                                       \
2636
0
            }                                                           \
2637
0
            cptr = (const void *)_p;                                    \
2638
0
        }                                                               \
2639
0
    } while (0)
2640
2641
typedef uintptr_t StackInt;
2642
2643
typedef enum {
2644
    RE_EXEC_STATE_SPLIT,
2645
    RE_EXEC_STATE_LOOKAHEAD,
2646
    RE_EXEC_STATE_NEGATIVE_LOOKAHEAD,
2647
    RE_EXEC_STATE_GREEDY_QUANT,
2648
} REExecStateEnum;
2649
2650
typedef struct REExecState {
2651
    REExecStateEnum type : 8;
2652
    uint8_t stack_len;
2653
    size_t count; /* only used for RE_EXEC_STATE_GREEDY_QUANT */
2654
    const uint8_t *cptr;
2655
    const uint8_t *pc;
2656
    void *buf[0];
2657
} REExecState;
2658
2659
typedef struct {
2660
    const uint8_t *cbuf;
2661
    const uint8_t *cbuf_end;
2662
    /* 0 = 8 bit chars, 1 = 16 bit chars, 2 = 16 bit chars, UTF-16 */
2663
    int cbuf_type;
2664
    int capture_count;
2665
    int stack_size_max;
2666
    BOOL is_unicode;
2667
    int interrupt_counter;
2668
    void *opaque; /* used for stack overflow check */
2669
2670
    size_t state_size;
2671
    uint8_t *state_stack;
2672
    size_t state_stack_size;
2673
    size_t state_stack_len;
2674
} REExecContext;
2675
2676
static int push_state(REExecContext *s,
2677
                      uint8_t **capture,
2678
                      StackInt *stack, size_t stack_len,
2679
                      const uint8_t *pc, const uint8_t *cptr,
2680
                      REExecStateEnum type, size_t count)
2681
0
{
2682
0
    REExecState *rs;
2683
0
    uint8_t *new_stack;
2684
0
    size_t new_size, i, n;
2685
0
    StackInt *stack_buf;
2686
2687
0
    if (unlikely((s->state_stack_len + 1) > s->state_stack_size)) {
2688
        /* reallocate the stack */
2689
0
        new_size = s->state_stack_size * 3 / 2;
2690
0
        if (new_size < 8)
2691
0
            new_size = 8;
2692
0
        new_stack = lre_realloc(s->opaque, s->state_stack, new_size * s->state_size);
2693
0
        if (!new_stack)
2694
0
            return -1;
2695
0
        s->state_stack_size = new_size;
2696
0
        s->state_stack = new_stack;
2697
0
    }
2698
0
    rs = (REExecState *)(s->state_stack + s->state_stack_len * s->state_size);
2699
0
    s->state_stack_len++;
2700
0
    rs->type = type;
2701
0
    rs->count = count;
2702
0
    rs->stack_len = stack_len;
2703
0
    rs->cptr = cptr;
2704
0
    rs->pc = pc;
2705
0
    n = 2 * s->capture_count;
2706
0
    for(i = 0; i < n; i++)
2707
0
        rs->buf[i] = capture[i];
2708
0
    stack_buf = (StackInt *)(rs->buf + n);
2709
0
    for(i = 0; i < stack_len; i++)
2710
0
        stack_buf[i] = stack[i];
2711
0
    return 0;
2712
0
}
2713
2714
static int lre_poll_timeout(REExecContext *s)
2715
3.02M
{
2716
3.02M
    if (unlikely(--s->interrupt_counter <= 0)) {
2717
0
        s->interrupt_counter = INTERRUPT_COUNTER_INIT;
2718
0
        if (lre_check_timeout(s->opaque))
2719
0
            return LRE_RET_TIMEOUT;
2720
0
    }
2721
3.02M
    return 0;
2722
3.02M
}
2723
2724
/* return 1 if match, 0 if not match or < 0 if error. */
2725
static intptr_t lre_exec_backtrack(REExecContext *s, uint8_t **capture,
2726
                                   StackInt *stack, int stack_len,
2727
                                   const uint8_t *pc, const uint8_t *cptr,
2728
                                   BOOL no_recurse)
2729
3.02M
{
2730
3.02M
    int opcode, ret;
2731
3.02M
    int cbuf_type;
2732
3.02M
    uint32_t val, c;
2733
3.02M
    const uint8_t *cbuf_end;
2734
2735
3.02M
    cbuf_type = s->cbuf_type;
2736
3.02M
    cbuf_end = s->cbuf_end;
2737
2738
6.06M
    for(;;) {
2739
        //        printf("top=%p: pc=%d\n", th_list.top, (int)(pc - (bc_buf + RE_HEADER_LEN)));
2740
6.06M
        opcode = *pc++;
2741
6.06M
        switch(opcode) {
2742
12
        case REOP_match:
2743
12
            {
2744
12
                REExecState *rs;
2745
12
                if (no_recurse)
2746
0
                    return (intptr_t)cptr;
2747
12
                ret = 1;
2748
12
                goto recurse;
2749
3.02M
            no_match:
2750
3.02M
                if (no_recurse)
2751
0
                    return 0;
2752
3.02M
                ret = 0;
2753
3.02M
            recurse:
2754
3.02M
                for(;;) {
2755
3.02M
                    if (lre_poll_timeout(s))
2756
0
                        return LRE_RET_TIMEOUT;
2757
3.02M
                    if (s->state_stack_len == 0)
2758
3.02M
                        return ret;
2759
0
                    rs = (REExecState *)(s->state_stack +
2760
0
                                         (s->state_stack_len - 1) * s->state_size);
2761
0
                    if (rs->type == RE_EXEC_STATE_SPLIT) {
2762
0
                        if (!ret) {
2763
0
                        pop_state:
2764
0
                            memcpy(capture, rs->buf,
2765
0
                                   sizeof(capture[0]) * 2 * s->capture_count);
2766
0
                        pop_state1:
2767
0
                            pc = rs->pc;
2768
0
                            cptr = rs->cptr;
2769
0
                            stack_len = rs->stack_len;
2770
0
                            memcpy(stack, rs->buf + 2 * s->capture_count,
2771
0
                                   stack_len * sizeof(stack[0]));
2772
0
                            s->state_stack_len--;
2773
0
                            break;
2774
0
                        }
2775
0
                    } else if (rs->type == RE_EXEC_STATE_GREEDY_QUANT) {
2776
0
                        if (!ret) {
2777
0
                            uint32_t char_count, i;
2778
0
                            memcpy(capture, rs->buf,
2779
0
                                   sizeof(capture[0]) * 2 * s->capture_count);
2780
0
                            stack_len = rs->stack_len;
2781
0
                            memcpy(stack, rs->buf + 2 * s->capture_count,
2782
0
                                   stack_len * sizeof(stack[0]));
2783
0
                            pc = rs->pc;
2784
0
                            cptr = rs->cptr;
2785
                            /* go backward */
2786
0
                            char_count = get_u32(pc + 12);
2787
0
                            for(i = 0; i < char_count; i++) {
2788
0
                                PREV_CHAR(cptr, s->cbuf, cbuf_type);
2789
0
                            }
2790
0
                            pc = (pc + 16) + (int)get_u32(pc);
2791
0
                            rs->cptr = cptr;
2792
0
                            rs->count--;
2793
0
                            if (rs->count == 0) {
2794
0
                                s->state_stack_len--;
2795
0
                            }
2796
0
                            break;
2797
0
                        }
2798
0
                    } else {
2799
0
                        ret = ((rs->type == RE_EXEC_STATE_LOOKAHEAD && ret) ||
2800
0
                               (rs->type == RE_EXEC_STATE_NEGATIVE_LOOKAHEAD && !ret));
2801
0
                        if (ret) {
2802
                            /* keep the capture in case of positive lookahead */
2803
0
                            if (rs->type == RE_EXEC_STATE_LOOKAHEAD)
2804
0
                                goto pop_state1;
2805
0
                            else
2806
0
                                goto pop_state;
2807
0
                        }
2808
0
                    }
2809
0
                    s->state_stack_len--;
2810
0
                }
2811
3.02M
            }
2812
0
            break;
2813
0
        case REOP_char32:
2814
0
        case REOP_char32_i:
2815
0
            val = get_u32(pc);
2816
0
            pc += 4;
2817
0
            goto test_char;
2818
2.87k
        case REOP_char:
2819
2.87k
        case REOP_char_i:
2820
2.87k
            val = get_u16(pc);
2821
2.87k
            pc += 2;
2822
2.87k
        test_char:
2823
2.87k
            if (cptr >= cbuf_end)
2824
3
                goto no_match;
2825
2.87k
            GET_CHAR(c, cptr, cbuf_end, cbuf_type);
2826
2.87k
            if (opcode == REOP_char_i || opcode == REOP_char32_i) {
2827
0
                c = lre_canonicalize(c, s->is_unicode);
2828
0
            }
2829
2.87k
            if (val != c)
2830
2.86k
                goto no_match;
2831
12
            break;
2832
12
        case REOP_split_goto_first:
2833
0
        case REOP_split_next_first:
2834
0
            {
2835
0
                const uint8_t *pc1;
2836
2837
0
                val = get_u32(pc);
2838
0
                pc += 4;
2839
0
                if (opcode == REOP_split_next_first) {
2840
0
                    pc1 = pc + (int)val;
2841
0
                } else {
2842
0
                    pc1 = pc;
2843
0
                    pc = pc + (int)val;
2844
0
                }
2845
0
                ret = push_state(s, capture, stack, stack_len,
2846
0
                                 pc1, cptr, RE_EXEC_STATE_SPLIT, 0);
2847
0
                if (ret < 0)
2848
0
                    return LRE_RET_MEMORY_ERROR;
2849
0
                break;
2850
0
            }
2851
0
        case REOP_lookahead:
2852
0
        case REOP_negative_lookahead:
2853
0
            val = get_u32(pc);
2854
0
            pc += 4;
2855
0
            ret = push_state(s, capture, stack, stack_len,
2856
0
                             pc + (int)val, cptr,
2857
0
                             RE_EXEC_STATE_LOOKAHEAD + opcode - REOP_lookahead,
2858
0
                             0);
2859
0
            if (ret < 0)
2860
0
                return LRE_RET_MEMORY_ERROR;
2861
0
            break;
2862
2863
0
        case REOP_goto:
2864
0
            val = get_u32(pc);
2865
0
            pc += 4 + (int)val;
2866
0
            if (lre_poll_timeout(s))
2867
0
                return LRE_RET_TIMEOUT;
2868
0
            break;
2869
0
        case REOP_line_start:
2870
0
        case REOP_line_start_m:
2871
0
            if (cptr == s->cbuf)
2872
0
                break;
2873
0
            if (opcode == REOP_line_start)
2874
0
                goto no_match;
2875
0
            PEEK_PREV_CHAR(c, cptr, s->cbuf, cbuf_type);
2876
0
            if (!is_line_terminator(c))
2877
0
                goto no_match;
2878
0
            break;
2879
0
        case REOP_line_end:
2880
0
        case REOP_line_end_m:
2881
0
            if (cptr == cbuf_end)
2882
0
                break;
2883
0
            if (opcode == REOP_line_end)
2884
0
                goto no_match;
2885
0
            PEEK_CHAR(c, cptr, cbuf_end, cbuf_type);
2886
0
            if (!is_line_terminator(c))
2887
0
                goto no_match;
2888
0
            break;
2889
3.02M
        case REOP_dot:
2890
3.02M
            if (cptr == cbuf_end)
2891
0
                goto no_match;
2892
3.02M
            GET_CHAR(c, cptr, cbuf_end, cbuf_type);
2893
3.02M
            if (is_line_terminator(c))
2894
3.02M
                goto no_match;
2895
2.87k
            break;
2896
2.87k
        case REOP_any:
2897
0
            if (cptr == cbuf_end)
2898
0
                goto no_match;
2899
0
            GET_CHAR(c, cptr, cbuf_end, cbuf_type);
2900
0
            break;
2901
3.02M
        case REOP_save_start:
2902
3.02M
        case REOP_save_end:
2903
3.02M
            val = *pc++;
2904
3.02M
            assert(val < s->capture_count);
2905
3.02M
            capture[2 * val + opcode - REOP_save_start] = (uint8_t *)cptr;
2906
3.02M
            break;
2907
0
        case REOP_save_reset:
2908
0
            {
2909
0
                uint32_t val2;
2910
0
                val = pc[0];
2911
0
                val2 = pc[1];
2912
0
                pc += 2;
2913
0
                assert(val2 < s->capture_count);
2914
0
                while (val <= val2) {
2915
0
                    capture[2 * val] = NULL;
2916
0
                    capture[2 * val + 1] = NULL;
2917
0
                    val++;
2918
0
                }
2919
0
            }
2920
0
            break;
2921
0
        case REOP_push_i32:
2922
0
            val = get_u32(pc);
2923
0
            pc += 4;
2924
0
            stack[stack_len++] = val;
2925
0
            break;
2926
0
        case REOP_drop:
2927
0
            stack_len--;
2928
0
            break;
2929
0
        case REOP_loop:
2930
0
            val = get_u32(pc);
2931
0
            pc += 4;
2932
0
            if (--stack[stack_len - 1] != 0) {
2933
0
                pc += (int)val;
2934
0
                if (lre_poll_timeout(s))
2935
0
                    return LRE_RET_TIMEOUT;
2936
0
            }
2937
0
            break;
2938
0
        case REOP_push_char_pos:
2939
0
            stack[stack_len++] = (uintptr_t)cptr;
2940
0
            break;
2941
0
        case REOP_check_advance:
2942
0
            if (stack[--stack_len] == (uintptr_t)cptr)
2943
0
                goto no_match;
2944
0
            break;
2945
0
        case REOP_word_boundary:
2946
0
        case REOP_word_boundary_i:
2947
0
        case REOP_not_word_boundary:
2948
0
        case REOP_not_word_boundary_i:
2949
0
            {
2950
0
                BOOL v1, v2;
2951
0
                int ignore_case = (opcode == REOP_word_boundary_i || opcode == REOP_not_word_boundary_i);
2952
0
                BOOL is_boundary = (opcode == REOP_word_boundary || opcode == REOP_word_boundary_i);
2953
                /* char before */
2954
0
                if (cptr == s->cbuf) {
2955
0
                    v1 = FALSE;
2956
0
                } else {
2957
0
                    PEEK_PREV_CHAR(c, cptr, s->cbuf, cbuf_type);
2958
0
                    if (ignore_case)
2959
0
                        c = lre_canonicalize(c, s->is_unicode);
2960
0
                    v1 = is_word_char(c);
2961
0
                }
2962
                /* current char */
2963
0
                if (cptr >= cbuf_end) {
2964
0
                    v2 = FALSE;
2965
0
                } else {
2966
0
                    PEEK_CHAR(c, cptr, cbuf_end, cbuf_type);
2967
0
                    if (ignore_case)
2968
0
                        c = lre_canonicalize(c, s->is_unicode);
2969
0
                    v2 = is_word_char(c);
2970
0
                }
2971
0
                if (v1 ^ v2 ^ is_boundary)
2972
0
                    goto no_match;
2973
0
            }
2974
0
            break;
2975
0
        case REOP_back_reference:
2976
0
        case REOP_back_reference_i:
2977
0
        case REOP_backward_back_reference:
2978
0
        case REOP_backward_back_reference_i:
2979
0
            {
2980
0
                const uint8_t *cptr1, *cptr1_end, *cptr1_start;
2981
0
                uint32_t c1, c2;
2982
2983
0
                val = *pc++;
2984
0
                if (val >= s->capture_count)
2985
0
                    goto no_match;
2986
0
                cptr1_start = capture[2 * val];
2987
0
                cptr1_end = capture[2 * val + 1];
2988
0
                if (!cptr1_start || !cptr1_end)
2989
0
                    break;
2990
0
                if (opcode == REOP_back_reference ||
2991
0
                    opcode == REOP_back_reference_i) {
2992
0
                    cptr1 = cptr1_start;
2993
0
                    while (cptr1 < cptr1_end) {
2994
0
                        if (cptr >= cbuf_end)
2995
0
                            goto no_match;
2996
0
                        GET_CHAR(c1, cptr1, cptr1_end, cbuf_type);
2997
0
                        GET_CHAR(c2, cptr, cbuf_end, cbuf_type);
2998
0
                        if (opcode == REOP_back_reference_i) {
2999
0
                            c1 = lre_canonicalize(c1, s->is_unicode);
3000
0
                            c2 = lre_canonicalize(c2, s->is_unicode);
3001
0
                        }
3002
0
                        if (c1 != c2)
3003
0
                            goto no_match;
3004
0
                    }
3005
0
                } else {
3006
0
                    cptr1 = cptr1_end;
3007
0
                    while (cptr1 > cptr1_start) {
3008
0
                        if (cptr == s->cbuf)
3009
0
                            goto no_match;
3010
0
                        GET_PREV_CHAR(c1, cptr1, cptr1_start, cbuf_type);
3011
0
                        GET_PREV_CHAR(c2, cptr, s->cbuf, cbuf_type);
3012
0
                        if (opcode == REOP_backward_back_reference_i) {
3013
0
                            c1 = lre_canonicalize(c1, s->is_unicode);
3014
0
                            c2 = lre_canonicalize(c2, s->is_unicode);
3015
0
                        }
3016
0
                        if (c1 != c2)
3017
0
                            goto no_match;
3018
0
                    }
3019
0
                }
3020
0
            }
3021
0
            break;
3022
0
        case REOP_range:
3023
0
        case REOP_range_i:
3024
0
            {
3025
0
                int n;
3026
0
                uint32_t low, high, idx_min, idx_max, idx;
3027
3028
0
                n = get_u16(pc); /* n must be >= 1 */
3029
0
                pc += 2;
3030
0
                if (cptr >= cbuf_end)
3031
0
                    goto no_match;
3032
0
                GET_CHAR(c, cptr, cbuf_end, cbuf_type);
3033
0
                if (opcode == REOP_range_i) {
3034
0
                    c = lre_canonicalize(c, s->is_unicode);
3035
0
                }
3036
0
                idx_min = 0;
3037
0
                low = get_u16(pc + 0 * 4);
3038
0
                if (c < low)
3039
0
                    goto no_match;
3040
0
                idx_max = n - 1;
3041
0
                high = get_u16(pc + idx_max * 4 + 2);
3042
                /* 0xffff in for last value means +infinity */
3043
0
                if (unlikely(c >= 0xffff) && high == 0xffff)
3044
0
                    goto range_match;
3045
0
                if (c > high)
3046
0
                    goto no_match;
3047
0
                while (idx_min <= idx_max) {
3048
0
                    idx = (idx_min + idx_max) / 2;
3049
0
                    low = get_u16(pc + idx * 4);
3050
0
                    high = get_u16(pc + idx * 4 + 2);
3051
0
                    if (c < low)
3052
0
                        idx_max = idx - 1;
3053
0
                    else if (c > high)
3054
0
                        idx_min = idx + 1;
3055
0
                    else
3056
0
                        goto range_match;
3057
0
                }
3058
0
                goto no_match;
3059
0
            range_match:
3060
0
                pc += 4 * n;
3061
0
            }
3062
0
            break;
3063
0
        case REOP_range32:
3064
0
        case REOP_range32_i:
3065
0
            {
3066
0
                int n;
3067
0
                uint32_t low, high, idx_min, idx_max, idx;
3068
3069
0
                n = get_u16(pc); /* n must be >= 1 */
3070
0
                pc += 2;
3071
0
                if (cptr >= cbuf_end)
3072
0
                    goto no_match;
3073
0
                GET_CHAR(c, cptr, cbuf_end, cbuf_type);
3074
0
                if (opcode == REOP_range32_i) {
3075
0
                    c = lre_canonicalize(c, s->is_unicode);
3076
0
                }
3077
0
                idx_min = 0;
3078
0
                low = get_u32(pc + 0 * 8);
3079
0
                if (c < low)
3080
0
                    goto no_match;
3081
0
                idx_max = n - 1;
3082
0
                high = get_u32(pc + idx_max * 8 + 4);
3083
0
                if (c > high)
3084
0
                    goto no_match;
3085
0
                while (idx_min <= idx_max) {
3086
0
                    idx = (idx_min + idx_max) / 2;
3087
0
                    low = get_u32(pc + idx * 8);
3088
0
                    high = get_u32(pc + idx * 8 + 4);
3089
0
                    if (c < low)
3090
0
                        idx_max = idx - 1;
3091
0
                    else if (c > high)
3092
0
                        idx_min = idx + 1;
3093
0
                    else
3094
0
                        goto range32_match;
3095
0
                }
3096
0
                goto no_match;
3097
0
            range32_match:
3098
0
                pc += 8 * n;
3099
0
            }
3100
0
            break;
3101
0
        case REOP_prev:
3102
            /* go to the previous char */
3103
0
            if (cptr == s->cbuf)
3104
0
                goto no_match;
3105
0
            PREV_CHAR(cptr, s->cbuf, cbuf_type);
3106
0
            break;
3107
0
        case REOP_simple_greedy_quant:
3108
0
            {
3109
0
                uint32_t next_pos, quant_min, quant_max;
3110
0
                size_t q;
3111
0
                intptr_t res;
3112
0
                const uint8_t *pc1;
3113
3114
0
                next_pos = get_u32(pc);
3115
0
                quant_min = get_u32(pc + 4);
3116
0
                quant_max = get_u32(pc + 8);
3117
0
                pc += 16;
3118
0
                pc1 = pc;
3119
0
                pc += (int)next_pos;
3120
3121
0
                q = 0;
3122
0
                for(;;) {
3123
0
                    if (lre_poll_timeout(s))
3124
0
                        return LRE_RET_TIMEOUT;
3125
0
                    res = lre_exec_backtrack(s, capture, stack, stack_len,
3126
0
                                             pc1, cptr, TRUE);
3127
0
                    if (res == LRE_RET_MEMORY_ERROR ||
3128
0
                        res == LRE_RET_TIMEOUT)
3129
0
                        return res;
3130
0
                    if (!res)
3131
0
                        break;
3132
0
                    cptr = (uint8_t *)res;
3133
0
                    q++;
3134
0
                    if (q >= quant_max && quant_max != INT32_MAX)
3135
0
                        break;
3136
0
                }
3137
0
                if (q < quant_min)
3138
0
                    goto no_match;
3139
0
                if (q > quant_min) {
3140
                    /* will examine all matches down to quant_min */
3141
0
                    ret = push_state(s, capture, stack, stack_len,
3142
0
                                     pc1 - 16, cptr,
3143
0
                                     RE_EXEC_STATE_GREEDY_QUANT,
3144
0
                                     q - quant_min);
3145
0
                    if (ret < 0)
3146
0
                        return LRE_RET_MEMORY_ERROR;
3147
0
                }
3148
0
            }
3149
0
            break;
3150
0
        default:
3151
0
            abort();
3152
6.06M
        }
3153
6.06M
    }
3154
3.02M
}
3155
3156
/* Return 1 if match, 0 if not match or < 0 if error (see LRE_RET_x). cindex is the
3157
   starting position of the match and must be such as 0 <= cindex <=
3158
   clen. */
3159
int lre_exec(uint8_t **capture,
3160
             const uint8_t *bc_buf, const uint8_t *cbuf, int cindex, int clen,
3161
             int cbuf_type, void *opaque)
3162
3.02M
{
3163
3.02M
    REExecContext s_s, *s = &s_s;
3164
3.02M
    int re_flags, i, alloca_size, ret;
3165
3.02M
    StackInt *stack_buf;
3166
3.02M
    const uint8_t *cptr;
3167
3168
3.02M
    re_flags = lre_get_flags(bc_buf);
3169
3.02M
    s->is_unicode = (re_flags & (LRE_FLAG_UNICODE | LRE_FLAG_UNICODE_SETS)) != 0;
3170
3.02M
    s->capture_count = bc_buf[RE_HEADER_CAPTURE_COUNT];
3171
3.02M
    s->stack_size_max = bc_buf[RE_HEADER_STACK_SIZE];
3172
3.02M
    s->cbuf = cbuf;
3173
3.02M
    s->cbuf_end = cbuf + (clen << cbuf_type);
3174
3.02M
    s->cbuf_type = cbuf_type;
3175
3.02M
    if (s->cbuf_type == 1 && s->is_unicode)
3176
3.02M
        s->cbuf_type = 2;
3177
3.02M
    s->interrupt_counter = INTERRUPT_COUNTER_INIT;
3178
3.02M
    s->opaque = opaque;
3179
3180
3.02M
    s->state_size = sizeof(REExecState) +
3181
3.02M
        s->capture_count * sizeof(capture[0]) * 2 +
3182
3.02M
        s->stack_size_max * sizeof(stack_buf[0]);
3183
3.02M
    s->state_stack = NULL;
3184
3.02M
    s->state_stack_len = 0;
3185
3.02M
    s->state_stack_size = 0;
3186
3187
9.08M
    for(i = 0; i < s->capture_count * 2; i++)
3188
6.05M
        capture[i] = NULL;
3189
3.02M
    alloca_size = s->stack_size_max * sizeof(stack_buf[0]);
3190
3.02M
    stack_buf = alloca(alloca_size);
3191
3192
3.02M
    cptr = cbuf + (cindex << cbuf_type);
3193
3.02M
    if (0 < cindex && cindex < clen && s->cbuf_type == 2) {
3194
3.02M
        const uint16_t *p = (const uint16_t *)cptr;
3195
3.02M
        if (is_lo_surrogate(*p) && is_hi_surrogate(p[-1])) {
3196
0
            cptr = (const uint8_t *)(p - 1);
3197
0
        }
3198
3.02M
    }
3199
3200
3.02M
    ret = lre_exec_backtrack(s, capture, stack_buf, 0, bc_buf + RE_HEADER_LEN,
3201
3.02M
                             cptr, FALSE);
3202
3.02M
    lre_realloc(s->opaque, s->state_stack, 0);
3203
3.02M
    return ret;
3204
3.02M
}
3205
3206
int lre_get_capture_count(const uint8_t *bc_buf)
3207
3.02M
{
3208
3.02M
    return bc_buf[RE_HEADER_CAPTURE_COUNT];
3209
3.02M
}
3210
3211
int lre_get_flags(const uint8_t *bc_buf)
3212
6.05M
{
3213
6.05M
    return get_u16(bc_buf + RE_HEADER_FLAGS);
3214
6.05M
}
3215
3216
/* Return NULL if no group names. Otherwise, return a pointer to
3217
   'capture_count - 1' zero terminated UTF-8 strings. */
3218
const char *lre_get_groupnames(const uint8_t *bc_buf)
3219
12
{
3220
12
    uint32_t re_bytecode_len;
3221
12
    if ((lre_get_flags(bc_buf) & LRE_FLAG_NAMED_GROUPS) == 0)
3222
12
        return NULL;
3223
0
    re_bytecode_len = get_u32(bc_buf + RE_HEADER_BYTECODE_LEN);
3224
0
    return (const char *)(bc_buf + RE_HEADER_LEN + re_bytecode_len);
3225
12
}
3226
3227
#ifdef TEST
3228
3229
BOOL lre_check_stack_overflow(void *opaque, size_t alloca_size)
3230
{
3231
    return FALSE;
3232
}
3233
3234
void *lre_realloc(void *opaque, void *ptr, size_t size)
3235
{
3236
    return realloc(ptr, size);
3237
}
3238
3239
int main(int argc, char **argv)
3240
{
3241
    int len, flags, ret, i;
3242
    uint8_t *bc;
3243
    char error_msg[64];
3244
    uint8_t *capture[CAPTURE_COUNT_MAX * 2];
3245
    const char *input;
3246
    int input_len, capture_count;
3247
3248
    if (argc < 4) {
3249
        printf("usage: %s regexp flags input\n", argv[0]);
3250
        return 1;
3251
    }
3252
    flags = atoi(argv[2]);
3253
    bc = lre_compile(&len, error_msg, sizeof(error_msg), argv[1],
3254
                     strlen(argv[1]), flags, NULL);
3255
    if (!bc) {
3256
        fprintf(stderr, "error: %s\n", error_msg);
3257
        exit(1);
3258
    }
3259
3260
    input = argv[3];
3261
    input_len = strlen(input);
3262
3263
    ret = lre_exec(capture, bc, (uint8_t *)input, 0, input_len, 0, NULL);
3264
    printf("ret=%d\n", ret);
3265
    if (ret == 1) {
3266
        capture_count = lre_get_capture_count(bc);
3267
        for(i = 0; i < 2 * capture_count; i++) {
3268
            uint8_t *ptr;
3269
            ptr = capture[i];
3270
            printf("%d: ", i);
3271
            if (!ptr)
3272
                printf("<nil>");
3273
            else
3274
                printf("%u", (int)(ptr - (uint8_t *)input));
3275
            printf("\n");
3276
        }
3277
    }
3278
    return 0;
3279
}
3280
#endif