You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

196 lines
5.7 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2008, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/urlencode.h"
  28. #include "talk/base/common.h"
  29. #include "talk/base/stringutils.h"
  30. static int HexPairValue(const char * code) {
  31. int value = 0;
  32. const char * pch = code;
  33. for (;;) {
  34. int digit = *pch++;
  35. if (digit >= '0' && digit <= '9') {
  36. value += digit - '0';
  37. }
  38. else if (digit >= 'A' && digit <= 'F') {
  39. value += digit - 'A' + 10;
  40. }
  41. else if (digit >= 'a' && digit <= 'f') {
  42. value += digit - 'a' + 10;
  43. }
  44. else {
  45. return -1;
  46. }
  47. if (pch == code + 2)
  48. return value;
  49. value <<= 4;
  50. }
  51. }
  52. int InternalUrlDecode(const char *source, char *dest,
  53. bool encode_space_as_plus) {
  54. char * start = dest;
  55. while (*source) {
  56. switch (*source) {
  57. case '+':
  58. if (encode_space_as_plus) {
  59. *(dest++) = ' ';
  60. } else {
  61. *dest++ = *source;
  62. }
  63. break;
  64. case '%':
  65. if (source[1] && source[2]) {
  66. int value = HexPairValue(source + 1);
  67. if (value >= 0) {
  68. *(dest++) = value;
  69. source += 2;
  70. }
  71. else {
  72. *dest++ = '?';
  73. }
  74. }
  75. else {
  76. *dest++ = '?';
  77. }
  78. break;
  79. default:
  80. *dest++ = *source;
  81. }
  82. source++;
  83. }
  84. *dest = 0;
  85. return static_cast<int>(dest - start);
  86. }
  87. int UrlDecode(const char *source, char *dest) {
  88. return InternalUrlDecode(source, dest, true);
  89. }
  90. int UrlDecodeWithoutEncodingSpaceAsPlus(const char *source, char *dest) {
  91. return InternalUrlDecode(source, dest, false);
  92. }
  93. bool IsValidUrlChar(char ch, bool unsafe_only) {
  94. if (unsafe_only) {
  95. return !(ch <= ' ' || strchr("\\\"^&`<>[]{}", ch));
  96. } else {
  97. return isalnum(ch) || strchr("-_.!~*'()", ch);
  98. }
  99. }
  100. int InternalUrlEncode(const char *source, char *dest, unsigned int max,
  101. bool encode_space_as_plus, bool unsafe_only) {
  102. static const char *digits = "0123456789ABCDEF";
  103. if (max == 0) {
  104. return 0;
  105. }
  106. char *start = dest;
  107. while (static_cast<unsigned>(dest - start) < max && *source) {
  108. unsigned char ch = static_cast<unsigned char>(*source);
  109. if (*source == ' ' && encode_space_as_plus && !unsafe_only) {
  110. *dest++ = '+';
  111. } else if (IsValidUrlChar(ch, unsafe_only)) {
  112. *dest++ = *source;
  113. } else {
  114. if (static_cast<unsigned>(dest - start) + 4 > max) {
  115. break;
  116. }
  117. *dest++ = '%';
  118. *dest++ = digits[(ch >> 4) & 0x0F];
  119. *dest++ = digits[ ch & 0x0F];
  120. }
  121. source++;
  122. }
  123. ASSERT(static_cast<unsigned int>(dest - start) < max);
  124. *dest = 0;
  125. return static_cast<int>(dest - start);
  126. }
  127. int UrlEncode(const char *source, char *dest, unsigned max) {
  128. return InternalUrlEncode(source, dest, max, true, false);
  129. }
  130. int UrlEncodeWithoutEncodingSpaceAsPlus(const char *source, char *dest,
  131. unsigned max) {
  132. return InternalUrlEncode(source, dest, max, false, false);
  133. }
  134. int UrlEncodeOnlyUnsafeChars(const char *source, char *dest, unsigned max) {
  135. return InternalUrlEncode(source, dest, max, false, true);
  136. }
  137. std::string
  138. InternalUrlDecodeString(const std::string & encoded,
  139. bool encode_space_as_plus) {
  140. size_t needed_length = encoded.length() + 1;
  141. char* buf = STACK_ARRAY(char, needed_length);
  142. InternalUrlDecode(encoded.c_str(), buf, encode_space_as_plus);
  143. return buf;
  144. }
  145. std::string
  146. UrlDecodeString(const std::string & encoded) {
  147. return InternalUrlDecodeString(encoded, true);
  148. }
  149. std::string
  150. UrlDecodeStringWithoutEncodingSpaceAsPlus(const std::string & encoded) {
  151. return InternalUrlDecodeString(encoded, false);
  152. }
  153. std::string
  154. InternalUrlEncodeString(const std::string & decoded,
  155. bool encode_space_as_plus,
  156. bool unsafe_only) {
  157. int needed_length = static_cast<int>(decoded.length()) * 3 + 1;
  158. char* buf = STACK_ARRAY(char, needed_length);
  159. InternalUrlEncode(decoded.c_str(), buf, needed_length,
  160. encode_space_as_plus, unsafe_only);
  161. return buf;
  162. }
  163. std::string
  164. UrlEncodeString(const std::string & decoded) {
  165. return InternalUrlEncodeString(decoded, true, false);
  166. }
  167. std::string
  168. UrlEncodeStringWithoutEncodingSpaceAsPlus(const std::string & decoded) {
  169. return InternalUrlEncodeString(decoded, false, false);
  170. }
  171. std::string
  172. UrlEncodeStringForOnlyUnsafeChars(const std::string & decoded) {
  173. return InternalUrlEncodeString(decoded, false, true);
  174. }