xref: /plan9/sys/src/libhttpd/httpunesc.c (revision 9a747e4fd48b9f4522c70c07e8f882a15030f964)
1 #include <u.h>
2 #include <libc.h>
3 #include <bin.h>
4 #include <httpd.h>
5 
6 /*
7  *  go from http with latin1 escapes to utf,
8  *  we assume that anything >= Runeself is already in utf
9  */
10 char *
httpunesc(HConnect * cc,char * s)11 httpunesc(HConnect *cc, char *s)
12 {
13 	char *t, *v;
14 	int c;
15 	Htmlesc *e;
16 
17 	v = halloc(cc, UTFmax*strlen(s) + 1);
18 	for(t = v; c = *s;){
19 		if(c == '&'){
20 			if(s[1] == '#' && s[2] && s[3] && s[4] && s[5] == ';'){
21 				c = atoi(s+2);
22 				if(c < Runeself){
23 					*t++ = c;
24 					s += 6;
25 					continue;
26 				}
27 				if(c < 256 && c >= 161){
28 					e = &htmlesc[c-161];
29 					t += runetochar(t, &e->value);
30 					s += 6;
31 					continue;
32 				}
33 			} else {
34 				for(e = htmlesc; e->name != nil; e++)
35 					if(strncmp(e->name, s, strlen(e->name)) == 0)
36 						break;
37 				if(e->name != nil){
38 					t += runetochar(t, &e->value);
39 					s += strlen(e->name);
40 					continue;
41 				}
42 			}
43 		}
44 		*t++ = c;
45 		s++;
46 	}
47 	*t = 0;
48 	return v;
49 }
50