urlunesc.c (995B)
1 #include <u.h> 2 #include <libc.h> 3 #include <bin.h> 4 #include <httpd.h> 5 6 /* go from url with escaped utf to utf */ 7 char * 8 hurlunesc(HConnect *cc, char *s) 9 { 10 char *t, *v, *u; 11 Rune r; 12 int c, n; 13 14 /* unescape */ 15 u = halloc(cc, strlen(s)+1); 16 for(t = u; c = *s; s++){ 17 if(c == '%'){ 18 n = s[1]; 19 if(n >= '0' && n <= '9') 20 n = n - '0'; 21 else if(n >= 'A' && n <= 'F') 22 n = n - 'A' + 10; 23 else if(n >= 'a' && n <= 'f') 24 n = n - 'a' + 10; 25 else 26 break; 27 r = n; 28 n = s[2]; 29 if(n >= '0' && n <= '9') 30 n = n - '0'; 31 else if(n >= 'A' && n <= 'F') 32 n = n - 'A' + 10; 33 else if(n >= 'a' && n <= 'f') 34 n = n - 'a' + 10; 35 else 36 break; 37 s += 2; 38 c = (r<<4)+n; 39 } 40 *t++ = c; 41 } 42 *t = '\0'; 43 44 /* convert to valid utf */ 45 v = halloc(cc, UTFmax*strlen(u) + 1); 46 s = u; 47 t = v; 48 while(*s){ 49 /* in decoding error, assume latin1 */ 50 if((n=chartorune(&r, s)) == 1 && r == Runeerror) 51 r = (uchar)*s; 52 s += n; 53 t += runetochar(t, &r); 54 } 55 *t = '\0'; 56 57 return v; 58 }