1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* 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
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
/* Escape and unescape URL encoding in strings. The functions return a new
* allocated string or NULL if an error occurred. */
#include "curl_setup.h"
#include <curl/curl.h>
struct Curl_easy;
#include "urldata.h"
#include "curlx/warnless.h"
#include "escape.h"
#include "strdup.h"
#include "curlx/strparse.h"
/* The last 3 #include files should be in this order */
#include "curl_printf.h"
#include "curl_memory.h"
#include "memdebug.h"
/* for ABI-compatibility with previous versions */
char *curl_escape(const char *string, int inlength)
{
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);
}
/* Escapes for URL the given unescaped string of given length.
* 'data' is ignored since 7.82.0.
*/
char *curl_easy_escape(CURL *data, const char *string,
int inlength)
{
size_t length;
struct dynbuf d;
(void)data;
if(!string || (inlength < 0))
return NULL;
length = (inlength ? (size_t)inlength : strlen(string));
if(!length)
return strdup("");
curlx_dyn_init(&d, length * 3 + 1);
while(length--) {
/* treat the characters unsigned */
unsigned char in = (unsigned char)*string++;
if(ISUNRESERVED(in)) {
/* append this */
if(curlx_dyn_addn(&d, &in, 1))
return NULL;
}
else {
/* encode it */
unsigned char out[3]={'%'};
Curl_hexbyte(&out[1], in, FALSE);
if(curlx_dyn_addn(&d, out, 3))
return NULL;
}
}
return curlx_dyn_ptr(&d);
}
/*
* 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).
*
* 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).
*/
CURLcode Curl_urldecode(const char *string, size_t length,
char **ostring, size_t *olen,
enum urlreject ctrl)
{
size_t alloc;
char *ns;
DEBUGASSERT(string);
DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
alloc = (length ? length : strlen(string));
ns = malloc(alloc + 1);
if(!ns)
return CURLE_OUT_OF_MEMORY;
/* store output string */
*ostring = ns;
while(alloc) {
unsigned char in = (unsigned char)*string;
if(('%' == in) && (alloc > 2) &&
ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
/* this is two hexadecimal digits following a '%' */
in = (unsigned char)((Curl_hexval(string[1]) << 4) |
Curl_hexval(string[2]));
string += 3;
alloc -= 3;
}
else {
string++;
alloc--;
}
if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
((ctrl == REJECT_ZERO) && (in == 0))) {
Curl_safefree(*ostring);
return CURLE_URL_MALFORMAT;
}
*ns++ = (char)in;
}
*ns = 0; /* terminate it */
if(olen)
/* store output size */
*olen = ns - *ostring;
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.
* 'data' is ignored since 7.82.0.
*/
char *curl_easy_unescape(CURL *data, const char *string,
int length, int *olen)
{
char *str = NULL;
(void)data;
if(string && (length >= 0)) {
size_t inputlen = (size_t)length;
size_t outputlen;
CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen,
REJECT_NADA);
if(res)
return NULL;
if(olen) {
if(outputlen <= (size_t) INT_MAX)
*olen = curlx_uztosi(outputlen);
else
/* too large to return in an int, fail! */
Curl_safefree(str);
}
}
return str;
}
/* For operating systems/environments that use different malloc/free
systems for the app and for this library, we provide a free that uses
the library's memory system */
void curl_free(void *p)
{
free(p);
}
/*
* 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 */
{
DEBUGASSERT(src && len && (olen >= 3));
if(src && len && (olen >= 3)) {
while(len-- && (olen >= 3)) {
Curl_hexbyte(out, *src, TRUE);
++src;
out += 2;
olen -= 2;
}
*out = 0;
}
else if(olen)
*out = 0;
}
/* Curl_hexbyte
*
* Output a single unsigned char as a two-digit hex number, lowercase or
* uppercase
*/
void Curl_hexbyte(unsigned char *dest, /* must fit two bytes */
unsigned char val,
bool lowercase)
{
const unsigned char *t = lowercase ? Curl_ldigits : Curl_udigits;
dest[0] = t[val >> 4];
dest[1] = t[val & 0x0F];
}
|