escape.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* Escape and unescape URL encoding in strings. The functions return a new
  23. * allocated string or NULL if an error occurred. */
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "urldata.h"
  27. #include "warnless.h"
  28. #include "non-ascii.h"
  29. #include "escape.h"
  30. #include "strdup.h"
  31. /* The last 3 #include files should be in this order */
  32. #include "curl_printf.h"
  33. #include "curl_memory.h"
  34. #include "memdebug.h"
  35. /* Portable character check (remember EBCDIC). Do not use isalnum() because
  36. its behavior is altered by the current locale.
  37. See https://tools.ietf.org/html/rfc3986#section-2.3
  38. */
  39. bool Curl_isunreserved(unsigned char in)
  40. {
  41. switch(in) {
  42. case '0': case '1': case '2': case '3': case '4':
  43. case '5': case '6': case '7': case '8': case '9':
  44. case 'a': case 'b': case 'c': case 'd': case 'e':
  45. case 'f': case 'g': case 'h': case 'i': case 'j':
  46. case 'k': case 'l': case 'm': case 'n': case 'o':
  47. case 'p': case 'q': case 'r': case 's': case 't':
  48. case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
  49. case 'A': case 'B': case 'C': case 'D': case 'E':
  50. case 'F': case 'G': case 'H': case 'I': case 'J':
  51. case 'K': case 'L': case 'M': case 'N': case 'O':
  52. case 'P': case 'Q': case 'R': case 'S': case 'T':
  53. case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
  54. case '-': case '.': case '_': case '~':
  55. return TRUE;
  56. default:
  57. break;
  58. }
  59. return FALSE;
  60. }
  61. /* for ABI-compatibility with previous versions */
  62. char *curl_escape(const char *string, int inlength)
  63. {
  64. return curl_easy_escape(NULL, string, inlength);
  65. }
  66. /* for ABI-compatibility with previous versions */
  67. char *curl_unescape(const char *string, int length)
  68. {
  69. return curl_easy_unescape(NULL, string, length, NULL);
  70. }
  71. char *curl_easy_escape(struct Curl_easy *data, const char *string,
  72. int inlength)
  73. {
  74. size_t length;
  75. CURLcode result;
  76. struct dynbuf d;
  77. if(inlength < 0)
  78. return NULL;
  79. Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH);
  80. length = (inlength?(size_t)inlength:strlen(string));
  81. if(!length)
  82. return strdup("");
  83. while(length--) {
  84. unsigned char in = *string; /* we need to treat the characters unsigned */
  85. if(Curl_isunreserved(in)) {
  86. /* append this */
  87. if(Curl_dyn_addn(&d, &in, 1))
  88. return NULL;
  89. }
  90. else {
  91. /* encode it */
  92. char encoded[4];
  93. result = Curl_convert_to_network(data, (char *)&in, 1);
  94. if(result) {
  95. /* Curl_convert_to_network calls failf if unsuccessful */
  96. Curl_dyn_free(&d);
  97. return NULL;
  98. }
  99. msnprintf(encoded, sizeof(encoded), "%%%02X", in);
  100. if(Curl_dyn_add(&d, encoded))
  101. return NULL;
  102. }
  103. string++;
  104. }
  105. return Curl_dyn_ptr(&d);
  106. }
  107. /*
  108. * Curl_urldecode() URL decodes the given string.
  109. *
  110. * Returns a pointer to a malloced string in *ostring with length given in
  111. * *olen. If length == 0, the length is assumed to be strlen(string).
  112. *
  113. * 'data' can be set to NULL but then this function can't convert network
  114. * data to host for non-ascii.
  115. *
  116. * ctrl options:
  117. * - REJECT_NADA: accept everything
  118. * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
  119. * the data
  120. * - REJECT_ZERO: rejects decoded zero bytes
  121. *
  122. * The values for the enum starts at 2, to make the assert detect legacy
  123. * invokes that used TRUE/FALSE (0 and 1).
  124. */
  125. CURLcode Curl_urldecode(struct Curl_easy *data,
  126. const char *string, size_t length,
  127. char **ostring, size_t *olen,
  128. enum urlreject ctrl)
  129. {
  130. size_t alloc;
  131. char *ns;
  132. size_t strindex = 0;
  133. unsigned long hex;
  134. CURLcode result = CURLE_OK;
  135. DEBUGASSERT(string);
  136. DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
  137. alloc = (length?length:strlen(string)) + 1;
  138. ns = malloc(alloc);
  139. if(!ns)
  140. return CURLE_OUT_OF_MEMORY;
  141. while(--alloc > 0) {
  142. unsigned char in = *string;
  143. if(('%' == in) && (alloc > 2) &&
  144. ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
  145. /* this is two hexadecimal digits following a '%' */
  146. char hexstr[3];
  147. char *ptr;
  148. hexstr[0] = string[1];
  149. hexstr[1] = string[2];
  150. hexstr[2] = 0;
  151. hex = strtoul(hexstr, &ptr, 16);
  152. in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
  153. if(data) {
  154. result = Curl_convert_from_network(data, (char *)&in, 1);
  155. if(result) {
  156. /* Curl_convert_from_network calls failf if unsuccessful */
  157. free(ns);
  158. return result;
  159. }
  160. }
  161. string += 2;
  162. alloc -= 2;
  163. }
  164. if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
  165. ((ctrl == REJECT_ZERO) && (in == 0))) {
  166. free(ns);
  167. return CURLE_URL_MALFORMAT;
  168. }
  169. ns[strindex++] = in;
  170. string++;
  171. }
  172. ns[strindex] = 0; /* terminate it */
  173. if(olen)
  174. /* store output size */
  175. *olen = strindex;
  176. /* store output string */
  177. *ostring = ns;
  178. return CURLE_OK;
  179. }
  180. /*
  181. * Unescapes the given URL escaped string of given length. Returns a
  182. * pointer to a malloced string with length given in *olen.
  183. * If length == 0, the length is assumed to be strlen(string).
  184. * If olen == NULL, no output length is stored.
  185. */
  186. char *curl_easy_unescape(struct Curl_easy *data, const char *string,
  187. int length, int *olen)
  188. {
  189. char *str = NULL;
  190. if(length >= 0) {
  191. size_t inputlen = length;
  192. size_t outputlen;
  193. CURLcode res = Curl_urldecode(data, string, inputlen, &str, &outputlen,
  194. REJECT_NADA);
  195. if(res)
  196. return NULL;
  197. if(olen) {
  198. if(outputlen <= (size_t) INT_MAX)
  199. *olen = curlx_uztosi(outputlen);
  200. else
  201. /* too large to return in an int, fail! */
  202. Curl_safefree(str);
  203. }
  204. }
  205. return str;
  206. }
  207. /* For operating systems/environments that use different malloc/free
  208. systems for the app and for this library, we provide a free that uses
  209. the library's memory system */
  210. void curl_free(void *p)
  211. {
  212. free(p);
  213. }