Module Name:    src
Committed By:   christos
Date:           Sat Nov 27 19:44:21 UTC 2010

Modified Files:
        src/lib/libc/gen: unvis.3 unvis.c

Log Message:
Implement VIS_NOESCAPE and VIS_HTTP1866.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/lib/libc/gen/unvis.3
cvs rdiff -u -r1.30 -r1.31 src/lib/libc/gen/unvis.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/gen/unvis.3
diff -u src/lib/libc/gen/unvis.3:1.18 src/lib/libc/gen/unvis.3:1.19
--- src/lib/libc/gen/unvis.3:1.18	Tue Feb 10 18:06:31 2009
+++ src/lib/libc/gen/unvis.3	Sat Nov 27 14:44:21 2010
@@ -1,4 +1,4 @@
-.\"	$NetBSD: unvis.3,v 1.18 2009/02/10 23:06:31 christos Exp $
+.\"	$NetBSD: unvis.3,v 1.19 2010/11/27 19:44:21 christos Exp $
 .\"
 .\" Copyright (c) 1989, 1991, 1993
 .\"	The Regents of the University of California.  All rights reserved.
@@ -29,7 +29,7 @@
 .\"
 .\"     @(#)unvis.3	8.2 (Berkeley) 12/11/93
 .\"
-.Dd February 10, 2009
+.Dd November 27, 2010
 .Dt UNVIS 3
 .Os
 .Sh NAME
@@ -149,12 +149,22 @@
 argument is also used to specify the encoding style of the source.
 If set to
 .Dv VIS_HTTPSTYLE ,
+or
+.Dv VIS_HTTP1808 ,
 .Fn unvis
 will decode URI strings as specified in RFC 1808.
 If set to
+.Dv VIS_HTTP1866 ,
+.Fn unvis
+will decode URI strings as specified in RFC 1866.
+If set to
 .Dv VIS_MIMESTYLE ,
 .Fn unvis
 will decode MIME Quoted-Printable strings as specified in RFC 2045.
+If set to
+.Dv VIS_NOESCAPE ,
+.Fn unvis
+will not decode \e quoted characters.
 .Pp
 The following code fragment illustrates a proper use of
 .Fn unvis .

Index: src/lib/libc/gen/unvis.c
diff -u src/lib/libc/gen/unvis.c:1.30 src/lib/libc/gen/unvis.c:1.31
--- src/lib/libc/gen/unvis.c:1.30	Wed Feb 11 08:51:59 2009
+++ src/lib/libc/gen/unvis.c	Sat Nov 27 14:44:21 2010
@@ -1,4 +1,4 @@
-/*	$NetBSD: unvis.c,v 1.30 2009/02/11 13:51:59 christos Exp $	*/
+/*	$NetBSD: unvis.c,v 1.31 2010/11/27 19:44:21 christos Exp $	*/
 
 /*-
  * Copyright (c) 1989, 1993
@@ -34,7 +34,7 @@
 #if 0
 static char sccsid[] = "@(#)unvis.c	8.1 (Berkeley) 6/4/93";
 #else
-__RCSID("$NetBSD: unvis.c,v 1.30 2009/02/11 13:51:59 christos Exp $");
+__RCSID("$NetBSD: unvis.c,v 1.31 2010/11/27 19:44:21 christos Exp $");
 #endif
 #endif /* LIBC_SCCS and not lint */
 
@@ -66,45 +66,176 @@
 #define S_MIME1		9	/* mime hex digit 1 */
 #define S_MIME2		10	/* mime hex digit 2 */
 #define S_EATCRNL	11	/* mime eating CRNL */
+#define S_AMP		12	/* seen & */
+#define S_NUMBER	13	/* collecting number */
+#define S_STRING	14	/* collecting string */
 
 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
 #define xtod(c)		(isdigit(c) ? (c - '0') : ((tolower(c) - 'a') + 10))
 #define XTOD(c)		(isdigit(c) ? (c - '0') : ((c - 'A') + 10))
 
 /*
+ * RFC 1866
+ */
+static const struct nv {
+	const char *name;
+	uint8_t value;
+} nv[] = {
+	{ "AElig",	198 }, /* capital AE diphthong (ligature)  */
+	{ "Aacute",	193 }, /* capital A, acute accent  */
+	{ "Acirc",	194 }, /* capital A, circumflex accent  */
+	{ "Agrave",	192 }, /* capital A, grave accent  */
+	{ "Aring",	197 }, /* capital A, ring  */
+	{ "Atilde",	195 }, /* capital A, tilde  */
+	{ "Auml",	196 }, /* capital A, dieresis or umlaut mark  */
+	{ "Ccedil",	199 }, /* capital C, cedilla  */
+	{ "ETH",	208 }, /* capital Eth, Icelandic  */
+	{ "Eacute",	201 }, /* capital E, acute accent  */
+	{ "Ecirc",	202 }, /* capital E, circumflex accent  */
+	{ "Egrave",	200 }, /* capital E, grave accent  */
+	{ "Euml",	203 }, /* capital E, dieresis or umlaut mark  */
+	{ "Iacute",	205 }, /* capital I, acute accent  */
+	{ "Icirc",	206 }, /* capital I, circumflex accent  */
+	{ "Igrave",	204 }, /* capital I, grave accent  */
+	{ "Iuml",	207 }, /* capital I, dieresis or umlaut mark  */
+	{ "Ntilde",	209 }, /* capital N, tilde  */
+	{ "Oacute",	211 }, /* capital O, acute accent  */
+	{ "Ocirc",	212 }, /* capital O, circumflex accent  */
+	{ "Ograve",	210 }, /* capital O, grave accent  */
+	{ "Oslash",	216 }, /* capital O, slash  */
+	{ "Otilde",	213 }, /* capital O, tilde  */
+	{ "Ouml",	214 }, /* capital O, dieresis or umlaut mark  */
+	{ "THORN",	222 }, /* capital THORN, Icelandic  */
+	{ "Uacute",	218 }, /* capital U, acute accent  */
+	{ "Ucirc",	219 }, /* capital U, circumflex accent  */
+	{ "Ugrave",	217 }, /* capital U, grave accent  */
+	{ "Uuml",	220 }, /* capital U, dieresis or umlaut mark  */
+	{ "Yacute",	221 }, /* capital Y, acute accent  */
+	{ "aacute",	225 }, /* small a, acute accent  */
+	{ "acirc",	226 }, /* small a, circumflex accent  */
+	{ "acute",	180 }, /* acute accent  */
+	{ "aelig",	230 }, /* small ae diphthong (ligature)  */
+	{ "agrave",	224 }, /* small a, grave accent  */
+	{ "amp",	 38 }, /* ampersand  */
+	{ "aring",	229 }, /* small a, ring  */
+	{ "atilde",	227 }, /* small a, tilde  */
+	{ "auml",	228 }, /* small a, dieresis or umlaut mark  */
+	{ "brvbar",	166 }, /* broken (vertical) bar  */
+	{ "ccedil",	231 }, /* small c, cedilla  */
+	{ "cedil",	184 }, /* cedilla  */
+	{ "cent",	162 }, /* cent sign  */
+	{ "copy",	169 }, /* copyright sign  */
+	{ "curren",	164 }, /* general currency sign  */
+	{ "deg",	176 }, /* degree sign  */
+	{ "divide",	247 }, /* divide sign  */
+	{ "eacute",	233 }, /* small e, acute accent  */
+	{ "ecirc",	234 }, /* small e, circumflex accent  */
+	{ "egrave",	232 }, /* small e, grave accent  */
+	{ "eth",	240 }, /* small eth, Icelandic  */
+	{ "euml",	235 }, /* small e, dieresis or umlaut mark  */
+	{ "frac12",	189 }, /* fraction one-half  */
+	{ "frac14",	188 }, /* fraction one-quarter  */
+	{ "frac34",	190 }, /* fraction three-quarters  */
+	{ "gt",		 62 }, /* greater than  */
+	{ "iacute",	237 }, /* small i, acute accent  */
+	{ "icirc",	238 }, /* small i, circumflex accent  */
+	{ "iexcl",	161 }, /* inverted exclamation mark  */
+	{ "igrave",	236 }, /* small i, grave accent  */
+	{ "iquest",	191 }, /* inverted question mark  */
+	{ "iuml",	239 }, /* small i, dieresis or umlaut mark  */
+	{ "laquo",	171 }, /* angle quotation mark, left  */
+	{ "lt",		 60 }, /* less than  */
+	{ "macr",	175 }, /* macron  */
+	{ "micro",	181 }, /* micro sign  */
+	{ "middot",	183 }, /* middle dot  */
+	{ "nbsp",	160 }, /* no-break space  */
+	{ "not",	172 }, /* not sign  */
+	{ "ntilde",	241 }, /* small n, tilde  */
+	{ "oacute",	243 }, /* small o, acute accent  */
+	{ "ocirc",	244 }, /* small o, circumflex accent  */
+	{ "ograve",	242 }, /* small o, grave accent  */
+	{ "ordf",	170 }, /* ordinal indicator, feminine  */
+	{ "ordm",	186 }, /* ordinal indicator, masculine  */
+	{ "oslash",	248 }, /* small o, slash  */
+	{ "otilde",	245 }, /* small o, tilde  */
+	{ "ouml",	246 }, /* small o, dieresis or umlaut mark  */
+	{ "para",	182 }, /* pilcrow (paragraph sign)  */
+	{ "plusmn",	177 }, /* plus-or-minus sign  */
+	{ "pound",	163 }, /* pound sterling sign  */
+	{ "quot",	 34 }, /* double quote  */
+	{ "raquo",	187 }, /* angle quotation mark, right  */
+	{ "reg",	174 }, /* registered sign  */
+	{ "sect",	167 }, /* section sign  */
+	{ "shy",	173 }, /* soft hyphen  */
+	{ "sup1",	185 }, /* superscript one  */
+	{ "sup2",	178 }, /* superscript two  */
+	{ "sup3",	179 }, /* superscript three  */
+	{ "szlig",	223 }, /* small sharp s, German (sz ligature)  */
+	{ "thorn",	254 }, /* small thorn, Icelandic  */
+	{ "times",	215 }, /* multiply sign  */
+	{ "uacute",	250 }, /* small u, acute accent  */
+	{ "ucirc",	251 }, /* small u, circumflex accent  */
+	{ "ugrave",	249 }, /* small u, grave accent  */
+	{ "uml",	168 }, /* umlaut (dieresis)  */
+	{ "uuml",	252 }, /* small u, dieresis or umlaut mark  */
+	{ "yacute",	253 }, /* small y, acute accent  */
+	{ "yen",	165 }, /* yen sign  */
+	{ "yuml",	255 }, /* small y, dieresis or umlaut mark  */
+};
+
+/*
  * unvis - decode characters previously encoded by vis
  */
 int
 unvis(char *cp, int c, int *astate, int flag)
 {
 	unsigned char uc = (unsigned char)c;
+	unsigned char st, ia, is, lc;
+
+/*
+ * Bottom 8 bits of astate hold the state machine state.
+ * Top 8 bits hold the current character in the http 1866 nv string decoding
+ */
+#define GS(a)		((a) & 0xff)
+#define SS(a, b)	(((a) << 24) | (b))
+#define GI(a)		((a) >> 24)
 
 	_DIAGASSERT(cp != NULL);
 	_DIAGASSERT(astate != NULL);
+	st = GS(*astate);
 
 	if (flag & UNVIS_END) {
-		if (*astate == S_OCTAL2 || *astate == S_OCTAL3
-		    || *astate == S_HEX2) {
-			*astate = S_GROUND;
+		switch (st) {
+		case S_OCTAL2:
+		case S_OCTAL3:
+		case S_HEX2:
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
+		case S_GROUND:
+			return UNVIS_NOCHAR;
+		default:
+			return UNVIS_SYNBAD;
 		}
-		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
 	}
 
-	switch (*astate) {
+	switch (st) {
 
 	case S_GROUND:
 		*cp = 0;
-		if (c == '\\') {
-			*astate = S_START;
+		if ((flag & VIS_NOESCAPE) == 0 && c == '\\') {
+			*astate = SS(0, S_START);
 			return UNVIS_NOCHAR;
 		}
-		if ((flag & VIS_HTTPSTYLE) && c == '%') {
-			*astate = S_HEX1;
+		if ((flag & VIS_HTTP1808) && c == '%') {
+			*astate = SS(0, S_HEX1);
+			return UNVIS_NOCHAR;
+		}
+		if ((flag & VIS_HTTP1866) && c == '&') {
+			*astate = SS(0, S_AMP);
 			return UNVIS_NOCHAR;
 		}
 		if ((flag & VIS_MIMESTYLE) && c == '=') {
-			*astate = S_MIME1;
+			*astate = SS(0, S_MIME1);
 			return UNVIS_NOCHAR;
 		}
 		*cp = c;
@@ -114,85 +245,82 @@
 		switch(c) {
 		case '\\':
 			*cp = c;
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case '0': case '1': case '2': case '3':
 		case '4': case '5': case '6': case '7':
 			*cp = (c - '0');
-			*astate = S_OCTAL2;
+			*astate = SS(0, S_OCTAL2);
 			return UNVIS_NOCHAR;
 		case 'M':
 			*cp = (char)0200;
-			*astate = S_META;
+			*astate = SS(0, S_META);
 			return UNVIS_NOCHAR;
 		case '^':
-			*astate = S_CTRL;
+			*astate = SS(0, S_CTRL);
 			return UNVIS_NOCHAR;
 		case 'n':
 			*cp = '\n';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 'r':
 			*cp = '\r';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 'b':
 			*cp = '\b';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 'a':
 			*cp = '\007';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 'v':
 			*cp = '\v';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 't':
 			*cp = '\t';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 'f':
 			*cp = '\f';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 's':
 			*cp = ' ';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case 'E':
 			*cp = '\033';
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		case '\n':
 			/*
 			 * hidden newline
 			 */
-			*astate = S_GROUND;
-			return (UNVIS_NOCHAR);
+			*astate = SS(0, S_GROUND);
+			return UNVIS_NOCHAR;
 		case '$':
 			/*
 			 * hidden marker
 			 */
-			*astate = S_GROUND;
-			return (UNVIS_NOCHAR);
+			*astate = SS(0, S_GROUND);
+			return UNVIS_NOCHAR;
 		}
-		*astate = S_GROUND;
-		return (UNVIS_SYNBAD);
+		goto bad;
 
 	case S_META:
 		if (c == '-')
-			*astate = S_META1;
+			*astate = SS(0, S_META1);
 		else if (c == '^')
-			*astate = S_CTRL;
-		else {
-			*astate = S_GROUND;
-			return (UNVIS_SYNBAD);
-		}
+			*astate = SS(0, S_CTRL);
+		else 
+			goto bad;
 		return UNVIS_NOCHAR;
 
 	case S_META1:
-		*astate = S_GROUND;
+		*astate = SS(0, S_GROUND);
 		*cp |= c;
 		return UNVIS_VALID;
 
@@ -201,7 +329,7 @@
 			*cp |= 0177;
 		else
 			*cp |= c & 037;
-		*astate = S_GROUND;
+		*astate = SS(0, S_GROUND);
 		return UNVIS_VALID;
 
 	case S_OCTAL2:	/* second possible octal digit */
@@ -210,17 +338,17 @@
 			 * yes - and maybe a third
 			 */
 			*cp = (*cp << 3) + (c - '0');
-			*astate = S_OCTAL3;
+			*astate = SS(0, S_OCTAL3);
 			return UNVIS_NOCHAR;
 		}
 		/*
 		 * no - done with current sequence, push back passed char
 		 */
-		*astate = S_GROUND;
+		*astate = SS(0, S_GROUND);
 		return UNVIS_VALIDPUSH;
 
 	case S_OCTAL3:	/* third possible octal digit */
-		*astate = S_GROUND;
+		*astate = SS(0, S_GROUND);
 		if (isoctal(uc)) {
 			*cp = (*cp << 3) + (c - '0');
 			return UNVIS_VALID;
@@ -233,13 +361,13 @@
 	case S_HEX1:
 		if (isxdigit(uc)) {
 			*cp = xtod(uc);
-			*astate = S_HEX2;
+			*astate = SS(0, S_HEX2);
 			return UNVIS_NOCHAR;
 		}
 		/*
 		 * no - done with current sequence, push back passed char
 		 */
-		*astate = S_GROUND;
+		*astate = SS(0, S_GROUND);
 		return UNVIS_VALIDPUSH;
 
 	case S_HEX2:
@@ -252,25 +380,23 @@
 
 	case S_MIME1:
 		if (uc == '\n' || uc == '\r') {
-			*astate = S_EATCRNL;
+			*astate = SS(0, S_EATCRNL);
 			return UNVIS_NOCHAR;
 		}
 		if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
 			*cp = XTOD(uc);
-			*astate = S_MIME2;
+			*astate = SS(0, S_MIME2);
 			return UNVIS_NOCHAR;
 		}
-		*astate = S_GROUND;
-		return UNVIS_SYNBAD;
+		goto bad;
 
 	case S_MIME2:
 		if (isxdigit(uc) && (isdigit(uc) || isupper(uc))) {
-			*astate = S_GROUND;
+			*astate = SS(0, S_GROUND);
 			*cp = XTOD(uc) | (*cp << 4);
 			return UNVIS_VALID;
 		}
-		*astate = S_GROUND;
-		return UNVIS_SYNBAD;
+		goto bad;
 
 	case S_EATCRNL:
 		switch (uc) {
@@ -278,18 +404,65 @@
 		case '\n':
 			return UNVIS_NOCHAR;
 		case '=':
-			*astate = S_MIME1;
+			*astate = SS(0, S_MIME1);
 			return UNVIS_NOCHAR;
 		default:
 			*cp = uc;
+			*astate = SS(0, S_GROUND);
 			return UNVIS_VALID;
 		}
 
+	case S_AMP:
+		*cp = 0;
+		if (uc == '#') {
+			*astate = SS(0, S_NUMBER);
+			return UNVIS_NOCHAR;
+		}
+		*astate = SS(0, S_STRING);
+		/*FALLTHROUGH*/
+
+	case S_STRING:
+		ia = *cp;		/* index in the array */
+		is = GI(*astate);	/* index in the string */
+		lc = is == 0 ? 0 : nv[ia].name[is - 1];	/* last character */
+
+		if (uc == ';')
+			uc = '\0';
+
+		for (; ia < __arraycount(nv); ia++) {
+			if (is != 0 && nv[ia].name[is - 1] != lc)
+				goto bad;
+			if (nv[ia].name[is] == uc)
+				break;
+		}
+
+		if (*cp == __arraycount(nv))
+			goto bad;
+
+		if (uc != 0) {
+			*cp = ia;
+			*astate = SS(is + 1, S_STRING);
+			return UNVIS_NOCHAR;
+		}
+
+		*cp = nv[ia].value;
+		*astate = SS(0, S_GROUND);
+		return UNVIS_VALID;
+
+	case S_NUMBER:
+		if (uc == ';')
+			return UNVIS_VALID;
+		if (!isdigit(uc))
+			goto bad;
+		*cp += (*cp * 10) + uc - '0';
+		return UNVIS_NOCHAR;
+
 	default:
+	bad:
 		/*
 		 * decoder in unknown state - (probably uninitialized)
 		 */
-		*astate = S_GROUND;
+		*astate = SS(0, S_GROUND);
 		return UNVIS_SYNBAD;
 	}
 }
@@ -302,10 +475,7 @@
  */
 
 int
-strunvisx(dst, src, flag)
-	char *dst;
-	const char *src;
-	int flag;
+strunvisx(char *dst, const char *src, int flag)
 {
 	char c;
 	char *start = dst;
@@ -337,9 +507,7 @@
 }
 
 int
-strunvis(dst, src)
-	char *dst;
-	const char *src;
+strunvis(char *dst, const char *src)
 {
 	return strunvisx(dst, src, 0);
 }

Reply via email to