xref: /freebsd/crypto/heimdal/lib/roken/unvis.c (revision 9336e0699bda8a301cd2bfa37106b6ec5e32012e)
1 /*	$NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #if 1
37 #ifdef HAVE_CONFIG_H
38 #include <config.h>
39 RCSID("$Id: unvis.c,v 1.2 2000/12/06 21:41:46 joda Exp $");
40 #endif
41 #include <roken.h>
42 #ifndef _DIAGASSERT
43 #define _DIAGASSERT(X)
44 #endif
45 #else
46 #include <sys/cdefs.h>
47 #if defined(LIBC_SCCS) && !defined(lint)
48 #if 0
49 static char sccsid[] = "@(#)unvis.c	8.1 (Berkeley) 6/4/93";
50 #else
51 __RCSID("$NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp $");
52 #endif
53 #endif /* LIBC_SCCS and not lint */
54 
55 #define __LIBC12_SOURCE__
56 
57 #include "namespace.h"
58 #endif
59 #include <sys/types.h>
60 
61 #include <assert.h>
62 #include <ctype.h>
63 #include <stdio.h>
64 #include <vis.h>
65 
66 #if 0
67 #ifdef __weak_alias
68 __weak_alias(strunvis,_strunvis)
69 __weak_alias(unvis,_unvis)
70 #endif
71 
72 __warn_references(unvis,
73     "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
74 #endif
75 
76 /*
77  * decode driven by state machine
78  */
79 #define	S_GROUND	0	/* haven't seen escape char */
80 #define	S_START		1	/* start decoding special sequence */
81 #define	S_META		2	/* metachar started (M) */
82 #define	S_META1		3	/* metachar more, regular char (-) */
83 #define	S_CTRL		4	/* control char started (^) */
84 #define	S_OCTAL2	5	/* octal digit 2 */
85 #define	S_OCTAL3	6	/* octal digit 3 */
86 
87 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
88 
89 /*
90  * unvis - decode characters previously encoded by vis
91  */
92 #ifndef HAVE_UNVIS
93 int
94 unvis(char *cp, int c, int *astate, int flag)
95 {
96 
97 	_DIAGASSERT(cp != NULL);
98 	_DIAGASSERT(astate != NULL);
99 
100 	if (flag & UNVIS_END) {
101 		if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
102 			*astate = S_GROUND;
103 			return (UNVIS_VALID);
104 		}
105 		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
106 	}
107 
108 	switch (*astate) {
109 
110 	case S_GROUND:
111 		*cp = 0;
112 		if (c == '\\') {
113 			*astate = S_START;
114 			return (0);
115 		}
116 		*cp = c;
117 		return (UNVIS_VALID);
118 
119 	case S_START:
120 		switch(c) {
121 		case '\\':
122 			*cp = c;
123 			*astate = S_GROUND;
124 			return (UNVIS_VALID);
125 		case '0': case '1': case '2': case '3':
126 		case '4': case '5': case '6': case '7':
127 			*cp = (c - '0');
128 			*astate = S_OCTAL2;
129 			return (0);
130 		case 'M':
131 			*cp = (char)0200;
132 			*astate = S_META;
133 			return (0);
134 		case '^':
135 			*astate = S_CTRL;
136 			return (0);
137 		case 'n':
138 			*cp = '\n';
139 			*astate = S_GROUND;
140 			return (UNVIS_VALID);
141 		case 'r':
142 			*cp = '\r';
143 			*astate = S_GROUND;
144 			return (UNVIS_VALID);
145 		case 'b':
146 			*cp = '\b';
147 			*astate = S_GROUND;
148 			return (UNVIS_VALID);
149 		case 'a':
150 			*cp = '\007';
151 			*astate = S_GROUND;
152 			return (UNVIS_VALID);
153 		case 'v':
154 			*cp = '\v';
155 			*astate = S_GROUND;
156 			return (UNVIS_VALID);
157 		case 't':
158 			*cp = '\t';
159 			*astate = S_GROUND;
160 			return (UNVIS_VALID);
161 		case 'f':
162 			*cp = '\f';
163 			*astate = S_GROUND;
164 			return (UNVIS_VALID);
165 		case 's':
166 			*cp = ' ';
167 			*astate = S_GROUND;
168 			return (UNVIS_VALID);
169 		case 'E':
170 			*cp = '\033';
171 			*astate = S_GROUND;
172 			return (UNVIS_VALID);
173 		case '\n':
174 			/*
175 			 * hidden newline
176 			 */
177 			*astate = S_GROUND;
178 			return (UNVIS_NOCHAR);
179 		case '$':
180 			/*
181 			 * hidden marker
182 			 */
183 			*astate = S_GROUND;
184 			return (UNVIS_NOCHAR);
185 		}
186 		*astate = S_GROUND;
187 		return (UNVIS_SYNBAD);
188 
189 	case S_META:
190 		if (c == '-')
191 			*astate = S_META1;
192 		else if (c == '^')
193 			*astate = S_CTRL;
194 		else {
195 			*astate = S_GROUND;
196 			return (UNVIS_SYNBAD);
197 		}
198 		return (0);
199 
200 	case S_META1:
201 		*astate = S_GROUND;
202 		*cp |= c;
203 		return (UNVIS_VALID);
204 
205 	case S_CTRL:
206 		if (c == '?')
207 			*cp |= 0177;
208 		else
209 			*cp |= c & 037;
210 		*astate = S_GROUND;
211 		return (UNVIS_VALID);
212 
213 	case S_OCTAL2:	/* second possible octal digit */
214 		if (isoctal(c)) {
215 			/*
216 			 * yes - and maybe a third
217 			 */
218 			*cp = (*cp << 3) + (c - '0');
219 			*astate = S_OCTAL3;
220 			return (0);
221 		}
222 		/*
223 		 * no - done with current sequence, push back passed char
224 		 */
225 		*astate = S_GROUND;
226 		return (UNVIS_VALIDPUSH);
227 
228 	case S_OCTAL3:	/* third possible octal digit */
229 		*astate = S_GROUND;
230 		if (isoctal(c)) {
231 			*cp = (*cp << 3) + (c - '0');
232 			return (UNVIS_VALID);
233 		}
234 		/*
235 		 * we were done, push back passed char
236 		 */
237 		return (UNVIS_VALIDPUSH);
238 
239 	default:
240 		/*
241 		 * decoder in unknown state - (probably uninitialized)
242 		 */
243 		*astate = S_GROUND;
244 		return (UNVIS_SYNBAD);
245 	}
246 }
247 #endif
248 
249 /*
250  * strunvis - decode src into dst
251  *
252  *	Number of chars decoded into dst is returned, -1 on error.
253  *	Dst is null terminated.
254  */
255 
256 #ifndef HAVE_STRUNVIS
257 int
258 strunvis(char *dst, const char *src)
259 {
260 	char c;
261 	char *start = dst;
262 	int state = 0;
263 
264 	_DIAGASSERT(src != NULL);
265 	_DIAGASSERT(dst != NULL);
266 
267 	while ((c = *src++) != '\0') {
268 	again:
269 		switch (unvis(dst, c, &state, 0)) {
270 		case UNVIS_VALID:
271 			dst++;
272 			break;
273 		case UNVIS_VALIDPUSH:
274 			dst++;
275 			goto again;
276 		case 0:
277 		case UNVIS_NOCHAR:
278 			break;
279 		default:
280 			return (-1);
281 		}
282 	}
283 	if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
284 		dst++;
285 	*dst = '\0';
286 	return (dst - start);
287 }
288 #endif
289