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