From 91619d9e1d0cba3ea4e1e5b6cb061d3b57d556b3 Mon Sep 17 00:00:00 2001 From: Andrei Zmievski Date: Thu, 30 Nov 2006 21:18:22 +0000 Subject: [PATCH] Mark pack()/unpack() as Unicode-safe. They will accept Unicode strings but convert them to binary. We can revisit supporting something like 'U' format in the future, if it's needed. --- ext/standard/pack.c | 41 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 31 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index 747037e1e5..43008586ef 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -98,7 +98,7 @@ static void php_pack(zval **val, int size, int *map, char *output) /* pack() idea stolen from Perl (implemented formats behave the same as there) * Implemented formats are A, a, h, H, c, C, s, S, i, I, l, L, n, N, f, d, x, X, @. */ -/* {{{ proto string pack(string format, mixed arg1 [, mixed arg2 [, mixed ...]]) +/* {{{ proto string pack(string format, mixed arg1 [, mixed arg2 [, mixed ...]]) U Takes one or more arguments and packs them into a binary string according to the format argument */ PHP_FUNCTION(pack) { @@ -113,27 +113,15 @@ PHP_FUNCTION(pack) int outputpos = 0, outputsize = 0; char *output; - argc = ZEND_NUM_ARGS(); - - if (argc < 1) { - WRONG_PARAM_COUNT; - } - - argv = safe_emalloc(argc, sizeof(zval **), 0); - - if (zend_get_parameters_array_ex(argc, argv) == FAILURE) { - efree(argv); - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&*", &format, + &formatlen, UG(ascii_conv), &argv, &argc) == FAILURE) { + return; } - convert_to_string_ex(argv[0]); - format = Z_STRVAL_PP(argv[0]); - formatlen = Z_STRLEN_PP(argv[0]); - /* We have a maximum of format codes to deal with */ formatcodes = safe_emalloc(formatlen, sizeof(*formatcodes), 0); formatargs = safe_emalloc(formatlen, sizeof(*formatargs), 0); - currentarg = 1; + currentarg = 0; /* Preprocess format into formatcodes and formatargs */ for (i = 0; i < formatlen; formatcount++) { @@ -303,7 +291,7 @@ PHP_FUNCTION(pack) output = emalloc(outputsize + 1); outputpos = 0; - currentarg = 1; + currentarg = 0; /* Do actual packing */ for (i = 0; i < formatcount; i++) { @@ -506,30 +494,21 @@ static long php_unpack(char *data, int size, int issigned, int *map) * f and d will return doubles. * Implemented formats are A, a, h, H, c, C, s, S, i, I, l, L, n, N, f, d, x, X, @. */ -/* {{{ proto array unpack(string format, string input) +/* {{{ proto array unpack(string format, string input) U Unpack binary string into named array elements according to format argument */ PHP_FUNCTION(unpack) { - zval **formatarg; - zval **inputarg; char *format; char *input; int formatlen; int inputpos, inputlen; int i; - if (ZEND_NUM_ARGS() != 2 || - zend_get_parameters_ex(2, &formatarg, &inputarg) == FAILURE) { - WRONG_PARAM_COUNT; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s&s", &format, + &formatlen, UG(ascii_conv), &input, &inputlen) == FAILURE) { + return; } - convert_to_string_ex(formatarg); - convert_to_string_ex(inputarg); - - format = Z_STRVAL_PP(formatarg); - formatlen = Z_STRLEN_PP(formatarg); - input = Z_STRVAL_PP(inputarg); - inputlen = Z_STRLEN_PP(inputarg); inputpos = 0; array_init(return_value); -- 2.40.0