]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
Update vis.h and vis.c from OpenBSD.
authorDarren Tucker <dtucker@zip.com.au>
Fri, 3 Jun 2016 06:03:44 +0000 (16:03 +1000)
committerDarren Tucker <dtucker@zip.com.au>
Fri, 3 Jun 2016 06:03:44 +0000 (16:03 +1000)
This will be needed for the upcoming utf8 changes.

openbsd-compat/vis.c
openbsd-compat/vis.h

index f6f5665c103fe6161ab08a15d6316f2a812ad5bf..3cef6bafdf12bf55fea666d869ba4e6c505507aa 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vis.c,v 1.19 2005/09/01 17:15:49 millert Exp $ */
+/*     $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
 /*-
  * Copyright (c) 1989, 1993
  *     The Regents of the University of California.  All rights reserved.
 #include "includes.h"
 #if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS)
 
+/*
+ * We want these to override in the BROKEN_STRNVIS case.  TO avoid future sync
+ * problems no-op out the weak symbol definition rather than remove it.
+ */
+#define DEF_WEAK(x)
+
+#include <sys/types.h>
+#include <errno.h>
 #include <ctype.h>
+#include <limits.h>
 #include <string.h>
+#include <stdlib.h>
 
 #include "vis.h"
 
 #define        isoctal(c)      (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
-#define        isvisible(c)                                                    \
+#define        isvisible(c,flag)                                               \
+       (((c) == '\\' || (flag & VIS_ALL) == 0) &&                      \
        (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) &&            \
        (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') ||      \
                (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) ||     \
@@ -48,7 +59,7 @@
        ((flag & VIS_NL) == 0 && (c) == '\n') ||                        \
        ((flag & VIS_SAFE) && ((c) == '\b' ||                           \
                (c) == '\007' || (c) == '\r' ||                         \
-               isgraph((u_char)(c)))))
+               isgraph((u_char)(c))))))
 
 /*
  * vis - visually encode characters
 char *
 vis(char *dst, int c, int flag, int nextc)
 {
-       if (isvisible(c)) {
-               *dst++ = c;
-               if (c == '\\' && (flag & VIS_NOSLASH) == 0)
+       if (isvisible(c, flag)) {
+               if ((c == '"' && (flag & VIS_DQ) != 0) ||
+                   (c == '\\' && (flag & VIS_NOSLASH) == 0))
                        *dst++ = '\\';
+               *dst++ = c;
                *dst = '\0';
                return (dst);
        }
@@ -136,6 +148,7 @@ done:
        *dst = '\0';
        return (dst);
 }
+DEF_WEAK(vis);
 
 /*
  * strvis, strnvis, strvisx - visually encode characters from src into dst
@@ -161,6 +174,7 @@ strvis(char *dst, const char *src, int flag)
        *dst = '\0';
        return (dst - start);
 }
+DEF_WEAK(strvis);
 
 int
 strnvis(char *dst, const char *src, size_t siz, int flag)
@@ -171,19 +185,18 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
 
        i = 0;
        for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
-               if (isvisible(c)) {
-                       i = 1;
-                       *dst++ = c;
-                       if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
+               if (isvisible(c, flag)) {
+                       if ((c == '"' && (flag & VIS_DQ) != 0) ||
+                           (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
                                /* need space for the extra '\\' */
-                               if (dst < end)
-                                       *dst++ = '\\';
-                               else {
-                                       dst--;
+                               if (dst + 1 >= end) {
                                        i = 2;
                                        break;
                                }
+                               *dst++ = '\\';
                        }
+                       i = 1;
+                       *dst++ = c;
                        src++;
                } else {
                        i = vis(tbuf, c, flag, *++src) - tbuf;
@@ -206,6 +219,25 @@ strnvis(char *dst, const char *src, size_t siz, int flag)
        return (dst - start);
 }
 
+int
+stravis(char **outp, const char *src, int flag)
+{
+       char *buf;
+       int len, serrno;
+
+       buf = reallocarray(NULL, 4, strlen(src) + 1);
+       if (buf == NULL)
+               return -1;
+       len = strvis(buf, src, flag);
+       serrno = errno;
+       *outp = realloc(buf, len + 1);
+       if (*outp == NULL) {
+               *outp = buf;
+               errno = serrno;
+       }
+       return (len);
+}
+
 int
 strvisx(char *dst, const char *src, size_t len, int flag)
 {
index d1286c99df3f3f9e2ce5ff8173a9c5b273a033d7..2cdfd364b64adab6f8b7463112c362b6bc3c1f24 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: vis.h,v 1.11 2005/08/09 19:38:31 millert Exp $        */
+/*     $OpenBSD: vis.h,v 1.15 2015/07/20 01:52:27 millert Exp $        */
 /*     $NetBSD: vis.h,v 1.4 1994/10/26 00:56:41 cgd Exp $      */
 
 /*-
@@ -58,6 +58,8 @@
 #define        VIS_NL          0x10    /* also encode newline */
 #define        VIS_WHITE       (VIS_SP | VIS_TAB | VIS_NL)
 #define        VIS_SAFE        0x20    /* only encode "unsafe" characters */
+#define        VIS_DQ          0x200   /* backslash-escape double quotes */
+#define        VIS_ALL         0x400   /* encode all characters */
 
 /*
  * other
@@ -81,6 +83,7 @@
 
 char   *vis(char *, int, int, int);
 int    strvis(char *, const char *, int);
+int    stravis(char **, const char *, int);
 int    strnvis(char *, const char *, size_t, int)
                __attribute__ ((__bounded__(__string__,1,3)));
 int    strvisx(char *, const char *, size_t, int)