--- src/utils.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-)
diff --git a/src/utils.c b/src/utils.c index c36979c..525bff1 100644 --- a/src/utils.c +++ b/src/utils.c @@ -73,7 +73,7 @@ uint8_t osmo_char2bcd(char c) return c - 0x30; }
-/*! \brief Parse a string ocntaining hexadecimal digits +/*! \brief Parse a string ocntaining hexadecimal digits, optionally prefixed with 0x * \param[in] str string containing ASCII encoded hexadecimal digits * \param[out] b output buffer * \param[in] max_len maximum space in output buffer @@ -81,10 +81,28 @@ uint8_t osmo_char2bcd(char c) int osmo_hexparse(const char *str, uint8_t *b, int max_len)
{ - int i, l, v; + int v; + size_t i, l = strlen(str); + + if (l > 2) { /* remove 0x prefix if any */ + if ('0' == str[0] && 'x' == str[1]) { + l -= 2; + str += 2; + } + } + + if (l & 1) i = l + 1; + else i = l; + char hs[i]; /* enough space to store additional leading 0 if str length is odd */ + + if (l & 1) { + memcpy(hs + 1, str, l); + hs[0] = '0'; + } else { + memcpy(hs, str, l); + }
- l = strlen(str); - if ((l&1) || ((l>>1) > max_len)) + if ((l>>1) > max_len) return -1;
memset(b, 0x00, max_len);
baseband-devel@lists.osmocom.org