1 /* @(#)unvis.c 8.1 (Berkeley) 6/4/93 */
2 /* $NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include "namespace.h"
34 #include <sys/types.h>
42 #include "un-namespace.h"
45 * Return the number of elements in a statically-allocated array,
48 #define __arraycount(__x) (sizeof(__x) / sizeof(__x[0]))
51 __weak_alias(strnunvisx
,_strnunvisx
)
56 * decode driven by state machine
58 #define S_GROUND 0 /* haven't seen escape char */
59 #define S_START 1 /* start decoding special sequence */
60 #define S_META 2 /* metachar started (M) */
61 #define S_META1 3 /* metachar more, regular char (-) */
62 #define S_CTRL 4 /* control char started (^) */
63 #define S_OCTAL2 5 /* octal digit 2 */
64 #define S_OCTAL3 6 /* octal digit 3 */
65 #define S_HEX 7 /* mandatory hex digit */
66 #define S_HEX1 8 /* http hex digit */
67 #define S_HEX2 9 /* http hex digit 2 */
68 #define S_MIME1 10 /* mime hex digit 1 */
69 #define S_MIME2 11 /* mime hex digit 2 */
70 #define S_EATCRNL 12 /* mime eating CRNL */
71 #define S_AMP 13 /* seen & */
72 #define S_NUMBER 14 /* collecting number */
73 #define S_STRING 15 /* collecting string */
75 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
76 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
77 #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
82 static const struct nv
{
86 { "AElig", 198 }, /* capital AE diphthong (ligature) */
87 { "Aacute", 193 }, /* capital A, acute accent */
88 { "Acirc", 194 }, /* capital A, circumflex accent */
89 { "Agrave", 192 }, /* capital A, grave accent */
90 { "Aring", 197 }, /* capital A, ring */
91 { "Atilde", 195 }, /* capital A, tilde */
92 { "Auml", 196 }, /* capital A, dieresis or umlaut mark */
93 { "Ccedil", 199 }, /* capital C, cedilla */
94 { "ETH", 208 }, /* capital Eth, Icelandic */
95 { "Eacute", 201 }, /* capital E, acute accent */
96 { "Ecirc", 202 }, /* capital E, circumflex accent */
97 { "Egrave", 200 }, /* capital E, grave accent */
98 { "Euml", 203 }, /* capital E, dieresis or umlaut mark */
99 { "Iacute", 205 }, /* capital I, acute accent */
100 { "Icirc", 206 }, /* capital I, circumflex accent */
101 { "Igrave", 204 }, /* capital I, grave accent */
102 { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */
103 { "Ntilde", 209 }, /* capital N, tilde */
104 { "Oacute", 211 }, /* capital O, acute accent */
105 { "Ocirc", 212 }, /* capital O, circumflex accent */
106 { "Ograve", 210 }, /* capital O, grave accent */
107 { "Oslash", 216 }, /* capital O, slash */
108 { "Otilde", 213 }, /* capital O, tilde */
109 { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */
110 { "THORN", 222 }, /* capital THORN, Icelandic */
111 { "Uacute", 218 }, /* capital U, acute accent */
112 { "Ucirc", 219 }, /* capital U, circumflex accent */
113 { "Ugrave", 217 }, /* capital U, grave accent */
114 { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */
115 { "Yacute", 221 }, /* capital Y, acute accent */
116 { "aacute", 225 }, /* small a, acute accent */
117 { "acirc", 226 }, /* small a, circumflex accent */
118 { "acute", 180 }, /* acute accent */
119 { "aelig", 230 }, /* small ae diphthong (ligature) */
120 { "agrave", 224 }, /* small a, grave accent */
121 { "amp", 38 }, /* ampersand */
122 { "aring", 229 }, /* small a, ring */
123 { "atilde", 227 }, /* small a, tilde */
124 { "auml", 228 }, /* small a, dieresis or umlaut mark */
125 { "brvbar", 166 }, /* broken (vertical) bar */
126 { "ccedil", 231 }, /* small c, cedilla */
127 { "cedil", 184 }, /* cedilla */
128 { "cent", 162 }, /* cent sign */
129 { "copy", 169 }, /* copyright sign */
130 { "curren", 164 }, /* general currency sign */
131 { "deg", 176 }, /* degree sign */
132 { "divide", 247 }, /* divide sign */
133 { "eacute", 233 }, /* small e, acute accent */
134 { "ecirc", 234 }, /* small e, circumflex accent */
135 { "egrave", 232 }, /* small e, grave accent */
136 { "eth", 240 }, /* small eth, Icelandic */
137 { "euml", 235 }, /* small e, dieresis or umlaut mark */
138 { "frac12", 189 }, /* fraction one-half */
139 { "frac14", 188 }, /* fraction one-quarter */
140 { "frac34", 190 }, /* fraction three-quarters */
141 { "gt", 62 }, /* greater than */
142 { "iacute", 237 }, /* small i, acute accent */
143 { "icirc", 238 }, /* small i, circumflex accent */
144 { "iexcl", 161 }, /* inverted exclamation mark */
145 { "igrave", 236 }, /* small i, grave accent */
146 { "iquest", 191 }, /* inverted question mark */
147 { "iuml", 239 }, /* small i, dieresis or umlaut mark */
148 { "laquo", 171 }, /* angle quotation mark, left */
149 { "lt", 60 }, /* less than */
150 { "macr", 175 }, /* macron */
151 { "micro", 181 }, /* micro sign */
152 { "middot", 183 }, /* middle dot */
153 { "nbsp", 160 }, /* no-break space */
154 { "not", 172 }, /* not sign */
155 { "ntilde", 241 }, /* small n, tilde */
156 { "oacute", 243 }, /* small o, acute accent */
157 { "ocirc", 244 }, /* small o, circumflex accent */
158 { "ograve", 242 }, /* small o, grave accent */
159 { "ordf", 170 }, /* ordinal indicator, feminine */
160 { "ordm", 186 }, /* ordinal indicator, masculine */
161 { "oslash", 248 }, /* small o, slash */
162 { "otilde", 245 }, /* small o, tilde */
163 { "ouml", 246 }, /* small o, dieresis or umlaut mark */
164 { "para", 182 }, /* pilcrow (paragraph sign) */
165 { "plusmn", 177 }, /* plus-or-minus sign */
166 { "pound", 163 }, /* pound sterling sign */
167 { "quot", 34 }, /* double quote */
168 { "raquo", 187 }, /* angle quotation mark, right */
169 { "reg", 174 }, /* registered sign */
170 { "sect", 167 }, /* section sign */
171 { "shy", 173 }, /* soft hyphen */
172 { "sup1", 185 }, /* superscript one */
173 { "sup2", 178 }, /* superscript two */
174 { "sup3", 179 }, /* superscript three */
175 { "szlig", 223 }, /* small sharp s, German (sz ligature) */
176 { "thorn", 254 }, /* small thorn, Icelandic */
177 { "times", 215 }, /* multiply sign */
178 { "uacute", 250 }, /* small u, acute accent */
179 { "ucirc", 251 }, /* small u, circumflex accent */
180 { "ugrave", 249 }, /* small u, grave accent */
181 { "uml", 168 }, /* umlaut (dieresis) */
182 { "uuml", 252 }, /* small u, dieresis or umlaut mark */
183 { "yacute", 253 }, /* small y, acute accent */
184 { "yen", 165 }, /* yen sign */
185 { "yuml", 255 }, /* small y, dieresis or umlaut mark */
189 * unvis - decode characters previously encoded by vis
192 unvis(char *cp
, int c
, int *astate
, int flag
)
194 unsigned char uc
= (unsigned char)c
;
195 unsigned char st
, ia
, is
, lc
;
198 * Bottom 8 bits of astate hold the state machine state.
199 * Top 8 bits hold the current character in the http 1866 nv string decoding
201 #define GS(a) ((a) & 0xff)
202 #define SS(a, b) (((uint32_t)(a) << 24) | (b))
203 #define GI(a) ((uint32_t)(a) >> 24)
205 _DIAGASSERT(cp
!= NULL
);
206 _DIAGASSERT(astate
!= NULL
);
209 if (flag
& UNVIS_END
) {
214 *astate
= SS(0, S_GROUND
);
227 if ((flag
& VIS_NOESCAPE
) == 0 && c
== '\\') {
228 *astate
= SS(0, S_START
);
231 if ((flag
& VIS_HTTP1808
) && c
== '%') {
232 *astate
= SS(0, S_HEX1
);
235 if ((flag
& VIS_HTTP1866
) && c
== '&') {
236 *astate
= SS(0, S_AMP
);
239 if ((flag
& VIS_MIMESTYLE
) && c
== '=') {
240 *astate
= SS(0, S_MIME1
);
250 *astate
= SS(0, S_GROUND
);
254 *astate
= SS(0, S_GROUND
);
256 case '0': case '1': case '2': case '3':
257 case '4': case '5': case '6': case '7':
259 *astate
= SS(0, S_OCTAL2
);
263 *astate
= SS(0, S_META
);
266 *astate
= SS(0, S_CTRL
);
270 *astate
= SS(0, S_GROUND
);
274 *astate
= SS(0, S_GROUND
);
278 *astate
= SS(0, S_GROUND
);
282 *astate
= SS(0, S_GROUND
);
286 *astate
= SS(0, S_GROUND
);
290 *astate
= SS(0, S_GROUND
);
294 *astate
= SS(0, S_GROUND
);
298 *astate
= SS(0, S_GROUND
);
302 *astate
= SS(0, S_GROUND
);
305 *astate
= SS(0, S_HEX
);
311 *astate
= SS(0, S_GROUND
);
317 *astate
= SS(0, S_GROUND
);
322 *astate
= SS(0, S_GROUND
);
330 *astate
= SS(0, S_META1
);
332 *astate
= SS(0, S_CTRL
);
338 *astate
= SS(0, S_GROUND
);
347 *astate
= SS(0, S_GROUND
);
350 case S_OCTAL2
: /* second possible octal digit */
353 * yes - and maybe a third
355 *cp
= (*cp
<< 3) + (c
- '0');
356 *astate
= SS(0, S_OCTAL3
);
360 * no - done with current sequence, push back passed char
362 *astate
= SS(0, S_GROUND
);
363 return UNVIS_VALIDPUSH
;
365 case S_OCTAL3
: /* third possible octal digit */
366 *astate
= SS(0, S_GROUND
);
368 *cp
= (*cp
<< 3) + (c
- '0');
372 * we were done, push back passed char
374 return UNVIS_VALIDPUSH
;
383 *astate
= SS(0, S_HEX2
);
387 * no - done with current sequence, push back passed char
389 *astate
= SS(0, S_GROUND
);
390 return UNVIS_VALIDPUSH
;
395 *cp
= xtod(uc
) | (*cp
<< 4);
398 return UNVIS_VALIDPUSH
;
401 if (uc
== '\n' || uc
== '\r') {
402 *astate
= SS(0, S_EATCRNL
);
405 if (isxdigit(uc
) && (isdigit(uc
) || isupper(uc
))) {
407 *astate
= SS(0, S_MIME2
);
413 if (isxdigit(uc
) && (isdigit(uc
) || isupper(uc
))) {
414 *astate
= SS(0, S_GROUND
);
415 *cp
= XTOD(uc
) | (*cp
<< 4);
426 *astate
= SS(0, S_MIME1
);
430 *astate
= SS(0, S_GROUND
);
437 *astate
= SS(0, S_NUMBER
);
440 *astate
= SS(0, S_STRING
);
444 ia
= *cp
; /* index in the array */
445 is
= GI(*astate
); /* index in the string */
446 lc
= is
== 0 ? 0 : nv
[ia
].name
[is
- 1]; /* last character */
451 for (; ia
< __arraycount(nv
); ia
++) {
452 if (is
!= 0 && nv
[ia
].name
[is
- 1] != lc
)
454 if (nv
[ia
].name
[is
] == uc
)
458 if (ia
== __arraycount(nv
))
463 *astate
= SS(is
+ 1, S_STRING
);
468 *astate
= SS(0, S_GROUND
);
476 *cp
+= (*cp
* 10) + uc
- '0';
482 * decoder in unknown state - (probably uninitialized)
484 *astate
= SS(0, S_GROUND
);
490 * strnunvisx - decode src into dst
492 * Number of chars decoded into dst is returned, -1 on error.
493 * Dst is null terminated.
497 strnunvisx(char *dst
, size_t dlen
, const char *src
, int flag
)
500 char t
= '\0', *start
= dst
;
503 _DIAGASSERT(src
!= NULL
);
504 _DIAGASSERT(dst
!= NULL
);
505 #define CHECKSPACE() \
511 } while (/*CONSTCOND*/0)
513 while ((c
= *src
++) != '\0') {
515 switch (unvis(&t
, c
, &state
, flag
)) {
520 case UNVIS_VALIDPUSH
:
531 _DIAGASSERT(/*CONSTCOND*/0);
536 if (unvis(&t
, c
, &state
, UNVIS_END
) == UNVIS_VALID
) {
542 return (int)(dst
- start
);
546 strunvisx(char *dst
, const char *src
, int flag
)
548 return strnunvisx(dst
, (size_t)~0, src
, flag
);
552 strunvis(char *dst
, const char *src
)
554 return strnunvisx(dst
, (size_t)~0, src
, 0);
558 strnunvis(char *dst
, size_t dlen
, const char *src
)
560 return strnunvisx(dst
, dlen
, src
, 0);