forked from kkos/oniguruma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regcomp.c
8580 lines (7278 loc) · 196 KB
/
regcomp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
regcomp.c - Oniguruma (regular expression library)
**********************************************************************/
/*-
* Copyright (c) 2002-2022 K.Kosako
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "regparse.h"
#define OPS_INIT_SIZE 8
#define ND_IS_REAL_IGNORECASE(node) \
(ND_IS_IGNORECASE(node) && !ND_STRING_IS_CRUDE(node))
typedef struct {
OnigLen min;
OnigLen max;
} MinMaxLen;
typedef struct {
OnigLen min;
OnigLen max;
int min_is_sure;
} MinMaxCharLen;
OnigCaseFoldType OnigDefaultCaseFoldFlag = ONIGENC_CASE_FOLD_MIN;
static OnigLen node_min_byte_len(Node* node, ParseEnv* env);
static int
ops_init(regex_t* reg, int init_alloc_size)
{
Operation* p;
size_t size;
if (init_alloc_size <= 0)
return ONIGERR_PARSER_BUG;
size = sizeof(Operation) * init_alloc_size;
p = (Operation* )xrealloc(reg->ops, size);
CHECK_NULL_RETURN_MEMERR(p);
reg->ops = p;
#ifdef USE_DIRECT_THREADED_CODE
{
enum OpCode* cp;
size = sizeof(enum OpCode) * init_alloc_size;
cp = (enum OpCode* )xrealloc(reg->ocs, size);
CHECK_NULL_RETURN_MEMERR(cp);
reg->ocs = cp;
}
#endif
reg->ops_curr = 0; /* !!! not yet done ops_new() */
reg->ops_alloc = init_alloc_size;
reg->ops_used = 0;
return ONIG_NORMAL;
}
static int
ops_resize(regex_t* reg, int n)
{
#ifdef USE_DIRECT_THREADED_CODE
enum OpCode* cp;
#endif
Operation* p;
size_t size;
if (n == reg->ops_alloc) return ONIG_NORMAL;
if (n <= 0) return ONIGERR_PARSER_BUG;
size = sizeof(Operation) * n;
p = (Operation* )xrealloc(reg->ops, size);
CHECK_NULL_RETURN_MEMERR(p);
reg->ops = p;
#ifdef USE_DIRECT_THREADED_CODE
size = sizeof(enum OpCode) * n;
cp = (enum OpCode* )xrealloc(reg->ocs, size);
CHECK_NULL_RETURN_MEMERR(cp);
reg->ocs = cp;
#endif
reg->ops_alloc = n;
if (reg->ops_used == 0)
reg->ops_curr = 0;
else
reg->ops_curr = reg->ops + (reg->ops_used - 1);
return ONIG_NORMAL;
}
static int
ops_new(regex_t* reg)
{
if (reg->ops_used >= reg->ops_alloc) {
int r = ops_resize(reg, reg->ops_alloc << 1);
if (r != ONIG_NORMAL) return r;
}
reg->ops_curr = reg->ops + reg->ops_used;
reg->ops_used++;
xmemset(reg->ops_curr, 0, sizeof(Operation));
return ONIG_NORMAL;
}
static int
is_in_string_pool(regex_t* reg, UChar* s)
{
return (s >= reg->string_pool && s < reg->string_pool_end);
}
static void
ops_free(regex_t* reg)
{
int i;
if (IS_NULL(reg->ops)) return ;
for (i = 0; i < (int )reg->ops_used; i++) {
enum OpCode opcode;
Operation* op;
op = reg->ops + i;
#ifdef USE_DIRECT_THREADED_CODE
opcode = *(reg->ocs + i);
#else
opcode = op->opcode;
#endif
switch (opcode) {
case OP_STR_MBN:
if (! is_in_string_pool(reg, op->exact_len_n.s))
xfree(op->exact_len_n.s);
break;
case OP_STR_N: case OP_STR_MB2N: case OP_STR_MB3N:
if (! is_in_string_pool(reg, op->exact_n.s))
xfree(op->exact_n.s);
break;
case OP_STR_1: case OP_STR_2: case OP_STR_3: case OP_STR_4:
case OP_STR_5: case OP_STR_MB2N1: case OP_STR_MB2N2:
case OP_STR_MB2N3:
break;
case OP_CCLASS_NOT: case OP_CCLASS:
xfree(op->cclass.bsp);
break;
case OP_CCLASS_MB_NOT: case OP_CCLASS_MB:
xfree(op->cclass_mb.mb);
break;
case OP_CCLASS_MIX_NOT: case OP_CCLASS_MIX:
xfree(op->cclass_mix.mb);
xfree(op->cclass_mix.bsp);
break;
case OP_BACKREF1: case OP_BACKREF2: case OP_BACKREF_N: case OP_BACKREF_N_IC:
break;
case OP_BACKREF_MULTI: case OP_BACKREF_MULTI_IC:
case OP_BACKREF_CHECK:
#ifdef USE_BACKREF_WITH_LEVEL
case OP_BACKREF_WITH_LEVEL:
case OP_BACKREF_WITH_LEVEL_IC:
case OP_BACKREF_CHECK_WITH_LEVEL:
#endif
if (op->backref_general.num != 1)
xfree(op->backref_general.ns);
break;
default:
break;
}
}
xfree(reg->ops);
#ifdef USE_DIRECT_THREADED_CODE
xfree(reg->ocs);
reg->ocs = 0;
#endif
reg->ops = 0;
reg->ops_curr = 0;
reg->ops_alloc = 0;
reg->ops_used = 0;
}
static int
ops_calc_size_of_string_pool(regex_t* reg)
{
int i;
int total;
if (IS_NULL(reg->ops)) return 0;
total = 0;
for (i = 0; i < (int )reg->ops_used; i++) {
enum OpCode opcode;
Operation* op;
op = reg->ops + i;
#ifdef USE_DIRECT_THREADED_CODE
opcode = *(reg->ocs + i);
#else
opcode = op->opcode;
#endif
switch (opcode) {
case OP_STR_MBN:
total += op->exact_len_n.len * op->exact_len_n.n;
break;
case OP_STR_N:
case OP_STR_MB2N:
total += op->exact_n.n * 2;
break;
case OP_STR_MB3N:
total += op->exact_n.n * 3;
break;
default:
break;
}
}
return total;
}
static int
ops_make_string_pool(regex_t* reg)
{
int i;
int len;
int size;
UChar* pool;
UChar* curr;
size = ops_calc_size_of_string_pool(reg);
if (size <= 0) {
return 0;
}
curr = pool = (UChar* )xmalloc((size_t )size);
CHECK_NULL_RETURN_MEMERR(pool);
for (i = 0; i < (int )reg->ops_used; i++) {
enum OpCode opcode;
Operation* op;
op = reg->ops + i;
#ifdef USE_DIRECT_THREADED_CODE
opcode = *(reg->ocs + i);
#else
opcode = op->opcode;
#endif
switch (opcode) {
case OP_STR_MBN:
len = op->exact_len_n.len * op->exact_len_n.n;
xmemcpy(curr, op->exact_len_n.s, len);
xfree(op->exact_len_n.s);
op->exact_len_n.s = curr;
curr += len;
break;
case OP_STR_N:
len = op->exact_n.n;
copy:
xmemcpy(curr, op->exact_n.s, len);
xfree(op->exact_n.s);
op->exact_n.s = curr;
curr += len;
break;
case OP_STR_MB2N:
len = op->exact_n.n * 2;
goto copy;
break;
case OP_STR_MB3N:
len = op->exact_n.n * 3;
goto copy;
break;
default:
break;
}
}
reg->string_pool = pool;
reg->string_pool_end = pool + size;
return 0;
}
extern OnigCaseFoldType
onig_get_default_case_fold_flag(void)
{
return OnigDefaultCaseFoldFlag;
}
extern int
onig_set_default_case_fold_flag(OnigCaseFoldType case_fold_flag)
{
OnigDefaultCaseFoldFlag = case_fold_flag;
return 0;
}
static int
len_multiply_cmp(OnigLen x, int y, OnigLen v)
{
if (x == 0 || y == 0) return -1;
if (x < INFINITE_LEN / y) {
OnigLen xy = x * (OnigLen )y;
if (xy > v) return 1;
else {
if (xy == v) return 0;
else return -1;
}
}
else
return v == INFINITE_LEN ? 0 : 1;
}
extern int
onig_positive_int_multiply(int x, int y)
{
if (x == 0 || y == 0) return 0;
if (x < ONIG_INT_MAX / y)
return x * y;
else
return -1;
}
static void
node_swap(Node* a, Node* b)
{
Node c;
c = *a; *a = *b; *b = c;
if (ND_TYPE(a) == ND_STRING) {
StrNode* sn = STR_(a);
if (sn->capacity == 0) {
int len = (int )(sn->end - sn->s);
sn->s = sn->buf;
sn->end = sn->s + len;
}
}
if (ND_TYPE(b) == ND_STRING) {
StrNode* sn = STR_(b);
if (sn->capacity == 0) {
int len = (int )(sn->end - sn->s);
sn->s = sn->buf;
sn->end = sn->s + len;
}
}
}
static int
node_list_len(Node* list)
{
int len;
len = 1;
while (IS_NOT_NULL(ND_CDR(list))) {
list = ND_CDR(list);
len++;
}
return len;
}
static Node*
node_list_add(Node* list, Node* x)
{
Node *n;
n = onig_node_new_list(x, NULL);
if (IS_NULL(n)) return NULL_NODE;
if (IS_NOT_NULL(list)) {
while (IS_NOT_NULL(ND_CDR(list)))
list = ND_CDR(list);
ND_CDR(list) = n;
}
return n;
}
static int
node_str_node_cat(Node* node, Node* add)
{
int r;
if (ND_STATUS(node) != ND_STATUS(add))
return ONIGERR_TYPE_BUG;
if (STR_(node)->flag != STR_(add)->flag)
return ONIGERR_TYPE_BUG;
r = onig_node_str_cat(node, STR_(add)->s, STR_(add)->end);
if (r != 0) return r;
return 0;
}
static void
node_conv_to_str_node(Node* node, Node* ref_node)
{
xmemset(node, 0, sizeof(*node));
ND_SET_TYPE(node, ND_STRING);
ND_STATUS(node) = ND_STATUS(ref_node);
STR_(node)->flag = STR_(ref_node)->flag;
STR_(node)->s = STR_(node)->buf;
STR_(node)->end = STR_(node)->buf;
STR_(node)->capacity = 0;
}
static OnigLen
distance_add(OnigLen d1, OnigLen d2)
{
if (d1 == INFINITE_LEN || d2 == INFINITE_LEN)
return INFINITE_LEN;
else {
if (d1 <= INFINITE_LEN - d2) return d1 + d2;
else return INFINITE_LEN;
}
}
static OnigLen
distance_multiply(OnigLen d, int m)
{
if (m == 0) return 0;
if (d < INFINITE_LEN / m)
return d * m;
else
return INFINITE_LEN;
}
static int
bitset_is_empty(BitSetRef bs)
{
int i;
for (i = 0; i < (int )BITSET_REAL_SIZE; i++) {
if (bs[i] != 0) return 0;
}
return 1;
}
#ifdef USE_CALL
static int
unset_addr_list_init(UnsetAddrList* list, int size)
{
UnsetAddr* p = (UnsetAddr* )xmalloc(sizeof(UnsetAddr)* size);
CHECK_NULL_RETURN_MEMERR(p);
list->num = 0;
list->alloc = size;
list->us = p;
return 0;
}
static void
unset_addr_list_end(UnsetAddrList* list)
{
if (IS_NOT_NULL(list->us))
xfree(list->us);
}
static int
unset_addr_list_add(UnsetAddrList* list, int offset, struct _Node* node)
{
UnsetAddr* p;
int size;
if (list->num >= list->alloc) {
size = list->alloc * 2;
p = (UnsetAddr* )xrealloc(list->us, sizeof(UnsetAddr) * size);
CHECK_NULL_RETURN_MEMERR(p);
list->alloc = size;
list->us = p;
}
list->us[list->num].offset = offset;
list->us[list->num].target = node;
list->num++;
return 0;
}
#endif /* USE_CALL */
enum CharLenReturnType {
CHAR_LEN_NORMAL = 0, /* fixed or variable */
CHAR_LEN_TOP_ALT_FIXED = 1
};
static int
mmcl_fixed(MinMaxCharLen* c)
{
return (c->min == c->max && c->min != INFINITE_LEN);
}
static void
mmcl_set(MinMaxCharLen* l, OnigLen len)
{
l->min = len;
l->max = len;
l->min_is_sure = TRUE;
}
static void
mmcl_set_min_max(MinMaxCharLen* l, OnigLen min, OnigLen max, int min_is_sure)
{
l->min = min;
l->max = max;
l->min_is_sure = min_is_sure;
}
static void
mmcl_add(MinMaxCharLen* to, MinMaxCharLen* add)
{
to->min = distance_add(to->min, add->min);
to->max = distance_add(to->max, add->max);
to->min_is_sure = add->min_is_sure != FALSE && to->min_is_sure != FALSE;
}
static void
mmcl_multiply(MinMaxCharLen* to, int m)
{
to->min = distance_multiply(to->min, m);
to->max = distance_multiply(to->max, m);
}
static void
mmcl_repeat_range_multiply(MinMaxCharLen* to, int mlow, int mhigh)
{
to->min = distance_multiply(to->min, mlow);
if (IS_INFINITE_REPEAT(mhigh))
to->max = INFINITE_LEN;
else
to->max = distance_multiply(to->max, mhigh);
}
static void
mmcl_alt_merge(MinMaxCharLen* to, MinMaxCharLen* alt)
{
if (to->min > alt->min) {
to->min = alt->min;
to->min_is_sure = alt->min_is_sure;
}
else if (to->min == alt->min) {
if (alt->min_is_sure != FALSE)
to->min_is_sure = TRUE;
}
if (to->max < alt->max) to->max = alt->max;
}
#ifndef ONIG_DONT_OPTIMIZE
static int
mml_is_equal(MinMaxLen* a, MinMaxLen* b)
{
return a->min == b->min && a->max == b->max;
}
static void
mml_set_min_max(MinMaxLen* l, OnigLen min, OnigLen max)
{
l->min = min;
l->max = max;
}
static void
mml_clear(MinMaxLen* l)
{
l->min = l->max = 0;
}
static void
mml_copy(MinMaxLen* to, MinMaxLen* from)
{
to->min = from->min;
to->max = from->max;
}
static void
mml_add(MinMaxLen* to, MinMaxLen* add)
{
to->min = distance_add(to->min, add->min);
to->max = distance_add(to->max, add->max);
}
static void
mml_alt_merge(MinMaxLen* to, MinMaxLen* alt)
{
if (to->min > alt->min) to->min = alt->min;
if (to->max < alt->max) to->max = alt->max;
}
#endif
/* fixed size pattern node only */
static int
node_char_len1(Node* node, regex_t* reg, MinMaxCharLen* ci, ParseEnv* env,
int level)
{
MinMaxCharLen tci;
int r = CHAR_LEN_NORMAL;
level++;
switch (ND_TYPE(node)) {
case ND_LIST:
{
int first = TRUE;
do {
r = node_char_len1(ND_CAR(node), reg, &tci, env, level);
if (r < 0) break;
if (first == TRUE) {
*ci = tci;
first = FALSE;
}
else
mmcl_add(ci, &tci);
} while (IS_NOT_NULL(node = ND_CDR(node)));
}
break;
case ND_ALT:
{
int fixed;
r = node_char_len1(ND_CAR(node), reg, ci, env, level);
if (r < 0) break;
fixed = TRUE;
while (IS_NOT_NULL(node = ND_CDR(node))) {
r = node_char_len1(ND_CAR(node), reg, &tci, env, level);
if (r < 0) break;
if (! mmcl_fixed(&tci))
fixed = FALSE;
mmcl_alt_merge(ci, &tci);
}
if (r < 0) break;
r = CHAR_LEN_NORMAL;
if (mmcl_fixed(ci)) break;
if (fixed == TRUE && level == 1) {
r = CHAR_LEN_TOP_ALT_FIXED;
}
}
break;
case ND_STRING:
{
OnigLen clen;
StrNode* sn = STR_(node);
UChar *s = sn->s;
if (ND_IS_REAL_IGNORECASE(node) &&
CASE_FOLD_IS_NOT_ASCII_ONLY(env->case_fold_flag)) {
/* Such a case is possible.
ex. /(?i)(?<=\1)(a)/
Backref node refer to capture group, but it doesn't tune yet.
*/
r = ONIGERR_INVALID_LOOK_BEHIND_PATTERN;
break;
}
clen = 0;
while (s < sn->end) {
s += enclen(reg->enc, s);
clen = distance_add(clen, 1);
}
mmcl_set(ci, clen);
}
break;
case ND_QUANT:
{
QuantNode* qn = QUANT_(node);
if (qn->lower == qn->upper) {
if (qn->upper == 0) {
mmcl_set(ci, 0);
}
else {
r = node_char_len1(ND_BODY(node), reg, ci, env, level);
if (r < 0) break;
mmcl_multiply(ci, qn->lower);
}
}
else {
r = node_char_len1(ND_BODY(node), reg, ci, env, level);
if (r < 0) break;
mmcl_repeat_range_multiply(ci, qn->lower, qn->upper);
}
}
break;
#ifdef USE_CALL
case ND_CALL:
if (ND_IS_RECURSION(node))
mmcl_set_min_max(ci, 0, INFINITE_LEN, FALSE);
else
r = node_char_len1(ND_BODY(node), reg, ci, env, level);
break;
#endif
case ND_CTYPE:
case ND_CCLASS:
mmcl_set(ci, 1);
break;
case ND_BAG:
{
BagNode* en = BAG_(node);
switch (en->type) {
case BAG_MEMORY:
if (ND_IS_FIXED_CLEN(node)) {
mmcl_set_min_max(ci, en->min_char_len, en->max_char_len,
ND_IS_FIXED_CLEN_MIN_SURE(node));
}
else {
if (ND_IS_MARK1(node)) {
mmcl_set_min_max(ci, 0, INFINITE_LEN, FALSE);
}
else {
ND_STATUS_ADD(node, MARK1);
r = node_char_len1(ND_BODY(node), reg, ci, env, level);
ND_STATUS_REMOVE(node, MARK1);
if (r < 0) break;
en->min_char_len = ci->min;
en->max_char_len = ci->max;
ND_STATUS_ADD(node, FIXED_CLEN);
if (ci->min_is_sure != FALSE)
ND_STATUS_ADD(node, FIXED_CLEN_MIN_SURE);
}
}
/* can't optimize look-behind if capture exists. */
ci->min_is_sure = FALSE;
break;
case BAG_OPTION:
case BAG_STOP_BACKTRACK:
r = node_char_len1(ND_BODY(node), reg, ci, env, level);
break;
case BAG_IF_ELSE:
{
MinMaxCharLen eci;
r = node_char_len1(ND_BODY(node), reg, ci, env, level);
if (r < 0) break;
if (IS_NOT_NULL(en->te.Then)) {
r = node_char_len1(en->te.Then, reg, &tci, env, level);
if (r < 0) break;
mmcl_add(ci, &tci);
}
if (IS_NOT_NULL(en->te.Else)) {
r = node_char_len1(en->te.Else, reg, &eci, env, level);
if (r < 0) break;
}
else {
mmcl_set(&eci, 0);
}
mmcl_alt_merge(ci, &eci);
}
break;
default: /* never come here */
r = ONIGERR_PARSER_BUG;
break;
}
}
break;
case ND_GIMMICK:
mmcl_set(ci, 0);
break;
case ND_ANCHOR:
zero:
mmcl_set(ci, 0);
/* can't optimize look-behind if anchor exists. */
ci->min_is_sure = FALSE;
break;
case ND_BACKREF:
if (ND_IS_CHECKER(node))
goto zero;
if (ND_IS_RECURSION(node)) {
#ifdef USE_BACKREF_WITH_LEVEL
if (ND_IS_NEST_LEVEL(node)) {
mmcl_set_min_max(ci, 0, INFINITE_LEN, FALSE);
break;
}
#endif
mmcl_set_min_max(ci, 0, 0, FALSE);
break;
}
{
int i;
int* backs;
MemEnv* mem_env = PARSEENV_MEMENV(env);
BackRefNode* br = BACKREF_(node);
backs = BACKREFS_P(br);
r = node_char_len1(mem_env[backs[0]].mem_node, reg, ci, env, level);
if (r < 0) break;
if (! mmcl_fixed(ci)) ci->min_is_sure = FALSE;
for (i = 1; i < br->back_num; i++) {
r = node_char_len1(mem_env[backs[i]].mem_node, reg, &tci, env, level);
if (r < 0) break;
if (! mmcl_fixed(&tci)) tci.min_is_sure = FALSE;
mmcl_alt_merge(ci, &tci);
}
}
break;
default: /* never come here */
r = ONIGERR_PARSER_BUG;
break;
}
return r;
}
static int
node_char_len(Node* node, regex_t* reg, MinMaxCharLen* ci, ParseEnv* env)
{
return node_char_len1(node, reg, ci, env, 0);
}
static int
add_op(regex_t* reg, int opcode)
{
int r;
r = ops_new(reg);
if (r != ONIG_NORMAL) return r;
#ifdef USE_DIRECT_THREADED_CODE
*(reg->ocs + (reg->ops_curr - reg->ops)) = opcode;
#else
reg->ops_curr->opcode = opcode;
#endif
return 0;
}
static int compile_length_tree(Node* node, regex_t* reg, ParseEnv* env);
static int compile_tree(Node* node, regex_t* reg, ParseEnv* env);
#define IS_NEED_STR_LEN_OP(op) \
((op) == OP_STR_N || (op) == OP_STR_MB2N ||\
(op) == OP_STR_MB3N || (op) == OP_STR_MBN)
static int
select_str_opcode(int mb_len, int str_len)
{
int op;
switch (mb_len) {
case 1:
switch (str_len) {
case 1: op = OP_STR_1; break;
case 2: op = OP_STR_2; break;
case 3: op = OP_STR_3; break;
case 4: op = OP_STR_4; break;
case 5: op = OP_STR_5; break;
default: op = OP_STR_N; break;
}
break;
case 2:
switch (str_len) {
case 1: op = OP_STR_MB2N1; break;
case 2: op = OP_STR_MB2N2; break;
case 3: op = OP_STR_MB2N3; break;
default: op = OP_STR_MB2N; break;
}
break;
case 3:
op = OP_STR_MB3N;
break;
default:
op = OP_STR_MBN;
break;
}
return op;
}
static int
is_strict_real_node(Node* node)
{
switch (ND_TYPE(node)) {
case ND_STRING:
{
StrNode* sn = STR_(node);
return (sn->end != sn->s);
}
break;
case ND_CCLASS:
case ND_CTYPE:
return 1;
break;
default:
return 0;
break;
}
}
static int
compile_quant_body_with_empty_check(QuantNode* qn, regex_t* reg, ParseEnv* env)
{
int r;
int saved_num_empty_check;
int emptiness;
Node* body;
body = ND_BODY((Node* )qn);
emptiness = qn->emptiness;
saved_num_empty_check = reg->num_empty_check;
if (emptiness != BODY_IS_NOT_EMPTY) {
r = add_op(reg, OP_EMPTY_CHECK_START);
if (r != 0) return r;
COP(reg)->empty_check_start.mem = reg->num_empty_check; /* NULL CHECK ID */
reg->num_empty_check++;
}
r = compile_tree(body, reg, env);
if (r != 0) return r;
if (emptiness != BODY_IS_NOT_EMPTY) {
if (emptiness == BODY_MAY_BE_EMPTY)
r = add_op(reg, OP_EMPTY_CHECK_END);
else if (emptiness == BODY_MAY_BE_EMPTY_MEM) {
if (ND_IS_EMPTY_STATUS_CHECK(qn) != 0 && qn->empty_status_mem != 0) {
r = add_op(reg, OP_EMPTY_CHECK_END_MEMST);
if (r != 0) return r;
COP(reg)->empty_check_end.empty_status_mem = qn->empty_status_mem;
}
else
r = add_op(reg, OP_EMPTY_CHECK_END);
}
#ifdef USE_CALL
else if (emptiness == BODY_MAY_BE_EMPTY_REC) {
r = add_op(reg, OP_EMPTY_CHECK_END_MEMST_PUSH);
if (r != 0) return r;
COP(reg)->empty_check_end.empty_status_mem = qn->empty_status_mem;
}
#endif
if (r != 0) return r;