xref: /dragonfly/lib/libc/gen/unvis.c (revision 593159d8)
1 /*	$NetBSD: unvis.c,v 1.44 2014/09/26 15:43:36 roy Exp $	*/
2 
3 /*-
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
18  *
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.
30  */
31 
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.44 2014/09/26 15:43:36 roy Exp $");
38 #endif
39 #endif /* LIBC_SCCS and not lint */
40 
41 #include "namespace.h"
42 #include <sys/types.h>
43 
44 #include <assert.h>
45 #include <ctype.h>
46 #include <stdint.h>
47 #include <stdio.h>
48 #include <errno.h>
49 #include <vis.h>
50 
51 /*
52  * Return the number of elements in a statically-allocated array,
53  * __x.
54  */
55 #define	__arraycount(__x)	(sizeof(__x) / sizeof(__x[0]))
56 
57 #ifdef __weak_alias
58 __weak_alias(strnunvisx,_strnunvisx)
59 #endif
60 
61 #if !HAVE_VIS
62 /*
63  * decode driven by state machine
64  */
65 #define	S_GROUND	0	/* haven't seen escape char */
66 #define	S_START		1	/* start decoding special sequence */
67 #define	S_META		2	/* metachar started (M) */
68 #define	S_META1		3	/* metachar more, regular char (-) */
69 #define	S_CTRL		4	/* control char started (^) */
70 #define	S_OCTAL2	5	/* octal digit 2 */
71 #define	S_OCTAL3	6	/* octal digit 3 */
72 #define	S_HEX		7	/* mandatory hex digit */
73 #define	S_HEX1		8	/* http hex digit */
74 #define	S_HEX2		9	/* http hex digit 2 */
75 #define	S_MIME1		10	/* mime hex digit 1 */
76 #define	S_MIME2		11	/* mime hex digit 2 */
77 #define	S_EATCRNL	12	/* mime eating CRNL */
78 #define	S_AMP		13	/* seen & */
79 #define	S_NUMBER	14	/* collecting number */
80 #define	S_STRING	15	/* collecting string */
81 
82 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
83 #define	xtod(c)		(isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
84 #define	XTOD(c)		(isdigit(c) ? (c - '0') : ((c - 'A') + 10))
85 
86 /*
87  * RFC 1866
88  */
89 static const struct nv {
90 	char name[7];
91 	uint8_t value;
92 } nv[] = {
93 	{ "AElig",	198 }, /* capital AE diphthong (ligature)  */
94 	{ "Aacute",	193 }, /* capital A, acute accent  */
95 	{ "Acirc",	194 }, /* capital A, circumflex accent  */
96 	{ "Agrave",	192 }, /* capital A, grave accent  */
97 	{ "Aring",	197 }, /* capital A, ring  */
98 	{ "Atilde",	195 }, /* capital A, tilde  */
99 	{ "Auml",	196 }, /* capital A, dieresis or umlaut mark  */
100 	{ "Ccedil",	199 }, /* capital C, cedilla  */
101 	{ "ETH",	208 }, /* capital Eth, Icelandic  */
102 	{ "Eacute",	201 }, /* capital E, acute accent  */
103 	{ "Ecirc",	202 }, /* capital E, circumflex accent  */
104 	{ "Egrave",	200 }, /* capital E, grave accent  */
105 	{ "Euml",	203 }, /* capital E, dieresis or umlaut mark  */
106 	{ "Iacute",	205 }, /* capital I, acute accent  */
107 	{ "Icirc",	206 }, /* capital I, circumflex accent  */
108 	{ "Igrave",	204 }, /* capital I, grave accent  */
109 	{ "Iuml",	207 }, /* capital I, dieresis or umlaut mark  */
110 	{ "Ntilde",	209 }, /* capital N, tilde  */
111 	{ "Oacute",	211 }, /* capital O, acute accent  */
112 	{ "Ocirc",	212 }, /* capital O, circumflex accent  */
113 	{ "Ograve",	210 }, /* capital O, grave accent  */
114 	{ "Oslash",	216 }, /* capital O, slash  */
115 	{ "Otilde",	213 }, /* capital O, tilde  */
116 	{ "Ouml",	214 }, /* capital O, dieresis or umlaut mark  */
117 	{ "THORN",	222 }, /* capital THORN, Icelandic  */
118 	{ "Uacute",	218 }, /* capital U, acute accent  */
119 	{ "Ucirc",	219 }, /* capital U, circumflex accent  */
120 	{ "Ugrave",	217 }, /* capital U, grave accent  */
121 	{ "Uuml",	220 }, /* capital U, dieresis or umlaut mark  */
122 	{ "Yacute",	221 }, /* capital Y, acute accent  */
123 	{ "aacute",	225 }, /* small a, acute accent  */
124 	{ "acirc",	226 }, /* small a, circumflex accent  */
125 	{ "acute",	180 }, /* acute accent  */
126 	{ "aelig",	230 }, /* small ae diphthong (ligature)  */
127 	{ "agrave",	224 }, /* small a, grave accent  */
128 	{ "amp",	 38 }, /* ampersand  */
129 	{ "aring",	229 }, /* small a, ring  */
130 	{ "atilde",	227 }, /* small a, tilde  */
131 	{ "auml",	228 }, /* small a, dieresis or umlaut mark  */
132 	{ "brvbar",	166 }, /* broken (vertical) bar  */
133 	{ "ccedil",	231 }, /* small c, cedilla  */
134 	{ "cedil",	184 }, /* cedilla  */
135 	{ "cent",	162 }, /* cent sign  */
136 	{ "copy",	169 }, /* copyright sign  */
137 	{ "curren",	164 }, /* general currency sign  */
138 	{ "deg",	176 }, /* degree sign  */
139 	{ "divide",	247 }, /* divide sign  */
140 	{ "eacute",	233 }, /* small e, acute accent  */
141 	{ "ecirc",	234 }, /* small e, circumflex accent  */
142 	{ "egrave",	232 }, /* small e, grave accent  */
143 	{ "eth",	240 }, /* small eth, Icelandic  */
144 	{ "euml",	235 }, /* small e, dieresis or umlaut mark  */
145 	{ "frac12",	189 }, /* fraction one-half  */
146 	{ "frac14",	188 }, /* fraction one-quarter  */
147 	{ "frac34",	190 }, /* fraction three-quarters  */
148 	{ "gt",		 62 }, /* greater than  */
149 	{ "iacute",	237 }, /* small i, acute accent  */
150 	{ "icirc",	238 }, /* small i, circumflex accent  */
151 	{ "iexcl",	161 }, /* inverted exclamation mark  */
152 	{ "igrave",	236 }, /* small i, grave accent  */
153 	{ "iquest",	191 }, /* inverted question mark  */
154 	{ "iuml",	239 }, /* small i, dieresis or umlaut mark  */
155 	{ "laquo",	171 }, /* angle quotation mark, left  */
156 	{ "lt",		 60 }, /* less than  */
157 	{ "macr",	175 }, /* macron  */
158 	{ "micro",	181 }, /* micro sign  */
159 	{ "middot",	183 }, /* middle dot  */
160 	{ "nbsp",	160 }, /* no-break space  */
161 	{ "not",	172 }, /* not sign  */
162 	{ "ntilde",	241 }, /* small n, tilde  */
163 	{ "oacute",	243 }, /* small o, acute accent  */
164 	{ "ocirc",	244 }, /* small o, circumflex accent  */
165 	{ "ograve",	242 }, /* small o, grave accent  */
166 	{ "ordf",	170 }, /* ordinal indicator, feminine  */
167 	{ "ordm",	186 }, /* ordinal indicator, masculine  */
168 	{ "oslash",	248 }, /* small o, slash  */
169 	{ "otilde",	245 }, /* small o, tilde  */
170 	{ "ouml",	246 }, /* small o, dieresis or umlaut mark  */
171 	{ "para",	182 }, /* pilcrow (paragraph sign)  */
172 	{ "plusmn",	177 }, /* plus-or-minus sign  */
173 	{ "pound",	163 }, /* pound sterling sign  */
174 	{ "quot",	 34 }, /* double quote  */
175 	{ "raquo",	187 }, /* angle quotation mark, right  */
176 	{ "reg",	174 }, /* registered sign  */
177 	{ "sect",	167 }, /* section sign  */
178 	{ "shy",	173 }, /* soft hyphen  */
179 	{ "sup1",	185 }, /* superscript one  */
180 	{ "sup2",	178 }, /* superscript two  */
181 	{ "sup3",	179 }, /* superscript three  */
182 	{ "szlig",	223 }, /* small sharp s, German (sz ligature)  */
183 	{ "thorn",	254 }, /* small thorn, Icelandic  */
184 	{ "times",	215 }, /* multiply sign  */
185 	{ "uacute",	250 }, /* small u, acute accent  */
186 	{ "ucirc",	251 }, /* small u, circumflex accent  */
187 	{ "ugrave",	249 }, /* small u, grave accent  */
188 	{ "uml",	168 }, /* umlaut (dieresis)  */
189 	{ "uuml",	252 }, /* small u, dieresis or umlaut mark  */
190 	{ "yacute",	253 }, /* small y, acute accent  */
191 	{ "yen",	165 }, /* yen sign  */
192 	{ "yuml",	255 }, /* small y, dieresis or umlaut mark  */
193 };
194 
195 /*
196  * unvis - decode characters previously encoded by vis
197  */
198 int
199 unvis(char *cp, int c, int *astate, int flag)
200 {
201 	unsigned char uc = (unsigned char)c;
202 	unsigned char st, ia, is, lc;
203 
204 /*
205  * Bottom 8 bits of astate hold the state machine state.
206  * Top 8 bits hold the current character in the http 1866 nv string decoding
207  */
208 #define GS(a)		((a) & 0xff)
209 #define SS(a, b)	(((uint32_t)(a) << 24) | (b))
210 #define GI(a)		((uint32_t)(a) >> 24)
211 
212 	_DIAGASSERT(cp != NULL);
213 	_DIAGASSERT(astate != NULL);
214 	st = GS(*astate);
215 
216 	if (flag & UNVIS_END) {
217 		switch (st) {
218 		case S_OCTAL2:
219 		case S_OCTAL3:
220 		case S_HEX2:
221 			*astate = SS(0, S_GROUND);
222 			return UNVIS_VALID;
223 		case S_GROUND:
224 			return UNVIS_NOCHAR;
225 		default:
226 			return UNVIS_SYNBAD;
227 		}
228 	}
229 
230 	switch (st) {
231 
232 	case S_GROUND:
233 		*cp = 0;
234 		if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
235 			*astate = SS(0, S_START);
236 			return UNVIS_NOCHAR;
237 		}
238 		if ((flag & VIS_HTTP1808) && c == '%') {
239 			*astate = SS(0, S_HEX1);
240 			return UNVIS_NOCHAR;
241 		}
242 		if ((flag & VIS_HTTP1866) && c == '&') {
243 			*astate = SS(0, S_AMP);
244 			return UNVIS_NOCHAR;
245 		}
246 		if ((flag & VIS_MIMESTYLE) && c == '=') {
247 			*astate = SS(0, S_MIME1);
248 			return UNVIS_NOCHAR;
249 		}
250 		*cp = c;
251 		return UNVIS_VALID;
252 
253 	case S_START:
254 		switch(c) {
255 		case '\\':
256 			*cp = c;
257 			*astate = SS(0, S_GROUND);
258 			return UNVIS_VALID;
259 		case '0': case '1': case '2': case '3':
260 		case '4': case '5': case '6': case '7':
261 			*cp = (c - '0');
262 			*astate = SS(0, S_OCTAL2);
263 			return UNVIS_NOCHAR;
264 		case 'M':
265 			*cp = (char)0200;
266 			*astate = SS(0, S_META);
267 			return UNVIS_NOCHAR;
268 		case '^':
269 			*astate = SS(0, S_CTRL);
270 			return UNVIS_NOCHAR;
271 		case 'n':
272 			*cp = '\n';
273 			*astate = SS(0, S_GROUND);
274 			return UNVIS_VALID;
275 		case 'r':
276 			*cp = '\r';
277 			*astate = SS(0, S_GROUND);
278 			return UNVIS_VALID;
279 		case 'b':
280 			*cp = '\b';
281 			*astate = SS(0, S_GROUND);
282 			return UNVIS_VALID;
283 		case 'a':
284 			*cp = '\007';
285 			*astate = SS(0, S_GROUND);
286 			return UNVIS_VALID;
287 		case 'v':
288 			*cp = '\v';
289 			*astate = SS(0, S_GROUND);
290 			return UNVIS_VALID;
291 		case 't':
292 			*cp = '\t';
293 			*astate = SS(0, S_GROUND);
294 			return UNVIS_VALID;
295 		case 'f':
296 			*cp = '\f';
297 			*astate = SS(0, S_GROUND);
298 			return UNVIS_VALID;
299 		case 's':
300 			*cp = ' ';
301 			*astate = SS(0, S_GROUND);
302 			return UNVIS_VALID;
303 		case 'E':
304 			*cp = '\033';
305 			*astate = SS(0, S_GROUND);
306 			return UNVIS_VALID;
307 		case 'x':
308 			*astate = SS(0, S_HEX);
309 			return UNVIS_NOCHAR;
310 		case '\n':
311 			/*
312 			 * hidden newline
313 			 */
314 			*astate = SS(0, S_GROUND);
315 			return UNVIS_NOCHAR;
316 		case '$':
317 			/*
318 			 * hidden marker
319 			 */
320 			*astate = SS(0, S_GROUND);
321 			return UNVIS_NOCHAR;
322 		default:
323 			if (isgraph(c)) {
324 				*cp = c;
325 				*astate = SS(0, S_GROUND);
326 				return UNVIS_VALID;
327 			}
328 		}
329 		goto bad;
330 
331 	case S_META:
332 		if (c == '-')
333 			*astate = SS(0, S_META1);
334 		else if (c == '^')
335 			*astate = SS(0, S_CTRL);
336 		else
337 			goto bad;
338 		return UNVIS_NOCHAR;
339 
340 	case S_META1:
341 		*astate = SS(0, S_GROUND);
342 		*cp |= c;
343 		return UNVIS_VALID;
344 
345 	case S_CTRL:
346 		if (c == '?')
347 			*cp |= 0177;
348 		else
349 			*cp |= c & 037;
350 		*astate = SS(0, S_GROUND);
351 		return UNVIS_VALID;
352 
353 	case S_OCTAL2:	/* second possible octal digit */
354 		if (isoctal(uc)) {
355 			/*
356 			 * yes - and maybe a third
357 			 */
358 			*cp = (*cp << 3) + (c - '0');
359 			*astate = SS(0, S_OCTAL3);
360 			return UNVIS_NOCHAR;
361 		}
362 		/*
363 		 * no - done with current sequence, push back passed char
364 		 */
365 		*astate = SS(0, S_GROUND);
366 		return UNVIS_VALIDPUSH;
367 
368 	case S_OCTAL3:	/* third possible octal digit */
369 		*astate = SS(0, S_GROUND);
370 		if (isoctal(uc)) {
371 			*cp = (*cp << 3) + (c - '0');
372 			return UNVIS_VALID;
373 		}
374 		/*
375 		 * we were done, push back passed char
376 		 */
377 		return UNVIS_VALIDPUSH;
378 
379 	case S_HEX:
380 		if (!isxdigit(uc))
381 			goto bad;
382 		/*FALLTHROUGH*/
383 	case S_HEX1:
384 		if (isxdigit(uc)) {
385 			*cp = xtod(uc);
386 			*astate = SS(0, S_HEX2);
387 			return UNVIS_NOCHAR;
388 		}
389 		/*
390 		 * no - done with current sequence, push back passed char
391 		 */
392 		*astate = SS(0, S_GROUND);
393 		return UNVIS_VALIDPUSH;
394 
395 	case S_HEX2:
396 		*astate = S_GROUND;
397 		if (isxdigit(uc)) {
398 			*cp = xtod(uc) | (*cp << 4);
399 			return UNVIS_VALID;
400 		}
401 		return UNVIS_VALIDPUSH;
402 
403 	case S_MIME1:
404 		if (uc == '\n' || uc == '\r') {
405 			*astate = SS(0, S_EATCRNL);
406 			return UNVIS_NOCHAR;
407 		}
408 		if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
409 			*cp = XTOD(uc);
410 			*astate = SS(0, S_MIME2);
411 			return UNVIS_NOCHAR;
412 		}
413 		goto bad;
414 
415 	case S_MIME2:
416 		if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
417 			*astate = SS(0, S_GROUND);
418 			*cp = XTOD(uc) | (*cp << 4);
419 			return UNVIS_VALID;
420 		}
421 		goto bad;
422 
423 	case S_EATCRNL:
424 		switch (uc) {
425 		case '\r':
426 		case '\n':
427 			return UNVIS_NOCHAR;
428 		case '=':
429 			*astate = SS(0, S_MIME1);
430 			return UNVIS_NOCHAR;
431 		default:
432 			*cp = uc;
433 			*astate = SS(0, S_GROUND);
434 			return UNVIS_VALID;
435 		}
436 
437 	case S_AMP:
438 		*cp = 0;
439 		if (uc == '#') {
440 			*astate = SS(0, S_NUMBER);
441 			return UNVIS_NOCHAR;
442 		}
443 		*astate = SS(0, S_STRING);
444 		/*FALLTHROUGH*/
445 
446 	case S_STRING:
447 		ia = *cp;		/* index in the array */
448 		is = GI(*astate);	/* index in the string */
449 		lc = is == 0 ? 0 : nv[ia].name[is - 1];	/* last character */
450 
451 		if (uc == ';')
452 			uc = '\0';
453 
454 		for (; ia < __arraycount(nv); ia++) {
455 			if (is != 0 && nv[ia].name[is - 1] != lc)
456 				goto bad;
457 			if (nv[ia].name[is] == uc)
458 				break;
459 		}
460 
461 		if (ia == __arraycount(nv))
462 			goto bad;
463 
464 		if (uc != 0) {
465 			*cp = ia;
466 			*astate = SS(is + 1, S_STRING);
467 			return UNVIS_NOCHAR;
468 		}
469 
470 		*cp = nv[ia].value;
471 		*astate = SS(0, S_GROUND);
472 		return UNVIS_VALID;
473 
474 	case S_NUMBER:
475 		if (uc == ';')
476 			return UNVIS_VALID;
477 		if (!isdigit(uc))
478 			goto bad;
479 		*cp += (*cp * 10) + uc - '0';
480 		return UNVIS_NOCHAR;
481 
482 	default:
483 	bad:
484 		/*
485 		 * decoder in unknown state - (probably uninitialized)
486 		 */
487 		*astate = SS(0, S_GROUND);
488 		return UNVIS_SYNBAD;
489 	}
490 }
491 
492 /*
493  * strnunvisx - decode src into dst
494  *
495  *	Number of chars decoded into dst is returned, -1 on error.
496  *	Dst is null terminated.
497  */
498 
499 int
500 strnunvisx(char *dst, size_t dlen, const char *src, int flag)
501 {
502 	char c;
503 	char t = '\0', *start = dst;
504 	int state = 0;
505 
506 	_DIAGASSERT(src != NULL);
507 	_DIAGASSERT(dst != NULL);
508 #define CHECKSPACE() \
509 	do { \
510 		if (dlen-- == 0) { \
511 			errno = ENOSPC; \
512 			return -1; \
513 		} \
514 	} while (/*CONSTCOND*/0)
515 
516 	while ((c = *src++) != '\0') {
517  again:
518 		switch (unvis(&t, c, &state, flag)) {
519 		case UNVIS_VALID:
520 			CHECKSPACE();
521 			*dst++ = t;
522 			break;
523 		case UNVIS_VALIDPUSH:
524 			CHECKSPACE();
525 			*dst++ = t;
526 			goto again;
527 		case 0:
528 		case UNVIS_NOCHAR:
529 			break;
530 		case UNVIS_SYNBAD:
531 			errno = EINVAL;
532 			return -1;
533 		default:
534 			_DIAGASSERT(/*CONSTCOND*/0);
535 			errno = EINVAL;
536 			return -1;
537 		}
538 	}
539 	if (unvis(&t, c, &state, UNVIS_END) == UNVIS_VALID) {
540 		CHECKSPACE();
541 		*dst++ = t;
542 	}
543 	CHECKSPACE();
544 	*dst = '\0';
545 	return (int)(dst - start);
546 }
547 
548 int
549 strunvisx(char *dst, const char *src, int flag)
550 {
551 	return strnunvisx(dst, (size_t)~0, src, flag);
552 }
553 
554 int
555 strunvis(char *dst, const char *src)
556 {
557 	return strnunvisx(dst, (size_t)~0, src, 0);
558 }
559 
560 int
561 strnunvis(char *dst, size_t dlen, const char *src)
562 {
563 	return strnunvisx(dst, dlen, src, 0);
564 }
565 #endif
566