xref: /openbsd/lib/libc/gen/vis.c (revision cca36db2)
1 /*	$OpenBSD: vis.c,v 1.22 2011/03/13 22:21:32 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 #include <sys/types.h>
32 #include <limits.h>
33 #include <ctype.h>
34 #include <string.h>
35 #include <vis.h>
36 
37 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
38 #define	isvisible(c,flag)						\
39 	(((c) == '\\' || (flag & VIS_ALL) == 0) &&			\
40 	(((u_int)(c) <= UCHAR_MAX && isascii((u_char)(c)) &&		\
41 	(((c) != '*' && (c) != '?' && (c) != '[' && (c) != '#') ||	\
42 		(flag & VIS_GLOB) == 0) && isgraph((u_char)(c))) ||	\
43 	((flag & VIS_SP) == 0 && (c) == ' ') ||				\
44 	((flag & VIS_TAB) == 0 && (c) == '\t') ||			\
45 	((flag & VIS_NL) == 0 && (c) == '\n') ||			\
46 	((flag & VIS_SAFE) && ((c) == '\b' ||				\
47 		(c) == '\007' || (c) == '\r' ||				\
48 		isgraph((u_char)(c))))))
49 
50 /*
51  * vis - visually encode characters
52  */
53 char *
54 vis(char *dst, int c, int flag, int nextc)
55 {
56 	if (isvisible(c, flag)) {
57 		*dst++ = c;
58 		if (c == '\\' && (flag & VIS_NOSLASH) == 0)
59 			*dst++ = '\\';
60 		*dst = '\0';
61 		return (dst);
62 	}
63 
64 	if (flag & VIS_CSTYLE) {
65 		switch(c) {
66 		case '\n':
67 			*dst++ = '\\';
68 			*dst++ = 'n';
69 			goto done;
70 		case '\r':
71 			*dst++ = '\\';
72 			*dst++ = 'r';
73 			goto done;
74 		case '\b':
75 			*dst++ = '\\';
76 			*dst++ = 'b';
77 			goto done;
78 		case '\a':
79 			*dst++ = '\\';
80 			*dst++ = 'a';
81 			goto done;
82 		case '\v':
83 			*dst++ = '\\';
84 			*dst++ = 'v';
85 			goto done;
86 		case '\t':
87 			*dst++ = '\\';
88 			*dst++ = 't';
89 			goto done;
90 		case '\f':
91 			*dst++ = '\\';
92 			*dst++ = 'f';
93 			goto done;
94 		case ' ':
95 			*dst++ = '\\';
96 			*dst++ = 's';
97 			goto done;
98 		case '\0':
99 			*dst++ = '\\';
100 			*dst++ = '0';
101 			if (isoctal(nextc)) {
102 				*dst++ = '0';
103 				*dst++ = '0';
104 			}
105 			goto done;
106 		}
107 	}
108 	if (((c & 0177) == ' ') || (flag & VIS_OCTAL) ||
109 	    ((flag & VIS_GLOB) && (c == '*' || c == '?' || c == '[' || c == '#'))) {
110 		*dst++ = '\\';
111 		*dst++ = ((u_char)c >> 6 & 07) + '0';
112 		*dst++ = ((u_char)c >> 3 & 07) + '0';
113 		*dst++ = ((u_char)c & 07) + '0';
114 		goto done;
115 	}
116 	if ((flag & VIS_NOSLASH) == 0)
117 		*dst++ = '\\';
118 	if (c & 0200) {
119 		c &= 0177;
120 		*dst++ = 'M';
121 	}
122 	if (iscntrl((u_char)c)) {
123 		*dst++ = '^';
124 		if (c == 0177)
125 			*dst++ = '?';
126 		else
127 			*dst++ = c + '@';
128 	} else {
129 		*dst++ = '-';
130 		*dst++ = c;
131 	}
132 done:
133 	*dst = '\0';
134 	return (dst);
135 }
136 
137 /*
138  * strvis, strnvis, strvisx - visually encode characters from src into dst
139  *
140  *	Dst must be 4 times the size of src to account for possible
141  *	expansion.  The length of dst, not including the trailing NULL,
142  *	is returned.
143  *
144  *	Strnvis will write no more than siz-1 bytes (and will NULL terminate).
145  *	The number of bytes needed to fully encode the string is returned.
146  *
147  *	Strvisx encodes exactly len bytes from src into dst.
148  *	This is useful for encoding a block of data.
149  */
150 int
151 strvis(char *dst, const char *src, int flag)
152 {
153 	char c;
154 	char *start;
155 
156 	for (start = dst; (c = *src);)
157 		dst = vis(dst, c, flag, *++src);
158 	*dst = '\0';
159 	return (dst - start);
160 }
161 
162 int
163 strnvis(char *dst, const char *src, size_t siz, int flag)
164 {
165 	char *start, *end;
166 	char tbuf[5];
167 	int c, i;
168 
169 	i = 0;
170 	for (start = dst, end = start + siz - 1; (c = *src) && dst < end; ) {
171 		if (isvisible(c, flag)) {
172 			i = 1;
173 			*dst++ = c;
174 			if (c == '\\' && (flag & VIS_NOSLASH) == 0) {
175 				/* need space for the extra '\\' */
176 				if (dst < end)
177 					*dst++ = '\\';
178 				else {
179 					dst--;
180 					i = 2;
181 					break;
182 				}
183 			}
184 			src++;
185 		} else {
186 			i = vis(tbuf, c, flag, *++src) - tbuf;
187 			if (dst + i <= end) {
188 				memcpy(dst, tbuf, i);
189 				dst += i;
190 			} else {
191 				src--;
192 				break;
193 			}
194 		}
195 	}
196 	if (siz > 0)
197 		*dst = '\0';
198 	if (dst + i > end) {
199 		/* adjust return value for truncation */
200 		while ((c = *src))
201 			dst += vis(tbuf, c, flag, *++src) - tbuf;
202 	}
203 	return (dst - start);
204 }
205 
206 int
207 strvisx(char *dst, const char *src, size_t len, int flag)
208 {
209 	char c;
210 	char *start;
211 
212 	for (start = dst; len > 1; len--) {
213 		c = *src;
214 		dst = vis(dst, c, flag, *++src);
215 	}
216 	if (len)
217 		dst = vis(dst, *src, flag, '\0');
218 	*dst = '\0';
219 	return (dst - start);
220 }
221