1 /* $NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy 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.44 2014/09/26 15:43:36 roy Exp $");
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include <sys/types.h>
52 __weak_alias(strnunvisx
,_strnunvisx
)
57 * decode driven by state machine
59 #define S_GROUND 0 /* haven't seen escape char */
60 #define S_START 1 /* start decoding special sequence */
61 #define S_META 2 /* metachar started (M) */
62 #define S_META1 3 /* metachar more, regular char (-) */
63 #define S_CTRL 4 /* control char started (^) */
64 #define S_OCTAL2 5 /* octal digit 2 */
65 #define S_OCTAL3 6 /* octal digit 3 */
66 #define S_HEX 7 /* mandatory hex digit */
67 #define S_HEX1 8 /* http hex digit */
68 #define S_HEX2 9 /* http hex digit 2 */
69 #define S_MIME1 10 /* mime hex digit 1 */
70 #define S_MIME2 11 /* mime hex digit 2 */
71 #define S_EATCRNL 12 /* mime eating CRNL */
72 #define S_AMP 13 /* seen & */
73 #define S_NUMBER 14 /* collecting number */
74 #define S_STRING 15 /* collecting string */
76 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
77 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
78 #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
83 static const struct nv
{
87 { "AElig", 198 }, /* capital AE diphthong (ligature) */
88 { "Aacute", 193 }, /* capital A, acute accent */
89 { "Acirc", 194 }, /* capital A, circumflex accent */
90 { "Agrave", 192 }, /* capital A, grave accent */
91 { "Aring", 197 }, /* capital A, ring */
92 { "Atilde", 195 }, /* capital A, tilde */
93 { "Auml", 196 }, /* capital A, dieresis or umlaut mark */
94 { "Ccedil", 199 }, /* capital C, cedilla */
95 { "ETH", 208 }, /* capital Eth, Icelandic */
96 { "Eacute", 201 }, /* capital E, acute accent */
97 { "Ecirc", 202 }, /* capital E, circumflex accent */
98 { "Egrave", 200 }, /* capital E, grave accent */
99 { "Euml", 203 }, /* capital E, dieresis or umlaut mark */
100 { "Iacute", 205 }, /* capital I, acute accent */
101 { "Icirc", 206 }, /* capital I, circumflex accent */
102 { "Igrave", 204 }, /* capital I, grave accent */
103 { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */
104 { "Ntilde", 209 }, /* capital N, tilde */
105 { "Oacute", 211 }, /* capital O, acute accent */
106 { "Ocirc", 212 }, /* capital O, circumflex accent */
107 { "Ograve", 210 }, /* capital O, grave accent */
108 { "Oslash", 216 }, /* capital O, slash */
109 { "Otilde", 213 }, /* capital O, tilde */
110 { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */
111 { "THORN", 222 }, /* capital THORN, Icelandic */
112 { "Uacute", 218 }, /* capital U, acute accent */
113 { "Ucirc", 219 }, /* capital U, circumflex accent */
114 { "Ugrave", 217 }, /* capital U, grave accent */
115 { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */
116 { "Yacute", 221 }, /* capital Y, acute accent */
117 { "aacute", 225 }, /* small a, acute accent */
118 { "acirc", 226 }, /* small a, circumflex accent */
119 { "acute", 180 }, /* acute accent */
120 { "aelig", 230 }, /* small ae diphthong (ligature) */
121 { "agrave", 224 }, /* small a, grave accent */
122 { "amp", 38 }, /* ampersand */
123 { "aring", 229 }, /* small a, ring */
124 { "atilde", 227 }, /* small a, tilde */
125 { "auml", 228 }, /* small a, dieresis or umlaut mark */
126 { "brvbar", 166 }, /* broken (vertical) bar */
127 { "ccedil", 231 }, /* small c, cedilla */
128 { "cedil", 184 }, /* cedilla */
129 { "cent", 162 }, /* cent sign */
130 { "copy", 169 }, /* copyright sign */
131 { "curren", 164 }, /* general currency sign */
132 { "deg", 176 }, /* degree sign */
133 { "divide", 247 }, /* divide sign */
134 { "eacute", 233 }, /* small e, acute accent */
135 { "ecirc", 234 }, /* small e, circumflex accent */
136 { "egrave", 232 }, /* small e, grave accent */
137 { "eth", 240 }, /* small eth, Icelandic */
138 { "euml", 235 }, /* small e, dieresis or umlaut mark */
139 { "frac12", 189 }, /* fraction one-half */
140 { "frac14", 188 }, /* fraction one-quarter */
141 { "frac34", 190 }, /* fraction three-quarters */
142 { "gt", 62 }, /* greater than */
143 { "iacute", 237 }, /* small i, acute accent */
144 { "icirc", 238 }, /* small i, circumflex accent */
145 { "iexcl", 161 }, /* inverted exclamation mark */
146 { "igrave", 236 }, /* small i, grave accent */
147 { "iquest", 191 }, /* inverted question mark */
148 { "iuml", 239 }, /* small i, dieresis or umlaut mark */
149 { "laquo", 171 }, /* angle quotation mark, left */
150 { "lt", 60 }, /* less than */
151 { "macr", 175 }, /* macron */
152 { "micro", 181 }, /* micro sign */
153 { "middot", 183 }, /* middle dot */
154 { "nbsp", 160 }, /* no-break space */
155 { "not", 172 }, /* not sign */
156 { "ntilde", 241 }, /* small n, tilde */
157 { "oacute", 243 }, /* small o, acute accent */
158 { "ocirc", 244 }, /* small o, circumflex accent */
159 { "ograve", 242 }, /* small o, grave accent */
160 { "ordf", 170 }, /* ordinal indicator, feminine */
161 { "ordm", 186 }, /* ordinal indicator, masculine */
162 { "oslash", 248 }, /* small o, slash */
163 { "otilde", 245 }, /* small o, tilde */
164 { "ouml", 246 }, /* small o, dieresis or umlaut mark */
165 { "para", 182 }, /* pilcrow (paragraph sign) */
166 { "plusmn", 177 }, /* plus-or-minus sign */
167 { "pound", 163 }, /* pound sterling sign */
168 { "quot", 34 }, /* double quote */
169 { "raquo", 187 }, /* angle quotation mark, right */
170 { "reg", 174 }, /* registered sign */
171 { "sect", 167 }, /* section sign */
172 { "shy", 173 }, /* soft hyphen */
173 { "sup1", 185 }, /* superscript one */
174 { "sup2", 178 }, /* superscript two */
175 { "sup3", 179 }, /* superscript three */
176 { "szlig", 223 }, /* small sharp s, German (sz ligature) */
177 { "thorn", 254 }, /* small thorn, Icelandic */
178 { "times", 215 }, /* multiply sign */
179 { "uacute", 250 }, /* small u, acute accent */
180 { "ucirc", 251 }, /* small u, circumflex accent */
181 { "ugrave", 249 }, /* small u, grave accent */
182 { "uml", 168 }, /* umlaut (dieresis) */
183 { "uuml", 252 }, /* small u, dieresis or umlaut mark */
184 { "yacute", 253 }, /* small y, acute accent */
185 { "yen", 165 }, /* yen sign */
186 { "yuml", 255 }, /* small y, dieresis or umlaut mark */
190 * unvis - decode characters previously encoded by vis
193 unvis(char *cp
, int c
, int *astate
, int flag
)
195 unsigned char uc
= (unsigned char)c
;
196 unsigned char st
, ia
, is
, lc
;
199 * Bottom 8 bits of astate hold the state machine state.
200 * Top 8 bits hold the current character in the http 1866 nv string decoding
202 #define GS(a) ((a) & 0xff)
203 #define SS(a, b) (((uint32_t)(a) << 24) | (b))
204 #define GI(a) ((uint32_t)(a) >> 24)
206 _DIAGASSERT(cp
!= NULL
);
207 _DIAGASSERT(astate
!= NULL
);
210 if (flag
& UNVIS_END
) {
215 *astate
= SS(0, S_GROUND
);
228 if ((flag
& VIS_NOESCAPE
) == 0 && c
== '\\') {
229 *astate
= SS(0, S_START
);
232 if ((flag
& VIS_HTTP1808
) && c
== '%') {
233 *astate
= SS(0, S_HEX1
);
236 if ((flag
& VIS_HTTP1866
) && c
== '&') {
237 *astate
= SS(0, S_AMP
);
240 if ((flag
& VIS_MIMESTYLE
) && c
== '=') {
241 *astate
= SS(0, S_MIME1
);
251 *astate
= SS(0, S_GROUND
);
253 case '0': case '1': case '2': case '3':
254 case '4': case '5': case '6': case '7':
256 *astate
= SS(0, S_OCTAL2
);
260 *astate
= SS(0, S_META
);
263 *astate
= SS(0, S_CTRL
);
267 *astate
= SS(0, S_GROUND
);
271 *astate
= SS(0, S_GROUND
);
275 *astate
= SS(0, S_GROUND
);
279 *astate
= SS(0, S_GROUND
);
283 *astate
= SS(0, S_GROUND
);
287 *astate
= SS(0, S_GROUND
);
291 *astate
= SS(0, S_GROUND
);
295 *astate
= SS(0, S_GROUND
);
299 *astate
= SS(0, S_GROUND
);
302 *astate
= SS(0, S_HEX
);
308 *astate
= SS(0, S_GROUND
);
314 *astate
= SS(0, S_GROUND
);
319 *astate
= SS(0, S_GROUND
);
327 *astate
= SS(0, S_META1
);
329 *astate
= SS(0, S_CTRL
);
335 *astate
= SS(0, S_GROUND
);
344 *astate
= SS(0, S_GROUND
);
347 case S_OCTAL2
: /* second possible octal digit */
350 * yes - and maybe a third
352 *cp
= (*cp
<< 3) + (c
- '0');
353 *astate
= SS(0, S_OCTAL3
);
357 * no - done with current sequence, push back passed char
359 *astate
= SS(0, S_GROUND
);
360 return UNVIS_VALIDPUSH
;
362 case S_OCTAL3
: /* third possible octal digit */
363 *astate
= SS(0, S_GROUND
);
365 *cp
= (*cp
<< 3) + (c
- '0');
369 * we were done, push back passed char
371 return UNVIS_VALIDPUSH
;
380 *astate
= SS(0, S_HEX2
);
384 * no - done with current sequence, push back passed char
386 *astate
= SS(0, S_GROUND
);
387 return UNVIS_VALIDPUSH
;
392 *cp
= xtod(uc
) | (*cp
<< 4);
395 return UNVIS_VALIDPUSH
;
398 if (uc
== '\n' || uc
== '\r') {
399 *astate
= SS(0, S_EATCRNL
);
402 if (isxdigit(uc
) && (isdigit(uc
) || isupper(uc
))) {
404 *astate
= SS(0, S_MIME2
);
410 if (isxdigit(uc
) && (isdigit(uc
) || isupper(uc
))) {
411 *astate
= SS(0, S_GROUND
);
412 *cp
= XTOD(uc
) | (*cp
<< 4);
423 *astate
= SS(0, S_MIME1
);
427 *astate
= SS(0, S_GROUND
);
434 *astate
= SS(0, S_NUMBER
);
437 *astate
= SS(0, S_STRING
);
441 ia
= *cp
; /* index in the array */
442 is
= GI(*astate
); /* index in the string */
443 lc
= is
== 0 ? 0 : nv
[ia
].name
[is
- 1]; /* last character */
448 for (; ia
< __arraycount(nv
); ia
++) {
449 if (is
!= 0 && nv
[ia
].name
[is
- 1] != lc
)
451 if (nv
[ia
].name
[is
] == uc
)
455 if (ia
== __arraycount(nv
))
460 *astate
= SS(is
+ 1, S_STRING
);
465 *astate
= SS(0, S_GROUND
);
473 *cp
+= (*cp
* 10) + uc
- '0';
479 * decoder in unknown state - (probably uninitialized)
481 *astate
= SS(0, S_GROUND
);
487 * strnunvisx - decode src into dst
489 * Number of chars decoded into dst is returned, -1 on error.
490 * Dst is null terminated.
494 strnunvisx(char *dst
, size_t dlen
, const char *src
, int flag
)
497 char t
= '\0', *start
= dst
;
500 _DIAGASSERT(src
!= NULL
);
501 _DIAGASSERT(dst
!= NULL
);
502 #define CHECKSPACE() \
508 } while (/*CONSTCOND*/0)
510 while ((c
= *src
++) != '\0') {
512 switch (unvis(&t
, c
, &state
, flag
)) {
517 case UNVIS_VALIDPUSH
:
528 _DIAGASSERT(/*CONSTCOND*/0);
533 if (unvis(&t
, c
, &state
, UNVIS_END
) == UNVIS_VALID
) {
539 return (int)(dst
- start
);
543 strunvisx(char *dst
, const char *src
, int flag
)
545 return strnunvisx(dst
, (size_t)~0, src
, flag
);
549 strunvis(char *dst
, const char *src
)
551 return strnunvisx(dst
, (size_t)~0, src
, 0);
555 strnunvis(char *dst
, size_t dlen
, const char *src
)
557 return strnunvisx(dst
, dlen
, src
, 0);