@@ -2691,26 +2691,16 @@ MBSTRING_API char *php_mb_convert_encoding_ex(const char *input, size_t length,
2691
2691
/* }}} */
2692
2692
2693
2693
/* {{{ MBSTRING_API char *php_mb_convert_encoding() */
2694
- MBSTRING_API char * php_mb_convert_encoding (const char * input , size_t length , const char * _to_encoding , const char * _from_encodings , size_t * output_len )
2694
+ MBSTRING_API char * php_mb_convert_encoding (const char * input , size_t length , const mbfl_encoding * to_encoding , const char * _from_encodings , size_t * output_len )
2695
2695
{
2696
- const mbfl_encoding * from_encoding , * to_encoding ;
2696
+ const mbfl_encoding * from_encoding ;
2697
2697
2698
2698
if (output_len ) {
2699
2699
* output_len = 0 ;
2700
2700
}
2701
2701
if (!input ) {
2702
2702
return NULL ;
2703
2703
}
2704
- /* new encoding */
2705
- if (_to_encoding && strlen (_to_encoding )) {
2706
- to_encoding = mbfl_name2encoding (_to_encoding );
2707
- if (!to_encoding ) {
2708
- php_error_docref (NULL , E_WARNING , "Unknown encoding \"%s\"" , _to_encoding );
2709
- return NULL ;
2710
- }
2711
- } else {
2712
- to_encoding = MBSTRG (current_internal_encoding );
2713
- }
2714
2704
2715
2705
/* pre-conversion encoding */
2716
2706
from_encoding = MBSTRG (current_internal_encoding );
@@ -2743,7 +2733,7 @@ MBSTRING_API char *php_mb_convert_encoding(const char *input, size_t length, con
2743
2733
}
2744
2734
/* }}} */
2745
2735
2746
- MBSTRING_API HashTable * php_mb_convert_encoding_recursive (HashTable * input , const char * _to_encoding , const char * _from_encodings )
2736
+ MBSTRING_API HashTable * php_mb_convert_encoding_recursive (HashTable * input , const mbfl_encoding * to_encoding , const char * _from_encodings )
2747
2737
{
2748
2738
HashTable * output , * chash ;
2749
2739
zend_long idx ;
@@ -2766,15 +2756,15 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
2766
2756
ZEND_HASH_FOREACH_KEY_VAL (input , idx , key , entry ) {
2767
2757
/* convert key */
2768
2758
if (key ) {
2769
- ckey = php_mb_convert_encoding (ZSTR_VAL (key ), ZSTR_LEN (key ), _to_encoding , _from_encodings , & ckey_len );
2759
+ ckey = php_mb_convert_encoding (ZSTR_VAL (key ), ZSTR_LEN (key ), to_encoding , _from_encodings , & ckey_len );
2770
2760
key = zend_string_init (ckey , ckey_len , 0 );
2771
2761
efree (ckey );
2772
2762
}
2773
2763
/* convert value */
2774
2764
ZEND_ASSERT (entry );
2775
2765
switch (Z_TYPE_P (entry )) {
2776
2766
case IS_STRING :
2777
- cval = php_mb_convert_encoding (Z_STRVAL_P (entry ), Z_STRLEN_P (entry ), _to_encoding , _from_encodings , & cval_len );
2767
+ cval = php_mb_convert_encoding (Z_STRVAL_P (entry ), Z_STRLEN_P (entry ), to_encoding , _from_encodings , & cval_len );
2778
2768
ZVAL_STRINGL (& entry_tmp , cval , cval_len );
2779
2769
efree (cval );
2780
2770
break ;
@@ -2786,7 +2776,7 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
2786
2776
ZVAL_COPY (& entry_tmp , entry );
2787
2777
break ;
2788
2778
case IS_ARRAY :
2789
- chash = php_mb_convert_encoding_recursive (Z_ARRVAL_P (entry ), _to_encoding , _from_encodings );
2779
+ chash = php_mb_convert_encoding_recursive (Z_ARRVAL_P (entry ), to_encoding , _from_encodings );
2790
2780
if (chash ) {
2791
2781
ZVAL_ARR (& entry_tmp , chash );
2792
2782
} else {
@@ -2820,19 +2810,24 @@ MBSTRING_API HashTable *php_mb_convert_encoding_recursive(HashTable *input, cons
2820
2810
PHP_FUNCTION (mb_convert_encoding )
2821
2811
{
2822
2812
zval * input ;
2823
- char * arg_new ;
2824
- size_t new_len ;
2825
2813
zval * arg_old = NULL ;
2826
2814
size_t size , l , n ;
2827
2815
char * _from_encodings = NULL , * ret , * s_free = NULL ;
2816
+ zend_string * to_encoding_name ;
2817
+ const mbfl_encoding * to_encoding ;
2828
2818
2829
2819
zval * hash_entry ;
2830
2820
HashTable * target_hash ;
2831
2821
2832
- if (zend_parse_parameters (ZEND_NUM_ARGS (), "zs |z" , & input , & arg_new , & new_len , & arg_old ) == FAILURE ) {
2822
+ if (zend_parse_parameters (ZEND_NUM_ARGS (), "zS |z" , & input , & to_encoding_name , & arg_old ) == FAILURE ) {
2833
2823
RETURN_THROWS ();
2834
2824
}
2835
2825
2826
+ to_encoding = php_mb_get_encoding (to_encoding_name );
2827
+ if (!to_encoding ) {
2828
+ RETURN_FALSE ;
2829
+ }
2830
+
2836
2831
if (Z_TYPE_P (input ) != IS_STRING && Z_TYPE_P (input ) != IS_ARRAY ) {
2837
2832
if (!try_convert_to_string (input )) {
2838
2833
RETURN_THROWS ();
@@ -2884,7 +2879,7 @@ PHP_FUNCTION(mb_convert_encoding)
2884
2879
2885
2880
if (Z_TYPE_P (input ) == IS_STRING ) {
2886
2881
/* new encoding */
2887
- ret = php_mb_convert_encoding (Z_STRVAL_P (input ), Z_STRLEN_P (input ), arg_new , _from_encodings , & size );
2882
+ ret = php_mb_convert_encoding (Z_STRVAL_P (input ), Z_STRLEN_P (input ), to_encoding , _from_encodings , & size );
2888
2883
if (ret != NULL ) {
2889
2884
// TODO: avoid reallocation ???
2890
2885
RETVAL_STRINGL (ret , size ); /* the string is already strdup()'ed */
@@ -2897,7 +2892,7 @@ PHP_FUNCTION(mb_convert_encoding)
2897
2892
}
2898
2893
} else {
2899
2894
HashTable * tmp ;
2900
- tmp = php_mb_convert_encoding_recursive (Z_ARRVAL_P (input ), arg_new , _from_encodings );
2895
+ tmp = php_mb_convert_encoding_recursive (Z_ARRVAL_P (input ), to_encoding , _from_encodings );
2901
2896
RETURN_ARR (tmp );
2902
2897
}
2903
2898
0 commit comments