libc, libutil: remove compat hacks
[minix.git] / lib / libc / gen / unvis.c
blob13bc869fcd3d201f5ba329767790366f5ef2c0f3
1 /* $NetBSD: unvis.c,v 1.32 2010/11/27 21:22:11 christos Exp $ */
3 /*-
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
9 * are met:
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
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #if defined(LIBC_SCCS) && !defined(lint)
34 #if 0
35 static char sccsid[] = "@(#)unvis.c 8.1 (Berkeley) 6/4/93";
36 #else
37 __RCSID("$NetBSD: unvis.c,v 1.32 2010/11/27 21:22:11 christos Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
41 #include "namespace.h"
42 #include <sys/types.h>
44 #include <assert.h>
45 #include <ctype.h>
46 #include <stdio.h>
47 #include <vis.h>
49 #ifdef __weak_alias
50 __weak_alias(strunvis,_strunvis)
51 #endif
53 #if !HAVE_VIS
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 */
69 #define S_AMP 12 /* seen & */
70 #define S_NUMBER 13 /* collecting number */
71 #define S_STRING 14 /* collecting string */
73 #define isoctal(c) (((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
74 #define xtod(c) (isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
75 #define XTOD(c) (isdigit(c) ? (c - '0') : ((c - 'A') + 10))
78 * RFC 1866
80 static const struct nv {
81 const char *name;
82 uint8_t value;
83 } nv[] = {
84 { "AElig", 198 }, /* capital AE diphthong (ligature) */
85 { "Aacute", 193 }, /* capital A, acute accent */
86 { "Acirc", 194 }, /* capital A, circumflex accent */
87 { "Agrave", 192 }, /* capital A, grave accent */
88 { "Aring", 197 }, /* capital A, ring */
89 { "Atilde", 195 }, /* capital A, tilde */
90 { "Auml", 196 }, /* capital A, dieresis or umlaut mark */
91 { "Ccedil", 199 }, /* capital C, cedilla */
92 { "ETH", 208 }, /* capital Eth, Icelandic */
93 { "Eacute", 201 }, /* capital E, acute accent */
94 { "Ecirc", 202 }, /* capital E, circumflex accent */
95 { "Egrave", 200 }, /* capital E, grave accent */
96 { "Euml", 203 }, /* capital E, dieresis or umlaut mark */
97 { "Iacute", 205 }, /* capital I, acute accent */
98 { "Icirc", 206 }, /* capital I, circumflex accent */
99 { "Igrave", 204 }, /* capital I, grave accent */
100 { "Iuml", 207 }, /* capital I, dieresis or umlaut mark */
101 { "Ntilde", 209 }, /* capital N, tilde */
102 { "Oacute", 211 }, /* capital O, acute accent */
103 { "Ocirc", 212 }, /* capital O, circumflex accent */
104 { "Ograve", 210 }, /* capital O, grave accent */
105 { "Oslash", 216 }, /* capital O, slash */
106 { "Otilde", 213 }, /* capital O, tilde */
107 { "Ouml", 214 }, /* capital O, dieresis or umlaut mark */
108 { "THORN", 222 }, /* capital THORN, Icelandic */
109 { "Uacute", 218 }, /* capital U, acute accent */
110 { "Ucirc", 219 }, /* capital U, circumflex accent */
111 { "Ugrave", 217 }, /* capital U, grave accent */
112 { "Uuml", 220 }, /* capital U, dieresis or umlaut mark */
113 { "Yacute", 221 }, /* capital Y, acute accent */
114 { "aacute", 225 }, /* small a, acute accent */
115 { "acirc", 226 }, /* small a, circumflex accent */
116 { "acute", 180 }, /* acute accent */
117 { "aelig", 230 }, /* small ae diphthong (ligature) */
118 { "agrave", 224 }, /* small a, grave accent */
119 { "amp", 38 }, /* ampersand */
120 { "aring", 229 }, /* small a, ring */
121 { "atilde", 227 }, /* small a, tilde */
122 { "auml", 228 }, /* small a, dieresis or umlaut mark */
123 { "brvbar", 166 }, /* broken (vertical) bar */
124 { "ccedil", 231 }, /* small c, cedilla */
125 { "cedil", 184 }, /* cedilla */
126 { "cent", 162 }, /* cent sign */
127 { "copy", 169 }, /* copyright sign */
128 { "curren", 164 }, /* general currency sign */
129 { "deg", 176 }, /* degree sign */
130 { "divide", 247 }, /* divide sign */
131 { "eacute", 233 }, /* small e, acute accent */
132 { "ecirc", 234 }, /* small e, circumflex accent */
133 { "egrave", 232 }, /* small e, grave accent */
134 { "eth", 240 }, /* small eth, Icelandic */
135 { "euml", 235 }, /* small e, dieresis or umlaut mark */
136 { "frac12", 189 }, /* fraction one-half */
137 { "frac14", 188 }, /* fraction one-quarter */
138 { "frac34", 190 }, /* fraction three-quarters */
139 { "gt", 62 }, /* greater than */
140 { "iacute", 237 }, /* small i, acute accent */
141 { "icirc", 238 }, /* small i, circumflex accent */
142 { "iexcl", 161 }, /* inverted exclamation mark */
143 { "igrave", 236 }, /* small i, grave accent */
144 { "iquest", 191 }, /* inverted question mark */
145 { "iuml", 239 }, /* small i, dieresis or umlaut mark */
146 { "laquo", 171 }, /* angle quotation mark, left */
147 { "lt", 60 }, /* less than */
148 { "macr", 175 }, /* macron */
149 { "micro", 181 }, /* micro sign */
150 { "middot", 183 }, /* middle dot */
151 { "nbsp", 160 }, /* no-break space */
152 { "not", 172 }, /* not sign */
153 { "ntilde", 241 }, /* small n, tilde */
154 { "oacute", 243 }, /* small o, acute accent */
155 { "ocirc", 244 }, /* small o, circumflex accent */
156 { "ograve", 242 }, /* small o, grave accent */
157 { "ordf", 170 }, /* ordinal indicator, feminine */
158 { "ordm", 186 }, /* ordinal indicator, masculine */
159 { "oslash", 248 }, /* small o, slash */
160 { "otilde", 245 }, /* small o, tilde */
161 { "ouml", 246 }, /* small o, dieresis or umlaut mark */
162 { "para", 182 }, /* pilcrow (paragraph sign) */
163 { "plusmn", 177 }, /* plus-or-minus sign */
164 { "pound", 163 }, /* pound sterling sign */
165 { "quot", 34 }, /* double quote */
166 { "raquo", 187 }, /* angle quotation mark, right */
167 { "reg", 174 }, /* registered sign */
168 { "sect", 167 }, /* section sign */
169 { "shy", 173 }, /* soft hyphen */
170 { "sup1", 185 }, /* superscript one */
171 { "sup2", 178 }, /* superscript two */
172 { "sup3", 179 }, /* superscript three */
173 { "szlig", 223 }, /* small sharp s, German (sz ligature) */
174 { "thorn", 254 }, /* small thorn, Icelandic */
175 { "times", 215 }, /* multiply sign */
176 { "uacute", 250 }, /* small u, acute accent */
177 { "ucirc", 251 }, /* small u, circumflex accent */
178 { "ugrave", 249 }, /* small u, grave accent */
179 { "uml", 168 }, /* umlaut (dieresis) */
180 { "uuml", 252 }, /* small u, dieresis or umlaut mark */
181 { "yacute", 253 }, /* small y, acute accent */
182 { "yen", 165 }, /* yen sign */
183 { "yuml", 255 }, /* small y, dieresis or umlaut mark */
187 * unvis - decode characters previously encoded by vis
190 unvis(char *cp, int c, int *astate, int flag)
192 unsigned char uc = (unsigned char)c;
193 unsigned char st, ia, is, lc;
196 * Bottom 8 bits of astate hold the state machine state.
197 * Top 8 bits hold the current character in the http 1866 nv string decoding
199 #define GS(a) ((a) & 0xff)
200 #define SS(a, b) (((uint32_t)(a) << 24) | (b))
201 #define GI(a) ((uint32_t)(a) >> 24)
203 _DIAGASSERT(cp != NULL);
204 _DIAGASSERT(astate != NULL);
205 st = GS(*astate);
207 if (flag & UNVIS_END) {
208 switch (st) {
209 case S_OCTAL2:
210 case S_OCTAL3:
211 case S_HEX2:
212 *astate = SS(0, S_GROUND);
213 return UNVIS_VALID;
214 case S_GROUND:
215 return UNVIS_NOCHAR;
216 default:
217 return UNVIS_SYNBAD;
221 switch (st) {
223 case S_GROUND:
224 *cp = 0;
225 if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
226 *astate = SS(0, S_START);
227 return UNVIS_NOCHAR;
229 if ((flag & VIS_HTTP1808) && c == '%') {
230 *astate = SS(0, S_HEX1);
231 return UNVIS_NOCHAR;
233 if ((flag & VIS_HTTP1866) && c == '&') {
234 *astate = SS(0, S_AMP);
235 return UNVIS_NOCHAR;
237 if ((flag & VIS_MIMESTYLE) && c == '=') {
238 *astate = SS(0, S_MIME1);
239 return UNVIS_NOCHAR;
241 *cp = c;
242 return UNVIS_VALID;
244 case S_START:
245 switch(c) {
246 case '\\':
247 *cp = c;
248 *astate = SS(0, S_GROUND);
249 return UNVIS_VALID;
250 case '0': case '1': case '2': case '3':
251 case '4': case '5': case '6': case '7':
252 *cp = (c - '0');
253 *astate = SS(0, S_OCTAL2);
254 return UNVIS_NOCHAR;
255 case 'M':
256 *cp = (char)0200;
257 *astate = SS(0, S_META);
258 return UNVIS_NOCHAR;
259 case '^':
260 *astate = SS(0, S_CTRL);
261 return UNVIS_NOCHAR;
262 case 'n':
263 *cp = '\n';
264 *astate = SS(0, S_GROUND);
265 return UNVIS_VALID;
266 case 'r':
267 *cp = '\r';
268 *astate = SS(0, S_GROUND);
269 return UNVIS_VALID;
270 case 'b':
271 *cp = '\b';
272 *astate = SS(0, S_GROUND);
273 return UNVIS_VALID;
274 case 'a':
275 *cp = '\007';
276 *astate = SS(0, S_GROUND);
277 return UNVIS_VALID;
278 case 'v':
279 *cp = '\v';
280 *astate = SS(0, S_GROUND);
281 return UNVIS_VALID;
282 case 't':
283 *cp = '\t';
284 *astate = SS(0, S_GROUND);
285 return UNVIS_VALID;
286 case 'f':
287 *cp = '\f';
288 *astate = SS(0, S_GROUND);
289 return UNVIS_VALID;
290 case 's':
291 *cp = ' ';
292 *astate = SS(0, S_GROUND);
293 return UNVIS_VALID;
294 case 'E':
295 *cp = '\033';
296 *astate = SS(0, S_GROUND);
297 return UNVIS_VALID;
298 case '\n':
300 * hidden newline
302 *astate = SS(0, S_GROUND);
303 return UNVIS_NOCHAR;
304 case '$':
306 * hidden marker
308 *astate = SS(0, S_GROUND);
309 return UNVIS_NOCHAR;
311 goto bad;
313 case S_META:
314 if (c == '-')
315 *astate = SS(0, S_META1);
316 else if (c == '^')
317 *astate = SS(0, S_CTRL);
318 else
319 goto bad;
320 return UNVIS_NOCHAR;
322 case S_META1:
323 *astate = SS(0, S_GROUND);
324 *cp |= c;
325 return UNVIS_VALID;
327 case S_CTRL:
328 if (c == '?')
329 *cp |= 0177;
330 else
331 *cp |= c & 037;
332 *astate = SS(0, S_GROUND);
333 return UNVIS_VALID;
335 case S_OCTAL2: /* second possible octal digit */
336 if (isoctal(uc)) {
338 * yes - and maybe a third
340 *cp = (*cp << 3) + (c - '0');
341 *astate = SS(0, S_OCTAL3);
342 return UNVIS_NOCHAR;
345 * no - done with current sequence, push back passed char
347 *astate = SS(0, S_GROUND);
348 return UNVIS_VALIDPUSH;
350 case S_OCTAL3: /* third possible octal digit */
351 *astate = SS(0, S_GROUND);
352 if (isoctal(uc)) {
353 *cp = (*cp << 3) + (c - '0');
354 return UNVIS_VALID;
357 * we were done, push back passed char
359 return UNVIS_VALIDPUSH;
361 case S_HEX1:
362 if (isxdigit(uc)) {
363 *cp = xtod(uc);
364 *astate = SS(0, S_HEX2);
365 return UNVIS_NOCHAR;
368 * no - done with current sequence, push back passed char
370 *astate = SS(0, S_GROUND);
371 return UNVIS_VALIDPUSH;
373 case S_HEX2:
374 *astate = S_GROUND;
375 if (isxdigit(uc)) {
376 *cp = xtod(uc) | (*cp << 4);
377 return UNVIS_VALID;
379 return UNVIS_VALIDPUSH;
381 case S_MIME1:
382 if (uc == '\n' || uc == '\r') {
383 *astate = SS(0, S_EATCRNL);
384 return UNVIS_NOCHAR;
386 if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
387 *cp = XTOD(uc);
388 *astate = SS(0, S_MIME2);
389 return UNVIS_NOCHAR;
391 goto bad;
393 case S_MIME2:
394 if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
395 *astate = SS(0, S_GROUND);
396 *cp = XTOD(uc) | (*cp << 4);
397 return UNVIS_VALID;
399 goto bad;
401 case S_EATCRNL:
402 switch (uc) {
403 case '\r':
404 case '\n':
405 return UNVIS_NOCHAR;
406 case '=':
407 *astate = SS(0, S_MIME1);
408 return UNVIS_NOCHAR;
409 default:
410 *cp = uc;
411 *astate = SS(0, S_GROUND);
412 return UNVIS_VALID;
415 case S_AMP:
416 *cp = 0;
417 if (uc == '#') {
418 *astate = SS(0, S_NUMBER);
419 return UNVIS_NOCHAR;
421 *astate = SS(0, S_STRING);
422 /*FALLTHROUGH*/
424 case S_STRING:
425 ia = *cp; /* index in the array */
426 is = GI(*astate); /* index in the string */
427 lc = is == 0 ? 0 : nv[ia].name[is - 1]; /* last character */
429 if (uc == ';')
430 uc = '\0';
432 for (; ia < __arraycount(nv); ia++) {
433 if (is != 0 && nv[ia].name[is - 1] != lc)
434 goto bad;
435 if (nv[ia].name[is] == uc)
436 break;
439 if (*cp == __arraycount(nv))
440 goto bad;
442 if (uc != 0) {
443 *cp = ia;
444 *astate = SS(is + 1, S_STRING);
445 return UNVIS_NOCHAR;
448 *cp = nv[ia].value;
449 *astate = SS(0, S_GROUND);
450 return UNVIS_VALID;
452 case S_NUMBER:
453 if (uc == ';')
454 return UNVIS_VALID;
455 if (!isdigit(uc))
456 goto bad;
457 *cp += (*cp * 10) + uc - '0';
458 return UNVIS_NOCHAR;
460 default:
461 bad:
463 * decoder in unknown state - (probably uninitialized)
465 *astate = SS(0, S_GROUND);
466 return UNVIS_SYNBAD;
471 * strunvis - decode src into dst
473 * Number of chars decoded into dst is returned, -1 on error.
474 * Dst is null terminated.
478 strunvisx(char *dst, const char *src, int flag)
480 char c;
481 char *start = dst;
482 int state = 0;
484 _DIAGASSERT(src != NULL);
485 _DIAGASSERT(dst != NULL);
487 while ((c = *src++) != '\0') {
488 again:
489 switch (unvis(dst, c, &state, flag)) {
490 case UNVIS_VALID:
491 dst++;
492 break;
493 case UNVIS_VALIDPUSH:
494 dst++;
495 goto again;
496 case 0:
497 case UNVIS_NOCHAR:
498 break;
499 default:
500 return (-1);
503 if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
504 dst++;
505 *dst = '\0';
506 return (int)(dst - start);
510 strunvis(char *dst, const char *src)
512 return strunvisx(dst, src, 0);
514 #endif