From 437ed4ab4295998a488769657fe300cb8d4b6080 Mon Sep 17 00:00:00 2001 From: Neels Hofmeyr Date: Tue, 14 Feb 2017 15:54:31 +0100 Subject: osmo_hexparse: allow whitespace in parsed string, add ws test This is particularly useful for hex dumps containing spaces found in a log (e.g. osmo-nitb authentication rand token), which can now be passed in quotes to osmo-auc-gen without having to edit the spaces away. Change-Id: Ib7af07f674a2d26c8569acdee98835fb3e626c45 --- src/utils.c | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) (limited to 'src/utils.c') diff --git a/src/utils.c b/src/utils.c index 34b2bca5..1a4aab4b 100644 --- a/src/utils.c +++ b/src/utils.c @@ -127,16 +127,22 @@ uint8_t osmo_char2bcd(char c) int osmo_hexparse(const char *str, uint8_t *b, int max_len) { - int i, l, v; - - l = strlen(str); - if ((l&1) || ((l>>1) > max_len)) - return -1; + char c; + uint8_t v; + const char *strpos; + unsigned int nibblepos = 0; memset(b, 0x00, max_len); - for (i=0; i= (max_len << 1)) + return -1; + if (c >= '0' && c <= '9') v = c - '0'; else if (c >= 'a' && c <= 'f') @@ -145,10 +151,17 @@ int osmo_hexparse(const char *str, uint8_t *b, int max_len) v = 10 + (c - 'A'); else return -1; - b[i>>1] |= v << (i&1 ? 0 : 4); + + b[nibblepos >> 1] |= v << (nibblepos & 1 ? 0 : 4); + nibblepos ++; } - return i>>1; + /* In case of uneven amount of digits, the last byte is not complete + * and that's an error. */ + if (nibblepos & 1) + return -1; + + return nibblepos >> 1; } static char hexd_buff[4096]; -- cgit v1.2.3