summaryrefslogtreecommitdiff
path: root/ext/iconv
diff options
context:
space:
mode:
authorYasuo Ohgaki <yohgaki@php.net>2014-03-24 11:04:35 +0900
committerYasuo Ohgaki <yohgaki@php.net>2014-03-27 17:20:57 +0900
commita84e5dc37dc0ff8c313164d9db141d3d9f2b2730 (patch)
tree16eb344caca740c5853171e18f0acab52381a545 /ext/iconv
parent7ccbfb2e8467f4a5bec90209254f78ffb948755e (diff)
downloadphp-git-a84e5dc37dc0ff8c313164d9db141d3d9f2b2730.tar.gz
Remove unneeded string copy.
Allow to set ''(empty string values) internal/input/output_encoding for better compatibility. i.e. Runtime INI value changes. More compliance to the RFC. Improve/add encoding handling tests. i.e. Rather than set encoding automagic way, detect it.
Diffstat (limited to 'ext/iconv')
-rw-r--r--ext/iconv/iconv.c98
-rw-r--r--ext/iconv/tests/iconv_default_charset.phpt77
2 files changed, 137 insertions, 38 deletions
diff --git a/ext/iconv/iconv.c b/ext/iconv/iconv.c
index 57aed6b4b0..2df759f99b 100644
--- a/ext/iconv/iconv.c
+++ b/ext/iconv/iconv.c
@@ -226,14 +226,10 @@ static PHP_INI_MH(OnUpdateInputEncoding)
if (new_value_length >= ICONV_CSNMAXLEN) {
return FAILURE;
}
- if (new_value_length) {
- if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
- php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.input_encoding is deprecated");
- }
- OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
- } else {
- OnUpdateString(entry, PG(input_encoding), strlen(PG(input_encoding))+1, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
+ if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
+ php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.input_encoding is deprecated");
}
+ OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
return SUCCESS;
}
@@ -243,14 +239,10 @@ static PHP_INI_MH(OnUpdateOutputEncoding)
if(new_value_length >= ICONV_CSNMAXLEN) {
return FAILURE;
}
- if (new_value_length) {
- if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
- php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.output_encoding is deprecated");
- }
- OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
- } else {
- OnUpdateString(entry, PG(output_encoding), strlen(PG(output_encoding))+1, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
+ if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
+ php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.output_encoding is deprecated");
}
+ OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
return SUCCESS;
}
@@ -260,14 +252,10 @@ static PHP_INI_MH(OnUpdateInternalEncoding)
if(new_value_length >= ICONV_CSNMAXLEN) {
return FAILURE;
}
- if (new_value_length) {
- if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
- php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.internal_encoding is deprecated");
- }
- OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
- } else {
- OnUpdateString(entry, PG(internal_encoding), strlen(PG(internal_encoding))+1, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
+ if (stage & (PHP_INI_STAGE_ACTIVATE | PHP_INI_STAGE_RUNTIME)) {
+ php_error_docref("ref.iconv" TSRMLS_CC, E_DEPRECATED, "Use of iconv.internal_encoding is deprecated");
}
+ OnUpdateString(entry, new_value, new_value_length, mh_arg1, mh_arg2, mh_arg3, stage TSRMLS_CC);
return SUCCESS;
}
@@ -356,6 +344,40 @@ PHP_MINFO_FUNCTION(miconv)
}
/* }}} */
+static char *get_internal_encoding(TSRMLS_D) {
+ if (ICONVG(internal_encoding) && ICONVG(internal_encoding)[0]) {
+ return ICONVG(internal_encoding);
+ } else if (PG(internal_encoding) && PG(internal_encoding)[0]) {
+ return PG(internal_encoding);
+ } else if (SG(default_charset)) {
+ return SG(default_charset);
+ }
+ return "";
+}
+
+static char *get_input_encoding(TSRMLS_D) {
+ if (ICONVG(input_encoding) && ICONVG(input_encoding)[0]) {
+ return ICONVG(input_encoding);
+ } else if (PG(input_encoding) && PG(input_encoding)[0]) {
+ return PG(input_encoding);
+ } else if (SG(default_charset)) {
+ return SG(default_charset);
+ }
+ return "";
+}
+
+static char *get_output_encoding(TSRMLS_D) {
+ if (ICONVG(output_encoding) && ICONVG(output_encoding)[0]) {
+ return ICONVG(output_encoding);
+ } else if (PG(output_encoding) && PG(output_encoding)[0]) {
+ return PG(output_encoding);
+ } else if (SG(default_charset)) {
+ return SG(default_charset);
+ }
+ return "";
+}
+
+
static int php_iconv_output_conflict(const char *handler_name, size_t handler_name_len TSRMLS_DC)
{
if (php_output_get_level(TSRMLS_C)) {
@@ -397,12 +419,12 @@ static int php_iconv_output_handler(void **nothing, php_output_context *output_c
if (mimetype != NULL && !(output_context->op & PHP_OUTPUT_HANDLER_CLEAN)) {
int len;
- char *p = strstr(ICONVG(output_encoding), "//");
+ char *p = strstr(get_output_encoding(TSRMLS_C), "//");
if (p) {
- len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%.*s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, (int)(p - ICONVG(output_encoding)), ICONVG(output_encoding));
+ len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%.*s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, (int)(p - get_output_encoding(TSRMLS_C)), get_output_encoding(TSRMLS_C));
} else {
- len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, ICONVG(output_encoding));
+ len = spprintf(&content_type, 0, "Content-Type:%.*s; charset=%s", mimetype_len ? mimetype_len : (int) strlen(mimetype), mimetype, get_output_encoding(TSRMLS_C));
}
if (content_type && SUCCESS == sapi_add_header(content_type, len, 0)) {
SG(sapi_headers).send_default_content_type = 0;
@@ -413,7 +435,7 @@ static int php_iconv_output_handler(void **nothing, php_output_context *output_c
if (output_context->in.used) {
output_context->out.free = 1;
- _php_iconv_show_error(php_iconv_string(output_context->in.data, output_context->in.used, &output_context->out.data, &output_context->out.used, ICONVG(output_encoding), ICONVG(internal_encoding)), ICONVG(output_encoding), ICONVG(internal_encoding) TSRMLS_CC);
+ _php_iconv_show_error(php_iconv_string(output_context->in.data, output_context->in.used, &output_context->out.data, &output_context->out.used, get_output_encoding(TSRMLS_C), get_internal_encoding(TSRMLS_C)), get_output_encoding(TSRMLS_C), get_internal_encoding(TSRMLS_C) TSRMLS_CC);
}
return SUCCESS;
@@ -2000,7 +2022,7 @@ static void _php_iconv_show_error(php_iconv_err_t err, const char *out_charset,
Returns the character count of str */
PHP_FUNCTION(iconv_strlen)
{
- char *charset = ICONVG(internal_encoding);
+ char *charset = get_internal_encoding(TSRMLS_C);
int charset_len = 0;
char *str;
int str_len;
@@ -2033,7 +2055,7 @@ PHP_FUNCTION(iconv_strlen)
Returns specified part of a string */
PHP_FUNCTION(iconv_substr)
{
- char *charset = ICONVG(internal_encoding);
+ char *charset = get_internal_encoding(TSRMLS_C);
int charset_len = 0;
char *str;
int str_len;
@@ -2073,7 +2095,7 @@ PHP_FUNCTION(iconv_substr)
Finds position of first occurrence of needle within part of haystack beginning with offset */
PHP_FUNCTION(iconv_strpos)
{
- char *charset = ICONVG(internal_encoding);
+ char *charset = get_internal_encoding(TSRMLS_C);
int charset_len = 0;
char *haystk;
int haystk_len;
@@ -2121,7 +2143,7 @@ PHP_FUNCTION(iconv_strpos)
Finds position of last occurrence of needle within part of haystack beginning with offset */
PHP_FUNCTION(iconv_strrpos)
{
- char *charset = ICONVG(internal_encoding);
+ char *charset = get_internal_encoding(TSRMLS_C);
int charset_len = 0;
char *haystk;
int haystk_len;
@@ -2172,7 +2194,7 @@ PHP_FUNCTION(iconv_mime_encode)
smart_str retval = {0};
php_iconv_err_t err;
- const char *in_charset = ICONVG(internal_encoding);
+ const char *in_charset = get_internal_encoding(TSRMLS_C);
const char *out_charset = in_charset;
long line_len = 76;
const char *lfchars = "\r\n";
@@ -2285,7 +2307,7 @@ PHP_FUNCTION(iconv_mime_decode)
{
char *encoded_str;
int encoded_str_len;
- char *charset = ICONVG(internal_encoding);
+ char *charset = get_internal_encoding(TSRMLS_C);
int charset_len = 0;
long mode = 0;
@@ -2326,7 +2348,7 @@ PHP_FUNCTION(iconv_mime_decode_headers)
{
const char *encoded_str;
int encoded_str_len;
- char *charset = ICONVG(internal_encoding);
+ char *charset = get_internal_encoding(TSRMLS_C);
int charset_len = 0;
long mode = 0;
@@ -2494,15 +2516,15 @@ PHP_FUNCTION(iconv_get_encoding)
if (!strcasecmp("all", type)) {
array_init(return_value);
- add_assoc_string(return_value, "input_encoding", ICONVG(input_encoding), 1);
- add_assoc_string(return_value, "output_encoding", ICONVG(output_encoding), 1);
- add_assoc_string(return_value, "internal_encoding", ICONVG(internal_encoding), 1);
+ add_assoc_string(return_value, "input_encoding", get_input_encoding(TSRMLS_C), 1);
+ add_assoc_string(return_value, "output_encoding", get_output_encoding(TSRMLS_C), 1);
+ add_assoc_string(return_value, "internal_encoding", get_internal_encoding(TSRMLS_C), 1);
} else if (!strcasecmp("input_encoding", type)) {
- RETVAL_STRING(ICONVG(input_encoding), 1);
+ RETVAL_STRING(get_input_encoding(TSRMLS_C), 1);
} else if (!strcasecmp("output_encoding", type)) {
- RETVAL_STRING(ICONVG(output_encoding), 1);
+ RETVAL_STRING(get_output_encoding(TSRMLS_C), 1);
} else if (!strcasecmp("internal_encoding", type)) {
- RETVAL_STRING(ICONVG(internal_encoding), 1);
+ RETVAL_STRING(get_internal_encoding(TSRMLS_C), 1);
} else {
RETURN_FALSE;
}
diff --git a/ext/iconv/tests/iconv_default_charset.phpt b/ext/iconv/tests/iconv_default_charset.phpt
new file mode 100644
index 0000000000..ebfc7e6ee1
--- /dev/null
+++ b/ext/iconv/tests/iconv_default_charset.phpt
@@ -0,0 +1,77 @@
+--TEST--
+Test default_charset handling
+--SKIPIF--
+<?php
+extension_loaded('iconv') or die('skip');
+function_exists('iconv_get_encoding') or die("skip iconv_get_encoding() is not available in this build");
+?>
+--INI--
+error_reporting=E_ALL & ~E_DEPRECATED
+default_charset=UTF-8
+internal_encoding=
+input_encoding=
+output_encoding=
+iconv.internal_encoding=
+iconv.input_encoding=
+iconv.output_encoding=
+--FILE--
+<?php
+echo "*** Testing default_charset handling ***\n";
+
+echo "--- Get php.ini values ---\n";
+var_dump(ini_get('default_charset'),
+ ini_get('internal_encoding'),
+ ini_get('input_encoding'),
+ ini_get('output_encoding'),
+ ini_get('iconv.internal_encoding'),
+ ini_get('iconv.input_encoding'),
+ ini_get('iconv.output_encoding'));
+
+echo "\n--- Altering encodings ---\n";
+var_dump(ini_set('default_charset', 'ISO-8859-1'));
+
+echo "\n--- results of alterations ---\n";
+var_dump(ini_get('default_charset'),
+ ini_get('internal_encoding'),
+ ini_get('input_encoding'),
+ ini_get('output_encoding'),
+ ini_get('iconv.internal_encoding'),
+ ini_get('iconv.input_encoding'),
+ ini_get('iconv.output_encoding'));
+
+/*
+echo "\n--- Altering encodings ---\n";
+var_dump(ini_set('default_charset', 'ISO-8859-1'),
+ ini_set('internal_encoding'),
+ ini_set('input_encoding'),
+ ini_set('output_encoding'),
+ ini_set('iconv.internal_encoding'),
+ ini_set('iconv.input_encoding'),
+ ini_set('iconv.output_encoding'));
+*/
+
+echo "Done";
+?>
+--EXPECTF--
+*** Testing default_charset handling ***
+--- Get php.ini values ---
+string(5) "UTF-8"
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+
+--- Altering encodings ---
+string(5) "UTF-8"
+
+--- results of alterations ---
+string(10) "ISO-8859-1"
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+string(0) ""
+Done