1/*	$OpenBSD: vis.c,v 1.25 2015/09/13 11:32:51 guenther Exp $ */
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.
14 * 3. Neither the name of the University nor the names of its contributors
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 */
30
31/* OPENBSD ORIGINAL: lib/libc/gen/vis.c */
32
33#include "includes.h"
34#if !defined(HAVE_STRNVIS) || defined(BROKEN_STRNVIS)
35
36#include <sys/types.h>
37#include <errno.h>
38#include <ctype.h>
39#include <limits.h>
40#include <string.h>
41#include <stdlib.h>
42
43#include "vis.h"
44
45#define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
46#define	isvisible(c,flag)						\
47	(((c) == '\\' || (flag & VIS_ALL) == 0) &&			\
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' ||				\
56		isgraph((u_char)(c))))))
57
58/*
59 * vis - visually encode characters
60 */
61char *
62vis(char *dst, int c, int flag, int nextc)
63{
64	if (isvisible(c, flag)) {
65		if ((c == '"' && (flag & VIS_DQ) != 0) ||
66		    (c == '\\' && (flag & VIS_NOSLASH) == 0))
67			*dst++ = '\\';
68		*dst++ = c;
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	}
117	if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
118	    ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
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	}
131	if (iscntrl((u_char)c)) {
132		*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}
145DEF_WEAK(vis);
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 */
160int
161strvis(char *dst, const char *src, int flag)
162{
163	char c;
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}
171DEF_WEAK(strvis);
172
173int
174strnvis(char *dst, const char *src, size_t siz, int flag)
175{
176	char *start, *end;
177	char tbuf[5];
178	int c, i;
179
180	i = 0;
181	for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
182		if (isvisible(c, flag)) {
183			if ((c == '"' && (flag & VIS_DQ) != 0) ||
184			    (c == '\\' && (flag & VIS_NOSLASH) == 0)) {
185				/* need space for the extra '\\' */
186				if (dst + 1 >= end) {
187					i = 2;
188					break;
189				}
190				*dst++ = '\\';
191			}
192			i = 1;
193			*dst++ = c;
194			src++;
195		} else {
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--;
202				break;
203			}
204		}
205	}
206	if (siz > 0)
207		*dst = '\0';
208	if (dst + i > end) {
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
216int
217stravis(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
236strvisx(char *dst, const char *src, size_t len, int flag)
237{
238	char c;
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
252