1 /* $OpenBSD: unvis.c,v 1.12 2005/08/08 08:05:34 espie Exp $ */
3 * Copyright (c) 1989, 1993
4 * The Regents of the University of California. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/types.h>
37 * decode driven by state machine
39 #define S_GROUND 0 /* haven't seen escape char */
40 #define S_START 1 /* start decoding special sequence */
41 #define S_META 2 /* metachar started (M) */
42 #define S_META1 3 /* metachar more, regular char (-) */
43 #define S_CTRL 4 /* control char started (^) */
44 #define S_OCTAL2 5 /* octal digit 2 */
45 #define S_OCTAL3 6 /* octal digit 3 */
47 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
50 * unvis - decode characters previously encoded by vis
53 unvis(char *cp
, char c
, int *astate
, int flag
)
56 if (flag
& UNVIS_END
) {
57 if (*astate
== S_OCTAL2
|| *astate
== S_OCTAL3
) {
61 return (*astate
== S_GROUND
? UNVIS_NOCHAR
: UNVIS_SYNBAD
);
81 case '0': case '1': case '2': case '3':
82 case '4': case '5': case '6': case '7':
100 return (UNVIS_VALID
);
104 return (UNVIS_VALID
);
108 return (UNVIS_VALID
);
112 return (UNVIS_VALID
);
116 return (UNVIS_VALID
);
120 return (UNVIS_VALID
);
124 return (UNVIS_VALID
);
128 return (UNVIS_VALID
);
134 return (UNVIS_NOCHAR
);
140 return (UNVIS_NOCHAR
);
143 return (UNVIS_SYNBAD
);
152 return (UNVIS_SYNBAD
);
159 return (UNVIS_VALID
);
167 return (UNVIS_VALID
);
169 case S_OCTAL2
: /* second possible octal digit */
172 * yes - and maybe a third
174 *cp
= (*cp
<< 3) + (c
- '0');
179 * no - done with current sequence, push back passed char
182 return (UNVIS_VALIDPUSH
);
184 case S_OCTAL3
: /* third possible octal digit */
187 *cp
= (*cp
<< 3) + (c
- '0');
188 return (UNVIS_VALID
);
191 * we were done, push back passed char
193 return (UNVIS_VALIDPUSH
);
197 * decoder in unknown state - (probably uninitialized)
200 return (UNVIS_SYNBAD
);
205 * strunvis - decode src into dst
207 * Number of chars decoded into dst is returned, -1 on error.
208 * Dst is null terminated.
212 strunvis(char *dst
, const char *src
)
218 while ((c
= *src
++)) {
220 switch (unvis(dst
, c
, &state
, 0)) {
224 case UNVIS_VALIDPUSH
:
235 if (unvis(dst
, c
, &state
, UNVIS_END
) == UNVIS_VALID
)
238 return (dst
- start
);
242 strnunvis(char *dst
, const char *src
, size_t sz
)
245 char *start
= dst
, *end
= dst
+ sz
- 1;
250 while ((c
= *src
++)) {
252 switch (unvis(&p
, c
, &state
, 0)) {
258 case UNVIS_VALIDPUSH
:
272 if (unvis(&p
, c
, &state
, UNVIS_END
) == UNVIS_VALID
) {
279 return (dst
- start
);