Remove vis/unvis encoding and dhcpcd-decode, this was a mistake.
authorRoy Marples <roy@marples.name>
Thu, 2 Oct 2014 07:58:38 +0000 (07:58 +0000)
committerRoy Marples <roy@marples.name>
Thu, 2 Oct 2014 07:58:38 +0000 (07:58 +0000)
configure
src/dhcpcd-decode/Makefile [deleted file]
src/dhcpcd-decode/dhcpcd-decode.8 [deleted file]
src/dhcpcd-decode/dhcpcd-decode.c [deleted file]
src/libdhcpcd/dhcpcd.c
src/libdhcpcd/dhcpcd.h
src/libdhcpcd/unvis.c [deleted file]
src/libdhcpcd/vis.c [deleted file]
src/libdhcpcd/wpa.c

index 73a761f4b4ba582c629ea27443aad3b404368ad2..f8d7c5dd830b36b0ed628396c7f0afd9214da745 100755 (executable)
--- a/configure
+++ b/configure
@@ -10,7 +10,7 @@ LIBDHCPCD=no
 HOST=
 HOSTCC=
 BUILD=
-WITH_TOOLS=yes
+WITH_DHCPCD_ONLINE=yes
 WITH_GTK=
 WITH_QT=
 WITH_ICONS=
@@ -44,8 +44,8 @@ for x; do
        --includedir) eval INCLUDEDIR="$INCLUDEDIR${INCLUDEDIR:+ }$var";;
        --datadir|--infodir) ;; # ignore autotools
        --disable-maintainer-mode|--disable-dependency-tracking) ;;
-       --with-tools) WITH_TOOLS=${var:-yes};;
-       --without-tools) WITH_TOOLS=no;;
+       --with-dhcpcd-online) WITH_DHCPCD_ONLINE=${var:-yes};;
+       --without-dhcpcd-online) WITH_DHCPCD_ONLINE=no;;
        --with-gtk|--with-gtk+) WITH_GTK=${var:-yes};;
        --without-gtk|--without-gtk+) WITH_GTK=no;;
        --with-qt) WITH_QT=${var:-yes};;
@@ -303,66 +303,6 @@ if [ "$STRLCPY" = no ]; then
            >>$CONFIG_H
 fi
 
-if [ -z "$VIS" ]; then
-       printf "Testing for vis ... "
-       cat <<EOF >_vis.c
-#include <vis.h>
-
-int main(void) {
-       char s[10];
-       xxvis(s, 0, 0, 0);
-       return 0;
-}
-EOF
-       if $XCC _vis.c -o _vis 2>&3; then
-               VIS=yes
-       else
-               VIS=no
-       fi
-       echo "$VIS"
-       rm -f _vis.c _vis
-fi
-if [ "$VIS" = no ]; then
-       echo "VIS_SRC=          vis.c" >>$CONFIG_MK
-       echo "#define vis       dhcpcd_vis" >>$CONFIG_H
-else
-       echo "#define HAVE_VIS_H" >>$CONFIG_H
-fi
-
-if [ -z "$STRNUNVIS" ]; then
-       printf "Testing for strnunvis ... "
-       cat <<EOF >_strnunvis.c
-#include <vis.h>
-#ifdef __FreeBSD__
-#error FreeBSD has broken strnunvis support
-#endif
-#ifdef __NetBSD_Version__
-#  if __NetBSD_Version__ <799000200
-#  error Upgrade NetBSD to 7.99.2 to get a working strnunvis
-#  endif
-#endif
-
-int main(void) {
-       char s[10];
-       strunvis(s, 0, s);
-       return 0;
-}
-EOF
-       if $XCC _strnunvis.c -o _strnunvis 2>&3; then
-               STRNUNVIS=yes
-       else
-               STRNUNVIS=no
-       fi
-       echo "$STRNUNVIS"
-       rm -f _strnunvis.c _strnunvis
-fi
-if [ "$STRNUNVIS" = no ]; then
-       echo "UNVIS_SRC=        unvis.c" >>$CONFIG_MK
-       echo "#define strnunvis dhcpcd_strnunvis" >>$CONFIG_H
-else
-       echo "#define HAVE_STRNUNVIS" >>$CONFIG_H
-fi
-
 if [ -z "$STRVERSCMP" ]; then
        printf "Testing for strverscmp ... "
        cat <<EOF >_strverscmp.c
@@ -474,8 +414,8 @@ if [ "$WITH_ICONS" = yes ]; then
        echo "MKICONS=  icons" >>$CONFIG_MK
 fi
 
-if [ -n "$WITH_TOOLS" -a "$WITH_TOOLS" != no ]; then
-       UI="dhcpcd-decode dhcpcd-online${UI:+ }$UI"
+if [ -n "$WITH_DHCPCD_ONLINE" -a "$WITH_ONLINE" != no ]; then
+       UI="dhcpcd-online${UI:+ }$UI"
 fi
 echo "UI=              ${UI:+libdhcpcd }$UI" >>$CONFIG_MK
 
diff --git a/src/dhcpcd-decode/Makefile b/src/dhcpcd-decode/Makefile
deleted file mode 100644 (file)
index b7870d6..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-PROG=          dhcpcd-decode
-SRCS=          dhcpcd-decode.c
-
-MAN8=          dhcpcd-decode.8
-
-TOPDIR=                ../..
-include ${TOPDIR}/iconfig.mk
-
-CPPFLAGS+=     -I${TOPDIR}
-
-LDADD+=                ${LIB_DHCPCD} ${LIB_INTL}
-
-include ../libdhcpcd/Makefile.inc
-include ${MKDIR}/prog.mk
diff --git a/src/dhcpcd-decode/dhcpcd-decode.8 b/src/dhcpcd-decode/dhcpcd-decode.8
deleted file mode 100644 (file)
index 88eade9..0000000
+++ /dev/null
@@ -1,59 +0,0 @@
-.\" Copyright (c) 2014 Roy Marples
-.\" All rights reserved
-.\"
-.\" Redistribution and use in source and binary forms, with or without
-.\" modification, are permitted provided that the following conditions
-.\" are met:
-.\" 1. Redistributions of source code must retain the above copyright
-.\"    notice, this list of conditions and the following disclaimer.
-.\" 2. Redistributions in binary form must reproduce the above copyright
-.\"    notice, this list of conditions and the following disclaimer in the
-.\"    documentation and/or other materials provided with the distribution.
-.\"
-.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
-.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
-.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
-.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
-.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
-.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
-.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
-.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
-.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
-.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
-.\" SUCH DAMAGE.
-.\"
-.Dd September 30, 2014
-.Dt DHCPCD-DECODE
-.Os
-.Sh NAME
-.Nm dhcpcd-decode
-.Nd a utility to decode
-.Xr dhcpcd 8
-shell variables
-.Sh SYNOPSIS
-.Nm
-.Op Fl s
-.Op Fl x
-.Op Ar data ...
-.Sh DESCRIPTION
-.Nm
-will decode each
-.Va data
-argument from the shell safe encoding
-.Xr dhcpcd 8
-applies and write the result to
-.Pa stdout .
-.Nm
-will do the same for each newline received from
-.Pa stdin .
-.Bl -tag
-.It Fl s
-decode the shell safe encoding only.
-Any non graphic octal escaped encoding will remain.
-.It Fl x
-decode each argument as hex into binary using ``:'' as a optional delimiter.
-.El
-.Sh AUTHORS
-.An Roy Marples Aq roy@marples.name
-.Sh BUGS
-Please report them to http://roy.marples.name/projects/dhcpcd-ui
diff --git a/src/dhcpcd-decode/dhcpcd-decode.c b/src/dhcpcd-decode/dhcpcd-decode.c
deleted file mode 100644 (file)
index 49e93c7..0000000
+++ /dev/null
@@ -1,128 +0,0 @@
-/*
- * dhcpcd-decode
- * Copyright 2014 Roy Marples <roy@marples.name>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <err.h>
-#include <errno.h>
-#include <libgen.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "dhcpcd.h"
-
-static ssize_t
-dhcpcd_decode_shell(char *dst, size_t dlen, const char *src)
-{
-       char *tmp;
-       ssize_t l;
-
-       tmp = malloc(dlen);
-       if (tmp == NULL)
-               return -1;
-       if ((l = dhcpcd_decode(tmp, dlen, src)) != -1)
-               l = dhcpcd_encode(dst, dlen, tmp, (size_t)l);
-
-       free(tmp);
-       return l;
-}
-
-static void
-process(char *src, ssize_t (*decode)(char *, size_t, const char *))
-{
-       char *buf;
-       size_t buflen;
-       ssize_t dl;
-
-       buflen = strlen(src) + 1;
-       buf = malloc(buflen);
-       if (buf == NULL)
-               err(EXIT_FAILURE, "malloc");
-       if ((dl = decode(buf, buflen, src)) == -1) {
-               free(buf);
-               err(EXIT_FAILURE, "decode");
-       }
-       if (fwrite(buf, 1, (size_t)dl, stdout) != (size_t)dl) {
-               free(buf);
-               err(EXIT_FAILURE, "fwrite");
-       }
-       free(buf);
-       fputc('\n', stdout);
-}
-
-static void
-usage(char *progname)
-{
-
-       fprintf(stderr, "usage: %s [-sx] [data ...]\n", basename(progname));
-}
-
-int
-main(int argc, char **argv)
-{
-       int opt;
-       ssize_t (*decode)(char *, size_t, const char *);
-
-       decode = dhcpcd_decode;
-       while ((opt = getopt(argc, argv, "sx")) != -1) {
-               switch (opt) {
-               case 's':
-                       decode = dhcpcd_decode_shell;
-                       break;
-               case 'x':
-                       decode = dhcpcd_decode_hex;
-                       break;
-               case '?':
-                       usage(argv[0]);
-                       return EXIT_FAILURE;
-               }
-       }
-
-       if (optind >= argc && isatty(fileno(stdin))) {
-               usage(argv[0]);
-               return EXIT_FAILURE;
-       }
-
-       for (; optind < argc; optind++)
-               process(argv[optind], decode);
-
-       if (!isatty(fileno(stdin))) {
-               char *arg;
-               size_t len;
-               ssize_t argl;
-
-               arg = NULL;
-               len = 0;
-               while ((argl = getline(&arg, &len, stdin)) != -1) {
-                       if (arg[argl - 1] == '\n')
-                               arg[argl - 1] = '\0';
-                       process(arg, decode);
-               }
-               free(arg);
-       }
-
-       return EXIT_SUCCESS;
-}
index 150594d11e763987fcd3b33180863206cd3b24b9..9861dd172bc33dbaa1b9ca68a82cb2860d04809e 100644 (file)
@@ -257,12 +257,125 @@ 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;
 
-       assert(dst);
-       assert(src);
-       return strnunvis(dst, dlen, src);
+       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++;
+               }
+       }
+
+       if (dst) {
+               if (len == 0) {
+                       errno = ENOSPC;
+                       return -1;
+               }
+               *dst = '\0';
+       }
+       bytes++;
+
+       return (ssize_t)bytes;
+}
+
+ssize_t
+dhcpcd_decode_string_escape(char *dst, size_t dlen, const char *src)
+{
+       char c, esc;
+       int oct;
+       ssize_t bytes;
+
+       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;
+                       }
+                       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;
+               }
+               bytes++;
+       }
+       return bytes;
 }
 
 ssize_t
@@ -302,31 +415,6 @@ dhcpcd_decode_hex(char *dst, size_t dlen, const char *src)
        return (ssize_t)bytes;
 }
 
-ssize_t
-dhcpcd_encode(char *dst, size_t dlen, const char *src, size_t slen)
-{
-       char *d, c, v[5], *ve, *vp;
-       const char *send;
-
-       d = dst;
-       send = src + slen;
-       while (src < send) {
-               c = *src++;
-               ve = vis(v, c, VIS_OCTAL | VIS_CSTYLE, src != send ? *src : 0);
-               if (dlen < (size_t)(ve - v) + 1) {
-                       errno = ENOSPC;
-                       return -1;
-               }
-               dlen -= (size_t)(ve - v);
-               vp = v;
-               while (vp != ve)
-                       *d++ = *vp++;
-       }
-       *d = '\0';
-
-       return d - dst;
-}
-
 const char *
 dhcpcd_get_prefix_value(const DHCPCD_IF *i, const char *prefix, const char *var)
 {
@@ -436,12 +524,10 @@ static DHCPCD_IF *
 dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
 {
        const char *ifname, *ifclass, *reason, *type, *order, *flags;
-       char *orderdup, *o, *p, *dbuf, *end, *dp, *eq;
+       char *orderdup, *o, *p;
        DHCPCD_IF *e, *i, *l, *n, *nl;
        int ti;
        bool addedi;
-       ssize_t dl, el;
-       size_t dbuflen, eql;
 
        ifname = get_value(data, len, "interface");
        if (ifname == NULL || *ifname == '\0') {
@@ -512,44 +598,6 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
                        return NULL;
        }
 
-       /* Remove all shell encoding but keep our non graphic encoding */
-       end = data + len;
-       dp = data;
-       dbuflen = 128; /* allocate an initial buffer */
-       dbuf = malloc(dbuflen);
-       if (dbuf == NULL)
-               return NULL;
-       while (dp < end) {
-               eq = strchr(dp, '=');
-               if (eq == NULL || *++eq == '\0') {
-                       errno = EINVAL;
-                       return NULL;
-               }
-               eql = strlen(eq) + 1;
-               if (dbuflen < eql) {
-                       char *nbuf;
-
-                       nbuf = realloc(dbuf, eql);
-                       if (nbuf == NULL) {
-                               free(dbuf);
-                               return NULL;
-                       }
-                       dbuf = nbuf;
-                       dbuflen = eql;
-               }
-               if ((dl = dhcpcd_decode(dbuf, dbuflen, eq)) == -1 ||
-                   (el = dhcpcd_encode(eq, eql, dbuf, (size_t)dl)) == -1)
-               {
-                       free(dbuf);
-                       return NULL;
-               }
-               /* NUL pad the remainder so get_value can skip it */
-               if (eql - (size_t)el > 0)
-                       memset(eq + el, 0, eql - (size_t)el);
-               dp = eq + eql;
-       }
-       free(dbuf);
-
        orderdup = strdup(order);
        if (orderdup == NULL)
                return NULL;
@@ -599,10 +647,7 @@ dhcpcd_new_if(DHCPCD_CONNECTION *con, char *data, size_t len)
        else
                i->up = strtobool(dhcpcd_get_value(i, "if_up"));
        i->wireless = strtobool(dhcpcd_get_value(i, "ifwireless"));
-       if (strcmp(i->type, "link") == 0)
-               i->ssid = dhcpcd_get_value(i, i->up ? "new_ssid" : "old_ssid");
-       else
-               i->ssid = dhcpcd_get_value(i, "if_ssid");
+       i->ssid = dhcpcd_get_value(i, "ifssid");
 
        /* Sort! */
        n = nl = NULL;
index d2076b264f28b07ac6277b49fe7d0c56fcbea0b5..fc4bcc9c9707ff56253eb8e585782190811d7701 100644 (file)
@@ -200,22 +200,10 @@ DHCPCD_CONNECTION * dhcpcd_if_connection(DHCPCD_IF *);
 const char *dhcpcd_get_value(const DHCPCD_IF *, const char *);
 const char *dhcpcd_get_prefix_value(const DHCPCD_IF *, const char *,
     const char *);
-
-#ifdef IN_LIBDHCPCD
-#ifndef VIS_OCTAL
-#define VIS_OCTAL      0x0001
-#define VIS_CSTYLE     0x0002
-#endif
-/* These functions only exists if libc does not provide a working one */
-char *dhcpcd_svis(char *dst, int c, int flags, int nextc, const char *extra);
-char *dhcpcd_vis(char *dst, int c, int flags, int nextc);
-int dhcpcd_strnunvis(char *dst, size_t dlen, const char *src);
-#endif
-ssize_t dhcpcd_encode(char *dst, size_t dlen, const char *src, size_t slen);
-ssize_t dhcpcd_decode(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_encode_string_escape(char *, size_t, const char *, size_t);
+ssize_t dhcpcd_decode_string_escape(char *, size_t, const char *);
+ssize_t dhcpcd_decode_hex(char *, size_t, const char *);
+char * dhcpcd_if_message(DHCPCD_IF *, bool *);
 
 ssize_t dhcpcd_command(DHCPCD_CONNECTION *, const char *, char **);
 ssize_t dhcpcd_command_arg(DHCPCD_CONNECTION *, const char *, const char *,
diff --git a/src/libdhcpcd/unvis.c b/src/libdhcpcd/unvis.c
deleted file mode 100644 (file)
index 407c524..0000000
+++ /dev/null
@@ -1,336 +0,0 @@
-/*     $NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $   */
-
-/*-
- * Copyright (c) 1989, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/*
- * HEAVILY trimmed down for use only in dhcpcd.
- * Please use the source in NetBSD for a fuller working copy.
- */
-
-#include <sys/types.h>
-
-#include <assert.h>
-#include <ctype.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <errno.h>
-
-#define IN_LIBDHCPCD
-#include "dhcpcd.h"
-
-#ifndef __arraycount
-#define __arraycount(__x)       (sizeof(__x) / sizeof(__x[0]))
-#endif
-
-/*
- * decode driven by state machine
- */
-#define        S_GROUND        0       /* haven't seen escape char */
-#define        S_START         1       /* start decoding special sequence */
-#define        S_META          2       /* metachar started (M) */
-#define        S_META1         3       /* metachar more, regular char (-) */
-#define        S_CTRL          4       /* control char started (^) */
-#define        S_OCTAL2        5       /* octal digit 2 */
-#define        S_OCTAL3        6       /* octal digit 3 */
-#define        S_HEX           7       /* mandatory hex digit */
-#define        S_HEX1          8       /* http hex digit */
-#define        S_HEX2          9       /* http hex digit 2 */
-
-#define        isoctal(c)      (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
-#define        xtod(c)         (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
-
-#define _VIS_END        0x0800  /* for unvis */
-#define UNVIS_END      _VIS_END
-
-/*
- * unvis return codes
- */
-#define        UNVIS_VALID      1      /* character valid */
-#define        UNVIS_VALIDPUSH  2      /* character valid, push back passed char */
-#define        UNVIS_NOCHAR     3      /* valid sequence, no character produced */
-#define        UNVIS_SYNBAD    -1      /* unrecognized escape sequence */
-#define        UNVIS_ERROR     -2      /* decoder in unknown state (unrecoverable) */
-
-/*
- * unvis - decode characters previously encoded by vis
- */
-static int
-unvis(char *cp, int c, int *astate, int flag)
-{
-       unsigned char uc = (unsigned char)c;
-       unsigned char st;
-
-/*
- * Bottom 8 bits of astate hold the state machine state.
- * Top 8 bits hold the current character in the http 1866 nv string decoding
- */
-#define GS(a)          ((a) & 0xff)
-#define SS(a, b)       (((uint32_t)(a) << 24) | (b))
-#define GI(a)          ((uint32_t)(a) >> 24)
-
-       st = (unsigned char)GS(*astate);
-
-       if (flag & UNVIS_END) {
-               switch (st) {
-               case S_OCTAL2:
-               case S_OCTAL3:
-               case S_HEX2:
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case S_GROUND:
-                       return UNVIS_NOCHAR;
-               default:
-                       return UNVIS_SYNBAD;
-               }
-       }
-
-       switch (st) {
-
-       case S_GROUND:
-               *cp = 0;
-               if (c == '\\') {
-                       *astate = SS(0, S_START);
-                       return UNVIS_NOCHAR;
-               }
-               *cp = (char)c;
-               return UNVIS_VALID;
-
-       case S_START:
-               switch(c) {
-               case '\\':
-                       *cp = (char)c;
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case '0': case '1': case '2': case '3':
-               case '4': case '5': case '6': case '7':
-                       *cp = (char)(c - '0');
-                       *astate = SS(0, S_OCTAL2);
-                       return UNVIS_NOCHAR;
-               case 'M':
-                       *cp = (char)0200;
-                       *astate = SS(0, S_META);
-                       return UNVIS_NOCHAR;
-               case '^':
-                       *astate = SS(0, S_CTRL);
-                       return UNVIS_NOCHAR;
-               case 'n':
-                       *cp = '\n';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'r':
-                       *cp = '\r';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'b':
-                       *cp = '\b';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'a':
-                       *cp = '\007';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'v':
-                       *cp = '\v';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 't':
-                       *cp = '\t';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'f':
-                       *cp = '\f';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 's':
-                       *cp = ' ';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'E':
-                       *cp = '\033';
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_VALID;
-               case 'x':
-                       *astate = SS(0, S_HEX);
-                       return UNVIS_NOCHAR;
-               case '\n':
-                       /*
-                        * hidden newline
-                        */
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_NOCHAR;
-               case '$':
-                       /*
-                        * hidden marker
-                        */
-                       *astate = SS(0, S_GROUND);
-                       return UNVIS_NOCHAR;
-               default:
-                       if (isgraph(c)) {
-                               *cp = (char)c;
-                               *astate = SS(0, S_GROUND);
-                               return UNVIS_VALID;
-                       }
-               }
-               goto bad;
-
-       case S_META:
-               if (c == '-')
-                       *astate = SS(0, S_META1);
-               else if (c == '^')
-                       *astate = SS(0, S_CTRL);
-               else 
-                       goto bad;
-               return UNVIS_NOCHAR;
-
-       case S_META1:
-               *astate = SS(0, S_GROUND);
-               *cp |= (char)c;
-               return UNVIS_VALID;
-
-       case S_CTRL:
-               if (c == '?')
-                       *cp |= 0177;
-               else
-                       *cp |= (char)(c & 037);
-               *astate = SS(0, S_GROUND);
-               return UNVIS_VALID;
-
-       case S_OCTAL2:  /* second possible octal digit */
-               if (isoctal(uc)) {
-                       /*
-                        * yes - and maybe a third
-                        */
-                       *cp = (char)((*cp << 3) + (c - '0'));
-                       *astate = SS(0, S_OCTAL3);
-                       return UNVIS_NOCHAR;
-               }
-               /*
-                * no - done with current sequence, push back passed char
-                */
-               *astate = SS(0, S_GROUND);
-               return UNVIS_VALIDPUSH;
-
-       case S_OCTAL3:  /* third possible octal digit */
-               *astate = SS(0, S_GROUND);
-               if (isoctal(uc)) {
-                       *cp = (char)((*cp << 3) + (c - '0'));
-                       return UNVIS_VALID;
-               }
-               /*
-                * we were done, push back passed char
-                */
-               return UNVIS_VALIDPUSH;
-
-       case S_HEX:
-               if (!isxdigit(uc))
-                       goto bad;
-               /*FALLTHROUGH*/
-       case S_HEX1:
-               if (isxdigit(uc)) {
-                       *cp = (char)xtod(uc);
-                       *astate = SS(0, S_HEX2);
-                       return UNVIS_NOCHAR;
-               }
-               /*
-                * no - done with current sequence, push back passed char
-                */
-               *astate = SS(0, S_GROUND);
-               return UNVIS_VALIDPUSH;
-
-       case S_HEX2:
-               *astate = S_GROUND;
-               if (isxdigit(uc)) {
-                       *cp = (char)(xtod(uc) | (*cp << 4));
-                       return UNVIS_VALID;
-               }
-               return UNVIS_VALIDPUSH;
-
-       default:
-       bad:
-               /*
-                * decoder in unknown state - (probably uninitialized)
-                */
-               *astate = SS(0, S_GROUND);
-               return UNVIS_SYNBAD;
-       }
-}
-
-/*
- * strnunvisx - decode src into dst
- *
- *     Number of chars decoded into dst is returned, -1 on error.
- *     Dst is null terminated.
- */
-
-int
-dhcpcd_strnunvis(char *dst, size_t dlen, const char *src)
-{
-       int c;
-       char t = '\0', *start = dst;
-       int state = 0;
-
-#define CHECKSPACE() \
-       do { \
-               if (dlen-- == 0) { \
-                       errno = ENOSPC; \
-                       return -1; \
-               } \
-       } while (/*CONSTCOND*/0)
-
-       while ((c = *src++) != '\0') {
- again:
-               switch (unvis(&t, c, &state, 0)) {
-               case UNVIS_VALID:
-                       CHECKSPACE();
-                       *dst++ = t;
-                       break;
-               case UNVIS_VALIDPUSH:
-                       CHECKSPACE();
-                       *dst++ = t;
-                       goto again;
-               case 0:
-               case UNVIS_NOCHAR:
-                       break;
-               case UNVIS_SYNBAD:
-                       errno = EINVAL;
-                       return -1;
-               default:
-                       errno = EINVAL;
-                       return -1;
-               }
-       }
-       if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
-               CHECKSPACE();
-               *dst++ = t;
-       }
-       CHECKSPACE();
-       *dst = '\0';
-       return (int)(dst - start);
-}
diff --git a/src/libdhcpcd/vis.c b/src/libdhcpcd/vis.c
deleted file mode 100644 (file)
index 8280c0d..0000000
+++ /dev/null
@@ -1,168 +0,0 @@
-/*     $NetBSD: vis.c,v 1.44 2011/03/12 19:52:48 christos Exp $        */
-
-/*-
- * Copyright (c) 1989, 1993
- *     The Regents of the University of California.  All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-/*-
- * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
- * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
- * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-/*
- * HEAVILY trimmed down for use only in dhcpcd.
- * Please use the source in NetBSD for a fuller working copy.
- */
-
-#include <ctype.h>
-#include <errno.h>
-#include <limits.h>
-#include <stdlib.h>
-#include <string.h>
-
-#define IN_LIBDHCPCD
-#include "dhcpcd.h"
-
-#undef BELL
-#define BELL '\a'
-
-#define isoctal(c)     (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
-#define iswhite(c)     (c == ' ' || c == '\t' || c == '\n')
-
-/*
- * This is do_vis, the central code of vis.
- * dst:              Pointer to the destination buffer
- * c:        Character to encode
- * flag:      Flag word
- * nextc:     The character following 'c'
- * extra:     Pointer to the list of extra characters to be
- *           backslash-protected.
- */
-char *
-dhcpcd_svis(char *dst, int c, int flag, int nextc, const char *extra)
-{
-       int isextra;
-
-       isextra = strchr(extra, c) != NULL;
-       if (!isextra && isascii(c) && (isgraph(c) || iswhite(c))) {
-               *dst++ = (char)c;
-               return dst;
-       }
-       if (flag & VIS_CSTYLE) {
-               switch (c) {
-               case '\n':
-                       *dst++ = '\\'; *dst++ = 'n';
-                       return dst;
-               case '\r':
-                       *dst++ = '\\'; *dst++ = 'r';
-                       return dst;
-               case '\b':
-                       *dst++ = '\\'; *dst++ = 'b';
-                       return dst;
-               case BELL:
-                       *dst++ = '\\'; *dst++ = 'a';
-                       return dst;
-               case '\v':
-                       *dst++ = '\\'; *dst++ = 'v';
-                       return dst;
-               case '\t':
-                       *dst++ = '\\'; *dst++ = 't';
-                       return dst;
-               case '\f':
-                       *dst++ = '\\'; *dst++ = 'f';
-                       return dst;
-               case ' ':
-                       *dst++ = '\\'; *dst++ = 's';
-                       return dst;
-               case '\0':
-                       *dst++ = '\\'; *dst++ = '0';
-                       if (isoctal(nextc)) {
-                               *dst++ = '0';
-                               *dst++ = '0';
-                       }
-                       return dst;
-               case '$': /* vis(1) - l */
-                       break;
-               default:
-                       if (isgraph(c)) {
-                               *dst++ = '\\'; *dst++ = (char)c;
-                               return dst;
-                       }
-               }
-       }
-
-       *dst++ = '\\';
-       if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {
-               *dst++ = (char)((((unsigned char)c >> 6) & 03) + '0');
-               *dst++ = (char)((((unsigned char)c >> 3) & 07) + '0');
-               *dst++ = (char)(( (unsigned char)c       & 07) + '0');
-       } else {
-               if (c & 0200) {
-                       c &= 0177; *dst++ = 'M';
-               }
-
-               if (iscntrl(c)) {
-                       *dst++ = '^';
-                       if (c == 0177)
-                               *dst++ = '?';
-                       else
-                               *dst++ = (char)c + '@';
-               } else {
-                       *dst++ = '-'; *dst++ = (char)c;
-               }
-       }
-       return dst;
-}
-
-char *
-dhcpcd_vis(char *dst, int c, int flag, int nextc)
-{
-
-       return dhcpcd_svis(dst, c, flag, nextc, "");
-}
index 0b03b2cafa0c61456ed63be4bd799bfaa177335e..07b125835400c051e7d2c5f6a1e196676f07c7b6 100644 (file)
@@ -29,6 +29,7 @@
 #include <sys/un.h>
 
 #include <assert.h>
+#include <ctype.h>
 #include <errno.h>
 #include <limits.h>
 #include <poll.h>
@@ -191,6 +192,83 @@ dhcpcd_strtoi(int *val, const char *s)
                errno = ERANGE;
 }
 
+static int
+dhcpcd_wpa_hex2num(char c)
+{
+
+       if (c >= '0' && c <= '9')
+               return c - '0';
+       if (c >= 'a' && c <= 'f')
+               return c - 'a' + 10;
+       if (c >= 'A' && c <= 'F')
+               return c - 'A' + 10;
+       return -1;
+}
+
+static int
+dhcpcd_wpa_hex2byte(const char *src)
+{
+       int h, l;
+
+       if ((h = dhcpcd_wpa_hex2num(*src++)) == -1 ||
+           (l = dhcpcd_wpa_hex2num(*src)) == -1)
+               return -1;
+       return (h << 4) | l;
+}
+
+static ssize_t
+dhcpcd_wpa_decode_ssid(char *dst, size_t dlen, const char *src)
+{
+       const char *start;
+       char c, esc;
+       int xb;
+
+       start = dst;
+       for (;;) {
+               if (*src == '\0')
+                       break;
+               if (--dlen == 0) {
+                       errno = ENOSPC;
+                       return -1;
+               }
+               c = *src++;
+               switch (c) {
+               case '\\':
+                       if (*src == '\0') {
+                               errno = EINVAL;
+                               return -1;
+                       }
+                       esc = *src++;
+                       switch (esc) {
+                       case '\\':
+                       case '"': *dst++ = esc; break;
+                       case 'n': *dst++ = '\n'; break;
+                       case 'r': *dst++ = '\r'; break;
+                       case 't': *dst++ = '\t'; break;
+                       case 'e': *dst++ = '\033'; break;
+                       case 'x':
+                               if (src[0] == '\0' || src[1] == '\0') {
+                                       errno = EINVAL;
+                                       return -1;
+                               }
+                               if ((xb = dhcpcd_wpa_hex2byte(src)) == -1)
+                                       return -1;
+                               *dst++ = (char)xb;
+                               src += 2;
+                               break;
+                       default: errno = EINVAL; return -1;
+                       }
+               default: *dst++ = c; break;
+               }
+       }
+       if (dlen == 0) {
+               errno = ENOSPC;
+               return -1;
+       }
+       *dst = '\0';
+       return dst - start;
+}
+
 static DHCPCD_WI_SCAN *
 dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
 {
@@ -198,7 +276,7 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
        ssize_t bytes, dl;
        DHCPCD_WI_SCAN *wis, *w, *l;
        char *s, *p, buf[32];
-       char wssid[sizeof(w->ssid)], tssid[sizeof(w->ssid)];
+       char wssid[sizeof(w->ssid)];
 
        if (!dhcpcd_realloc(wpa->con, 2048))
                return NULL;
@@ -214,11 +292,8 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
                w = calloc(1, sizeof(*w));
                if (w == NULL)
                        break;
-               if (wis == NULL)
-                       wis = w;
-               else
-                       l->next = w;
-               l = w;
+               dl = 0;
+               wssid[0] = '\0';
                while ((s = strsep(&p, "\n"))) {
                        if (*s == '\0')
                                continue;
@@ -239,12 +314,26 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
                        else if (strncmp(s, "ssid=", 5) == 0) {
                                /* Decode it from \xNN to \NNN
                                 * so we're consistent */
-                               strlcpy(wssid, s + 5, sizeof(wssid));
-                               dl = dhcpcd_decode(tssid, sizeof(tssid), wssid);
-                               dhcpcd_encode(w->ssid, sizeof(w->ssid),
-                                   tssid, (size_t)dl);
+                               dl = dhcpcd_wpa_decode_ssid(wssid,
+                                   sizeof(wssid), s + 5);
+                               if (dl == -1)
+                                       break;
+                               dl = dhcpcd_encode_string_escape(w->ssid,
+                                   sizeof(w->ssid), wssid, (size_t)dl);
+                               if (dl == -1)
+                                       break;
                        }
                }
+               if (dl == -1) {
+                       free(w);
+                       break;
+               }
+
+               if (wis == NULL)
+                       wis = w;
+               else
+                       l->next = w;
+               l = w;
 
                w->strength.value = w->level.value;
                if (w->strength.value > 110 && w->strength.value < 256)
@@ -261,7 +350,6 @@ dhcpcd_wpa_scans_read(DHCPCD_WPA *wpa)
                        /* Assume quality percentage */
                        w->strength.value = CLAMP(w->strength.value, 0, 100);
                }
-
        }
        return wis;
 }
@@ -417,6 +505,7 @@ static int
 dhcpcd_wpa_network_find(DHCPCD_WPA *wpa, const char *fssid)
 {
        ssize_t bytes, dl, tl;
+       size_t fl;
        char *s, *t, *ssid, *bssid, *flags;
        char dssid[IF_SSIDSIZE], tssid[IF_SSIDSIZE];
        long l;
@@ -427,8 +516,7 @@ dhcpcd_wpa_network_find(DHCPCD_WPA *wpa, const char *fssid)
        if (bytes == 0 || bytes == -1)
                return -1;
 
-       if ((dl = dhcpcd_decode(dssid, sizeof(dssid), fssid)) == -1)
-               return -1;
+       fl = strlen(fssid);
 
        s = strchr(wpa->con->buf, '\n');
        if (s == NULL)
@@ -453,10 +541,18 @@ dhcpcd_wpa_network_find(DHCPCD_WPA *wpa, const char *fssid)
                        errno = ERANGE;
                        break;
                }
-
-               if ((tl = dhcpcd_decode(tssid, sizeof(tssid), ssid)) == -1)
+       
+               /* Decode the wpa_supplicant SSID into raw chars and
+                * then encode into our octal escaped string to
+                * compare. */
+               dl = dhcpcd_wpa_decode_ssid(dssid, sizeof(dssid), ssid);
+               if (dl == -1)
                        return -1;
-               if (tl == dl && memcmp(tssid, dssid, (size_t)tl) == 0)
+               tl = dhcpcd_encode_string_escape(tssid,
+                   sizeof(tssid), dssid, (size_t)dl);
+               if (tl == -1)
+                       return -1;
+               if ((size_t)tl == fl && memcmp(tssid, fssid, (size_t)tl) == 0)
                        return (int)l;
        }
        errno = ENOENT;
@@ -482,24 +578,28 @@ dhcpcd_wpa_network_new(DHCPCD_WPA *wpa)
        return (int)l;
 }
 
-static const char hexstr[] = "0123456789ABCDEF";
+static const char hexstr[] = "0123456789abcdef";
 int
 dhcpcd_wpa_network_find_new(DHCPCD_WPA *wpa, const char *ssid)
 {
        int id;
        char dssid[IF_SSIDSIZE], essid[IF_SSIDSIZE], *ep;
-       ssize_t dl;
+       ssize_t dl, i;
 
        id = dhcpcd_wpa_network_find(wpa, ssid);
        if (id != -1)
                return id;
 
-       dl = dhcpcd_decode(dssid, sizeof(dssid), ssid);
+       dl = dhcpcd_decode_string_escape(dssid, sizeof(dssid), ssid);
        if (dl == -1)
                return -1;
 
+       for (i = 0; i < dl; i++) {
+               if (!isascii(dssid[i]) && !isprint(dssid[i]))
+                       break;
+       }
        ep = essid;
-       if ((size_t)dl != strlen(ssid) || memcmp(dssid, ssid, (size_t)dl)) {
+       if (i < dl) {
                /* Non standard characters found! Encode as hex string */
                char *dp;
                unsigned char c;
@@ -512,7 +612,7 @@ dhcpcd_wpa_network_find_new(DHCPCD_WPA *wpa, const char *ssid)
                }
        } else {
                *ep++ = '\"';
-               ep = stpcpy(ep, ssid);
+               ep = stpcpy(ep, dssid);
                *ep++ = '\"';
        }
        *ep = '\0';