1 /* $NetBSD: unvis.c,v 1.29 2009/02/10 23:06:31 christos 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. 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.
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
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
35 static char sccsid
[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
37 __RCSID("$NetBSD: unvis.c,v 1.29 2009/02/10 23:06:31 christos Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include <sys/types.h>
50 __weak_alias(strunvis
,_strunvis
)
55 * decode driven by state machine
57 #define S_GROUND 0 /* haven't seen escape char */
58 #define S_START 1 /* start decoding special sequence */
59 #define S_META 2 /* metachar started (M) */
60 #define S_META1 3 /* metachar more, regular char (-) */
61 #define S_CTRL 4 /* control char started (^) */
62 #define S_OCTAL2 5 /* octal digit 2 */
63 #define S_OCTAL3 6 /* octal digit 3 */
64 #define S_HEX1 7 /* http hex digit */
65 #define S_HEX2 8 /* http hex digit 2 */
66 #define S_MIME1 9 /* mime hex digit 1 */
67 #define S_MIME2 10 /* mime hex digit 2 */
68 #define S_EATCRNL 11 /* mime eating CRNL */
70 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
71 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
72 #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
75 * unvis - decode characters previously encoded by vis
78 unvis(char *cp
, int c
, int *astate
, int flag
)
80 unsigned char uc
= (unsigned char)c
;
82 _DIAGASSERT(cp
!= NULL
);
83 _DIAGASSERT(astate
!= NULL
);
85 if (flag
& UNVIS_END
) {
86 if (*astate
== S_OCTAL2
|| *astate
== S_OCTAL3
87 || *astate
== S_HEX2
) {
91 return (*astate
== S_GROUND
? UNVIS_NOCHAR
: UNVIS_SYNBAD
);
102 if ((flag
& VIS_HTTPSTYLE
) && c
== '%') {
106 if ((flag
& VIS_MIMESTYLE
) && c
== '=') {
119 case '0': case '1': case '2': case '3':
120 case '4': case '5': case '6': case '7':
172 return (UNVIS_NOCHAR
);
178 return (UNVIS_NOCHAR
);
181 return (UNVIS_SYNBAD
);
190 return (UNVIS_SYNBAD
);
207 case S_OCTAL2
: /* second possible octal digit */
210 * yes - and maybe a third
212 *cp
= (*cp
<< 3) + (c
- '0');
217 * no - done with current sequence, push back passed char
220 return UNVIS_VALIDPUSH
;
222 case S_OCTAL3
: /* third possible octal digit */
225 *cp
= (*cp
<< 3) + (c
- '0');
229 * we were done, push back passed char
231 return UNVIS_VALIDPUSH
;
240 * no - done with current sequence, push back passed char
243 return UNVIS_VALIDPUSH
;
248 *cp
= xtod(uc
) | (*cp
<< 4);
251 return UNVIS_VALIDPUSH
;
254 if (uc
== '\n' || uc
== '\r') {
258 if (isxdigit(uc
) && (isdigit(uc
) || isupper(uc
))) {
267 if (isxdigit(uc
) && (isdigit(uc
) || isupper(uc
))) {
269 *cp
= XTOD(uc
) | (*cp
<< 4);
290 * decoder in unknown state - (probably uninitialized)
298 * strunvis - decode src into dst
300 * Number of chars decoded into dst is returned, -1 on error.
301 * Dst is null terminated.
305 strunvisx(dst
, src
, flag
)
314 _DIAGASSERT(src
!= NULL
);
315 _DIAGASSERT(dst
!= NULL
);
317 while ((c
= *src
++) != '\0') {
319 switch (unvis(dst
, c
, &state
, flag
)) {
323 case UNVIS_VALIDPUSH
:
333 if (unvis(dst
, c
, &state
, UNVIS_END
) == UNVIS_VALID
)
336 return (int)(dst
- start
);
344 return strunvisx(dst
, src
, 0);