1
/* $NetBSD: unvis.c,v 1.22 2002/03/23 17:38:27 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. 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
37
#if defined(LIBC_SCCS) && !defined(lint)
39
static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
41
__RCSID("$NetBSD: unvis.c,v 1.22 2002/03/23 17:38:27 christos Exp $");
43
#endif /* LIBC_SCCS and not lint */
45
#define __LIBC12_SOURCE__
47
#include <sys/types.h>
55
__weak_alias(strunvis,_strunvis)
56
__weak_alias(unvis,_unvis)
59
#ifdef __warn_references
60
__warn_references(unvis,
61
"warning: reference to compatibility unvis(); include <vis.h> for correct reference")
66
* decode driven by state machine
68
#define S_GROUND 0 /* haven't seen escape char */
69
#define S_START 1 /* start decoding special sequence */
70
#define S_META 2 /* metachar started (M) */
71
#define S_META1 3 /* metachar more, regular char (-) */
72
#define S_CTRL 4 /* control char started (^) */
73
#define S_OCTAL2 5 /* octal digit 2 */
74
#define S_OCTAL3 6 /* octal digit 3 */
75
#define S_HEX1 7 /* hex digit */
76
#define S_HEX2 8 /* hex digit 2 */
78
#define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
79
#define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
82
unvis(cp, c, astate, flag)
87
return __unvis13(cp, (int)c, astate, flag);
91
* unvis - decode characters previously encoded by vis
94
__unvis13(cp, c, astate, flag)
100
_DIAGASSERT(cp != NULL);
101
_DIAGASSERT(astate != NULL);
103
if (flag & UNVIS_END) {
104
if (*astate == S_OCTAL2 || *astate == S_OCTAL3
105
|| *astate == S_HEX2) {
107
return (UNVIS_VALID);
109
return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
120
if ((flag & VIS_HTTPSTYLE) && c == '%') {
125
return (UNVIS_VALID);
132
return (UNVIS_VALID);
133
case '0': case '1': case '2': case '3':
134
case '4': case '5': case '6': case '7':
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);
176
return (UNVIS_VALID);
180
return (UNVIS_VALID);
186
return (UNVIS_NOCHAR);
192
return (UNVIS_NOCHAR);
195
return (UNVIS_SYNBAD);
204
return (UNVIS_SYNBAD);
211
return (UNVIS_VALID);
219
return (UNVIS_VALID);
221
case S_OCTAL2: /* second possible octal digit */
224
* yes - and maybe a third
226
*cp = (*cp << 3) + (c - '0');
231
* no - done with current sequence, push back passed char
234
return (UNVIS_VALIDPUSH);
236
case S_OCTAL3: /* third possible octal digit */
239
*cp = (*cp << 3) + (c - '0');
240
return (UNVIS_VALID);
243
* we were done, push back passed char
245
return (UNVIS_VALIDPUSH);
253
* no - done with current sequence, push back passed char
256
return (UNVIS_VALIDPUSH);
260
*cp = xtod(c) | (*cp << 4);
261
return (UNVIS_VALID);
263
return (UNVIS_VALIDPUSH);
266
* decoder in unknown state - (probably uninitialized)
269
return (UNVIS_SYNBAD);
274
* strunvis - decode src into dst
276
* Number of chars decoded into dst is returned, -1 on error.
277
* Dst is null terminated.
281
strunvisx(dst, src, flag)
290
_DIAGASSERT(src != NULL);
291
_DIAGASSERT(dst != NULL);
293
while ((c = *src++) != '\0') {
295
switch (__unvis13(dst, c, &state, flag)) {
299
case UNVIS_VALIDPUSH:
309
if (__unvis13(dst, c, &state, UNVIS_END) == UNVIS_VALID)
312
return (dst - start);
320
return strunvisx(dst, src, 0);