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