@@ -3281,64 +3281,62 @@ PHP_FUNCTION(mb_convert_variables)
3281
3281
}
3282
3282
/* }}} */
3283
3283
3284
- /* {{{ HTML numeric entity */
3285
- /* {{{ static void php_mb_numericentity_exec() */
3286
- static void
3287
- php_mb_numericentity_exec (INTERNAL_FUNCTION_PARAMETERS , int type )
3284
+ /* HTML numeric entities */
3285
+
3286
+ /* Convert PHP array to data structure required by mbfl_html_numeric_entity */
3287
+ static int * make_conversion_map (HashTable * target_hash , int * convmap_size )
3288
+ {
3289
+ zval * hash_entry ;
3290
+
3291
+ int n_elems = zend_hash_num_elements (target_hash );
3292
+ if (n_elems % 4 != 0 ) {
3293
+ zend_argument_value_error (2 , "must have a multiple of 4 elements" );
3294
+ return NULL ;
3295
+ }
3296
+
3297
+ int * convmap = (int * )safe_emalloc (n_elems , sizeof (int ), 0 );
3298
+ int * mapelm = convmap ;
3299
+ int mapsize = 0 ;
3300
+
3301
+ ZEND_HASH_FOREACH_VAL (target_hash , hash_entry ) {
3302
+ * mapelm ++ = zval_get_long (hash_entry );
3303
+ mapsize ++ ;
3304
+ } ZEND_HASH_FOREACH_END ();
3305
+
3306
+ * convmap_size = mapsize / 4 ;
3307
+ return convmap ;
3308
+ }
3309
+
3310
+ /* {{{ Converts specified characters to HTML numeric entities */
3311
+ PHP_FUNCTION (mb_encode_numericentity )
3288
3312
{
3289
3313
char * str = NULL ;
3290
- size_t str_len ;
3291
3314
zend_string * encoding = NULL ;
3292
- zval * hash_entry ;
3315
+ int mapsize ;
3293
3316
HashTable * target_hash ;
3294
- int i , * convmap , * mapelm , mapsize = 0 ;
3295
3317
zend_bool is_hex = 0 ;
3296
3318
mbfl_string string , result , * ret ;
3297
3319
3298
- if (type == 0 ) {
3299
- ZEND_PARSE_PARAMETERS_START (2 , 4 )
3300
- Z_PARAM_STRING (str , str_len )
3301
- Z_PARAM_ARRAY_HT (target_hash )
3302
- Z_PARAM_OPTIONAL
3303
- Z_PARAM_STR (encoding )
3304
- Z_PARAM_BOOL (is_hex )
3305
- ZEND_PARSE_PARAMETERS_END ();
3306
- } else {
3307
- ZEND_PARSE_PARAMETERS_START (2 , 3 )
3308
- Z_PARAM_STRING (str , str_len )
3309
- Z_PARAM_ARRAY_HT (target_hash )
3310
- Z_PARAM_OPTIONAL
3311
- Z_PARAM_STR (encoding )
3312
- ZEND_PARSE_PARAMETERS_END ();
3313
- }
3320
+ ZEND_PARSE_PARAMETERS_START (2 , 4 )
3321
+ Z_PARAM_STRING (str , string .len )
3322
+ Z_PARAM_ARRAY_HT (target_hash )
3323
+ Z_PARAM_OPTIONAL
3324
+ Z_PARAM_STR (encoding )
3325
+ Z_PARAM_BOOL (is_hex )
3326
+ ZEND_PARSE_PARAMETERS_END ();
3314
3327
3315
3328
string .val = (unsigned char * )str ;
3316
- string .len = str_len ;
3317
3329
string .encoding = php_mb_get_encoding (encoding , 3 );
3318
3330
if (!string .encoding ) {
3319
3331
RETURN_THROWS ();
3320
3332
}
3321
3333
3322
- if (type == 0 && is_hex ) {
3323
- type = 2 ; /* output in hex format */
3324
- }
3325
-
3326
- /* conversion map */
3327
- i = zend_hash_num_elements (target_hash );
3328
- if (i % 4 != 0 ) {
3329
- zend_argument_value_error (2 , "must have a multiple of 4 elements" );
3334
+ int * convmap = make_conversion_map (target_hash , & mapsize );
3335
+ if (convmap == NULL ) {
3330
3336
RETURN_THROWS ();
3331
3337
}
3332
- convmap = (int * )safe_emalloc (i , sizeof (int ), 0 );
3333
- mapelm = convmap ;
3334
- mapsize = 0 ;
3335
- ZEND_HASH_FOREACH_VAL (target_hash , hash_entry ) {
3336
- * mapelm ++ = zval_get_long (hash_entry );
3337
- mapsize ++ ;
3338
- } ZEND_HASH_FOREACH_END ();
3339
- mapsize /= 4 ;
3340
3338
3341
- ret = mbfl_html_numeric_entity (& string , & result , convmap , mapsize , type );
3339
+ ret = mbfl_html_numeric_entity (& string , & result , convmap , mapsize , is_hex ? 2 : 0 );
3342
3340
ZEND_ASSERT (ret != NULL );
3343
3341
// TODO: avoid reallocation ???
3344
3342
RETVAL_STRINGL ((char * )ret -> val , ret -> len );
@@ -3347,20 +3345,41 @@ php_mb_numericentity_exec(INTERNAL_FUNCTION_PARAMETERS, int type)
3347
3345
}
3348
3346
/* }}} */
3349
3347
3350
- /* {{{ Converts specified characters to HTML numeric entities */
3351
- PHP_FUNCTION (mb_encode_numericentity )
3352
- {
3353
- php_mb_numericentity_exec (INTERNAL_FUNCTION_PARAM_PASSTHRU , 0 );
3354
- }
3355
- /* }}} */
3356
-
3357
3348
/* {{{ Converts HTML numeric entities to character code */
3358
3349
PHP_FUNCTION (mb_decode_numericentity )
3359
3350
{
3360
- php_mb_numericentity_exec (INTERNAL_FUNCTION_PARAM_PASSTHRU , 1 );
3351
+ char * str = NULL ;
3352
+ zend_string * encoding = NULL ;
3353
+ int mapsize ;
3354
+ HashTable * target_hash ;
3355
+ mbfl_string string , result , * ret ;
3356
+
3357
+ ZEND_PARSE_PARAMETERS_START (2 , 3 )
3358
+ Z_PARAM_STRING (str , string .len )
3359
+ Z_PARAM_ARRAY_HT (target_hash )
3360
+ Z_PARAM_OPTIONAL
3361
+ Z_PARAM_STR (encoding )
3362
+ ZEND_PARSE_PARAMETERS_END ();
3363
+
3364
+ string .val = (unsigned char * )str ;
3365
+ string .encoding = php_mb_get_encoding (encoding , 3 );
3366
+ if (!string .encoding ) {
3367
+ RETURN_THROWS ();
3368
+ }
3369
+
3370
+ int * convmap = make_conversion_map (target_hash , & mapsize );
3371
+ if (convmap == NULL ) {
3372
+ RETURN_THROWS ();
3373
+ }
3374
+
3375
+ ret = mbfl_html_numeric_entity (& string , & result , convmap , mapsize , 1 );
3376
+ ZEND_ASSERT (ret != NULL );
3377
+ // TODO: avoid reallocation ???
3378
+ RETVAL_STRINGL ((char * )ret -> val , ret -> len );
3379
+ efree (ret -> val );
3380
+ efree ((void * )convmap );
3361
3381
}
3362
3382
/* }}} */
3363
- /* }}} */
3364
3383
3365
3384
/* {{{ Sends an email message with MIME scheme */
3366
3385
0 commit comments