Skip to content

Patch for bug #61660 #42

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Next Next commit
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.
  • Loading branch information
krtek4 committed Apr 8, 2012
commit b8ba1cb23590afd6d44c376481aef371b9c87457
10 changes: 8 additions & 2 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,23 @@ 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;
size_t target_length = (oldlen + 1) >> 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 ? oldlen + 1: 0;
for (i = 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 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;
Expand Down