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