From 5af178dee33b22cf189c085e41ca96423f809034 Mon Sep 17 00:00:00 2001 From: Rob Austein Date: Fri, 2 Sep 2016 01:31:17 -0400 Subject: Code to convert between text and internal forms of UUIDs. Includes a few cosmetic fixes to address gcc format string warnings and git trailing whitespace warnings. --- core.c | 2 +- daemon.c | 4 ++-- hal_internal.h | 6 ++++++ slip.c | 2 +- tests/test-bus.c | 3 ++- tests/test-mkmif.c | 10 +++++----- tests/test-trng.c | 4 ++-- uuid.c | 36 ++++++++++++++++++++++++++++++++++++ 8 files changed, 55 insertions(+), 12 deletions(-) diff --git a/core.c b/core.c index 378f085..fed4b66 100644 --- a/core.c +++ b/core.c @@ -208,7 +208,7 @@ hal_error_t hal_core_alloc(const char *name, hal_core_t **pcore) { hal_core_t *core; hal_error_t err = HAL_ERROR_CORE_NOT_FOUND; - + if (name == NULL && (pcore == NULL || *pcore == NULL)) return HAL_ERROR_BAD_ARGUMENTS; diff --git a/daemon.c b/daemon.c index b30d730..ff95353 100644 --- a/daemon.c +++ b/daemon.c @@ -74,7 +74,7 @@ static void poll_add(int fd) { /* add 4 entries at a time to avoid having to realloc too often */ #define NNEW 4 - + /* expand the array if necessary */ if (nfds == npollfds) { npollfds = nfds + NNEW; @@ -247,7 +247,7 @@ int main(int argc, char *argv[]) perror("poll"); exit(EXIT_FAILURE); } - + for (nfds_t i = 0; i < nfds; ++i) { if (pollfds[i].revents != 0) { /* XXX POLLERR|POLLHUP|POLLNVAL */ diff --git a/hal_internal.h b/hal_internal.h index 72f326c..ef00328 100644 --- a/hal_internal.h +++ b/hal_internal.h @@ -258,6 +258,8 @@ extern hal_error_t hal_rpc_pkcs1_construct_digestinfo(const hal_hash_handle_t ha * UUID stuff. All UUIDs we use (or are likely to use) are type 4 "random" UUIDs */ +#define HAL_UUID_TEXT_SIZE (sizeof("00112233-4455-6677-8899-aabbccddeeff")) + static inline int hal_uuid_cmp(const hal_uuid_t * const a, const hal_uuid_t * const b) { return memcmp(a, b, sizeof(hal_uuid_t)); @@ -265,6 +267,10 @@ static inline int hal_uuid_cmp(const hal_uuid_t * const a, const hal_uuid_t * co extern hal_error_t hal_uuid_gen(hal_uuid_t *uuid); +extern hal_error_t hal_uuid_parse(hal_uuid_t *uuid, const char * const string); + +extern hal_error_t hal_uuid_format(const hal_uuid_t * const uuid, char *buffer, const size_t buffer_len); + /* * Keystore API. */ diff --git a/slip.c b/slip.c index 889663a..6329afd 100644 --- a/slip.c +++ b/slip.c @@ -144,7 +144,7 @@ hal_error_t hal_slip_recv(uint8_t * const buf, size_t * const len, const size_t { int complete; hal_error_t ret; - + while (1) { ret = hal_slip_recv_char(buf, len, maxlen, &complete); if ((ret != HAL_OK) || complete) diff --git a/tests/test-bus.c b/tests/test-bus.c index b4a3e1c..94d7a70 100644 --- a/tests/test-bus.c +++ b/tests/test-bus.c @@ -68,7 +68,8 @@ static int sanity(const hal_core_t *board_core) } if (data != rnd) { - printf("Data bus fail: expected %08x, got %08x, diff %08x\n", rnd, data, data ^ rnd); + printf("Data bus fail: expected %08lx, got %08lx, diff %08lx\n", + (unsigned long) rnd, (unsigned long) data, (unsigned long) (data ^ rnd)); return 1; } diff --git a/tests/test-mkmif.c b/tests/test-mkmif.c index ab5801e..3645577 100644 --- a/tests/test-mkmif.c +++ b/tests/test-mkmif.c @@ -61,12 +61,12 @@ static hal_error_t write_test(hal_core_t *core) uint32_t write_address; int i; hal_error_t err; - + for (write_data = 0x01020304, write_address = 0, i = 0; i < 0x10; write_data += 0x01010101, write_address += 4, ++i) { - printf("Trying to write 0x%08x to memory address 0x%08x.\n", + printf("Trying to write 0x%08x to memory address 0x%08x.\n", (unsigned int)write_data, (unsigned int)write_address); if ((err = hal_mkmif_write_word(core, write_address, write_data)) != HAL_OK) { @@ -84,7 +84,7 @@ static hal_error_t read_test(hal_core_t *core) uint32_t read_address; int i; hal_error_t err; - + for (read_address = 0, i = 0; i < 0x10; read_address += 4, ++i) { @@ -115,7 +115,7 @@ static hal_error_t write_read_test(hal_core_t *core) printf("write error: %s\n", hal_error_string(err)); return err; } - + if ((err = hal_mkmif_read_word(core, 0x00000000, &readback)) != HAL_OK) { printf("read error: %s\n", hal_error_string(err)); return err; @@ -125,7 +125,7 @@ static hal_error_t write_read_test(hal_core_t *core) printf("read %08x, expected %08x\n", (unsigned int)readback, (unsigned int)data); return HAL_ERROR_IO_UNEXPECTED; } - + return HAL_OK; } diff --git a/tests/test-trng.c b/tests/test-trng.c index ebfe701..f570752 100644 --- a/tests/test-trng.c +++ b/tests/test-trng.c @@ -84,7 +84,7 @@ static hal_error_t test_random(const char *name) return err; } - printf("%08x ", rnd); + printf("%08lx ", (unsigned long) rnd); } printf("\n"); @@ -106,7 +106,7 @@ int main(void) } else { for (i = 0; i < 8; ++i) { - printf("%08x ", rnd[i]); + printf("%08lx ", (unsigned long) rnd[i]); } printf("\n"); } diff --git a/uuid.c b/uuid.c index 04410c0..be13356 100644 --- a/uuid.c +++ b/uuid.c @@ -32,6 +32,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include #include #include "hal.h" @@ -68,6 +69,41 @@ hal_error_t hal_uuid_gen(hal_uuid_t *uuid) return HAL_OK; } +hal_error_t hal_uuid_parse(hal_uuid_t *uuid, const char * const string) +{ + static const char fmt[] + = "%2hhx%2hhx%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx-%2hhx%2hhx%2hhx%2hhx%2hhx%2hhx"; + + if (uuid == NULL || string == NULL || + sscanf(string, fmt, + uuid->uuid + 0, uuid->uuid + 1, uuid->uuid + 2, uuid->uuid + 3, + uuid->uuid + 4, uuid->uuid + 5, uuid->uuid + 6, uuid->uuid + 7, + uuid->uuid + 8, uuid->uuid + 9, uuid->uuid + 10, uuid->uuid + 11, + uuid->uuid + 12, uuid->uuid + 13, uuid->uuid + 14, uuid->uuid + 15) != 16) + return HAL_ERROR_BAD_ARGUMENTS; + + return HAL_OK; +} + +hal_error_t hal_uuid_format(const hal_uuid_t * const uuid, char *buffer, const size_t buffer_len) +{ + static const char fmt[] + = "%02hhx%02hhx%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx-%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"; + + if (uuid == NULL || buffer == NULL || buffer_len < HAL_UUID_TEXT_SIZE) + return HAL_ERROR_BAD_ARGUMENTS; + + if (buffer_len != snprintf(buffer, buffer_len, fmt, + uuid->uuid[ 0], uuid->uuid[ 1], uuid->uuid[ 2], uuid->uuid[ 3], + uuid->uuid[ 4], uuid->uuid[ 5], uuid->uuid[ 6], uuid->uuid[ 7], + uuid->uuid[ 8], uuid->uuid[ 9], uuid->uuid[10], uuid->uuid[11], + uuid->uuid[12], uuid->uuid[13], uuid->uuid[14], uuid->uuid[15])) + return HAL_ERROR_RESULT_TOO_LONG; + + return HAL_OK; +} + + /* * Local variables: * indent-tabs-mode: nil -- cgit v1.2.3