1 0d6fb46a 2022-01-09 op /* $OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
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.
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
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.
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.
31 0d6fb46a 2022-01-09 op #include "compat.h"
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>
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))))))
54 0d6fb46a 2022-01-09 op * vis - visually encode characters
57 0d6fb46a 2022-01-09 op vis(char *dst, int c, int flag, int nextc)
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++ = '\\';
68 0d6fb46a 2022-01-09 op if (flag & VIS_CSTYLE) {
71 0d6fb46a 2022-01-09 op *dst++ = '\\';
75 0d6fb46a 2022-01-09 op *dst++ = '\\';
79 0d6fb46a 2022-01-09 op *dst++ = '\\';
83 0d6fb46a 2022-01-09 op *dst++ = '\\';
87 0d6fb46a 2022-01-09 op *dst++ = '\\';
91 0d6fb46a 2022-01-09 op *dst++ = '\\';
95 0d6fb46a 2022-01-09 op *dst++ = '\\';
99 0d6fb46a 2022-01-09 op *dst++ = '\\';
100 0d6fb46a 2022-01-09 op *dst++ = 's';
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';
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';
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) {
124 0d6fb46a 2022-01-09 op *dst++ = 'M';
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++ = '?';
131 0d6fb46a 2022-01-09 op *dst++ = c + '@';
133 0d6fb46a 2022-01-09 op *dst++ = '-';
138 0d6fb46a 2022-01-09 op return (dst);
142 0d6fb46a 2022-01-09 op * strvis, strnvis, strvisx - visually encode characters from src into dst
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.
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.
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.
155 0d6fb46a 2022-01-09 op strvis(char *dst, const char *src, int flag)
160 0d6fb46a 2022-01-09 op for (start = dst; (c = *src);)
161 0d6fb46a 2022-01-09 op dst = vis(dst, c, flag, *++src);
163 0d6fb46a 2022-01-09 op return (dst - start);
167 0d6fb46a 2022-01-09 op strnvis(char *dst, const char *src, size_t siz, int flag)
169 0d6fb46a 2022-01-09 op char *start, *end;
170 0d6fb46a 2022-01-09 op char tbuf[5];
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) {
183 0d6fb46a 2022-01-09 op *dst++ = '\\';
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);
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;
206 0d6fb46a 2022-01-09 op return (dst - start);
210 0d6fb46a 2022-01-09 op stravis(char **outp, const char *src, int flag)
213 0d6fb46a 2022-01-09 op int len, serrno;
215 0d6fb46a 2022-01-09 op buf = reallocarray(NULL, 4, strlen(src) + 1);
216 0d6fb46a 2022-01-09 op if (buf == NULL)
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) {
223 0d6fb46a 2022-01-09 op errno = serrno;
225 0d6fb46a 2022-01-09 op return (len);
229 0d6fb46a 2022-01-09 op strvisx(char *dst, const char *src, size_t len, int flag)
234 0d6fb46a 2022-01-09 op for (start = dst; len > 1; len--) {
236 0d6fb46a 2022-01-09 op dst = vis(dst, c, flag, *++src);
239 0d6fb46a 2022-01-09 op dst = vis(dst, *src, flag, '\0');
241 0d6fb46a 2022-01-09 op return (dst - start);