scottmac Fri, 03 Jun 2011 00:12:22 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=311743
Log: Added fabled hex2bin() function Changed paths: U php/php-src/branches/PHP_5_4/NEWS U php/php-src/branches/PHP_5_4/UPGRADING U php/php-src/branches/PHP_5_4/ext/standard/basic_functions.c U php/php-src/branches/PHP_5_4/ext/standard/php_string.h U php/php-src/branches/PHP_5_4/ext/standard/string.c U php/php-src/trunk/ext/standard/basic_functions.c U php/php-src/trunk/ext/standard/php_string.h U php/php-src/trunk/ext/standard/string.c Modified: php/php-src/branches/PHP_5_4/NEWS =================================================================== --- php/php-src/branches/PHP_5_4/NEWS 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/branches/PHP_5_4/NEWS 2011-06-03 00:12:22 UTC (rev 311743) @@ -130,6 +130,7 @@ - Improved core functions: . number_format() no longer truncates multibyte decimal points and thousand separators to the first byte. FR #53457. (Adam) + . Added hex2bin() function. (Scott) - Improved CURL extension: . Added support for CURLOPT_MAX_RECV_SPEED_LARGE and Modified: php/php-src/branches/PHP_5_4/UPGRADING =================================================================== --- php/php-src/branches/PHP_5_4/UPGRADING 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/branches/PHP_5_4/UPGRADING 2011-06-03 00:12:22 UTC (rev 311743) @@ -358,10 +358,11 @@ - Core: - get_declared_traits() + - hex2bin() - http_response_code() - - trait_exists() - stream_set_chunk_size() - socket_import_stream() + - trait_exists() - LDAP: - ldap_control_paged_results() Modified: php/php-src/branches/PHP_5_4/ext/standard/basic_functions.c =================================================================== --- php/php-src/branches/PHP_5_4/ext/standard/basic_functions.c 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/branches/PHP_5_4/ext/standard/basic_functions.c 2011-06-03 00:12:22 UTC (rev 311743) @@ -2142,6 +2142,10 @@ ZEND_ARG_INFO(0, data) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_hex2bin, 0) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_strspn, 0, 0, 2) ZEND_ARG_INFO(0, str) ZEND_ARG_INFO(0, mask) @@ -2682,6 +2686,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(constant, arginfo_constant) PHP_FE(bin2hex, arginfo_bin2hex) + PHP_FE(hex2bin, arginfo_hex2bin) PHP_FE(sleep, arginfo_sleep) PHP_FE(usleep, arginfo_usleep) #if HAVE_NANOSLEEP Modified: php/php-src/branches/PHP_5_4/ext/standard/php_string.h =================================================================== --- php/php-src/branches/PHP_5_4/ext/standard/php_string.h 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/branches/PHP_5_4/ext/standard/php_string.h 2011-06-03 00:12:22 UTC (rev 311743) @@ -78,6 +78,7 @@ PHP_FUNCTION(parse_str); PHP_FUNCTION(str_getcsv); PHP_FUNCTION(bin2hex); +PHP_FUNCTION(hex2bin); PHP_FUNCTION(similar_text); PHP_FUNCTION(strip_tags); PHP_FUNCTION(str_repeat); Modified: php/php-src/branches/PHP_5_4/ext/standard/string.c =================================================================== --- php/php-src/branches/PHP_5_4/ext/standard/string.c 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/branches/PHP_5_4/ext/standard/string.c 2011-06-03 00:12:22 UTC (rev 311743) @@ -146,6 +146,46 @@ } /* }}} */ +/* {{{ php_hex2bin + */ +static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *newlen) +{ + size_t target_length = oldlen >> 1; + register unsigned char *str = (unsigned char *)safe_emalloc(target_length, sizeof(char), 1); + size_t i, j; + for (i = j = 0; i < target_length; i++) { + char c = old[j++]; + if (c >= '0' && c <= '9') { + str[i] = (c - '0') << 4; + } else if (c >= 'a' && c <= 'f') { + str[i] = (c - 'a' + 10) << 4; + } else if (c >= 'A' && c <= 'F') { + str[i] = (c - 'A' + 10) << 4; + } else { + efree(str); + return NULL; + } + c = old[j++]; + if (c >= '0' && c <= '9') { + str[i] |= c - '0'; + } else if (c >= 'a' && c <= 'f') { + str[i] |= c - 'a' + 10; + } else if (c >= 'A' && c <= 'F') { + str[i] |= c - 'A' + 10; + } else { + efree(str); + return NULL; + } + } + str[target_length] = '\0'; + + if (newlen) + *newlen = target_length; + + return (char *)str; +} +/* }}} */ + #ifdef HAVE_LOCALECONV /* {{{ localeconv_r * glibc's localeconv is not reentrant, so lets make it so ... sorta */ @@ -214,6 +254,28 @@ } /* }}} */ +/* {{{ proto string hex2bin(string data) + Converts the hex representation of data to binary */ +PHP_FUNCTION(hex2bin) +{ + char *result, *data; + size_t newlen; + int datalen; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) { + return; + } + + result = php_hex2bin((unsigned char *)data, datalen, &newlen); + + if (!result) { + RETURN_FALSE; + } + + RETURN_STRINGL(result, newlen, 0); +} +/* }}} */ + static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */ { char *s11, *s22; Modified: php/php-src/trunk/ext/standard/basic_functions.c =================================================================== --- php/php-src/trunk/ext/standard/basic_functions.c 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/trunk/ext/standard/basic_functions.c 2011-06-03 00:12:22 UTC (rev 311743) @@ -2142,6 +2142,10 @@ ZEND_ARG_INFO(0, data) ZEND_END_ARG_INFO() +ZEND_BEGIN_ARG_INFO(arginfo_hex2bin, 0) + ZEND_ARG_INFO(0, data) +ZEND_END_ARG_INFO() + ZEND_BEGIN_ARG_INFO_EX(arginfo_strspn, 0, 0, 2) ZEND_ARG_INFO(0, str) ZEND_ARG_INFO(0, mask) @@ -2682,6 +2686,7 @@ const zend_function_entry basic_functions[] = { /* {{{ */ PHP_FE(constant, arginfo_constant) PHP_FE(bin2hex, arginfo_bin2hex) + PHP_FE(hex2bin, arginfo_hex2bin) PHP_FE(sleep, arginfo_sleep) PHP_FE(usleep, arginfo_usleep) #if HAVE_NANOSLEEP Modified: php/php-src/trunk/ext/standard/php_string.h =================================================================== --- php/php-src/trunk/ext/standard/php_string.h 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/trunk/ext/standard/php_string.h 2011-06-03 00:12:22 UTC (rev 311743) @@ -78,6 +78,7 @@ PHP_FUNCTION(parse_str); PHP_FUNCTION(str_getcsv); PHP_FUNCTION(bin2hex); +PHP_FUNCTION(hex2bin); PHP_FUNCTION(similar_text); PHP_FUNCTION(strip_tags); PHP_FUNCTION(str_repeat); Modified: php/php-src/trunk/ext/standard/string.c =================================================================== --- php/php-src/trunk/ext/standard/string.c 2011-06-02 22:54:14 UTC (rev 311742) +++ php/php-src/trunk/ext/standard/string.c 2011-06-03 00:12:22 UTC (rev 311743) @@ -146,6 +146,46 @@ } /* }}} */ +/* {{{ php_hex2bin + */ +static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *newlen) +{ + size_t target_length = oldlen >> 1; + register unsigned char *str = (unsigned char *)safe_emalloc(target_length, sizeof(char), 1); + size_t i, j; + for (i = j = 0; i < target_length; i++) { + char c = old[j++]; + if (c >= '0' && c <= '9') { + str[i] = (c - '0') << 4; + } else if (c >= 'a' && c <= 'f') { + str[i] = (c - 'a' + 10) << 4; + } else if (c >= 'A' && c <= 'F') { + str[i] = (c - 'A' + 10) << 4; + } else { + efree(str); + return NULL; + } + c = old[j++]; + if (c >= '0' && c <= '9') { + str[i] |= c - '0'; + } else if (c >= 'a' && c <= 'f') { + str[i] |= c - 'a' + 10; + } else if (c >= 'A' && c <= 'F') { + str[i] |= c - 'A' + 10; + } else { + efree(str); + return NULL; + } + } + str[target_length] = '\0'; + + if (newlen) + *newlen = target_length; + + return (char *)str; +} +/* }}} */ + #ifdef HAVE_LOCALECONV /* {{{ localeconv_r * glibc's localeconv is not reentrant, so lets make it so ... sorta */ @@ -214,6 +254,28 @@ } /* }}} */ +/* {{{ proto string hex2bin(string data) + Converts the hex representation of data to binary */ +PHP_FUNCTION(hex2bin) +{ + char *result, *data; + size_t newlen; + int datalen; + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) { + return; + } + + result = php_hex2bin((unsigned char *)data, datalen, &newlen); + + if (!result) { + RETURN_FALSE; + } + + RETURN_STRINGL(result, newlen, 0); +} +/* }}} */ + static void php_spn_common_handler(INTERNAL_FUNCTION_PARAMETERS, int behavior) /* {{{ */ { char *s11, *s22;
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php