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