xref: /dragonfly/lib/libc/gen/vis.c (revision 07ed7d32)
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * $FreeBSD: src/lib/libc/gen/vis.c,v 1.13 2003/10/30 12:41:50 phk Exp $
30  * $DragonFly: src/lib/libc/gen/vis.c,v 1.6 2005/11/13 00:07:42 swildner Exp $
31  */
32 
33 #include <sys/types.h>
34 #include <ctype.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <vis.h>
39 
40 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
41 
42 /*
43  * vis - visually encode characters
44  */
45 char *
46 vis(char *dst, int c, int flag, int nextc)
47 {
48 	c = (unsigned char)c;
49 
50 	if (flag & VIS_HTTPSTYLE) {
51 		/* Described in RFC 1808 */
52 		if (!(isalnum(c) /* alpha-numeric */
53 		    /* safe */
54 		    || c == '$' || c == '-' || c == '_' || c == '.' || c == '+'
55 		    /* extra */
56 		    || c == '!' || c == '*' || c == '\'' || c == '('
57 		    || c == ')' || c == ',')) {
58 			*dst++ = '%';
59 			snprintf(dst, 4, (c < 16 ? "0%X" : "%X"), c);
60 			dst += 2;
61 			goto done;
62 		}
63 	}
64 
65 	if ((flag & VIS_GLOB) &&
66 	    (c == '*' || c == '?' || c == '[' || c == '#'))
67 		;
68 	else if (isgraph(c) ||
69 	   ((flag & VIS_SP) == 0 && c == ' ') ||
70 	   ((flag & VIS_TAB) == 0 && c == '\t') ||
71 	   ((flag & VIS_NL) == 0 && c == '\n') ||
72 	   ((flag & VIS_SAFE) && (c == '\b' || c == '\007' || c == '\r'))) {
73 		*dst++ = c;
74 		if (c == '\\' && (flag & VIS_NOSLASH) == 0)
75 			*dst++ = '\\';
76 		*dst = '\0';
77 		return (dst);
78 	}
79 
80 	if (flag & VIS_CSTYLE) {
81 		switch(c) {
82 		case '\n':
83 			*dst++ = '\\';
84 			*dst++ = 'n';
85 			goto done;
86 		case '\r':
87 			*dst++ = '\\';
88 			*dst++ = 'r';
89 			goto done;
90 		case '\b':
91 			*dst++ = '\\';
92 			*dst++ = 'b';
93 			goto done;
94 		case '\a':
95 			*dst++ = '\\';
96 			*dst++ = 'a';
97 			goto done;
98 		case '\v':
99 			*dst++ = '\\';
100 			*dst++ = 'v';
101 			goto done;
102 		case '\t':
103 			*dst++ = '\\';
104 			*dst++ = 't';
105 			goto done;
106 		case '\f':
107 			*dst++ = '\\';
108 			*dst++ = 'f';
109 			goto done;
110 		case ' ':
111 			*dst++ = '\\';
112 			*dst++ = 's';
113 			goto done;
114 		case '\0':
115 			*dst++ = '\\';
116 			*dst++ = '0';
117 			if (isoctal(nextc)) {
118 				*dst++ = '0';
119 				*dst++ = '0';
120 			}
121 			goto done;
122 		}
123 	}
124 	if (((c & 0177) == ' ') || isgraph(c) || (flag & VIS_OCTAL)) {
125 		*dst++ = '\\';
126 		*dst++ = ((u_char)c >> 6 & 07) + '0';
127 		*dst++ = ((u_char)c >> 3 & 07) + '0';
128 		*dst++ = ((u_char)c & 07) + '0';
129 		goto done;
130 	}
131 	if ((flag & VIS_NOSLASH) == 0)
132 		*dst++ = '\\';
133 	if (c & 0200) {
134 		c &= 0177;
135 		*dst++ = 'M';
136 	}
137 	if (iscntrl(c)) {
138 		*dst++ = '^';
139 		if (c == 0177)
140 			*dst++ = '?';
141 		else
142 			*dst++ = c + '@';
143 	} else {
144 		*dst++ = '-';
145 		*dst++ = c;
146 	}
147 done:
148 	*dst = '\0';
149 	return (dst);
150 }
151 
152 /*
153  * strvis, strvisx - visually encode characters from src into dst
154  *
155  *	Dst must be 4 times the size of src to account for possible
156  *	expansion.  The length of dst, not including the trailing NUL,
157  *	is returned.
158  *
159  *	Strvisx encodes exactly len bytes from src into dst.
160  *	This is useful for encoding a block of data.
161  */
162 int
163 strvis(char *dst, const char *src, int flag)
164 {
165 	char c;
166 	char *start;
167 
168 	for (start = dst; (c = *src); )
169 		dst = vis(dst, c, flag, *++src);
170 	*dst = '\0';
171 	return (dst - start);
172 }
173 
174 int
175 strnvis(char *dst, const char *src, size_t len, int flag)
176 {
177 	char *start, *end;
178 	char buf[5];
179 	int c, i;
180 
181 	i = 0;
182 	start = dst;
183 	end = start + len -1;
184 	for (; (c = *src) != '\0';) {
185 		i = vis(buf, c, flag, *++src) - buf;
186 		if (start + i > end) {
187 			--src;
188 			break;
189 		}
190 		memcpy(dst, buf, i);
191 	        dst += i;
192 	}
193         if (len > 0)
194 		*dst = '\0';
195 	if (dst + i > end) {
196 		/* adjust return value for truncation */
197 		while ((c = *src) != '\0')
198 			dst += vis(buf, c, flag, *++src) - buf;
199 	}
200 	return (dst - start);
201 }
202 
203 int
204 strvisx(char *dst, const char *src, size_t len, int flag)
205 {
206 	int c;
207 	char *start;
208 
209 	for (start = dst; len > 1; len--) {
210 		c = *src;
211 		dst = vis(dst, c, flag, *++src);
212 	}
213 	if (len)
214 		dst = vis(dst, *src, flag, '\0');
215 	*dst = '\0';
216 
217 	return (dst - start);
218 }
219