add odd numbers for hex_decode

This commit is contained in:
Evan Burkey 2024-03-28 18:32:59 -07:00
parent cf6d3ce892
commit c795f607ef
2 changed files with 16 additions and 1 deletions

View File

@ -209,7 +209,12 @@ unsigned char *hex_decode(const char *orig, size_t *sz) {
}
char *buf = malloc(sizeof(char) * buf_sz);
strcpy(buf, orig);
if (strlen(orig) % 2 != 0) {
strcpy(buf + 1, orig);
buf[0] = '0';
} else {
strcpy(buf, orig);
}
buf[buf_sz - 1] = '\0';
*sz = buf_sz / 2;

View File

@ -335,6 +335,16 @@ void test_crypto() {
assert(s[i] == h[i]);
}
free(s);
// Odd number of characters
s = hex_decode("f00f5", &s_sz);
unsigned char h2[4] = {
0x0F, 0x00, 0xF5
};
for (size_t i = 0; i < 3; ++i) {
assert(s[i] == h2[i]);
}
free(s);
}
#if defined(__APPLE__) || defined(__MACH__)