httpunesc.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. /*
  14. * go from http with latin1 escapes to utf,
  15. * we assume that anything >= Runeself is already in utf
  16. */
  17. char *
  18. httpunesc(HConnect *cc, char *s)
  19. {
  20. char *t, *v;
  21. int c;
  22. Htmlesc *e;
  23. v = halloc(cc, UTFmax*strlen(s) + 1);
  24. for(t = v; (c = *s) != '\0';){
  25. if(c == '&'){
  26. if(s[1] == '#' && s[2] && s[3] && s[4] && s[5] == ';'){
  27. c = atoi(s+2);
  28. if(c < Runeself){
  29. *t++ = c;
  30. s += 6;
  31. continue;
  32. }
  33. if(c < 256 && c >= 161){
  34. e = &htmlesc[c-161];
  35. t += runetochar(t, &e->value);
  36. s += 6;
  37. continue;
  38. }
  39. } else {
  40. for(e = htmlesc; e->name != nil; e++)
  41. if(strncmp(e->name, s, strlen(e->name)) == 0)
  42. break;
  43. if(e->name != nil){
  44. t += runetochar(t, &e->value);
  45. s += strlen(e->name);
  46. continue;
  47. }
  48. }
  49. }
  50. *t++ = c;
  51. s++;
  52. }
  53. *t = 0;
  54. return v;
  55. }