forked from php/php-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmbstring.c
3648 lines (3268 loc) · 98.8 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
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2004 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.0 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_0.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]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
/*
* PHP4 Multibyte String module "mbstring"
*
* History:
* 2000.5.19 Release php-4.0RC2_jstring-1.0
* 2001.4.1 Release php4_jstring-1.0.91
* 2001.4.30 Release php4_jstring-1.1 (contribute to The PHP Group)
* 2001.5.1 Renamed from jstring to mbstring ([email protected])
*/
/*
* PHP3 Internationalization support program.
*
* Copyright (c) 1999,2000 by the PHP3 internationalization team.
* All rights reserved.
*
* See README_PHP3-i18n-ja for more detail.
*
* Authors:
* Hironori Sato <[email protected]>
* Shigeru Kanemoto <[email protected]>
* Tsukada Takuya <[email protected]>
* Rui Hirokawa <[email protected]>
*/
/* {{{ includes */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#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/php_smart_str.h"
#include "ext/standard/url.h"
#include "main/php_output.h"
#include "ext/standard/info.h"
#include "libmbfl/mbfl/mbfl_allocators.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 ZEND_MULTIBYTE
#include "zend_multibyte.h"
#endif /* ZEND_MULTIBYTE */
#if HAVE_MBSTRING
/* }}} */
/* {{{ prototypes */
static void _php_mb_globals_ctor(zend_mbstring_globals *pglobals TSRMLS_DC);
static void _php_mb_globals_dtor(zend_mbstring_globals *pglobals TSRMLS_DC);
/* }}} */
/* {{{ php_mb_default_identify_list */
typedef struct _php_mb_nls_ident_list {
enum mbfl_no_language lang;
enum mbfl_no_encoding* list;
int 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_neut[] = {
mbfl_no_encoding_ascii,
mbfl_no_encoding_utf8
};
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_neutral, php_mb_default_identify_list_neut, sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]) }
};
/* }}} */
static
ZEND_BEGIN_ARG_INFO(third_and_rest_force_ref, 1)
ZEND_ARG_PASS_INFO(0)
ZEND_ARG_PASS_INFO(0)
ZEND_END_ARG_INFO();
/* {{{ mb_overload_def mb_ovld[] */
static const struct mb_overload_def mb_ovld[] = {
{MB_OVERLOAD_MAIL, "mail", "mb_send_mail", "mb_orig_mail"},
{MB_OVERLOAD_STRING, "strlen", "mb_strlen", "mb_orig_strlen"},
{MB_OVERLOAD_STRING, "strpos", "mb_strpos", "mb_orig_strpos"},
{MB_OVERLOAD_STRING, "strrpos", "mb_strrpos", "mb_orig_strrpos"},
{MB_OVERLOAD_STRING, "substr", "mb_substr", "mb_orig_substr"},
{MB_OVERLOAD_STRING, "strtolower", "mb_strtolower", "mb_orig_strtolower"},
{MB_OVERLOAD_STRING, "strtoupper", "mb_strtoupper", "mb_orig_strtoupper"},
{MB_OVERLOAD_STRING, "substr_count", "mb_substr_count", "mb_orig_substr_count"},
#if HAVE_MBREGEX
{MB_OVERLOAD_REGEX, "ereg", "mb_ereg", "mb_orig_ereg"},
{MB_OVERLOAD_REGEX, "eregi", "mb_eregi", "mb_orig_eregi"},
{MB_OVERLOAD_REGEX, "ereg_replace", "mb_ereg_replace", "mb_orig_ereg_replace"},
{MB_OVERLOAD_REGEX, "eregi_replace", "mb_eregi_replace", "mb_orig_eregi_replace"},
{MB_OVERLOAD_REGEX, "split", "mb_split", "mb_orig_split"},
#endif
{0, NULL, NULL, NULL}
};
/* }}} */
/* {{{ function_entry mbstring_functions[] */
function_entry mbstring_functions[] = {
PHP_FE(mb_convert_case, NULL)
PHP_FE(mb_strtoupper, NULL)
PHP_FE(mb_strtolower, NULL)
PHP_FE(mb_language, NULL)
PHP_FE(mb_internal_encoding, NULL)
PHP_FE(mb_http_input, NULL)
PHP_FE(mb_http_output, NULL)
PHP_FE(mb_detect_order, NULL)
PHP_FE(mb_substitute_character, NULL)
PHP_FE(mb_parse_str, second_arg_force_ref)
PHP_FE(mb_output_handler, NULL)
PHP_FE(mb_preferred_mime_name, NULL)
PHP_FE(mb_strlen, NULL)
PHP_FE(mb_strpos, NULL)
PHP_FE(mb_strrpos, NULL)
PHP_FE(mb_substr_count, NULL)
PHP_FE(mb_substr, NULL)
PHP_FE(mb_strcut, NULL)
PHP_FE(mb_strwidth, NULL)
PHP_FE(mb_strimwidth, NULL)
PHP_FE(mb_convert_encoding, NULL)
PHP_FE(mb_detect_encoding, NULL)
PHP_FE(mb_convert_kana, NULL)
PHP_FE(mb_encode_mimeheader, NULL)
PHP_FE(mb_decode_mimeheader, NULL)
PHP_FE(mb_convert_variables, third_and_rest_force_ref)
PHP_FE(mb_encode_numericentity, NULL)
PHP_FE(mb_decode_numericentity, NULL)
PHP_FE(mb_send_mail, NULL)
PHP_FE(mb_get_info, NULL)
#if HAVE_MBREGEX
PHP_MBREGEX_FUNCTION_ENTRIES
#endif
{ NULL, NULL, NULL }
};
/* }}} */
/* {{{ zend_module_entry mbstring_module_entry */
zend_module_entry mbstring_module_entry = {
STANDARD_MODULE_HEADER,
"mbstring",
mbstring_functions,
PHP_MINIT(mbstring),
PHP_MSHUTDOWN(mbstring),
PHP_RINIT(mbstring),
PHP_RSHUTDOWN(mbstring),
PHP_MINFO(mbstring),
NO_VERSION_YET,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
ZEND_DECLARE_MODULE_GLOBALS(mbstring)
#ifdef COMPILE_DL_MBSTRING
ZEND_GET_MODULE(mbstring)
# ifdef PHP_WIN32
# include "zend_arg_defs.c"
# endif
#endif
/* {{{ allocators */
static void *_php_mb_allocators_malloc(unsigned int sz)
{
return emalloc(sz);
}
static void *_php_mb_allocators_realloc(void *ptr, unsigned int sz)
{
return erealloc(ptr, sz);
}
static void *_php_mb_allocators_calloc(unsigned int nelems, unsigned int szelem)
{
return ecalloc(nelems, szelem);
}
static void _php_mb_allocators_free(void *ptr)
{
efree(ptr);
}
static void *_php_mb_allocators_pmalloc(unsigned int sz)
{
return pemalloc(sz, 1);
}
static void *_php_mb_allocators_prealloc(void *ptr, unsigned int sz)
{
return perealloc(ptr, sz, 1);
}
static void _php_mb_allocators_pfree(void *ptr)
{
pefree(ptr, 1);
}
static mbfl_allocators _php_mb_allocators = {
_php_mb_allocators_malloc,
_php_mb_allocators_realloc,
_php_mb_allocators_calloc,
_php_mb_allocators_free,
_php_mb_allocators_pmalloc,
_php_mb_allocators_prealloc,
_php_mb_allocators_pfree
};
/* }}} */
/* {{{ static int php_mb_parse_encoding_list()
* Return 0 if input contains any illegal encoding, otherwise 1.
* Even if any illegal encoding is detected the result may contain a list
* of parsed encodings.
*/
static int
php_mb_parse_encoding_list(const char *value, int value_length, enum mbfl_no_encoding **return_list, int *return_size, int persistent TSRMLS_DC)
{
int n, l, size, bauto, ret = 1;
char *p, *p1, *p2, *endp, *tmpstr;
enum mbfl_no_encoding no_encoding;
enum mbfl_no_encoding *src, *entry, *list;
list = NULL;
if (value == NULL || value_length <= 0) {
if (return_list) {
*return_list = NULL;
}
if (return_size) {
*return_size = 0;
}
return 0;
} else {
enum mbfl_no_encoding *identify_list;
int identify_list_size;
identify_list = MBSTRG(default_detect_order_list);
identify_list_size = MBSTRG(default_detect_order_list_size);
/* 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);
if (tmpstr == NULL) {
return 0;
}
/* count the number of listed encoding names */
endp = tmpstr + value_length;
n = 1;
p1 = tmpstr;
while ((p2 = php_memnstr(p1, ",", 1, endp)) != NULL) {
p1 = p2 + 1;
n++;
}
size = n + identify_list_size;
/* make list */
list = (enum mbfl_no_encoding *)pecalloc(size, sizeof(int), persistent);
if (list != NULL) {
entry = list;
n = 0;
bauto = 0;
p1 = tmpstr;
do {
p2 = p = php_memnstr(p1, ",", 1, endp);
if (p == NULL) {
p = 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 (!bauto) {
bauto = 1;
l = identify_list_size;
src = identify_list;
while (l > 0) {
*entry++ = *src++;
l--;
n++;
}
}
} else {
no_encoding = mbfl_name2no_encoding(p1);
if (no_encoding != mbfl_no_encoding_invalid) {
*entry++ = no_encoding;
n++;
} else {
ret = 0;
}
}
p1 = p2 + 1;
} while (n < size && p2 != NULL);
if (n > 0) {
if (return_list) {
*return_list = list;
} else {
pefree(list, persistent);
}
} else {
pefree(list, persistent);
if (return_list) {
*return_list = NULL;
}
ret = 0;
}
if (return_size) {
*return_size = n;
}
} else {
if (return_list) {
*return_list = NULL;
}
if (return_size) {
*return_size = 0;
}
ret = 0;
}
efree(tmpstr);
}
return ret;
}
/* }}} */
/* {{{ MBSTRING_API php_mb_check_encoding_list */
MBSTRING_API int php_mb_check_encoding_list(const char *encoding_list TSRMLS_DC) {
return php_mb_parse_encoding_list(encoding_list, strlen(encoding_list), NULL, NULL, 0 TSRMLS_CC);
}
/* }}} */
/* {{{ static int php_mb_parse_encoding_array()
* Return 0 if input contains any illegal encoding, otherwise 1.
* Even if any illegal encoding is detected the result may contain a list
* of parsed encodings.
*/
static int
php_mb_parse_encoding_array(zval *array, enum mbfl_no_encoding **return_list, int *return_size, int persistent TSRMLS_DC)
{
zval **hash_entry;
HashTable *target_hash;
int i, n, l, size, bauto,ret = 1;
enum mbfl_no_encoding no_encoding;
enum mbfl_no_encoding *src, *list, *entry;
list = NULL;
if (Z_TYPE_P(array) == IS_ARRAY) {
enum mbfl_no_encoding *identify_list;
int identify_list_size;
identify_list = MBSTRG(default_detect_order_list);
identify_list_size = MBSTRG(default_detect_order_list_size);
target_hash = Z_ARRVAL_P(array);
zend_hash_internal_pointer_reset(target_hash);
i = zend_hash_num_elements(target_hash);
size = i + identify_list_size;
list = (enum mbfl_no_encoding *)pecalloc(size, sizeof(int), persistent);
if (list != NULL) {
entry = list;
bauto = 0;
n = 0;
while (i > 0) {
if (zend_hash_get_current_data(target_hash, (void **) &hash_entry) == FAILURE) {
break;
}
convert_to_string_ex(hash_entry);
if (strcasecmp(Z_STRVAL_PP(hash_entry), "auto") == 0) {
if (!bauto) {
bauto = 1;
l = identify_list_size;
src = identify_list;
while (l > 0) {
*entry++ = *src++;
l--;
n++;
}
}
} else {
no_encoding = mbfl_name2no_encoding(Z_STRVAL_PP(hash_entry));
if (no_encoding != mbfl_no_encoding_invalid) {
*entry++ = no_encoding;
n++;
} else {
ret = 0;
}
}
zend_hash_move_forward(target_hash);
i--;
}
if (n > 0) {
if (return_list) {
*return_list = list;
} else {
pefree(list, persistent);
}
} else {
pefree(list, persistent);
if (return_list) {
*return_list = NULL;
}
ret = 0;
}
if (return_size) {
*return_size = n;
}
} else {
if (return_list) {
*return_list = NULL;
}
if (return_size) {
*return_size = 0;
}
ret = 0;
}
}
return ret;
}
/* }}} */
/* {{{ 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, int* 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 = php_mb_default_identify_list[i].list;
*plist_size = php_mb_default_identify_list[i].list_size;
return 1;
}
}
return 0;
}
/* }}} */
/* {{{ php.ini directive handler */
static PHP_INI_MH(OnUpdate_mbstring_language)
{
enum mbfl_no_language no_language;
no_language = mbfl_name2no_language(new_value);
if (no_language == mbfl_no_language_invalid) {
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)
{
enum mbfl_no_encoding *list;
int size;
if (php_mb_parse_encoding_list(new_value, new_value_length, &list, &size, 1 TSRMLS_CC)) {
if (MBSTRG(detect_order_list) != NULL) {
free(MBSTRG(detect_order_list));
}
MBSTRG(detect_order_list) = list;
MBSTRG(detect_order_list_size) = size;
} else {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_input) */
static PHP_INI_MH(OnUpdate_mbstring_http_input)
{
enum mbfl_no_encoding *list;
int size;
if (php_mb_parse_encoding_list(new_value, new_value_length, &list, &size, 1 TSRMLS_CC)) {
if (MBSTRG(http_input_list) != NULL) {
free(MBSTRG(http_input_list));
}
MBSTRG(http_input_list) = list;
MBSTRG(http_input_list_size) = size;
} else {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_http_output) */
static PHP_INI_MH(OnUpdate_mbstring_http_output)
{
enum mbfl_no_encoding no_encoding;
no_encoding = mbfl_name2no_encoding(new_value);
if (no_encoding != mbfl_no_encoding_invalid) {
MBSTRG(http_output_encoding) = no_encoding;
MBSTRG(current_http_output_encoding) = no_encoding;
} else {
if (new_value != NULL && new_value_length > 0) {
return FAILURE;
}
}
return SUCCESS;
}
/* }}} */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_internal_encoding) */
static PHP_INI_MH(OnUpdate_mbstring_internal_encoding)
{
enum mbfl_no_encoding no_encoding;
if (new_value == NULL) {
return SUCCESS;
}
no_encoding = mbfl_name2no_encoding(new_value);
if (no_encoding != mbfl_no_encoding_invalid) {
MBSTRG(internal_encoding) = no_encoding;
MBSTRG(current_internal_encoding) = no_encoding;
#if HAVE_MBREGEX
{
php_mb_reg_char_encoding mbctype;
mbctype = php_mb_regex_name2mbctype(new_value);
if (mbctype == REGCODE_UNDEF) {
mbctype = REGCODE_EUCJP;
}
MBSTRG(current_mbctype) = MBSTRG(default_mbctype) = mbctype;
}
#endif
#ifdef ZEND_MULTIBYTE
zend_multibyte_set_internal_encoding(new_value, new_value_length TSRMLS_CC);
#endif /* ZEND_MULTIBYTE */
} else {
if (new_value != NULL && new_value_length > 0) {
return FAILURE;
}
}
return SUCCESS;
}
/* }}} */
#ifdef ZEND_MULTIBYTE
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_script_encoding) */
static PHP_INI_MH(OnUpdate_mbstring_script_encoding)
{
int *list, size;
if (php_mb_parse_encoding_list(new_value, new_value_length, &list, &size, 1 TSRMLS_CC)) {
if (MBSTRG(script_encoding_list) != NULL) {
free(MBSTRG(script_encoding_list));
}
MBSTRG(script_encoding_list) = list;
MBSTRG(script_encoding_list_size) = size;
} else {
return FAILURE;
}
return SUCCESS;
}
/* }}} */
#endif /* ZEND_MULTIBYTE */
/* {{{ static PHP_INI_MH(OnUpdate_mbstring_substitute_character) */
static PHP_INI_MH(OnUpdate_mbstring_substitute_character)
{
if (new_value != NULL) {
if (strcasecmp("none", new_value) == 0) {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_NONE;
} else if (strcasecmp("long", new_value) == 0) {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_LONG;
} else {
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
MBSTRG(filter_illegal_substchar) = zend_atoi(new_value, new_value_length);
}
}
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, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
if (MBSTRG(encoding_translation)){
_php_mb_enable_encoding_translation(1);
} else {
_php_mb_enable_encoding_translation(0);
}
return SUCCESS;
}
/* }}} */
/* {{{ php.ini directive registration */
PHP_INI_BEGIN()
PHP_INI_ENTRY("mbstring.language", "neutral", PHP_INI_SYSTEM | PHP_INI_PERDIR, OnUpdate_mbstring_language)
PHP_INI_ENTRY("mbstring.detect_order", NULL, PHP_INI_ALL, OnUpdate_mbstring_detect_order)
PHP_INI_ENTRY("mbstring.http_input", "pass", PHP_INI_ALL, OnUpdate_mbstring_http_input)
PHP_INI_ENTRY("mbstring.http_output", "pass", PHP_INI_ALL, OnUpdate_mbstring_http_output)
PHP_INI_ENTRY("mbstring.internal_encoding", NULL, PHP_INI_ALL, OnUpdate_mbstring_internal_encoding)
#ifdef ZEND_MULTIBYTE
PHP_INI_ENTRY("mbstring.script_encoding", NULL, PHP_INI_ALL, OnUpdate_mbstring_script_encoding)
#endif /* ZEND_MULTIBYTE */
PHP_INI_ENTRY("mbstring.substitute_character", NULL, PHP_INI_ALL, OnUpdate_mbstring_substitute_character)
STD_PHP_INI_ENTRY("mbstring.func_overload", "0", PHP_INI_SYSTEM |
PHP_INI_PERDIR, OnUpdateLong, func_overload, zend_mbstring_globals, mbstring_globals)
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_END()
/* }}} */
/* {{{ module global initialize handler */
static void _php_mb_globals_ctor(zend_mbstring_globals *pglobals TSRMLS_DC)
{
MBSTRG(language) = mbfl_no_language_uni;
MBSTRG(current_language) = MBSTRG(language);
MBSTRG(internal_encoding) = mbfl_no_encoding_invalid;
MBSTRG(current_internal_encoding) = MBSTRG(internal_encoding);
#ifdef ZEND_MULTIBYTE
MBSTRG(script_encoding_list) = NULL;
MBSTRG(script_encoding_list_size) = 0;
#endif /* ZEND_MULTIBYTE */
MBSTRG(http_output_encoding) = mbfl_no_encoding_pass;
MBSTRG(current_http_output_encoding) = mbfl_no_encoding_pass;
MBSTRG(http_input_identify) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_get) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_post) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_cookie) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_string) = mbfl_no_encoding_invalid;
MBSTRG(http_input_list) = NULL;
MBSTRG(http_input_list_size) = 0;
MBSTRG(detect_order_list) = NULL;
MBSTRG(detect_order_list_size) = 0;
MBSTRG(current_detect_order_list) = NULL;
MBSTRG(current_detect_order_list_size) = 0;
MBSTRG(default_detect_order_list) = (enum mbfl_no_encoding *) php_mb_default_identify_list_neut;
MBSTRG(default_detect_order_list_size) = sizeof(php_mb_default_identify_list_neut) / sizeof(php_mb_default_identify_list_neut[0]);
MBSTRG(filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
MBSTRG(filter_illegal_substchar) = 0x3f; /* '?' */
MBSTRG(current_filter_illegal_mode) = MBFL_OUTPUTFILTER_ILLEGAL_MODE_CHAR;
MBSTRG(current_filter_illegal_substchar) = 0x3f; /* '?' */
MBSTRG(func_overload) = 0;
MBSTRG(encoding_translation) = 0;
pglobals->outconv = NULL;
#if HAVE_MBREGEX
_php_mb_regex_globals_ctor(pglobals TSRMLS_CC);
#endif
}
/* }}} */
/* {{{ static void _php_mb_globals_dtor() */
static void _php_mb_globals_dtor(zend_mbstring_globals *pglobals TSRMLS_DC)
{
#if HAVE_MBREGEX
_php_mb_regex_globals_dtor(pglobals TSRMLS_CC);
#endif
}
/* }}} */
/* {{{ PHP_MINIT_FUNCTION(mbstring) */
PHP_MINIT_FUNCTION(mbstring)
{
__mbfl_allocators = &_php_mb_allocators;
#ifdef ZTS
ts_allocate_id(&mbstring_globals_id, sizeof(zend_mbstring_globals),
(ts_allocate_ctor) _php_mb_globals_ctor,
(ts_allocate_dtor) _php_mb_globals_dtor);
#else
_php_mb_globals_ctor(&mbstring_globals TSRMLS_CC);
#endif
REGISTER_INI_ENTRIES();
if (MBSTRG(encoding_translation)) {
_php_mb_enable_encoding_translation(1);
}
REGISTER_LONG_CONSTANT("MB_OVERLOAD_MAIL", MB_OVERLOAD_MAIL, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MB_OVERLOAD_STRING", MB_OVERLOAD_STRING, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MB_OVERLOAD_REGEX", MB_OVERLOAD_REGEX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MB_CASE_UPPER", PHP_UNICODE_CASE_UPPER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MB_CASE_LOWER", PHP_UNICODE_CASE_LOWER, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("MB_CASE_TITLE", PHP_UNICODE_CASE_TITLE, CONST_CS | CONST_PERSISTENT);
#if HAVE_MBREGEX
PHP_MINIT(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION(mbstring) */
PHP_MSHUTDOWN_FUNCTION(mbstring)
{
UNREGISTER_INI_ENTRIES();
if (MBSTRG(http_input_list)) {
free(MBSTRG(http_input_list));
}
#ifdef ZEND_MULTIBYTE
if (MBSTRG(script_encoding_list)) {
free(MBSTRG(script_encoding_list));
}
#endif /* ZEND_MULTIBYTE */
if (MBSTRG(detect_order_list)) {
free(MBSTRG(detect_order_list));
}
if (MBSTRG(encoding_translation)) {
_php_mb_enable_encoding_translation(0);
}
#if HAVE_MBREGEX
PHP_MSHUTDOWN(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
#endif
#ifdef ZTS
ts_free_id(mbstring_globals_id);
#else
_php_mb_globals_dtor(&mbstring_globals TSRMLS_CC);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RINIT_FUNCTION(mbstring) */
PHP_RINIT_FUNCTION(mbstring)
{
int n;
enum mbfl_no_encoding *list=NULL, *entry;
zend_function *func, *orig;
const struct mb_overload_def *p;
MBSTRG(current_language) = MBSTRG(language);
if (MBSTRG(internal_encoding) == mbfl_no_encoding_invalid) {
char *default_enc = NULL;
switch (MBSTRG(current_language)) {
case mbfl_no_language_uni:
default_enc = "UTF-8";
break;
case mbfl_no_language_japanese:
default_enc = "EUC-JP";
break;
case mbfl_no_language_korean:
default_enc = "EUC-KR";
break;
case mbfl_no_language_simplified_chinese:
default_enc = "EUC-CN";
break;
case mbfl_no_language_traditional_chinese:
default_enc = "EUC-TW";
break;
case mbfl_no_language_russian:
default_enc = "KOI8-R";
break;
case mbfl_no_language_german:
default_enc = "ISO-8859-15";
break;
case mbfl_no_language_english:
default:
default_enc = "ISO-8859-1";
break;
}
if (default_enc) {
zend_alter_ini_entry("mbstring.internal_encoding",
sizeof("mbstring.internal_encoding"),
default_enc, strlen(default_enc),
PHP_INI_PERDIR, PHP_INI_STAGE_RUNTIME);
}
}
MBSTRG(current_internal_encoding) = MBSTRG(internal_encoding);
MBSTRG(current_http_output_encoding) = MBSTRG(http_output_encoding);
MBSTRG(current_filter_illegal_mode) = MBSTRG(filter_illegal_mode);
MBSTRG(current_filter_illegal_substchar) = MBSTRG(filter_illegal_substchar);
n = 0;
if (MBSTRG(detect_order_list)) {
list = MBSTRG(detect_order_list);
n = MBSTRG(detect_order_list_size);
}
if (n <= 0) {
list = MBSTRG(default_detect_order_list);
n = MBSTRG(default_detect_order_list_size);
}
entry = (enum mbfl_no_encoding *)safe_emalloc(n, sizeof(int), 0);
MBSTRG(current_detect_order_list) = entry;
MBSTRG(current_detect_order_list_size) = n;
while (n > 0) {
*entry++ = *list++;
n--;
}
/* override original function. */
if (MBSTRG(func_overload)){
p = &(mb_ovld[0]);
while (p->type > 0) {
if ((MBSTRG(func_overload) & p->type) == p->type &&
zend_hash_find(EG(function_table), p->save_func,
strlen(p->save_func)+1, (void **)&orig) != SUCCESS) {
zend_hash_find(EG(function_table), p->ovld_func, strlen(p->ovld_func)+1 , (void **)&func);
if (zend_hash_find(EG(function_table), p->orig_func, strlen(p->orig_func)+1, (void **)&orig) != SUCCESS) {
php_error_docref("ref.mbstring" TSRMLS_CC, E_WARNING, "mbstring couldn't find function %s.", p->orig_func);
return FAILURE;
} else {
zend_hash_add(EG(function_table), p->save_func, strlen(p->save_func)+1, orig, sizeof(zend_function), NULL);
if (zend_hash_update(EG(function_table), p->orig_func, strlen(p->orig_func)+1, func, sizeof(zend_function),
NULL) == FAILURE) {
php_error_docref("ref.mbstring" TSRMLS_CC, E_WARNING, "mbstring couldn't replace function %s.", p->orig_func);
return FAILURE;
}
}
}
p++;
}
}
#if HAVE_MBREGEX
PHP_RINIT(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
#endif
#ifdef ZEND_MULTIBYTE
php_mb_set_zend_encoding(TSRMLS_C);
#endif /* ZEND_MULTIBYTE */
return SUCCESS;
}
/* }}} */
/* {{{ PHP_RSHUTDOWN_FUNCTION(mbstring) */
PHP_RSHUTDOWN_FUNCTION(mbstring)
{
const struct mb_overload_def *p;
zend_function *orig;
if (MBSTRG(current_detect_order_list) != NULL) {
efree(MBSTRG(current_detect_order_list));
MBSTRG(current_detect_order_list) = NULL;
MBSTRG(current_detect_order_list_size) = 0;
}
if (MBSTRG(outconv) != NULL) {
mbfl_buffer_converter_delete(MBSTRG(outconv));
MBSTRG(outconv) = NULL;
}
/* clear http input identification. */
MBSTRG(http_input_identify) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_post) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_get) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_cookie) = mbfl_no_encoding_invalid;
MBSTRG(http_input_identify_string) = mbfl_no_encoding_invalid;
/* clear overloaded function. */
if (MBSTRG(func_overload)){
p = &(mb_ovld[0]);
while (p->type > 0 && zend_hash_find(EG(function_table), p->save_func, strlen(p->save_func)+1 , (void **)&orig) == SUCCESS) {
zend_hash_update(EG(function_table), p->orig_func, strlen(p->orig_func)+1, orig, sizeof(zend_function), NULL);
zend_hash_del(EG(function_table), p->save_func, strlen(p->save_func)+1);
p++;
}
}
#if HAVE_MBREGEX
PHP_RSHUTDOWN(mb_regex) (INIT_FUNC_ARGS_PASSTHRU);
#endif
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION(mbstring) */
PHP_MINFO_FUNCTION(mbstring)
{
php_info_print_table_start();
php_info_print_table_row(2, "Multibyte Support", "enabled");
php_info_print_table_row(2, "Multibyte string engine", "libmbfl");
if (MBSTRG(encoding_translation)) {
php_info_print_table_row(2, "HTTP input encoding translation", "enabled");
}
#if defined(HAVE_MBREGEX)
{
char buf[32];
php_info_print_table_row(2, "Multibyte (japanese) regex support", "enabled");
sprintf(buf, "%d.%d.%d",
ONIGURUMA_VERSION_MAJOR,ONIGURUMA_VERSION_MINOR,ONIGURUMA_VERSION_TEENY);
php_info_print_table_row(2, "Multibyte regex (oniguruma) version", buf);
}
#endif
php_info_print_table_end();
php_info_print_table_start();
php_info_print_table_colspan_header(2, "mbstring extension makes use of \"streamable kanji code filter and converter\", which is distributed under the GNU Lesser General Public License version 2.1.");
php_info_print_table_end();