-
Notifications
You must be signed in to change notification settings - Fork 7.8k
/
mbstring.c
4378 lines (3797 loc) · 122 KB
/
mbstring.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: |
| http://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: Tsukada Takuya <[email protected]> |
| Rui Hirokawa <[email protected]> |
| Hironori Sato <[email protected]> |
| Shigeru Kanemoto <[email protected]> |
+----------------------------------------------------------------------+
*/
/* {{{ includes */
#include "libmbfl/config.h"
#include "php.h"
#include "php_ini.h"
#include "php_variables.h"
#include "mbstring.h"
#include "ext/standard/php_string.h"
#include "ext/standard/php_mail.h"
#include "ext/standard/exec.h"
#include "ext/standard/url.h"
#include "main/php_output.h"
#include "ext/standard/info.h"
#include "ext/pcre/php_pcre.h"
#include "libmbfl/mbfl/mbfilter_8bit.h"
#include "libmbfl/mbfl/mbfilter_pass.h"
#include "libmbfl/mbfl/mbfilter_wchar.h"
#include "libmbfl/filters/mbfilter_ascii.h"
#include "libmbfl/filters/mbfilter_base64.h"
#include "libmbfl/filters/mbfilter_qprint.h"
#include "libmbfl/filters/mbfilter_ucs4.h"
#include "libmbfl/filters/mbfilter_utf8.h"
#include "libmbfl/filters/mbfilter_tl_jisx0201_jisx0208.h"
#include "php_variables.h"
#include "php_globals.h"
#include "rfc1867.h"
#include "php_content_types.h"
#include "SAPI.h"
#include "php_unicode.h"
#include "TSRM.h"
#include "mb_gpc.h"
#ifdef HAVE_MBREGEX
# include "php_mbregex.h"
#endif
#include "zend_multibyte.h"
#include "mbstring_arginfo.h"
/* }}} */
/* {{{ prototypes */
ZEND_DECLARE_MODULE_GLOBALS(mbstring)
static PHP_GINIT_FUNCTION(mbstring);
static PHP_GSHUTDOWN_FUNCTION(mbstring);
static void php_mb_populate_current_detect_order_list(void);
static int php_mb_encoding_translation(void);
static void php_mb_gpc_get_detect_order(const zend_encoding ***list, size_t *list_size);
static void php_mb_gpc_set_input_encoding(const zend_encoding *encoding);
static inline zend_bool php_mb_is_unsupported_no_encoding(enum mbfl_no_encoding no_enc);
static inline zend_bool php_mb_is_no_encoding_utf8(enum mbfl_no_encoding no_enc);
/* }}} */
/* {{{ php_mb_default_identify_list */
typedef struct _php_mb_nls_ident_list {
enum mbfl_no_language lang;
const enum mbfl_no_encoding *list;
size_t list_size;
} php_mb_nls_ident_list;
static const enum mbfl_no_encoding php_mb_default_identify_list_ja[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_jis,
mbfl_no_encoding_utf8,
mbfl_no_encoding_euc_jp,
mbfl_no_encoding_sjis
};
static const enum mbfl_no_encoding php_mb_default_identify_list_cn[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_euc_cn,
mbfl_no_encoding_cp936
};
static const enum mbfl_no_encoding php_mb_default_identify_list_tw_hk[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_euc_tw,
mbfl_no_encoding_big5
};
static const enum mbfl_no_encoding php_mb_default_identify_list_kr[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_euc_kr,
mbfl_no_encoding_uhc
};
static const enum mbfl_no_encoding php_mb_default_identify_list_ru[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_koi8r,
mbfl_no_encoding_cp1251,
mbfl_no_encoding_cp866
};
static const enum mbfl_no_encoding php_mb_default_identify_list_hy[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_armscii8
};
static const enum mbfl_no_encoding php_mb_default_identify_list_tr[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_cp1254,
mbfl_no_encoding_8859_9
};
static const enum mbfl_no_encoding php_mb_default_identify_list_ua[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8,
mbfl_no_encoding_koi8u
};
static const enum mbfl_no_encoding php_mb_default_identify_list_neut[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8
};
static const php_mb_nls_ident_list php_mb_default_identify_list[] = {
{ mbfl_no_language_japanese, php_mb_default_identify_list_ja, sizeof(php_mb_default_identify_list_ja) / sizeof(php_mb_default_identify_list_ja[0]) },
{ mbfl_no_language_korean, php_mb_default_identify_list_kr, sizeof(php_mb_default_identify_list_kr) / sizeof(php_mb_default_identify_list_kr[0]) },
{ mbfl_no_language_traditional_chinese, php_mb_default_identify_list_tw_hk, sizeof(php_mb_default_identify_list_tw_hk) / sizeof(php_mb_default_identify_list_tw_hk[0]) },
{ mbfl_no_language_simplified_chinese, php_mb_default_identify_list_cn, sizeof(php_mb_default_identify_list_cn) / sizeof(php_mb_default_identify_list_cn[0]) },
{ mbfl_no_language_russian, php_mb_default_identify_list_ru, sizeof(php_mb_default_identify_list_ru) / sizeof(php_mb_default_identify_list_ru[0]) },
{ mbfl_no_language_armenian, php_mb_default_identify_list_hy, sizeof(php_mb_default_identify_list_hy) / sizeof(php_mb_default_identify_list_hy[0]) },
{ mbfl_no_language_turkish, php_mb_default_identify_list_tr, sizeof(php_mb_default_identify_list_tr) / sizeof(php_mb_default_identify_list_tr[0]) },
{ mbfl_no_language_ukrainian, php_mb_default_identify_list_ua, sizeof(php_mb_default_identify_list_ua) / sizeof(php_mb_default_identify_list_ua[0]) },
{ mbfl_no_language_neutral, php_mb_default_identify_list_neut, sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]) }
};
/* }}} */
/* {{{ zend_module_entry mbstring_module_entry */
zend_module_entry mbstring_module_entry = {
STANDARD_MODULE_HEADER,
"mbstring",
ext_functions,
PHP_MINIT(mbstring),
PHP_MSHUTDOWN(mbstring),
PHP_RINIT(mbstring),
PHP_RSHUTDOWN(mbstring),
PHP_MINFO(mbstring),
PHP_MBSTRING_VERSION,
PHP_MODULE_GLOBALS(mbstring),
PHP_GINIT(mbstring),
PHP_GSHUTDOWN(mbstring),
NULL,
STANDARD_MODULE_PROPERTIES_EX
};
/* }}} */
/* {{{ static sapi_post_entry php_post_entries[] */
static const sapi_post_entry php_post_entries[] = {
{ DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_std_post_handler },
{ MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
{ NULL, 0, NULL, NULL }
};
/* }}} */
#ifdef COMPILE_DL_MBSTRING
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(mbstring)
#endif
/* {{{ static sapi_post_entry mbstr_post_entries[] */
static const sapi_post_entry mbstr_post_entries[] = {
{ DEFAULT_POST_CONTENT_TYPE, sizeof(DEFAULT_POST_CONTENT_TYPE)-1, sapi_read_standard_form_data, php_mb_post_handler },
{ MULTIPART_CONTENT_TYPE, sizeof(MULTIPART_CONTENT_TYPE)-1, NULL, rfc1867_post_handler },
{ NULL, 0, NULL, NULL }
};
/* }}} */
static const mbfl_encoding *php_mb_get_encoding(zend_string *encoding_name, uint32_t arg_num) {
if (encoding_name) {
const mbfl_encoding *encoding;
zend_string *last_encoding_name = MBSTRG(last_used_encoding_name);
if (last_encoding_name && (last_encoding_name == encoding_name
|| !strcasecmp(ZSTR_VAL(encoding_name), ZSTR_VAL(last_encoding_name)))) {
return MBSTRG(last_used_encoding);
}
encoding = mbfl_name2encoding(ZSTR_VAL(encoding_name));
if (!encoding) {
zend_argument_value_error(arg_num, "must be a valid encoding, \"%s\" given", ZSTR_VAL(encoding_name));
return NULL;
}
if (last_encoding_name) {
zend_string_release(last_encoding_name);
}
MBSTRG(last_used_encoding_name) = zend_string_copy(encoding_name);
MBSTRG(last_used_encoding) = encoding;
return encoding;
} else {
return MBSTRG(current_internal_encoding);
}
}
static const mbfl_encoding *php_mb_get_encoding_or_pass(const char *encoding_name) {
if (strcmp(encoding_name, "pass") == 0) {
return &mbfl_encoding_pass;
}
return mbfl_name2encoding(encoding_name);
}
static size_t count_commas(const char *p, const char *end) {
size_t count = 0;
while ((p = memchr(p, ',', end - p))) {
count++;
p++;
}
return count;
}
/* {{{ static zend_result php_mb_parse_encoding_list()
* Return FAILURE if input contains any illegal encoding, otherwise SUCCESS.
* Emits a ValueError in function context and a warning in INI context, in INI context arg_num must be 0.
*/
static zend_result php_mb_parse_encoding_list(const char *value, size_t value_length,
const mbfl_encoding ***return_list, size_t *return_size, bool persistent, uint32_t arg_num,
zend_bool allow_pass_encoding)
{
if (value == NULL || value_length == 0) {
*return_list = NULL;
*return_size = 0;
return SUCCESS;
} else {
zend_bool included_auto;
size_t n, size;
char *p1, *endp, *tmpstr;
const mbfl_encoding **entry, **list;
/* copy the value string for work */
if (value[0]=='"' && value[value_length-1]=='"' && value_length>2) {
tmpstr = (char *)estrndup(value+1, value_length-2);
value_length -= 2;
} else {
tmpstr = (char *)estrndup(value, value_length);
}
endp = tmpstr + value_length;
size = 1 + count_commas(tmpstr, endp) + MBSTRG(default_detect_order_list_size);
list = (const mbfl_encoding **)pecalloc(size, sizeof(mbfl_encoding*), persistent);
entry = list;
n = 0;
included_auto = 0;
p1 = tmpstr;
while (1) {
char *comma = (char *) php_memnstr(p1, ",", 1, endp);
char *p = comma ? comma : endp;
*p = '\0';
/* trim spaces */
while (p1 < p && (*p1 == ' ' || *p1 == '\t')) {
p1++;
}
p--;
while (p > p1 && (*p == ' ' || *p == '\t')) {
*p = '\0';
p--;
}
/* convert to the encoding number and check encoding */
if (strcasecmp(p1, "auto") == 0) {
if (!included_auto) {
const enum mbfl_no_encoding *src = MBSTRG(default_detect_order_list);
const size_t identify_list_size = MBSTRG(default_detect_order_list_size);
size_t i;
included_auto = 1;
for (i = 0; i < identify_list_size; i++) {
*entry++ = mbfl_no2encoding(*src++);
n++;
}
}
} else {
const mbfl_encoding *encoding =
allow_pass_encoding ? php_mb_get_encoding_or_pass(p1) : mbfl_name2encoding(p1);
if (!encoding) {
/* Called from an INI setting modification */
if (arg_num == 0) {
php_error_docref("ref.mbstring", E_WARNING, "INI setting contains invalid encoding \"%s\"", p1);
} else {
zend_argument_value_error(arg_num, "contains invalid encoding \"%s\"", p1);
}
efree(tmpstr);
pefree(ZEND_VOIDP(list), persistent);
return FAILURE;
}
*entry++ = encoding;
n++;
}
if (n >= size || comma == NULL) {
break;
}
p1 = comma + 1;
}
*return_list = list;
*return_size = n;
efree(tmpstr);
}
return SUCCESS;
}
/* }}} */
/* {{{ static int php_mb_parse_encoding_array()
* Return FAILURE if input contains any illegal encoding, otherwise SUCCESS.
* Emits a ValueError in function context and a warning in INI context, in INI context arg_num must be 0.
*/
static int php_mb_parse_encoding_array(HashTable *target_hash, const mbfl_encoding ***return_list,
size_t *return_size, uint32_t arg_num)
{
/* Allocate enough space to include the default detect order if "auto" is used. */
size_t size = zend_hash_num_elements(target_hash) + MBSTRG(default_detect_order_list_size);
const mbfl_encoding **list = ecalloc(size, sizeof(mbfl_encoding*));
const mbfl_encoding **entry = list;
zend_bool included_auto = 0;
size_t n = 0;
zval *hash_entry;
ZEND_HASH_FOREACH_VAL(target_hash, hash_entry) {
zend_string *encoding_str = zval_try_get_string(hash_entry);
if (UNEXPECTED(!encoding_str)) {
efree(ZEND_VOIDP(list));
return FAILURE;
}
if (strcasecmp(ZSTR_VAL(encoding_str), "auto") == 0) {
if (!included_auto) {
const enum mbfl_no_encoding *src = MBSTRG(default_detect_order_list);
const size_t identify_list_size = MBSTRG(default_detect_order_list_size);
size_t j;
included_auto = 1;
for (j = 0; j < identify_list_size; j++) {
*entry++ = mbfl_no2encoding(*src++);
n++;
}
}
} else {
const mbfl_encoding *encoding = mbfl_name2encoding(ZSTR_VAL(encoding_str));
if (encoding) {
*entry++ = encoding;
n++;
} else {
zend_argument_value_error(arg_num, "contains invalid encoding \"%s\"", ZSTR_VAL(encoding_str));
zend_string_release(encoding_str);
efree(ZEND_VOIDP(list));
return FAILURE;
}
}
zend_string_release(encoding_str);
} ZEND_HASH_FOREACH_END();
*return_list = list;
*return_size = n;
return SUCCESS;
}
/* }}} */
/* {{{ zend_multibyte interface */
static const zend_encoding* php_mb_zend_encoding_fetcher(const char *encoding_name)
{
return (const zend_encoding*)mbfl_name2encoding(encoding_name);
}
static const char *php_mb_zend_encoding_name_getter(const zend_encoding *encoding)
{
return ((const mbfl_encoding *)encoding)->name;
}
static bool php_mb_zend_encoding_lexer_compatibility_checker(const zend_encoding *_encoding)
{
const mbfl_encoding *encoding = (const mbfl_encoding*)_encoding;
if (encoding->flag & MBFL_ENCTYPE_SBCS) {
return 1;
}
if ((encoding->flag & (MBFL_ENCTYPE_MBCS | MBFL_ENCTYPE_GL_UNSAFE)) == MBFL_ENCTYPE_MBCS) {
return 1;
}
return 0;
}
static const zend_encoding *php_mb_zend_encoding_detector(const unsigned char *arg_string, size_t arg_length, const zend_encoding **list, size_t list_size)
{
mbfl_string string;
if (!list) {
list = (const zend_encoding **)MBSTRG(current_detect_order_list);
list_size = MBSTRG(current_detect_order_list_size);
}
mbfl_string_init(&string);
string.val = (unsigned char *)arg_string;
string.len = arg_length;
return (const zend_encoding *) mbfl_identify_encoding(&string, (const mbfl_encoding **)list, list_size, 0);
}
static size_t php_mb_zend_encoding_converter(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length, const zend_encoding *encoding_to, const zend_encoding *encoding_from)
{
mbfl_string string, result;
mbfl_buffer_converter *convd;
/* new encoding */
/* initialize string */
string.encoding = (const mbfl_encoding*)encoding_from;
string.val = (unsigned char*)from;
string.len = from_length;
/* initialize converter */
convd = mbfl_buffer_converter_new((const mbfl_encoding *)encoding_from, (const mbfl_encoding *)encoding_to, string.len);
if (convd == NULL) {
return (size_t) -1;
}
mbfl_buffer_converter_illegal_mode(convd, MBSTRG(current_filter_illegal_mode));
mbfl_buffer_converter_illegal_substchar(convd, MBSTRG(current_filter_illegal_substchar));
/* do it */
size_t loc = mbfl_buffer_converter_feed(convd, &string);
mbfl_buffer_converter_flush(convd);
mbfl_string_init(&result);
if (!mbfl_buffer_converter_result(convd, &result)) {
mbfl_buffer_converter_delete(convd);
return (size_t)-1;
}
*to = result.val;
*to_length = result.len;
mbfl_buffer_converter_delete(convd);
return loc;
}
static zend_result php_mb_zend_encoding_list_parser(const char *encoding_list, size_t encoding_list_len, const zend_encoding ***return_list, size_t *return_size, bool persistent)
{
return php_mb_parse_encoding_list(
encoding_list, encoding_list_len,
(const mbfl_encoding ***)return_list, return_size,
persistent, /* arg_num */ 0, /* allow_pass_encoding */ 1);
}
static const zend_encoding *php_mb_zend_internal_encoding_getter(void)
{
return (const zend_encoding *)MBSTRG(internal_encoding);
}
static zend_result php_mb_zend_internal_encoding_setter(const zend_encoding *encoding)
{
MBSTRG(internal_encoding) = (const mbfl_encoding *)encoding;
return SUCCESS;
}
static zend_multibyte_functions php_mb_zend_multibyte_functions = {
"mbstring",
php_mb_zend_encoding_fetcher,
php_mb_zend_encoding_name_getter,
php_mb_zend_encoding_lexer_compatibility_checker,
php_mb_zend_encoding_detector,
php_mb_zend_encoding_converter,
php_mb_zend_encoding_list_parser,
php_mb_zend_internal_encoding_getter,
php_mb_zend_internal_encoding_setter
};
/* }}} */
/* {{{ _php_mb_compile_regex */
static void *_php_mb_compile_regex(const char *pattern)
{
pcre2_code *retval;
PCRE2_SIZE err_offset;
int errnum;
if (!(retval = pcre2_compile((PCRE2_SPTR)pattern, PCRE2_ZERO_TERMINATED,
PCRE2_CASELESS, &errnum, &err_offset, php_pcre_cctx()))) {
PCRE2_UCHAR err_str[128];
pcre2_get_error_message(errnum, err_str, sizeof(err_str));
php_error_docref(NULL, E_WARNING, "%s (offset=%zu): %s", pattern, err_offset, err_str);
}
return retval;
}
/* }}} */
/* {{{ _php_mb_match_regex */
static int _php_mb_match_regex(void *opaque, const char *str, size_t str_len)
{
int res;
pcre2_match_data *match_data = php_pcre_create_match_data(0, opaque);
if (NULL == match_data) {
pcre2_code_free(opaque);
php_error_docref(NULL, E_WARNING, "Cannot allocate match data");
return FAILURE;
}
res = pcre2_match(opaque, (PCRE2_SPTR)str, str_len, 0, 0, match_data, php_pcre_mctx()) >= 0;
php_pcre_free_match_data(match_data);
return res;
}
/* }}} */
/* {{{ _php_mb_free_regex */
static void _php_mb_free_regex(void *opaque)
{
pcre2_code_free(opaque);
}
/* }}} */
/* {{{ php_mb_nls_get_default_detect_order_list */
static int php_mb_nls_get_default_detect_order_list(enum mbfl_no_language lang, enum mbfl_no_encoding **plist, size_t *plist_size)
{
size_t i;
*plist = (enum mbfl_no_encoding *) php_mb_default_identify_list_neut;
*plist_size = sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]);
for (i = 0; i < sizeof(php_mb_default_identify_list) / sizeof(php_mb_default_identify_list[0]); i++) {
if (php_mb_default_identify_list[i].lang == lang) {
*plist = (enum mbfl_no_encoding *)php_mb_default_identify_list[i].list;
*plist_size = php_mb_default_identify_list[i].list_size;
return 1;
}
}
return 0;
}
/* }}} */
static char *php_mb_rfc1867_substring_conf(const zend_encoding *encoding, char *start, size_t len, char quote)
{
char *result = emalloc(len + 2);
char *resp = result;
size_t i;
for (i = 0; i < len && start[i] != quote; ++i) {
if (start[i] == '\\' && (start[i + 1] == '\\' || (quote && start[i + 1] == quote))) {
*resp++ = start[++i];
} else {
size_t j = php_mb_mbchar_bytes_ex(start+i, (const mbfl_encoding *)encoding);
while (j-- > 0 && i < len) {
*resp++ = start[i++];
}
--i;
}
}
*resp = '\0';
return result;
}
static char *php_mb_rfc1867_getword(const zend_encoding *encoding, char **line, char stop) /* {{{ */
{
char *pos = *line, quote;
char *res;
while (*pos && *pos != stop) {
if ((quote = *pos) == '"' || quote == '\'') {
++pos;
while (*pos && *pos != quote) {
if (*pos == '\\' && pos[1] && pos[1] == quote) {
pos += 2;
} else {
++pos;
}
}
if (*pos) {
++pos;
}
} else {
pos += php_mb_mbchar_bytes_ex(pos, (const mbfl_encoding *)encoding);
}
}
if (*pos == '\0') {
res = estrdup(*line);
*line += strlen(*line);
return res;
}
res = estrndup(*line, pos - *line);
while (*pos == stop) {
pos += php_mb_mbchar_bytes_ex(pos, (const mbfl_encoding *)encoding);
}
*line = pos;
return res;
}
/* }}} */
static char *php_mb_rfc1867_getword_conf(const zend_encoding *encoding, char *str) /* {{{ */
{
while (*str && isspace(*(unsigned char *)str)) {
++str;
}
if (!*str) {
return estrdup("");
}
if (*str == '"' || *str == '\'') {
char quote = *str;
str++;
return php_mb_rfc1867_substring_conf(encoding, str, strlen(str), quote);
} else {
char *strend = str;
while (*strend && !isspace(*(unsigned char *)strend)) {
++strend;
}
return php_mb_rfc1867_substring_conf(encoding, str, strend - str, 0);
}
}
/* }}} */
static char *php_mb_rfc1867_basename(const zend_encoding *encoding, char *filename) /* {{{ */
{
char *s, *s2;
const size_t filename_len = strlen(filename);
/* The \ check should technically be needed for win32 systems only where
* it is a valid path separator. However, IE in all it's wisdom always sends
* the full path of the file on the user's filesystem, which means that unless
* the user does basename() they get a bogus file name. Until IE's user base drops
* to nill or problem is fixed this code must remain enabled for all systems. */
s = php_mb_safe_strrchr_ex(filename, '\\', filename_len, (const mbfl_encoding *)encoding);
s2 = php_mb_safe_strrchr_ex(filename, '/', filename_len, (const mbfl_encoding *)encoding);
if (s && s2) {
if (s > s2) {
return ++s;
} else {
return ++s2;
}
} else if (s) {
return ++s;
} else if (s2) {
return ++s2;
} else {
return filename;
}
}
/* }}} */
/* {{{ php.ini directive handler */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_language) */
static PHP_INI_MH(OnUpdate_mbstring_language)
{
enum mbfl_no_language no_language;
no_language = mbfl_name2no_language(ZSTR_VAL(new_value));
if (no_language == mbfl_no_language_invalid) {
MBSTRG(language) = mbfl_no_language_neutral;
return FAILURE;
}
MBSTRG(language) = no_language;
php_mb_nls_get_default_detect_order_list(no_language, &MBSTRG(default_detect_order_list), &MBSTRG(default_detect_order_list_size));
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_detect_order) */
static PHP_INI_MH(OnUpdate_mbstring_detect_order)
{
const mbfl_encoding **list;
size_t size;
if (!new_value) {
if (MBSTRG(detect_order_list)) {
pefree(ZEND_VOIDP(MBSTRG(detect_order_list)), 1);
}
MBSTRG(detect_order_list) = NULL;
MBSTRG(detect_order_list_size) = 0;
return SUCCESS;
}
if (FAILURE == php_mb_parse_encoding_list(ZSTR_VAL(new_value), ZSTR_LEN(new_value), &list, &size, /* persistent */ 1, /* arg_num */ 0, /* allow_pass_encoding */ 0) || size == 0) {
return FAILURE;
}
if (MBSTRG(detect_order_list)) {
pefree(ZEND_VOIDP(MBSTRG(detect_order_list)), 1);
}
MBSTRG(detect_order_list) = list;
MBSTRG(detect_order_list_size) = size;
return SUCCESS;
}
/* }}} */
static int _php_mb_ini_mbstring_http_input_set(const char *new_value, size_t new_value_length) {
const mbfl_encoding **list;
size_t size;
if (FAILURE == php_mb_parse_encoding_list(new_value, new_value_length, &list, &size, /* persistent */ 1, /* arg_num */ 0, /* allow_pass_encoding */ 1) || size == 0) {
return FAILURE;
}
if (MBSTRG(http_input_list)) {
pefree(ZEND_VOIDP(MBSTRG(http_input_list)), 1);
}
MBSTRG(http_input_list) = list;
MBSTRG(http_input_list_size) = size;
return SUCCESS;
}
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_input) */
static PHP_INI_MH(OnUpdate_mbstring_http_input)
{
if (new_value) {
php_error_docref("ref.mbstring", E_DEPRECATED, "Use of mbstring.http_input is deprecated");
}
if (!new_value || !ZSTR_VAL(new_value)) {
const char *encoding = php_get_input_encoding();
MBSTRG(http_input_set) = 0;
_php_mb_ini_mbstring_http_input_set(encoding, strlen(encoding));
return SUCCESS;
}
MBSTRG(http_input_set) = 1;
return _php_mb_ini_mbstring_http_input_set(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
}
/* }}} */
static int _php_mb_ini_mbstring_http_output_set(const char *new_value) {
const mbfl_encoding *encoding = php_mb_get_encoding_or_pass(new_value);
if (!encoding) {
return FAILURE;
}
MBSTRG(http_output_encoding) = encoding;
MBSTRG(current_http_output_encoding) = encoding;
return SUCCESS;
}
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_output) */
static PHP_INI_MH(OnUpdate_mbstring_http_output)
{
if (new_value) {
php_error_docref("ref.mbstring", E_DEPRECATED, "Use of mbstring.http_output is deprecated");
}
if (new_value == NULL || ZSTR_LEN(new_value) == 0) {
MBSTRG(http_output_set) = 0;
_php_mb_ini_mbstring_http_output_set(php_get_output_encoding());
return SUCCESS;
}
MBSTRG(http_output_set) = 1;
return _php_mb_ini_mbstring_http_output_set(ZSTR_VAL(new_value));
}
/* }}} */
/* {{{ static _php_mb_ini_mbstring_internal_encoding_set */
static int _php_mb_ini_mbstring_internal_encoding_set(const char *new_value, size_t new_value_length)
{
const mbfl_encoding *encoding;
if (!new_value || !new_value_length || !(encoding = mbfl_name2encoding(new_value))) {
/* falls back to UTF-8 if an unknown encoding name is given */
if (new_value) {
php_error_docref("ref.mbstring", E_WARNING, "Unknown encoding \"%s\" in ini setting", new_value);
}
encoding = &mbfl_encoding_utf8;
}
MBSTRG(internal_encoding) = encoding;
MBSTRG(current_internal_encoding) = encoding;
#ifdef HAVE_MBREGEX
{
const char *enc_name = new_value;
if (FAILURE == php_mb_regex_set_default_mbctype(enc_name)) {
/* falls back to UTF-8 if an unknown encoding name is given */
enc_name = "UTF-8";
php_mb_regex_set_default_mbctype(enc_name);
}
php_mb_regex_set_mbctype(new_value);
}
#endif
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_internal_encoding) */
static PHP_INI_MH(OnUpdate_mbstring_internal_encoding)
{
if (new_value) {
php_error_docref("ref.mbstring", E_DEPRECATED, "Use of mbstring.internal_encoding is deprecated");
}
if (OnUpdateString(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage) == FAILURE) {
return FAILURE;
}
if (new_value && ZSTR_LEN(new_value)) {
MBSTRG(internal_encoding_set) = 1;
return _php_mb_ini_mbstring_internal_encoding_set(ZSTR_VAL(new_value), ZSTR_LEN(new_value));
} else {
const char *encoding = php_get_internal_encoding();
MBSTRG(internal_encoding_set) = 0;
return _php_mb_ini_mbstring_internal_encoding_set(encoding, strlen(encoding));
}
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_substitute_character) */
static PHP_INI_MH(OnUpdate_mbstring_substitute_character)
{
int c;
char *endptr = NULL;
if (new_value != NULL) {
if (strcasecmp("none", ZSTR_VAL(new_value)) == 0) {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
} else if (strcasecmp("long", ZSTR_VAL(new_value)) == 0) {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG;
MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG;
} else if (strcasecmp("entity", ZSTR_VAL(new_value)) == 0) {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY;
MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_ENTITY;
} else {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
if (ZSTR_LEN(new_value) > 0) {
c = strtol(ZSTR_VAL(new_value), &endptr, 0);
if (*endptr == '\0') {
MBSTRG(filter_illegal_substchar) = c;
MBSTRG(current_filter_illegal_substchar) = c;
}
}
}
} else {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
MBSTRG(filter_illegal_substchar) = 0x3f; /* '?' */
MBSTRG(current_filter_illegal_substchar) = 0x3f; /* '?' */
}
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_encoding_translation) */
static PHP_INI_MH(OnUpdate_mbstring_encoding_translation)
{
if (new_value == NULL) {
return FAILURE;
}
OnUpdateBool(entry, new_value, mh_arg1, mh_arg2, mh_arg3, stage);
if (MBSTRG(encoding_translation)) {
sapi_unregister_post_entry(php_post_entries);
sapi_register_post_entries(mbstr_post_entries);
} else {
sapi_unregister_post_entry(mbstr_post_entries);
sapi_register_post_entries(php_post_entries);
}
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_output_conv_mimetypes */
static PHP_INI_MH(OnUpdate_mbstring_http_output_conv_mimetypes)
{
zend_string *tmp;
void *re = NULL;
if (!new_value) {
new_value = entry->orig_value;
}
tmp = php_trim(new_value, NULL, 0, 3);
if (ZSTR_LEN(tmp) > 0) {
if (!(re = _php_mb_compile_regex(ZSTR_VAL(tmp)))) {
zend_string_release_ex(tmp, 0);
return FAILURE;
}
}
if (MBSTRG(http_output_conv_mimetypes)) {
_php_mb_free_regex(MBSTRG(http_output_conv_mimetypes));
}
MBSTRG(http_output_conv_mimetypes) = re;
zend_string_release_ex(tmp, 0);
return SUCCESS;
}
/* }}} */
/* }}} */
/* {{{ php.ini directive registration */
PHP_INI_BEGIN()
PHP_INI_ENTRY("mbstring.language", "neutral", PHP_INI_ALL, OnUpdate_mbstring_language)
PHP_INI_ENTRY("mbstring.detect_order", NULL, PHP_INI_ALL, OnUpdate_mbstring_detect_order)
PHP_INI_ENTRY("mbstring.http_input", NULL, PHP_INI_ALL, OnUpdate_mbstring_http_input)
PHP_INI_ENTRY("mbstring.http_output", NULL, PHP_INI_ALL, OnUpdate_mbstring_http_output)
STD_PHP_INI_ENTRY("mbstring.internal_encoding", NULL, PHP_INI_ALL, OnUpdate_mbstring_internal_encoding, internal_encoding_name, zend_mbstring_globals, mbstring_globals)
PHP_INI_ENTRY("mbstring.substitute_character", NULL, PHP_INI_ALL, OnUpdate_mbstring_substitute_character)
STD_PHP_INI_BOOLEAN("mbstring.encoding_translation", "0",
PHP_INI_SYSTEM | PHP_INI_PERDIR,
OnUpdate_mbstring_encoding_translation,
encoding_translation, zend_mbstring_globals, mbstring_globals)
PHP_INI_ENTRY("mbstring.http_output_conv_mimetypes",
"^(text/|application/xhtml\\+xml)",
PHP_INI_ALL,
OnUpdate_mbstring_http_output_conv_mimetypes)
STD_PHP_INI_BOOLEAN("mbstring.strict_detection", "0",
PHP_INI_ALL,
OnUpdateBool,
strict_detection, zend_mbstring_globals, mbstring_globals)
#ifdef HAVE_MBREGEX
STD_PHP_INI_ENTRY("mbstring.regex_stack_limit", "100000",PHP_INI_ALL, OnUpdateLong, regex_stack_limit, zend_mbstring_globals, mbstring_globals)
STD_PHP_INI_ENTRY("mbstring.regex_retry_limit", "1000000",PHP_INI_ALL, OnUpdateLong, regex_retry_limit, zend_mbstring_globals, mbstring_globals)
#endif
PHP_INI_END()
/* }}} */
static void mbstring_internal_encoding_changed_hook(void) {
/* One of the internal_encoding / input_encoding / output_encoding ini settings changed. */
if (!MBSTRG(internal_encoding_set)) {
const char *encoding = php_get_internal_encoding();
_php_mb_ini_mbstring_internal_encoding_set(encoding, strlen(encoding));
}
if (!MBSTRG(http_output_set)) {
const char *encoding = php_get_output_encoding();
_php_mb_ini_mbstring_http_output_set(encoding);
}
if (!MBSTRG(http_input_set)) {
const char *encoding = php_get_input_encoding();
_php_mb_ini_mbstring_http_input_set(encoding, strlen(encoding));
}
}
/* {{{ module global initialize handler */
static PHP_GINIT_FUNCTION(mbstring)
{
#if defined(COMPILE_DL_MBSTRING) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
mbstring_globals->language = mbfl_no_language_uni;
mbstring_globals->internal_encoding = NULL;
mbstring_globals->current_internal_encoding = mbstring_globals->internal_encoding;
mbstring_globals->http_output_encoding = &mbfl_encoding_pass;
mbstring_globals->current_http_output_encoding = &mbfl_encoding_pass;
mbstring_globals->http_input_identify = NULL;
mbstring_globals->http_input_identify_get = NULL;
mbstring_globals->http_input_identify_post = NULL;
mbstring_globals->http_input_identify_cookie = NULL;
mbstring_globals->http_input_identify_string = NULL;
mbstring_globals->http_input_list = NULL;
mbstring_globals->http_input_list_size = 0;
mbstring_globals->detect_order_list = NULL;
mbstring_globals->detect_order_list_size = 0;
mbstring_globals->current_detect_order_list = NULL;
mbstring_globals->current_detect_order_list_size = 0;
mbstring_globals->default_detect_order_list = (enum mbfl_no_encoding *) php_mb_default_identify_list_neut;
mbstring_globals->default_detect_order_list_size = sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]);
mbstring_globals->filter_illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
mbstring_globals->filter_illegal_substchar = 0x3f; /* '?' */
mbstring_globals->current_filter_illegal_mode = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;