escape.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. /* Escape and unescape URL encoding in strings. The functions return a new
  25. * allocated string or NULL if an error occurred. */
  26. #include "curl_setup.h"
  27. #include <curl/curl.h>
  28. struct Curl_easy;
  29. #include "urldata.h"
  30. #include "curlx/warnless.h"
  31. #include "escape.h"
  32. #include "strdup.h"
  33. #include "curlx/strparse.h"
  34. /* The last 3 #include files should be in this order */
  35. #include "curl_printf.h"
  36. #include "curl_memory.h"
  37. #include "memdebug.h"
  38. /* for ABI-compatibility with previous versions */
  39. char *curl_escape(const char *string, int inlength)
  40. {
  41. return curl_easy_escape(NULL, string, inlength);
  42. }
  43. /* for ABI-compatibility with previous versions */
  44. char *curl_unescape(const char *string, int length)
  45. {
  46. return curl_easy_unescape(NULL, string, length, NULL);
  47. }
  48. /* Escapes for URL the given unescaped string of given length.
  49. * 'data' is ignored since 7.82.0.
  50. */
  51. char *curl_easy_escape(CURL *data, const char *string,
  52. int inlength)
  53. {
  54. size_t length;
  55. struct dynbuf d;
  56. (void)data;
  57. if(!string || (inlength < 0))
  58. return NULL;
  59. length = (inlength ? (size_t)inlength : strlen(string));
  60. if(!length)
  61. return strdup("");
  62. curlx_dyn_init(&d, length * 3 + 1);
  63. while(length--) {
  64. /* treat the characters unsigned */
  65. unsigned char in = (unsigned char)*string++;
  66. if(ISUNRESERVED(in)) {
  67. /* append this */
  68. if(curlx_dyn_addn(&d, &in, 1))
  69. return NULL;
  70. }
  71. else {
  72. /* encode it */
  73. unsigned char out[3]={'%'};
  74. Curl_hexbyte(&out[1], in);
  75. if(curlx_dyn_addn(&d, out, 3))
  76. return NULL;
  77. }
  78. }
  79. return curlx_dyn_ptr(&d);
  80. }
  81. /*
  82. * Curl_urldecode() URL decodes the given string.
  83. *
  84. * Returns a pointer to a malloced string in *ostring with length given in
  85. * *olen. If length == 0, the length is assumed to be strlen(string).
  86. *
  87. * ctrl options:
  88. * - REJECT_NADA: accept everything
  89. * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
  90. * the data
  91. * - REJECT_ZERO: rejects decoded zero bytes
  92. *
  93. * The values for the enum starts at 2, to make the assert detect legacy
  94. * invokes that used TRUE/FALSE (0 and 1).
  95. */
  96. CURLcode Curl_urldecode(const char *string, size_t length,
  97. char **ostring, size_t *olen,
  98. enum urlreject ctrl)
  99. {
  100. size_t alloc;
  101. char *ns;
  102. DEBUGASSERT(string);
  103. DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
  104. alloc = (length ? length : strlen(string));
  105. ns = malloc(alloc + 1);
  106. if(!ns)
  107. return CURLE_OUT_OF_MEMORY;
  108. /* store output string */
  109. *ostring = ns;
  110. while(alloc) {
  111. unsigned char in = (unsigned char)*string;
  112. if(('%' == in) && (alloc > 2) &&
  113. ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
  114. /* this is two hexadecimal digits following a '%' */
  115. in = (unsigned char)((Curl_hexval(string[1]) << 4) |
  116. Curl_hexval(string[2]));
  117. string += 3;
  118. alloc -= 3;
  119. }
  120. else {
  121. string++;
  122. alloc--;
  123. }
  124. if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
  125. ((ctrl == REJECT_ZERO) && (in == 0))) {
  126. Curl_safefree(*ostring);
  127. return CURLE_URL_MALFORMAT;
  128. }
  129. *ns++ = (char)in;
  130. }
  131. *ns = 0; /* terminate it */
  132. if(olen)
  133. /* store output size */
  134. *olen = ns - *ostring;
  135. return CURLE_OK;
  136. }
  137. /*
  138. * Unescapes the given URL escaped string of given length. Returns a
  139. * pointer to a malloced string with length given in *olen.
  140. * If length == 0, the length is assumed to be strlen(string).
  141. * If olen == NULL, no output length is stored.
  142. * 'data' is ignored since 7.82.0.
  143. */
  144. char *curl_easy_unescape(CURL *data, const char *string,
  145. int length, int *olen)
  146. {
  147. char *str = NULL;
  148. (void)data;
  149. if(string && (length >= 0)) {
  150. size_t inputlen = (size_t)length;
  151. size_t outputlen;
  152. CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
  153. REJECT_NADA);
  154. if(res)
  155. return NULL;
  156. if(olen) {
  157. if(outputlen <= (size_t) INT_MAX)
  158. *olen = curlx_uztosi(outputlen);
  159. else
  160. /* too large to return in an int, fail! */
  161. Curl_safefree(str);
  162. }
  163. }
  164. return str;
  165. }
  166. /* For operating systems/environments that use different malloc/free
  167. systems for the app and for this library, we provide a free that uses
  168. the library's memory system */
  169. void curl_free(void *p)
  170. {
  171. free(p);
  172. }
  173. /*
  174. * Curl_hexencode()
  175. *
  176. * Converts binary input to lowercase hex-encoded ASCII output.
  177. * Null-terminated.
  178. */
  179. void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
  180. unsigned char *out, size_t olen) /* output buffer size */
  181. {
  182. DEBUGASSERT(src && len && (olen >= 3));
  183. if(src && len && (olen >= 3)) {
  184. while(len-- && (olen >= 3)) {
  185. out[0] = Curl_ldigits[*src >> 4];
  186. out[1] = Curl_ldigits[*src & 0x0F];
  187. ++src;
  188. out += 2;
  189. olen -= 2;
  190. }
  191. *out = 0;
  192. }
  193. else if(olen)
  194. *out = 0;
  195. }
  196. /* Curl_hexbyte
  197. *
  198. * Output a single unsigned char as a two-digit UPPERCASE hex number.
  199. */
  200. void Curl_hexbyte(unsigned char *dest, /* must fit two bytes */
  201. unsigned char val)
  202. {
  203. dest[0] = Curl_udigits[val >> 4];
  204. dest[1] = Curl_udigits[val & 0x0F];
  205. }