Line | Count | Source |
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 | | |
28 | | struct Curl_easy; |
29 | | |
30 | | #include "urldata.h" |
31 | | #include "escape.h" |
32 | | #include "curlx/strparse.h" |
33 | | #include "curl_printf.h" |
34 | | |
35 | | /* for ABI-compatibility with previous versions */ |
36 | | char *curl_escape(const char *string, int length) |
37 | 0 | { |
38 | 0 | return curl_easy_escape(NULL, string, length); |
39 | 0 | } |
40 | | |
41 | | /* for ABI-compatibility with previous versions */ |
42 | | char *curl_unescape(const char *string, int length) |
43 | 0 | { |
44 | 0 | return curl_easy_unescape(NULL, string, length, NULL); |
45 | 0 | } |
46 | | |
47 | | /* Escapes for URL the given unescaped string of given length. |
48 | | * 'data' is ignored since 7.82.0. |
49 | | */ |
50 | | char *curl_easy_escape(CURL *curl, const char *string, int length) |
51 | 14.5k | { |
52 | 14.5k | size_t len; |
53 | 14.5k | struct dynbuf d; |
54 | 14.5k | (void)curl; |
55 | | |
56 | 14.5k | if(!string || (length < 0)) |
57 | 0 | return NULL; |
58 | | |
59 | 14.5k | len = (length ? (size_t)length : strlen(string)); |
60 | 14.5k | if(!len) |
61 | 0 | return curlx_strdup(""); |
62 | | |
63 | 14.5k | if(len > SIZE_MAX / 16) |
64 | 0 | return NULL; |
65 | | |
66 | 14.5k | curlx_dyn_init(&d, (len * 3) + 1); |
67 | | |
68 | 184k | while(len--) { |
69 | | /* treat the characters unsigned */ |
70 | 170k | unsigned char in = (unsigned char)*string++; |
71 | | |
72 | 170k | if(ISUNRESERVED(in)) { |
73 | | /* append this */ |
74 | 170k | if(curlx_dyn_addn(&d, &in, 1)) |
75 | 0 | return NULL; |
76 | 170k | } |
77 | 4 | else { |
78 | | /* encode it */ |
79 | 4 | unsigned char out[3] = { '%' }; |
80 | 4 | Curl_hexbyte(&out[1], in); |
81 | 4 | if(curlx_dyn_addn(&d, out, 3)) |
82 | 0 | return NULL; |
83 | 4 | } |
84 | 170k | } |
85 | | |
86 | 14.5k | return curlx_dyn_ptr(&d); |
87 | 14.5k | } |
88 | | |
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 | | |
105 | | CURLcode Curl_urldecode(const char *string, size_t length, |
106 | | char **ostring, size_t *olen, |
107 | | enum urlreject ctrl) |
108 | 96.0k | { |
109 | 96.0k | size_t alloc; |
110 | 96.0k | char *ns; |
111 | | |
112 | 96.0k | DEBUGASSERT(string); |
113 | 96.0k | DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */ |
114 | | |
115 | 96.0k | alloc = (length ? length : strlen(string)); |
116 | 96.0k | ns = curlx_malloc(alloc + 1); |
117 | | |
118 | 96.0k | if(!ns) |
119 | 0 | return CURLE_OUT_OF_MEMORY; |
120 | | |
121 | | /* store output string */ |
122 | 96.0k | *ostring = ns; |
123 | | |
124 | 171M | while(alloc) { |
125 | 171M | unsigned char in = (unsigned char)*string; |
126 | 171M | if(('%' == in) && (alloc > 2) && |
127 | 82.9M | ISXDIGIT(string[1]) && ISXDIGIT(string[2])) { |
128 | | /* this is two hexadecimal digits following a '%' */ |
129 | 40.6M | in = (unsigned char)((curlx_hexval(string[1]) << 4) | |
130 | 40.6M | curlx_hexval(string[2])); |
131 | 40.6M | string += 3; |
132 | 40.6M | alloc -= 3; |
133 | 40.6M | } |
134 | 130M | else { |
135 | 130M | string++; |
136 | 130M | alloc--; |
137 | 130M | } |
138 | | |
139 | 171M | if(((ctrl == REJECT_CTRL) && (in < 0x20)) || |
140 | 171M | ((ctrl == REJECT_ZERO) && (in == 0))) { |
141 | 286 | curlx_safefree(*ostring); |
142 | 286 | return CURLE_URL_MALFORMAT; |
143 | 286 | } |
144 | | |
145 | 171M | *ns++ = (char)in; |
146 | 171M | } |
147 | 95.7k | *ns = 0; /* terminate it */ |
148 | | |
149 | 95.7k | if(olen) |
150 | | /* store output size */ |
151 | 62.0k | *olen = ns - *ostring; |
152 | | |
153 | 95.7k | return CURLE_OK; |
154 | 96.0k | } |
155 | | |
156 | | /* |
157 | | * Unescapes the given URL escaped string of given length. Returns a |
158 | | * pointer to a malloced string with length given in *olen. |
159 | | * If length == 0, the length is assumed to be strlen(string). |
160 | | * If olen == NULL, no output length is stored. |
161 | | * 'data' is ignored since 7.82.0. |
162 | | */ |
163 | | char *curl_easy_unescape(CURL *curl, const char *string, int inlength, |
164 | | int *outlength) |
165 | 0 | { |
166 | 0 | char *str = NULL; |
167 | 0 | (void)curl; |
168 | 0 | if(string && (inlength >= 0)) { |
169 | 0 | size_t inputlen = (size_t)inlength; |
170 | 0 | size_t outputlen; |
171 | 0 | CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen, |
172 | 0 | REJECT_NADA); |
173 | 0 | if(res) |
174 | 0 | return NULL; |
175 | | |
176 | 0 | if(outlength) { |
177 | 0 | if(outputlen <= (size_t)INT_MAX) |
178 | 0 | *outlength = curlx_uztosi(outputlen); |
179 | 0 | else |
180 | | /* too large to return in an int, fail! */ |
181 | 0 | curlx_safefree(str); |
182 | 0 | } |
183 | 0 | } |
184 | 0 | return str; |
185 | 0 | } |
186 | | |
187 | | /* For operating systems/environments that use different malloc/free |
188 | | systems for the app and for this library, we provide a free that uses |
189 | | the library's memory system */ |
190 | | void curl_free(void *p) |
191 | 180k | { |
192 | 180k | curlx_free(p); |
193 | 180k | } |
194 | | |
195 | | /* |
196 | | * Curl_hexencode() |
197 | | * |
198 | | * Converts binary input to lowercase hex-encoded ASCII output. |
199 | | * Null-terminated. |
200 | | */ |
201 | | void Curl_hexencode(const unsigned char *src, size_t len, /* input length */ |
202 | | unsigned char *out, size_t olen) /* output buffer size */ |
203 | 38.5k | { |
204 | 38.5k | DEBUGASSERT(src && len && (olen >= 3)); |
205 | 38.5k | if(src && len && (olen >= 3)) { |
206 | 1.27M | while(len-- && (olen >= 3)) { |
207 | 1.23M | out[0] = Curl_ldigits[*src >> 4]; |
208 | 1.23M | out[1] = Curl_ldigits[*src & 0x0F]; |
209 | 1.23M | ++src; |
210 | 1.23M | out += 2; |
211 | 1.23M | olen -= 2; |
212 | 1.23M | } |
213 | 38.5k | *out = 0; |
214 | 38.5k | } |
215 | 0 | else if(olen) |
216 | 0 | *out = 0; |
217 | 38.5k | } |
218 | | |
219 | | /* Curl_hexbyte |
220 | | * |
221 | | * Output a single unsigned char as a two-digit UPPERCASE hex number. |
222 | | */ |
223 | | void Curl_hexbyte(unsigned char *dest, /* must fit two bytes */ |
224 | | unsigned char val) |
225 | 62.8M | { |
226 | 62.8M | dest[0] = Curl_udigits[val >> 4]; |
227 | 62.8M | dest[1] = Curl_udigits[val & 0x0F]; |
228 | 62.8M | } |