From 4e3ba6bd7883ba7f7c5b7e402b46e6b93753a756 Mon Sep 17 00:00:00 2001 From: Gilles Meier Date: Sun, 8 Apr 2012 13:15:56 +0300 Subject: [PATCH 1/2] Patch for bug #61660 When the hexadecimal string has an odd length, the resulting binary data must be left padded with 0s in order to be aligned on 8 bits. To avoid extraneous if test on each iteration, we distinguish the special case by pointing the source data iterator the the NUL bytes at the end which we can then detect and act accordingly. --- ext/standard/string.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index 0aade780856b9..d26f0461570d9 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -13,7 +13,7 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf | - | Stig Sæther Bakken | + | Stig S�ther Bakken | | Zeev Suraski | +----------------------------------------------------------------------+ */ @@ -153,7 +153,9 @@ static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t * 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++) { + // if we have an odd length, point to the end of the string to distinguish the special case + j = oldlen & 1 == 0 ? 0 : oldlen + 1; + for (i = 0; i < target_length; i++) { char c = old[j++]; if (c >= '0' && c <= '9') { str[i] = (c - '0') << 4; @@ -161,7 +163,11 @@ static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t * str[i] = (c - 'a' + 10) << 4; } else if (c >= 'A' && c <= 'F') { str[i] = (c - 'A' + 10) << 4; - } else { + } else if(c == '\0') { + // odd length, put the first 4 bits to 0 and restart at the beginning of the string + str[i] = 0; + j = 0; + } else { efree(str); return NULL; } From c2d096507869e3550fd11e75a1959c3282b17323 Mon Sep 17 00:00:00 2001 From: Gilles Meier Date: Sun, 8 Apr 2012 14:08:10 +0200 Subject: [PATCH 2/2] changed the comment style and reverted the encoding problem --- ext/standard/string.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/standard/string.c b/ext/standard/string.c index d26f0461570d9..aeb863866a6a9 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -13,7 +13,7 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Rasmus Lerdorf | - | Stig S�ther Bakken | + | Stig Sæther Bakken | | Zeev Suraski | +----------------------------------------------------------------------+ */ @@ -153,7 +153,7 @@ static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t * size_t target_length = oldlen >> 1; register unsigned char *str = (unsigned char *)safe_emalloc(target_length, sizeof(char), 1); size_t i, j; - // if we have an odd length, point to the end of the string to distinguish the special case + /* if we have an odd length, point to the end of the string to distinguish the special case */ j = oldlen & 1 == 0 ? 0 : oldlen + 1; for (i = 0; i < target_length; i++) { char c = old[j++]; @@ -164,7 +164,7 @@ static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t * } else if (c >= 'A' && c <= 'F') { str[i] = (c - 'A' + 10) << 4; } else if(c == '\0') { - // odd length, put the first 4 bits to 0 and restart at the beginning of the string + /* odd length, put the first 4 bits to 0 and restart at the beginning of the string */ str[i] = 0; j = 0; } else {