diff options
Diffstat (limited to 'host/lib/host_misc.c')
-rw-r--r-- | host/lib/host_misc.c | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/host/lib/host_misc.c b/host/lib/host_misc.c index 63d1fee4..899d807f 100644 --- a/host/lib/host_misc.c +++ b/host/lib/host_misc.c @@ -116,3 +116,53 @@ vb2_error_t WriteFile(const char* filename, const void *data, uint64_t size) fclose(f); return 0; } + +bool parse_hex(uint8_t *val, const char *str) +{ + uint8_t v = 0; + char c; + int digit; + + for (digit = 0; digit < 2; digit++) { + c = *str; + if (!c) + return false; + if (!isxdigit(c)) + return false; + c = tolower(c); + if (c >= '0' && c <= '9') + v += c - '0'; + else + v += 10 + c - 'a'; + if (!digit) + v <<= 4; + str++; + } + + *val = v; + return true; +} + +bool parse_hash(uint8_t *buf, size_t len, const char *str) +{ + const char *s = str; + int i; + + for (i = 0; i < len; i++) { + /* skip whitespace */ + while (*s && isspace(*s)) + s++; + if (!*s) + break; + if (!parse_hex(buf, s)) + break; + + /* on to the next byte */ + s += 2; + buf++; + } + + if (i != len || *s) + return false; + return true; +} |