httpunesc.c 888 B

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