]> git.ipfire.org Git - thirdparty/iw.git/commitdiff
add hex parser util
authorJohannes Berg <johannes@sipsolutions.net>
Thu, 25 Mar 2010 06:38:15 +0000 (23:38 -0700)
committerJohannes Berg <johannes@sipsolutions.net>
Thu, 25 Mar 2010 06:38:15 +0000 (23:38 -0700)
iw.h
util.c

diff --git a/iw.h b/iw.h
index 305604e5b9c4fb1b55577a74d4de372d69503454..d1608e8d71e4cae09442cc31f58c6231d4d13ca9 100644 (file)
--- a/iw.h
+++ b/iw.h
@@ -117,6 +117,7 @@ __u32 __listen_events(struct nl80211_state *state,
 
 int mac_addr_a2n(unsigned char *mac_addr, char *arg);
 void mac_addr_n2a(char *mac_addr, unsigned char *arg);
+unsigned char *parse_hex(char *hex, size_t *outlen);
 
 int parse_keys(struct nl_msg *msg, char **argv, int argc);
 
diff --git a/util.c b/util.c
index 3cb77038fe4f9433e281e3679f098d256ad11d64..9fc08441a7bd79812dcd47a1b11d75ab7f938118 100644 (file)
--- a/util.c
+++ b/util.c
@@ -48,6 +48,43 @@ int mac_addr_a2n(unsigned char *mac_addr, char *arg)
        return 0;
 }
 
+unsigned char *parse_hex(char *hex, size_t *outlen)
+{
+       size_t len = strlen(hex);
+       unsigned char *result = calloc(len/2 + 2, 1);
+       int pos = 0;
+
+       if (!result)
+               return NULL;
+
+       *outlen = 0;
+
+       while (1) {
+               int temp;
+               char *cp = strchr(hex, ':');
+               if (cp) {
+                       *cp = 0;
+                       cp++;
+               }
+               if (sscanf(hex, "%x", &temp) != 1)
+                       goto error;
+               if (temp < 0 || temp > 255)
+                       goto error;
+
+               (*outlen)++;
+
+               result[pos++] = temp;
+               if (!cp)
+                       break;
+               hex = cp;
+       }
+
+       return result;
+ error:
+       free(result);
+       return NULL;
+}
+
 static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
        "unspecified",
        "IBSS",