aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2010-07-30 10:37:29 +0200
committerHarald Welte <laforge@gnumonks.org>2010-07-30 10:37:29 +0200
commit3eba991b3d005c3ae4aafe2235558a60e06b49f4 (patch)
tree1b47eb996754a4da30728d358085f1c73b35f427
parent7459d6cfd1765f8431b3d9a262eff332f9af807c (diff)
Import 'hexparse' function from OpenBSC0.1.14
-rw-r--r--include/osmocore/utils.h2
-rw-r--r--src/utils.c27
2 files changed, 29 insertions, 0 deletions
diff --git a/include/osmocore/utils.h b/include/osmocore/utils.h
index 51c6f03..c5b95bd 100644
--- a/include/osmocore/utils.h
+++ b/include/osmocore/utils.h
@@ -17,4 +17,6 @@ char bcd2char(uint8_t bcd);
/* only works for numbers in ascci */
uint8_t char2bcd(char c);
+int hexparse(const char *str, uint8_t *b, int max_len);
+
#endif
diff --git a/src/utils.c b/src/utils.c
index 4dab064..05381c8 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -48,3 +48,30 @@ uint8_t char2bcd(char c)
{
return c - 0x30;
}
+
+int 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;
+
+ memset(b, 0x00, max_len);
+
+ for (i=0; i<l; i++) {
+ char c = str[i];
+ if (c >= '0' && c <= '9')
+ v = c - '0';
+ else if (c >= 'a' && c <= 'f')
+ v = 10 + (c - 'a');
+ else if (c >= 'A' && c <= 'F')
+ v = 10 + (c - 'A');
+ else
+ return -1;
+ b[i>>1] |= v << (i&1 ? 0 : 4);
+ }
+
+ return i>>1;
+}