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 inlength) |
37 | 0 | { |
38 | 0 | return curl_easy_escape(NULL, string, inlength); |
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 *data, const char *string, int inlength) |
51 | 15.3k | { |
52 | 15.3k | size_t length; |
53 | 15.3k | struct dynbuf d; |
54 | 15.3k | (void)data; |
55 | | |
56 | 15.3k | if(!string || (inlength < 0)) |
57 | 0 | return NULL; |
58 | | |
59 | 15.3k | length = (inlength ? (size_t)inlength : strlen(string)); |
60 | 15.3k | if(!length) |
61 | 0 | return curlx_strdup(""); |
62 | | |
63 | 15.3k | if(length > SIZE_MAX / 16) |
64 | 0 | return NULL; |
65 | | |
66 | 15.3k | curlx_dyn_init(&d, length * 3 + 1); |
67 | | |
68 | 89.7k | while(length--) { |
69 | | /* treat the characters unsigned */ |
70 | 74.3k | unsigned char in = (unsigned char)*string++; |
71 | | |
72 | 74.3k | if(ISUNRESERVED(in)) { |
73 | | /* append this */ |
74 | 73.9k | if(curlx_dyn_addn(&d, &in, 1)) |
75 | 0 | return NULL; |
76 | 73.9k | } |
77 | 356 | else { |
78 | | /* encode it */ |
79 | 356 | unsigned char out[3] = { '%' }; |
80 | 356 | Curl_hexbyte(&out[1], in); |
81 | 356 | if(curlx_dyn_addn(&d, out, 3)) |
82 | 0 | return NULL; |
83 | 356 | } |
84 | 74.3k | } |
85 | | |
86 | 15.3k | return curlx_dyn_ptr(&d); |
87 | 15.3k | } |
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 | 174k | { |
109 | 174k | size_t alloc; |
110 | 174k | char *ns; |
111 | | |
112 | 174k | DEBUGASSERT(string); |
113 | 174k | DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */ |
114 | | |
115 | 174k | alloc = (length ? length : strlen(string)); |
116 | 174k | ns = curlx_malloc(alloc + 1); |
117 | | |
118 | 174k | if(!ns) |
119 | 0 | return CURLE_OUT_OF_MEMORY; |
120 | | |
121 | | /* store output string */ |
122 | 174k | *ostring = ns; |
123 | | |
124 | 169M | while(alloc) { |
125 | 169M | unsigned char in = (unsigned char)*string; |
126 | 169M | if(('%' == in) && (alloc > 2) && |
127 | 66.4M | ISXDIGIT(string[1]) && ISXDIGIT(string[2])) { |
128 | | /* this is two hexadecimal digits following a '%' */ |
129 | 19.4M | in = (unsigned char)((curlx_hexval(string[1]) << 4) | |
130 | 19.4M | curlx_hexval(string[2])); |
131 | 19.4M | string += 3; |
132 | 19.4M | alloc -= 3; |
133 | 19.4M | } |
134 | 149M | else { |
135 | 149M | string++; |
136 | 149M | alloc--; |
137 | 149M | } |
138 | | |
139 | 169M | if(((ctrl == REJECT_CTRL) && (in < 0x20)) || |
140 | 169M | ((ctrl == REJECT_ZERO) && (in == 0))) { |
141 | 283 | Curl_safefree(*ostring); |
142 | 283 | return CURLE_URL_MALFORMAT; |
143 | 283 | } |
144 | | |
145 | 169M | *ns++ = (char)in; |
146 | 169M | } |
147 | 173k | *ns = 0; /* terminate it */ |
148 | | |
149 | 173k | if(olen) |
150 | | /* store output size */ |
151 | 132k | *olen = ns - *ostring; |
152 | | |
153 | 173k | return CURLE_OK; |
154 | 174k | } |
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 *data, const char *string, int length, int *olen) |
164 | 0 | { |
165 | 0 | char *str = NULL; |
166 | 0 | (void)data; |
167 | 0 | if(string && (length >= 0)) { |
168 | 0 | size_t inputlen = (size_t)length; |
169 | 0 | size_t outputlen; |
170 | 0 | CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen, |
171 | 0 | REJECT_NADA); |
172 | 0 | if(res) |
173 | 0 | return NULL; |
174 | | |
175 | 0 | if(olen) { |
176 | 0 | if(outputlen <= (size_t)INT_MAX) |
177 | 0 | *olen = curlx_uztosi(outputlen); |
178 | 0 | else |
179 | | /* too large to return in an int, fail! */ |
180 | 0 | Curl_safefree(str); |
181 | 0 | } |
182 | 0 | } |
183 | 0 | return str; |
184 | 0 | } |
185 | | |
186 | | /* For operating systems/environments that use different malloc/free |
187 | | systems for the app and for this library, we provide a free that uses |
188 | | the library's memory system */ |
189 | | void curl_free(void *p) |
190 | 189k | { |
191 | 189k | curlx_free(p); |
192 | 189k | } |
193 | | |
194 | | /* |
195 | | * Curl_hexencode() |
196 | | * |
197 | | * Converts binary input to lowercase hex-encoded ASCII output. |
198 | | * Null-terminated. |
199 | | */ |
200 | | void Curl_hexencode(const unsigned char *src, size_t len, /* input length */ |
201 | | unsigned char *out, size_t olen) /* output buffer size */ |
202 | 32.1k | { |
203 | 32.1k | DEBUGASSERT(src && len && (olen >= 3)); |
204 | 32.1k | if(src && len && (olen >= 3)) { |
205 | 1.06M | while(len-- && (olen >= 3)) { |
206 | 1.02M | out[0] = Curl_ldigits[*src >> 4]; |
207 | 1.02M | out[1] = Curl_ldigits[*src & 0x0F]; |
208 | 1.02M | ++src; |
209 | 1.02M | out += 2; |
210 | 1.02M | olen -= 2; |
211 | 1.02M | } |
212 | 32.1k | *out = 0; |
213 | 32.1k | } |
214 | 0 | else if(olen) |
215 | 0 | *out = 0; |
216 | 32.1k | } |
217 | | |
218 | | /* Curl_hexbyte |
219 | | * |
220 | | * Output a single unsigned char as a two-digit UPPERCASE hex number. |
221 | | */ |
222 | | void Curl_hexbyte(unsigned char *dest, /* must fit two bytes */ |
223 | | unsigned char val) |
224 | 60.4M | { |
225 | 60.4M | dest[0] = Curl_udigits[val >> 4]; |
226 | 60.4M | dest[1] = Curl_udigits[val & 0x0F]; |
227 | 60.4M | } |