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