php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #61660
Patch hex2bin_padd_odd_new_patch revision 2012-04-08 09:18 UTC by theanomaly dot is at gmail dot com
Patch hex2bin_padd_odd revision 2012-04-08 04:33 UTC by theanomaly dot is at gmail dot com
Patch hex2bin_pad_odd_length.patch revision 2012-04-07 17:02 UTC by krtek4+php at gmail dot com

Patch hex2bin_padd_odd_new_patch for *General Issues Bug #61660

Patch version 2012-04-08 09:18 UTC

Return to Bug #61660 | Download this patch
This patch renders other patches obsolete

Obsolete patches:

Patch Revisions:

Developer: theanomaly.is@gmail.com

diff --git a/ext/standard/string.c b/ext/standard/string.c
index 0aade78..a42e91a 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -151,32 +151,40 @@ static char *php_bin2hex(const unsigned char *old, const siz
 static char *php_hex2bin(const unsigned char *old, const size_t oldlen, size_t *n
 {
        size_t target_length = oldlen >> 1;
-       register unsigned char *str = (unsigned char *)safe_emalloc(target_length,
-       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') {
:
diff --git a/ext/standard/string.c b/ext/standard/string.c
index 0aade78..a42e91a 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -151,32 +151,40 @@ static char *php_bin2hex(const unsigned char *old, const size_t oldlen, size_t *
 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;
+       if (oldlen % 2) {
+               target_length++;
+       }
+       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 = '0';
+               if (j == 0 && oldlen % 2) {
+                       c = '0';
                } else {
:

diff --git a/ext/standard/string.c b/ext/standard/string.c
index 0aade78..a42e91a 100644
--- a/ext/standard/string.c
+++ b/ext/standard/string.c
@@ -151,32 +151,40 @@ static char *php_bin2hex(const unsigned char *old, const size_t oldlen, size_t *
 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;
+       if (oldlen % 2) {
+               target_length++;
+       }
+       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 = '0';
+               if (j == 0 && oldlen % 2) {
+                       c = '0';
                } else {
-                       efree(str);
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 16:01:29 2024 UTC