1 /* $NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
39 RCSID("$Id: unvis.c,v 1.2 2000/12/06 21:41:46 joda Exp $");
43 #define _DIAGASSERT(X)
46 #include <sys/cdefs.h>
47 #if defined(LIBC_SCCS) && !defined(lint)
49 static char sccsid
[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
51 __RCSID("$NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp $");
53 #endif /* LIBC_SCCS and not lint */
55 #define __LIBC12_SOURCE__
57 #include "namespace.h"
59 #include <sys/types.h>
68 __weak_alias(strunvis
,_strunvis
)
69 __weak_alias(unvis
,_unvis
)
72 __warn_references(unvis
,
73 "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
77 * decode driven by state machine
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 */
87 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
90 * unvis - decode characters previously encoded by vis
94 unvis(char *cp
, int c
, int *astate
, int flag
)
97 _DIAGASSERT(cp
!= NULL
);
98 _DIAGASSERT(astate
!= NULL
);
100 if (flag
& UNVIS_END
) {
101 if (*astate
== S_OCTAL2
|| *astate
== S_OCTAL3
) {
103 return (UNVIS_VALID
);
105 return (*astate
== S_GROUND
? UNVIS_NOCHAR
: UNVIS_SYNBAD
);
117 return (UNVIS_VALID
);
124 return (UNVIS_VALID
);
125 case '0': case '1': case '2': case '3':
126 case '4': case '5': case '6': case '7':
140 return (UNVIS_VALID
);
144 return (UNVIS_VALID
);
148 return (UNVIS_VALID
);
152 return (UNVIS_VALID
);
156 return (UNVIS_VALID
);
160 return (UNVIS_VALID
);
164 return (UNVIS_VALID
);
168 return (UNVIS_VALID
);
172 return (UNVIS_VALID
);
178 return (UNVIS_NOCHAR
);
184 return (UNVIS_NOCHAR
);
187 return (UNVIS_SYNBAD
);
196 return (UNVIS_SYNBAD
);
203 return (UNVIS_VALID
);
211 return (UNVIS_VALID
);
213 case S_OCTAL2
: /* second possible octal digit */
216 * yes - and maybe a third
218 *cp
= (*cp
<< 3) + (c
- '0');
223 * no - done with current sequence, push back passed char
226 return (UNVIS_VALIDPUSH
);
228 case S_OCTAL3
: /* third possible octal digit */
231 *cp
= (*cp
<< 3) + (c
- '0');
232 return (UNVIS_VALID
);
235 * we were done, push back passed char
237 return (UNVIS_VALIDPUSH
);
241 * decoder in unknown state - (probably uninitialized)
244 return (UNVIS_SYNBAD
);
250 * strunvis - decode src into dst
252 * Number of chars decoded into dst is returned, -1 on error.
253 * Dst is null terminated.
256 #ifndef HAVE_STRUNVIS
258 strunvis(char *dst
, const char *src
)
264 _DIAGASSERT(src
!= NULL
);
265 _DIAGASSERT(dst
!= NULL
);
267 while ((c
= *src
++) != '\0') {
269 switch (unvis(dst
, c
, &state
, 0)) {
273 case UNVIS_VALIDPUSH
:
283 if (unvis(dst
, c
, &state
, UNVIS_END
) == UNVIS_VALID
)
286 return (dst
- start
);