Line | Count | Source (jump to first uncovered line) |
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 | | |
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 | | /* for ABI-compatibility with previous versions */ |
42 | | char *curl_escape(const char *string, int inlength) |
43 | 0 | { |
44 | 0 | return curl_easy_escape(NULL, string, inlength); |
45 | 0 | } |
46 | | |
47 | | /* for ABI-compatibility with previous versions */ |
48 | | char *curl_unescape(const char *string, int length) |
49 | 0 | { |
50 | 0 | return curl_easy_unescape(NULL, string, length, NULL); |
51 | 0 | } |
52 | | |
53 | | /* Escapes for URL the given unescaped string of given length. |
54 | | * 'data' is ignored since 7.82.0. |
55 | | */ |
56 | | char *curl_easy_escape(struct Curl_easy *data, const char *string, |
57 | | int inlength) |
58 | 0 | { |
59 | 0 | size_t length; |
60 | 0 | struct dynbuf d; |
61 | 0 | (void)data; |
62 | |
|
63 | 0 | if(inlength < 0) |
64 | 0 | return NULL; |
65 | | |
66 | 0 | Curl_dyn_init(&d, CURL_MAX_INPUT_LENGTH * 3); |
67 | |
|
68 | 0 | length = (inlength?(size_t)inlength:strlen(string)); |
69 | 0 | if(!length) |
70 | 0 | return strdup(""); |
71 | | |
72 | 0 | while(length--) { |
73 | 0 | unsigned char in = *string++; /* treat the characters unsigned */ |
74 | |
|
75 | 0 | if(ISUNRESERVED(in)) { |
76 | | /* append this */ |
77 | 0 | if(Curl_dyn_addn(&d, &in, 1)) |
78 | 0 | return NULL; |
79 | 0 | } |
80 | 0 | else { |
81 | | /* encode it */ |
82 | 0 | const char hex[] = "0123456789ABCDEF"; |
83 | 0 | char out[3]={'%'}; |
84 | 0 | out[1] = hex[in>>4]; |
85 | 0 | out[2] = hex[in & 0xf]; |
86 | 0 | if(Curl_dyn_addn(&d, out, 3)) |
87 | 0 | return NULL; |
88 | 0 | } |
89 | 0 | } |
90 | | |
91 | 0 | return Curl_dyn_ptr(&d); |
92 | 0 | } |
93 | | |
94 | | static const unsigned char hextable[] = { |
95 | | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, /* 0x30 - 0x3f */ |
96 | | 0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x40 - 0x4f */ |
97 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */ |
98 | | 0, 10, 11, 12, 13, 14, 15 /* 0x60 - 0x66 */ |
99 | | }; |
100 | | |
101 | | /* the input is a single hex digit */ |
102 | 106k | #define onehex2dec(x) hextable[x - '0'] |
103 | | |
104 | | /* |
105 | | * Curl_urldecode() URL decodes the given string. |
106 | | * |
107 | | * Returns a pointer to a malloced string in *ostring with length given in |
108 | | * *olen. If length == 0, the length is assumed to be strlen(string). |
109 | | * |
110 | | * ctrl options: |
111 | | * - REJECT_NADA: accept everything |
112 | | * - REJECT_CTRL: rejects control characters (byte codes lower than 32) in |
113 | | * the data |
114 | | * - REJECT_ZERO: rejects decoded zero bytes |
115 | | * |
116 | | * The values for the enum starts at 2, to make the assert detect legacy |
117 | | * invokes that used TRUE/FALSE (0 and 1). |
118 | | */ |
119 | | |
120 | | CURLcode Curl_urldecode(const char *string, size_t length, |
121 | | char **ostring, size_t *olen, |
122 | | enum urlreject ctrl) |
123 | 248 | { |
124 | 248 | size_t alloc; |
125 | 248 | char *ns; |
126 | | |
127 | 248 | DEBUGASSERT(string); |
128 | 248 | DEBUGASSERT(ctrl >= REJECT_NADA); /* crash on TRUE/FALSE */ |
129 | | |
130 | 248 | alloc = (length?length:strlen(string)); |
131 | 248 | ns = malloc(alloc + 1); |
132 | | |
133 | 248 | if(!ns) |
134 | 0 | return CURLE_OUT_OF_MEMORY; |
135 | | |
136 | | /* store output string */ |
137 | 248 | *ostring = ns; |
138 | | |
139 | 16.1M | while(alloc) { |
140 | 16.1M | unsigned char in = *string; |
141 | 16.1M | if(('%' == in) && (alloc > 2) && |
142 | 16.1M | ISXDIGIT(string[1]) && ISXDIGIT(string[2])) { |
143 | | /* this is two hexadecimal digits following a '%' */ |
144 | 53.1k | in = (unsigned char)(onehex2dec(string[1]) << 4) | onehex2dec(string[2]); |
145 | | |
146 | 53.1k | string += 3; |
147 | 53.1k | alloc -= 3; |
148 | 53.1k | } |
149 | 16.0M | else { |
150 | 16.0M | string++; |
151 | 16.0M | alloc--; |
152 | 16.0M | } |
153 | | |
154 | 16.1M | if(((ctrl == REJECT_CTRL) && (in < 0x20)) || |
155 | 16.1M | ((ctrl == REJECT_ZERO) && (in == 0))) { |
156 | 14 | Curl_safefree(*ostring); |
157 | 14 | return CURLE_URL_MALFORMAT; |
158 | 14 | } |
159 | | |
160 | 16.1M | *ns++ = in; |
161 | 16.1M | } |
162 | 234 | *ns = 0; /* terminate it */ |
163 | | |
164 | 234 | if(olen) |
165 | | /* store output size */ |
166 | 234 | *olen = ns - *ostring; |
167 | | |
168 | 234 | return CURLE_OK; |
169 | 248 | } |
170 | | |
171 | | /* |
172 | | * Unescapes the given URL escaped string of given length. Returns a |
173 | | * pointer to a malloced string with length given in *olen. |
174 | | * If length == 0, the length is assumed to be strlen(string). |
175 | | * If olen == NULL, no output length is stored. |
176 | | * 'data' is ignored since 7.82.0. |
177 | | */ |
178 | | char *curl_easy_unescape(struct Curl_easy *data, const char *string, |
179 | | int length, int *olen) |
180 | 0 | { |
181 | 0 | char *str = NULL; |
182 | 0 | (void)data; |
183 | 0 | if(length >= 0) { |
184 | 0 | size_t inputlen = (size_t)length; |
185 | 0 | size_t outputlen; |
186 | 0 | CURLcode res = Curl_urldecode(string, inputlen, &str, &outputlen, |
187 | 0 | REJECT_NADA); |
188 | 0 | if(res) |
189 | 0 | return NULL; |
190 | | |
191 | 0 | if(olen) { |
192 | 0 | if(outputlen <= (size_t) INT_MAX) |
193 | 0 | *olen = curlx_uztosi(outputlen); |
194 | 0 | else |
195 | | /* too large to return in an int, fail! */ |
196 | 0 | Curl_safefree(str); |
197 | 0 | } |
198 | 0 | } |
199 | 0 | return str; |
200 | 0 | } |
201 | | |
202 | | /* For operating systems/environments that use different malloc/free |
203 | | systems for the app and for this library, we provide a free that uses |
204 | | the library's memory system */ |
205 | | void curl_free(void *p) |
206 | 0 | { |
207 | 0 | free(p); |
208 | 0 | } |
209 | | |
210 | | /* |
211 | | * Curl_hexencode() |
212 | | * |
213 | | * Converts binary input to lowercase hex-encoded ASCII output. |
214 | | * Null-terminated. |
215 | | */ |
216 | | void Curl_hexencode(const unsigned char *src, size_t len, /* input length */ |
217 | | unsigned char *out, size_t olen) /* output buffer size */ |
218 | 0 | { |
219 | 0 | const char *hex = "0123456789abcdef"; |
220 | 0 | DEBUGASSERT(src && len && (olen >= 3)); |
221 | 0 | if(src && len && (olen >= 3)) { |
222 | 0 | while(len-- && (olen >= 3)) { |
223 | | /* clang-tidy warns on this line without this comment: */ |
224 | | /* NOLINTNEXTLINE(clang-analyzer-core.UndefinedBinaryOperatorResult) */ |
225 | 0 | *out++ = hex[(*src & 0xF0)>>4]; |
226 | 0 | *out++ = hex[*src & 0x0F]; |
227 | 0 | ++src; |
228 | 0 | olen -= 2; |
229 | 0 | } |
230 | 0 | *out = 0; |
231 | 0 | } |
232 | 0 | else if(olen) |
233 | 0 | *out = 0; |
234 | 0 | } |