xref: /netbsd-src/lib/libc/gen/vis.c (revision 2de962bd804263c16657f586aa00f1704045df8e)
1 /*	$NetBSD: vis.c,v 1.36 2008/04/29 06:53:01 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*-
33  * Copyright (c) 1999, 2005 The NetBSD Foundation, Inc.
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  *
45  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55  * POSSIBILITY OF SUCH DAMAGE.
56  */
57 
58 #include <sys/cdefs.h>
59 #if defined(LIBC_SCCS) && !defined(lint)
60 __RCSID("$NetBSD: vis.c,v 1.36 2008/04/29 06:53:01 martin Exp $");
61 #endif /* LIBC_SCCS and not lint */
62 
63 #include "namespace.h"
64 #include <sys/types.h>
65 
66 #include <assert.h>
67 #include <vis.h>
68 #include <stdlib.h>
69 
70 #ifdef __weak_alias
71 __weak_alias(strsvis,_strsvis)
72 __weak_alias(strsvisx,_strsvisx)
73 __weak_alias(strvis,_strvis)
74 __weak_alias(strvisx,_strvisx)
75 __weak_alias(svis,_svis)
76 __weak_alias(vis,_vis)
77 #endif
78 
79 #if !HAVE_VIS || !HAVE_SVIS
80 #include <ctype.h>
81 #include <limits.h>
82 #include <stdio.h>
83 #include <string.h>
84 
85 #undef BELL
86 #define BELL '\a'
87 
88 #define isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
89 #define iswhite(c)	(c == ' ' || c == '\t' || c == '\n')
90 #define issafe(c)	(c == '\b' || c == BELL || c == '\r')
91 #define xtoa(c)		"0123456789abcdef"[c]
92 
93 #define MAXEXTRAS	5
94 
95 
96 #define MAKEEXTRALIST(flag, extra, orig_str)				      \
97 do {									      \
98 	const char *orig = orig_str;					      \
99 	const char *o = orig;						      \
100 	char *e;							      \
101 	while (*o++)							      \
102 		continue;						      \
103 	extra = malloc((size_t)((o - orig) + MAXEXTRAS));		      \
104 	if (!extra) break;						      \
105 	for (o = orig, e = extra; (*e++ = *o++) != '\0';)		      \
106 		continue;						      \
107 	e--;								      \
108 	if (flag & VIS_SP) *e++ = ' ';					      \
109 	if (flag & VIS_TAB) *e++ = '\t';				      \
110 	if (flag & VIS_NL) *e++ = '\n';					      \
111 	if ((flag & VIS_NOSLASH) == 0) *e++ = '\\';			      \
112 	*e = '\0';							      \
113 } while (/*CONSTCOND*/0)
114 
115 
116 /*
117  * This is HVIS, the macro of vis used to HTTP style (RFC 1808)
118  */
119 #define HVIS(dst, c, flag, nextc, extra)				      \
120 do									      \
121 	if (!isascii(c) || !isalnum(c) || strchr("$-_.+!*'(),", c) != NULL) { \
122 		*dst++ = '%';						      \
123 		*dst++ = xtoa(((unsigned int)c >> 4) & 0xf);		      \
124 		*dst++ = xtoa((unsigned int)c & 0xf);			      \
125 	} else {							      \
126 		SVIS(dst, c, flag, nextc, extra);			      \
127 	}								      \
128 while (/*CONSTCOND*/0)
129 
130 /*
131  * This is SVIS, the central macro of vis.
132  * dst:	      Pointer to the destination buffer
133  * c:	      Character to encode
134  * flag:      Flag word
135  * nextc:     The character following 'c'
136  * extra:     Pointer to the list of extra characters to be
137  *	      backslash-protected.
138  */
139 #define SVIS(dst, c, flag, nextc, extra)				      \
140 do {									      \
141 	int isextra;							      \
142 	isextra = strchr(extra, c) != NULL;				      \
143 	if (!isextra && isascii(c) && (isgraph(c) || iswhite(c) ||	      \
144 	    ((flag & VIS_SAFE) && issafe(c)))) {			      \
145 		*dst++ = c;						      \
146 		break;							      \
147 	}								      \
148 	if (flag & VIS_CSTYLE) {					      \
149 		switch (c) {						      \
150 		case '\n':						      \
151 			*dst++ = '\\'; *dst++ = 'n';			      \
152 			continue;					      \
153 		case '\r':						      \
154 			*dst++ = '\\'; *dst++ = 'r';			      \
155 			continue;					      \
156 		case '\b':						      \
157 			*dst++ = '\\'; *dst++ = 'b';			      \
158 			continue;					      \
159 		case BELL:						      \
160 			*dst++ = '\\'; *dst++ = 'a';			      \
161 			continue;					      \
162 		case '\v':						      \
163 			*dst++ = '\\'; *dst++ = 'v';			      \
164 			continue;					      \
165 		case '\t':						      \
166 			*dst++ = '\\'; *dst++ = 't';			      \
167 			continue;					      \
168 		case '\f':						      \
169 			*dst++ = '\\'; *dst++ = 'f';			      \
170 			continue;					      \
171 		case ' ':						      \
172 			*dst++ = '\\'; *dst++ = 's';			      \
173 			continue;					      \
174 		case '\0':						      \
175 			*dst++ = '\\'; *dst++ = '0';			      \
176 			if (isoctal(nextc)) {				      \
177 				*dst++ = '0';				      \
178 				*dst++ = '0';				      \
179 			}						      \
180 			continue;					      \
181 		default:						      \
182 			if (isgraph(c)) {				      \
183 				*dst++ = '\\'; *dst++ = c;		      \
184 				continue;				      \
185 			}						      \
186 		}							      \
187 	}								      \
188 	if (isextra || ((c & 0177) == ' ') || (flag & VIS_OCTAL)) {	      \
189 		*dst++ = '\\';						      \
190 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 6) & 03) + '0';    \
191 		*dst++ = (u_char)(((u_int32_t)(u_char)c >> 3) & 07) + '0';    \
192 		*dst++ =			     (c	      & 07) + '0';    \
193 	} else {							      \
194 		if ((flag & VIS_NOSLASH) == 0) *dst++ = '\\';		      \
195 		if (c & 0200) {						      \
196 			c &= 0177; *dst++ = 'M';			      \
197 		}							      \
198 		if (iscntrl(c)) {					      \
199 			*dst++ = '^';					      \
200 			if (c == 0177)					      \
201 				*dst++ = '?';				      \
202 			else						      \
203 				*dst++ = c + '@';			      \
204 		} else {						      \
205 			*dst++ = '-'; *dst++ = c;			      \
206 		}							      \
207 	}								      \
208 } while (/*CONSTCOND*/0)
209 
210 
211 /*
212  * svis - visually encode characters, also encoding the characters
213  *	  pointed to by `extra'
214  */
215 char *
216 svis(char *dst, int c, int flag, int nextc, const char *extra)
217 {
218 	char *nextra = NULL;
219 
220 	_DIAGASSERT(dst != NULL);
221 	_DIAGASSERT(extra != NULL);
222 	MAKEEXTRALIST(flag, nextra, extra);
223 	if (!nextra) {
224 		*dst = '\0';		/* can't create nextra, return "" */
225 		return dst;
226 	}
227 	if (flag & VIS_HTTPSTYLE)
228 		HVIS(dst, c, flag, nextc, nextra);
229 	else
230 		SVIS(dst, c, flag, nextc, nextra);
231 	free(nextra);
232 	*dst = '\0';
233 	return dst;
234 }
235 
236 
237 /*
238  * strsvis, strsvisx - visually encode characters from src into dst
239  *
240  *	Extra is a pointer to a \0-terminated list of characters to
241  *	be encoded, too. These functions are useful e. g. to
242  *	encode strings in such a way so that they are not interpreted
243  *	by a shell.
244  *
245  *	Dst must be 4 times the size of src to account for possible
246  *	expansion.  The length of dst, not including the trailing NULL,
247  *	is returned.
248  *
249  *	Strsvisx encodes exactly len bytes from src into dst.
250  *	This is useful for encoding a block of data.
251  */
252 int
253 strsvis(char *dst, const char *csrc, int flag, const char *extra)
254 {
255 	int c;
256 	char *start;
257 	char *nextra = NULL;
258 	const unsigned char *src = (const unsigned char *)csrc;
259 
260 	_DIAGASSERT(dst != NULL);
261 	_DIAGASSERT(src != NULL);
262 	_DIAGASSERT(extra != NULL);
263 	MAKEEXTRALIST(flag, nextra, extra);
264 	if (!nextra) {
265 		*dst = '\0';		/* can't create nextra, return "" */
266 		return 0;
267 	}
268 	if (flag & VIS_HTTPSTYLE) {
269 		for (start = dst; (c = *src++) != '\0'; /* empty */)
270 			HVIS(dst, c, flag, *src, nextra);
271 	} else {
272 		for (start = dst; (c = *src++) != '\0'; /* empty */)
273 			SVIS(dst, c, flag, *src, nextra);
274 	}
275 	free(nextra);
276 	*dst = '\0';
277 	return (dst - start);
278 }
279 
280 
281 int
282 strsvisx(char *dst, const char *csrc, size_t len, int flag, const char *extra)
283 {
284 	unsigned char c;
285 	char *start;
286 	char *nextra = NULL;
287 	const unsigned char *src = (const unsigned char *)csrc;
288 
289 	_DIAGASSERT(dst != NULL);
290 	_DIAGASSERT(src != NULL);
291 	_DIAGASSERT(extra != NULL);
292 	MAKEEXTRALIST(flag, nextra, extra);
293 	if (! nextra) {
294 		*dst = '\0';		/* can't create nextra, return "" */
295 		return 0;
296 	}
297 
298 	if (flag & VIS_HTTPSTYLE) {
299 		for (start = dst; len > 0; len--) {
300 			c = *src++;
301 			HVIS(dst, c, flag, len ? *src : '\0', nextra);
302 		}
303 	} else {
304 		for (start = dst; len > 0; len--) {
305 			c = *src++;
306 			SVIS(dst, c, flag, len ? *src : '\0', nextra);
307 		}
308 	}
309 	free(nextra);
310 	*dst = '\0';
311 	return (dst - start);
312 }
313 #endif
314 
315 #if !HAVE_VIS
316 /*
317  * vis - visually encode characters
318  */
319 char *
320 vis(char *dst, int c, int flag, int nextc)
321 {
322 	char *extra = NULL;
323 	unsigned char uc = (unsigned char)c;
324 
325 	_DIAGASSERT(dst != NULL);
326 
327 	MAKEEXTRALIST(flag, extra, "");
328 	if (! extra) {
329 		*dst = '\0';		/* can't create extra, return "" */
330 		return dst;
331 	}
332 	if (flag & VIS_HTTPSTYLE)
333 		HVIS(dst, uc, flag, nextc, extra);
334 	else
335 		SVIS(dst, uc, flag, nextc, extra);
336 	free(extra);
337 	*dst = '\0';
338 	return dst;
339 }
340 
341 
342 /*
343  * strvis, strvisx - visually encode characters from src into dst
344  *
345  *	Dst must be 4 times the size of src to account for possible
346  *	expansion.  The length of dst, not including the trailing NULL,
347  *	is returned.
348  *
349  *	Strvisx encodes exactly len bytes from src into dst.
350  *	This is useful for encoding a block of data.
351  */
352 int
353 strvis(char *dst, const char *src, int flag)
354 {
355 	char *extra = NULL;
356 	int rv;
357 
358 	MAKEEXTRALIST(flag, extra, "");
359 	if (!extra) {
360 		*dst = '\0';		/* can't create extra, return "" */
361 		return 0;
362 	}
363 	rv = strsvis(dst, src, flag, extra);
364 	free(extra);
365 	return rv;
366 }
367 
368 
369 int
370 strvisx(char *dst, const char *src, size_t len, int flag)
371 {
372 	char *extra = NULL;
373 	int rv;
374 
375 	MAKEEXTRALIST(flag, extra, "");
376 	if (!extra) {
377 		*dst = '\0';		/* can't create extra, return "" */
378 		return 0;
379 	}
380 	rv = strsvisx(dst, src, len, flag, extra);
381 	free(extra);
382 	return rv;
383 }
384 #endif
385