Skip to content

Commit 755c2cd

Browse files
committed
Removed compile time dependency from ext/mbstring
1 parent 088a6ad commit 755c2cd

File tree

12 files changed

+481
-279
lines changed

12 files changed

+481
-279
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ PHP NEWS
3232
. Added multibyte suppport by default. Previosly php had to be compiled
3333
with --enable-zend-multibyte. Now it can be enabled or disabled throug
3434
zend.multibyte directive in php.ini (Dmitry)
35+
. Removed compile time dependency from ext/mbstring (Dmitry)
3536
. Added scalar typehints to the parser and the reflection API. (Ilia, Derick)
3637
. Added support for Traits. (Stefan)
3738
. Added closure $this support back. (Stas)

Zend/zend_compile.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ void zend_init_compiler_data_structures(TSRMLS_D) /* {{{ */
200200
CG(script_encoding_list) = NULL;
201201
CG(script_encoding_list_size) = 0;
202202
CG(internal_encoding) = NULL;
203-
CG(encoding_detector) = NULL;
204-
CG(encoding_converter) = NULL;
205-
CG(encoding_oddlen) = NULL;
206203
CG(encoding_declared) = 0;
207204
}
208205
/* }}} */

Zend/zend_globals.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,11 +155,6 @@ struct _zend_compiler_globals {
155155

156156
zend_encoding *internal_encoding;
157157

158-
/* multibyte utility functions */
159-
zend_encoding_detector encoding_detector;
160-
zend_encoding_converter encoding_converter;
161-
zend_encoding_oddlen encoding_oddlen;
162-
163158
#ifdef ZTS
164159
zval ***static_members_table;
165160
int last_static_member;

Zend/zend_multibyte.c

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,36 @@ static zend_encoding *zend_encoding_table[] = {
513513
NULL
514514
};
515515

516+
static char* dummy_encoding_detector(const unsigned char *string, size_t length, char *list TSRMLS_DC)
517+
{
518+
return NULL;
519+
}
520+
521+
static int dummy_encoding_converter(unsigned char **to, size_t *to_length, const unsigned char *from, size_t from_length, const char *encoding_to, const char *encoding_from TSRMLS_DC)
522+
{
523+
return -1;
524+
}
516525

526+
static size_t dummy_encoding_oddlen(const unsigned char *string, size_t length, const char *encoding TSRMLS_DC)
527+
{
528+
return 0;
529+
}
530+
531+
static int dummy_encoding_list_checker(const char *encoding_list TSRMLS_DC)
532+
{
533+
return 0;
534+
}
535+
536+
static const char* dummy_get_internal_encoding(TSRMLS_D)
537+
{
538+
return NULL;
539+
}
540+
541+
ZEND_API zend_encoding_detector zend_multibyte_encoding_detector = dummy_encoding_detector;
542+
ZEND_API zend_encoding_converter zend_multibyte_encoding_converter = dummy_encoding_converter;
543+
ZEND_API zend_encoding_oddlen zend_multibyte_encoding_oddlen = dummy_encoding_oddlen;
544+
ZEND_API zend_encoding_list_checker zend_multibyte_check_encoding_list = dummy_encoding_list_checker;
545+
ZEND_API zend_encoding_name_getter zend_multibyte_get_internal_encoding = dummy_get_internal_encoding;
517546

518547
ZEND_API int zend_multibyte_set_script_encoding(const char *encoding_list,
519548
size_t encoding_list_size TSRMLS_DC)
@@ -540,11 +569,13 @@ ZEND_API int zend_multibyte_set_internal_encoding(const char *encoding_name TSRM
540569
return 0;
541570
}
542571

543-
ZEND_API int zend_multibyte_set_functions(zend_encoding_detector encoding_detector, zend_encoding_converter encoding_converter, zend_encoding_oddlen encoding_oddlen TSRMLS_DC)
572+
ZEND_API int zend_multibyte_set_functions(zend_encoding_detector encoding_detector, zend_encoding_converter encoding_converter, zend_encoding_oddlen encoding_oddlen, zend_encoding_list_checker encoding_list_checker, zend_encoding_name_getter get_internal_encoding TSRMLS_DC)
544573
{
545-
CG(encoding_detector) = encoding_detector;
546-
CG(encoding_converter) = encoding_converter;
547-
CG(encoding_oddlen) = encoding_oddlen;
574+
zend_multibyte_encoding_detector = encoding_detector;
575+
zend_multibyte_encoding_converter = encoding_converter;
576+
zend_multibyte_encoding_oddlen = encoding_oddlen;
577+
zend_multibyte_check_encoding_list = encoding_list_checker;
578+
zend_multibyte_get_internal_encoding = get_internal_encoding;
548579
return 0;
549580
}
550581

@@ -659,18 +690,16 @@ static size_t zend_multibyte_encoding_filter(unsigned char **to, size_t *to_leng
659690
{
660691
size_t oddlen;
661692

662-
if (!CG(encoding_converter)) {
693+
if (zend_multibyte_encoding_converter == dummy_encoding_converter) {
663694
return 0;
664695
}
665696

666-
if (CG(encoding_oddlen)) {
667-
oddlen = CG(encoding_oddlen)(from, from_length, from_encoding TSRMLS_CC);
668-
if (oddlen > 0) {
669-
from_length -= oddlen;
670-
}
697+
oddlen = zend_multibyte_encoding_oddlen(from, from_length, from_encoding TSRMLS_CC);
698+
if (oddlen > 0) {
699+
from_length -= oddlen;
671700
}
672701

673-
if (CG(encoding_converter)(to, to_length, from, from_length, to_encoding, from_encoding TSRMLS_CC) != 0) {
702+
if (zend_multibyte_encoding_converter(to, to_length, from, from_length, to_encoding, from_encoding TSRMLS_CC) != 0) {
674703
return 0;
675704
}
676705

@@ -1053,10 +1082,11 @@ static zend_encoding* zend_multibyte_find_script_encoding(zend_encoding *onetime
10531082
}
10541083

10551084
/* if multiple encodings specified, detect automagically */
1056-
if (CG(script_encoding_list_size) > 1 && CG(encoding_detector)) {
1085+
if (CG(script_encoding_list_size) > 1 &&
1086+
zend_multibyte_encoding_detector != dummy_encoding_detector) {
10571087
list = zend_multibyte_assemble_encoding_list(CG(script_encoding_list),
10581088
CG(script_encoding_list_size));
1059-
name = CG(encoding_detector)(LANG_SCNG(script_org),
1089+
name = zend_multibyte_encoding_detector(LANG_SCNG(script_org),
10601090
LANG_SCNG(script_org_size), list TSRMLS_CC);
10611091
if (list) {
10621092
efree(list);

Zend/zend_multibyte.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ typedef int (*zend_encoding_converter)(unsigned char **to, size_t *to_length, co
3636

3737
typedef size_t (*zend_encoding_oddlen)(const unsigned char *string, size_t length, const char *encoding TSRMLS_DC);
3838

39+
typedef int (*zend_encoding_list_checker)(const char *encoding_list TSRMLS_DC);
40+
41+
typedef const char* (*zend_encoding_name_getter)(TSRMLS_D);
42+
3943
typedef struct _zend_encoding {
4044
zend_encoding_filter input_filter; /* escape input filter */
4145
zend_encoding_filter output_filter; /* escape output filter */
@@ -49,10 +53,18 @@ typedef struct _zend_encoding {
4953
* zend multibyte APIs
5054
*/
5155
BEGIN_EXTERN_C()
56+
57+
/* multibyte utility functions */
58+
ZEND_API extern zend_encoding_detector zend_multibyte_encoding_detector;
59+
ZEND_API extern zend_encoding_converter zend_multibyte_encoding_converter;
60+
ZEND_API extern zend_encoding_oddlen zend_multibyte_encoding_oddlen;
61+
ZEND_API extern zend_encoding_list_checker zend_multibyte_check_encoding_list;
62+
ZEND_API extern zend_encoding_name_getter zend_multibyte_get_internal_encoding;
63+
5264
ZEND_API int zend_multibyte_set_script_encoding(const char *encoding_list,
5365
size_t encoding_list_size TSRMLS_DC);
5466
ZEND_API int zend_multibyte_set_internal_encoding(const char *encoding_name TSRMLS_DC);
55-
ZEND_API int zend_multibyte_set_functions(zend_encoding_detector encoding_detector, zend_encoding_converter encoding_converter, zend_encoding_oddlen encoding_oddlen TSRMLS_DC);
67+
ZEND_API int zend_multibyte_set_functions(zend_encoding_detector encoding_detector, zend_encoding_converter encoding_converter, zend_encoding_oddlen encoding_oddlen, zend_encoding_list_checker encoding_list_checker, zend_encoding_name_getter get_internal_encoding TSRMLS_DC);
5668
ZEND_API int zend_multibyte_set_filter(zend_encoding *onetime_encoding TSRMLS_DC);
5769
ZEND_API zend_encoding* zend_multibyte_fetch_encoding(const char *encoding_name);
5870
ZEND_API size_t zend_multibyte_script_encoding_filter(unsigned char **to, size_t

ext/exif/exif.c

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@
6666
#include "ext/standard/php_image.h"
6767
#include "ext/standard/info.h"
6868

69-
#if defined(PHP_WIN32) || (HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING))
70-
#define EXIF_USE_MBSTRING 1
71-
#else
72-
#define EXIF_USE_MBSTRING 0
73-
#endif
74-
75-
#if EXIF_USE_MBSTRING
76-
#include "ext/mbstring/mbstring.h"
77-
#endif
78-
7969
/* needed for ssize_t definition */
8070
#include <sys/types.h>
8171

@@ -176,23 +166,19 @@ ZEND_DECLARE_MODULE_GLOBALS(exif)
176166

177167
ZEND_INI_MH(OnUpdateEncode)
178168
{
179-
#if EXIF_USE_MBSTRING
180-
if (new_value && strlen(new_value) && !php_mb_check_encoding_list(new_value TSRMLS_CC)) {
169+
if (new_value && strlen(new_value) && !zend_multibyte_check_encoding_list(new_value TSRMLS_CC)) {
181170
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value);
182171
return FAILURE;
183172
}
184-
#endif
185173
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
186174
}
187175

188176
ZEND_INI_MH(OnUpdateDecode)
189177
{
190-
#if EXIF_USE_MBSTRING
191-
if (!php_mb_check_encoding_list(new_value TSRMLS_CC)) {
178+
if (!zend_multibyte_check_encoding_list(new_value TSRMLS_CC)) {
192179
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value);
193180
return FAILURE;
194181
}
195-
#endif
196182
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
197183
}
198184

@@ -224,7 +210,11 @@ static PHP_GINIT_FUNCTION(exif)
224210
PHP_MINIT_FUNCTION(exif)
225211
{
226212
REGISTER_INI_ENTRIES();
227-
REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", EXIF_USE_MBSTRING, CONST_CS | CONST_PERSISTENT);
213+
if (zend_hash_exists(&module_registry, "mbstring", sizeof("mbstring"))) {
214+
REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 1, CONST_CS | CONST_PERSISTENT);
215+
} else {
216+
REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 0, CONST_CS | CONST_PERSISTENT);
217+
}
228218
return SUCCESS;
229219
}
230220
/* }}} */
@@ -241,9 +231,7 @@ PHP_MSHUTDOWN_FUNCTION(exif)
241231
/* {{{ exif dependencies */
242232
static const zend_module_dep exif_module_deps[] = {
243233
ZEND_MOD_REQUIRED("standard")
244-
#if EXIF_USE_MBSTRING
245-
ZEND_MOD_REQUIRED("mbstring")
246-
#endif
234+
ZEND_MOD_OPTIONAL("mbstring")
247235
{NULL, NULL, NULL}
248236
};
249237
/* }}} */
@@ -2588,7 +2576,6 @@ static int exif_process_undefined(char **result, char *value, size_t byte_count
25882576

25892577
/* {{{ exif_process_string_raw
25902578
* Copy a string in Exif header to a character string returns length of allocated buffer if any. */
2591-
#if !EXIF_USE_MBSTRING
25922579
static int exif_process_string_raw(char **result, char *value, size_t byte_count) {
25932580
/* we cannot use strlcpy - here the problem is that we have to copy NUL
25942581
* chars up to byte_count, we also have to add a single NUL character to
@@ -2602,7 +2589,6 @@ static int exif_process_string_raw(char **result, char *value, size_t byte_count
26022589
}
26032590
return 0;
26042591
}
2605-
#endif
26062592
/* }}} */
26072593

26082594
/* {{{ exif_process_string
@@ -2629,11 +2615,8 @@ static int exif_process_string(char **result, char *value, size_t byte_count TSR
26292615
static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoPtr, char **pszEncoding, char *szValuePtr, int ByteCount TSRMLS_DC)
26302616
{
26312617
int a;
2632-
2633-
#if EXIF_USE_MBSTRING
26342618
char *decode;
26352619
size_t len;;
2636-
#endif
26372620

26382621
*pszEncoding = NULL;
26392622
/* Copy the comment */
@@ -2642,7 +2625,6 @@ static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoP
26422625
*pszEncoding = estrdup((const char*)szValuePtr);
26432626
szValuePtr = szValuePtr+8;
26442627
ByteCount -= 8;
2645-
#if EXIF_USE_MBSTRING
26462628
/* First try to detect BOM: ZERO WIDTH NOBREAK SPACE (FEFF 16)
26472629
* since we have no encoding support for the BOM yet we skip that.
26482630
*/
@@ -2659,34 +2641,38 @@ static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoP
26592641
} else {
26602642
decode = ImageInfo->decode_unicode_le;
26612643
}
2662-
*pszInfoPtr = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, decode, &len TSRMLS_CC);
2644+
if (zend_multibyte_encoding_converter(
2645+
pszInfoPtr,
2646+
&len,
2647+
szValuePtr,
2648+
ByteCount,
2649+
ImageInfo->encode_unicode,
2650+
decode
2651+
TSRMLS_DC) != 0) {
2652+
len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
2653+
}
26632654
return len;
2664-
#else
2665-
return exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
2666-
#endif
2667-
} else
2668-
if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
2655+
} else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
26692656
*pszEncoding = estrdup((const char*)szValuePtr);
26702657
szValuePtr = szValuePtr+8;
26712658
ByteCount -= 8;
2672-
} else
2673-
if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
2659+
} else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
26742660
/* JIS should be tanslated to MB or we leave it to the user - leave it to the user */
26752661
*pszEncoding = estrdup((const char*)szValuePtr);
26762662
szValuePtr = szValuePtr+8;
26772663
ByteCount -= 8;
2678-
#if EXIF_USE_MBSTRING
2679-
if (ImageInfo->motorola_intel) {
2680-
*pszInfoPtr = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_jis, ImageInfo->decode_jis_be, &len TSRMLS_CC);
2681-
} else {
2682-
*pszInfoPtr = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_jis, ImageInfo->decode_jis_le, &len TSRMLS_CC);
2664+
if (zend_multibyte_encoding_converter(
2665+
pszInfoPtr,
2666+
&len,
2667+
szValuePtr,
2668+
ByteCount,
2669+
ImageInfo->encode_jis,
2670+
ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le
2671+
TSRMLS_DC) != 0) {
2672+
len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
26832673
}
26842674
return len;
2685-
#else
2686-
return exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
2687-
#endif
2688-
} else
2689-
if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
2675+
} else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
26902676
/* 8 NULL means undefined and should be ASCII... */
26912677
*pszEncoding = estrdup("UNDEFINED");
26922678
szValuePtr = szValuePtr+8;
@@ -2714,19 +2700,17 @@ static int exif_process_unicode(image_info_type *ImageInfo, xp_field_type *xp_fi
27142700
xp_field->tag = tag;
27152701

27162702
/* Copy the comment */
2717-
#if EXIF_USE_MBSTRING
2718-
/* What if MS supports big-endian with XP? */
2719-
/* if (ImageInfo->motorola_intel) {
2720-
xp_field->value = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, ImageInfo->decode_unicode_be, &xp_field->size TSRMLS_CC);
2721-
} else {
2722-
xp_field->value = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, ImageInfo->decode_unicode_le, &xp_field->size TSRMLS_CC);
2723-
}*/
2724-
xp_field->value = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, ImageInfo->decode_unicode_le, &xp_field->size TSRMLS_CC);
2725-
return xp_field->size;
2726-
#else
2727-
xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
2703+
if (zend_multibyte_encoding_converter(
2704+
&xp_field->value,
2705+
&xp_field->size,
2706+
szValuePtr,
2707+
ByteCount,
2708+
ImageInfo->encode_unicode,
2709+
ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le
2710+
TSRMLS_DC) != 0) {
2711+
xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
2712+
}
27282713
return xp_field->size;
2729-
#endif
27302714
}
27312715
/* }}} */
27322716

0 commit comments

Comments
 (0)