Add a function to decode a hex string to binary.
authorRoy Marples <roy@marples.name>
Sun, 28 Sep 2014 19:36:11 +0000 (19:36 +0000)
committerRoy Marples <roy@marples.name>
Sun, 28 Sep 2014 19:36:11 +0000 (19:36 +0000)
src/libdhcpcd/dhcpcd.c
src/libdhcpcd/dhcpcd.h

index 522ad18c8cd1728bc02a7a896f1764325469a552..5008420536b4dfcf04ca01869876e0606f24acf1 100644 (file)
@@ -261,6 +261,43 @@ dhcpcd_decode(char *dst, size_t dlen, const char *src)
 }
 
 ssize_t
+dhcpcd_decode_hex(char *dst, size_t dlen, const char *src)
+{
+       size_t bytes, i;
+       char c;
+       int val, n;
+
+       bytes = 0;
+       while (*src) {
+               if (dlen == 0 || dlen == 1) {
+                       errno = ENOSPC;
+                       return -1;
+               }
+               val = 0;
+               for (i = 0; i < 2; i++) {
+                       c = *src++;
+                       if (c >= '0' && c <= '9')
+                               n = c - '0';
+                       else if (c >= 'a' && c <= 'f')
+                               n = 10 + c - 'a';
+                       else if (c >= 'A' && c <= 'F')
+                               n = 10 + c - 'A';
+                       else {
+                               errno = EINVAL;
+                               return -1;
+                       }
+                       val = val * 16 + n;
+               }
+               *dst++ = (char)val;
+               bytes += 2;
+               dlen -= 2;
+               if (*src == ':')
+                       src++;
+       }
+       return (ssize_t)bytes;
+}
+
+ssize_t
 dhcpcd_decode_shell(char *dst, size_t dlen, const char *src)
 {
        char *tmp, *p, c, *e, *d;
index 2adb4da676ca8b244d308e1853ba73b83bae0756..604ac6a6fdf781c4884d0cc0aa19fc59cace4337 100644 (file)
@@ -206,6 +206,7 @@ int dhcpcd_strnunvis(char *dst, size_t dlen, const char *src);
 #endif
 ssize_t dhcpcd_decode(char *dst, size_t dlen, const char *src);
 ssize_t dhcpcd_decode_shell(char *dst, size_t dlen, const char *src);
+ssize_t dhcpcd_decode_hex(char *dst, size_t dlen, const char *src);
 char * dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg);
 
 ssize_t dhcpcd_command(DHCPCD_CONNECTION *, const char *, char **);