From 5c2e0b7b5911477dcc4dedfd9910e28e9af65a91 Mon Sep 17 00:00:00 2001 From: Samuel Dionne-Riel Date: Thu, 29 Aug 2019 15:07:36 -0400 Subject: [PATCH 1/2] uuid: add facilities to parse and print UUIDs Signed-off-by: Samuel Dionne-Riel --- uuid.c | 43 +++++++++++++++++++++++++++++++++++++++++++ uuid.h | 6 ++++++ 2 files changed, 49 insertions(+) diff --git a/uuid.c b/uuid.c index 7ded43a..47ef097 100644 --- a/uuid.c +++ b/uuid.c @@ -59,3 +59,46 @@ void generate_uuid(const char *namespace, const char *name, u8 result[16]) uuid->clk_seq_hi_res &= ~(1 << 6); uuid->clk_seq_hi_res |= 1 << 7; } + +void parse_uuid(const char *input, uint8_t result [16]) +{ + int u = 0; + if (strlen(input) != UUID_STR_LEN-1) { + fprintf(stderr, "UUID MUST be in the format 00112233-4455-6677-8899-AABBCCDDEEFF.\n"); + abort(); + } + for (int i = 0; i < UUID_STR_LEN - 1; i++) { + if (i == 8 || i == 13 || i == 18 || i == 23) { + /* Skip hyphens */ + } + else { + char pair[3] = "00"; + strncpy(pair, input + i, 2); + result[u] = strtol(pair, NULL, 16); + /* This handled a pair, let's move one more further along. */ + i++; + /* Move to the next byte. */ + u++; + } + } +} + +void uuid_to_string(const uint8_t uuid [16], char result [UUID_STR_LEN]) +{ + /* Index in uuid */ + int u = 0; + for (int i = 0; i < UUID_STR_LEN-1; i++) { + /* Handles separators */ + if (i == 8 || i == 13 || i == 18 || i == 23) { + strcpy(result+i, "-"); + } + else { + /* This writes \0 at the end of every invocation */ + snprintf(result+i, 3, "%02X", uuid[u]); + /* This handled a pair, let's move one more further along. */ + i++; + /* Move to the next byte. */ + u++; + } + } +} diff --git a/uuid.h b/uuid.h index ff1b438..21ba165 100644 --- a/uuid.h +++ b/uuid.h @@ -19,6 +19,12 @@ #include "ext4_utils.h" +#define UUID_STR_LEN 36+1 + void generate_uuid(const char *namespace, const char *name, u8 result[16]); +void parse_uuid(const char *input, uint8_t result [16]); + +void uuid_to_string(const uint8_t uuid [16], char result [UUID_STR_LEN]); + #endif -- 2.19.2