urlunesc.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. #include <bin.h>
  12. #include <httpd.h>
  13. /* go from url with escaped utf to utf */
  14. char *
  15. hurlunesc(HConnect *cc, char *s)
  16. {
  17. char *t, *v, *u;
  18. Rune r;
  19. int c, n;
  20. /* unescape */
  21. u = halloc(cc, strlen(s)+1);
  22. for(t = u; c = *s; s++){
  23. if(c == '%'){
  24. n = s[1];
  25. if(n >= '0' && n <= '9')
  26. n = n - '0';
  27. else if(n >= 'A' && n <= 'F')
  28. n = n - 'A' + 10;
  29. else if(n >= 'a' && n <= 'f')
  30. n = n - 'a' + 10;
  31. else
  32. break;
  33. r = n;
  34. n = s[2];
  35. if(n >= '0' && n <= '9')
  36. n = n - '0';
  37. else if(n >= 'A' && n <= 'F')
  38. n = n - 'A' + 10;
  39. else if(n >= 'a' && n <= 'f')
  40. n = n - 'a' + 10;
  41. else
  42. break;
  43. s += 2;
  44. c = (r<<4)+n;
  45. }
  46. *t++ = c;
  47. }
  48. *t = '\0';
  49. /* convert to valid utf */
  50. v = halloc(cc, UTFmax*strlen(u) + 1);
  51. s = u;
  52. t = v;
  53. while(*s){
  54. /* in decoding error, assume latin1 */
  55. if((n=chartorune(&r, s)) == 1 && r == Runeerror)
  56. r = (unsigned char)*s;
  57. s += n;
  58. t += runetochar(t, &r);
  59. }
  60. *t = '\0';
  61. return v;
  62. }