Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 26 May 2000 11:40:05 +0200 (CEST)
From:      alex@big.endian.de
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   misc/18829: add HTTP-URI-encoding to vis(3) and unvis(3)
Message-ID:  <200005260940.LAA05897@cichlids.cichlids.com>

next in thread | raw e-mail | index | archive | help

>Number:         18829
>Category:       misc
>Synopsis:       add HTTP-URI-encoding to vis(3) and unvis(3)
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Fri May 26 04:40:01 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Alexander Langer
>Release:        FreeBSD 5.0-CURRENT i386
>Organization:
none
>Environment:


>Description:

This adds HTTP-URI-encoding.

E.g.:
Hello this is my homedir~:
would become:
Hello%20this%20is ...

Needed often, for example for fetch(1). 

>How-To-Repeat:


>Fix:

--- /usr/src/lib/libc/gen/vis.c	Fri Jul 12 20:54:15 1996
+++ vis.c	Sat Oct  2 20:20:17 1999
@@ -52,6 +52,21 @@
 	register int flag;
 {
 	c = (unsigned char)c;
+
+	if (flag & VIS_HTTPSTYLE) {
+	    if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z')
+		|| (c >= 'a' && c <= 'z') || c == '$' || c == '-'
+		|| c == '_' || c == '\'' || c == '+' || c == '!' ||
+		c == '(' || c == ')' || c == ',' || c == '"' ||
+		c == ';' || c == '/' || c == '?' || c == ':' ||
+		c == '@' || c == '&' || c == '=' || c == '+')) {
+		*dst++ = '%';
+		snprintf(dst, 4, (c < 16 ? "0%X" : "%X"), c);
+		dst += 2;
+		goto done;
+	    }
+	}
+
 	if (isgraph(c) ||
 	   ((flag & VIS_SP) == 0 && c == ' ') ||
 	   ((flag & VIS_TAB) == 0 && c == '\t') ||


--- /usr/src/lib/libc/gen/unvis.c	Fri Jul 12 20:54:14 1996
+++ unvis.c	Sun Oct 24 13:39:08 1999
@@ -48,8 +48,12 @@
 #define	S_CTRL		4	/* control char started (^) */
 #define	S_OCTAL2	5	/* octal digit 2 */
 #define	S_OCTAL3	6	/* octal digit 3 */
+#define	S_HEX2		7	/* hex digit 2 */
+
+#define	S_HTTP		0x080	/* %HEXHEX escape */
 
 #define	isoctal(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '7')
+#define	ishex(c)	(((u_char)(c)) >= '0' && ((u_char)(c)) <= '9' || ((u_char)(c)) >= 'a' && ((u_char)(c)) <= 'f')
 
 /*
  * unvis - decode characters previously encoded by vis
@@ -68,7 +72,7 @@
 		return (*astate == S_GROUND ? UNVIS_NOCHAR : UNVIS_SYNBAD);
 	}
 
-	switch (*astate) {
+	switch (*astate & ~S_HTTP) {
 
 	case S_GROUND:
 		*cp = 0;
@@ -76,10 +80,21 @@
 			*astate = S_START;
 			return (0);
 		}
+		if (flag & VIS_HTTPSTYLE && c == '%') {
+			*astate = S_START | S_HTTP;
+			return (0);
+		}
 		*cp = c;
 		return (UNVIS_VALID);
 
 	case S_START:
+		if (*astate & S_HTTP) {
+		    if (ishex(tolower(c))) {
+			*cp = isdigit(c) ? (c - '0') : (tolower(c) - 'a');
+			*astate = S_HEX2;
+			return (0);
+		    }
+		}
 		switch(c) {
 		case '\\':
 			*cp = c;
@@ -199,6 +214,13 @@
 		 */
 		return (UNVIS_VALIDPUSH);
 
+	case S_HEX2:	/* second mandatory hex digit */
+		if (ishex(tolower(c))) {
+			*cp = (isdigit(c) ? (*cp << 4) + (c - '0') : (*cp << 4) + (tolower(c) - 'a' + 10));
+		}
+		*astate = S_GROUND;
+		return (UNVIS_VALID);
+
 	default:
 		/*
 		 * decoder in unknown state - (probably uninitialized)
@@ -227,6 +249,37 @@
 	while ( (c = *src++) ) {
 	again:
 		switch (unvis(dst, c, &state, 0)) {
+		case UNVIS_VALID:
+			dst++;
+			break;
+		case UNVIS_VALIDPUSH:
+			dst++;
+			goto again;
+		case 0:
+		case UNVIS_NOCHAR:
+			break;
+		default:
+			return (-1);
+		}
+	}
+	if (unvis(dst, c, &state, UNVIS_END) == UNVIS_VALID)
+		dst++;
+	*dst = '\0';
+	return (dst - start);
+}
+
+int
+strunvisx(dst, src, flag)
+	register char *dst;
+	register const char *src;
+{
+	register char c;
+	char *start = dst;
+	int state = 0;
+    
+	while ( (c = *src++) ) {
+	again:
+		switch (unvis(dst, c, &state, flag)) {
 		case UNVIS_VALID:
 			dst++;
 			break;


--- /usr/src/include/vis.h	Sun Aug 29 16:38:58 1999
+++ vis.h	Sun Oct  3 13:45:00 1999
@@ -64,6 +64,7 @@
  * other
  */
 #define	VIS_NOSLASH	0x40	/* inhibit printing '\' */
+#define	VIS_HTTPSTYLE	0x80	/* http-style escape % HEX HEX */
 
 /*
  * unvis return codes

>Release-Note:
>Audit-Trail:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200005260940.LAA05897>