escape.c 6.5 KB

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