Skip to content

Commit 8d13348

Browse files
committed
Separate implementation of mb_{en,de}code_numericentity
Rather than using a magic boolean parameter to choose different behavior of the subfunction, inline it. The code size doesn't really grow anyways. And soon these will be trimmed down more.
1 parent 29b02bf commit 8d13348

File tree

1 file changed

+69
-50
lines changed

1 file changed

+69
-50
lines changed

ext/mbstring/mbstring.c

Lines changed: 69 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,64 +3281,62 @@ PHP_FUNCTION(mb_convert_variables)
32813281
}
32823282
/* }}} */
32833283

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)
32883312
{
32893313
char *str = NULL;
3290-
size_t str_len;
32913314
zend_string *encoding = NULL;
3292-
zval *hash_entry;
3315+
int mapsize;
32933316
HashTable *target_hash;
3294-
int i, *convmap, *mapelm, mapsize=0;
32953317
zend_bool is_hex = 0;
32963318
mbfl_string string, result, *ret;
32973319

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();
33143327

33153328
string.val = (unsigned char *)str;
3316-
string.len = str_len;
33173329
string.encoding = php_mb_get_encoding(encoding, 3);
33183330
if (!string.encoding) {
33193331
RETURN_THROWS();
33203332
}
33213333

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) {
33303336
RETURN_THROWS();
33313337
}
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;
33403338

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);
33423340
ZEND_ASSERT(ret != NULL);
33433341
// TODO: avoid reallocation ???
33443342
RETVAL_STRINGL((char *)ret->val, ret->len);
@@ -3347,20 +3345,41 @@ php_mb_numericentity_exec(INTERNAL_FUNCTION_PARAMETERS, int type)
33473345
}
33483346
/* }}} */
33493347

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-
33573348
/* {{{ Converts HTML numeric entities to character code */
33583349
PHP_FUNCTION(mb_decode_numericentity)
33593350
{
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);
33613381
}
33623382
/* }}} */
3363-
/* }}} */
33643383

33653384
/* {{{ Sends an email message with MIME scheme */
33663385

0 commit comments

Comments
 (0)