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