Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug #61660 bin2hex(hex2bin($data)) != $data #51

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/standard/php_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@ PHPAPI char *php_strerror(int errnum);
# endif
#endif

#define PHP_HEX2BIN_PARTIAL_OUTPUT_ON_ERROR 1<<1

void register_string_constants(INIT_FUNC_ARGS);

#endif /* PHP_STRING_H */
15 changes: 8 additions & 7 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ void register_string_constants(INIT_FUNC_ARGS)
REGISTER_LONG_CONSTANT("PATHINFO_BASENAME", PHP_PATHINFO_BASENAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PATHINFO_EXTENSION", PHP_PATHINFO_EXTENSION, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("PATHINFO_FILENAME", PHP_PATHINFO_FILENAME, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("HEX2BIN_PARTIAL_OUTPUT_ON_ERROR", PHP_HEX2BIN_PARTIAL_OUTPUT_ON_ERROR, CONST_CS | CONST_PERSISTENT);

#ifdef HAVE_LOCALECONV
/* If last members of struct lconv equal CHAR_MAX, no grouping is done */
Expand Down Expand Up @@ -148,7 +149,7 @@ static char *php_bin2hex(const unsigned char *old, const size_t oldlen, size_t *

/* {{{ php_hex2bin
*/
static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *newlen)
static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *newlen, int options TSRMLS_DC)
{
size_t target_length = oldlen >> 1;
register unsigned char *str = (unsigned char *)safe_emalloc(target_length, sizeof(char), 1);
Expand Down Expand Up @@ -254,24 +255,24 @@ PHP_FUNCTION(bin2hex)
}
/* }}} */

/* {{{ proto string hex2bin(string data)
/* {{{ proto string hex2bin(string data, [int options])
Converts the hex representation of data to binary */
PHP_FUNCTION(hex2bin)
{
char *result, *data;
size_t newlen;
int datalen;
int datalen, options = 0;

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &data, &datalen) == FAILURE) {
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &data, &datalen, &options) == FAILURE) {
return;
}

if (datalen % 2 != 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Hexadecimal input string must have an even length");
if ((datalen % 2 != 0 || datalen == 0) && options ^ PHP_HEX2BIN_PARTIAL_OUTPUT_ON_ERROR) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Hexadecimal input string must have an even length and cannot be empty");
RETURN_FALSE;
}

result = php_hex2bin((unsigned char *)data, datalen, &newlen);
result = php_hex2bin((unsigned char *)data, datalen, &newlen, options TSRMLS_CC);

if (!result) {
RETURN_FALSE;
Expand Down
4 changes: 3 additions & 1 deletion ext/standard/tests/strings/bug61660.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ Bug #61660: bin2hex(hex2bin($data)) != $data
<?php

var_dump(hex2bin('123'));
var_dump(hex2bin('123', HEX2BIN_PARTIAL_OUTPUT_ON_ERROR));

?>
--EXPECTF--
Warning: hex2bin(): Hexadecimal input string must have an even length in %s on line %d
Warning: hex2bin(): Hexadecimal input string must have an even length and cannot be empty in %s on line %d
bool(false)
string(1) ""