Blame


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