1
0

percent_decode.c 953 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /*
  2. * Decode %-encoding in URL style.
  3. */
  4. #include <ctype.h>
  5. #include "misc.h"
  6. void percent_decode_bs(BinarySink *bs, ptrlen data)
  7. {
  8. const char *p, *e; // WINSCP
  9. for (p = data.ptr, e = ptrlen_end(data); p < e; p++) {
  10. char c = *p;
  11. if (c == '%' && e-p >= 3 &&
  12. isxdigit((unsigned char)p[1]) &&
  13. isxdigit((unsigned char)p[2])) {
  14. char hex[3];
  15. hex[0] = p[1];
  16. hex[1] = p[2];
  17. hex[2] = '\0';
  18. put_byte(bs, strtoul(hex, NULL, 16));
  19. p += 2;
  20. } else {
  21. put_byte(bs, c);
  22. }
  23. }
  24. }
  25. void percent_decode_fp(FILE *fp, ptrlen data)
  26. {
  27. stdio_sink ss;
  28. stdio_sink_init(&ss, fp);
  29. percent_decode_bs(BinarySink_UPCAST(&ss), data);
  30. }
  31. strbuf *percent_decode_sb(ptrlen data)
  32. {
  33. strbuf *sb = strbuf_new();
  34. percent_decode_bs(BinarySink_UPCAST(sb), data);
  35. return sb;
  36. }