Fix hex_to_bytes

This commit is contained in:
Nekotekina 2017-06-18 23:47:24 +03:00
parent 907c800e87
commit 9cc52c75e3
1 changed files with 8 additions and 14 deletions

View File

@ -52,28 +52,22 @@ u64 hex_to_u64(const char* hex_str)
return result; return result;
} }
void hex_to_bytes(unsigned char *data, const char *hex_str, unsigned int str_length) void hex_to_bytes(unsigned char* data, const char* hex_str, unsigned int str_length)
{ {
u32 strn_length = (str_length > 0) ? str_length : (u32) strlen(hex_str); u32 strn_length = (str_length > 0) ? str_length : (u32)std::strlen(hex_str);
u32 data_length = strn_length / 2; u32 data_length = strn_length / 2;
char tmp_buf[3] = {0, 0, 0}; char tmp_buf[3] = {0, 0, 0};
// Don't convert if the string length is odd. // Don't convert if the string length is odd.
if (!(strn_length % 2)) if ((strn_length % 2) == 0)
{ {
auto out = std::make_unique<u8[]>(strn_length * sizeof(u8)); while (data_length--)
u8 *pos = out.get();
while (strn_length--)
{ {
tmp_buf[0] = *hex_str++; tmp_buf[0] = *hex_str++;
tmp_buf[1] = *hex_str++; tmp_buf[1] = *hex_str++;
*pos++ = (u8)(hex_to_u64(tmp_buf) & 0xFF); *data++ = (u8)(hex_to_u64(tmp_buf) & 0xFF);
} }
// Copy back to our array.
memcpy(data, out.get(), data_length);
} }
} }