2002-09-03 19:52:59 +08:00
|
|
|
/***************************************************************************
|
2004-06-24 22:35:45 +08:00
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
1999-12-29 22:20:26 +08:00
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2023-01-02 20:51:48 +08:00
|
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2002-09-03 19:52:59 +08:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 21:02:01 +08:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2004-06-24 22:35:45 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
2002-09-03 19:52:59 +08:00
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2001-01-03 17:29:33 +08:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
1999-12-29 22:20:26 +08:00
|
|
|
*
|
2002-09-03 19:52:59 +08:00
|
|
|
* SPDX-License-Identifier: curl
|
2022-05-17 17:16:50 +08:00
|
|
|
*
|
2002-09-03 19:52:59 +08:00
|
|
|
***************************************************************************/
|
1999-12-29 22:20:26 +08:00
|
|
|
|
|
|
|
/* Escape and unescape URL encoding in strings. The functions return a new
|
|
|
|
* allocated string or NULL if an error occurred. */
|
|
|
|
|
2013-01-07 02:06:49 +08:00
|
|
|
#include "curl_setup.h"
|
2011-07-26 23:23:27 +08:00
|
|
|
|
2000-08-31 20:03:04 +08:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "warnless.h"
|
|
|
|
#include "escape.h"
|
2016-11-07 17:55:25 +08:00
|
|
|
#include "strdup.h"
|
2016-04-29 21:46:40 +08:00
|
|
|
/* The last 3 #include files should be in this order */
|
2015-03-03 19:36:18 +08:00
|
|
|
#include "curl_printf.h"
|
2015-03-25 06:12:03 +08:00
|
|
|
#include "curl_memory.h"
|
2013-01-04 09:50:28 +08:00
|
|
|
#include "memdebug.h"
|
2000-10-09 19:12:34 +08:00
|
|
|
|
2006-04-08 05:50:47 +08:00
|
|
|
/* for ABI-compatibility with previous versions */
|
2004-07-01 16:10:21 +08:00
|
|
|
char *curl_escape(const char *string, int inlength)
|
2006-04-08 05:50:47 +08:00
|
|
|
{
|
|
|
|
return curl_easy_escape(NULL, string, inlength);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* for ABI-compatibility with previous versions */
|
|
|
|
char *curl_unescape(const char *string, int length)
|
|
|
|
{
|
|
|
|
return curl_easy_unescape(NULL, string, length, NULL);
|
|
|
|
}
|
|
|
|
|
2022-07-08 14:04:35 +08:00
|
|
|
/* Escapes for URL the given unescaped string of given length.
|
|
|
|
* 'data' is ignored since 7.82.0.
|
|
|
|
*/
|
2016-06-22 01:31:24 +08:00
|
|
|
char *curl_easy_escape(struct Curl_easy *data, const char *string,
|
|
|
|
int inlength)
|
1999-12-29 22:20:26 +08:00
|
|
|
{
|
2004-07-01 16:10:21 +08:00
|
|
|
size_t length;
|
2020-05-02 23:04:08 +08:00
|
|
|
struct dynbuf d;
|
2022-02-03 20:04:30 +08:00
|
|
|
(void)data;
|
1999-12-29 22:20:26 +08:00
|
|
|
|
2016-09-09 04:59:54 +08:00
|
|
|
if(inlength < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2020-11-09 23:24:13 +08:00
|
|
|
Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH * 3);
|
2004-05-12 21:04:30 +08:00
|
|
|
|
2020-05-02 23:04:08 +08:00
|
|
|
length = (inlength?(size_t)inlength:strlen(string));
|
2020-06-24 21:23:19 +08:00
|
|
|
if(!length)
|
|
|
|
return strdup("");
|
|
|
|
|
2001-03-22 19:40:58 +08:00
|
|
|
while(length--) {
|
2023-01-31 19:34:08 +08:00
|
|
|
unsigned char in = *string++; /* treat the characters unsigned */
|
2007-08-05 04:47:59 +08:00
|
|
|
|
2023-09-14 02:41:51 +08:00
|
|
|
if(ISUNRESERVED(in)) {
|
2020-05-02 23:04:08 +08:00
|
|
|
/* append this */
|
|
|
|
if(Curl_dyn_addn(&d, &in, 1))
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-23 07:21:39 +08:00
|
|
|
else {
|
2001-03-22 19:40:58 +08:00
|
|
|
/* encode it */
|
2023-01-31 19:34:08 +08:00
|
|
|
const char hex[] = "0123456789ABCDEF";
|
|
|
|
char out[3]={'%'};
|
|
|
|
out[1] = hex[in>>4];
|
|
|
|
out[2] = hex[in & 0xf];
|
|
|
|
if(Curl_dyn_addn(&d, out, 3))
|
2020-05-02 23:04:08 +08:00
|
|
|
return NULL;
|
2001-03-22 19:40:58 +08:00
|
|
|
}
|
|
|
|
}
|
2020-05-02 23:04:08 +08:00
|
|
|
|
|
|
|
return Curl_dyn_ptr(&d);
|
1999-12-29 22:20:26 +08:00
|
|
|
}
|
|
|
|
|
2023-01-31 16:36:07 +08:00
|
|
|
static const unsigned char hextable[] = {
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */
|
|
|
|
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
|
|
|
|
0, 10, 11, 12, 13, 14, 15 /* 0x60 - 0x66 */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* the input is a single hex digit */
|
|
|
|
#define onehex2dec(x) hextable[x - '0']
|
|
|
|
|
2008-10-08 09:17:51 +08:00
|
|
|
/*
|
2011-12-23 20:24:16 +08:00
|
|
|
* Curl_urldecode() URL decodes the given string.
|
|
|
|
*
|
|
|
|
* Returns a pointer to a malloced string in *ostring with length given in
|
|
|
|
* *olen. If length == 0, the length is assumed to be strlen(string).
|
|
|
|
*
|
2020-06-23 22:13:50 +08:00
|
|
|
* ctrl options:
|
|
|
|
* - REJECT_NADA: accept everything
|
|
|
|
* - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
|
|
|
|
* the data
|
|
|
|
* - REJECT_ZERO: rejects decoded zero bytes
|
|
|
|
*
|
|
|
|
* The values for the enum starts at 2, to make the assert detect legacy
|
|
|
|
* invokes that used TRUE/FALSE (0 and 1).
|
2008-10-08 09:17:51 +08:00
|
|
|
*/
|
2020-06-23 22:13:50 +08:00
|
|
|
|
2022-02-03 20:04:30 +08:00
|
|
|
CURLcode Curl_urldecode(const char *string, size_t length,
|
2011-12-23 20:24:16 +08:00
|
|
|
char **ostring, size_t *olen,
|
2020-06-23 22:13:50 +08:00
|
|
|
enum urlreject ctrl)
|
1999-12-29 22:20:26 +08:00
|
|
|
{
|
2020-05-25 21:38:36 +08:00
|
|
|
size_t alloc;
|
|
|
|
char *ns;
|
2004-06-24 22:35:45 +08:00
|
|
|
|
2020-05-25 21:38:36 +08:00
|
|
|
DEBUGASSERT(string);
|
2020-06-23 22:13:50 +08:00
|
|
|
DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
|
2020-05-25 21:38:36 +08:00
|
|
|
|
2023-01-31 16:36:07 +08:00
|
|
|
alloc = (length?length:strlen(string));
|
|
|
|
ns = malloc(alloc + 1);
|
2020-05-25 21:38:36 +08:00
|
|
|
|
2011-04-20 06:48:20 +08:00
|
|
|
if(!ns)
|
2011-12-23 20:24:16 +08:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2004-06-24 22:35:45 +08:00
|
|
|
|
2023-01-31 16:36:07 +08:00
|
|
|
/* store output string */
|
|
|
|
*ostring = ns;
|
|
|
|
|
|
|
|
while(alloc) {
|
2018-06-03 04:52:56 +08:00
|
|
|
unsigned char in = *string;
|
2013-05-20 05:24:29 +08:00
|
|
|
if(('%' == in) && (alloc > 2) &&
|
|
|
|
ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
|
2003-05-21 23:53:59 +08:00
|
|
|
/* this is two hexadecimal digits following a '%' */
|
2023-01-31 16:36:07 +08:00
|
|
|
in = (unsigned char)(onehex2dec(string[1]) << 4) | onehex2dec(string[2]);
|
2006-04-08 05:50:47 +08:00
|
|
|
|
2023-01-31 16:36:07 +08:00
|
|
|
string += 3;
|
|
|
|
alloc -= 3;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
string++;
|
|
|
|
alloc--;
|
2001-03-22 19:40:58 +08:00
|
|
|
}
|
2014-10-29 06:42:42 +08:00
|
|
|
|
2020-06-23 22:13:50 +08:00
|
|
|
if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
|
|
|
|
((ctrl == REJECT_ZERO) && (in == 0))) {
|
2023-01-31 16:36:07 +08:00
|
|
|
Curl_safefree(*ostring);
|
2011-12-23 20:24:16 +08:00
|
|
|
return CURLE_URL_MALFORMAT;
|
|
|
|
}
|
2004-06-24 22:35:45 +08:00
|
|
|
|
2023-01-31 16:36:07 +08:00
|
|
|
*ns++ = in;
|
2001-03-22 19:40:58 +08:00
|
|
|
}
|
2023-01-31 16:36:07 +08:00
|
|
|
*ns = 0; /* terminate it */
|
2006-04-08 05:50:47 +08:00
|
|
|
|
|
|
|
if(olen)
|
|
|
|
/* store output size */
|
2023-01-31 16:36:07 +08:00
|
|
|
*olen = ns - *ostring;
|
2011-12-23 20:24:16 +08:00
|
|
|
|
|
|
|
return CURLE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unescapes the given URL escaped string of given length. Returns a
|
|
|
|
* pointer to a malloced string with length given in *olen.
|
|
|
|
* If length == 0, the length is assumed to be strlen(string).
|
|
|
|
* If olen == NULL, no output length is stored.
|
2022-07-08 14:04:35 +08:00
|
|
|
* 'data' is ignored since 7.82.0.
|
2011-12-23 20:24:16 +08:00
|
|
|
*/
|
2016-06-22 01:31:24 +08:00
|
|
|
char *curl_easy_unescape(struct Curl_easy *data, const char *string,
|
|
|
|
int length, int *olen)
|
2011-12-23 20:24:16 +08:00
|
|
|
{
|
|
|
|
char *str = NULL;
|
2022-02-03 20:04:30 +08:00
|
|
|
(void)data;
|
2016-09-14 05:00:50 +08:00
|
|
|
if(length >= 0) {
|
2022-10-29 00:32:09 +08:00
|
|
|
size_t inputlen = (size_t)length;
|
2016-09-14 05:00:50 +08:00
|
|
|
size_t outputlen;
|
2022-02-03 20:04:30 +08:00
|
|
|
CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
|
2020-06-23 22:13:50 +08:00
|
|
|
REJECT_NADA);
|
2016-09-14 05:00:50 +08:00
|
|
|
if(res)
|
|
|
|
return NULL;
|
2016-10-05 00:56:45 +08:00
|
|
|
|
|
|
|
if(olen) {
|
|
|
|
if(outputlen <= (size_t) INT_MAX)
|
|
|
|
*olen = curlx_uztosi(outputlen);
|
|
|
|
else
|
|
|
|
/* too large to return in an int, fail! */
|
|
|
|
Curl_safefree(str);
|
|
|
|
}
|
2016-09-14 05:00:50 +08:00
|
|
|
}
|
2011-12-23 20:24:16 +08:00
|
|
|
return str;
|
1999-12-29 22:20:26 +08:00
|
|
|
}
|
2001-09-07 12:01:32 +08:00
|
|
|
|
2003-05-21 23:53:59 +08:00
|
|
|
/* For operating systems/environments that use different malloc/free
|
2008-09-10 05:15:50 +08:00
|
|
|
systems for the app and for this library, we provide a free that uses
|
2003-05-21 23:53:59 +08:00
|
|
|
the library's memory system */
|
2002-09-25 20:26:07 +08:00
|
|
|
void curl_free(void *p)
|
|
|
|
{
|
2015-03-12 00:41:01 +08:00
|
|
|
free(p);
|
2002-09-25 20:26:07 +08:00
|
|
|
}
|
2023-09-30 00:06:49 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_hexencode()
|
|
|
|
*
|
|
|
|
* Converts binary input to lowercase hex-encoded ASCII output.
|
|
|
|
* Null-terminated.
|
|
|
|
*/
|
|
|
|
void Curl_hexencode(const unsigned char *src, size_t len, /* input length */
|
|
|
|
unsigned char *out, size_t olen) /* output buffer size */
|
|
|
|
{
|
|
|
|
const char *hex = "0123456789abcdef";
|
|
|
|
DEBUGASSERT(src && len && (olen >= 3));
|
|
|
|
if(src && len && (olen >= 3)) {
|
|
|
|
while(len-- && (olen >= 3)) {
|
|
|
|
/* clang-tidy warns on this line without this comment: */
|
|
|
|
/* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */
|
|
|
|
*out++ = hex[(*src & 0xF0)>>4];
|
|
|
|
*out++ = hex[*src & 0x0F];
|
|
|
|
++src;
|
|
|
|
olen -= 2;
|
|
|
|
}
|
|
|
|
*out = 0;
|
|
|
|
}
|
|
|
|
else if(olen)
|
|
|
|
*out = 0;
|
|
|
|
}
|