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