blob: 0e04ed025dde2dbca976cc6e5df3e6c0d88bbc53 [file] [log] [blame]
Darren Tuckerae9c0d42016-06-03 16:03:44 +10001/* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
Damien Millerb93addb2003-01-07 17:04:18 +11002/*-
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 Miller329638e2003-06-03 12:12:50 +100014 * 3. Neither the name of the University nor the names of its contributors
Damien Millerb93addb2003-01-07 17:04:18 +110015 * 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 Tucker9ac1a652005-10-09 11:40:03 +100030
Darren Tucker7f24a0e2005-11-10 16:18:56 +110031/* OPENBSD ORIGINAL: lib/libc/gen/vis.c */
32
Ben Lindstrom515d0f92003-08-29 16:59:52 +000033#include "includes.h"
Damien Miller63b4bcd2013-03-20 12:55:14 +110034#if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS)
Damien Millerb93addb2003-01-07 17:04:18 +110035
Darren Tuckerae9c0d42016-06-03 16:03:44 +100036#include <sys/types.h>
37#include <errno.h>
Damien Millerb93addb2003-01-07 17:04:18 +110038#include <ctype.h>
Darren Tuckerae9c0d42016-06-03 16:03:44 +100039#include <limits.h>
Damien Miller31741252003-05-19 00:13:38 +100040#include <string.h>
Darren Tuckerae9c0d42016-06-03 16:03:44 +100041#include <stdlib.h>
Damien Millerb93addb2003-01-07 17:04:18 +110042
43#include "vis.h"
44
45#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
Darren Tuckerae9c0d42016-06-03 16:03:44 +100046#define isvisible(c,flag) \
47 (((c) == '\\' || (flag & VIS_ALL) == 0) && \
Darren Tucker9ac1a652005-10-09 11:40:03 +100048 (((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 Tuckerae9c0d42016-06-03 16:03:44 +100056 isgraph((u_char)(c))))))
Damien Millerb93addb2003-01-07 17:04:18 +110057
58/*
59 * vis - visually encode characters
60 */
61char *
Darren Tucker9ac1a652005-10-09 11:40:03 +100062vis(char *dst, int c, int flag, int nextc)
Damien Millerb93addb2003-01-07 17:04:18 +110063{
Darren Tuckerae9c0d42016-06-03 16:03:44 +100064 if (isvisible(c, flag)) {
65 if ((c == '"' && (flag & VIS_DQ) != 0) ||
66 (c == '\\' && (flag & VIS_NOSLASH) == 0))
Damien Millerb93addb2003-01-07 17:04:18 +110067 *dst++ = '\\';
Darren Tuckerae9c0d42016-06-03 16:03:44 +100068 *dst++ = c;
Damien Millerb93addb2003-01-07 17:04:18 +110069 *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 Tucker9ac1a652005-10-09 11:40:03 +1000117 if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
118 ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
Damien Millerb93addb2003-01-07 17:04:18 +1100119 *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 Tucker9ac1a652005-10-09 11:40:03 +1000131 if (iscntrl((u_char)c)) {
Damien Millerb93addb2003-01-07 17:04:18 +1100132 *dst++ = '^';
133 if (c == 0177)
134 *dst++ = '?';
135 else
136 *dst++ = c + '@';
137 } else {
138 *dst++ = '-';
139 *dst++ = c;
140 }
141done:
142 *dst = '\0';
143 return (dst);
144}
Darren Tuckerae9c0d42016-06-03 16:03:44 +1000145DEF_WEAK(vis);
Damien Millerb93addb2003-01-07 17:04:18 +1100146
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 */
160int
Darren Tucker9ac1a652005-10-09 11:40:03 +1000161strvis(char *dst, const char *src, int flag)
Damien Millerb93addb2003-01-07 17:04:18 +1100162{
Darren Tucker9ac1a652005-10-09 11:40:03 +1000163 char c;
Damien Millerb93addb2003-01-07 17:04:18 +1100164 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 Tuckerae9c0d42016-06-03 16:03:44 +1000171DEF_WEAK(strvis);
Damien Millerb93addb2003-01-07 17:04:18 +1100172
173int
Darren Tucker9ac1a652005-10-09 11:40:03 +1000174strnvis(char *dst, const char *src, size_t siz, int flag)
Damien Millerb93addb2003-01-07 17:04:18 +1100175{
Damien Millerb93addb2003-01-07 17:04:18 +1100176 char *start, *end;
Damien Millere323df62003-05-18 22:24:09 +1000177 char tbuf[5];
Darren Tucker9ac1a652005-10-09 11:40:03 +1000178 int c, i;
Damien Millerb93addb2003-01-07 17:04:18 +1100179
Damien Millere323df62003-05-18 22:24:09 +1000180 i = 0;
Damien Millerb93addb2003-01-07 17:04:18 +1100181 for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
Darren Tuckerae9c0d42016-06-03 16:03:44 +1000182 if (isvisible(c, flag)) {
183 if ((c == '"' && (flag & VIS_DQ) != 0) ||
184 (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
Damien Millerb93addb2003-01-07 17:04:18 +1100185 /* need space for the extra '\\' */
Darren Tuckerae9c0d42016-06-03 16:03:44 +1000186 if (dst + 1 >= end) {
Damien Millere323df62003-05-18 22:24:09 +1000187 i = 2;
Damien Millerb93addb2003-01-07 17:04:18 +1100188 break;
189 }
Darren Tuckerae9c0d42016-06-03 16:03:44 +1000190 *dst++ = '\\';
Damien Millerb93addb2003-01-07 17:04:18 +1100191 }
Darren Tuckerae9c0d42016-06-03 16:03:44 +1000192 i = 1;
193 *dst++ = c;
Damien Millerb93addb2003-01-07 17:04:18 +1100194 src++;
195 } else {
Damien Millere323df62003-05-18 22:24:09 +1000196 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 Millerb93addb2003-01-07 17:04:18 +1100202 break;
Damien Millere323df62003-05-18 22:24:09 +1000203 }
Damien Millerb93addb2003-01-07 17:04:18 +1100204 }
205 }
Damien Millere323df62003-05-18 22:24:09 +1000206 if (siz > 0)
207 *dst = '\0';
208 if (dst + i > end) {
Damien Millerb93addb2003-01-07 17:04:18 +1100209 /* adjust return value for truncation */
210 while ((c = *src))
211 dst += vis(tbuf, c, flag, *++src) - tbuf;
212 }
213 return (dst - start);
214}
215
216int
Darren Tuckerae9c0d42016-06-03 16:03:44 +1000217stravis(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
235int
Darren Tucker9ac1a652005-10-09 11:40:03 +1000236strvisx(char *dst, const char *src, size_t len, int flag)
Damien Millerb93addb2003-01-07 17:04:18 +1100237{
Darren Tucker9ac1a652005-10-09 11:40:03 +1000238 char c;
Damien Millerb93addb2003-01-07 17:04:18 +1100239 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