1 /* $NetBSD: unvis.c,v 1.1.1.2 2014/04/24 12:45:52 pettai Exp $ */
3 /* NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp */
6 * Copyright (c) 1989, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <krb5/roken.h>
38 #define _DIAGASSERT(X)
41 #include <sys/cdefs.h>
42 #if defined(LIBC_SCCS) && !defined(lint)
44 static char sccsid
[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
46 __RCSID("NetBSD: unvis.c,v 1.19 2000/01/22 22:19:13 mycroft Exp");
48 #endif /* LIBC_SCCS and not lint */
50 #define __LIBC12_SOURCE__
52 #include "namespace.h"
54 #include <sys/types.h>
63 __weak_alias(strunvis
,_strunvis
)
64 __weak_alias(unvis
,_unvis
)
67 __warn_references(unvis
,
68 "warning: reference to compatibility unvis(); include <vis.h> for correct reference")
72 * decode driven by state machine
74 #define S_GROUND 0 /* haven't seen escape char */
75 #define S_START 1 /* start decoding special sequence */
76 #define S_META 2 /* metachar started (M) */
77 #define S_META1 3 /* metachar more, regular char (-) */
78 #define S_CTRL 4 /* control char started (^) */
79 #define S_OCTAL2 5 /* octal digit 2 */
80 #define S_OCTAL3 6 /* octal digit 3 */
82 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
84 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
85 rk_strunvis (char *, const char *);
86 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
87 rk_unvis (char *, int, int *, int);
90 * unvis - decode characters previously encoded by vis
93 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
94 rk_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
);
249 * strunvis - decode src into dst
251 * Number of chars decoded into dst is returned, -1 on error.
252 * Dst is null terminated.
255 ROKEN_LIB_FUNCTION
int ROKEN_LIB_CALL
256 rk_strunvis(char *dst
, const char *src
)
262 _DIAGASSERT(src
!= NULL
);
263 _DIAGASSERT(dst
!= NULL
);
265 while ((c
= *src
++) != '\0') {
267 switch (rk_unvis(dst
, (unsigned char)c
, &state
, 0)) {
271 case UNVIS_VALIDPUSH
:
281 if (unvis(dst
, (unsigned char)c
, &state
, UNVIS_END
) == UNVIS_VALID
)
284 return (dst
- start
);