Sync with trunk r63174.
[reactos.git] / dll / win32 / urlmon / uri.c
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "urlmon_main.h"
21
22 #include <strsafe.h>
23
24 #define URI_DISPLAY_NO_ABSOLUTE_URI 0x1
25 #define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2
26
27 #define ALLOW_NULL_TERM_SCHEME 0x01
28 #define ALLOW_NULL_TERM_USER_NAME 0x02
29 #define ALLOW_NULL_TERM_PASSWORD 0x04
30 #define ALLOW_BRACKETLESS_IP_LITERAL 0x08
31 #define SKIP_IP_FUTURE_CHECK 0x10
32 #define IGNORE_PORT_DELIMITER 0x20
33
34 #define RAW_URI_FORCE_PORT_DISP 0x1
35 #define RAW_URI_CONVERT_TO_DOS_PATH 0x2
36
37 #define COMBINE_URI_FORCE_FLAG_USE 0x1
38
39 static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}};
40
41 typedef struct {
42 IUri IUri_iface;
43 IUriBuilderFactory IUriBuilderFactory_iface;
44 IPersistStream IPersistStream_iface;
45 IMarshal IMarshal_iface;
46
47 LONG ref;
48
49 BSTR raw_uri;
50
51 /* Information about the canonicalized URI's buffer. */
52 WCHAR *canon_uri;
53 DWORD canon_size;
54 DWORD canon_len;
55 BOOL display_modifiers;
56 DWORD create_flags;
57
58 INT scheme_start;
59 DWORD scheme_len;
60 URL_SCHEME scheme_type;
61
62 INT userinfo_start;
63 DWORD userinfo_len;
64 INT userinfo_split;
65
66 INT host_start;
67 DWORD host_len;
68 Uri_HOST_TYPE host_type;
69
70 INT port_offset;
71 DWORD port;
72 BOOL has_port;
73
74 INT authority_start;
75 DWORD authority_len;
76
77 INT domain_offset;
78
79 INT path_start;
80 DWORD path_len;
81 INT extension_offset;
82
83 INT query_start;
84 DWORD query_len;
85
86 INT fragment_start;
87 DWORD fragment_len;
88 } Uri;
89
90 typedef struct {
91 IUriBuilder IUriBuilder_iface;
92 LONG ref;
93
94 Uri *uri;
95 DWORD modified_props;
96
97 WCHAR *fragment;
98 DWORD fragment_len;
99
100 WCHAR *host;
101 DWORD host_len;
102
103 WCHAR *password;
104 DWORD password_len;
105
106 WCHAR *path;
107 DWORD path_len;
108
109 BOOL has_port;
110 DWORD port;
111
112 WCHAR *query;
113 DWORD query_len;
114
115 WCHAR *scheme;
116 DWORD scheme_len;
117
118 WCHAR *username;
119 DWORD username_len;
120 } UriBuilder;
121
122 typedef struct {
123 const WCHAR *str;
124 DWORD len;
125 } h16;
126
127 typedef struct {
128 /* IPv6 addresses can hold up to 8 h16 components. */
129 h16 components[8];
130 DWORD h16_count;
131
132 /* An IPv6 can have 1 elision ("::"). */
133 const WCHAR *elision;
134
135 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
136 const WCHAR *ipv4;
137 DWORD ipv4_len;
138
139 INT components_size;
140 INT elision_size;
141 } ipv6_address;
142
143 typedef struct {
144 BSTR uri;
145
146 BOOL is_relative;
147 BOOL is_opaque;
148 BOOL has_implicit_scheme;
149 BOOL has_implicit_ip;
150 UINT implicit_ipv4;
151 BOOL must_have_path;
152
153 const WCHAR *scheme;
154 DWORD scheme_len;
155 URL_SCHEME scheme_type;
156
157 const WCHAR *username;
158 DWORD username_len;
159
160 const WCHAR *password;
161 DWORD password_len;
162
163 const WCHAR *host;
164 DWORD host_len;
165 Uri_HOST_TYPE host_type;
166
167 BOOL has_ipv6;
168 ipv6_address ipv6_address;
169
170 BOOL has_port;
171 const WCHAR *port;
172 DWORD port_len;
173 DWORD port_value;
174
175 const WCHAR *path;
176 DWORD path_len;
177
178 const WCHAR *query;
179 DWORD query_len;
180
181 const WCHAR *fragment;
182 DWORD fragment_len;
183 } parse_data;
184
185 static const CHAR hexDigits[] = "0123456789ABCDEF";
186
187 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
188 static const struct {
189 URL_SCHEME scheme;
190 WCHAR scheme_name[16];
191 } recognized_schemes[] = {
192 {URL_SCHEME_FTP, {'f','t','p',0}},
193 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
194 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
195 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
196 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
197 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
198 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
199 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
200 {URL_SCHEME_FILE, {'f','i','l','e',0}},
201 {URL_SCHEME_MK, {'m','k',0}},
202 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
203 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
204 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
205 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
206 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
207 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
208 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
209 {URL_SCHEME_RES, {'r','e','s',0}},
210 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
211 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
212 {URL_SCHEME_MSHELP, {'h','c','p',0}},
213 {URL_SCHEME_WILDCARD, {'*',0}}
214 };
215
216 /* List of default ports Windows recognizes. */
217 static const struct {
218 URL_SCHEME scheme;
219 USHORT port;
220 } default_ports[] = {
221 {URL_SCHEME_FTP, 21},
222 {URL_SCHEME_HTTP, 80},
223 {URL_SCHEME_GOPHER, 70},
224 {URL_SCHEME_NNTP, 119},
225 {URL_SCHEME_TELNET, 23},
226 {URL_SCHEME_WAIS, 210},
227 {URL_SCHEME_HTTPS, 443},
228 };
229
230 /* List of 3-character top level domain names Windows seems to recognize.
231 * There might be more, but, these are the only ones I've found so far.
232 */
233 static const struct {
234 WCHAR tld_name[4];
235 } recognized_tlds[] = {
236 {{'c','o','m',0}},
237 {{'e','d','u',0}},
238 {{'g','o','v',0}},
239 {{'i','n','t',0}},
240 {{'m','i','l',0}},
241 {{'n','e','t',0}},
242 {{'o','r','g',0}}
243 };
244
245 static Uri *get_uri_obj(IUri *uri)
246 {
247 Uri *ret;
248 HRESULT hres;
249
250 hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret);
251 return SUCCEEDED(hres) ? ret : NULL;
252 }
253
254 static inline BOOL is_alpha(WCHAR val) {
255 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
256 }
257
258 static inline BOOL is_num(WCHAR val) {
259 return (val >= '0' && val <= '9');
260 }
261
262 static inline BOOL is_drive_path(const WCHAR *str) {
263 return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|'));
264 }
265
266 static inline BOOL is_unc_path(const WCHAR *str) {
267 return (str[0] == '\\' && str[1] == '\\');
268 }
269
270 static inline BOOL is_forbidden_dos_path_char(WCHAR val) {
271 return (val == '>' || val == '<' || val == '\"');
272 }
273
274 /* A URI is implicitly a file path if it begins with
275 * a drive letter (e.g. X:) or starts with "\\" (UNC path).
276 */
277 static inline BOOL is_implicit_file_path(const WCHAR *str) {
278 return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':'));
279 }
280
281 /* Checks if the URI is a hierarchical URI. A hierarchical
282 * URI is one that has "//" after the scheme.
283 */
284 static BOOL check_hierarchical(const WCHAR **ptr) {
285 const WCHAR *start = *ptr;
286
287 if(**ptr != '/')
288 return FALSE;
289
290 ++(*ptr);
291 if(**ptr != '/') {
292 *ptr = start;
293 return FALSE;
294 }
295
296 ++(*ptr);
297 return TRUE;
298 }
299
300 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
301 static inline BOOL is_unreserved(WCHAR val) {
302 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
303 val == '_' || val == '~');
304 }
305
306 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
307 * / "*" / "+" / "," / ";" / "="
308 */
309 static inline BOOL is_subdelim(WCHAR val) {
310 return (val == '!' || val == '$' || val == '&' ||
311 val == '\'' || val == '(' || val == ')' ||
312 val == '*' || val == '+' || val == ',' ||
313 val == ';' || val == '=');
314 }
315
316 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
317 static inline BOOL is_gendelim(WCHAR val) {
318 return (val == ':' || val == '/' || val == '?' ||
319 val == '#' || val == '[' || val == ']' ||
320 val == '@');
321 }
322
323 /* Characters that delimit the end of the authority
324 * section of a URI. Sometimes a '\\' is considered
325 * an authority delimiter.
326 */
327 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
328 return (val == '#' || val == '/' || val == '?' ||
329 val == '\0' || (acceptSlash && val == '\\'));
330 }
331
332 /* reserved = gen-delims / sub-delims */
333 static inline BOOL is_reserved(WCHAR val) {
334 return (is_subdelim(val) || is_gendelim(val));
335 }
336
337 static inline BOOL is_hexdigit(WCHAR val) {
338 return ((val >= 'a' && val <= 'f') ||
339 (val >= 'A' && val <= 'F') ||
340 (val >= '0' && val <= '9'));
341 }
342
343 static inline BOOL is_path_delim(WCHAR val) {
344 return (!val || val == '#' || val == '?');
345 }
346
347 static inline BOOL is_slash(WCHAR c)
348 {
349 return c == '/' || c == '\\';
350 }
351
352 static BOOL is_default_port(URL_SCHEME scheme, DWORD port) {
353 DWORD i;
354
355 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
356 if(default_ports[i].scheme == scheme && default_ports[i].port)
357 return TRUE;
358 }
359
360 return FALSE;
361 }
362
363 /* List of schemes types Windows seems to expect to be hierarchical. */
364 static inline BOOL is_hierarchical_scheme(URL_SCHEME type) {
365 return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP ||
366 type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP ||
367 type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS ||
368 type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS ||
369 type == URL_SCHEME_RES);
370 }
371
372 /* Checks if 'flags' contains an invalid combination of Uri_CREATE flags. */
373 static inline BOOL has_invalid_flag_combination(DWORD flags) {
374 return((flags & Uri_CREATE_DECODE_EXTRA_INFO && flags & Uri_CREATE_NO_DECODE_EXTRA_INFO) ||
375 (flags & Uri_CREATE_CANONICALIZE && flags & Uri_CREATE_NO_CANONICALIZE) ||
376 (flags & Uri_CREATE_CRACK_UNKNOWN_SCHEMES && flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES) ||
377 (flags & Uri_CREATE_PRE_PROCESS_HTML_URI && flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI) ||
378 (flags & Uri_CREATE_IE_SETTINGS && flags & Uri_CREATE_NO_IE_SETTINGS));
379 }
380
381 /* Applies each default Uri_CREATE flags to 'flags' if it
382 * doesn't cause a flag conflict.
383 */
384 static void apply_default_flags(DWORD *flags) {
385 if(!(*flags & Uri_CREATE_NO_CANONICALIZE))
386 *flags |= Uri_CREATE_CANONICALIZE;
387 if(!(*flags & Uri_CREATE_NO_DECODE_EXTRA_INFO))
388 *flags |= Uri_CREATE_DECODE_EXTRA_INFO;
389 if(!(*flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES))
390 *flags |= Uri_CREATE_CRACK_UNKNOWN_SCHEMES;
391 if(!(*flags & Uri_CREATE_NO_PRE_PROCESS_HTML_URI))
392 *flags |= Uri_CREATE_PRE_PROCESS_HTML_URI;
393 if(!(*flags & Uri_CREATE_IE_SETTINGS))
394 *flags |= Uri_CREATE_NO_IE_SETTINGS;
395 }
396
397 /* Determines if the URI is hierarchical using the information already parsed into
398 * data and using the current location of parsing in the URI string.
399 *
400 * Windows considers a URI hierarchical if one of the following is true:
401 * A.) It's a wildcard scheme.
402 * B.) It's an implicit file scheme.
403 * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name.
404 * (the '\\' will be converted into "//" during canonicalization).
405 * D.) "//" appears after the scheme name (or at the beginning if no scheme is given).
406 */
407 static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) {
408 const WCHAR *start = *ptr;
409
410 if(data->scheme_type == URL_SCHEME_WILDCARD)
411 return TRUE;
412 else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme)
413 return TRUE;
414 else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') {
415 *ptr += 2;
416 return TRUE;
417 } else if(data->scheme_type != URL_SCHEME_MAILTO && check_hierarchical(ptr))
418 return TRUE;
419
420 *ptr = start;
421 return FALSE;
422 }
423
424 /* Computes the size of the given IPv6 address.
425 * Each h16 component is 16 bits. If there is an IPv4 address, it's
426 * 32 bits. If there's an elision it can be 16 to 128 bits, depending
427 * on the number of other components.
428 *
429 * Modeled after google-url's CheckIPv6ComponentsSize function
430 */
431 static void compute_ipv6_comps_size(ipv6_address *address) {
432 address->components_size = address->h16_count * 2;
433
434 if(address->ipv4)
435 /* IPv4 address is 4 bytes. */
436 address->components_size += 4;
437
438 if(address->elision) {
439 /* An elision can be anywhere from 2 bytes up to 16 bytes.
440 * Its size depends on the size of the h16 and IPv4 components.
441 */
442 address->elision_size = 16 - address->components_size;
443 if(address->elision_size < 2)
444 address->elision_size = 2;
445 } else
446 address->elision_size = 0;
447 }
448
449 /* Taken from dlls/jscript/lex.c */
450 static int hex_to_int(WCHAR val) {
451 if(val >= '0' && val <= '9')
452 return val - '0';
453 else if(val >= 'a' && val <= 'f')
454 return val - 'a' + 10;
455 else if(val >= 'A' && val <= 'F')
456 return val - 'A' + 10;
457
458 return -1;
459 }
460
461 /* Helper function for converting a percent encoded string
462 * representation of a WCHAR value into its actual WCHAR value. If
463 * the two characters following the '%' aren't valid hex values then
464 * this function returns the NULL character.
465 *
466 * E.g.
467 * "%2E" will result in '.' being returned by this function.
468 */
469 static WCHAR decode_pct_val(const WCHAR *ptr) {
470 WCHAR ret = '\0';
471
472 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
473 INT a = hex_to_int(*(ptr + 1));
474 INT b = hex_to_int(*(ptr + 2));
475
476 ret = a << 4;
477 ret += b;
478 }
479
480 return ret;
481 }
482
483 /* Helper function for percent encoding a given character
484 * and storing the encoded value into a given buffer (dest).
485 *
486 * It's up to the calling function to ensure that there is
487 * at least enough space in 'dest' for the percent encoded
488 * value to be stored (so dest + 3 spaces available).
489 */
490 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
491 dest[0] = '%';
492 dest[1] = hexDigits[(val >> 4) & 0xf];
493 dest[2] = hexDigits[val & 0xf];
494 }
495
496 /* Attempts to parse the domain name from the host.
497 *
498 * This function also includes the Top-level Domain (TLD) name
499 * of the host when it tries to find the domain name. If it finds
500 * a valid domain name it will assign 'domain_start' the offset
501 * into 'host' where the domain name starts.
502 *
503 * It's implied that if there is a domain name its range is:
504 * [host+domain_start, host+host_len).
505 */
506 void find_domain_name(const WCHAR *host, DWORD host_len,
507 INT *domain_start) {
508 const WCHAR *last_tld, *sec_last_tld, *end;
509
510 end = host+host_len-1;
511
512 *domain_start = -1;
513
514 /* There has to be at least enough room for a '.' followed by a
515 * 3-character TLD for a domain to even exist in the host name.
516 */
517 if(host_len < 4)
518 return;
519
520 last_tld = memrchrW(host, '.', host_len);
521 if(!last_tld)
522 /* http://hostname -> has no domain name. */
523 return;
524
525 sec_last_tld = memrchrW(host, '.', last_tld-host);
526 if(!sec_last_tld) {
527 /* If the '.' is at the beginning of the host there
528 * has to be at least 3 characters in the TLD for it
529 * to be valid.
530 * Ex: .com -> .com as the domain name.
531 * .co -> has no domain name.
532 */
533 if(last_tld-host == 0) {
534 if(end-(last_tld-1) < 3)
535 return;
536 } else if(last_tld-host == 3) {
537 DWORD i;
538
539 /* If there are three characters in front of last_tld and
540 * they are on the list of recognized TLDs, then this
541 * host doesn't have a domain (since the host only contains
542 * a TLD name.
543 * Ex: edu.uk -> has no domain name.
544 * foo.uk -> foo.uk as the domain name.
545 */
546 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
547 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
548 return;
549 }
550 } else if(last_tld-host < 3)
551 /* Anything less than 3 characters is considered part
552 * of the TLD name.
553 * Ex: ak.uk -> Has no domain name.
554 */
555 return;
556
557 /* Otherwise the domain name is the whole host name. */
558 *domain_start = 0;
559 } else if(end+1-last_tld > 3) {
560 /* If the last_tld has more than 3 characters, then it's automatically
561 * considered the TLD of the domain name.
562 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
563 */
564 *domain_start = (sec_last_tld+1)-host;
565 } else if(last_tld - (sec_last_tld+1) < 4) {
566 DWORD i;
567 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
568 * recognized to still be considered part of the TLD name, otherwise
569 * it's considered the domain name.
570 * Ex: www.google.com.uk -> google.com.uk as the domain name.
571 * www.google.foo.uk -> foo.uk as the domain name.
572 */
573 if(last_tld - (sec_last_tld+1) == 3) {
574 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
575 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
576 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
577
578 if(!domain)
579 *domain_start = 0;
580 else
581 *domain_start = (domain+1) - host;
582 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
583 (host+host_len)-(host+*domain_start)));
584 return;
585 }
586 }
587
588 *domain_start = (sec_last_tld+1)-host;
589 } else {
590 /* Since the sec_last_tld is less than 3 characters it's considered
591 * part of the TLD.
592 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
593 */
594 const WCHAR *domain = memrchrW(host, '.', sec_last_tld-host);
595
596 if(!domain)
597 *domain_start = 0;
598 else
599 *domain_start = (domain+1) - host;
600 }
601 } else {
602 /* The second to last TLD has more than 3 characters making it
603 * the domain name.
604 * Ex: www.google.test.us -> test.us as the domain name.
605 */
606 *domain_start = (sec_last_tld+1)-host;
607 }
608
609 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
610 (host+host_len)-(host+*domain_start)));
611 }
612
613 /* Removes the dot segments from a hierarchical URIs path component. This
614 * function performs the removal in place.
615 *
616 * This function returns the new length of the path string.
617 */
618 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
619 WCHAR *out = path;
620 const WCHAR *in = out;
621 const WCHAR *end = out + path_len;
622 DWORD len;
623
624 while(in < end) {
625 /* Move the first path segment in the input buffer to the end of
626 * the output buffer, and any subsequent characters up to, including
627 * the next "/" character (if any) or the end of the input buffer.
628 */
629 while(in < end && !is_slash(*in))
630 *out++ = *in++;
631 if(in == end)
632 break;
633 *out++ = *in++;
634
635 while(in < end) {
636 if(*in != '.')
637 break;
638
639 /* Handle ending "/." */
640 if(in + 1 == end) {
641 ++in;
642 break;
643 }
644
645 /* Handle "/./" */
646 if(is_slash(in[1])) {
647 in += 2;
648 continue;
649 }
650
651 /* If we don't have "/../" or ending "/.." */
652 if(in[1] != '.' || (in + 2 != end && !is_slash(in[2])))
653 break;
654
655 /* Find the slash preceding out pointer and move out pointer to it */
656 if(out > path+1 && is_slash(*--out))
657 --out;
658 while(out > path && !is_slash(*(--out)));
659 if(is_slash(*out))
660 ++out;
661 in += 2;
662 if(in != end)
663 ++in;
664 }
665 }
666
667 len = out - path;
668 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
669 debugstr_wn(path, len), len);
670 return len;
671 }
672
673 /* Attempts to find the file extension in a given path. */
674 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
675 const WCHAR *end;
676
677 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
678 if(*end == '.')
679 return end-path;
680 }
681
682 return -1;
683 }
684
685 /* Computes the location where the elision should occur in the IPv6
686 * address using the numerical values of each component stored in
687 * 'values'. If the address shouldn't contain an elision then 'index'
688 * is assigned -1 as its value. Otherwise 'index' will contain the
689 * starting index (into values) where the elision should be, and 'count'
690 * will contain the number of cells the elision covers.
691 *
692 * NOTES:
693 * Windows will expand an elision if the elision only represents one h16
694 * component of the address.
695 *
696 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
697 *
698 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
699 * considered for being included as part of an elision if all its components
700 * are zeros.
701 *
702 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
703 */
704 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
705 INT *index, DWORD *count) {
706 DWORD i, max_len, cur_len;
707 INT max_index, cur_index;
708
709 max_len = cur_len = 0;
710 max_index = cur_index = -1;
711 for(i = 0; i < 8; ++i) {
712 BOOL check_ipv4 = (address->ipv4 && i == 6);
713 BOOL is_end = (check_ipv4 || i == 7);
714
715 if(check_ipv4) {
716 /* Check if the IPv4 address contains only zeros. */
717 if(values[i] == 0 && values[i+1] == 0) {
718 if(cur_index == -1)
719 cur_index = i;
720
721 cur_len += 2;
722 ++i;
723 }
724 } else if(values[i] == 0) {
725 if(cur_index == -1)
726 cur_index = i;
727
728 ++cur_len;
729 }
730
731 if(is_end || values[i] != 0) {
732 /* We only consider it for an elision if it's
733 * more than 1 component long.
734 */
735 if(cur_len > 1 && cur_len > max_len) {
736 /* Found the new elision location. */
737 max_len = cur_len;
738 max_index = cur_index;
739 }
740
741 /* Reset the current range for the next range of zeros. */
742 cur_index = -1;
743 cur_len = 0;
744 }
745 }
746
747 *index = max_index;
748 *count = max_len;
749 }
750
751 /* Removes all the leading and trailing white spaces or
752 * control characters from the URI and removes all control
753 * characters inside of the URI string.
754 */
755 static BSTR pre_process_uri(LPCWSTR uri) {
756 const WCHAR *start, *end, *ptr;
757 WCHAR *ptr2;
758 DWORD len;
759 BSTR ret;
760
761 start = uri;
762 /* Skip leading controls and whitespace. */
763 while(*start && (iscntrlW(*start) || isspaceW(*start))) ++start;
764
765 /* URI consisted only of control/whitespace. */
766 if(!*start)
767 return SysAllocStringLen(NULL, 0);
768
769 end = start + strlenW(start);
770 while(--end > start && (iscntrlW(*end) || isspaceW(*end)));
771
772 len = ++end - start;
773 for(ptr = start; ptr < end; ptr++) {
774 if(iscntrlW(*ptr))
775 len--;
776 }
777
778 ret = SysAllocStringLen(NULL, len);
779 if(!ret)
780 return NULL;
781
782 for(ptr = start, ptr2=ret; ptr < end; ptr++) {
783 if(!iscntrlW(*ptr))
784 *ptr2++ = *ptr;
785 }
786
787 return ret;
788 }
789
790 /* Converts the specified IPv4 address into an uint value.
791 *
792 * This function assumes that the IPv4 address has already been validated.
793 */
794 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
795 UINT ret = 0;
796 DWORD comp_value = 0;
797 const WCHAR *ptr;
798
799 for(ptr = ip; ptr < ip+len; ++ptr) {
800 if(*ptr == '.') {
801 ret <<= 8;
802 ret += comp_value;
803 comp_value = 0;
804 } else
805 comp_value = comp_value*10 + (*ptr-'0');
806 }
807
808 ret <<= 8;
809 ret += comp_value;
810
811 return ret;
812 }
813
814 /* Converts an IPv4 address in numerical form into its fully qualified
815 * string form. This function returns the number of characters written
816 * to 'dest'. If 'dest' is NULL this function will return the number of
817 * characters that would have been written.
818 *
819 * It's up to the caller to ensure there's enough space in 'dest' for the
820 * address.
821 */
822 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
823 static const WCHAR formatW[] =
824 {'%','u','.','%','u','.','%','u','.','%','u',0};
825 DWORD ret = 0;
826 UCHAR digits[4];
827
828 digits[0] = (address >> 24) & 0xff;
829 digits[1] = (address >> 16) & 0xff;
830 digits[2] = (address >> 8) & 0xff;
831 digits[3] = address & 0xff;
832
833 if(!dest) {
834 WCHAR tmp[16];
835 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
836 } else
837 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
838
839 return ret;
840 }
841
842 static DWORD ui2str(WCHAR *dest, UINT value) {
843 static const WCHAR formatW[] = {'%','u',0};
844 DWORD ret = 0;
845
846 if(!dest) {
847 WCHAR tmp[11];
848 ret = sprintfW(tmp, formatW, value);
849 } else
850 ret = sprintfW(dest, formatW, value);
851
852 return ret;
853 }
854
855 /* Converts a h16 component (from an IPv6 address) into its
856 * numerical value.
857 *
858 * This function assumes that the h16 component has already been validated.
859 */
860 static USHORT h16tous(h16 component) {
861 DWORD i;
862 USHORT ret = 0;
863
864 for(i = 0; i < component.len; ++i) {
865 ret <<= 4;
866 ret += hex_to_int(component.str[i]);
867 }
868
869 return ret;
870 }
871
872 /* Converts an IPv6 address into its 128 bits (16 bytes) numerical value.
873 *
874 * This function assumes that the ipv6_address has already been validated.
875 */
876 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
877 DWORD i, cur_component = 0;
878 BOOL already_passed_elision = FALSE;
879
880 for(i = 0; i < address->h16_count; ++i) {
881 if(address->elision) {
882 if(address->components[i].str > address->elision && !already_passed_elision) {
883 /* Means we just passed the elision and need to add its values to
884 * 'number' before we do anything else.
885 */
886 INT j;
887 for(j = 0; j < address->elision_size; j+=2)
888 number[cur_component++] = 0;
889
890 already_passed_elision = TRUE;
891 }
892 }
893
894 number[cur_component++] = h16tous(address->components[i]);
895 }
896
897 /* Case when the elision appears after the h16 components. */
898 if(!already_passed_elision && address->elision) {
899 INT j;
900 for(j = 0; j < address->elision_size; j+=2)
901 number[cur_component++] = 0;
902 }
903
904 if(address->ipv4) {
905 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
906
907 if(cur_component != 6) {
908 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
909 return FALSE;
910 }
911
912 number[cur_component++] = (value >> 16) & 0xffff;
913 number[cur_component] = value & 0xffff;
914 }
915
916 return TRUE;
917 }
918
919 /* Checks if the characters pointed to by 'ptr' are
920 * a percent encoded data octet.
921 *
922 * pct-encoded = "%" HEXDIG HEXDIG
923 */
924 static BOOL check_pct_encoded(const WCHAR **ptr) {
925 const WCHAR *start = *ptr;
926
927 if(**ptr != '%')
928 return FALSE;
929
930 ++(*ptr);
931 if(!is_hexdigit(**ptr)) {
932 *ptr = start;
933 return FALSE;
934 }
935
936 ++(*ptr);
937 if(!is_hexdigit(**ptr)) {
938 *ptr = start;
939 return FALSE;
940 }
941
942 ++(*ptr);
943 return TRUE;
944 }
945
946 /* dec-octet = DIGIT ; 0-9
947 * / %x31-39 DIGIT ; 10-99
948 * / "1" 2DIGIT ; 100-199
949 * / "2" %x30-34 DIGIT ; 200-249
950 * / "25" %x30-35 ; 250-255
951 */
952 static BOOL check_dec_octet(const WCHAR **ptr) {
953 const WCHAR *c1, *c2, *c3;
954
955 c1 = *ptr;
956 /* A dec-octet must be at least 1 digit long. */
957 if(*c1 < '0' || *c1 > '9')
958 return FALSE;
959
960 ++(*ptr);
961
962 c2 = *ptr;
963 /* Since the 1-digit requirement was met, it doesn't
964 * matter if this is a DIGIT value, it's considered a
965 * dec-octet.
966 */
967 if(*c2 < '0' || *c2 > '9')
968 return TRUE;
969
970 ++(*ptr);
971
972 c3 = *ptr;
973 /* Same explanation as above. */
974 if(*c3 < '0' || *c3 > '9')
975 return TRUE;
976
977 /* Anything > 255 isn't a valid IP dec-octet. */
978 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
979 *ptr = c1;
980 return FALSE;
981 }
982
983 ++(*ptr);
984 return TRUE;
985 }
986
987 /* Checks if there is an implicit IPv4 address in the host component of the URI.
988 * The max value of an implicit IPv4 address is UINT_MAX.
989 *
990 * Ex:
991 * "234567" would be considered an implicit IPv4 address.
992 */
993 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
994 const WCHAR *start = *ptr;
995 ULONGLONG ret = 0;
996 *val = 0;
997
998 while(is_num(**ptr)) {
999 ret = ret*10 + (**ptr - '0');
1000
1001 if(ret > UINT_MAX) {
1002 *ptr = start;
1003 return FALSE;
1004 }
1005 ++(*ptr);
1006 }
1007
1008 if(*ptr == start)
1009 return FALSE;
1010
1011 *val = ret;
1012 return TRUE;
1013 }
1014
1015 /* Checks if the string contains an IPv4 address.
1016 *
1017 * This function has a strict mode or a non-strict mode of operation
1018 * When 'strict' is set to FALSE this function will return TRUE if
1019 * the string contains at least 'dec-octet "." dec-octet' since partial
1020 * IPv4 addresses will be normalized out into full IPv4 addresses. When
1021 * 'strict' is set this function expects there to be a full IPv4 address.
1022 *
1023 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
1024 */
1025 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
1026 const WCHAR *start = *ptr;
1027
1028 if(!check_dec_octet(ptr)) {
1029 *ptr = start;
1030 return FALSE;
1031 }
1032
1033 if(**ptr != '.') {
1034 *ptr = start;
1035 return FALSE;
1036 }
1037
1038 ++(*ptr);
1039 if(!check_dec_octet(ptr)) {
1040 *ptr = start;
1041 return FALSE;
1042 }
1043
1044 if(**ptr != '.') {
1045 if(strict) {
1046 *ptr = start;
1047 return FALSE;
1048 } else
1049 return TRUE;
1050 }
1051
1052 ++(*ptr);
1053 if(!check_dec_octet(ptr)) {
1054 *ptr = start;
1055 return FALSE;
1056 }
1057
1058 if(**ptr != '.') {
1059 if(strict) {
1060 *ptr = start;
1061 return FALSE;
1062 } else
1063 return TRUE;
1064 }
1065
1066 ++(*ptr);
1067 if(!check_dec_octet(ptr)) {
1068 *ptr = start;
1069 return FALSE;
1070 }
1071
1072 /* Found a four digit ip address. */
1073 return TRUE;
1074 }
1075 /* Tries to parse the scheme name of the URI.
1076 *
1077 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
1078 * NOTE: Windows accepts a number as the first character of a scheme.
1079 */
1080 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) {
1081 const WCHAR *start = *ptr;
1082
1083 data->scheme = NULL;
1084 data->scheme_len = 0;
1085
1086 while(**ptr) {
1087 if(**ptr == '*' && *ptr == start) {
1088 /* Might have found a wildcard scheme. If it is the next
1089 * char has to be a ':' for it to be a valid URI
1090 */
1091 ++(*ptr);
1092 break;
1093 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
1094 **ptr != '-' && **ptr != '.')
1095 break;
1096
1097 (*ptr)++;
1098 }
1099
1100 if(*ptr == start)
1101 return FALSE;
1102
1103 /* Schemes must end with a ':' */
1104 if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) {
1105 *ptr = start;
1106 return FALSE;
1107 }
1108
1109 data->scheme = start;
1110 data->scheme_len = *ptr - start;
1111
1112 ++(*ptr);
1113 return TRUE;
1114 }
1115
1116 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
1117 * the deduced URL_SCHEME in data->scheme_type.
1118 */
1119 static BOOL parse_scheme_type(parse_data *data) {
1120 /* If there's scheme data then see if it's a recognized scheme. */
1121 if(data->scheme && data->scheme_len) {
1122 DWORD i;
1123
1124 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
1125 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
1126 /* Has to be a case insensitive compare. */
1127 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
1128 data->scheme_type = recognized_schemes[i].scheme;
1129 return TRUE;
1130 }
1131 }
1132 }
1133
1134 /* If we get here it means it's not a recognized scheme. */
1135 data->scheme_type = URL_SCHEME_UNKNOWN;
1136 return TRUE;
1137 } else if(data->is_relative) {
1138 /* Relative URI's have no scheme. */
1139 data->scheme_type = URL_SCHEME_UNKNOWN;
1140 return TRUE;
1141 } else {
1142 /* Should never reach here! what happened... */
1143 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
1144 return FALSE;
1145 }
1146 }
1147
1148 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
1149 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
1150 * using the flags specified in 'flags' (if any). Flags that affect how this function
1151 * operates are the Uri_CREATE_ALLOW_* flags.
1152 *
1153 * All parsed/deduced information will be stored in 'data' when the function returns.
1154 *
1155 * Returns TRUE if it was able to successfully parse the information.
1156 */
1157 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1158 static const WCHAR fileW[] = {'f','i','l','e',0};
1159 static const WCHAR wildcardW[] = {'*',0};
1160
1161 /* First check to see if the uri could implicitly be a file path. */
1162 if(is_implicit_file_path(*ptr)) {
1163 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
1164 data->scheme = fileW;
1165 data->scheme_len = lstrlenW(fileW);
1166 data->has_implicit_scheme = TRUE;
1167
1168 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
1169 } else {
1170 /* Windows does not consider anything that can implicitly be a file
1171 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
1172 */
1173 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1174 ptr, data, flags);
1175 return FALSE;
1176 }
1177 } else if(!parse_scheme_name(ptr, data, extras)) {
1178 /* No scheme was found, this means it could be:
1179 * a) an implicit Wildcard scheme
1180 * b) a relative URI
1181 * c) an invalid URI.
1182 */
1183 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1184 data->scheme = wildcardW;
1185 data->scheme_len = lstrlenW(wildcardW);
1186 data->has_implicit_scheme = TRUE;
1187
1188 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1189 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1190 data->is_relative = TRUE;
1191 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1192 } else {
1193 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1194 return FALSE;
1195 }
1196 }
1197
1198 if(!data->is_relative)
1199 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1200 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1201
1202 if(!parse_scheme_type(data))
1203 return FALSE;
1204
1205 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1206 return TRUE;
1207 }
1208
1209 static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1210 data->username = *ptr;
1211
1212 while(**ptr != ':' && **ptr != '@') {
1213 if(**ptr == '%') {
1214 if(!check_pct_encoded(ptr)) {
1215 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1216 *ptr = data->username;
1217 data->username = NULL;
1218 return FALSE;
1219 }
1220 } else
1221 continue;
1222 } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr)
1223 break;
1224 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1225 *ptr = data->username;
1226 data->username = NULL;
1227 return FALSE;
1228 }
1229
1230 ++(*ptr);
1231 }
1232
1233 data->username_len = *ptr - data->username;
1234 return TRUE;
1235 }
1236
1237 static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1238 data->password = *ptr;
1239
1240 while(**ptr != '@') {
1241 if(**ptr == '%') {
1242 if(!check_pct_encoded(ptr)) {
1243 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1244 *ptr = data->password;
1245 data->password = NULL;
1246 return FALSE;
1247 }
1248 } else
1249 continue;
1250 } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr)
1251 break;
1252 else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1253 *ptr = data->password;
1254 data->password = NULL;
1255 return FALSE;
1256 }
1257
1258 ++(*ptr);
1259 }
1260
1261 data->password_len = *ptr - data->password;
1262 return TRUE;
1263 }
1264
1265 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1266 * a URI can consist of "username:password@", or just "username@".
1267 *
1268 * RFC def:
1269 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1270 *
1271 * NOTES:
1272 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1273 * uses the first occurrence of ':' to delimit the username and password
1274 * components.
1275 *
1276 * ex:
1277 * ftp://user:pass:word@winehq.org
1278 *
1279 * would yield "user" as the username and "pass:word" as the password.
1280 *
1281 * 2) Windows allows any character to appear in the "userinfo" part of
1282 * a URI, as long as it's not an authority delimiter character set.
1283 */
1284 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1285 const WCHAR *start = *ptr;
1286
1287 if(!parse_username(ptr, data, flags, 0)) {
1288 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1289 return;
1290 }
1291
1292 if(**ptr == ':') {
1293 ++(*ptr);
1294 if(!parse_password(ptr, data, flags, 0)) {
1295 *ptr = start;
1296 data->username = NULL;
1297 data->username_len = 0;
1298 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1299 return;
1300 }
1301 }
1302
1303 if(**ptr != '@') {
1304 *ptr = start;
1305 data->username = NULL;
1306 data->username_len = 0;
1307 data->password = NULL;
1308 data->password_len = 0;
1309
1310 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1311 return;
1312 }
1313
1314 if(data->username)
1315 TRACE("(%p %p %x): Found username %s len=%d.\n", ptr, data, flags,
1316 debugstr_wn(data->username, data->username_len), data->username_len);
1317
1318 if(data->password)
1319 TRACE("(%p %p %x): Found password %s len=%d.\n", ptr, data, flags,
1320 debugstr_wn(data->password, data->password_len), data->password_len);
1321
1322 ++(*ptr);
1323 }
1324
1325 /* Attempts to parse a port from the URI.
1326 *
1327 * NOTES:
1328 * Windows seems to have a cap on what the maximum value
1329 * for a port can be. The max value is USHORT_MAX.
1330 *
1331 * port = *DIGIT
1332 */
1333 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1334 UINT port = 0;
1335 data->port = *ptr;
1336
1337 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1338 if(!is_num(**ptr)) {
1339 *ptr = data->port;
1340 data->port = NULL;
1341 return FALSE;
1342 }
1343
1344 port = port*10 + (**ptr-'0');
1345
1346 if(port > USHRT_MAX) {
1347 *ptr = data->port;
1348 data->port = NULL;
1349 return FALSE;
1350 }
1351
1352 ++(*ptr);
1353 }
1354
1355 data->has_port = TRUE;
1356 data->port_value = port;
1357 data->port_len = *ptr - data->port;
1358
1359 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1360 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1361 return TRUE;
1362 }
1363
1364 /* Attempts to parse a IPv4 address from the URI.
1365 *
1366 * NOTES:
1367 * Windows normalizes IPv4 addresses, This means there are three
1368 * possibilities for the URI to contain an IPv4 address.
1369 * 1) A well formed address (ex. 192.2.2.2).
1370 * 2) A partially formed address. For example "192.0" would
1371 * normalize to "192.0.0.0" during canonicalization.
1372 * 3) An implicit IPv4 address. For example "256" would
1373 * normalize to "0.0.1.0" during canonicalization. Also
1374 * note that the maximum value for an implicit IP address
1375 * is UINT_MAX, if the value in the URI exceeds this then
1376 * it is not considered an IPv4 address.
1377 */
1378 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1379 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1380 data->host = *ptr;
1381
1382 if(!check_ipv4address(ptr, FALSE)) {
1383 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1384 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1385 ptr, data, flags);
1386 *ptr = data->host;
1387 data->host = NULL;
1388 return FALSE;
1389 } else
1390 data->has_implicit_ip = TRUE;
1391 }
1392
1393 data->host_len = *ptr - data->host;
1394 data->host_type = Uri_HOST_IPV4;
1395
1396 /* Check if what we found is the only part of the host name (if it isn't
1397 * we don't have an IPv4 address).
1398 */
1399 if(**ptr == ':') {
1400 ++(*ptr);
1401 if(!parse_port(ptr, data, flags)) {
1402 *ptr = data->host;
1403 data->host = NULL;
1404 return FALSE;
1405 }
1406 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1407 /* Found more data which belongs to the host, so this isn't an IPv4. */
1408 *ptr = data->host;
1409 data->host = NULL;
1410 data->has_implicit_ip = FALSE;
1411 return FALSE;
1412 }
1413
1414 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1415 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1416 data->host_len, data->host_type);
1417 return TRUE;
1418 }
1419
1420 /* Attempts to parse the reg-name from the URI.
1421 *
1422 * Because of the way Windows handles ':' this function also
1423 * handles parsing the port.
1424 *
1425 * reg-name = *( unreserved / pct-encoded / sub-delims )
1426 *
1427 * NOTE:
1428 * Windows allows everything, but, the characters in "auth_delims" and ':'
1429 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1430 * allowed to appear (even if a valid port isn't after it).
1431 *
1432 * Windows doesn't like host names which start with '[' and end with ']'
1433 * and don't contain a valid IP literal address in between them.
1434 *
1435 * On Windows if a '[' is encountered in the host name the ':' no longer
1436 * counts as a delimiter until you reach the next ']' or an "authority delimiter".
1437 *
1438 * A reg-name CAN be empty.
1439 */
1440 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1441 const BOOL has_start_bracket = **ptr == '[';
1442 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1443 const BOOL is_res = data->scheme_type == URL_SCHEME_RES;
1444 BOOL inside_brackets = has_start_bracket;
1445
1446 /* res URIs don't have ports. */
1447 BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res;
1448
1449 /* We have to be careful with file schemes. */
1450 if(data->scheme_type == URL_SCHEME_FILE) {
1451 /* This is because an implicit file scheme could be "C:\\test" and it
1452 * would trick this function into thinking the host is "C", when after
1453 * canonicalization the host would end up being an empty string. A drive
1454 * path can also have a '|' instead of a ':' after the drive letter.
1455 */
1456 if(is_drive_path(*ptr)) {
1457 /* Regular old drive paths have no host type (or host name). */
1458 data->host_type = Uri_HOST_UNKNOWN;
1459 data->host = *ptr;
1460 data->host_len = 0;
1461 return TRUE;
1462 } else if(is_unc_path(*ptr))
1463 /* Skip past the "\\" of a UNC path. */
1464 *ptr += 2;
1465 }
1466
1467 data->host = *ptr;
1468
1469 /* For res URIs, everything before the first '/' is
1470 * considered the host.
1471 */
1472 while((!is_res && !is_auth_delim(**ptr, known_scheme)) ||
1473 (is_res && **ptr && **ptr != '/')) {
1474 if(**ptr == ':' && !ignore_col) {
1475 /* We can ignore ':' if were inside brackets.*/
1476 if(!inside_brackets) {
1477 const WCHAR *tmp = (*ptr)++;
1478
1479 /* Attempt to parse the port. */
1480 if(!parse_port(ptr, data, flags)) {
1481 /* Windows expects there to be a valid port for known scheme types. */
1482 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1483 *ptr = data->host;
1484 data->host = NULL;
1485 TRACE("(%p %p %x %x): Expected valid port\n", ptr, data, flags, extras);
1486 return FALSE;
1487 } else
1488 /* Windows gives up on trying to parse a port when it
1489 * encounters an invalid port.
1490 */
1491 ignore_col = TRUE;
1492 } else {
1493 data->host_len = tmp - data->host;
1494 break;
1495 }
1496 }
1497 } else if(**ptr == '%' && (known_scheme && !is_res)) {
1498 /* Has to be a legit % encoded value. */
1499 if(!check_pct_encoded(ptr)) {
1500 *ptr = data->host;
1501 data->host = NULL;
1502 return FALSE;
1503 } else
1504 continue;
1505 } else if(is_res && is_forbidden_dos_path_char(**ptr)) {
1506 *ptr = data->host;
1507 data->host = NULL;
1508 return FALSE;
1509 } else if(**ptr == ']')
1510 inside_brackets = FALSE;
1511 else if(**ptr == '[')
1512 inside_brackets = TRUE;
1513
1514 ++(*ptr);
1515 }
1516
1517 if(has_start_bracket) {
1518 /* Make sure the last character of the host wasn't a ']'. */
1519 if(*(*ptr-1) == ']') {
1520 TRACE("(%p %p %x %x): Expected an IP literal inside of the host\n",
1521 ptr, data, flags, extras);
1522 *ptr = data->host;
1523 data->host = NULL;
1524 return FALSE;
1525 }
1526 }
1527
1528 /* Don't overwrite our length if we found a port earlier. */
1529 if(!data->port)
1530 data->host_len = *ptr - data->host;
1531
1532 /* If the host is empty, then it's an unknown host type. */
1533 if(data->host_len == 0 || is_res)
1534 data->host_type = Uri_HOST_UNKNOWN;
1535 else
1536 data->host_type = Uri_HOST_DNS;
1537
1538 TRACE("(%p %p %x %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags, extras,
1539 debugstr_wn(data->host, data->host_len), data->host_len);
1540 return TRUE;
1541 }
1542
1543 /* Attempts to parse an IPv6 address out of the URI.
1544 *
1545 * IPv6address = 6( h16 ":" ) ls32
1546 * / "::" 5( h16 ":" ) ls32
1547 * / [ h16 ] "::" 4( h16 ":" ) ls32
1548 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1549 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1550 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1551 * / [ *4( h16 ":" ) h16 ] "::" ls32
1552 * / [ *5( h16 ":" ) h16 ] "::" h16
1553 * / [ *6( h16 ":" ) h16 ] "::"
1554 *
1555 * ls32 = ( h16 ":" h16 ) / IPv4address
1556 * ; least-significant 32 bits of address.
1557 *
1558 * h16 = 1*4HEXDIG
1559 * ; 16 bits of address represented in hexadecimal.
1560 *
1561 * Modeled after google-url's 'DoParseIPv6' function.
1562 */
1563 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1564 const WCHAR *start, *cur_start;
1565 ipv6_address ip;
1566
1567 start = cur_start = *ptr;
1568 memset(&ip, 0, sizeof(ipv6_address));
1569
1570 for(;; ++(*ptr)) {
1571 /* Check if we're on the last character of the host. */
1572 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1573 || **ptr == ']');
1574
1575 BOOL is_split = (**ptr == ':');
1576 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1577
1578 /* Check if we're at the end of a component, or
1579 * if we're at the end of the IPv6 address.
1580 */
1581 if(is_split || is_end) {
1582 DWORD cur_len = 0;
1583
1584 cur_len = *ptr - cur_start;
1585
1586 /* h16 can't have a length > 4. */
1587 if(cur_len > 4) {
1588 *ptr = start;
1589
1590 TRACE("(%p %p %x): h16 component to long.\n",
1591 ptr, data, flags);
1592 return FALSE;
1593 }
1594
1595 if(cur_len == 0) {
1596 /* An h16 component can't have the length of 0 unless
1597 * the elision is at the beginning of the address, or
1598 * at the end of the address.
1599 */
1600 if(!((*ptr == start && is_elision) ||
1601 (is_end && (*ptr-2) == ip.elision))) {
1602 *ptr = start;
1603 TRACE("(%p %p %x): IPv6 component cannot have a length of 0.\n",
1604 ptr, data, flags);
1605 return FALSE;
1606 }
1607 }
1608
1609 if(cur_len > 0) {
1610 /* An IPv6 address can have no more than 8 h16 components. */
1611 if(ip.h16_count >= 8) {
1612 *ptr = start;
1613 TRACE("(%p %p %x): Not a IPv6 address, too many h16 components.\n",
1614 ptr, data, flags);
1615 return FALSE;
1616 }
1617
1618 ip.components[ip.h16_count].str = cur_start;
1619 ip.components[ip.h16_count].len = cur_len;
1620
1621 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1622 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1623 ip.h16_count);
1624 ++ip.h16_count;
1625 }
1626 }
1627
1628 if(is_end)
1629 break;
1630
1631 if(is_elision) {
1632 /* A IPv6 address can only have 1 elision ('::'). */
1633 if(ip.elision) {
1634 *ptr = start;
1635
1636 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1637 ptr, data, flags);
1638 return FALSE;
1639 }
1640
1641 ip.elision = *ptr;
1642 ++(*ptr);
1643 }
1644
1645 if(is_split)
1646 cur_start = *ptr+1;
1647 else {
1648 if(!check_ipv4address(ptr, TRUE)) {
1649 if(!is_hexdigit(**ptr)) {
1650 /* Not a valid character for an IPv6 address. */
1651 *ptr = start;
1652 return FALSE;
1653 }
1654 } else {
1655 /* Found an IPv4 address. */
1656 ip.ipv4 = cur_start;
1657 ip.ipv4_len = *ptr - cur_start;
1658
1659 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1660 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1661 ip.ipv4_len);
1662
1663 /* IPv4 addresses can only appear at the end of a IPv6. */
1664 break;
1665 }
1666 }
1667 }
1668
1669 compute_ipv6_comps_size(&ip);
1670
1671 /* Make sure the IPv6 address adds up to 16 bytes. */
1672 if(ip.components_size + ip.elision_size != 16) {
1673 *ptr = start;
1674 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1675 ptr, data, flags);
1676 return FALSE;
1677 }
1678
1679 if(ip.elision_size == 2) {
1680 /* For some reason on Windows if an elision that represents
1681 * only one h16 component is encountered at the very begin or
1682 * end of an IPv6 address, Windows does not consider it a
1683 * valid IPv6 address.
1684 *
1685 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1686 * of all the components == 128bits.
1687 */
1688 if(ip.elision < ip.components[0].str ||
1689 ip.elision > ip.components[ip.h16_count-1].str) {
1690 *ptr = start;
1691 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1692 ptr, data, flags);
1693 return FALSE;
1694 }
1695 }
1696
1697 data->host_type = Uri_HOST_IPV6;
1698 data->has_ipv6 = TRUE;
1699 data->ipv6_address = ip;
1700
1701 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1702 ptr, data, flags, debugstr_wn(start, *ptr-start),
1703 (int)(*ptr-start));
1704 return TRUE;
1705 }
1706
1707 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1708 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1709 const WCHAR *start = *ptr;
1710
1711 /* IPvFuture has to start with a 'v' or 'V'. */
1712 if(**ptr != 'v' && **ptr != 'V')
1713 return FALSE;
1714
1715 /* Following the v there must be at least 1 hex digit. */
1716 ++(*ptr);
1717 if(!is_hexdigit(**ptr)) {
1718 *ptr = start;
1719 return FALSE;
1720 }
1721
1722 ++(*ptr);
1723 while(is_hexdigit(**ptr))
1724 ++(*ptr);
1725
1726 /* End of the hexdigit sequence must be a '.' */
1727 if(**ptr != '.') {
1728 *ptr = start;
1729 return FALSE;
1730 }
1731
1732 ++(*ptr);
1733 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1734 *ptr = start;
1735 return FALSE;
1736 }
1737
1738 ++(*ptr);
1739 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1740 ++(*ptr);
1741
1742 data->host_type = Uri_HOST_UNKNOWN;
1743
1744 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1745 debugstr_wn(start, *ptr-start), (int)(*ptr-start));
1746
1747 return TRUE;
1748 }
1749
1750 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1751 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1752 data->host = *ptr;
1753
1754 if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1755 data->host = NULL;
1756 return FALSE;
1757 } else if(**ptr == '[')
1758 ++(*ptr);
1759
1760 if(!parse_ipv6address(ptr, data, flags)) {
1761 if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data, flags)) {
1762 *ptr = data->host;
1763 data->host = NULL;
1764 return FALSE;
1765 }
1766 }
1767
1768 if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) {
1769 *ptr = data->host;
1770 data->host = NULL;
1771 return FALSE;
1772 } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) {
1773 /* The IP literal didn't contain brackets and was followed by
1774 * a NULL terminator, so no reason to even check the port.
1775 */
1776 data->host_len = *ptr - data->host;
1777 return TRUE;
1778 }
1779
1780 ++(*ptr);
1781 if(**ptr == ':') {
1782 ++(*ptr);
1783 /* If a valid port is not found, then let it trickle down to
1784 * parse_reg_name.
1785 */
1786 if(!parse_port(ptr, data, flags)) {
1787 *ptr = data->host;
1788 data->host = NULL;
1789 return FALSE;
1790 }
1791 } else
1792 data->host_len = *ptr - data->host;
1793
1794 return TRUE;
1795 }
1796
1797 /* Parses the host information from the URI.
1798 *
1799 * host = IP-literal / IPv4address / reg-name
1800 */
1801 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) {
1802 if(!parse_ip_literal(ptr, data, flags, extras)) {
1803 if(!parse_ipv4address(ptr, data, flags)) {
1804 if(!parse_reg_name(ptr, data, flags, extras)) {
1805 TRACE("(%p %p %x %x): Malformed URI, Unknown host type.\n",
1806 ptr, data, flags, extras);
1807 return FALSE;
1808 }
1809 }
1810 }
1811
1812 return TRUE;
1813 }
1814
1815 /* Parses the authority information from the URI.
1816 *
1817 * authority = [ userinfo "@" ] host [ ":" port ]
1818 */
1819 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1820 parse_userinfo(ptr, data, flags);
1821
1822 /* Parsing the port will happen during one of the host parsing
1823 * routines (if the URI has a port).
1824 */
1825 if(!parse_host(ptr, data, flags, 0))
1826 return FALSE;
1827
1828 return TRUE;
1829 }
1830
1831 /* Attempts to parse the path information of a hierarchical URI. */
1832 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1833 const WCHAR *start = *ptr;
1834 static const WCHAR slash[] = {'/',0};
1835 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1836
1837 if(is_path_delim(**ptr)) {
1838 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) {
1839 data->path = NULL;
1840 data->path_len = 0;
1841 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1842 /* If the path component is empty, then a '/' is added. */
1843 data->path = slash;
1844 data->path_len = 1;
1845 }
1846 } else {
1847 while(!is_path_delim(**ptr)) {
1848 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) {
1849 if(!check_pct_encoded(ptr)) {
1850 *ptr = start;
1851 return FALSE;
1852 } else
1853 continue;
1854 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1855 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1856 /* File schemes with USE_DOS_PATH set aren't allowed to have
1857 * a '<' or '>' or '\"' appear in them.
1858 */
1859 *ptr = start;
1860 return FALSE;
1861 } else if(**ptr == '\\') {
1862 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1863 * and the scheme is known type (but not a file scheme).
1864 */
1865 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1866 if(data->scheme_type != URL_SCHEME_FILE &&
1867 data->scheme_type != URL_SCHEME_UNKNOWN) {
1868 *ptr = start;
1869 return FALSE;
1870 }
1871 }
1872 }
1873
1874 ++(*ptr);
1875 }
1876
1877 /* The only time a URI doesn't have a path is when
1878 * the NO_CANONICALIZE flag is set and the raw URI
1879 * didn't contain one.
1880 */
1881 if(*ptr == start) {
1882 data->path = NULL;
1883 data->path_len = 0;
1884 } else {
1885 data->path = start;
1886 data->path_len = *ptr - start;
1887 }
1888 }
1889
1890 if(data->path)
1891 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1892 debugstr_wn(data->path, data->path_len), data->path_len);
1893 else
1894 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1895
1896 return TRUE;
1897 }
1898
1899 /* Parses the path of an opaque URI (much less strict than the parser
1900 * for a hierarchical URI).
1901 *
1902 * NOTE:
1903 * Windows allows invalid % encoded data to appear in opaque URI paths
1904 * for unknown scheme types.
1905 *
1906 * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"'
1907 * appear in them.
1908 */
1909 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1910 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1911 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
1912 const BOOL is_mailto = data->scheme_type == URL_SCHEME_MAILTO;
1913
1914 if (is_mailto && (*ptr)[0] == '/' && (*ptr)[1] == '/')
1915 {
1916 if ((*ptr)[2]) data->path = *ptr + 2;
1917 else data->path = NULL;
1918 }
1919 else
1920 data->path = *ptr;
1921
1922 while(!is_path_delim(**ptr)) {
1923 if(**ptr == '%' && known_scheme) {
1924 if(!check_pct_encoded(ptr)) {
1925 *ptr = data->path;
1926 data->path = NULL;
1927 return FALSE;
1928 } else
1929 continue;
1930 } else if(is_forbidden_dos_path_char(**ptr) && is_file &&
1931 (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
1932 *ptr = data->path;
1933 data->path = NULL;
1934 return FALSE;
1935 }
1936
1937 ++(*ptr);
1938 }
1939
1940 if (data->path) data->path_len = *ptr - data->path;
1941 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1942 debugstr_wn(data->path, data->path_len), data->path_len);
1943 return TRUE;
1944 }
1945
1946 /* Determines how the URI should be parsed after the scheme information.
1947 *
1948 * If the scheme is followed by "//", then it is treated as a hierarchical URI
1949 * which then the authority and path information will be parsed out. Otherwise, the
1950 * URI will be treated as an opaque URI which the authority information is not parsed
1951 * out.
1952 *
1953 * RFC 3896 definition of hier-part:
1954 *
1955 * hier-part = "//" authority path-abempty
1956 * / path-absolute
1957 * / path-rootless
1958 * / path-empty
1959 *
1960 * MSDN opaque URI definition:
1961 * scheme ":" path [ "#" fragment ]
1962 *
1963 * NOTES:
1964 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1965 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1966 * set then it is considered an opaque URI regardless of what follows the scheme information
1967 * (per MSDN documentation).
1968 */
1969 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1970 const WCHAR *start = *ptr;
1971
1972 data->must_have_path = FALSE;
1973
1974 /* For javascript: URIs, simply set everything as a path */
1975 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
1976 data->path = *ptr;
1977 data->path_len = strlenW(*ptr);
1978 data->is_opaque = TRUE;
1979 *ptr += data->path_len;
1980 return TRUE;
1981 }
1982
1983 /* Checks if the authority information needs to be parsed. */
1984 if(is_hierarchical_uri(ptr, data)) {
1985 /* Only treat it as a hierarchical URI if the scheme_type is known or
1986 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1987 */
1988 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1989 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1990 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1991 data->is_opaque = FALSE;
1992
1993 if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) {
1994 if(**ptr == '/' && *(*ptr+1) == '/') {
1995 data->must_have_path = TRUE;
1996 *ptr += 2;
1997 }
1998 }
1999
2000 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
2001 if(!parse_authority(ptr, data, flags))
2002 return FALSE;
2003
2004 return parse_path_hierarchical(ptr, data, flags);
2005 } else
2006 /* Reset ptr to its starting position so opaque path parsing
2007 * begins at the correct location.
2008 */
2009 *ptr = start;
2010 }
2011
2012 /* If it reaches here, then the URI will be treated as an opaque
2013 * URI.
2014 */
2015
2016 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
2017
2018 data->is_opaque = TRUE;
2019 if(!parse_path_opaque(ptr, data, flags))
2020 return FALSE;
2021
2022 return TRUE;
2023 }
2024
2025 /* Attempts to parse the query string from the URI.
2026 *
2027 * NOTES:
2028 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2029 * data is allowed to appear in the query string. For unknown scheme types
2030 * invalid percent encoded data is allowed to appear regardless.
2031 */
2032 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
2033 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2034
2035 if(**ptr != '?') {
2036 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
2037 return TRUE;
2038 }
2039
2040 data->query = *ptr;
2041
2042 ++(*ptr);
2043 while(**ptr && **ptr != '#') {
2044 if(**ptr == '%' && known_scheme &&
2045 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2046 if(!check_pct_encoded(ptr)) {
2047 *ptr = data->query;
2048 data->query = NULL;
2049 return FALSE;
2050 } else
2051 continue;
2052 }
2053
2054 ++(*ptr);
2055 }
2056
2057 data->query_len = *ptr - data->query;
2058
2059 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
2060 debugstr_wn(data->query, data->query_len), data->query_len);
2061 return TRUE;
2062 }
2063
2064 /* Attempts to parse the fragment from the URI.
2065 *
2066 * NOTES:
2067 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
2068 * data is allowed to appear in the query string. For unknown scheme types
2069 * invalid percent encoded data is allowed to appear regardless.
2070 */
2071 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
2072 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2073
2074 if(**ptr != '#') {
2075 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
2076 return TRUE;
2077 }
2078
2079 data->fragment = *ptr;
2080
2081 ++(*ptr);
2082 while(**ptr) {
2083 if(**ptr == '%' && known_scheme &&
2084 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2085 if(!check_pct_encoded(ptr)) {
2086 *ptr = data->fragment;
2087 data->fragment = NULL;
2088 return FALSE;
2089 } else
2090 continue;
2091 }
2092
2093 ++(*ptr);
2094 }
2095
2096 data->fragment_len = *ptr - data->fragment;
2097
2098 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
2099 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
2100 return TRUE;
2101 }
2102
2103 /* Parses and validates the components of the specified by data->uri
2104 * and stores the information it parses into 'data'.
2105 *
2106 * Returns TRUE if it successfully parsed the URI. False otherwise.
2107 */
2108 static BOOL parse_uri(parse_data *data, DWORD flags) {
2109 const WCHAR *ptr;
2110 const WCHAR **pptr;
2111
2112 ptr = data->uri;
2113 pptr = &ptr;
2114
2115 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
2116
2117 if(!parse_scheme(pptr, data, flags, 0))
2118 return FALSE;
2119
2120 if(!parse_hierpart(pptr, data, flags))
2121 return FALSE;
2122
2123 if(!parse_query(pptr, data, flags))
2124 return FALSE;
2125
2126 if(!parse_fragment(pptr, data, flags))
2127 return FALSE;
2128
2129 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
2130 return TRUE;
2131 }
2132
2133 static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2134 const WCHAR *ptr;
2135
2136 if(!data->username) {
2137 uri->userinfo_start = -1;
2138 return TRUE;
2139 }
2140
2141 uri->userinfo_start = uri->canon_len;
2142 for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) {
2143 if(*ptr == '%') {
2144 /* Only decode % encoded values for known scheme types. */
2145 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2146 /* See if the value really needs decoding. */
2147 WCHAR val = decode_pct_val(ptr);
2148 if(is_unreserved(val)) {
2149 if(!computeOnly)
2150 uri->canon_uri[uri->canon_len] = val;
2151
2152 ++uri->canon_len;
2153
2154 /* Move pass the hex characters. */
2155 ptr += 2;
2156 continue;
2157 }
2158 }
2159 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2160 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2161 * is NOT set.
2162 */
2163 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2164 if(!computeOnly)
2165 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2166
2167 uri->canon_len += 3;
2168 continue;
2169 }
2170 }
2171
2172 if(!computeOnly)
2173 /* Nothing special, so just copy the character over. */
2174 uri->canon_uri[uri->canon_len] = *ptr;
2175 ++uri->canon_len;
2176 }
2177
2178 return TRUE;
2179 }
2180
2181 static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2182 const WCHAR *ptr;
2183
2184 if(!data->password) {
2185 uri->userinfo_split = -1;
2186 return TRUE;
2187 }
2188
2189 if(uri->userinfo_start == -1)
2190 /* Has a password, but, doesn't have a username. */
2191 uri->userinfo_start = uri->canon_len;
2192
2193 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
2194
2195 /* Add the ':' to the userinfo component. */
2196 if(!computeOnly)
2197 uri->canon_uri[uri->canon_len] = ':';
2198 ++uri->canon_len;
2199
2200 for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) {
2201 if(*ptr == '%') {
2202 /* Only decode % encoded values for known scheme types. */
2203 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2204 /* See if the value really needs decoding. */
2205 WCHAR val = decode_pct_val(ptr);
2206 if(is_unreserved(val)) {
2207 if(!computeOnly)
2208 uri->canon_uri[uri->canon_len] = val;
2209
2210 ++uri->canon_len;
2211
2212 /* Move pass the hex characters. */
2213 ptr += 2;
2214 continue;
2215 }
2216 }
2217 } else if(!is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') {
2218 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
2219 * is NOT set.
2220 */
2221 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2222 if(!computeOnly)
2223 pct_encode_val(*ptr, uri->canon_uri + uri->canon_len);
2224
2225 uri->canon_len += 3;
2226 continue;
2227 }
2228 }
2229
2230 if(!computeOnly)
2231 /* Nothing special, so just copy the character over. */
2232 uri->canon_uri[uri->canon_len] = *ptr;
2233 ++uri->canon_len;
2234 }
2235
2236 return TRUE;
2237 }
2238
2239 /* Canonicalizes the userinfo of the URI represented by the parse_data.
2240 *
2241 * Canonicalization of the userinfo is a simple process. If there are any percent
2242 * encoded characters that fall in the "unreserved" character set, they are decoded
2243 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
2244 * then it is percent encoded. Other than that the characters are copied over without
2245 * change.
2246 */
2247 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2248 uri->userinfo_start = uri->userinfo_split = -1;
2249 uri->userinfo_len = 0;
2250
2251 if(!data->username && !data->password)
2252 /* URI doesn't have userinfo, so nothing to do here. */
2253 return TRUE;
2254
2255 if(!canonicalize_username(data, uri, flags, computeOnly))
2256 return FALSE;
2257
2258 if(!canonicalize_password(data, uri, flags, computeOnly))
2259 return FALSE;
2260
2261 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
2262 if(!computeOnly)
2263 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
2264 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
2265 uri->userinfo_split, uri->userinfo_len);
2266
2267 /* Now insert the '@' after the userinfo. */
2268 if(!computeOnly)
2269 uri->canon_uri[uri->canon_len] = '@';
2270 ++uri->canon_len;
2271
2272 return TRUE;
2273 }
2274
2275 /* Attempts to canonicalize a reg_name.
2276 *
2277 * Things that happen:
2278 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
2279 * lower cased. Unless it's an unknown scheme type, which case it's
2280 * no lower cased regardless.
2281 *
2282 * 2) Unreserved % encoded characters are decoded for known
2283 * scheme types.
2284 *
2285 * 3) Forbidden characters are % encoded as long as
2286 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
2287 * it isn't an unknown scheme type.
2288 *
2289 * 4) If it's a file scheme and the host is "localhost" it's removed.
2290 *
2291 * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set,
2292 * then the UNC path characters are added before the host name.
2293 */
2294 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
2295 DWORD flags, BOOL computeOnly) {
2296 static const WCHAR localhostW[] =
2297 {'l','o','c','a','l','h','o','s','t',0};
2298 const WCHAR *ptr;
2299 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2300
2301 if(data->scheme_type == URL_SCHEME_FILE &&
2302 data->host_len == lstrlenW(localhostW)) {
2303 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
2304 uri->host_start = -1;
2305 uri->host_len = 0;
2306 uri->host_type = Uri_HOST_UNKNOWN;
2307 return TRUE;
2308 }
2309 }
2310
2311 if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) {
2312 if(!computeOnly) {
2313 uri->canon_uri[uri->canon_len] = '\\';
2314 uri->canon_uri[uri->canon_len+1] = '\\';
2315 }
2316 uri->canon_len += 2;
2317 uri->authority_start = uri->canon_len;
2318 }
2319
2320 uri->host_start = uri->canon_len;
2321
2322 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
2323 if(*ptr == '%' && known_scheme) {
2324 WCHAR val = decode_pct_val(ptr);
2325 if(is_unreserved(val)) {
2326 /* If NO_CANONICALIZE is not set, then windows lower cases the
2327 * decoded value.
2328 */
2329 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
2330 if(!computeOnly)
2331 uri->canon_uri[uri->canon_len] = tolowerW(val);
2332 } else {
2333 if(!computeOnly)
2334 uri->canon_uri[uri->canon_len] = val;
2335 }
2336 ++uri->canon_len;
2337
2338 /* Skip past the % encoded character. */
2339 ptr += 2;
2340 continue;
2341 } else {
2342 /* Just copy the % over. */
2343 if(!computeOnly)
2344 uri->canon_uri[uri->canon_len] = *ptr;
2345 ++uri->canon_len;
2346 }
2347 } else if(*ptr == '\\') {
2348 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2349 if(!computeOnly)
2350 uri->canon_uri[uri->canon_len] = *ptr;
2351 ++uri->canon_len;
2352 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2353 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2354 if(!computeOnly) {
2355 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2356
2357 /* The percent encoded value gets lower cased also. */
2358 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2359 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2360 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2361 }
2362 }
2363
2364 uri->canon_len += 3;
2365 } else {
2366 if(!computeOnly) {
2367 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2368 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2369 else
2370 uri->canon_uri[uri->canon_len] = *ptr;
2371 }
2372
2373 ++uri->canon_len;
2374 }
2375 }
2376
2377 uri->host_len = uri->canon_len - uri->host_start;
2378
2379 if(!computeOnly)
2380 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2381 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2382 uri->host_len);
2383
2384 if(!computeOnly)
2385 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2386 &(uri->domain_offset));
2387
2388 return TRUE;
2389 }
2390
2391 /* Attempts to canonicalize an implicit IPv4 address. */
2392 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2393 uri->host_start = uri->canon_len;
2394
2395 TRACE("%u\n", data->implicit_ipv4);
2396 /* For unknown scheme types Windows doesn't convert
2397 * the value into an IP address, but it still considers
2398 * it an IPv4 address.
2399 */
2400 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2401 if(!computeOnly)
2402 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2403 uri->canon_len += data->host_len;
2404 } else {
2405 if(!computeOnly)
2406 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2407 else
2408 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2409 }
2410
2411 uri->host_len = uri->canon_len - uri->host_start;
2412 uri->host_type = Uri_HOST_IPV4;
2413
2414 if(!computeOnly)
2415 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2416 data, uri, flags, computeOnly,
2417 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2418 uri->host_len);
2419
2420 return TRUE;
2421 }
2422
2423 /* Attempts to canonicalize an IPv4 address.
2424 *
2425 * If the parse_data represents a URI that has an implicit IPv4 address
2426 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2427 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2428 * for an IPv4 address) it's canonicalized as if it were a reg-name.
2429 *
2430 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2431 * A partial IPv4 address is something like "192.0" and would be normalized to
2432 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2433 * be normalized to "192.2.1.3".
2434 *
2435 * NOTES:
2436 * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't
2437 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2438 * the original URI into the canonicalized URI, but, it still recognizes URI's
2439 * host type as HOST_IPV4.
2440 */
2441 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2442 if(data->has_implicit_ip)
2443 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2444 else {
2445 uri->host_start = uri->canon_len;
2446
2447 /* Windows only normalizes for known scheme types. */
2448 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2449 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2450 DWORD i, octetDigitCount = 0, octetCount = 0;
2451 BOOL octetHasDigit = FALSE;
2452
2453 for(i = 0; i < data->host_len; ++i) {
2454 if(data->host[i] == '0' && !octetHasDigit) {
2455 /* Can ignore leading zeros if:
2456 * 1) It isn't the last digit of the octet.
2457 * 2) i+1 != data->host_len
2458 * 3) i+1 != '.'
2459 */
2460 if(octetDigitCount == 2 ||
2461 i+1 == data->host_len ||
2462 data->host[i+1] == '.') {
2463 if(!computeOnly)
2464 uri->canon_uri[uri->canon_len] = data->host[i];
2465 ++uri->canon_len;
2466 TRACE("Adding zero\n");
2467 }
2468 } else if(data->host[i] == '.') {
2469 if(!computeOnly)
2470 uri->canon_uri[uri->canon_len] = data->host[i];
2471 ++uri->canon_len;
2472
2473 octetDigitCount = 0;
2474 octetHasDigit = FALSE;
2475 ++octetCount;
2476 } else {
2477 if(!computeOnly)
2478 uri->canon_uri[uri->canon_len] = data->host[i];
2479 ++uri->canon_len;
2480
2481 ++octetDigitCount;
2482 octetHasDigit = TRUE;
2483 }
2484 }
2485
2486 /* Make sure the canonicalized IP address has 4 dec-octets.
2487 * If doesn't add "0" ones until there is 4;
2488 */
2489 for( ; octetCount < 3; ++octetCount) {
2490 if(!computeOnly) {
2491 uri->canon_uri[uri->canon_len] = '.';
2492 uri->canon_uri[uri->canon_len+1] = '0';
2493 }
2494
2495 uri->canon_len += 2;
2496 }
2497 } else {
2498 /* Windows doesn't normalize addresses in unknown schemes. */
2499 if(!computeOnly)
2500 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2501 uri->canon_len += data->host_len;
2502 }
2503
2504 uri->host_len = uri->canon_len - uri->host_start;
2505 if(!computeOnly)
2506 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2507 data, uri, flags, computeOnly,
2508 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2509 uri->host_len);
2510 }
2511
2512 return TRUE;
2513 }
2514
2515 /* Attempts to canonicalize the IPv6 address of the URI.
2516 *
2517 * Multiple things happen during the canonicalization of an IPv6 address:
2518 * 1) Any leading zero's in a h16 component are removed.
2519 * Ex: [0001:0022::] -> [1:22::]
2520 *
2521 * 2) The longest sequence of zero h16 components are compressed
2522 * into a "::" (elision). If there's a tie, the first is chosen.
2523 *
2524 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2525 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2526 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2527 *
2528 * 3) If an IPv4 address is attached to the IPv6 address, it's
2529 * also normalized.
2530 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2531 *
2532 * 4) If an elision is present, but, only represents one h16 component
2533 * it's expanded.
2534 *
2535 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2536 *
2537 * 5) If the IPv6 address contains an IPv4 address and there exists
2538 * at least 1 non-zero h16 component the IPv4 address is converted
2539 * into two h16 components, otherwise it's normalized and kept as is.
2540 *
2541 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2542 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2543 *
2544 * NOTE:
2545 * For unknown scheme types Windows simply copies the address over without any
2546 * changes.
2547 *
2548 * IPv4 address can be included in an elision if all its components are 0's.
2549 */
2550 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2551 DWORD flags, BOOL computeOnly) {
2552 uri->host_start = uri->canon_len;
2553
2554 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2555 if(!computeOnly)
2556 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2557 uri->canon_len += data->host_len;
2558 } else {
2559 USHORT values[8];
2560 INT elision_start;
2561 DWORD i, elision_len;
2562
2563 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2564 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2565 data, uri, flags, computeOnly);
2566 return FALSE;
2567 }
2568
2569 if(!computeOnly)
2570 uri->canon_uri[uri->canon_len] = '[';
2571 ++uri->canon_len;
2572
2573 /* Find where the elision should occur (if any). */
2574 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2575
2576 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2577 computeOnly, elision_start, elision_len);
2578
2579 for(i = 0; i < 8; ++i) {
2580 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2581 i < elision_start+elision_len);
2582 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2583 data->ipv6_address.h16_count == 0);
2584
2585 if(i == elision_start) {
2586 if(!computeOnly) {
2587 uri->canon_uri[uri->canon_len] = ':';
2588 uri->canon_uri[uri->canon_len+1] = ':';
2589 }
2590 uri->canon_len += 2;
2591 }
2592
2593 /* We can ignore the current component if we're in the elision. */
2594 if(in_elision)
2595 continue;
2596
2597 /* We only add a ':' if we're not at i == 0, or when we're at
2598 * the very end of elision range since the ':' colon was handled
2599 * earlier. Otherwise we would end up with ":::" after elision.
2600 */
2601 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2602 if(!computeOnly)
2603 uri->canon_uri[uri->canon_len] = ':';
2604 ++uri->canon_len;
2605 }
2606
2607 if(do_ipv4) {
2608 UINT val;
2609 DWORD len;
2610
2611 /* Combine the two parts of the IPv4 address values. */
2612 val = values[i];
2613 val <<= 16;
2614 val += values[i+1];
2615
2616 if(!computeOnly)
2617 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2618 else
2619 len = ui2ipv4(NULL, val);
2620
2621 uri->canon_len += len;
2622 ++i;
2623 } else {
2624 /* Write a regular h16 component to the URI. */
2625
2626 /* Short circuit for the trivial case. */
2627 if(values[i] == 0) {
2628 if(!computeOnly)
2629 uri->canon_uri[uri->canon_len] = '0';
2630 ++uri->canon_len;
2631 } else {
2632 static const WCHAR formatW[] = {'%','x',0};
2633
2634 if(!computeOnly)
2635 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2636 formatW, values[i]);
2637 else {
2638 WCHAR tmp[5];
2639 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2640 }
2641 }
2642 }
2643 }
2644
2645 /* Add the closing ']'. */
2646 if(!computeOnly)
2647 uri->canon_uri[uri->canon_len] = ']';
2648 ++uri->canon_len;
2649 }
2650
2651 uri->host_len = uri->canon_len - uri->host_start;
2652
2653 if(!computeOnly)
2654 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2655 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2656 uri->host_len);
2657
2658 return TRUE;
2659 }
2660
2661 /* Attempts to canonicalize the host of the URI (if any). */
2662 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2663 uri->host_start = -1;
2664 uri->host_len = 0;
2665 uri->domain_offset = -1;
2666
2667 if(data->host) {
2668 switch(data->host_type) {
2669 case Uri_HOST_DNS:
2670 uri->host_type = Uri_HOST_DNS;
2671 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2672 return FALSE;
2673
2674 break;
2675 case Uri_HOST_IPV4:
2676 uri->host_type = Uri_HOST_IPV4;
2677 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2678 return FALSE;
2679
2680 break;
2681 case Uri_HOST_IPV6:
2682 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2683 return FALSE;
2684
2685 uri->host_type = Uri_HOST_IPV6;
2686 break;
2687 case Uri_HOST_UNKNOWN:
2688 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2689 uri->host_start = uri->canon_len;
2690
2691 /* Nothing happens to unknown host types. */
2692 if(!computeOnly)
2693 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2694 uri->canon_len += data->host_len;
2695 uri->host_len = data->host_len;
2696 }
2697
2698 uri->host_type = Uri_HOST_UNKNOWN;
2699 break;
2700 default:
2701 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2702 uri, flags, computeOnly, data->host_type);
2703 return FALSE;
2704 }
2705 }
2706
2707 return TRUE;
2708 }
2709
2710 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2711 BOOL has_default_port = FALSE;
2712 USHORT default_port = 0;
2713 DWORD i;
2714
2715 uri->port_offset = -1;
2716
2717 /* Check if the scheme has a default port. */
2718 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2719 if(default_ports[i].scheme == data->scheme_type) {
2720 has_default_port = TRUE;
2721 default_port = default_ports[i].port;
2722 break;
2723 }
2724 }
2725
2726 uri->has_port = data->has_port || has_default_port;
2727
2728 /* Possible cases:
2729 * 1) Has a port which is the default port.
2730 * 2) Has a port (not the default).
2731 * 3) Doesn't have a port, but, scheme has a default port.
2732 * 4) No port.
2733 */
2734 if(has_default_port && data->has_port && data->port_value == default_port) {
2735 /* If it's the default port and this flag isn't set, don't do anything. */
2736 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2737 uri->port_offset = uri->canon_len-uri->authority_start;
2738 if(!computeOnly)
2739 uri->canon_uri[uri->canon_len] = ':';
2740 ++uri->canon_len;
2741
2742 if(data->port) {
2743 /* Copy the original port over. */
2744 if(!computeOnly)
2745 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2746 uri->canon_len += data->port_len;
2747 } else {
2748 if(!computeOnly)
2749 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2750 else
2751 uri->canon_len += ui2str(NULL, data->port_value);
2752 }
2753 }
2754
2755 uri->port = default_port;
2756 } else if(data->has_port) {
2757 uri->port_offset = uri->canon_len-uri->authority_start;
2758 if(!computeOnly)
2759 uri->canon_uri[uri->canon_len] = ':';
2760 ++uri->canon_len;
2761
2762 if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) {
2763 /* Copy the original over without changes. */
2764 if(!computeOnly)
2765 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2766 uri->canon_len += data->port_len;
2767 } else {
2768 if(!computeOnly)
2769 uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value);
2770 else
2771 uri->canon_len += ui2str(NULL, data->port_value);
2772 }
2773
2774 uri->port = data->port_value;
2775 } else if(has_default_port)
2776 uri->port = default_port;
2777
2778 return TRUE;
2779 }
2780
2781 /* Canonicalizes the authority of the URI represented by the parse_data. */
2782 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2783 uri->authority_start = uri->canon_len;
2784 uri->authority_len = 0;
2785
2786 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2787 return FALSE;
2788
2789 if(!canonicalize_host(data, uri, flags, computeOnly))
2790 return FALSE;
2791
2792 if(!canonicalize_port(data, uri, flags, computeOnly))
2793 return FALSE;
2794
2795 if(uri->host_start != -1 || (data->is_relative && (data->password || data->username)))
2796 uri->authority_len = uri->canon_len - uri->authority_start;
2797 else
2798 uri->authority_start = -1;
2799
2800 return TRUE;
2801 }
2802
2803 /* Attempts to canonicalize the path of a hierarchical URI.
2804 *
2805 * Things that happen:
2806 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2807 * flag is set or it's a file URI. Forbidden characters are always encoded
2808 * for file schemes regardless and forbidden characters are never encoded
2809 * for unknown scheme types.
2810 *
2811 * 2). For known scheme types '\\' are changed to '/'.
2812 *
2813 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2814 * Unless the scheme type is unknown. For file schemes any percent encoded
2815 * character in the unreserved or reserved set is decoded.
2816 *
2817 * 4). For File schemes if the path is starts with a drive letter and doesn't
2818 * start with a '/' then one is appended.
2819 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2820 *
2821 * 5). Dot segments are removed from the path for all scheme types
2822 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2823 * for wildcard scheme types.
2824 *
2825 * NOTES:
2826 * file://c:/test%20test -> file:///c:/test%2520test
2827 * file://c:/test%3Etest -> file:///c:/test%253Etest
2828 * if Uri_CREATE_FILE_USE_DOS_PATH is not set:
2829 * file:///c:/test%20test -> file:///c:/test%20test
2830 * file:///c:/test%test -> file:///c:/test%25test
2831 */
2832 static DWORD canonicalize_path_hierarchical(const WCHAR *path, DWORD path_len, URL_SCHEME scheme_type, BOOL has_host, DWORD flags,
2833 WCHAR *ret_path) {
2834 const BOOL known_scheme = scheme_type != URL_SCHEME_UNKNOWN;
2835 const BOOL is_file = scheme_type == URL_SCHEME_FILE;
2836 const BOOL is_res = scheme_type == URL_SCHEME_RES;
2837 const WCHAR *ptr;
2838 BOOL escape_pct = FALSE;
2839 DWORD len = 0;
2840
2841 if(!path)
2842 return 0;
2843
2844 ptr = path;
2845
2846 if(is_file && !has_host) {
2847 /* Check if a '/' needs to be appended for the file scheme. */
2848 if(path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2849 if(ret_path)
2850 ret_path[len] = '/';
2851 len++;
2852 escape_pct = TRUE;
2853 } else if(*ptr == '/') {
2854 if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2855 /* Copy the extra '/' over. */
2856 if(ret_path)
2857 ret_path[len] = '/';
2858 len++;
2859 }
2860 ++ptr;
2861 }
2862
2863 if(is_drive_path(ptr)) {
2864 if(ret_path) {
2865 ret_path[len] = *ptr;
2866 /* If there's a '|' after the drive letter, convert it to a ':'. */
2867 ret_path[len+1] = ':';
2868 }
2869 ptr += 2;
2870 len += 2;
2871 }
2872 }
2873
2874 if(!is_file && *path && *path != '/') {
2875 /* Prepend a '/' to the path if it doesn't have one. */
2876 if(ret_path)
2877 ret_path[len] = '/';
2878 len++;
2879 }
2880
2881 for(; ptr < path+path_len; ++ptr) {
2882 BOOL do_default_action = TRUE;
2883
2884 if(*ptr == '%' && !is_res) {
2885 const WCHAR *tmp = ptr;
2886 WCHAR val;
2887
2888 /* Check if the % represents a valid encoded char, or if it needs encoding. */
2889 BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH);
2890 val = decode_pct_val(ptr);
2891
2892 if(force_encode || escape_pct) {
2893 /* Escape the percent sign in the file URI. */
2894 if(ret_path)
2895 pct_encode_val(*ptr, ret_path+len);
2896 len += 3;
2897 do_default_action = FALSE;
2898 } else if((is_unreserved(val) && known_scheme) ||
2899 (is_file && (is_unreserved(val) || is_reserved(val) ||
2900 (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) {
2901 if(ret_path)
2902 ret_path[len] = val;
2903 len++;
2904
2905 ptr += 2;
2906 continue;
2907 }
2908 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
2909 /* Convert the '/' back to a '\\'. */
2910 if(ret_path)
2911 ret_path[len] = '\\';
2912 len++;
2913 do_default_action = FALSE;
2914 } else if(*ptr == '\\' && known_scheme) {
2915 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2916 /* Convert '\\' into a '/'. */
2917 if(ret_path)
2918 ret_path[len] = '/';
2919 len++;
2920 do_default_action = FALSE;
2921 }
2922 } else if(known_scheme && !is_res && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2923 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2924 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
2925 /* Escape the forbidden character. */
2926 if(ret_path)
2927 pct_encode_val(*ptr, ret_path+len);
2928 len += 3;
2929 do_default_action = FALSE;
2930 }
2931 }
2932
2933 if(do_default_action) {
2934 if(ret_path)
2935 ret_path[len] = *ptr;
2936 len++;
2937 }
2938 }
2939
2940 /* Removing the dot segments only happens when it's not in
2941 * computeOnly mode and it's not a wildcard scheme. File schemes
2942 * with USE_DOS_PATH set don't get dot segments removed.
2943 */
2944 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) &&
2945 scheme_type != URL_SCHEME_WILDCARD) {
2946 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && ret_path) {
2947 /* Remove the dot segments (if any) and reset everything to the new
2948 * correct length.
2949 */
2950 len = remove_dot_segments(ret_path, len);
2951 }
2952 }
2953
2954 if(ret_path)
2955 TRACE("Canonicalized path %s len=%d\n", debugstr_wn(ret_path, len), len);
2956 return len;
2957 }
2958
2959 /* Attempts to canonicalize the path for an opaque URI.
2960 *
2961 * For known scheme types:
2962 * 1) forbidden characters are percent encoded if
2963 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2964 *
2965 * 2) Percent encoded, unreserved characters are decoded
2966 * to their actual values, for known scheme types.
2967 *
2968 * 3) '\\' are changed to '/' for known scheme types
2969 * except for mailto schemes.
2970 *
2971 * 4) For file schemes, if USE_DOS_PATH is set all '/'
2972 * are converted to backslashes.
2973 *
2974 * 5) For file schemes, if USE_DOS_PATH isn't set all '\'
2975 * are converted to forward slashes.
2976 */
2977 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2978 const WCHAR *ptr;
2979 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2980 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2981 const BOOL is_mk = data->scheme_type == URL_SCHEME_MK;
2982
2983 if(!data->path) {
2984 uri->path_start = -1;
2985 uri->path_len = 0;
2986 return TRUE;
2987 }
2988
2989 uri->path_start = uri->canon_len;
2990
2991 if(is_mk){
2992 /* hijack this flag for SCHEME_MK to tell the function when to start
2993 * converting slashes */
2994 flags |= Uri_CREATE_FILE_USE_DOS_PATH;
2995 }
2996
2997 /* For javascript: URIs, simply copy path part without any canonicalization */
2998 if(data->scheme_type == URL_SCHEME_JAVASCRIPT) {
2999 if(!computeOnly)
3000 memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR));
3001 uri->path_len = data->path_len;
3002 uri->canon_len += data->path_len;
3003 return TRUE;
3004 }
3005
3006 /* Windows doesn't allow a "//" to appear after the scheme
3007 * of a URI, if it's an opaque URI.
3008 */
3009 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
3010 /* So it inserts a "/." before the "//" if it exists. */
3011 if(!computeOnly) {
3012 uri->canon_uri[uri->canon_len] = '/';
3013 uri->canon_uri[uri->canon_len+1] = '.';
3014 }
3015
3016 uri->canon_len += 2;
3017 }
3018
3019 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
3020 BOOL do_default_action = TRUE;
3021
3022 if(*ptr == '%' && known_scheme) {
3023 WCHAR val = decode_pct_val(ptr);
3024
3025 if(is_unreserved(val)) {
3026 if(!computeOnly)
3027 uri->canon_uri[uri->canon_len] = val;
3028 ++uri->canon_len;
3029
3030 ptr += 2;
3031 continue;
3032 }
3033 } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3034 if(!computeOnly)
3035 uri->canon_uri[uri->canon_len] = '\\';
3036 ++uri->canon_len;
3037 do_default_action = FALSE;
3038 } else if(*ptr == '\\') {
3039 if((data->is_relative || is_mk || is_file) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) {
3040 /* Convert to a '/'. */
3041 if(!computeOnly)
3042 uri->canon_uri[uri->canon_len] = '/';
3043 ++uri->canon_len;
3044 do_default_action = FALSE;
3045 }
3046 } else if(is_mk && *ptr == ':' && ptr + 1 < data->path + data->path_len && *(ptr + 1) == ':') {
3047 flags &= ~Uri_CREATE_FILE_USE_DOS_PATH;
3048 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
3049 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
3050 if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) {
3051 if(!computeOnly)
3052 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3053 uri->canon_len += 3;
3054 do_default_action = FALSE;
3055 }
3056 }
3057
3058 if(do_default_action) {
3059 if(!computeOnly)
3060 uri->canon_uri[uri->canon_len] = *ptr;
3061 ++uri->canon_len;
3062 }
3063 }
3064
3065 if(is_mk && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) {
3066 DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start,
3067 uri->canon_len - uri->path_start);
3068 uri->canon_len = uri->path_start + new_len;
3069 }
3070
3071 uri->path_len = uri->canon_len - uri->path_start;
3072
3073 if(!computeOnly)
3074 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
3075 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
3076 return TRUE;
3077 }
3078
3079 /* Determines how the URI represented by the parse_data should be canonicalized.
3080 *
3081 * Essentially, if the parse_data represents an hierarchical URI then it calls
3082 * canonicalize_authority and the canonicalization functions for the path. If the
3083 * URI is opaque it canonicalizes the path of the URI.
3084 */
3085 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3086 if(!data->is_opaque || (data->is_relative && (data->password || data->username))) {
3087 /* "//" is only added for non-wildcard scheme types.
3088 *
3089 * A "//" is only added to a relative URI if it has a
3090 * host or port component (this only happens if a IUriBuilder
3091 * is generating an IUri).
3092 */
3093 if((data->is_relative && (data->host || data->has_port)) ||
3094 (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) {
3095 if(data->scheme_type == URL_SCHEME_WILDCARD)
3096 FIXME("Here\n");
3097
3098 if(!computeOnly) {
3099 INT pos = uri->canon_len;
3100
3101 uri->canon_uri[pos] = '/';
3102 uri->canon_uri[pos+1] = '/';
3103 }
3104 uri->canon_len += 2;
3105 }
3106
3107 if(!canonicalize_authority(data, uri, flags, computeOnly))
3108 return FALSE;
3109
3110 if(data->is_relative && (data->password || data->username)) {
3111 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3112 return FALSE;
3113 } else {
3114 if(!computeOnly)
3115 uri->path_start = uri->canon_len;
3116 uri->path_len = canonicalize_path_hierarchical(data->path, data->path_len, data->scheme_type, data->host_len != 0,
3117 flags, computeOnly ? NULL : uri->canon_uri+uri->canon_len);
3118 uri->canon_len += uri->path_len;
3119 if(!computeOnly && !uri->path_len)
3120 uri->path_start = -1;
3121 }
3122 } else {
3123 /* Opaque URI's don't have an authority. */
3124 uri->userinfo_start = uri->userinfo_split = -1;
3125 uri->userinfo_len = 0;
3126 uri->host_start = -1;
3127 uri->host_len = 0;
3128 uri->host_type = Uri_HOST_UNKNOWN;
3129 uri->has_port = FALSE;
3130 uri->authority_start = -1;
3131 uri->authority_len = 0;
3132 uri->domain_offset = -1;
3133 uri->port_offset = -1;
3134
3135 if(is_hierarchical_scheme(data->scheme_type)) {
3136 DWORD i;
3137
3138 /* Absolute URIs aren't displayed for known scheme types
3139 * which should be hierarchical URIs.
3140 */
3141 uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI;
3142
3143 /* Windows also sets the port for these (if they have one). */
3144 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
3145 if(data->scheme_type == default_ports[i].scheme) {
3146 uri->has_port = TRUE;
3147 uri->port = default_ports[i].port;
3148 break;
3149 }
3150 }
3151 }
3152
3153 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
3154 return FALSE;
3155 }
3156
3157 if(uri->path_start > -1 && !computeOnly)
3158 /* Finding file extensions happens for both types of URIs. */
3159 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
3160 else
3161 uri->extension_offset = -1;
3162
3163 return TRUE;
3164 }
3165
3166 /* Attempts to canonicalize the query string of the URI.
3167 *
3168 * Things that happen:
3169 * 1) For known scheme types forbidden characters
3170 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
3171 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
3172 *
3173 * 2) For known scheme types, percent encoded, unreserved characters
3174 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
3175 */
3176 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
3177 const WCHAR *ptr, *end;
3178 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
3179
3180 if(!data->query) {
3181 uri->query_start = -1;
3182 uri->query_len = 0;
3183 return TRUE;
3184 }
3185
3186 uri->query_start = uri->canon_len;
3187
3188 end = data->query+data->query_len;
3189 for(ptr = data->query; ptr < end; ++ptr) {
3190 if(*ptr == '%') {
3191 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3192 WCHAR val = decode_pct_val(ptr);
3193 if(is_unreserved(val)) {
3194 if(!computeOnly)
3195 uri->canon_uri[uri->canon_len] = val;
3196 ++uri->canon_len;
3197
3198 ptr += 2;
3199 continue;
3200 }
3201 }
3202 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
3203 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
3204 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
3205 if(!computeOnly)
3206 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
3207 uri->canon_len += 3;
3208 continue;
3209 }
3210 }
3211
3212 if(!computeOnly)
3213 uri->canon_uri[uri->canon_len] = *ptr;
3214 ++uri->canon_len;
3215 }
3216
3217 uri->query_len = uri->canon_len - uri->query_start;
3218
3219 if(!computeOnly)
3220 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
3221 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
3222 uri->query_len);
3223 return TRUE;
3224 }
3225
3226