2
0

escape.c 6.5 KB

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