1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, 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 ***************************************************************************/
22
23/* Escape and unescape URL encoding in strings. The functions return a new
24 * allocated string or NULL if an error occurred. */
25
26#include "curl_setup.h"
27
28#include <curl/curl.h>
29
30#include "urldata.h"
31#include "warnless.h"
32#include "non-ascii.h"
33#include "escape.h"
34#include "strdup.h"
35/* The last 3 #include files should be in this order */
36#include "curl_printf.h"
37#include "curl_memory.h"
38#include "memdebug.h"
39
40/* Portable character check (remember EBCDIC). Do not use isalnum() because
41 its behavior is altered by the current locale.
42 See https://tools.ietf.org/html/rfc3986#section-2.3
43*/
44bool Curl_isunreserved(unsigned char in)
45{
46 switch(in) {
47 case '0': case '1': case '2': case '3': case '4':
48 case '5': case '6': case '7': case '8': case '9':
49 case 'a': case 'b': case 'c': case 'd': case 'e':
50 case 'f': case 'g': case 'h': case 'i': case 'j':
51 case 'k': case 'l': case 'm': case 'n': case 'o':
52 case 'p': case 'q': case 'r': case 's': case 't':
53 case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
54 case 'A': case 'B': case 'C': case 'D': case 'E':
55 case 'F': case 'G': case 'H': case 'I': case 'J':
56 case 'K': case 'L': case 'M': case 'N': case 'O':
57 case 'P': case 'Q': case 'R': case 'S': case 'T':
58 case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
59 case '-': case '.': case '_': case '~':
60 return TRUE;
61 default:
62 break;
63 }
64 return FALSE;
65}
66
67/* for ABI-compatibility with previous versions */
68char *curl_escape(const char *string, int inlength)
69{
70 return curl_easy_escape(NULL, string, inlength);
71}
72
73/* for ABI-compatibility with previous versions */
74char *curl_unescape(const char *string, int length)
75{
76 return curl_easy_unescape(NULL, string, length, NULL);
77}
78
79char *curl_easy_escape(struct Curl_easy *data, const char *string,
80 int inlength)
81{
82 size_t length;
83 CURLcode result;
84 struct dynbuf d;
85
86 if(inlength < 0)
87 return NULL;
88
89 Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH * 3);
90
91 length = (inlength?(size_t)inlength:strlen(string));
92 if(!length)
93 return strdup("");
94
95 while(length--) {
96 unsigned char in = *string; /* we need to treat the characters unsigned */
97
98 if(Curl_isunreserved(in)) {
99 /* append this */
100 if(Curl_dyn_addn(&d, &in, 1))
101 return NULL;
102 }
103 else {
104 /* encode it */
105 char encoded[4];
106 result = Curl_convert_to_network(data, (char *)&in, 1);
107 if(result) {
108 /* Curl_convert_to_network calls failf if unsuccessful */
109 Curl_dyn_free(&d);
110 return NULL;
111 }
112
113 msnprintf(encoded, sizeof(encoded), "%%%02X", in);
114 if(Curl_dyn_add(&d, encoded))
115 return NULL;
116 }
117 string++;
118 }
119
120 return Curl_dyn_ptr(&d);
121}
122
123/*
124 * Curl_urldecode() URL decodes the given string.
125 *
126 * Returns a pointer to a malloced string in *ostring with length given in
127 * *olen. If length == 0, the length is assumed to be strlen(string).
128 *
129 * 'data' can be set to NULL but then this function can't convert network
130 * data to host for non-ascii.
131 *
132 * ctrl options:
133 * - REJECT_NADA: accept everything
134 * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in
135 * the data
136 * - REJECT_ZERO: rejects decoded zero bytes
137 *
138 * The values for the enum starts at 2, to make the assert detect legacy
139 * invokes that used TRUE/FALSE (0 and 1).
140 */
141
142CURLcode Curl_urldecode(struct Curl_easy *data,
143 const char *string, size_t length,
144 char **ostring, size_t *olen,
145 enum urlreject ctrl)
146{
147 size_t alloc;
148 char *ns;
149 size_t strindex = 0;
150 unsigned long hex;
151 CURLcode result = CURLE_OK;
152
153 DEBUGASSERT(string);
154 DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */
155
156 alloc = (length?length:strlen(string)) + 1;
157 ns = malloc(alloc);
158
159 if(!ns)
160 return CURLE_OUT_OF_MEMORY;
161
162 while(--alloc > 0) {
163 unsigned char in = *string;
164 if(('%' == in) && (alloc > 2) &&
165 ISXDIGIT(string[1]) && ISXDIGIT(string[2])) {
166 /* this is two hexadecimal digits following a '%' */
167 char hexstr[3];
168 char *ptr;
169 hexstr[0] = string[1];
170 hexstr[1] = string[2];
171 hexstr[2] = 0;
172
173 hex = strtoul(hexstr, &ptr, 16);
174
175 in = curlx_ultouc(hex); /* this long is never bigger than 255 anyway */
176
177 if(data) {
178 result = Curl_convert_from_network(data, (char *)&in, 1);
179 if(result) {
180 /* Curl_convert_from_network calls failf if unsuccessful */
181 free(ns);
182 return result;
183 }
184 }
185
186 string += 2;
187 alloc -= 2;
188 }
189
190 if(((ctrl == REJECT_CTRL) && (in < 0x20)) ||
191 ((ctrl == REJECT_ZERO) && (in == 0))) {
192 free(ns);
193 return CURLE_URL_MALFORMAT;
194 }
195
196 ns[strindex++] = in;
197 string++;
198 }
199 ns[strindex] = 0; /* terminate it */
200
201 if(olen)
202 /* store output size */
203 *olen = strindex;
204
205 /* store output string */
206 *ostring = ns;
207
208 return CURLE_OK;
209}
210
211/*
212 * Unescapes the given URL escaped string of given length. Returns a
213 * pointer to a malloced string with length given in *olen.
214 * If length == 0, the length is assumed to be strlen(string).
215 * If olen == NULL, no output length is stored.
216 */
217char *curl_easy_unescape(struct Curl_easy *data, const char *string,
218 int length, int *olen)
219{
220 char *str = NULL;
221 if(length >= 0) {
222 size_t inputlen = length;
223 size_t outputlen;
224 CURLcode res = Curl_urldecode(data, string, inputlen, &str, &outputlen,
225 REJECT_NADA);
226 if(res)
227 return NULL;
228
229 if(olen) {
230 if(outputlen <= (size_t) INT_MAX)
231 *olen = curlx_uztosi(outputlen);
232 else
233 /* too large to return in an int, fail! */
234 Curl_safefree(str);
235 }
236 }
237 return str;
238}
239
240/* For operating systems/environments that use different malloc/free
241 systems for the app and for this library, we provide a free that uses
242 the library's memory system */
243void curl_free(void *p)
244{
245 free(p);
246}
247