-
Notifications
You must be signed in to change notification settings - Fork 36
/
php_pcre.c
3058 lines (2676 loc) · 86.5 KB
/
php_pcre.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
/*
+----------------------------------------------------------------------+
| Copyright (c) The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Andrei Zmievski <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php.h"
#include "php_ini.h"
#include "php_globals.h"
#include "php_pcre.h"
#include "php_pcre_arginfo.h"
#include "ext/standard/info.h"
#include "ext/standard/basic_functions.h"
#include "zend_smart_str.h"
#include "SAPI.h"
#include "ext/standard/php_string.h"
#define PREG_PATTERN_ORDER 1
#define PREG_SET_ORDER 2
#define PREG_OFFSET_CAPTURE (1<<8)
#define PREG_UNMATCHED_AS_NULL (1<<9)
#define PREG_SPLIT_NO_EMPTY (1<<0)
#define PREG_SPLIT_DELIM_CAPTURE (1<<1)
#define PREG_SPLIT_OFFSET_CAPTURE (1<<2)
#define PREG_REPLACE_EVAL (1<<0)
#define PREG_GREP_INVERT (1<<0)
#define PREG_JIT (1<<3)
#define PCRE_CACHE_SIZE 4096
struct _pcre_cache_entry {
pcre2_code *re;
uint32_t preg_options;
uint32_t capture_count;
uint32_t name_count;
uint32_t compile_options;
uint32_t refcount;
};
PHPAPI ZEND_DECLARE_MODULE_GLOBALS(pcre)
#ifdef HAVE_PCRE_JIT_SUPPORT
#define PCRE_JIT_STACK_MIN_SIZE (32 * 1024)
#define PCRE_JIT_STACK_MAX_SIZE (192 * 1024)
ZEND_TLS pcre2_jit_stack *jit_stack = NULL;
#endif
/* General context using (infallible) system allocator. */
ZEND_TLS pcre2_general_context *gctx = NULL;
/* These two are global per thread for now. Though it is possible to use these
per pattern. Either one can copy it and use in pce, or one does no global
contexts at all, but creates for every pce. */
ZEND_TLS pcre2_compile_context *cctx = NULL;
ZEND_TLS pcre2_match_context *mctx = NULL;
ZEND_TLS pcre2_match_data *mdata = NULL;
ZEND_TLS bool mdata_used = 0;
ZEND_TLS uint8_t pcre2_init_ok = 0;
#if defined(ZTS) && defined(HAVE_PCRE_JIT_SUPPORT)
static MUTEX_T pcre_mt = NULL;
#define php_pcre_mutex_alloc() \
if (tsrm_is_main_thread() && !pcre_mt) pcre_mt = tsrm_mutex_alloc();
#define php_pcre_mutex_free() \
if (tsrm_is_main_thread() && pcre_mt) { tsrm_mutex_free(pcre_mt); pcre_mt = NULL; }
#define php_pcre_mutex_lock() tsrm_mutex_lock(pcre_mt);
#define php_pcre_mutex_unlock() tsrm_mutex_unlock(pcre_mt);
#else
#define php_pcre_mutex_alloc()
#define php_pcre_mutex_free()
#define php_pcre_mutex_lock()
#define php_pcre_mutex_unlock()
#endif
ZEND_TLS HashTable char_tables;
static void php_pcre_free_char_table(zval *data)
{/*{{{*/
void *ptr = Z_PTR_P(data);
pefree(ptr, 1);
}/*}}}*/
static void pcre_handle_exec_error(int pcre_code) /* {{{ */
{
int preg_code = 0;
switch (pcre_code) {
case PCRE2_ERROR_MATCHLIMIT:
preg_code = PHP_PCRE_BACKTRACK_LIMIT_ERROR;
break;
case PCRE2_ERROR_RECURSIONLIMIT:
preg_code = PHP_PCRE_RECURSION_LIMIT_ERROR;
break;
case PCRE2_ERROR_BADUTFOFFSET:
preg_code = PHP_PCRE_BAD_UTF8_OFFSET_ERROR;
break;
#ifdef HAVE_PCRE_JIT_SUPPORT
case PCRE2_ERROR_JIT_STACKLIMIT:
preg_code = PHP_PCRE_JIT_STACKLIMIT_ERROR;
break;
#endif
default:
if (pcre_code <= PCRE2_ERROR_UTF8_ERR1 && pcre_code >= PCRE2_ERROR_UTF8_ERR21) {
preg_code = PHP_PCRE_BAD_UTF8_ERROR;
} else {
preg_code = PHP_PCRE_INTERNAL_ERROR;
}
break;
}
PCRE_G(error_code) = preg_code;
}
/* }}} */
static const char *php_pcre_get_error_msg(php_pcre_error_code error_code) /* {{{ */
{
switch (error_code) {
case PHP_PCRE_NO_ERROR:
return "No error";
case PHP_PCRE_INTERNAL_ERROR:
return "Internal error";
case PHP_PCRE_BAD_UTF8_ERROR:
return "Malformed UTF-8 characters, possibly incorrectly encoded";
case PHP_PCRE_BAD_UTF8_OFFSET_ERROR:
return "The offset did not correspond to the beginning of a valid UTF-8 code point";
case PHP_PCRE_BACKTRACK_LIMIT_ERROR:
return "Backtrack limit exhausted";
case PHP_PCRE_RECURSION_LIMIT_ERROR:
return "Recursion limit exhausted";
#ifdef HAVE_PCRE_JIT_SUPPORT
case PHP_PCRE_JIT_STACKLIMIT_ERROR:
return "JIT stack limit exhausted";
#endif
default:
return "Unknown error";
}
}
/* }}} */
static void php_free_pcre_cache(zval *data) /* {{{ */
{
pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data);
if (!pce) return;
pcre2_code_free(pce->re);
free(pce);
}
/* }}} */
static void php_efree_pcre_cache(zval *data) /* {{{ */
{
pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data);
if (!pce) return;
pcre2_code_free(pce->re);
efree(pce);
}
/* }}} */
static void *php_pcre_malloc(PCRE2_SIZE size, void *data)
{
return pemalloc(size, 1);
}
static void php_pcre_free(void *block, void *data)
{
pefree(block, 1);
}
static void *php_pcre_emalloc(PCRE2_SIZE size, void *data)
{
return emalloc(size);
}
static void php_pcre_efree(void *block, void *data)
{
efree(block);
}
#ifdef PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
/* pcre 10.38 needs PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK, disabled by default */
#define PHP_PCRE_DEFAULT_EXTRA_COPTIONS PCRE2_EXTRA_ALLOW_LOOKAROUND_BSK
#else
#define PHP_PCRE_DEFAULT_EXTRA_COPTIONS 0
#endif
#define PHP_PCRE_PREALLOC_MDATA_SIZE 32
static void php_pcre_init_pcre2(uint8_t jit)
{/*{{{*/
if (!gctx) {
gctx = pcre2_general_context_create(php_pcre_malloc, php_pcre_free, NULL);
if (!gctx) {
pcre2_init_ok = 0;
return;
}
}
if (!cctx) {
cctx = pcre2_compile_context_create(gctx);
if (!cctx) {
pcre2_init_ok = 0;
return;
}
}
pcre2_set_compile_extra_options(cctx, PHP_PCRE_DEFAULT_EXTRA_COPTIONS);
if (!mctx) {
mctx = pcre2_match_context_create(gctx);
if (!mctx) {
pcre2_init_ok = 0;
return;
}
}
#ifdef HAVE_PCRE_JIT_SUPPORT
if (jit && !jit_stack) {
jit_stack = pcre2_jit_stack_create(PCRE_JIT_STACK_MIN_SIZE, PCRE_JIT_STACK_MAX_SIZE, gctx);
if (!jit_stack) {
pcre2_init_ok = 0;
return;
}
}
#endif
if (!mdata) {
mdata = pcre2_match_data_create(PHP_PCRE_PREALLOC_MDATA_SIZE, gctx);
if (!mdata) {
pcre2_init_ok = 0;
return;
}
}
pcre2_init_ok = 1;
}/*}}}*/
static void php_pcre_shutdown_pcre2(void)
{/*{{{*/
if (gctx) {
pcre2_general_context_free(gctx);
gctx = NULL;
}
if (cctx) {
pcre2_compile_context_free(cctx);
cctx = NULL;
}
if (mctx) {
pcre2_match_context_free(mctx);
mctx = NULL;
}
#ifdef HAVE_PCRE_JIT_SUPPORT
/* Stack may only be destroyed when no cached patterns
possibly associated with it do exist. */
if (jit_stack) {
pcre2_jit_stack_free(jit_stack);
jit_stack = NULL;
}
#endif
if (mdata) {
pcre2_match_data_free(mdata);
mdata = NULL;
}
pcre2_init_ok = 0;
}/*}}}*/
static PHP_GINIT_FUNCTION(pcre) /* {{{ */
{
php_pcre_mutex_alloc();
/* If we're on the CLI SAPI, there will only be one request, so we don't need the
* cache to survive after RSHUTDOWN. */
pcre_globals->per_request_cache = strcmp(sapi_module.name, "cli") == 0;
if (!pcre_globals->per_request_cache) {
zend_hash_init(&pcre_globals->pcre_cache, 0, NULL, php_free_pcre_cache, 1);
}
pcre_globals->backtrack_limit = 0;
pcre_globals->recursion_limit = 0;
pcre_globals->error_code = PHP_PCRE_NO_ERROR;
ZVAL_UNDEF(&pcre_globals->unmatched_null_pair);
ZVAL_UNDEF(&pcre_globals->unmatched_empty_pair);
#ifdef HAVE_PCRE_JIT_SUPPORT
pcre_globals->jit = 1;
#endif
php_pcre_init_pcre2(1);
zend_hash_init(&char_tables, 1, NULL, php_pcre_free_char_table, 1);
}
/* }}} */
static PHP_GSHUTDOWN_FUNCTION(pcre) /* {{{ */
{
if (!pcre_globals->per_request_cache) {
zend_hash_destroy(&pcre_globals->pcre_cache);
}
php_pcre_shutdown_pcre2();
zend_hash_destroy(&char_tables);
php_pcre_mutex_free();
}
/* }}} */
static PHP_INI_MH(OnUpdateBacktrackLimit)
{/*{{{*/
OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
if (mctx) {
pcre2_set_match_limit(mctx, (uint32_t)PCRE_G(backtrack_limit));
}
return SUCCESS;
}/*}}}*/
static PHP_INI_MH(OnUpdateRecursionLimit)
{/*{{{*/
OnUpdateLong(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
if (mctx) {
pcre2_set_depth_limit(mctx, (uint32_t)PCRE_G(recursion_limit));
}
return SUCCESS;
}/*}}}*/
#ifdef HAVE_PCRE_JIT_SUPPORT
static PHP_INI_MH(OnUpdateJit)
{/*{{{*/
OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
if (PCRE_G(jit) && jit_stack) {
pcre2_jit_stack_assign(mctx, NULL, jit_stack);
} else {
pcre2_jit_stack_assign(mctx, NULL, NULL);
}
return SUCCESS;
}/*}}}*/
#endif
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("pcre.backtrack_limit", "1000000", PHP_INI_ALL, OnUpdateBacktrackLimit, backtrack_limit, zend_pcre_globals, pcre_globals)
STD_PHP_INI_ENTRY("pcre.recursion_limit", "100000", PHP_INI_ALL, OnUpdateRecursionLimit, recursion_limit, zend_pcre_globals, pcre_globals)
#ifdef HAVE_PCRE_JIT_SUPPORT
STD_PHP_INI_ENTRY("pcre.jit", "1", PHP_INI_ALL, OnUpdateJit, jit, zend_pcre_globals, pcre_globals)
#endif
PHP_INI_END()
static char *_pcre2_config_str(uint32_t what)
{/*{{{*/
int len = pcre2_config(what, NULL);
char *ret = (char *) malloc(len + 1);
len = pcre2_config(what, ret);
if (!len) {
free(ret);
return NULL;
}
return ret;
}/*}}}*/
/* {{{ PHP_MINFO_FUNCTION(pcre) */
static PHP_MINFO_FUNCTION(pcre)
{
#ifdef HAVE_PCRE_JIT_SUPPORT
uint32_t flag = 0;
char *jit_target = _pcre2_config_str(PCRE2_CONFIG_JITTARGET);
#endif
char *version = _pcre2_config_str(PCRE2_CONFIG_VERSION);
char *unicode = _pcre2_config_str(PCRE2_CONFIG_UNICODE_VERSION);
php_info_print_table_start();
php_info_print_table_row(2, "PCRE (Perl Compatible Regular Expressions) Support", "enabled" );
php_info_print_table_row(2, "PCRE Library Version", version);
free(version);
php_info_print_table_row(2, "PCRE Unicode Version", unicode);
free(unicode);
#ifdef HAVE_PCRE_JIT_SUPPORT
if (!pcre2_config(PCRE2_CONFIG_JIT, &flag)) {
php_info_print_table_row(2, "PCRE JIT Support", flag ? "enabled" : "disabled");
} else {
php_info_print_table_row(2, "PCRE JIT Support", "unknown" );
}
if (jit_target) {
php_info_print_table_row(2, "PCRE JIT Target", jit_target);
}
free(jit_target);
#else
php_info_print_table_row(2, "PCRE JIT Support", "not compiled in" );
#endif
#ifdef HAVE_PCRE_VALGRIND_SUPPORT
php_info_print_table_row(2, "PCRE Valgrind Support", "enabled" );
#endif
php_info_print_table_end();
DISPLAY_INI_ENTRIES();
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION(pcre) */
static PHP_MINIT_FUNCTION(pcre)
{
char *version;
#ifdef HAVE_PCRE_JIT_SUPPORT
if (UNEXPECTED(!pcre2_init_ok)) {
/* Retry. */
php_pcre_init_pcre2(PCRE_G(jit));
if (!pcre2_init_ok) {
return FAILURE;
}
}
#endif
REGISTER_INI_ENTRIES();
REGISTER_LONG_CONSTANT("PREG_PATTERN_ORDER", PREG_PATTERN_ORDER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_SET_ORDER", PREG_SET_ORDER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_OFFSET_CAPTURE", PREG_OFFSET_CAPTURE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_UNMATCHED_AS_NULL", PREG_UNMATCHED_AS_NULL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_SPLIT_NO_EMPTY", PREG_SPLIT_NO_EMPTY, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_SPLIT_DELIM_CAPTURE", PREG_SPLIT_DELIM_CAPTURE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_SPLIT_OFFSET_CAPTURE", PREG_SPLIT_OFFSET_CAPTURE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_GREP_INVERT", PREG_GREP_INVERT, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_NO_ERROR", PHP_PCRE_NO_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_INTERNAL_ERROR", PHP_PCRE_INTERNAL_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_BACKTRACK_LIMIT_ERROR", PHP_PCRE_BACKTRACK_LIMIT_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_RECURSION_LIMIT_ERROR", PHP_PCRE_RECURSION_LIMIT_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_BAD_UTF8_ERROR", PHP_PCRE_BAD_UTF8_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_BAD_UTF8_OFFSET_ERROR", PHP_PCRE_BAD_UTF8_OFFSET_ERROR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PREG_JIT_STACKLIMIT_ERROR", PHP_PCRE_JIT_STACKLIMIT_ERROR, CONST_CS | CONST_PERSISTENT);
version = _pcre2_config_str(PCRE2_CONFIG_VERSION);
REGISTER_STRING_CONSTANT("PCRE_VERSION", version, CONST_CS | CONST_PERSISTENT);
free(version);
REGISTER_LONG_CONSTANT("PCRE_VERSION_MAJOR", PCRE2_MAJOR, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PCRE_VERSION_MINOR", PCRE2_MINOR, CONST_CS | CONST_PERSISTENT);
#ifdef HAVE_PCRE_JIT_SUPPORT
REGISTER_BOOL_CONSTANT("PCRE_JIT_SUPPORT", 1, CONST_CS | CONST_PERSISTENT);
#else
REGISTER_BOOL_CONSTANT("PCRE_JIT_SUPPORT", 0, CONST_CS | CONST_PERSISTENT);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION(pcre) */
static PHP_MSHUTDOWN_FUNCTION(pcre)
{
UNREGISTER_INI_ENTRIES();
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION(pcre) */
static PHP_RINIT_FUNCTION(pcre)
{
#ifdef HAVE_PCRE_JIT_SUPPORT
if (UNEXPECTED(!pcre2_init_ok)) {
/* Retry. */
php_pcre_mutex_lock();
php_pcre_init_pcre2(PCRE_G(jit));
if (!pcre2_init_ok) {
php_pcre_mutex_unlock();
return FAILURE;
}
php_pcre_mutex_unlock();
}
mdata_used = 0;
#endif
PCRE_G(error_code) = PHP_PCRE_NO_ERROR;
PCRE_G(gctx_zmm) = pcre2_general_context_create(php_pcre_emalloc, php_pcre_efree, NULL);
if (!PCRE_G(gctx_zmm)) {
return FAILURE;
}
if (PCRE_G(per_request_cache)) {
zend_hash_init(&PCRE_G(pcre_cache), 0, NULL, php_efree_pcre_cache, 0);
}
return SUCCESS;
}
/* }}} */
static PHP_RSHUTDOWN_FUNCTION(pcre)
{
pcre2_general_context_free(PCRE_G(gctx_zmm));
PCRE_G(gctx_zmm) = NULL;
if (PCRE_G(per_request_cache)) {
zend_hash_destroy(&PCRE_G(pcre_cache));
}
zval_ptr_dtor(&PCRE_G(unmatched_null_pair));
zval_ptr_dtor(&PCRE_G(unmatched_empty_pair));
ZVAL_UNDEF(&PCRE_G(unmatched_null_pair));
ZVAL_UNDEF(&PCRE_G(unmatched_empty_pair));
return SUCCESS;
}
/* {{{ static pcre_clean_cache */
static int pcre_clean_cache(zval *data, void *arg)
{
pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data);
int *num_clean = (int *)arg;
if (*num_clean > 0 && !pce->refcount) {
(*num_clean)--;
return ZEND_HASH_APPLY_REMOVE;
} else {
return ZEND_HASH_APPLY_KEEP;
}
}
/* }}} */
static void free_subpats_table(zend_string **subpat_names, uint32_t num_subpats) {
uint32_t i;
for (i = 0; i < num_subpats; i++) {
if (subpat_names[i]) {
zend_string_release(subpat_names[i]);
}
}
efree(subpat_names);
}
/* {{{ static make_subpats_table */
static zend_string **make_subpats_table(uint32_t num_subpats, pcre_cache_entry *pce)
{
uint32_t name_cnt = pce->name_count, name_size, ni = 0;
char *name_table;
zend_string **subpat_names;
int rc1, rc2;
rc1 = pcre2_pattern_info(pce->re, PCRE2_INFO_NAMETABLE, &name_table);
rc2 = pcre2_pattern_info(pce->re, PCRE2_INFO_NAMEENTRYSIZE, &name_size);
if (rc1 < 0 || rc2 < 0) {
php_error_docref(NULL, E_WARNING, "Internal pcre2_pattern_info() error %d", rc1 < 0 ? rc1 : rc2);
return NULL;
}
subpat_names = ecalloc(num_subpats, sizeof(zend_string *));
while (ni++ < name_cnt) {
unsigned short name_idx = 0x100 * (unsigned char)name_table[0] + (unsigned char)name_table[1];
const char *name = name_table + 2;
subpat_names[name_idx] = zend_string_init(name, strlen(name), 0);
if (is_numeric_string(ZSTR_VAL(subpat_names[name_idx]), ZSTR_LEN(subpat_names[name_idx]), NULL, NULL, 0) > 0) {
php_error_docref(NULL, E_WARNING, "Numeric named subpatterns are not allowed");
free_subpats_table(subpat_names, num_subpats);
return NULL;
}
name_table += name_size;
}
return subpat_names;
}
/* }}} */
/* {{{ static calculate_unit_length */
/* Calculates the byte length of the next character. Assumes valid UTF-8 for PCRE2_UTF. */
static zend_always_inline size_t calculate_unit_length(pcre_cache_entry *pce, const char *start)
{
size_t unit_len;
if (pce->compile_options & PCRE2_UTF) {
const char *end = start;
/* skip continuation bytes */
while ((*++end & 0xC0) == 0x80);
unit_len = end - start;
} else {
unit_len = 1;
}
return unit_len;
}
/* }}} */
/* {{{ pcre_get_compiled_regex_cache */
PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache_ex(zend_string *regex, int locale_aware)
{
pcre2_code *re = NULL;
#if 10 == PCRE2_MAJOR && 37 == PCRE2_MINOR && !HAVE_BUNDLED_PCRE
uint32_t coptions = PCRE2_NO_START_OPTIMIZE;
#else
uint32_t coptions = 0;
#endif
PCRE2_UCHAR error[128];
PCRE2_SIZE erroffset;
int errnumber;
char delimiter;
char start_delimiter;
char end_delimiter;
char *p, *pp;
char *pattern;
size_t pattern_len;
uint32_t poptions = 0;
const uint8_t *tables = NULL;
zval *zv;
pcre_cache_entry new_entry;
int rc;
zend_string *key;
pcre_cache_entry *ret;
if (locale_aware && BG(ctype_string)) {
key = zend_string_concat2(
ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)),
ZSTR_VAL(regex), ZSTR_LEN(regex));
} else {
key = regex;
}
/* Try to lookup the cached regex entry, and if successful, just pass
back the compiled pattern, otherwise go on and compile it. */
zv = zend_hash_find(&PCRE_G(pcre_cache), key);
if (zv) {
if (key != regex) {
zend_string_release_ex(key, 0);
}
return (pcre_cache_entry*)Z_PTR_P(zv);
}
p = ZSTR_VAL(regex);
/* Parse through the leading whitespace, and display a warning if we
get to the end without encountering a delimiter. */
while (isspace((int)*(unsigned char *)p)) p++;
if (*p == 0) {
if (key != regex) {
zend_string_release_ex(key, 0);
}
php_error_docref(NULL, E_WARNING,
p < ZSTR_VAL(regex) + ZSTR_LEN(regex) ? "Null byte in regex" : "Empty regular expression");
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
return NULL;
}
/* Get the delimiter and display a warning if it is alphanumeric
or a backslash. */
delimiter = *p++;
if (isalnum((int)*(unsigned char *)&delimiter) || delimiter == '\\') {
if (key != regex) {
zend_string_release_ex(key, 0);
}
php_error_docref(NULL,E_WARNING, "Delimiter must not be alphanumeric or backslash");
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
return NULL;
}
start_delimiter = delimiter;
if ((pp = strchr("([{< )]}> )]}>", delimiter)))
delimiter = pp[5];
end_delimiter = delimiter;
pp = p;
if (start_delimiter == end_delimiter) {
/* We need to iterate through the pattern, searching for the ending delimiter,
but skipping the backslashed delimiters. If the ending delimiter is not
found, display a warning. */
while (*pp != 0) {
if (*pp == '\\' && pp[1] != 0) pp++;
else if (*pp == delimiter)
break;
pp++;
}
} else {
/* We iterate through the pattern, searching for the matching ending
* delimiter. For each matching starting delimiter, we increment nesting
* level, and decrement it for each matching ending delimiter. If we
* reach the end of the pattern without matching, display a warning.
*/
int brackets = 1; /* brackets nesting level */
while (*pp != 0) {
if (*pp == '\\' && pp[1] != 0) pp++;
else if (*pp == end_delimiter && --brackets <= 0)
break;
else if (*pp == start_delimiter)
brackets++;
pp++;
}
}
if (*pp == 0) {
if (key != regex) {
zend_string_release_ex(key, 0);
}
if (pp < ZSTR_VAL(regex) + ZSTR_LEN(regex)) {
php_error_docref(NULL,E_WARNING, "Null byte in regex");
} else if (start_delimiter == end_delimiter) {
php_error_docref(NULL,E_WARNING, "No ending delimiter '%c' found", delimiter);
} else {
php_error_docref(NULL,E_WARNING, "No ending matching delimiter '%c' found", delimiter);
}
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
return NULL;
}
/* Make a copy of the actual pattern. */
pattern_len = pp - p;
pattern = estrndup(p, pattern_len);
/* Move on to the options */
pp++;
/* Parse through the options, setting appropriate flags. Display
a warning if we encounter an unknown modifier. */
while (pp < ZSTR_VAL(regex) + ZSTR_LEN(regex)) {
switch (*pp++) {
/* Perl compatible options */
case 'i': coptions |= PCRE2_CASELESS; break;
case 'm': coptions |= PCRE2_MULTILINE; break;
case 's': coptions |= PCRE2_DOTALL; break;
case 'x': coptions |= PCRE2_EXTENDED; break;
/* PCRE specific options */
case 'A': coptions |= PCRE2_ANCHORED; break;
case 'D': coptions |= PCRE2_DOLLAR_ENDONLY;break;
case 'S': /* Pass. */ break;
case 'X': /* Pass. */ break;
case 'U': coptions |= PCRE2_UNGREEDY; break;
case 'u': coptions |= PCRE2_UTF;
/* In PCRE, by default, \d, \D, \s, \S, \w, and \W recognize only ASCII
characters, even in UTF-8 mode. However, this can be changed by setting
the PCRE2_UCP option. */
#ifdef PCRE2_UCP
coptions |= PCRE2_UCP;
#endif
break;
case 'J': coptions |= PCRE2_DUPNAMES; break;
/* Custom preg options */
case 'e': poptions |= PREG_REPLACE_EVAL; break;
case ' ':
case '\n':
case '\r':
break;
default:
if (pp[-1]) {
php_error_docref(NULL,E_WARNING, "Unknown modifier '%c'", pp[-1]);
} else {
php_error_docref(NULL,E_WARNING, "Null byte in regex");
}
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
efree(pattern);
if (key != regex) {
zend_string_release_ex(key, 0);
}
return NULL;
}
}
if (poptions & PREG_REPLACE_EVAL) {
php_error_docref(NULL, E_WARNING, "The /e modifier is no longer supported, use preg_replace_callback instead");
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
efree(pattern);
if (key != regex) {
zend_string_release_ex(key, 0);
}
return NULL;
}
if (key != regex) {
tables = (uint8_t *)zend_hash_find_ptr(&char_tables, BG(ctype_string));
if (!tables) {
zend_string *_k;
tables = pcre2_maketables(gctx);
if (UNEXPECTED(!tables)) {
php_error_docref(NULL,E_WARNING, "Failed to generate locale character tables");
pcre_handle_exec_error(PCRE2_ERROR_NOMEMORY);
zend_string_release_ex(key, 0);
efree(pattern);
return NULL;
}
_k = zend_string_init(ZSTR_VAL(BG(ctype_string)), ZSTR_LEN(BG(ctype_string)), 1);
GC_MAKE_PERSISTENT_LOCAL(_k);
zend_hash_add_ptr(&char_tables, _k, (void *)tables);
zend_string_release(_k);
}
}
pcre2_set_character_tables(cctx, tables);
/* Compile pattern and display a warning if compilation failed. */
re = pcre2_compile((PCRE2_SPTR)pattern, pattern_len, coptions, &errnumber, &erroffset, cctx);
if (re == NULL) {
if (key != regex) {
zend_string_release_ex(key, 0);
}
pcre2_get_error_message(errnumber, error, sizeof(error));
php_error_docref(NULL,E_WARNING, "Compilation failed: %s at offset %zu", error, erroffset);
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
efree(pattern);
return NULL;
}
#ifdef HAVE_PCRE_JIT_SUPPORT
if (PCRE_G(jit)) {
/* Enable PCRE JIT compiler */
rc = pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);
if (EXPECTED(rc >= 0)) {
size_t jit_size = 0;
if (!pcre2_pattern_info(re, PCRE2_INFO_JITSIZE, &jit_size) && jit_size > 0) {
poptions |= PREG_JIT;
}
} else if (rc == PCRE2_ERROR_NOMEMORY) {
php_error_docref(NULL, E_WARNING,
"Allocation of JIT memory failed, PCRE JIT will be disabled. "
"This is likely caused by security restrictions. "
"Either grant PHP permission to allocate executable memory, or set pcre.jit=0");
PCRE_G(jit) = 0;
} else {
pcre2_get_error_message(rc, error, sizeof(error));
php_error_docref(NULL, E_WARNING, "JIT compilation failed: %s", error);
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
}
}
#endif
efree(pattern);
/*
* If we reached cache limit, clean out the items from the head of the list;
* these are supposedly the oldest ones (but not necessarily the least used
* ones).
*/
if (zend_hash_num_elements(&PCRE_G(pcre_cache)) == PCRE_CACHE_SIZE) {
int num_clean = PCRE_CACHE_SIZE / 8;
zend_hash_apply_with_argument(&PCRE_G(pcre_cache), pcre_clean_cache, &num_clean);
}
/* Store the compiled pattern and extra info in the cache. */
new_entry.re = re;
new_entry.preg_options = poptions;
new_entry.compile_options = coptions;
new_entry.refcount = 0;
rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &new_entry.capture_count);
if (rc < 0) {
if (key != regex) {
zend_string_release_ex(key, 0);
}
php_error_docref(NULL, E_WARNING, "Internal pcre2_pattern_info() error %d", rc);
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
return NULL;
}
rc = pcre2_pattern_info(re, PCRE2_INFO_NAMECOUNT, &new_entry.name_count);
if (rc < 0) {
if (key != regex) {
zend_string_release_ex(key, 0);
}
php_error_docref(NULL, E_WARNING, "Internal pcre_pattern_info() error %d", rc);
pcre_handle_exec_error(PCRE2_ERROR_INTERNAL);
return NULL;
}
/*
* Interned strings are not duplicated when stored in HashTable,
* but all the interned strings created during HTTP request are removed
* at end of request. However PCRE_G(pcre_cache) must be consistent
* on the next request as well. So we disable usage of interned strings
* as hash keys especually for this table.
* See bug #63180
*/
if (!(GC_FLAGS(key) & IS_STR_PERMANENT) && !PCRE_G(per_request_cache)) {
zend_string *str = zend_string_init(ZSTR_VAL(key), ZSTR_LEN(key), 1);
GC_MAKE_PERSISTENT_LOCAL(str);
ret = zend_hash_add_new_mem(&PCRE_G(pcre_cache), str, &new_entry, sizeof(pcre_cache_entry));
zend_string_release(str);
} else {
ret = zend_hash_add_new_mem(&PCRE_G(pcre_cache), key, &new_entry, sizeof(pcre_cache_entry));
}
if (key != regex) {
zend_string_release_ex(key, 0);
}
return ret;
}
/* }}} */
/* {{{ pcre_get_compiled_regex_cache */
PHPAPI pcre_cache_entry* pcre_get_compiled_regex_cache(zend_string *regex)
{
return pcre_get_compiled_regex_cache_ex(regex, 1);
}
/* }}} */
/* {{{ pcre_get_compiled_regex */
PHPAPI pcre2_code *pcre_get_compiled_regex(zend_string *regex, uint32_t *capture_count)
{
pcre_cache_entry * pce = pcre_get_compiled_regex_cache(regex);
if (capture_count) {
*capture_count = pce ? pce->capture_count : 0;
}
return pce ? pce->re : NULL;
}
/* }}} */
/* {{{ pcre_get_compiled_regex_ex */
PHPAPI pcre2_code* pcre_get_compiled_regex_ex(zend_string *regex, uint32_t *capture_count, uint32_t *preg_options, uint32_t *compile_options)
{
pcre_cache_entry * pce = pcre_get_compiled_regex_cache(regex);
if (preg_options) {
*preg_options = pce ? pce->preg_options : 0;
}
if (compile_options) {
*compile_options = pce ? pce->compile_options : 0;
}
if (capture_count) {
*capture_count = pce ? pce->capture_count : 0;
}
return pce ? pce->re : NULL;
}
/* }}} */
/* XXX For the cases where it's only about match yes/no and no capture
required, perhaps just a minimum sized data would suffice. */
PHPAPI pcre2_match_data *php_pcre_create_match_data(uint32_t capture_count, pcre2_code *re)
{/*{{{*/
assert(NULL != re);
if (EXPECTED(!mdata_used)) {
int rc = 0;
if (!capture_count) {
/* As we deal with a non cached pattern, no other way to gather this info. */
rc = pcre2_pattern_info(re, PCRE2_INFO_CAPTURECOUNT, &capture_count);
}
if (rc >= 0 && capture_count + 1 <= PHP_PCRE_PREALLOC_MDATA_SIZE) {
mdata_used = 1;
return mdata;
}
}
return pcre2_match_data_create_from_pattern(re, gctx);
}/*}}}*/
PHPAPI void php_pcre_free_match_data(pcre2_match_data *match_data)
{/*{{{*/
if (UNEXPECTED(match_data != mdata)) {
pcre2_match_data_free(match_data);
} else {
mdata_used = 0;
}
}/*}}}*/
static void init_unmatched_null_pair(void) {
zval val1, val2;
ZVAL_NULL(&val1);
ZVAL_LONG(&val2, -1);
ZVAL_ARR(&PCRE_G(unmatched_null_pair), zend_new_pair(&val1, &val2));
}
static void init_unmatched_empty_pair(void) {
zval val1, val2;
ZVAL_EMPTY_STRING(&val1);
ZVAL_LONG(&val2, -1);
ZVAL_ARR(&PCRE_G(unmatched_empty_pair), zend_new_pair(&val1, &val2));
}
static zend_always_inline void populate_match_value_str(
zval *val, const char *subject, PCRE2_SIZE start_offset, PCRE2_SIZE end_offset) {
ZVAL_STRINGL_FAST(val, subject + start_offset, end_offset - start_offset);
}
static inline void populate_match_value(