Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 1 | /* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */ |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 2 | /*- |
| 3 | * Copyright (c) 1989, 1993 |
| 4 | * The Regents of the University of California. All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
Damien Miller | 329638e | 2003-06-03 12:12:50 +1000 | [diff] [blame] | 14 | * 3. Neither the name of the University nor the names of its contributors |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 15 | * may be used to endorse or promote products derived from this software |
| 16 | * without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 30 | |
Darren Tucker | 7f24a0e | 2005-11-10 16:18:56 +1100 | [diff] [blame] | 31 | /* OPENBSD ORIGINAL: lib/libc/gen/vis.c */ |
| 32 | |
Ben Lindstrom | 515d0f9 | 2003-08-29 16:59:52 +0000 | [diff] [blame] | 33 | #include "includes.h" |
Damien Miller | 63b4bcd | 2013-03-20 12:55:14 +1100 | [diff] [blame] | 34 | #if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 35 | |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <errno.h> |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 38 | #include <ctype.h> |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 39 | #include <limits.h> |
Damien Miller | 3174125 | 2003-05-19 00:13:38 +1000 | [diff] [blame] | 40 | #include <string.h> |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 41 | #include <stdlib.h> |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 42 | |
| 43 | #include "vis.h" |
| 44 | |
| 45 | #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7') |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 46 | #define isvisible(c,flag) \ |
| 47 | (((c) == '\\' || (flag & VIS_ALL) == 0) && \ |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 48 | (((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) && \ |
| 49 | (((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') || \ |
| 50 | (flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) || \ |
| 51 | ((flag & VIS_SP) == 0 && (c) == ' ') || \ |
| 52 | ((flag & VIS_TAB) == 0 && (c) == '\t') || \ |
| 53 | ((flag & VIS_NL) == 0 && (c) == '\n') || \ |
| 54 | ((flag & VIS_SAFE) && ((c) == '\b' || \ |
| 55 | (c) == '\007' || (c) == '\r' || \ |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 56 | isgraph((u_char)(c)))))) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * vis - visually encode characters |
| 60 | */ |
| 61 | char * |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 62 | vis(char *dst, int c, int flag, int nextc) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 63 | { |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 64 | if (isvisible(c, flag)) { |
| 65 | if ((c == '"' && (flag & VIS_DQ) != 0) || |
| 66 | (c == '\\' && (flag & VIS_NOSLASH) == 0)) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 67 | *dst++ = '\\'; |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 68 | *dst++ = c; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 69 | *dst = '\0'; |
| 70 | return (dst); |
| 71 | } |
| 72 | |
| 73 | if (flag & VIS_CSTYLE) { |
| 74 | switch(c) { |
| 75 | case '\n': |
| 76 | *dst++ = '\\'; |
| 77 | *dst++ = 'n'; |
| 78 | goto done; |
| 79 | case '\r': |
| 80 | *dst++ = '\\'; |
| 81 | *dst++ = 'r'; |
| 82 | goto done; |
| 83 | case '\b': |
| 84 | *dst++ = '\\'; |
| 85 | *dst++ = 'b'; |
| 86 | goto done; |
| 87 | case '\a': |
| 88 | *dst++ = '\\'; |
| 89 | *dst++ = 'a'; |
| 90 | goto done; |
| 91 | case '\v': |
| 92 | *dst++ = '\\'; |
| 93 | *dst++ = 'v'; |
| 94 | goto done; |
| 95 | case '\t': |
| 96 | *dst++ = '\\'; |
| 97 | *dst++ = 't'; |
| 98 | goto done; |
| 99 | case '\f': |
| 100 | *dst++ = '\\'; |
| 101 | *dst++ = 'f'; |
| 102 | goto done; |
| 103 | case ' ': |
| 104 | *dst++ = '\\'; |
| 105 | *dst++ = 's'; |
| 106 | goto done; |
| 107 | case '\0': |
| 108 | *dst++ = '\\'; |
| 109 | *dst++ = '0'; |
| 110 | if (isoctal(nextc)) { |
| 111 | *dst++ = '0'; |
| 112 | *dst++ = '0'; |
| 113 | } |
| 114 | goto done; |
| 115 | } |
| 116 | } |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 117 | if (((c & 0177) == ' ') || (flag & VIS_OCTAL) || |
| 118 | ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) { |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 119 | *dst++ = '\\'; |
| 120 | *dst++ = ((u_char)c >> 6 & 07) + '0'; |
| 121 | *dst++ = ((u_char)c >> 3 & 07) + '0'; |
| 122 | *dst++ = ((u_char)c & 07) + '0'; |
| 123 | goto done; |
| 124 | } |
| 125 | if ((flag & VIS_NOSLASH) == 0) |
| 126 | *dst++ = '\\'; |
| 127 | if (c & 0200) { |
| 128 | c &= 0177; |
| 129 | *dst++ = 'M'; |
| 130 | } |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 131 | if (iscntrl((u_char)c)) { |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 132 | *dst++ = '^'; |
| 133 | if (c == 0177) |
| 134 | *dst++ = '?'; |
| 135 | else |
| 136 | *dst++ = c + '@'; |
| 137 | } else { |
| 138 | *dst++ = '-'; |
| 139 | *dst++ = c; |
| 140 | } |
| 141 | done: |
| 142 | *dst = '\0'; |
| 143 | return (dst); |
| 144 | } |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 145 | DEF_WEAK(vis); |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 146 | |
| 147 | /* |
| 148 | * strvis, strnvis, strvisx - visually encode characters from src into dst |
| 149 | * |
| 150 | * Dst must be 4 times the size of src to account for possible |
| 151 | * expansion. The length of dst, not including the trailing NULL, |
| 152 | * is returned. |
| 153 | * |
| 154 | * Strnvis will write no more than siz-1 bytes (and will NULL terminate). |
| 155 | * The number of bytes needed to fully encode the string is returned. |
| 156 | * |
| 157 | * Strvisx encodes exactly len bytes from src into dst. |
| 158 | * This is useful for encoding a block of data. |
| 159 | */ |
| 160 | int |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 161 | strvis(char *dst, const char *src, int flag) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 162 | { |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 163 | char c; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 164 | char *start; |
| 165 | |
| 166 | for (start = dst; (c = *src);) |
| 167 | dst = vis(dst, c, flag, *++src); |
| 168 | *dst = '\0'; |
| 169 | return (dst - start); |
| 170 | } |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 171 | DEF_WEAK(strvis); |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 172 | |
| 173 | int |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 174 | strnvis(char *dst, const char *src, size_t siz, int flag) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 175 | { |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 176 | char *start, *end; |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 177 | char tbuf[5]; |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 178 | int c, i; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 179 | |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 180 | i = 0; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 181 | for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) { |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 182 | if (isvisible(c, flag)) { |
| 183 | if ((c == '"' && (flag & VIS_DQ) != 0) || |
| 184 | (c == '\\' && (flag & VIS_NOSLASH) == 0)) { |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 185 | /* need space for the extra '\\' */ |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 186 | if (dst + 1 >= end) { |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 187 | i = 2; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 188 | break; |
| 189 | } |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 190 | *dst++ = '\\'; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 191 | } |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 192 | i = 1; |
| 193 | *dst++ = c; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 194 | src++; |
| 195 | } else { |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 196 | i = vis(tbuf, c, flag, *++src) - tbuf; |
| 197 | if (dst + i <= end) { |
| 198 | memcpy(dst, tbuf, i); |
| 199 | dst += i; |
| 200 | } else { |
| 201 | src--; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 202 | break; |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 203 | } |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 204 | } |
| 205 | } |
Damien Miller | e323df6 | 2003-05-18 22:24:09 +1000 | [diff] [blame] | 206 | if (siz > 0) |
| 207 | *dst = '\0'; |
| 208 | if (dst + i > end) { |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 209 | /* adjust return value for truncation */ |
| 210 | while ((c = *src)) |
| 211 | dst += vis(tbuf, c, flag, *++src) - tbuf; |
| 212 | } |
| 213 | return (dst - start); |
| 214 | } |
| 215 | |
| 216 | int |
Darren Tucker | ae9c0d4 | 2016-06-03 16:03:44 +1000 | [diff] [blame] | 217 | stravis(char **outp, const char *src, int flag) |
| 218 | { |
| 219 | char *buf; |
| 220 | int len, serrno; |
| 221 | |
| 222 | buf = reallocarray(NULL, 4, strlen(src) + 1); |
| 223 | if (buf == NULL) |
| 224 | return -1; |
| 225 | len = strvis(buf, src, flag); |
| 226 | serrno = errno; |
| 227 | *outp = realloc(buf, len + 1); |
| 228 | if (*outp == NULL) { |
| 229 | *outp = buf; |
| 230 | errno = serrno; |
| 231 | } |
| 232 | return (len); |
| 233 | } |
| 234 | |
| 235 | int |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 236 | strvisx(char *dst, const char *src, size_t len, int flag) |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 237 | { |
Darren Tucker | 9ac1a65 | 2005-10-09 11:40:03 +1000 | [diff] [blame] | 238 | char c; |
Damien Miller | b93addb | 2003-01-07 17:04:18 +1100 | [diff] [blame] | 239 | char *start; |
| 240 | |
| 241 | for (start = dst; len > 1; len--) { |
| 242 | c = *src; |
| 243 | dst = vis(dst, c, flag, *++src); |
| 244 | } |
| 245 | if (len) |
| 246 | dst = vis(dst, *src, flag, '\0'); |
| 247 | *dst = '\0'; |
| 248 | return (dst - start); |
| 249 | } |
| 250 | |
| 251 | #endif |