Remove vis/unvis encoding and dhcpcd-decode, this was a mistake.
[dhcpcd-ui] / src / libdhcpcd / dhcpcd.c
index 522ad18c8cd1728bc02a7a896f1764325469a552..9861dd172bc33dbaa1b9ca68a82cb2860d04809e 100644 (file)
@@ -231,8 +231,13 @@ get_value(const char *data, size_t len, const char *var)
        vlen = strlen(var);
        p = NULL;
        while (data + vlen + 1 < end) {
-               if (strncmp(data, var, vlen) == 0) {
-                       p = data + vlen;
+               /* Skip past NUL padding */
+               if (*data == '\0') {
+                       data++;
+                       continue;
+               }
+               if (strncmp(data, var, vlen) == 0 && data[vlen] == '=') {
+                       p = data + vlen + 1;
                        break;
                }
                data += strlen(data) + 1;
@@ -252,61 +257,162 @@ dhcpcd_get_value(const DHCPCD_IF *i, const char *var)
 }
 
 ssize_t
-dhcpcd_decode(char *dst, size_t dlen, const char *src)
+dhcpcd_encode_string_escape(char *dst, size_t len, const char *src, size_t slen)
 {
+       const char *end;
+       size_t bytes;
+       char c;
+
+       end = src + slen;
+       bytes = 0;
+       while (src < end) {
+               c = *src++;
+               if ((c == '\\' || !isascii(c) || !isprint(c))) {
+                       if (c == '\\') {
+                               if (dst) {
+                                       if (len  == 0 || len == 1) {
+                                               errno = ENOSPC;
+                                               return -1;
+                                       }
+                                       *dst++ = '\\'; *dst++ = '\\';
+                                       len -= 2;
+                               }
+                               bytes += 2;
+                               continue;
+                       }
+                       if (dst) {
+                               if (len < 5) {
+                                       errno = ENOSPC;
+                                       return -1;
+                               }
+                               *dst++ = '\\';
+                               *dst++ = (((unsigned char)c >> 6) & 03) + '0';
+                               *dst++ = (((unsigned char)c >> 3) & 07) + '0';
+                               *dst++ = ( (unsigned char)c       & 07) + '0';
+                               len -= 4;
+                       }
+                       bytes += 4;
+               } else {
+                       if (dst) {
+                               if (len == 0) {
+                                       errno = ENOSPC;
+                                       return -1;
+                               }
+                               *dst++ = (char)c;
+                               len--;
+                       }
+                       bytes++;
+               }
+       }
 
-       assert(dst);
-       assert(src);
-       return strnunvis(dst, dlen, src);
+       if (dst) {
+               if (len == 0) {
+                       errno = ENOSPC;
+                       return -1;
+               }
+               *dst = '\0';
+       }
+       bytes++;
+
+       return (ssize_t)bytes;
 }
 
 ssize_t
-dhcpcd_decode_shell(char *dst, size_t dlen, const char *src)
+dhcpcd_decode_string_escape(char *dst, size_t dlen, const char *src)
 {
-       char *tmp, *p, c, *e, *d;
-       ssize_t l;
-
-       assert(dst);
-       assert(src);
-
-       tmp = malloc(dlen);
-       if (tmp == NULL)
-               return -1;
-       if ((l = dhcpcd_decode(tmp, dlen, src)) == -1) {
-               free(tmp);
-               return -1;
-       }
-
-       p = tmp;
-       e = tmp + l;
-       d = dst;
-       while (p < e) {
-               c = *p++;
+       char c, esc;
+       int oct;
+       ssize_t bytes;
 
-               if (isascii(c) && (isgraph(c) || iswhite(c))) {
-                       if (dlen < 2) {
-                               errno = ENOSPC;
+       bytes = 0;
+       for (;;) {
+               c = *src++;
+               if (c == '\0')
+                       break;
+               if (dst && --dlen == 0) {
+                       errno = ENOSPC;
+                       return 0;
+               }
+               switch (c) {
+               case '\\':
+                       if (*src == '\0') {
+                               errno = EINVAL;
                                return -1;
                        }
-                       *d++ = c;
-                       dlen--;
-                       continue;
+                       esc = *src++;
+                       switch (esc) {
+                       case '\\':
+                       case '0':
+                       case '1':
+                       case '3':
+                       case '4':
+                       case '5':
+                       case '6':
+                       case '7':
+                               oct = esc - '0';
+                               if (*src >= '0' && *src <='7')
+                                       oct = oct * 8 + (*src++ - '0');
+                               else {
+                                       errno = EINVAL;
+                                       return -1;
+                               }
+                               if (*src >= '0' && *src <='7')
+                                       oct = oct * 8 + (*src++ - '0');
+                               else {
+                                       errno = EINVAL;
+                                       return -1;
+                               }
+                               if (dst)
+                                       *dst++ = (char)oct;
+                       default:
+                               errno = EINVAL;
+                               return -1;
+                       }
+                       break;
+               default:
+                       if (dst)
+                               *dst++ = c;
                }
-               
-               if (dlen < 5) {
+               bytes++;
+       }
+       return bytes;
+}
+
+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;
                }
-               *d++ = '\\';
-               *d++ = (((unsigned char)c >> 6) & 03) + '0';
-               *d++ = (((unsigned char)c >> 3) & 07) + '0';
-               *d++ = ( (unsigned char)c       & 07) + '0';
-               dlen -= 4;
+               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++;
        }
-       *d = '\0';
-       free(tmp);
-
-       return d - dst;
+       return (ssize_t)bytes;
 }
 
 const char *
@@ -417,23 +523,23 @@ dhcpcd_get_if(DHCPCD_CONNECTION *con, const char *ifname, const char *type)
 static DHCPCD_IF *
 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
 {
-       const char *ifname, *ifclass, *reason, *type, *order, *flags, *ssid;
+       const char *ifname, *ifclass, *reason, *type, *order, *flags;
        char *orderdup, *o, *p;
        DHCPCD_IF *e, *i, *l, *n, *nl;
        int ti;
        bool addedi;
 
-       ifname = get_value(data, len, "interface=");
+       ifname = get_value(data, len, "interface");
        if (ifname == NULL || *ifname == '\0') {
                errno = ESRCH;
                return NULL;
        }
-       reason = get_value(data, len, "reason=");
+       reason = get_value(data, len, "reason");
        if (reason == NULL || *reason == '\0') {
                errno = ESRCH;
                return NULL;
        }
-       ifclass = get_value(data, len, "ifclass=");
+       ifclass = get_value(data, len, "ifclass");
        /* Skip pseudo interfaces */
        if (ifclass && *ifclass != '\0') {
                errno = ENOTSUP;
@@ -443,7 +549,7 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
                errno = ENOTSUP;
                return NULL;
        }
-       order = get_value(data, len, "interface_order=");
+       order = get_value(data, len, "interface_order");
        if (order == NULL || *order == '\0') {
                errno = ESRCH;
                return NULL;
@@ -531,7 +637,7 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
        i->ifname = ifname;
        i->type = type;
        i->reason = reason;
-       flags = dhcpcd_get_value(i, "ifflags=");
+       flags = dhcpcd_get_value(i, "ifflags");
        if (flags)
                i->flags = (unsigned int)strtoul(flags, NULL, 0);
        else
@@ -539,12 +645,9 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
        if (strcmp(reason, "CARRIER") == 0)
                i->up = true;
        else
-               i->up = strtobool(dhcpcd_get_value(i, "if_up="));
-       i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless="));
-       ssid = dhcpcd_get_value(i, i->up ? "new_ssid=" : "old_ssid=");
-       if (ssid == NULL ||
-           dhcpcd_decode_shell(i->ssid, sizeof(i->ssid), ssid) == -1)
-               *i->ssid = '\0';
+               i->up = strtobool(dhcpcd_get_value(i, "if_up"));
+       i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless"));
+       i->ssid = dhcpcd_get_value(i, "ifssid");
 
        /* Sort! */
        n = nl = NULL;
@@ -912,7 +1015,7 @@ dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
        assert(i);
        /* Don't report non SLAAC configurations */
        if (strcmp(i->type, "ra") == 0 && i->up &&
-           dhcpcd_get_value(i, "ra1_prefix=") == NULL)
+           dhcpcd_get_value(i, "ra1_prefix") == NULL)
                return NULL;
 
        showssid = false;
@@ -952,12 +1055,12 @@ dhcpcd_if_message(DHCPCD_IF *i, bool *new_msg)
        }
 
        pfx = i->up ? "new_" : "old_";
-       if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address=")))
-               iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr=");
-       else if ((ip = dhcpcd_get_value(i, "ra1_prefix=")))
+       if ((ip = dhcpcd_get_prefix_value(i, pfx, "ip_address")))
+               iplen = dhcpcd_get_prefix_value(i, pfx, "subnet_cidr");
+       else if ((ip = dhcpcd_get_value(i, "ra1_prefix")))
                iplen = NULL;
        else if ((ip = dhcpcd_get_prefix_value(i, pfx,
-           "dhcp6_ia_na1_ia_addr1=")))
+           "dhcp6_ia_na1_ia_addr1")))
                iplen = "128";
        else {
                ip = NULL;