urlunesc.c 995 B

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