From e43d699973eb3207f1f7948d5d8cbb71106a7ec1 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Thu, 31 Oct 2019 12:52:18 +0100 Subject: [PATCH] util: add functions for printing and parsing hexadecimal data --- keys.c | 16 ++-------------- test/unit/util.c | 15 +++++++++++++++ util.c | 39 +++++++++++++++++++++++++++++++++++++++ util.h | 7 +++++++ 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/keys.c b/keys.c index 1305e82..1fa2215 100644 --- a/keys.c +++ b/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; diff --git a/test/unit/util.c b/test/unit/util.c index af9e90c..824f3a8 100644 --- a/test/unit/util.c +++ b/test/unit/util.c @@ -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); } diff --git a/util.c b/util.c index 0e605ac..ec3ea60 100644 --- a/util.c +++ b/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; +} diff --git a/util.h b/util.h index f0cc141..6f0db5c 100644 --- a/util.h +++ b/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