util: add functions for printing and parsing hexadecimal data
This commit is contained in:
parent
1e727c4497
commit
e43d699973
4 changed files with 63 additions and 14 deletions
16
keys.c
16
keys.c
|
@ -159,25 +159,13 @@ determine_hash_delay(uint32_t key_id)
|
|||
static int
|
||||
decode_key(char *key)
|
||||
{
|
||||
int i, j, len = strlen(key);
|
||||
char buf[3], *p;
|
||||
int len = strlen(key);
|
||||
|
||||
if (!strncmp(key, "ASCII:", 6)) {
|
||||
memmove(key, key + 6, len - 6);
|
||||
return len - 6;
|
||||
} else if (!strncmp(key, "HEX:", 4)) {
|
||||
if ((len - 4) % 2)
|
||||
return 0;
|
||||
|
||||
for (i = 0, j = 4; j + 1 < len; i++, j += 2) {
|
||||
buf[0] = key[j], buf[1] = key[j + 1], buf[2] = '\0';
|
||||
key[i] = strtol(buf, &p, 16);
|
||||
|
||||
if (p != buf + 2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return i;
|
||||
return UTI_HexToBytes(key + 4, key, len);
|
||||
} else {
|
||||
/* assume ASCII */
|
||||
return len;
|
||||
|
|
|
@ -318,4 +318,19 @@ void test_unit(void) {
|
|||
TEST_CHECK(UTI_RemoveFile(NULL, "testfile", NULL));
|
||||
TEST_CHECK(stat("testfile", &st) < 0);
|
||||
TEST_CHECK(!UTI_RemoveFile(NULL, "testfile", NULL));
|
||||
|
||||
assert(sizeof (buf) >= 16);
|
||||
TEST_CHECK(UTI_HexToBytes("", buf, sizeof (buf)) == 0);
|
||||
TEST_CHECK(UTI_HexToBytes("0", buf, sizeof (buf)) == 0);
|
||||
TEST_CHECK(UTI_HexToBytes("00123456789ABCDEF", buf, sizeof (buf)) == 0);
|
||||
TEST_CHECK(UTI_HexToBytes("00123456789ABCDEF0", buf, 8) == 0);
|
||||
TEST_CHECK(UTI_HexToBytes("00123456789ABCDEF0", buf, sizeof (buf)) == 9);
|
||||
TEST_CHECK(memcmp(buf, "\x00\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 9) == 0);
|
||||
memcpy(buf, "AB123456780001", 15);
|
||||
TEST_CHECK(UTI_HexToBytes(buf, buf, sizeof (buf)) == 7);
|
||||
TEST_CHECK(memcmp(buf, "\xAB\x12\x34\x56\x78\x00\x01", 7) == 0);
|
||||
|
||||
TEST_CHECK(UTI_BytesToHex("\xAB\x12\x34\x56\x78\x00\x01", 7, buf, 14) == 0);
|
||||
TEST_CHECK(UTI_BytesToHex("\xAB\x12\x34\x56\x78\x00\x01", 7, buf, 15) == 1);
|
||||
TEST_CHECK(strcmp(buf, "AB123456780001") == 0);
|
||||
}
|
||||
|
|
39
util.c
39
util.c
|
@ -1361,3 +1361,42 @@ UTI_GetRandomBytes(void *buf, unsigned int len)
|
|||
UTI_GetRandomBytesUrandom(buf, len);
|
||||
#endif
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
int
|
||||
UTI_BytesToHex(const void *buf, unsigned int buf_len, char *hex, unsigned int hex_len)
|
||||
{
|
||||
unsigned int i, l;
|
||||
|
||||
for (i = l = 0; i < buf_len; i++, l += 2) {
|
||||
if (l + 2 >= hex_len ||
|
||||
snprintf(hex + l, hex_len - l, "%02hhX", ((const char *)buf)[i]) != 2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* ================================================== */
|
||||
|
||||
unsigned int
|
||||
UTI_HexToBytes(const char *hex, void *buf, unsigned int len)
|
||||
{
|
||||
char *p, byte[3];
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < len && *hex != '\0'; i++) {
|
||||
byte[0] = *hex++;
|
||||
if (*hex == '\0')
|
||||
return 0;
|
||||
byte[1] = *hex++;
|
||||
byte[2] = '\0';
|
||||
((char *)buf)[i] = strtol(byte, &p, 16);
|
||||
|
||||
if (p != byte + 2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
return *hex == '\0' ? i : 0;
|
||||
}
|
||||
|
|
7
util.h
7
util.h
|
@ -205,6 +205,13 @@ extern void UTI_GetRandomBytesUrandom(void *buf, unsigned int len);
|
|||
generating long-term keys */
|
||||
extern void UTI_GetRandomBytes(void *buf, unsigned int len);
|
||||
|
||||
/* Print data in hexadecimal format */
|
||||
extern int UTI_BytesToHex(const void *buf, unsigned int buf_len, char *hex, unsigned int hex_len);
|
||||
|
||||
/* Parse a string containing data in hexadecimal format. In-place conversion
|
||||
is supported. */
|
||||
extern unsigned int UTI_HexToBytes(const char *hex, void *buf, unsigned int len);
|
||||
|
||||
/* Macros to get maximum and minimum of two values */
|
||||
#ifdef MAX
|
||||
#undef MAX
|
||||
|
|
Loading…
Reference in a new issue