xref: /freebsd/crypto/heimdal/lib/roken/unvis.c (revision 195ebc7e9e4b129de810833791a19dfb4349d6a9)
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. 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 #if 1
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 RCSID("$Id: unvis.c 21005 2007-06-08 01:54:35Z lha $");
36 #endif
37 #include "roken.h"
38 #ifndef _DIAGASSERT
39 #define _DIAGASSERT(X)
40 #endif
41 #else
42 #include <sys/cdefs.h>
43 #if defined(LIBC_SCCS) && !defined(lint)
44 #if 0
45 static char sccsid[] = "@(#)unvis.c	8.1 (Berkeley) 6/4/93";
46 #else
47 __RCSID("$NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp $");
48 #endif
49 #endif /* LIBC_SCCS and not lint */
50 
51 #define __LIBC12_SOURCE__
52 
53 #include "namespace.h"
54 #endif
55 #include <sys/types.h>
56 
57 #include <assert.h>
58 #include <ctype.h>
59 #include <stdio.h>
60 #include <vis.h>
61 
62 #if 0
63 #ifdef __weak_alias
64 __weak_alias(strunvis,_strunvis)
65 __weak_alias(unvis,_unvis)
66 #endif
67 
68 __warn_references(unvis,
69     "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
70 #endif
71 
72 /*
73  * decode driven by state machine
74  */
75 #define	S_GROUND	0	/* haven't seen escape char */
76 #define	S_START		1	/* start decoding special sequence */
77 #define	S_META		2	/* metachar started (M) */
78 #define	S_META1		3	/* metachar more, regular char (-) */
79 #define	S_CTRL		4	/* control char started (^) */
80 #define	S_OCTAL2	5	/* octal digit 2 */
81 #define	S_OCTAL3	6	/* octal digit 3 */
82 
83 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
84 
85 int ROKEN_LIB_FUNCTION
86 	rk_strunvis (char *, const char *);
87 int ROKEN_LIB_FUNCTION
88 	rk_unvis (char *, int, int *, int);
89 
90 /*
91  * unvis - decode characters previously encoded by vis
92  */
93 
94 int ROKEN_LIB_FUNCTION
95 rk_unvis(char *cp, int c, int *astate, int flag)
96 {
97 
98 	_DIAGASSERT(cp != NULL);
99 	_DIAGASSERT(astate != NULL);
100 
101 	if (flag & UNVIS_END) {
102 		if (*astate == S_OCTAL2 || *astate == S_OCTAL3) {
103 			*astate = S_GROUND;
104 			return (UNVIS_VALID);
105 		}
106 		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
107 	}
108 
109 	switch (*astate) {
110 
111 	case S_GROUND:
112 		*cp = 0;
113 		if (c == '\\') {
114 			*astate = S_START;
115 			return (0);
116 		}
117 		*cp = c;
118 		return (UNVIS_VALID);
119 
120 	case S_START:
121 		switch(c) {
122 		case '\\':
123 			*cp = c;
124 			*astate = S_GROUND;
125 			return (UNVIS_VALID);
126 		case '0': case '1': case '2': case '3':
127 		case '4': case '5': case '6': case '7':
128 			*cp = (c - '0');
129 			*astate = S_OCTAL2;
130 			return (0);
131 		case 'M':
132 			*cp = (char)0200;
133 			*astate = S_META;
134 			return (0);
135 		case '^':
136 			*astate = S_CTRL;
137 			return (0);
138 		case 'n':
139 			*cp = '\n';
140 			*astate = S_GROUND;
141 			return (UNVIS_VALID);
142 		case 'r':
143 			*cp = '\r';
144 			*astate = S_GROUND;
145 			return (UNVIS_VALID);
146 		case 'b':
147 			*cp = '\b';
148 			*astate = S_GROUND;
149 			return (UNVIS_VALID);
150 		case 'a':
151 			*cp = '\007';
152 			*astate = S_GROUND;
153 			return (UNVIS_VALID);
154 		case 'v':
155 			*cp = '\v';
156 			*astate = S_GROUND;
157 			return (UNVIS_VALID);
158 		case 't':
159 			*cp = '\t';
160 			*astate = S_GROUND;
161 			return (UNVIS_VALID);
162 		case 'f':
163 			*cp = '\f';
164 			*astate = S_GROUND;
165 			return (UNVIS_VALID);
166 		case 's':
167 			*cp = ' ';
168 			*astate = S_GROUND;
169 			return (UNVIS_VALID);
170 		case 'E':
171 			*cp = '\033';
172 			*astate = S_GROUND;
173 			return (UNVIS_VALID);
174 		case '\n':
175 			/*
176 			 * hidden newline
177 			 */
178 			*astate = S_GROUND;
179 			return (UNVIS_NOCHAR);
180 		case '$':
181 			/*
182 			 * hidden marker
183 			 */
184 			*astate = S_GROUND;
185 			return (UNVIS_NOCHAR);
186 		}
187 		*astate = S_GROUND;
188 		return (UNVIS_SYNBAD);
189 
190 	case S_META:
191 		if (c == '-')
192 			*astate = S_META1;
193 		else if (c == '^')
194 			*astate = S_CTRL;
195 		else {
196 			*astate = S_GROUND;
197 			return (UNVIS_SYNBAD);
198 		}
199 		return (0);
200 
201 	case S_META1:
202 		*astate = S_GROUND;
203 		*cp |= c;
204 		return (UNVIS_VALID);
205 
206 	case S_CTRL:
207 		if (c == '?')
208 			*cp |= 0177;
209 		else
210 			*cp |= c & 037;
211 		*astate = S_GROUND;
212 		return (UNVIS_VALID);
213 
214 	case S_OCTAL2:	/* second possible octal digit */
215 		if (isoctal(c)) {
216 			/*
217 			 * yes - and maybe a third
218 			 */
219 			*cp = (*cp << 3) + (c - '0');
220 			*astate = S_OCTAL3;
221 			return (0);
222 		}
223 		/*
224 		 * no - done with current sequence, push back passed char
225 		 */
226 		*astate = S_GROUND;
227 		return (UNVIS_VALIDPUSH);
228 
229 	case S_OCTAL3:	/* third possible octal digit */
230 		*astate = S_GROUND;
231 		if (isoctal(c)) {
232 			*cp = (*cp << 3) + (c - '0');
233 			return (UNVIS_VALID);
234 		}
235 		/*
236 		 * we were done, push back passed char
237 		 */
238 		return (UNVIS_VALIDPUSH);
239 
240 	default:
241 		/*
242 		 * decoder in unknown state - (probably uninitialized)
243 		 */
244 		*astate = S_GROUND;
245 		return (UNVIS_SYNBAD);
246 	}
247 }
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 int ROKEN_LIB_FUNCTION
257 rk_strunvis(char *dst, const char *src)
258 {
259 	char c;
260 	char *start = dst;
261 	int state = 0;
262 
263 	_DIAGASSERT(src != NULL);
264 	_DIAGASSERT(dst != NULL);
265 
266 	while ((c = *src++) != '\0') {
267 	again:
268 		switch (rk_unvis(dst, (unsigned char)c, &state, 0)) {
269 		case UNVIS_VALID:
270 			dst++;
271 			break;
272 		case UNVIS_VALIDPUSH:
273 			dst++;
274 			goto again;
275 		case 0:
276 		case UNVIS_NOCHAR:
277 			break;
278 		default:
279 			return (-1);
280 		}
281 	}
282 	if (unvis(dst, (unsigned char)c, &state, UNVIS_END) == UNVIS_VALID)
283 		dst++;
284 	*dst = '\0';
285 	return (dst - start);
286 }
287