summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-02-14 15:54:31 +0100
committerNeels Hofmeyr <nhofmeyr@sysmocom.de>2017-02-14 15:59:22 +0100
commit437ed4ab4295998a488769657fe300cb8d4b6080 (patch)
tree656117a821c60b4b0ce1e9c9ddf01b4a6f2c0e1c /src
parent7adb567ffbbfc46dad70f1bb62f6da549127758f (diff)
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
Diffstat (limited to 'src')
-rw-r--r--src/utils.c31
1 files changed, 22 insertions, 9 deletions
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<l; i++) {
- char c = str[i];
+ for (strpos = str; (c = *strpos); strpos++) {
+ /* skip whitespace */
+ if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
+ continue;
+
+ /* If the buffer is too small, error out */
+ if (nibblepos >= (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];