From 55d39b8625164c13b5ad527e5b441b9d928a11e7 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Tue, 2 Jun 2020 21:36:42 +0200 Subject: [PATCH] Refactor hex_to_bytes --- utils.c | 16 +++++++++------- utils.h | 1 + 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/utils.c b/utils.c index eb0f342..0fbf42e 100644 --- a/utils.c +++ b/utils.c @@ -2,15 +2,17 @@ #include #include +unsigned char hex_to_byte(const char c) { + if (isdigit(c)) { + return c - '0'; + } + return 10 + c - 'a'; +} + size_t hex_to_bytes(unsigned char *out, const char hex[static 1]) { for (size_t i = 0; i < strlen(hex); ++i) { char c = tolower(hex[i]); - unsigned char value; - if (isdigit(c)) { - value = c - '0'; - } else { - value = 10 + c - 'a'; - } + unsigned char value = hex_to_byte(c); if (i % 2 == 0) { value <<= 4; } @@ -20,7 +22,7 @@ size_t hex_to_bytes(unsigned char *out, const char hex[static 1]) { } unsigned char *bytes_to_base64(unsigned char *out, - const unsigned char in[static 1], size_t len) { + const unsigned char in[static 1], size_t len) { static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; diff --git a/utils.h b/utils.h index 29bb13a..2f3f1cf 100644 --- a/utils.h +++ b/utils.h @@ -3,6 +3,7 @@ #include +unsigned char hex_to_byte(const char c); size_t hex_to_bytes(unsigned char *out, const char hex[static 1]); unsigned char *bytes_to_base64(unsigned char *out, const unsigned char in[static 1], size_t len);