xref: /netbsd-src/lib/libc/gen/unvis.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: unvis.c,v 1.30 2009/02/11 13:51:59 christos 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 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)unvis.c	8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: unvis.c,v 1.30 2009/02/11 13:51:59 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 #include <sys/types.h>
43 
44 #include <assert.h>
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <vis.h>
48 
49 #ifdef __weak_alias
50 __weak_alias(strunvis,_strunvis)
51 #endif
52 
53 #if !HAVE_VIS
54 /*
55  * decode driven by state machine
56  */
57 #define	S_GROUND	0	/* haven't seen escape char */
58 #define	S_START		1	/* start decoding special sequence */
59 #define	S_META		2	/* metachar started (M) */
60 #define	S_META1		3	/* metachar more, regular char (-) */
61 #define	S_CTRL		4	/* control char started (^) */
62 #define	S_OCTAL2	5	/* octal digit 2 */
63 #define	S_OCTAL3	6	/* octal digit 3 */
64 #define	S_HEX1		7	/* http hex digit */
65 #define	S_HEX2		8	/* http hex digit 2 */
66 #define S_MIME1		9	/* mime hex digit 1 */
67 #define S_MIME2		10	/* mime hex digit 2 */
68 #define S_EATCRNL	11	/* mime eating CRNL */
69 
70 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
71 #define xtod(c)		(isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
72 #define XTOD(c)		(isdigit(c) ? (c - '0') : ((c - 'A') + 10))
73 
74 /*
75  * unvis - decode characters previously encoded by vis
76  */
77 int
78 unvis(char *cp, int c, int *astate, int flag)
79 {
80 	unsigned char uc = (unsigned char)c;
81 
82 	_DIAGASSERT(cp != NULL);
83 	_DIAGASSERT(astate != NULL);
84 
85 	if (flag & UNVIS_END) {
86 		if (*astate == S_OCTAL2 || *astate == S_OCTAL3
87 		    || *astate == S_HEX2) {
88 			*astate = S_GROUND;
89 			return UNVIS_VALID;
90 		}
91 		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
92 	}
93 
94 	switch (*astate) {
95 
96 	case S_GROUND:
97 		*cp = 0;
98 		if (c == '\\') {
99 			*astate = S_START;
100 			return UNVIS_NOCHAR;
101 		}
102 		if ((flag & VIS_HTTPSTYLE) && c == '%') {
103 			*astate = S_HEX1;
104 			return UNVIS_NOCHAR;
105 		}
106 		if ((flag & VIS_MIMESTYLE) && c == '=') {
107 			*astate = S_MIME1;
108 			return UNVIS_NOCHAR;
109 		}
110 		*cp = c;
111 		return UNVIS_VALID;
112 
113 	case S_START:
114 		switch(c) {
115 		case '\\':
116 			*cp = c;
117 			*astate = S_GROUND;
118 			return UNVIS_VALID;
119 		case '0': case '1': case '2': case '3':
120 		case '4': case '5': case '6': case '7':
121 			*cp = (c - '0');
122 			*astate = S_OCTAL2;
123 			return UNVIS_NOCHAR;
124 		case 'M':
125 			*cp = (char)0200;
126 			*astate = S_META;
127 			return UNVIS_NOCHAR;
128 		case '^':
129 			*astate = S_CTRL;
130 			return UNVIS_NOCHAR;
131 		case 'n':
132 			*cp = '\n';
133 			*astate = S_GROUND;
134 			return UNVIS_VALID;
135 		case 'r':
136 			*cp = '\r';
137 			*astate = S_GROUND;
138 			return UNVIS_VALID;
139 		case 'b':
140 			*cp = '\b';
141 			*astate = S_GROUND;
142 			return UNVIS_VALID;
143 		case 'a':
144 			*cp = '\007';
145 			*astate = S_GROUND;
146 			return UNVIS_VALID;
147 		case 'v':
148 			*cp = '\v';
149 			*astate = S_GROUND;
150 			return UNVIS_VALID;
151 		case 't':
152 			*cp = '\t';
153 			*astate = S_GROUND;
154 			return UNVIS_VALID;
155 		case 'f':
156 			*cp = '\f';
157 			*astate = S_GROUND;
158 			return UNVIS_VALID;
159 		case 's':
160 			*cp = ' ';
161 			*astate = S_GROUND;
162 			return UNVIS_VALID;
163 		case 'E':
164 			*cp = '\033';
165 			*astate = S_GROUND;
166 			return UNVIS_VALID;
167 		case '\n':
168 			/*
169 			 * hidden newline
170 			 */
171 			*astate = S_GROUND;
172 			return (UNVIS_NOCHAR);
173 		case '$':
174 			/*
175 			 * hidden marker
176 			 */
177 			*astate = S_GROUND;
178 			return (UNVIS_NOCHAR);
179 		}
180 		*astate = S_GROUND;
181 		return (UNVIS_SYNBAD);
182 
183 	case S_META:
184 		if (c == '-')
185 			*astate = S_META1;
186 		else if (c == '^')
187 			*astate = S_CTRL;
188 		else {
189 			*astate = S_GROUND;
190 			return (UNVIS_SYNBAD);
191 		}
192 		return UNVIS_NOCHAR;
193 
194 	case S_META1:
195 		*astate = S_GROUND;
196 		*cp |= c;
197 		return UNVIS_VALID;
198 
199 	case S_CTRL:
200 		if (c == '?')
201 			*cp |= 0177;
202 		else
203 			*cp |= c & 037;
204 		*astate = S_GROUND;
205 		return UNVIS_VALID;
206 
207 	case S_OCTAL2:	/* second possible octal digit */
208 		if (isoctal(uc)) {
209 			/*
210 			 * yes - and maybe a third
211 			 */
212 			*cp = (*cp << 3) + (c - '0');
213 			*astate = S_OCTAL3;
214 			return UNVIS_NOCHAR;
215 		}
216 		/*
217 		 * no - done with current sequence, push back passed char
218 		 */
219 		*astate = S_GROUND;
220 		return UNVIS_VALIDPUSH;
221 
222 	case S_OCTAL3:	/* third possible octal digit */
223 		*astate = S_GROUND;
224 		if (isoctal(uc)) {
225 			*cp = (*cp << 3) + (c - '0');
226 			return UNVIS_VALID;
227 		}
228 		/*
229 		 * we were done, push back passed char
230 		 */
231 		return UNVIS_VALIDPUSH;
232 
233 	case S_HEX1:
234 		if (isxdigit(uc)) {
235 			*cp = xtod(uc);
236 			*astate = S_HEX2;
237 			return UNVIS_NOCHAR;
238 		}
239 		/*
240 		 * no - done with current sequence, push back passed char
241 		 */
242 		*astate = S_GROUND;
243 		return UNVIS_VALIDPUSH;
244 
245 	case S_HEX2:
246 		*astate = S_GROUND;
247 		if (isxdigit(uc)) {
248 			*cp = xtod(uc) | (*cp << 4);
249 			return UNVIS_VALID;
250 		}
251 		return UNVIS_VALIDPUSH;
252 
253 	case S_MIME1:
254 		if (uc == '\n' || uc == '\r') {
255 			*astate = S_EATCRNL;
256 			return UNVIS_NOCHAR;
257 		}
258 		if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
259 			*cp = XTOD(uc);
260 			*astate = S_MIME2;
261 			return UNVIS_NOCHAR;
262 		}
263 		*astate = S_GROUND;
264 		return UNVIS_SYNBAD;
265 
266 	case S_MIME2:
267 		if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
268 			*astate = S_GROUND;
269 			*cp = XTOD(uc) | (*cp << 4);
270 			return UNVIS_VALID;
271 		}
272 		*astate = S_GROUND;
273 		return UNVIS_SYNBAD;
274 
275 	case S_EATCRNL:
276 		switch (uc) {
277 		case '\r':
278 		case '\n':
279 			return UNVIS_NOCHAR;
280 		case '=':
281 			*astate = S_MIME1;
282 			return UNVIS_NOCHAR;
283 		default:
284 			*cp = uc;
285 			return UNVIS_VALID;
286 		}
287 
288 	default:
289 		/*
290 		 * decoder in unknown state - (probably uninitialized)
291 		 */
292 		*astate = S_GROUND;
293 		return UNVIS_SYNBAD;
294 	}
295 }
296 
297 /*
298  * strunvis - decode src into dst
299  *
300  *	Number of chars decoded into dst is returned, -1 on error.
301  *	Dst is null terminated.
302  */
303 
304 int
305 strunvisx(dst, src, flag)
306 	char *dst;
307 	const char *src;
308 	int flag;
309 {
310 	char c;
311 	char *start = dst;
312 	int state = 0;
313 
314 	_DIAGASSERT(src != NULL);
315 	_DIAGASSERT(dst != NULL);
316 
317 	while ((c = *src++) != '\0') {
318  again:
319 		switch (unvis(dst, c, &state, flag)) {
320 		case UNVIS_VALID:
321 			dst++;
322 			break;
323 		case UNVIS_VALIDPUSH:
324 			dst++;
325 			goto again;
326 		case 0:
327 		case UNVIS_NOCHAR:
328 			break;
329 		default:
330 			return (-1);
331 		}
332 	}
333 	if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
334 		dst++;
335 	*dst = '\0';
336 	return (int)(dst - start);
337 }
338 
339 int
340 strunvis(dst, src)
341 	char *dst;
342 	const char *src;
343 {
344 	return strunvisx(dst, src, 0);
345 }
346 #endif
347