Skip to content

Commit

Permalink
Removed compile time dependency from ext/mbstring
Browse files Browse the repository at this point in the history
  • Loading branch information
dstogov committed Dec 8, 2010
1 parent 088a6ad commit 755c2cd
Show file tree
Hide file tree
Showing 12 changed files with 481 additions and 279 deletions.
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ PHP NEWS
. Added multibyte suppport by default. Previosly php had to be compiled
with --enable-zend-multibyte. Now it can be enabled or disabled throug
zend.multibyte directive in php.ini (Dmitry)
. Removed compile time dependency from ext/mbstring (Dmitry)
. Added scalar typehints to the parser and the reflection API. (Ilia, Derick)
. Added support for Traits. (Stefan)
. Added closure $this support back. (Stas)
Expand Down
3 changes: 0 additions & 3 deletions Zend/zend_compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,6 @@ void zend_init_compiler_data_structures(TSRMLS_D) /* {{{ */
CG(script_encoding_list) = NULL;
CG(script_encoding_list_size) = 0;
CG(internal_encoding) = NULL;
CG(encoding_detector) = NULL;
CG(encoding_converter) = NULL;
CG(encoding_oddlen) = NULL;
CG(encoding_declared) = 0;
}
/* }}} */
Expand Down
5 changes: 0 additions & 5 deletions Zend/zend_globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ struct _zend_compiler_globals {

zend_encoding *internal_encoding;

/* multibyte utility functions */
zend_encoding_detector encoding_detector;
zend_encoding_converter encoding_converter;
zend_encoding_oddlen encoding_oddlen;

#ifdef ZTS
zval ***static_members_table;
int last_static_member;
Expand Down
56 changes: 43 additions & 13 deletions Zend/zend_multibyte.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,36 @@ static zend_encoding *zend_encoding_table[] = {
NULL
};

static char* dummy_encoding_detector(const unsigned char *string, size_t length, char *list TSRMLS_DC)
{
return NULL;
}

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)
{
return -1;
}

static size_t dummy_encoding_oddlen(const unsigned char *string, size_t length, const char *encoding TSRMLS_DC)
{
return 0;
}

static int dummy_encoding_list_checker(const char *encoding_list TSRMLS_DC)
{
return 0;
}

static const char* dummy_get_internal_encoding(TSRMLS_D)
{
return NULL;
}

ZEND_API zend_encoding_detector zend_multibyte_encoding_detector = dummy_encoding_detector;
ZEND_API zend_encoding_converter zend_multibyte_encoding_converter = dummy_encoding_converter;
ZEND_API zend_encoding_oddlen zend_multibyte_encoding_oddlen = dummy_encoding_oddlen;
ZEND_API zend_encoding_list_checker zend_multibyte_check_encoding_list = dummy_encoding_list_checker;
ZEND_API zend_encoding_name_getter zend_multibyte_get_internal_encoding = dummy_get_internal_encoding;

ZEND_API int zend_multibyte_set_script_encoding(const char *encoding_list,
size_t encoding_list_size TSRMLS_DC)
Expand All @@ -540,11 +569,13 @@ ZEND_API int zend_multibyte_set_internal_encoding(const char *encoding_name TSRM
return 0;
}

ZEND_API int zend_multibyte_set_functions(zend_encoding_detector encoding_detector, zend_encoding_converter encoding_converter, zend_encoding_oddlen encoding_oddlen TSRMLS_DC)
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)
{
CG(encoding_detector) = encoding_detector;
CG(encoding_converter) = encoding_converter;
CG(encoding_oddlen) = encoding_oddlen;
zend_multibyte_encoding_detector = encoding_detector;
zend_multibyte_encoding_converter = encoding_converter;
zend_multibyte_encoding_oddlen = encoding_oddlen;
zend_multibyte_check_encoding_list = encoding_list_checker;
zend_multibyte_get_internal_encoding = get_internal_encoding;
return 0;
}

Expand Down Expand Up @@ -659,18 +690,16 @@ static size_t zend_multibyte_encoding_filter(unsigned char **to, size_t *to_leng
{
size_t oddlen;

if (!CG(encoding_converter)) {
if (zend_multibyte_encoding_converter == dummy_encoding_converter) {
return 0;
}

if (CG(encoding_oddlen)) {
oddlen = CG(encoding_oddlen)(from, from_length, from_encoding TSRMLS_CC);
if (oddlen > 0) {
from_length -= oddlen;
}
oddlen = zend_multibyte_encoding_oddlen(from, from_length, from_encoding TSRMLS_CC);
if (oddlen > 0) {
from_length -= oddlen;
}

if (CG(encoding_converter)(to, to_length, from, from_length, to_encoding, from_encoding TSRMLS_CC) != 0) {
if (zend_multibyte_encoding_converter(to, to_length, from, from_length, to_encoding, from_encoding TSRMLS_CC) != 0) {
return 0;
}

Expand Down Expand Up @@ -1053,10 +1082,11 @@ static zend_encoding* zend_multibyte_find_script_encoding(zend_encoding *onetime
}

/* if multiple encodings specified, detect automagically */
if (CG(script_encoding_list_size) > 1 && CG(encoding_detector)) {
if (CG(script_encoding_list_size) > 1 &&
zend_multibyte_encoding_detector != dummy_encoding_detector) {
list = zend_multibyte_assemble_encoding_list(CG(script_encoding_list),
CG(script_encoding_list_size));
name = CG(encoding_detector)(LANG_SCNG(script_org),
name = zend_multibyte_encoding_detector(LANG_SCNG(script_org),
LANG_SCNG(script_org_size), list TSRMLS_CC);
if (list) {
efree(list);
Expand Down
14 changes: 13 additions & 1 deletion Zend/zend_multibyte.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ typedef int (*zend_encoding_converter)(unsigned char **to, size_t *to_length, co

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

typedef int (*zend_encoding_list_checker)(const char *encoding_list TSRMLS_DC);

typedef const char* (*zend_encoding_name_getter)(TSRMLS_D);

typedef struct _zend_encoding {
zend_encoding_filter input_filter; /* escape input filter */
zend_encoding_filter output_filter; /* escape output filter */
Expand All @@ -49,10 +53,18 @@ typedef struct _zend_encoding {
* zend multibyte APIs
*/
BEGIN_EXTERN_C()

/* multibyte utility functions */
ZEND_API extern zend_encoding_detector zend_multibyte_encoding_detector;
ZEND_API extern zend_encoding_converter zend_multibyte_encoding_converter;
ZEND_API extern zend_encoding_oddlen zend_multibyte_encoding_oddlen;
ZEND_API extern zend_encoding_list_checker zend_multibyte_check_encoding_list;
ZEND_API extern zend_encoding_name_getter zend_multibyte_get_internal_encoding;

ZEND_API int zend_multibyte_set_script_encoding(const char *encoding_list,
size_t encoding_list_size TSRMLS_DC);
ZEND_API int zend_multibyte_set_internal_encoding(const char *encoding_name TSRMLS_DC);
ZEND_API int zend_multibyte_set_functions(zend_encoding_detector encoding_detector, zend_encoding_converter encoding_converter, zend_encoding_oddlen encoding_oddlen TSRMLS_DC);
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);
ZEND_API int zend_multibyte_set_filter(zend_encoding *onetime_encoding TSRMLS_DC);
ZEND_API zend_encoding* zend_multibyte_fetch_encoding(const char *encoding_name);
ZEND_API size_t zend_multibyte_script_encoding_filter(unsigned char **to, size_t
Expand Down
96 changes: 40 additions & 56 deletions ext/exif/exif.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,6 @@
#include "ext/standard/php_image.h"
#include "ext/standard/info.h"

#if defined(PHP_WIN32) || (HAVE_MBSTRING && !defined(COMPILE_DL_MBSTRING))
#define EXIF_USE_MBSTRING 1
#else
#define EXIF_USE_MBSTRING 0
#endif

#if EXIF_USE_MBSTRING
#include "ext/mbstring/mbstring.h"
#endif

/* needed for ssize_t definition */
#include <sys/types.h>

Expand Down Expand Up @@ -176,23 +166,19 @@ ZEND_DECLARE_MODULE_GLOBALS(exif)

ZEND_INI_MH(OnUpdateEncode)
{
#if EXIF_USE_MBSTRING
if (new_value && strlen(new_value) && !php_mb_check_encoding_list(new_value TSRMLS_CC)) {
if (new_value && strlen(new_value) && !zend_multibyte_check_encoding_list(new_value TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value);
return FAILURE;
}
#endif
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
}

ZEND_INI_MH(OnUpdateDecode)
{
#if EXIF_USE_MBSTRING
if (!php_mb_check_encoding_list(new_value TSRMLS_CC)) {
if (!zend_multibyte_check_encoding_list(new_value TSRMLS_CC)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Illegal encoding ignored: '%s'", new_value);
return FAILURE;
}
#endif
return OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
}

Expand Down Expand Up @@ -224,7 +210,11 @@ static PHP_GINIT_FUNCTION(exif)
PHP_MINIT_FUNCTION(exif)
{
REGISTER_INI_ENTRIES();
REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", EXIF_USE_MBSTRING, CONST_CS | CONST_PERSISTENT);
if (zend_hash_exists(&module_registry, "mbstring", sizeof("mbstring"))) {
REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 1, CONST_CS | CONST_PERSISTENT);
} else {
REGISTER_LONG_CONSTANT("EXIF_USE_MBSTRING", 0, CONST_CS | CONST_PERSISTENT);
}
return SUCCESS;
}
/* }}} */
Expand All @@ -241,9 +231,7 @@ PHP_MSHUTDOWN_FUNCTION(exif)
/* {{{ exif dependencies */
static const zend_module_dep exif_module_deps[] = {
ZEND_MOD_REQUIRED("standard")
#if EXIF_USE_MBSTRING
ZEND_MOD_REQUIRED("mbstring")
#endif
ZEND_MOD_OPTIONAL("mbstring")
{NULL, NULL, NULL}
};
/* }}} */
Expand Down Expand Up @@ -2588,7 +2576,6 @@ static int exif_process_undefined(char **result, char *value, size_t byte_count

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

/* {{{ exif_process_string
Expand All @@ -2629,11 +2615,8 @@ static int exif_process_string(char **result, char *value, size_t byte_count TSR
static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoPtr, char **pszEncoding, char *szValuePtr, int ByteCount TSRMLS_DC)
{
int a;

#if EXIF_USE_MBSTRING
char *decode;
size_t len;;
#endif

*pszEncoding = NULL;
/* Copy the comment */
Expand All @@ -2642,7 +2625,6 @@ static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoP
*pszEncoding = estrdup((const char*)szValuePtr);
szValuePtr = szValuePtr+8;
ByteCount -= 8;
#if EXIF_USE_MBSTRING
/* First try to detect BOM: ZERO WIDTH NOBREAK SPACE (FEFF 16)
* since we have no encoding support for the BOM yet we skip that.
*/
Expand All @@ -2659,34 +2641,38 @@ static int exif_process_user_comment(image_info_type *ImageInfo, char **pszInfoP
} else {
decode = ImageInfo->decode_unicode_le;
}
*pszInfoPtr = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, decode, &len TSRMLS_CC);
if (zend_multibyte_encoding_converter(
pszInfoPtr,
&len,
szValuePtr,
ByteCount,
ImageInfo->encode_unicode,
decode
TSRMLS_DC) != 0) {
len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
}
return len;
#else
return exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
#endif
} else
if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
} else if (!memcmp(szValuePtr, "ASCII\0\0\0", 8)) {
*pszEncoding = estrdup((const char*)szValuePtr);
szValuePtr = szValuePtr+8;
ByteCount -= 8;
} else
if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
} else if (!memcmp(szValuePtr, "JIS\0\0\0\0\0", 8)) {
/* JIS should be tanslated to MB or we leave it to the user - leave it to the user */
*pszEncoding = estrdup((const char*)szValuePtr);
szValuePtr = szValuePtr+8;
ByteCount -= 8;
#if EXIF_USE_MBSTRING
if (ImageInfo->motorola_intel) {
*pszInfoPtr = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_jis, ImageInfo->decode_jis_be, &len TSRMLS_CC);
} else {
*pszInfoPtr = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_jis, ImageInfo->decode_jis_le, &len TSRMLS_CC);
if (zend_multibyte_encoding_converter(
pszInfoPtr,
&len,
szValuePtr,
ByteCount,
ImageInfo->encode_jis,
ImageInfo->motorola_intel ? ImageInfo->decode_jis_be : ImageInfo->decode_jis_le
TSRMLS_DC) != 0) {
len = exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
}
return len;
#else
return exif_process_string_raw(pszInfoPtr, szValuePtr, ByteCount);
#endif
} else
if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
} else if (!memcmp(szValuePtr, "\0\0\0\0\0\0\0\0", 8)) {
/* 8 NULL means undefined and should be ASCII... */
*pszEncoding = estrdup("UNDEFINED");
szValuePtr = szValuePtr+8;
Expand Down Expand Up @@ -2714,19 +2700,17 @@ static int exif_process_unicode(image_info_type *ImageInfo, xp_field_type *xp_fi
xp_field->tag = tag;

/* Copy the comment */
#if EXIF_USE_MBSTRING
/* What if MS supports big-endian with XP? */
/* if (ImageInfo->motorola_intel) {
xp_field->value = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, ImageInfo->decode_unicode_be, &xp_field->size TSRMLS_CC);
} else {
xp_field->value = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, ImageInfo->decode_unicode_le, &xp_field->size TSRMLS_CC);
}*/
xp_field->value = php_mb_convert_encoding(szValuePtr, ByteCount, ImageInfo->encode_unicode, ImageInfo->decode_unicode_le, &xp_field->size TSRMLS_CC);
return xp_field->size;
#else
xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
if (zend_multibyte_encoding_converter(
&xp_field->value,
&xp_field->size,
szValuePtr,
ByteCount,
ImageInfo->encode_unicode,
ImageInfo->motorola_intel ? ImageInfo->decode_unicode_be : ImageInfo->decode_unicode_le
TSRMLS_DC) != 0) {
xp_field->size = exif_process_string_raw(&xp_field->value, szValuePtr, ByteCount);
}
return xp_field->size;
#endif
}
/* }}} */

Expand Down
Loading

0 comments on commit 755c2cd

Please sign in to comment.