1
0

hurl.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #include "hurl.h"
  2. #include "hdef.h"
  3. /*
  4. static bool Curl_isunreserved(unsigned char in)
  5. {
  6. switch(in) {
  7. case '0': case '1': case '2': case '3': case '4':
  8. case '5': case '6': case '7': case '8': case '9':
  9. case 'a': case 'b': case 'c': case 'd': case 'e':
  10. case 'f': case 'g': case 'h': case 'i': case 'j':
  11. case 'k': case 'l': case 'm': case 'n': case 'o':
  12. case 'p': case 'q': case 'r': case 's': case 't':
  13. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  14. case 'A': case 'B': case 'C': case 'D': case 'E':
  15. case 'F': case 'G': case 'H': case 'I': case 'J':
  16. case 'K': case 'L': case 'M': case 'N': case 'O':
  17. case 'P': case 'Q': case 'R': case 'S': case 'T':
  18. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  19. case '-': case '.': case '_': case '~':
  20. return TRUE;
  21. default:
  22. break;
  23. }
  24. return FLASE;
  25. }
  26. */
  27. static inline bool is_unambiguous(char c) {
  28. return IS_ALPHANUM(c) ||
  29. c == '-' ||
  30. c == '_' ||
  31. c == '.' ||
  32. c == '~';
  33. }
  34. static inline bool char_in_str(char c, const char* str) {
  35. const char* p = str;
  36. while (*p && *p != c) ++p;
  37. return *p != '\0';
  38. }
  39. static inline unsigned char hex2i(char hex) {
  40. return hex <= '9' ? hex - '0' :
  41. hex <= 'F' ? hex - 'A' + 10 : hex - 'a' + 10;
  42. }
  43. std::string url_escape(const char* istr, const char* unescaped_chars) {
  44. std::string ostr;
  45. static char tab[] = "0123456789ABCDEF";
  46. const unsigned char* p = reinterpret_cast<const unsigned char*>(istr);
  47. char szHex[4] = "%00";
  48. while (*p != '\0') {
  49. if (is_unambiguous(*p) || char_in_str(*p, unescaped_chars)) {
  50. ostr += *p;
  51. }
  52. else {
  53. szHex[1] = tab[*p >> 4];
  54. szHex[2] = tab[*p & 0xF];
  55. ostr += szHex;
  56. }
  57. ++p;
  58. }
  59. return ostr;
  60. }
  61. std::string url_unescape(const char* istr) {
  62. std::string ostr;
  63. const char* p = istr;
  64. while (*p != '\0') {
  65. if (*p == '%' &&
  66. IS_HEX(p[1]) &&
  67. IS_HEX(p[2])) {
  68. ostr += ((hex2i(p[1]) << 4) | hex2i(p[2]));
  69. p += 3;
  70. }
  71. else {
  72. ostr += *p;
  73. ++p;
  74. }
  75. }
  76. return ostr;
  77. }