Skip to content

Commit

Permalink
Merge pull request teserakt-io#17 from teserakt-io/fix-and-simplify-h…
Browse files Browse the repository at this point in the history
…ex-decode

src/e4util: simplify hex_decode
  • Loading branch information
diagprov authored Jan 8, 2020
2 parents f2f821a + 09686e6 commit aec561c
Showing 1 changed file with 17 additions and 34 deletions.
51 changes: 17 additions & 34 deletions src/e4util.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,54 +100,37 @@ INLINE int8_t from_hex(const char c) {
}



int e4c_hex_decode(char *bytes, const size_t byteslen, const char *hexstring, const size_t hexstringlen)
int e4c_hex_decode(char *dst, const size_t dst_len, const char *encoded, const size_t encoded_len)
{

size_t reqbytes = 0;
int i = 0;
char *walk;
int i;
size_t decoded_len;
int8_t h1, h2;

/* can't decode empty string; can't decode odd bytes, */
if (hexstringlen == 0 || hexstringlen % 2 == 1)
if (encoded_len == 0 || encoded_len % 2 != 0)
{
printf("length condition failed.\n");
printf("e4c_hex_decode: encoded_len=%ld but must be non-zero and even.\n", encoded_len);
return 0;
}
reqbytes = hexstringlen >> 1;

if (reqbytes > byteslen)
decoded_len = encoded_len >> 1;
if (decoded_len > dst_len)
{
printf("reqbytes is wrong. r=%ld, hl=%ld, bl=%ld\n", reqbytes, hexstringlen, byteslen);
printf("e4c_hex_decode: decoded_len is wrong. r=%ld, hl=%ld, bl=%ld\n",
decoded_len, encoded_len, dst_len);
return 0;
}

for (i = 0; i < reqbytes; i++)
walk = dst;
for (i = 0; i < encoded_len; i += 2)
{

unsigned char byte = 0;
char hbits = hexstring[2 * i];
char lbits = hexstring[2 * i + 1];
int8_t val = -1;

val = from_hex(hbits);
if (val < 0)
{
if ((h1 = from_hex(encoded[i])) < 0)
return 0;
}

byte |= (uint8_t)(val) << 4;
val = -1;

val = from_hex(lbits);
if (val < 0)
{
if ((h2 = from_hex(encoded[i+1])) < 0)
return 0;
}

byte |= (uint8_t)(val);

memcpy(bytes + i, &byte, 1);
*walk++ = (char)(h1<<4 | h2);
}

return (int)reqbytes;
return i;
}

0 comments on commit aec561c

Please sign in to comment.