urlmon: Implemented IUri_GetProperties and IUri_HasProperty.
[wine.git] / dlls / urlmon / uri.c
blobc4aa1b0d2b03c4e1a0d5328289afc1c1d310fe36
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
3 * Copyright 2010 Thomas Mullaly
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.
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.
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
20 #include "urlmon_main.h"
21 #include "wine/debug.h"
23 #define NO_SHLWAPI_REG
24 #include "shlwapi.h"
26 #define UINT_MAX 0xffffffff
27 #define USHORT_MAX 0xffff
29 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
31 typedef struct {
32 const IUriVtbl *lpIUriVtbl;
33 LONG ref;
35 BSTR raw_uri;
37 /* Information about the canonicalized URI's buffer. */
38 WCHAR *canon_uri;
39 DWORD canon_size;
40 DWORD canon_len;
42 INT scheme_start;
43 DWORD scheme_len;
44 URL_SCHEME scheme_type;
46 INT userinfo_start;
47 DWORD userinfo_len;
48 INT userinfo_split;
50 INT host_start;
51 DWORD host_len;
52 Uri_HOST_TYPE host_type;
54 USHORT port;
55 BOOL has_port;
57 INT authority_start;
58 DWORD authority_len;
60 INT domain_offset;
62 INT path_start;
63 DWORD path_len;
64 INT extension_offset;
66 INT query_start;
67 DWORD query_len;
69 INT fragment_start;
70 DWORD fragment_len;
71 } Uri;
73 typedef struct {
74 const IUriBuilderVtbl *lpIUriBuilderVtbl;
75 LONG ref;
76 } UriBuilder;
78 typedef struct {
79 const WCHAR *str;
80 DWORD len;
81 } h16;
83 typedef struct {
84 /* IPv6 addresses can hold up to 8 h16 components. */
85 h16 components[8];
86 DWORD h16_count;
88 /* An IPv6 can have 1 elision ("::"). */
89 const WCHAR *elision;
91 /* An IPv6 can contain 1 IPv4 address as the last 32bits of the address. */
92 const WCHAR *ipv4;
93 DWORD ipv4_len;
95 INT components_size;
96 INT elision_size;
97 } ipv6_address;
99 typedef struct {
100 BSTR uri;
102 BOOL is_relative;
103 BOOL is_opaque;
104 BOOL has_implicit_scheme;
105 BOOL has_implicit_ip;
106 UINT implicit_ipv4;
108 const WCHAR *scheme;
109 DWORD scheme_len;
110 URL_SCHEME scheme_type;
112 const WCHAR *userinfo;
113 DWORD userinfo_len;
114 INT userinfo_split;
116 const WCHAR *host;
117 DWORD host_len;
118 Uri_HOST_TYPE host_type;
120 BOOL has_ipv6;
121 ipv6_address ipv6_address;
123 const WCHAR *port;
124 DWORD port_len;
125 USHORT port_value;
127 const WCHAR *path;
128 DWORD path_len;
130 const WCHAR *query;
131 DWORD query_len;
133 const WCHAR *fragment;
134 DWORD fragment_len;
135 } parse_data;
137 static const CHAR hexDigits[] = "0123456789ABCDEF";
139 /* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */
140 static const struct {
141 URL_SCHEME scheme;
142 WCHAR scheme_name[16];
143 } recognized_schemes[] = {
144 {URL_SCHEME_FTP, {'f','t','p',0}},
145 {URL_SCHEME_HTTP, {'h','t','t','p',0}},
146 {URL_SCHEME_GOPHER, {'g','o','p','h','e','r',0}},
147 {URL_SCHEME_MAILTO, {'m','a','i','l','t','o',0}},
148 {URL_SCHEME_NEWS, {'n','e','w','s',0}},
149 {URL_SCHEME_NNTP, {'n','n','t','p',0}},
150 {URL_SCHEME_TELNET, {'t','e','l','n','e','t',0}},
151 {URL_SCHEME_WAIS, {'w','a','i','s',0}},
152 {URL_SCHEME_FILE, {'f','i','l','e',0}},
153 {URL_SCHEME_MK, {'m','k',0}},
154 {URL_SCHEME_HTTPS, {'h','t','t','p','s',0}},
155 {URL_SCHEME_SHELL, {'s','h','e','l','l',0}},
156 {URL_SCHEME_SNEWS, {'s','n','e','w','s',0}},
157 {URL_SCHEME_LOCAL, {'l','o','c','a','l',0}},
158 {URL_SCHEME_JAVASCRIPT, {'j','a','v','a','s','c','r','i','p','t',0}},
159 {URL_SCHEME_VBSCRIPT, {'v','b','s','c','r','i','p','t',0}},
160 {URL_SCHEME_ABOUT, {'a','b','o','u','t',0}},
161 {URL_SCHEME_RES, {'r','e','s',0}},
162 {URL_SCHEME_MSSHELLROOTED, {'m','s','-','s','h','e','l','l','-','r','o','o','t','e','d',0}},
163 {URL_SCHEME_MSSHELLIDLIST, {'m','s','-','s','h','e','l','l','-','i','d','l','i','s','t',0}},
164 {URL_SCHEME_MSHELP, {'h','c','p',0}},
165 {URL_SCHEME_WILDCARD, {'*',0}}
168 /* List of default ports Windows recognizes. */
169 static const struct {
170 URL_SCHEME scheme;
171 USHORT port;
172 } default_ports[] = {
173 {URL_SCHEME_FTP, 21},
174 {URL_SCHEME_HTTP, 80},
175 {URL_SCHEME_GOPHER, 70},
176 {URL_SCHEME_NNTP, 119},
177 {URL_SCHEME_TELNET, 23},
178 {URL_SCHEME_WAIS, 210},
179 {URL_SCHEME_HTTPS, 443},
182 /* List of 3 character top level domain names Windows seems to recognize.
183 * There might be more, but, these are the only ones I've found so far.
185 static const struct {
186 WCHAR tld_name[4];
187 } recognized_tlds[] = {
188 {{'c','o','m',0}},
189 {{'e','d','u',0}},
190 {{'g','o','v',0}},
191 {{'i','n','t',0}},
192 {{'m','i','l',0}},
193 {{'n','e','t',0}},
194 {{'o','r','g',0}}
197 static inline BOOL is_alpha(WCHAR val) {
198 return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z'));
201 static inline BOOL is_num(WCHAR val) {
202 return (val >= '0' && val <= '9');
205 /* A URI is implicitly a file path if it begins with
206 * a drive letter (eg X:) or starts with "\\" (UNC path).
208 static inline BOOL is_implicit_file_path(const WCHAR *str) {
209 if(is_alpha(str[0]) && str[1] == ':')
210 return TRUE;
211 else if(str[0] == '\\' && str[1] == '\\')
212 return TRUE;
214 return FALSE;
217 /* Checks if the URI is a hierarchical URI. A hierarchical
218 * URI is one that has "//" after the scheme.
220 static BOOL check_hierarchical(const WCHAR **ptr) {
221 const WCHAR *start = *ptr;
223 if(**ptr != '/')
224 return FALSE;
226 ++(*ptr);
227 if(**ptr != '/') {
228 *ptr = start;
229 return FALSE;
232 ++(*ptr);
233 return TRUE;
236 /* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */
237 static inline BOOL is_unreserved(WCHAR val) {
238 return (is_alpha(val) || is_num(val) || val == '-' || val == '.' ||
239 val == '_' || val == '~');
242 /* sub-delims = "!" / "$" / "&" / "'" / "(" / ")"
243 * / "*" / "+" / "," / ";" / "="
245 static inline BOOL is_subdelim(WCHAR val) {
246 return (val == '!' || val == '$' || val == '&' ||
247 val == '\'' || val == '(' || val == ')' ||
248 val == '*' || val == '+' || val == ',' ||
249 val == ';' || val == '=');
252 /* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */
253 static inline BOOL is_gendelim(WCHAR val) {
254 return (val == ':' || val == '/' || val == '?' ||
255 val == '#' || val == '[' || val == ']' ||
256 val == '@');
259 /* Characters that delimit the end of the authority
260 * section of a URI. Sometimes a '\\' is considered
261 * an authority delimeter.
263 static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) {
264 return (val == '#' || val == '/' || val == '?' ||
265 val == '\0' || (acceptSlash && val == '\\'));
268 /* reserved = gen-delims / sub-delims */
269 static inline BOOL is_reserved(WCHAR val) {
270 return (is_subdelim(val) || is_gendelim(val));
273 static inline BOOL is_hexdigit(WCHAR val) {
274 return ((val >= 'a' && val <= 'f') ||
275 (val >= 'A' && val <= 'F') ||
276 (val >= '0' && val <= '9'));
279 static inline BOOL is_path_delim(WCHAR val) {
280 return (!val || val == '#' || val == '?');
283 /* Computes the size of the given IPv6 address.
284 * Each h16 component is 16bits, if there is an IPv4 address, it's
285 * 32bits. If there's an elision it can be 16bits to 128bits, depending
286 * on the number of other components.
288 * Modeled after google-url's CheckIPv6ComponentsSize function
290 static void compute_ipv6_comps_size(ipv6_address *address) {
291 address->components_size = address->h16_count * 2;
293 if(address->ipv4)
294 /* IPv4 address is 4 bytes. */
295 address->components_size += 4;
297 if(address->elision) {
298 /* An elision can be anywhere from 2 bytes up to 16 bytes.
299 * It size depends on the size of the h16 and IPv4 components.
301 address->elision_size = 16 - address->components_size;
302 if(address->elision_size < 2)
303 address->elision_size = 2;
304 } else
305 address->elision_size = 0;
308 /* Taken from dlls/jscript/lex.c */
309 static int hex_to_int(WCHAR val) {
310 if(val >= '0' && val <= '9')
311 return val - '0';
312 else if(val >= 'a' && val <= 'f')
313 return val - 'a' + 10;
314 else if(val >= 'A' && val <= 'F')
315 return val - 'A' + 10;
317 return -1;
320 /* Helper function for converting a percent encoded string
321 * representation of a WCHAR value into its actual WCHAR value. If
322 * the two characters following the '%' aren't valid hex values then
323 * this function returns the NULL character.
325 * Eg.
326 * "%2E" will result in '.' being returned by this function.
328 static WCHAR decode_pct_val(const WCHAR *ptr) {
329 WCHAR ret = '\0';
331 if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) {
332 INT a = hex_to_int(*(ptr + 1));
333 INT b = hex_to_int(*(ptr + 2));
335 ret = a << 4;
336 ret += b;
339 return ret;
342 /* Helper function for percent encoding a given character
343 * and storing the encoded value into a given buffer (dest).
345 * It's up to the calling function to ensure that there is
346 * at least enough space in 'dest' for the percent encoded
347 * value to be stored (so dest + 3 spaces available).
349 static inline void pct_encode_val(WCHAR val, WCHAR *dest) {
350 dest[0] = '%';
351 dest[1] = hexDigits[(val >> 4) & 0xf];
352 dest[2] = hexDigits[val & 0xf];
355 /* Scans the range of characters [str, end] and returns the last occurence
356 * of 'ch' or returns NULL.
358 static const WCHAR *str_last_of(const WCHAR *str, const WCHAR *end, WCHAR ch) {
359 const WCHAR *ptr = end;
361 while(ptr >= str) {
362 if(*ptr == ch)
363 return ptr;
364 --ptr;
367 return NULL;
370 /* Attempts to parse the domain name from the host.
372 * This function also includes the Top-level Domain (TLD) name
373 * of the host when it tries to find the domain name. If it finds
374 * a valid domain name it will assign 'domain_start' the offset
375 * into 'host' where the domain name starts.
377 * It's implied that if a domain name its range is implied to be
378 * [host+domain_start, host+host_len).
380 static void find_domain_name(const WCHAR *host, DWORD host_len,
381 INT *domain_start) {
382 const WCHAR *last_tld, *sec_last_tld, *end;
384 end = host+host_len-1;
386 *domain_start = -1;
388 /* There has to be at least enough room for a '.' followed by a
389 * 3 character TLD for a domain to even exist in the host name.
391 if(host_len < 4)
392 return;
394 last_tld = str_last_of(host, end, '.');
395 if(!last_tld)
396 /* http://hostname -> has no domain name. */
397 return;
399 sec_last_tld = str_last_of(host, last_tld-1, '.');
400 if(!sec_last_tld) {
401 /* If the '.' is at the beginning of the host there
402 * has to be at least 3 characters in the TLD for it
403 * to be valid.
404 * Ex: .com -> .com as the domain name.
405 * .co -> has no domain name.
407 if(last_tld-host == 0) {
408 if(end-(last_tld-1) < 3)
409 return;
410 } else if(last_tld-host == 3) {
411 DWORD i;
413 /* If there's three characters in front of last_tld and
414 * they are on the list of recognized TLDs, then this
415 * host doesn't have a domain (since the host only contains
416 * a TLD name.
417 * Ex: edu.uk -> has no domain name.
418 * foo.uk -> foo.uk as the domain name.
420 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
421 if(!StrCmpNIW(host, recognized_tlds[i].tld_name, 3))
422 return;
424 } else if(last_tld-host < 3)
425 /* Anything less then 3 characters is considered part
426 * of the TLD name.
427 * Ex: ak.uk -> Has no domain name.
429 return;
431 /* Otherwise the domain name is the whole host name. */
432 *domain_start = 0;
433 } else if(end+1-last_tld > 3) {
434 /* If the last_tld has more then 3 characters then it's automatically
435 * considered the TLD of the domain name.
436 * Ex: www.winehq.org.uk.test -> uk.test as the domain name.
438 *domain_start = (sec_last_tld+1)-host;
439 } else if(last_tld - (sec_last_tld+1) < 4) {
440 DWORD i;
441 /* If the sec_last_tld is 3 characters long it HAS to be on the list of
442 * recognized to still be considered part of the TLD name, otherwise
443 * its considered the domain name.
444 * Ex: www.google.com.uk -> google.com.uk as the domain name.
445 * www.google.foo.uk -> foo.uk as the domain name.
447 if(last_tld - (sec_last_tld+1) == 3) {
448 for(i = 0; i < sizeof(recognized_tlds)/sizeof(recognized_tlds[0]); ++i) {
449 if(!StrCmpNIW(sec_last_tld+1, recognized_tlds[i].tld_name, 3)) {
450 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
452 if(!domain)
453 *domain_start = 0;
454 else
455 *domain_start = (domain+1) - host;
456 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
457 (host+host_len)-(host+*domain_start)));
458 return;
462 *domain_start = (sec_last_tld+1)-host;
463 } else {
464 /* Since the sec_last_tld is less then 3 characters it's considered
465 * part of the TLD.
466 * Ex: www.google.fo.uk -> google.fo.uk as the domain name.
468 const WCHAR *domain = str_last_of(host, sec_last_tld-1, '.');
470 if(!domain)
471 *domain_start = 0;
472 else
473 *domain_start = (domain+1) - host;
475 } else {
476 /* The second to last TLD has more then 3 characters making it
477 * the domain name.
478 * Ex: www.google.test.us -> test.us as the domain name.
480 *domain_start = (sec_last_tld+1)-host;
483 TRACE("Found domain name %s\n", debugstr_wn(host+*domain_start,
484 (host+host_len)-(host+*domain_start)));
487 /* Removes the dot segments from a heirarchical URIs path component. This
488 * function performs the removal in place.
490 * This is a modified version of Qt's QUrl function "removeDotsFromPath".
492 * This function returns the new length of the path string.
494 static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) {
495 WCHAR *out = path;
496 const WCHAR *in = out;
497 const WCHAR *end = out + path_len;
498 DWORD len;
500 while(in < end) {
501 /* A. if the input buffer begins with a prefix of "/./" or "/.",
502 * where "." is a complete path segment, then replace that
503 * prefix with "/" in the input buffer; otherwise,
505 if(in <= end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '/') {
506 in += 2;
507 continue;
508 } else if(in == end - 2 && in[0] == '/' && in[1] == '.') {
509 *out++ = '/';
510 in += 2;
511 break;
514 /* B. if the input buffer begins with a prefix of "/../" or "/..",
515 * where ".." is a complete path segment, then replace that
516 * prefix with "/" in the input buffer and remove the last
517 * segment and its preceding "/" (if any) from the output
518 * buffer; otherwise,
520 if(in <= end - 4 && in[0] == '/' && in[1] == '.' && in[2] == '.' && in[3] == '/') {
521 while(out > path && *(--out) != '/');
523 in += 3;
524 continue;
525 } else if(in == end - 3 && in[0] == '/' && in[1] == '.' && in[2] == '.') {
526 while(out > path && *(--out) != '/');
528 if(*out == '/')
529 ++out;
531 in += 3;
532 break;
535 /* C. move the first path segment in the input buffer to the end of
536 * the output buffer, including the initial "/" character (if
537 * any) and any subsequent characters up to, but not including,
538 * the next "/" character or the end of the input buffer.
540 *out++ = *in++;
541 while(in < end && *in != '/')
542 *out++ = *in++;
545 len = out - path;
546 TRACE("(%p %d): Path after dot segments removed %s len=%d\n", path, path_len,
547 debugstr_wn(path, len), len);
548 return len;
551 /* Attempts to find the file extension in a given path. */
552 static INT find_file_extension(const WCHAR *path, DWORD path_len) {
553 const WCHAR *end;
555 for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) {
556 if(*end == '.')
557 return end-path;
560 return -1;
563 /* Computes the location where the elision should occur in the IPv6
564 * address using the numerical values of each component stored in
565 * 'values'. If the address shouldn't contain an elision then 'index'
566 * is assigned -1 as it's value. Otherwise 'index' will contain the
567 * starting index (into values) where the elision should be, and 'count'
568 * will contain the number of cells the elision covers.
570 * NOTES:
571 * Windows will expand an elision if the elision only represents 1 h16
572 * component of the URI.
574 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
576 * If the IPv6 address contains an IPv4 address, the IPv4 address is also
577 * considered for being included as part of an elision if all it's components
578 * are zeros.
580 * Ex: [1:2:3:4:5:6:0.0.0.0] -> [1:2:3:4:5:6::]
582 static void compute_elision_location(const ipv6_address *address, const USHORT values[8],
583 INT *index, DWORD *count) {
584 DWORD i, max_len, cur_len;
585 INT max_index, cur_index;
587 max_len = cur_len = 0;
588 max_index = cur_index = -1;
589 for(i = 0; i < 8; ++i) {
590 BOOL check_ipv4 = (address->ipv4 && i == 6);
591 BOOL is_end = (check_ipv4 || i == 7);
593 if(check_ipv4) {
594 /* Check if the IPv4 address contains only zeros. */
595 if(values[i] == 0 && values[i+1] == 0) {
596 if(cur_index == -1)
597 cur_index = i;
599 cur_len += 2;
600 ++i;
602 } else if(values[i] == 0) {
603 if(cur_index == -1)
604 cur_index = i;
606 ++cur_len;
609 if(is_end || values[i] != 0) {
610 /* We only consider it for an elision if it's
611 * more then 1 component long.
613 if(cur_len > 1 && cur_len > max_len) {
614 /* Found the new elision location. */
615 max_len = cur_len;
616 max_index = cur_index;
619 /* Reset the current range for the next range of zeros. */
620 cur_index = -1;
621 cur_len = 0;
625 *index = max_index;
626 *count = max_len;
629 /* Converts the specified IPv4 address into an uint value.
631 * This function assumes that the IPv4 address has already been validated.
633 static UINT ipv4toui(const WCHAR *ip, DWORD len) {
634 UINT ret = 0;
635 DWORD comp_value = 0;
636 const WCHAR *ptr;
638 for(ptr = ip; ptr < ip+len; ++ptr) {
639 if(*ptr == '.') {
640 ret <<= 8;
641 ret += comp_value;
642 comp_value = 0;
643 } else
644 comp_value = comp_value*10 + (*ptr-'0');
647 ret <<= 8;
648 ret += comp_value;
650 return ret;
653 /* Converts an IPv4 address in numerical form into it's fully qualified
654 * string form. This function returns the number of characters written
655 * to 'dest'. If 'dest' is NULL this function will return the number of
656 * characters that would have been written.
658 * It's up to the caller to ensure there's enough space in 'dest' for the
659 * address.
661 static DWORD ui2ipv4(WCHAR *dest, UINT address) {
662 static const WCHAR formatW[] =
663 {'%','u','.','%','u','.','%','u','.','%','u',0};
664 DWORD ret = 0;
665 UCHAR digits[4];
667 digits[0] = (address >> 24) & 0xff;
668 digits[1] = (address >> 16) & 0xff;
669 digits[2] = (address >> 8) & 0xff;
670 digits[3] = address & 0xff;
672 if(!dest) {
673 WCHAR tmp[16];
674 ret = sprintfW(tmp, formatW, digits[0], digits[1], digits[2], digits[3]);
675 } else
676 ret = sprintfW(dest, formatW, digits[0], digits[1], digits[2], digits[3]);
678 return ret;
681 /* Converts an h16 component (from an IPv6 address) into it's
682 * numerical value.
684 * This function assumes that the h16 component has already been validated.
686 static USHORT h16tous(h16 component) {
687 DWORD i;
688 USHORT ret = 0;
690 for(i = 0; i < component.len; ++i) {
691 ret <<= 4;
692 ret += hex_to_int(component.str[i]);
695 return ret;
698 /* Converts an IPv6 address into it's 128 bits (16 bytes) numerical value.
700 * This function assumes that the ipv6_address has already been validated.
702 static BOOL ipv6_to_number(const ipv6_address *address, USHORT number[8]) {
703 DWORD i, cur_component = 0;
704 BOOL already_passed_elision = FALSE;
706 for(i = 0; i < address->h16_count; ++i) {
707 if(address->elision) {
708 if(address->components[i].str > address->elision && !already_passed_elision) {
709 /* Means we just passed the elision and need to add it's values to
710 * 'number' before we do anything else.
712 DWORD j = 0;
713 for(j = 0; j < address->elision_size; j+=2)
714 number[cur_component++] = 0;
716 already_passed_elision = TRUE;
720 number[cur_component++] = h16tous(address->components[i]);
723 /* Case when the elision appears after the h16 components. */
724 if(!already_passed_elision && address->elision) {
725 for(i = 0; i < address->elision_size; i+=2)
726 number[cur_component++] = 0;
727 already_passed_elision = TRUE;
730 if(address->ipv4) {
731 UINT value = ipv4toui(address->ipv4, address->ipv4_len);
733 if(cur_component != 6) {
734 ERR("(%p %p): Failed sanity check with %d\n", address, number, cur_component);
735 return FALSE;
738 number[cur_component++] = (value >> 16) & 0xffff;
739 number[cur_component] = value & 0xffff;
742 return TRUE;
745 /* Checks if the characters pointed to by 'ptr' are
746 * a percent encoded data octet.
748 * pct-encoded = "%" HEXDIG HEXDIG
750 static BOOL check_pct_encoded(const WCHAR **ptr) {
751 const WCHAR *start = *ptr;
753 if(**ptr != '%')
754 return FALSE;
756 ++(*ptr);
757 if(!is_hexdigit(**ptr)) {
758 *ptr = start;
759 return FALSE;
762 ++(*ptr);
763 if(!is_hexdigit(**ptr)) {
764 *ptr = start;
765 return FALSE;
768 ++(*ptr);
769 return TRUE;
772 /* dec-octet = DIGIT ; 0-9
773 * / %x31-39 DIGIT ; 10-99
774 * / "1" 2DIGIT ; 100-199
775 * / "2" %x30-34 DIGIT ; 200-249
776 * / "25" %x30-35 ; 250-255
778 static BOOL check_dec_octet(const WCHAR **ptr) {
779 const WCHAR *c1, *c2, *c3;
781 c1 = *ptr;
782 /* A dec-octet must be at least 1 digit long. */
783 if(*c1 < '0' || *c1 > '9')
784 return FALSE;
786 ++(*ptr);
788 c2 = *ptr;
789 /* Since the 1 digit requirment was meet, it doesn't
790 * matter if this is a DIGIT value, it's considered a
791 * dec-octet.
793 if(*c2 < '0' || *c2 > '9')
794 return TRUE;
796 ++(*ptr);
798 c3 = *ptr;
799 /* Same explanation as above. */
800 if(*c3 < '0' || *c3 > '9')
801 return TRUE;
803 /* Anything > 255 isn't a valid IP dec-octet. */
804 if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') {
805 *ptr = c1;
806 return FALSE;
809 ++(*ptr);
810 return TRUE;
813 /* Checks if there is an implicit IPv4 address in the host component of the URI.
814 * The max value of an implicit IPv4 address is UINT_MAX.
816 * Ex:
817 * "234567" would be considered an implicit IPv4 address.
819 static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) {
820 const WCHAR *start = *ptr;
821 ULONGLONG ret = 0;
822 *val = 0;
824 while(is_num(**ptr)) {
825 ret = ret*10 + (**ptr - '0');
827 if(ret > UINT_MAX) {
828 *ptr = start;
829 return FALSE;
831 ++(*ptr);
834 if(*ptr == start)
835 return FALSE;
837 *val = ret;
838 return TRUE;
841 /* Checks if the string contains an IPv4 address.
843 * This function has a strict mode or a non-strict mode of operation
844 * When 'strict' is set to FALSE this function will return TRUE if
845 * the string contains at least 'dec-octet "." dec-octet' since partial
846 * IPv4 addresses will be normalized out into full IPv4 addresses. When
847 * 'strict' is set this function expects there to be a full IPv4 address.
849 * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet
851 static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) {
852 const WCHAR *start = *ptr;
854 if(!check_dec_octet(ptr)) {
855 *ptr = start;
856 return FALSE;
859 if(**ptr != '.') {
860 *ptr = start;
861 return FALSE;
864 ++(*ptr);
865 if(!check_dec_octet(ptr)) {
866 *ptr = start;
867 return FALSE;
870 if(**ptr != '.') {
871 if(strict) {
872 *ptr = start;
873 return FALSE;
874 } else
875 return TRUE;
878 ++(*ptr);
879 if(!check_dec_octet(ptr)) {
880 *ptr = start;
881 return FALSE;
884 if(**ptr != '.') {
885 if(strict) {
886 *ptr = start;
887 return FALSE;
888 } else
889 return TRUE;
892 ++(*ptr);
893 if(!check_dec_octet(ptr)) {
894 *ptr = start;
895 return FALSE;
898 /* Found a four digit ip address. */
899 return TRUE;
901 /* Tries to parse the scheme name of the URI.
903 * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896.
904 * NOTE: Windows accepts a number as the first character of a scheme.
906 static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data) {
907 const WCHAR *start = *ptr;
909 data->scheme = NULL;
910 data->scheme_len = 0;
912 while(**ptr) {
913 if(**ptr == '*' && *ptr == start) {
914 /* Might have found a wildcard scheme. If it is the next
915 * char has to be a ':' for it to be a valid URI
917 ++(*ptr);
918 break;
919 } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' &&
920 **ptr != '-' && **ptr != '.')
921 break;
923 (*ptr)++;
926 if(*ptr == start)
927 return FALSE;
929 /* Schemes must end with a ':' */
930 if(**ptr != ':') {
931 *ptr = start;
932 return FALSE;
935 data->scheme = start;
936 data->scheme_len = *ptr - start;
938 ++(*ptr);
939 return TRUE;
942 /* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores
943 * the deduced URL_SCHEME in data->scheme_type.
945 static BOOL parse_scheme_type(parse_data *data) {
946 /* If there's scheme data then see if it's a recognized scheme. */
947 if(data->scheme && data->scheme_len) {
948 DWORD i;
950 for(i = 0; i < sizeof(recognized_schemes)/sizeof(recognized_schemes[0]); ++i) {
951 if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) {
952 /* Has to be a case insensitive compare. */
953 if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) {
954 data->scheme_type = recognized_schemes[i].scheme;
955 return TRUE;
960 /* If we get here it means it's not a recognized scheme. */
961 data->scheme_type = URL_SCHEME_UNKNOWN;
962 return TRUE;
963 } else if(data->is_relative) {
964 /* Relative URI's have no scheme. */
965 data->scheme_type = URL_SCHEME_UNKNOWN;
966 return TRUE;
967 } else {
968 /* Should never reach here! what happened... */
969 FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri));
970 return FALSE;
974 /* Tries to parse (or deduce) the scheme_name of a URI. If it can't
975 * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type
976 * using the flags specified in 'flags' (if any). Flags that affect how this function
977 * operates are the Uri_CREATE_ALLOW_* flags.
979 * All parsed/deduced information will be stored in 'data' when the function returns.
981 * Returns TRUE if it was able to successfully parse the information.
983 static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags) {
984 static const WCHAR fileW[] = {'f','i','l','e',0};
985 static const WCHAR wildcardW[] = {'*',0};
987 /* First check to see if the uri could implicitly be a file path. */
988 if(is_implicit_file_path(*ptr)) {
989 if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) {
990 data->scheme = fileW;
991 data->scheme_len = lstrlenW(fileW);
992 data->has_implicit_scheme = TRUE;
994 TRACE("(%p %p %x): URI is an implicit file path.\n", ptr, data, flags);
995 } else {
996 /* Window's does not consider anything that can implicitly be a file
997 * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set...
999 TRACE("(%p %p %x): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n",
1000 ptr, data, flags);
1001 return FALSE;
1003 } else if(!parse_scheme_name(ptr, data)) {
1004 /* No Scheme was found, this means it could be:
1005 * a) an implicit Wildcard scheme
1006 * b) a relative URI
1007 * c) a invalid URI.
1009 if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) {
1010 data->scheme = wildcardW;
1011 data->scheme_len = lstrlenW(wildcardW);
1012 data->has_implicit_scheme = TRUE;
1014 TRACE("(%p %p %x): URI is an implicit wildcard scheme.\n", ptr, data, flags);
1015 } else if (flags & Uri_CREATE_ALLOW_RELATIVE) {
1016 data->is_relative = TRUE;
1017 TRACE("(%p %p %x): URI is relative.\n", ptr, data, flags);
1018 } else {
1019 TRACE("(%p %p %x): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags);
1020 return FALSE;
1024 if(!data->is_relative)
1025 TRACE("(%p %p %x): Found scheme=%s scheme_len=%d\n", ptr, data, flags,
1026 debugstr_wn(data->scheme, data->scheme_len), data->scheme_len);
1028 if(!parse_scheme_type(data))
1029 return FALSE;
1031 TRACE("(%p %p %x): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type);
1032 return TRUE;
1035 /* Parses the userinfo part of the URI (if it exists). The userinfo field of
1036 * a URI can consist of "username:password@", or just "username@".
1038 * RFC def:
1039 * userinfo = *( unreserved / pct-encoded / sub-delims / ":" )
1041 * NOTES:
1042 * 1) If there is more than one ':' in the userinfo part of the URI Windows
1043 * uses the first occurence of ':' to delimit the username and password
1044 * components.
1046 * ex:
1047 * ftp://user:pass:word@winehq.org
1049 * Would yield, "user" as the username and "pass:word" as the password.
1051 * 2) Windows allows any character to appear in the "userinfo" part of
1052 * a URI, as long as it's not an authority delimeter character set.
1054 static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) {
1055 data->userinfo = *ptr;
1056 data->userinfo_split = -1;
1058 while(**ptr != '@') {
1059 if(**ptr == ':' && data->userinfo_split == -1)
1060 data->userinfo_split = *ptr - data->userinfo;
1061 else if(**ptr == '%') {
1062 /* If it's a known scheme type, it has to be a valid percent
1063 * encoded value.
1065 if(!check_pct_encoded(ptr)) {
1066 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1067 *ptr = data->userinfo;
1068 data->userinfo = NULL;
1069 data->userinfo_split = -1;
1071 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1072 return;
1074 } else
1075 continue;
1076 } else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN))
1077 break;
1079 ++(*ptr);
1082 if(**ptr != '@') {
1083 *ptr = data->userinfo;
1084 data->userinfo = NULL;
1085 data->userinfo_split = -1;
1087 TRACE("(%p %p %x): URI contained no userinfo.\n", ptr, data, flags);
1088 return;
1091 data->userinfo_len = *ptr - data->userinfo;
1092 TRACE("(%p %p %x): Found userinfo=%s userinfo_len=%d split=%d.\n", ptr, data, flags,
1093 debugstr_wn(data->userinfo, data->userinfo_len), data->userinfo_len, data->userinfo_split);
1094 ++(*ptr);
1097 /* Attempts to parse a port from the URI.
1099 * NOTES:
1100 * Windows seems to have a cap on what the maximum value
1101 * for a port can be. The max value is USHORT_MAX.
1103 * port = *DIGIT
1105 static BOOL parse_port(const WCHAR **ptr, parse_data *data, DWORD flags) {
1106 UINT port = 0;
1107 data->port = *ptr;
1109 while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) {
1110 if(!is_num(**ptr)) {
1111 *ptr = data->port;
1112 data->port = NULL;
1113 return FALSE;
1116 port = port*10 + (**ptr-'0');
1118 if(port > USHORT_MAX) {
1119 *ptr = data->port;
1120 data->port = NULL;
1121 return FALSE;
1124 ++(*ptr);
1127 data->port_value = port;
1128 data->port_len = *ptr - data->port;
1130 TRACE("(%p %p %x): Found port %s len=%d value=%u\n", ptr, data, flags,
1131 debugstr_wn(data->port, data->port_len), data->port_len, data->port_value);
1132 return TRUE;
1135 /* Attempts to parse a IPv4 address from the URI.
1137 * NOTES:
1138 * Window's normalizes IPv4 addresses, This means there's three
1139 * possibilities for the URI to contain an IPv4 address.
1140 * 1) A well formed address (ex. 192.2.2.2).
1141 * 2) A partially formed address. For example "192.0" would
1142 * normalize to "192.0.0.0" during canonicalization.
1143 * 3) An implicit IPv4 address. For example "256" would
1144 * normalize to "0.0.1.0" during canonicalization. Also
1145 * note that the maximum value for an implicit IP address
1146 * is UINT_MAX, if the value in the URI exceeds this then
1147 * it is not considered an IPv4 address.
1149 static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1150 const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN;
1151 data->host = *ptr;
1153 if(!check_ipv4address(ptr, FALSE)) {
1154 if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) {
1155 TRACE("(%p %p %x): URI didn't contain anything looking like an IPv4 address.\n",
1156 ptr, data, flags);
1157 *ptr = data->host;
1158 data->host = NULL;
1159 return FALSE;
1160 } else
1161 data->has_implicit_ip = TRUE;
1164 /* Check if what we found is the only part of the host name (if it isn't
1165 * we don't have an IPv4 address).
1167 if(**ptr == ':') {
1168 ++(*ptr);
1169 if(!parse_port(ptr, data, flags)) {
1170 *ptr = data->host;
1171 data->host = NULL;
1172 return FALSE;
1174 } else if(!is_auth_delim(**ptr, !is_unknown)) {
1175 /* Found more data which belongs the host, so this isn't an IPv4. */
1176 *ptr = data->host;
1177 data->host = NULL;
1178 data->has_implicit_ip = FALSE;
1179 return FALSE;
1182 data->host_len = *ptr - data->host;
1183 data->host_type = Uri_HOST_IPV4;
1185 TRACE("(%p %p %x): IPv4 address found. host=%s host_len=%d host_type=%d\n",
1186 ptr, data, flags, debugstr_wn(data->host, data->host_len),
1187 data->host_len, data->host_type);
1188 return TRUE;
1191 /* Attempts to parse the reg-name from the URI.
1193 * Because of the way Windows handles ':' this function also
1194 * handles parsing the port.
1196 * reg-name = *( unreserved / pct-encoded / sub-delims )
1198 * NOTE:
1199 * Windows allows everything, but, the characters in "auth_delims" and ':'
1200 * to appear in a reg-name, unless it's an unknown scheme type then ':' is
1201 * allowed to appear (even if a valid port isn't after it).
1203 * Windows doesn't like host names which start with '[' and end with ']'
1204 * and don't contain a valid IP literal address in between them.
1206 * On Windows if an '[' is encountered in the host name the ':' no longer
1207 * counts as a delimiter until you reach the next ']' or an "authority delimeter".
1209 * A reg-name CAN be empty.
1211 static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD flags) {
1212 const BOOL has_start_bracket = **ptr == '[';
1213 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1214 BOOL inside_brackets = has_start_bracket;
1215 BOOL ignore_col = FALSE;
1217 /* We have to be careful with file schemes. */
1218 if(data->scheme_type == URL_SCHEME_FILE) {
1219 /* This is because an implicit file scheme could be "C:\\test" and it
1220 * would trick this function into thinking the host is "C", when after
1221 * canonicalization the host would end up being an empty string.
1223 if(is_alpha(**ptr) && *(*ptr+1) == ':') {
1224 /* Regular old drive paths don't have a host type (or host name). */
1225 data->host_type = Uri_HOST_UNKNOWN;
1226 data->host = *ptr;
1227 data->host_len = 0;
1228 return TRUE;
1229 } else if(**ptr == '\\' && *(*ptr+1) == '\\')
1230 /* Skip past the "\\" of a UNC path. */
1231 *ptr += 2;
1234 data->host = *ptr;
1236 while(!is_auth_delim(**ptr, known_scheme)) {
1237 if(**ptr == ':' && !ignore_col) {
1238 /* We can ignore ':' if were inside brackets.*/
1239 if(!inside_brackets) {
1240 const WCHAR *tmp = (*ptr)++;
1242 /* Attempt to parse the port. */
1243 if(!parse_port(ptr, data, flags)) {
1244 /* Windows expects there to be a valid port for known scheme types. */
1245 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1246 *ptr = data->host;
1247 data->host = NULL;
1248 TRACE("(%p %p %x): Expected valid port\n", ptr, data, flags);
1249 return FALSE;
1250 } else
1251 /* Windows gives up on trying to parse a port when it
1252 * encounters 1 invalid port.
1254 ignore_col = TRUE;
1255 } else {
1256 data->host_len = tmp - data->host;
1257 break;
1260 } else if(**ptr == '%' && known_scheme) {
1261 /* Has to be a legit % encoded value. */
1262 if(!check_pct_encoded(ptr)) {
1263 *ptr = data->host;
1264 data->host = NULL;
1265 return FALSE;
1266 } else
1267 continue;
1268 } else if(**ptr == ']')
1269 inside_brackets = FALSE;
1270 else if(**ptr == '[')
1271 inside_brackets = TRUE;
1273 ++(*ptr);
1276 if(has_start_bracket) {
1277 /* Make sure the last character of the host wasn't a ']'. */
1278 if(*(*ptr-1) == ']') {
1279 TRACE("(%p %p %x): Expected an IP literal inside of the host\n",
1280 ptr, data, flags);
1281 *ptr = data->host;
1282 data->host = NULL;
1283 return FALSE;
1287 /* Don't overwrite our length if we found a port earlier. */
1288 if(!data->port)
1289 data->host_len = *ptr - data->host;
1291 /* If the host is empty, then it's an unknown host type. */
1292 if(data->host_len == 0)
1293 data->host_type = Uri_HOST_UNKNOWN;
1294 else
1295 data->host_type = Uri_HOST_DNS;
1297 TRACE("(%p %p %x): Parsed reg-name. host=%s len=%d\n", ptr, data, flags,
1298 debugstr_wn(data->host, data->host_len), data->host_len);
1299 return TRUE;
1302 /* Attempts to parse an IPv6 address out of the URI.
1304 * IPv6address = 6( h16 ":" ) ls32
1305 * / "::" 5( h16 ":" ) ls32
1306 * / [ h16 ] "::" 4( h16 ":" ) ls32
1307 * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32
1308 * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32
1309 * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32
1310 * / [ *4( h16 ":" ) h16 ] "::" ls32
1311 * / [ *5( h16 ":" ) h16 ] "::" h16
1312 * / [ *6( h16 ":" ) h16 ] "::"
1314 * ls32 = ( h16 ":" h16 ) / IPv4address
1315 * ; least-significant 32 bits of address.
1317 * h16 = 1*4HEXDIG
1318 * ; 16 bits of address represented in hexadecimal.
1320 * Modeled after google-url's 'DoParseIPv6' function.
1322 static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data, DWORD flags) {
1323 const WCHAR *start, *cur_start;
1324 ipv6_address ip;
1326 start = cur_start = *ptr;
1327 memset(&ip, 0, sizeof(ipv6_address));
1329 for(;; ++(*ptr)) {
1330 /* Check if we're on the last character of the host. */
1331 BOOL is_end = (is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)
1332 || **ptr == ']');
1334 BOOL is_split = (**ptr == ':');
1335 BOOL is_elision = (is_split && !is_end && *(*ptr+1) == ':');
1337 /* Check if we're at the end of of the a component, or
1338 * if we're at the end of the IPv6 address.
1340 if(is_split || is_end) {
1341 DWORD cur_len = 0;
1343 cur_len = *ptr - cur_start;
1345 /* h16 can't have a length > 4. */
1346 if(cur_len > 4) {
1347 *ptr = start;
1349 TRACE("(%p %p %x): h16 component to long.\n",
1350 ptr, data, flags);
1351 return FALSE;
1354 if(cur_len == 0) {
1355 /* An h16 component can't have the length of 0 unless
1356 * the elision is at the beginning of the address, or
1357 * at the end of the address.
1359 if(!((*ptr == start && is_elision) ||
1360 (is_end && (*ptr-2) == ip.elision))) {
1361 *ptr = start;
1362 TRACE("(%p %p %x): IPv6 component can not have a length of 0.\n",
1363 ptr, data, flags);
1364 return FALSE;
1368 if(cur_len > 0) {
1369 /* An IPv6 address can have no more than 8 h16 components. */
1370 if(ip.h16_count >= 8) {
1371 *ptr = start;
1372 TRACE("(%p %p %x): Not a IPv6 address, to many h16 components.\n",
1373 ptr, data, flags);
1374 return FALSE;
1377 ip.components[ip.h16_count].str = cur_start;
1378 ip.components[ip.h16_count].len = cur_len;
1380 TRACE("(%p %p %x): Found h16 component %s, len=%d, h16_count=%d\n",
1381 ptr, data, flags, debugstr_wn(cur_start, cur_len), cur_len,
1382 ip.h16_count);
1383 ++ip.h16_count;
1387 if(is_end)
1388 break;
1390 if(is_elision) {
1391 /* A IPv6 address can only have 1 elision ('::'). */
1392 if(ip.elision) {
1393 *ptr = start;
1395 TRACE("(%p %p %x): IPv6 address cannot have 2 elisions.\n",
1396 ptr, data, flags);
1397 return FALSE;
1400 ip.elision = *ptr;
1401 ++(*ptr);
1404 if(is_split)
1405 cur_start = *ptr+1;
1406 else {
1407 if(!check_ipv4address(ptr, TRUE)) {
1408 if(!is_hexdigit(**ptr)) {
1409 /* Not a valid character for an IPv6 address. */
1410 *ptr = start;
1411 return FALSE;
1413 } else {
1414 /* Found an IPv4 address. */
1415 ip.ipv4 = cur_start;
1416 ip.ipv4_len = *ptr - cur_start;
1418 TRACE("(%p %p %x): Found an attached IPv4 address %s len=%d.\n",
1419 ptr, data, flags, debugstr_wn(ip.ipv4, ip.ipv4_len),
1420 ip.ipv4_len);
1422 /* IPv4 addresses can only appear at the end of a IPv6. */
1423 break;
1428 compute_ipv6_comps_size(&ip);
1430 /* Make sure the IPv6 address adds up to 16 bytes. */
1431 if(ip.components_size + ip.elision_size != 16) {
1432 *ptr = start;
1433 TRACE("(%p %p %x): Invalid IPv6 address, did not add up to 16 bytes.\n",
1434 ptr, data, flags);
1435 return FALSE;
1438 if(ip.elision_size == 2) {
1439 /* For some reason on Windows if an elision that represents
1440 * only 1 h16 component is encountered at the very begin or
1441 * end of an IPv6 address, Windows does not consider it a
1442 * valid IPv6 address.
1444 * Ex: [::2:3:4:5:6:7] is not valid, even though the sum
1445 * of all the components == 128bits.
1447 if(ip.elision < ip.components[0].str ||
1448 ip.elision > ip.components[ip.h16_count-1].str) {
1449 *ptr = start;
1450 TRACE("(%p %p %x): Invalid IPv6 address. Detected elision of 2 bytes at the beginning or end of the address.\n",
1451 ptr, data, flags);
1452 return FALSE;
1456 data->host_type = Uri_HOST_IPV6;
1457 data->has_ipv6 = TRUE;
1458 data->ipv6_address = ip;
1460 TRACE("(%p %p %x): Found valid IPv6 literal %s len=%d\n",
1461 ptr, data, flags, debugstr_wn(start, *ptr-start),
1462 *ptr-start);
1463 return TRUE;
1466 /* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */
1467 static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data, DWORD flags) {
1468 const WCHAR *start = *ptr;
1470 /* IPvFuture has to start with a 'v' or 'V'. */
1471 if(**ptr != 'v' && **ptr != 'V')
1472 return FALSE;
1474 /* Following the v their must be atleast 1 hexdigit. */
1475 ++(*ptr);
1476 if(!is_hexdigit(**ptr)) {
1477 *ptr = start;
1478 return FALSE;
1481 ++(*ptr);
1482 while(is_hexdigit(**ptr))
1483 ++(*ptr);
1485 /* End of the hexdigit sequence must be a '.' */
1486 if(**ptr != '.') {
1487 *ptr = start;
1488 return FALSE;
1491 ++(*ptr);
1492 if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') {
1493 *ptr = start;
1494 return FALSE;
1497 ++(*ptr);
1498 while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':')
1499 ++(*ptr);
1501 data->host_type = Uri_HOST_UNKNOWN;
1503 TRACE("(%p %p %x): Parsed IPvFuture address %s len=%d\n", ptr, data, flags,
1504 debugstr_wn(start, *ptr-start), *ptr-start);
1506 return TRUE;
1509 /* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */
1510 static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD flags) {
1511 data->host = *ptr;
1513 if(**ptr != '[') {
1514 data->host = NULL;
1515 return FALSE;
1518 ++(*ptr);
1519 if(!parse_ipv6address(ptr, data, flags)) {
1520 if(!parse_ipvfuture(ptr, data, flags)) {
1521 *ptr = data->host;
1522 data->host = NULL;
1523 return FALSE;
1527 if(**ptr != ']') {
1528 *ptr = data->host;
1529 data->host = NULL;
1530 return FALSE;
1533 ++(*ptr);
1534 if(**ptr == ':') {
1535 ++(*ptr);
1536 /* If a valid port is not found, then let it trickle down to
1537 * parse_reg_name.
1539 if(!parse_port(ptr, data, flags)) {
1540 *ptr = data->host;
1541 data->host = NULL;
1542 return FALSE;
1544 } else
1545 data->host_len = *ptr - data->host;
1547 return TRUE;
1550 /* Parses the host information from the URI.
1552 * host = IP-literal / IPv4address / reg-name
1554 static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD flags) {
1555 if(!parse_ip_literal(ptr, data, flags)) {
1556 if(!parse_ipv4address(ptr, data, flags)) {
1557 if(!parse_reg_name(ptr, data, flags)) {
1558 TRACE("(%p %p %x): Malformed URI, Unknown host type.\n",
1559 ptr, data, flags);
1560 return FALSE;
1565 return TRUE;
1568 /* Parses the authority information from the URI.
1570 * authority = [ userinfo "@" ] host [ ":" port ]
1572 static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) {
1573 parse_userinfo(ptr, data, flags);
1575 /* Parsing the port will happen during one of the host parsing
1576 * routines (if the URI has a port).
1578 if(!parse_host(ptr, data, flags))
1579 return FALSE;
1581 return TRUE;
1584 /* Attempts to parse the path information of a hierarchical URI. */
1585 static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) {
1586 const WCHAR *start = *ptr;
1587 static const WCHAR slash[] = {'/',0};
1589 if(is_path_delim(**ptr)) {
1590 if(data->scheme_type == URL_SCHEME_WILDCARD) {
1591 /* Wildcard schemes don't get a '/' attached if their path is
1592 * empty.
1594 data->path = NULL;
1595 data->path_len = 0;
1596 } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
1597 /* If the path component is empty, then a '/' is added. */
1598 data->path = slash;
1599 data->path_len = 1;
1601 } else {
1602 while(!is_path_delim(**ptr)) {
1603 if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN &&
1604 data->scheme_type != URL_SCHEME_FILE) {
1605 if(!check_pct_encoded(ptr)) {
1606 *ptr = start;
1607 return FALSE;
1608 } else
1609 continue;
1610 } else if(**ptr == '\\') {
1611 /* Not allowed to have a backslash if NO_CANONICALIZE is set
1612 * and the scheme is known type (but not a file scheme).
1614 if(flags & Uri_CREATE_NO_CANONICALIZE) {
1615 if(data->scheme_type != URL_SCHEME_FILE &&
1616 data->scheme_type != URL_SCHEME_UNKNOWN) {
1617 *ptr = start;
1618 return FALSE;
1623 ++(*ptr);
1626 /* The only time a URI doesn't have a path is when
1627 * the NO_CANONICALIZE flag is set and the raw URI
1628 * didn't contain one.
1630 if(*ptr == start) {
1631 data->path = NULL;
1632 data->path_len = 0;
1633 } else {
1634 data->path = start;
1635 data->path_len = *ptr - start;
1639 if(data->path)
1640 TRACE("(%p %p %x): Parsed path %s len=%d\n", ptr, data, flags,
1641 debugstr_wn(data->path, data->path_len), data->path_len);
1642 else
1643 TRACE("(%p %p %x): The URI contained no path\n", ptr, data, flags);
1645 return TRUE;
1648 /* Parses the path of a opaque URI (much less strict then the parser
1649 * for a hierarchical URI).
1651 * NOTE:
1652 * Windows allows invalid % encoded data to appear in opaque URI paths
1653 * for unknown scheme types.
1655 static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) {
1656 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1658 data->path = *ptr;
1660 while(!is_path_delim(**ptr)) {
1661 if(**ptr == '%' && known_scheme) {
1662 if(!check_pct_encoded(ptr)) {
1663 *ptr = data->path;
1664 data->path = NULL;
1665 return FALSE;
1666 } else
1667 continue;
1670 ++(*ptr);
1673 data->path_len = *ptr - data->path;
1674 TRACE("(%p %p %x): Parsed opaque URI path %s len=%d\n", ptr, data, flags,
1675 debugstr_wn(data->path, data->path_len), data->path_len);
1676 return TRUE;
1679 /* Determines how the URI should be parsed after the scheme information.
1681 * If the scheme is followed, by "//" then, it is treated as an hierarchical URI
1682 * which then the authority and path information will be parsed out. Otherwise, the
1683 * URI will be treated as an opaque URI which the authority information is not parsed
1684 * out.
1686 * RFC 3896 definition of hier-part:
1688 * hier-part = "//" authority path-abempty
1689 * / path-absolute
1690 * / path-rootless
1691 * / path-empty
1693 * MSDN opaque URI definition:
1694 * scheme ":" path [ "#" fragment ]
1696 * NOTES:
1697 * If the URI is of an unknown scheme type and has a "//" following the scheme then it
1698 * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is
1699 * set then it is considered an opaque URI reguardless of what follows the scheme information
1700 * (per MSDN documentation).
1702 static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) {
1703 const WCHAR *start = *ptr;
1705 /* Checks if the authority information needs to be parsed.
1707 * Relative URI's aren't hierarchical URI's, but, they could trick
1708 * "check_hierarchical" into thinking it is, so we need to explicitly
1709 * make sure it's not relative. Also, if the URI is an implicit file
1710 * scheme it might not contain a "//", but, it's considered hierarchical
1711 * anyways. Wildcard Schemes are always considered hierarchical
1713 if(data->scheme_type == URL_SCHEME_WILDCARD ||
1714 data->scheme_type == URL_SCHEME_FILE ||
1715 (!data->is_relative && check_hierarchical(ptr))) {
1716 /* Only treat it as a hierarchical URI if the scheme_type is known or
1717 * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set.
1719 if(data->scheme_type != URL_SCHEME_UNKNOWN ||
1720 !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) {
1721 TRACE("(%p %p %x): Treating URI as an hierarchical URI.\n", ptr, data, flags);
1722 data->is_opaque = FALSE;
1724 if(data->scheme_type == URL_SCHEME_FILE)
1725 /* Skip past the "//" after the scheme (if any). */
1726 check_hierarchical(ptr);
1728 /* TODO: Handle hierarchical URI's, parse authority then parse the path. */
1729 if(!parse_authority(ptr, data, flags))
1730 return FALSE;
1732 return parse_path_hierarchical(ptr, data, flags);
1733 } else
1734 /* Reset ptr to it's starting position so opaque path parsing
1735 * begins at the correct location.
1737 *ptr = start;
1740 /* If it reaches here, then the URI will be treated as an opaque
1741 * URI.
1744 TRACE("(%p %p %x): Treating URI as an opaque URI.\n", ptr, data, flags);
1746 data->is_opaque = TRUE;
1747 if(!parse_path_opaque(ptr, data, flags))
1748 return FALSE;
1750 return TRUE;
1753 /* Attempts to parse the query string from the URI.
1755 * NOTES:
1756 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1757 * data is allowed appear in the query string. For unknown scheme types
1758 * invalid percent encoded data is allowed to appear reguardless.
1760 static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) {
1761 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1763 if(**ptr != '?') {
1764 TRACE("(%p %p %x): URI didn't contain a query string.\n", ptr, data, flags);
1765 return TRUE;
1768 data->query = *ptr;
1770 ++(*ptr);
1771 while(**ptr && **ptr != '#') {
1772 if(**ptr == '%' && known_scheme &&
1773 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1774 if(!check_pct_encoded(ptr)) {
1775 *ptr = data->query;
1776 data->query = NULL;
1777 return FALSE;
1778 } else
1779 continue;
1782 ++(*ptr);
1785 data->query_len = *ptr - data->query;
1787 TRACE("(%p %p %x): Parsed query string %s len=%d\n", ptr, data, flags,
1788 debugstr_wn(data->query, data->query_len), data->query_len);
1789 return TRUE;
1792 /* Attempts to parse the fragment from the URI.
1794 * NOTES:
1795 * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded
1796 * data is allowed appear in the query string. For unknown scheme types
1797 * invalid percent encoded data is allowed to appear reguardless.
1799 static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) {
1800 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1802 if(**ptr != '#') {
1803 TRACE("(%p %p %x): URI didn't contain a fragment.\n", ptr, data, flags);
1804 return TRUE;
1807 data->fragment = *ptr;
1809 ++(*ptr);
1810 while(**ptr) {
1811 if(**ptr == '%' && known_scheme &&
1812 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
1813 if(!check_pct_encoded(ptr)) {
1814 *ptr = data->fragment;
1815 data->fragment = NULL;
1816 return FALSE;
1817 } else
1818 continue;
1821 ++(*ptr);
1824 data->fragment_len = *ptr - data->fragment;
1826 TRACE("(%p %p %x): Parsed fragment %s len=%d\n", ptr, data, flags,
1827 debugstr_wn(data->fragment, data->fragment_len), data->fragment_len);
1828 return TRUE;
1831 /* Parses and validates the components of the specified by data->uri
1832 * and stores the information it parses into 'data'.
1834 * Returns TRUE if it successfully parsed the URI. False otherwise.
1836 static BOOL parse_uri(parse_data *data, DWORD flags) {
1837 const WCHAR *ptr;
1838 const WCHAR **pptr;
1840 ptr = data->uri;
1841 pptr = &ptr;
1843 TRACE("(%p %x): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri));
1845 if(!parse_scheme(pptr, data, flags))
1846 return FALSE;
1848 if(!parse_hierpart(pptr, data, flags))
1849 return FALSE;
1851 if(!parse_query(pptr, data, flags))
1852 return FALSE;
1854 if(!parse_fragment(pptr, data, flags))
1855 return FALSE;
1857 TRACE("(%p %x): FINISHED PARSING URI.\n", data, flags);
1858 return TRUE;
1861 /* Canonicalizes the userinfo of the URI represented by the parse_data.
1863 * Canonicalization of the userinfo is a simple process. If there are any percent
1864 * encoded characters that fall in the "unreserved" character set, they are decoded
1865 * to their actual value. If a character is not in the "unreserved" or "reserved" sets
1866 * then it is percent encoded. Other than that the characters are copied over without
1867 * change.
1869 static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
1870 DWORD i = 0;
1872 uri->userinfo_start = uri->userinfo_split = -1;
1873 uri->userinfo_len = 0;
1875 if(!data->userinfo)
1876 /* URI doesn't have userinfo, so nothing to do here. */
1877 return TRUE;
1879 uri->userinfo_start = uri->canon_len;
1881 while(i < data->userinfo_len) {
1882 if(data->userinfo[i] == ':' && uri->userinfo_split == -1)
1883 /* Windows only considers the first ':' as the delimiter. */
1884 uri->userinfo_split = uri->canon_len - uri->userinfo_start;
1885 else if(data->userinfo[i] == '%') {
1886 /* Only decode % encoded values for known scheme types. */
1887 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
1888 /* See if the value really needs decoded. */
1889 WCHAR val = decode_pct_val(data->userinfo + i);
1890 if(is_unreserved(val)) {
1891 if(!computeOnly)
1892 uri->canon_uri[uri->canon_len] = val;
1894 ++uri->canon_len;
1896 /* Move pass the hex characters. */
1897 i += 3;
1898 continue;
1901 } else if(!is_reserved(data->userinfo[i]) && !is_unreserved(data->userinfo[i]) &&
1902 data->userinfo[i] != '\\') {
1903 /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag
1904 * is NOT set.
1906 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
1907 if(!computeOnly)
1908 pct_encode_val(data->userinfo[i], uri->canon_uri + uri->canon_len);
1910 uri->canon_len += 3;
1911 ++i;
1912 continue;
1916 if(!computeOnly)
1917 /* Nothing special, so just copy the character over. */
1918 uri->canon_uri[uri->canon_len] = data->userinfo[i];
1920 ++uri->canon_len;
1921 ++i;
1924 uri->userinfo_len = uri->canon_len - uri->userinfo_start;
1925 if(!computeOnly)
1926 TRACE("(%p %p %x %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%d.\n",
1927 data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len),
1928 uri->userinfo_split, uri->userinfo_len);
1930 /* Now insert the '@' after the userinfo. */
1931 if(!computeOnly)
1932 uri->canon_uri[uri->canon_len] = '@';
1934 ++uri->canon_len;
1935 return TRUE;
1938 /* Attempts to canonicalize a reg_name.
1940 * Things that happen:
1941 * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is
1942 * lower cased. Unless it's an unknown scheme type, which case it's
1943 * no lower cased reguardless.
1945 * 2) Unreserved % encoded characters are decoded for known
1946 * scheme types.
1948 * 3) Forbidden characters are % encoded as long as
1949 * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and
1950 * it isn't an unknown scheme type.
1952 * 4) If it's a file scheme and the host is "localhost" it's removed.
1954 static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri,
1955 DWORD flags, BOOL computeOnly) {
1956 static const WCHAR localhostW[] =
1957 {'l','o','c','a','l','h','o','s','t',0};
1958 const WCHAR *ptr;
1959 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
1961 uri->host_start = uri->canon_len;
1963 if(data->scheme_type == URL_SCHEME_FILE &&
1964 data->host_len == lstrlenW(localhostW)) {
1965 if(!StrCmpNIW(data->host, localhostW, data->host_len)) {
1966 uri->host_start = -1;
1967 uri->host_len = 0;
1968 uri->host_type = Uri_HOST_UNKNOWN;
1969 return TRUE;
1973 for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) {
1974 if(*ptr == '%' && known_scheme) {
1975 WCHAR val = decode_pct_val(ptr);
1976 if(is_unreserved(val)) {
1977 /* If NO_CANONICALZE is not set, then windows lower cases the
1978 * decoded value.
1980 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && isupperW(val)) {
1981 if(!computeOnly)
1982 uri->canon_uri[uri->canon_len] = tolowerW(val);
1983 } else {
1984 if(!computeOnly)
1985 uri->canon_uri[uri->canon_len] = val;
1987 ++uri->canon_len;
1989 /* Skip past the % encoded character. */
1990 ptr += 2;
1991 continue;
1992 } else {
1993 /* Just copy the % over. */
1994 if(!computeOnly)
1995 uri->canon_uri[uri->canon_len] = *ptr;
1996 ++uri->canon_len;
1998 } else if(*ptr == '\\') {
1999 /* Only unknown scheme types could have made it here with a '\\' in the host name. */
2000 if(!computeOnly)
2001 uri->canon_uri[uri->canon_len] = *ptr;
2002 ++uri->canon_len;
2003 } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2004 !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) {
2005 if(!computeOnly) {
2006 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2008 /* The percent encoded value gets lower cased also. */
2009 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2010 uri->canon_uri[uri->canon_len+1] = tolowerW(uri->canon_uri[uri->canon_len+1]);
2011 uri->canon_uri[uri->canon_len+2] = tolowerW(uri->canon_uri[uri->canon_len+2]);
2015 uri->canon_len += 3;
2016 } else {
2017 if(!computeOnly) {
2018 if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme)
2019 uri->canon_uri[uri->canon_len] = tolowerW(*ptr);
2020 else
2021 uri->canon_uri[uri->canon_len] = *ptr;
2024 ++uri->canon_len;
2028 uri->host_len = uri->canon_len - uri->host_start;
2030 if(!computeOnly)
2031 TRACE("(%p %p %x %d): Canonicalize reg_name=%s len=%d\n", data, uri, flags,
2032 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2033 uri->host_len);
2035 if(!computeOnly)
2036 find_domain_name(uri->canon_uri+uri->host_start, uri->host_len,
2037 &(uri->domain_offset));
2039 return TRUE;
2042 /* Attempts to canonicalize an implicit IPv4 address. */
2043 static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2044 uri->host_start = uri->canon_len;
2046 TRACE("%u\n", data->implicit_ipv4);
2047 /* For unknown scheme types Window's doesn't convert
2048 * the value into an IP address, but, it still considers
2049 * it an IPv4 address.
2051 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2052 if(!computeOnly)
2053 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2054 uri->canon_len += data->host_len;
2055 } else {
2056 if(!computeOnly)
2057 uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4);
2058 else
2059 uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4);
2062 uri->host_len = uri->canon_len - uri->host_start;
2063 uri->host_type = Uri_HOST_IPV4;
2065 if(!computeOnly)
2066 TRACE("%p %p %x %d): Canonicalized implicit IP address=%s len=%d\n",
2067 data, uri, flags, computeOnly,
2068 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2069 uri->host_len);
2071 return TRUE;
2074 /* Attempts to canonicalize an IPv4 address.
2076 * If the parse_data represents a URI that has an implicit IPv4 address
2077 * (ex. http://256/, this function will convert 256 into 0.0.1.0). If
2078 * the implicit IP address exceeds the value of UINT_MAX (maximum value
2079 * for an IPv4 address) it's canonicalized as if were a reg-name.
2081 * If the parse_data contains a partial or full IPv4 address it normalizes it.
2082 * A partial IPv4 address is something like "192.0" and would be normalized to
2083 * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would
2084 * be normalized to "192.2.1.3".
2086 * NOTES:
2087 * Window's ONLY normalizes IPv4 address for known scheme types (one that isn't
2088 * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from
2089 * the original URI into the canonicalized URI, but, it still recognizes URI's
2090 * host type as HOST_IPV4.
2092 static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2093 if(data->has_implicit_ip)
2094 return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly);
2095 else {
2096 uri->host_start = uri->canon_len;
2098 /* Windows only normalizes for known scheme types. */
2099 if(data->scheme_type != URL_SCHEME_UNKNOWN) {
2100 /* parse_data contains a partial or full IPv4 address, so normalize it. */
2101 DWORD i, octetDigitCount = 0, octetCount = 0;
2102 BOOL octetHasDigit = FALSE;
2104 for(i = 0; i < data->host_len; ++i) {
2105 if(data->host[i] == '0' && !octetHasDigit) {
2106 /* Can ignore leading zeros if:
2107 * 1) It isn't the last digit of the octet.
2108 * 2) i+1 != data->host_len
2109 * 3) i+1 != '.'
2111 if(octetDigitCount == 2 ||
2112 i+1 == data->host_len ||
2113 data->host[i+1] == '.') {
2114 if(!computeOnly)
2115 uri->canon_uri[uri->canon_len] = data->host[i];
2116 ++uri->canon_len;
2117 TRACE("Adding zero\n");
2119 } else if(data->host[i] == '.') {
2120 if(!computeOnly)
2121 uri->canon_uri[uri->canon_len] = data->host[i];
2122 ++uri->canon_len;
2124 octetDigitCount = 0;
2125 octetHasDigit = FALSE;
2126 ++octetCount;
2127 } else {
2128 if(!computeOnly)
2129 uri->canon_uri[uri->canon_len] = data->host[i];
2130 ++uri->canon_len;
2132 ++octetDigitCount;
2133 octetHasDigit = TRUE;
2137 /* Make sure the canonicalized IP address has 4 dec-octets.
2138 * If doesn't add "0" ones until there is 4;
2140 for( ; octetCount < 3; ++octetCount) {
2141 if(!computeOnly) {
2142 uri->canon_uri[uri->canon_len] = '.';
2143 uri->canon_uri[uri->canon_len+1] = '0';
2146 uri->canon_len += 2;
2148 } else {
2149 /* Windows doesn't normalize addresses in unknown schemes. */
2150 if(!computeOnly)
2151 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2152 uri->canon_len += data->host_len;
2155 uri->host_len = uri->canon_len - uri->host_start;
2156 if(!computeOnly)
2157 TRACE("(%p %p %x %d): Canonicalized IPv4 address, ip=%s len=%d\n",
2158 data, uri, flags, computeOnly,
2159 debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2160 uri->host_len);
2163 return TRUE;
2166 /* Attempts to canonicalize the IPv6 address of the URI.
2168 * Multiple things happen during the canonicalization of an IPv6 address:
2169 * 1) Any leading zero's in an h16 component are removed.
2170 * Ex: [0001:0022::] -> [1:22::]
2172 * 2) The longest sequence of zero h16 components are compressed
2173 * into a "::" (elision). If there's a tie, the first is choosen.
2175 * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8]
2176 * [0:0:0:0:1:2::] -> [::1:2:0:0]
2177 * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8]
2179 * 3) If an IPv4 address is attached to the IPv6 address, it's
2180 * also normalized.
2181 * Ex: [::001.002.022.000] -> [::1.2.22.0]
2183 * 4) If an elision is present, but, only represents 1 h16 component
2184 * it's expanded.
2186 * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7]
2188 * 5) If the IPv6 address contains an IPv4 address and there exists
2189 * at least 1 non-zero h16 component the IPv4 address is converted
2190 * into two h16 components, otherwise it's normalized and kept as is.
2192 * Ex: [::192.200.003.4] -> [::192.200.3.4]
2193 * [ffff::192.200.003.4] -> [ffff::c0c8:3041]
2195 * NOTE:
2196 * For unknown scheme types Windows simply copies the address over without any
2197 * changes.
2199 * IPv4 address can be included in an elision if all its components are 0's.
2201 static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri,
2202 DWORD flags, BOOL computeOnly) {
2203 uri->host_start = uri->canon_len;
2205 if(data->scheme_type == URL_SCHEME_UNKNOWN) {
2206 if(!computeOnly)
2207 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2208 uri->canon_len += data->host_len;
2209 } else {
2210 USHORT values[8];
2211 INT elision_start;
2212 DWORD i, elision_len;
2214 if(!ipv6_to_number(&(data->ipv6_address), values)) {
2215 TRACE("(%p %p %x %d): Failed to compute numerical value for IPv6 address.\n",
2216 data, uri, flags, computeOnly);
2217 return FALSE;
2220 if(!computeOnly)
2221 uri->canon_uri[uri->canon_len] = '[';
2222 ++uri->canon_len;
2224 /* Find where the elision should occur (if any). */
2225 compute_elision_location(&(data->ipv6_address), values, &elision_start, &elision_len);
2227 TRACE("%p %p %x %d): Elision starts at %d, len=%u\n", data, uri, flags,
2228 computeOnly, elision_start, elision_len);
2230 for(i = 0; i < 8; ++i) {
2231 BOOL in_elision = (elision_start > -1 && i >= elision_start &&
2232 i < elision_start+elision_len);
2233 BOOL do_ipv4 = (i == 6 && data->ipv6_address.ipv4 && !in_elision &&
2234 data->ipv6_address.h16_count == 0);
2236 if(i == elision_start) {
2237 if(!computeOnly) {
2238 uri->canon_uri[uri->canon_len] = ':';
2239 uri->canon_uri[uri->canon_len+1] = ':';
2241 uri->canon_len += 2;
2244 /* We can ignore the current component if we're in the elision. */
2245 if(in_elision)
2246 continue;
2248 /* We only add a ':' if we're not at i == 0, or when we're at
2249 * the very end of elision range since the ':' colon was handled
2250 * earlier. Otherwise we would end up with ":::" after elision.
2252 if(i != 0 && !(elision_start > -1 && i == elision_start+elision_len)) {
2253 if(!computeOnly)
2254 uri->canon_uri[uri->canon_len] = ':';
2255 ++uri->canon_len;
2258 if(do_ipv4) {
2259 UINT val;
2260 DWORD len;
2262 /* Combine the two parts of the IPv4 address values. */
2263 val = values[i];
2264 val <<= 16;
2265 val += values[i+1];
2267 if(!computeOnly)
2268 len = ui2ipv4(uri->canon_uri+uri->canon_len, val);
2269 else
2270 len = ui2ipv4(NULL, val);
2272 uri->canon_len += len;
2273 ++i;
2274 } else {
2275 /* Write a regular h16 component to the URI. */
2277 /* Short circuit for the trivial case. */
2278 if(values[i] == 0) {
2279 if(!computeOnly)
2280 uri->canon_uri[uri->canon_len] = '0';
2281 ++uri->canon_len;
2282 } else {
2283 static const WCHAR formatW[] = {'%','x',0};
2285 if(!computeOnly)
2286 uri->canon_len += sprintfW(uri->canon_uri+uri->canon_len,
2287 formatW, values[i]);
2288 else {
2289 WCHAR tmp[5];
2290 uri->canon_len += sprintfW(tmp, formatW, values[i]);
2296 /* Add the closing ']'. */
2297 if(!computeOnly)
2298 uri->canon_uri[uri->canon_len] = ']';
2299 ++uri->canon_len;
2302 uri->host_len = uri->canon_len - uri->host_start;
2304 if(!computeOnly)
2305 TRACE("(%p %p %x %d): Canonicalized IPv6 address %s, len=%d\n", data, uri, flags,
2306 computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len),
2307 uri->host_len);
2309 return TRUE;
2312 /* Attempts to canonicalize the host of the URI (if any). */
2313 static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2314 uri->host_start = -1;
2315 uri->host_len = 0;
2316 uri->domain_offset = -1;
2318 if(data->host) {
2319 switch(data->host_type) {
2320 case Uri_HOST_DNS:
2321 uri->host_type = Uri_HOST_DNS;
2322 if(!canonicalize_reg_name(data, uri, flags, computeOnly))
2323 return FALSE;
2325 break;
2326 case Uri_HOST_IPV4:
2327 uri->host_type = Uri_HOST_IPV4;
2328 if(!canonicalize_ipv4address(data, uri, flags, computeOnly))
2329 return FALSE;
2331 break;
2332 case Uri_HOST_IPV6:
2333 if(!canonicalize_ipv6address(data, uri, flags, computeOnly))
2334 return FALSE;
2336 uri->host_type = Uri_HOST_IPV6;
2337 break;
2338 case Uri_HOST_UNKNOWN:
2339 if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) {
2340 uri->host_start = uri->canon_len;
2342 /* Nothing happens to unknown host types. */
2343 if(!computeOnly)
2344 memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR));
2345 uri->canon_len += data->host_len;
2346 uri->host_len = data->host_len;
2349 uri->host_type = Uri_HOST_UNKNOWN;
2350 break;
2351 default:
2352 FIXME("(%p %p %x %d): Canonicalization for host type %d not supported.\n", data,
2353 uri, flags, computeOnly, data->host_type);
2354 return FALSE;
2358 return TRUE;
2361 static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2362 BOOL has_default_port = FALSE;
2363 USHORT default_port = 0;
2364 DWORD i;
2366 uri->has_port = FALSE;
2368 /* Check if the scheme has a default port. */
2369 for(i = 0; i < sizeof(default_ports)/sizeof(default_ports[0]); ++i) {
2370 if(default_ports[i].scheme == data->scheme_type) {
2371 has_default_port = TRUE;
2372 default_port = default_ports[i].port;
2373 break;
2377 if(data->port || has_default_port)
2378 uri->has_port = TRUE;
2380 /* Possible cases:
2381 * 1) Has a port which is the default port.
2382 * 2) Has a port (not the default).
2383 * 3) Doesn't have a port, but, scheme has a default port.
2384 * 4) No port.
2386 if(has_default_port && data->port && data->port_value == default_port) {
2387 /* If it's the default port and this flag isn't set, don't do anything. */
2388 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2389 /* Copy the original port over. */
2390 if(!computeOnly) {
2391 uri->canon_uri[uri->canon_len] = ':';
2392 memcpy(uri->canon_uri+uri->canon_len+1, data->port, data->port_len*sizeof(WCHAR));
2394 uri->canon_len += data->port_len+1;
2397 uri->port = default_port;
2398 } else if(data->port) {
2399 if(!computeOnly)
2400 uri->canon_uri[uri->canon_len] = ':';
2401 ++uri->canon_len;
2403 if(flags & Uri_CREATE_NO_CANONICALIZE) {
2404 /* Copy the original over without changes. */
2405 if(!computeOnly)
2406 memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR));
2407 uri->canon_len += data->port_len;
2408 } else {
2409 const WCHAR formatW[] = {'%','u',0};
2410 INT len = 0;
2411 if(!computeOnly)
2412 len = sprintfW(uri->canon_uri+uri->canon_len, formatW, data->port_value);
2413 else {
2414 WCHAR tmp[6];
2415 len = sprintfW(tmp, formatW, data->port_value);
2417 uri->canon_len += len;
2420 uri->port = data->port_value;
2421 } else if(has_default_port)
2422 uri->port = default_port;
2424 return TRUE;
2427 /* Canonicalizes the authority of the URI represented by the parse_data. */
2428 static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2429 uri->authority_start = uri->canon_len;
2430 uri->authority_len = 0;
2432 if(!canonicalize_userinfo(data, uri, flags, computeOnly))
2433 return FALSE;
2435 if(!canonicalize_host(data, uri, flags, computeOnly))
2436 return FALSE;
2438 if(!canonicalize_port(data, uri, flags, computeOnly))
2439 return FALSE;
2441 if(uri->host_start != -1)
2442 uri->authority_len = uri->canon_len - uri->authority_start;
2443 else
2444 uri->authority_start = -1;
2446 return TRUE;
2449 /* Attempts to canonicalize the path of a hierarchical URI.
2451 * Things that happen:
2452 * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN
2453 * flag is set or it's a file URI. Forbidden characters are always encoded
2454 * for file schemes reguardless and forbidden characters are never encoded
2455 * for unknown scheme types.
2457 * 2). For known scheme types '\\' are changed to '/'.
2459 * 3). Percent encoded, unreserved characters are decoded to their actual values.
2460 * Unless the scheme type is unknown. For file schemes any percent encoded
2461 * character in the unreserved or reserved set is decoded.
2463 * 4). For File schemes if the path is starts with a drive letter and doesn't
2464 * start with a '/' then one is appended.
2465 * Ex: file://c:/test.mp3 -> file:///c:/test.mp3
2467 * 5). Dot segments are removed from the path for all scheme types
2468 * unless NO_CANONICALIZE flag is set. Dot segments aren't removed
2469 * for wildcard scheme types.
2471 * NOTES:
2472 * file://c:/test%20test -> file:///c:/test%2520test
2473 * file://c:/test%3Etest -> file:///c:/test%253Etest
2474 * file:///c:/test%20test -> file:///c:/test%20test
2475 * file:///c:/test%test -> file:///c:/test%25test
2477 static BOOL canonicalize_path_hierarchical(const parse_data *data, Uri *uri,
2478 DWORD flags, BOOL computeOnly) {
2479 const WCHAR *ptr;
2480 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2481 const BOOL is_file = data->scheme_type == URL_SCHEME_FILE;
2483 BOOL escape_pct = FALSE;
2485 if(!data->path) {
2486 uri->path_start = -1;
2487 uri->path_len = 0;
2488 return TRUE;
2491 uri->path_start = uri->canon_len;
2493 /* Check if a '/' needs to be appended for the file scheme. */
2494 if(is_file) {
2495 if(data->path_len > 1 && is_alpha(*(data->path)) &&
2496 *(data->path+1) == ':') {
2497 if(!computeOnly)
2498 uri->canon_uri[uri->canon_len] = '/';
2499 uri->canon_len++;
2500 escape_pct = TRUE;
2504 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2505 if(*ptr == '%') {
2506 const WCHAR *tmp = ptr;
2507 WCHAR val;
2509 /* Check if the % represents a valid encoded char, or if it needs encoded. */
2510 BOOL force_encode = !check_pct_encoded(&tmp) && is_file;
2511 val = decode_pct_val(ptr);
2513 if(force_encode || escape_pct) {
2514 /* Escape the percent sign in the file URI. */
2515 if(!computeOnly)
2516 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2517 uri->canon_len += 3;
2518 } else if((is_unreserved(val) && known_scheme) ||
2519 (is_file && (is_unreserved(val) || is_reserved(val)))) {
2520 if(!computeOnly)
2521 uri->canon_uri[uri->canon_len] = val;
2522 ++uri->canon_len;
2524 ptr += 2;
2525 continue;
2526 } else {
2527 if(!computeOnly)
2528 uri->canon_uri[uri->canon_len] = *ptr;
2529 ++uri->canon_len;
2531 } else if(*ptr == '\\' && known_scheme) {
2532 if(!computeOnly)
2533 uri->canon_uri[uri->canon_len] = '/';
2534 ++uri->canon_len;
2535 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2536 (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) {
2537 /* Escape the forbidden character. */
2538 if(!computeOnly)
2539 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2540 uri->canon_len += 3;
2541 } else {
2542 if(!computeOnly)
2543 uri->canon_uri[uri->canon_len] = *ptr;
2544 ++uri->canon_len;
2548 uri->path_len = uri->canon_len - uri->path_start;
2550 /* Removing the dot segments only happens when it's not in
2551 * computeOnly mode and it's not a wildcard scheme.
2553 if(!computeOnly && data->scheme_type != URL_SCHEME_WILDCARD) {
2554 if(!(flags & Uri_CREATE_NO_CANONICALIZE)) {
2555 /* Remove the dot segments (if any) and reset everything to the new
2556 * correct length.
2558 DWORD new_len = remove_dot_segments(uri->canon_uri+uri->path_start, uri->path_len);
2559 uri->canon_len -= uri->path_len-new_len;
2560 uri->path_len = new_len;
2564 if(!computeOnly)
2565 TRACE("Canonicalized path %s len=%d\n",
2566 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len),
2567 uri->path_len);
2569 return TRUE;
2572 /* Attempts to canonicalize the path for an opaque URI.
2574 * For known scheme types:
2575 * 1) forbidden characters are percent encoded if
2576 * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set.
2578 * 2) Percent encoded, unreserved characters are decoded
2579 * to their actual values, for known scheme types.
2581 * 3) '\\' are changed to '/' for known scheme types
2582 * except for mailto schemes.
2584 static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2585 const WCHAR *ptr;
2586 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2588 if(!data->path) {
2589 uri->path_start = -1;
2590 uri->path_len = 0;
2591 return TRUE;
2594 uri->path_start = uri->canon_len;
2596 /* Windows doesn't allow a "//" to appear after the scheme
2597 * of a URI, if it's an opaque URI.
2599 if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') {
2600 /* So it inserts a "/." before the "//" if it exists. */
2601 if(!computeOnly) {
2602 uri->canon_uri[uri->canon_len] = '/';
2603 uri->canon_uri[uri->canon_len+1] = '.';
2606 uri->canon_len += 2;
2609 for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) {
2610 if(*ptr == '%' && known_scheme) {
2611 WCHAR val = decode_pct_val(ptr);
2613 if(is_unreserved(val)) {
2614 if(!computeOnly)
2615 uri->canon_uri[uri->canon_len] = val;
2616 ++uri->canon_len;
2618 ptr += 2;
2619 continue;
2620 } else {
2621 if(!computeOnly)
2622 uri->canon_uri[uri->canon_len] = *ptr;
2623 ++uri->canon_len;
2625 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr) &&
2626 !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) {
2627 if(!computeOnly)
2628 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2629 uri->canon_len += 3;
2630 } else {
2631 if(!computeOnly)
2632 uri->canon_uri[uri->canon_len] = *ptr;
2633 ++uri->canon_len;
2637 uri->path_len = uri->canon_len - uri->path_start;
2639 TRACE("(%p %p %x %d): Canonicalized opaque URI path %s len=%d\n", data, uri, flags, computeOnly,
2640 debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len);
2641 return TRUE;
2644 /* Determines how the URI represented by the parse_data should be canonicalized.
2646 * Essentially, if the parse_data represents an hierarchical URI then it calls
2647 * canonicalize_authority and the canonicalization functions for the path. If the
2648 * URI is opaque it canonicalizes the path of the URI.
2650 static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2651 if(!data->is_opaque) {
2652 /* "//" is only added for non-wildcard scheme types. */
2653 if(data->scheme_type != URL_SCHEME_WILDCARD) {
2654 if(!computeOnly) {
2655 INT pos = uri->canon_len;
2657 uri->canon_uri[pos] = '/';
2658 uri->canon_uri[pos+1] = '/';
2660 uri->canon_len += 2;
2663 if(!canonicalize_authority(data, uri, flags, computeOnly))
2664 return FALSE;
2666 /* TODO: Canonicalize the path of the URI. */
2667 if(!canonicalize_path_hierarchical(data, uri, flags, computeOnly))
2668 return FALSE;
2670 } else {
2671 /* Opaque URI's don't have an authority. */
2672 uri->userinfo_start = uri->userinfo_split = -1;
2673 uri->userinfo_len = 0;
2674 uri->host_start = -1;
2675 uri->host_len = 0;
2676 uri->host_type = Uri_HOST_UNKNOWN;
2677 uri->has_port = FALSE;
2678 uri->authority_start = -1;
2679 uri->authority_len = 0;
2680 uri->domain_offset = -1;
2682 if(!canonicalize_path_opaque(data, uri, flags, computeOnly))
2683 return FALSE;
2686 if(uri->path_start > -1 && !computeOnly)
2687 /* Finding file extensions happens for both types of URIs. */
2688 uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len);
2689 else
2690 uri->extension_offset = -1;
2692 return TRUE;
2695 /* Attempts to canonicalize the query string of the URI.
2697 * Things that happen:
2698 * 1) For known scheme types forbidden characters
2699 * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set
2700 * or NO_ENCODE_FORBIDDEN_CHARACTERS is set.
2702 * 2) For known scheme types, percent encoded, unreserved characters
2703 * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set.
2705 static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2706 const WCHAR *ptr, *end;
2707 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2709 if(!data->query) {
2710 uri->query_start = -1;
2711 uri->query_len = 0;
2712 return TRUE;
2715 uri->query_start = uri->canon_len;
2717 end = data->query+data->query_len;
2718 for(ptr = data->query; ptr < end; ++ptr) {
2719 if(*ptr == '%') {
2720 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2721 WCHAR val = decode_pct_val(ptr);
2722 if(is_unreserved(val)) {
2723 if(!computeOnly)
2724 uri->canon_uri[uri->canon_len] = val;
2725 ++uri->canon_len;
2727 ptr += 2;
2728 continue;
2731 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2732 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2733 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2734 if(!computeOnly)
2735 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2736 uri->canon_len += 3;
2737 continue;
2741 if(!computeOnly)
2742 uri->canon_uri[uri->canon_len] = *ptr;
2743 ++uri->canon_len;
2746 uri->query_len = uri->canon_len - uri->query_start;
2748 if(!computeOnly)
2749 TRACE("(%p %p %x %d): Canonicalized query string %s len=%d\n", data, uri, flags,
2750 computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len),
2751 uri->query_len);
2752 return TRUE;
2755 static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2756 const WCHAR *ptr, *end;
2757 const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN;
2759 if(!data->fragment) {
2760 uri->fragment_start = -1;
2761 uri->fragment_len = 0;
2762 return TRUE;
2765 uri->fragment_start = uri->canon_len;
2767 end = data->fragment + data->fragment_len;
2768 for(ptr = data->fragment; ptr < end; ++ptr) {
2769 if(*ptr == '%') {
2770 if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2771 WCHAR val = decode_pct_val(ptr);
2772 if(is_unreserved(val)) {
2773 if(!computeOnly)
2774 uri->canon_uri[uri->canon_len] = val;
2775 ++uri->canon_len;
2777 ptr += 2;
2778 continue;
2781 } else if(known_scheme && !is_unreserved(*ptr) && !is_reserved(*ptr)) {
2782 if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) &&
2783 !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) {
2784 if(!computeOnly)
2785 pct_encode_val(*ptr, uri->canon_uri+uri->canon_len);
2786 uri->canon_len += 3;
2787 continue;
2791 if(!computeOnly)
2792 uri->canon_uri[uri->canon_len] = *ptr;
2793 ++uri->canon_len;
2796 uri->fragment_len = uri->canon_len - uri->fragment_start;
2798 if(!computeOnly)
2799 TRACE("(%p %p %x %d): Canonicalized fragment %s len=%d\n", data, uri, flags,
2800 computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len),
2801 uri->fragment_len);
2802 return TRUE;
2805 /* Canonicalizes the scheme information specified in the parse_data using the specified flags. */
2806 static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) {
2807 uri->scheme_start = -1;
2808 uri->scheme_len = 0;
2810 if(!data->scheme) {
2811 /* The only type of URI that doesn't have to have a scheme is a relative
2812 * URI.
2814 if(!data->is_relative) {
2815 FIXME("(%p %p %x): Unable to determine the scheme type of %s.\n", data,
2816 uri, flags, debugstr_w(data->uri));
2817 return FALSE;
2819 } else {
2820 if(!computeOnly) {
2821 DWORD i;
2822 INT pos = uri->canon_len;
2824 for(i = 0; i < data->scheme_len; ++i) {
2825 /* Scheme name must be lower case after canonicalization. */
2826 uri->canon_uri[i + pos] = tolowerW(data->scheme[i]);
2829 uri->canon_uri[i + pos] = ':';
2830 uri->scheme_start = pos;
2832 TRACE("(%p %p %x): Canonicalized scheme=%s, len=%d.\n", data, uri, flags,
2833 debugstr_wn(uri->canon_uri, uri->scheme_len), data->scheme_len);
2836 /* This happens in both computation modes. */
2837 uri->canon_len += data->scheme_len + 1;
2838 uri->scheme_len = data->scheme_len;
2840 return TRUE;
2843 /* Compute's what the length of the URI specified by the parse_data will be
2844 * after canonicalization occurs using the specified flags.
2846 * This function will return a non-zero value indicating the length of the canonicalized
2847 * URI, or -1 on error.
2849 static int compute_canonicalized_length(const parse_data *data, DWORD flags) {
2850 Uri uri;
2852 memset(&uri, 0, sizeof(Uri));
2854 TRACE("(%p %x): Beginning to compute canonicalized length for URI %s\n", data, flags,
2855 debugstr_w(data->uri));
2857 if(!canonicalize_scheme(data, &uri, flags, TRUE)) {
2858 ERR("(%p %x): Failed to compute URI scheme length.\n", data, flags);
2859 return -1;
2862 if(!canonicalize_hierpart(data, &uri, flags, TRUE)) {
2863 ERR("(%p %x): Failed to compute URI hierpart length.\n", data, flags);
2864 return -1;
2867 if(!canonicalize_query(data, &uri, flags, TRUE)) {
2868 ERR("(%p %x): Failed to compute query string length.\n", data, flags);
2869 return -1;
2872 if(!canonicalize_fragment(data, &uri, flags, TRUE)) {
2873 ERR("(%p %x): Failed to compute fragment length.\n", data, flags);
2874 return -1;
2877 TRACE("(%p %x): Finished computing canonicalized URI length. length=%d\n", data, flags, uri.canon_len);
2879 return uri.canon_len;
2882 /* Canonicalizes the URI data specified in the parse_data, using the given flags. If the
2883 * canonicalization succeededs it will store all the canonicalization information
2884 * in the pointer to the Uri.
2886 * To canonicalize a URI this function first computes what the length of the URI
2887 * specified by the parse_data will be. Once this is done it will then perfom the actual
2888 * canonicalization of the URI.
2890 static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) {
2891 INT len;
2893 uri->canon_uri = NULL;
2894 len = uri->canon_size = uri->canon_len = 0;
2896 TRACE("(%p %p %x): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri));
2898 /* First try to compute the length of the URI. */
2899 len = compute_canonicalized_length(data, flags);
2900 if(len == -1) {
2901 ERR("(%p %p %x): Could not compute the canonicalized length of %s.\n", data, uri, flags,
2902 debugstr_w(data->uri));
2903 return E_INVALIDARG;
2906 uri->canon_uri = heap_alloc((len+1)*sizeof(WCHAR));
2907 if(!uri->canon_uri)
2908 return E_OUTOFMEMORY;
2910 uri->canon_size = len;
2911 if(!canonicalize_scheme(data, uri, flags, FALSE)) {
2912 ERR("(%p %p %x): Unable to canonicalize the scheme of the URI.\n", data, uri, flags);
2913 heap_free(uri->canon_uri);
2914 return E_INVALIDARG;
2916 uri->scheme_type = data->scheme_type;
2918 if(!canonicalize_hierpart(data, uri, flags, FALSE)) {
2919 ERR("(%p %p %x): Unable to canonicalize the heirpart of the URI\n", data, uri, flags);
2920 heap_free(uri->canon_uri);
2921 return E_INVALIDARG;
2924 if(!canonicalize_query(data, uri, flags, FALSE)) {
2925 ERR("(%p %p %x): Unable to canonicalize query string of the URI.\n",
2926 data, uri, flags);
2927 return E_INVALIDARG;
2930 if(!canonicalize_fragment(data, uri, flags, FALSE)) {
2931 ERR("(%p %p %x): Unable to canonicalize fragment of the URI.\n",
2932 data, uri, flags);
2933 return E_INVALIDARG;
2936 /* There's a possibility we didn't use all the space we allocated
2937 * earlier.
2939 if(uri->canon_len < uri->canon_size) {
2940 /* This happens if the URI is hierarchical and dot
2941 * segments were removed from it's path.
2943 WCHAR *tmp = heap_realloc(uri->canon_uri, (uri->canon_len+1)*sizeof(WCHAR));
2944 if(!tmp)
2945 return E_OUTOFMEMORY;
2947 uri->canon_uri = tmp;
2948 uri->canon_size = uri->canon_len;
2951 uri->canon_uri[uri->canon_len] = '\0';
2952 TRACE("(%p %p %x): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri));
2954 return S_OK;
2957 #define URI(x) ((IUri*) &(x)->lpIUriVtbl)
2958 #define URIBUILDER(x) ((IUriBuilder*) &(x)->lpIUriBuilderVtbl)
2960 #define URI_THIS(iface) DEFINE_THIS(Uri, IUri, iface)
2962 static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv)
2964 Uri *This = URI_THIS(iface);
2966 if(IsEqualGUID(&IID_IUnknown, riid)) {
2967 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
2968 *ppv = URI(This);
2969 }else if(IsEqualGUID(&IID_IUri, riid)) {
2970 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
2971 *ppv = URI(This);
2972 }else {
2973 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
2974 *ppv = NULL;
2975 return E_NOINTERFACE;
2978 IUnknown_AddRef((IUnknown*)*ppv);
2979 return S_OK;
2982 static ULONG WINAPI Uri_AddRef(IUri *iface)
2984 Uri *This = URI_THIS(iface);
2985 LONG ref = InterlockedIncrement(&This->ref);
2987 TRACE("(%p) ref=%d\n", This, ref);
2989 return ref;
2992 static ULONG WINAPI Uri_Release(IUri *iface)
2994 Uri *This = URI_THIS(iface);
2995 LONG ref = InterlockedDecrement(&This->ref);
2997 TRACE("(%p) ref=%d\n", This, ref);
2999 if(!ref) {
3000 SysFreeString(This->raw_uri);
3001 heap_free(This->canon_uri);
3002 heap_free(This);
3005 return ref;
3008 static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags)
3010 Uri *This = URI_THIS(iface);
3011 HRESULT hres;
3012 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3014 if(!pbstrProperty)
3015 return E_POINTER;
3017 if(uriProp > Uri_PROPERTY_STRING_LAST) {
3018 /* Windows allocates an empty BSTR for invalid Uri_PROPERTY's. */
3019 *pbstrProperty = SysAllocStringLen(NULL, 0);
3020 if(!(*pbstrProperty))
3021 return E_OUTOFMEMORY;
3023 /* It only returns S_FALSE for the ZONE property... */
3024 if(uriProp == Uri_PROPERTY_ZONE)
3025 return S_FALSE;
3026 else
3027 return S_OK;
3030 /* Don't have support for flags yet. */
3031 if(dwFlags) {
3032 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3033 return E_NOTIMPL;
3036 switch(uriProp) {
3037 case Uri_PROPERTY_ABSOLUTE_URI:
3038 *pbstrProperty = SysAllocString(This->canon_uri);
3040 if(!(*pbstrProperty))
3041 hres = E_OUTOFMEMORY;
3042 else
3043 hres = S_OK;
3045 break;
3046 case Uri_PROPERTY_AUTHORITY:
3047 if(This->authority_start > -1) {
3048 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len);
3049 hres = S_OK;
3050 } else {
3051 *pbstrProperty = SysAllocStringLen(NULL, 0);
3052 hres = S_FALSE;
3055 if(!(*pbstrProperty))
3056 hres = E_OUTOFMEMORY;
3058 break;
3059 case Uri_PROPERTY_DISPLAY_URI:
3060 /* The Display URI contains everything except for the userinfo for known
3061 * scheme types.
3063 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) {
3064 *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->userinfo_len);
3066 if(*pbstrProperty) {
3067 /* Copy everything before the userinfo over. */
3068 memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR));
3069 /* Copy everything after the userinfo over. */
3070 memcpy(*pbstrProperty+This->userinfo_start,
3071 This->canon_uri+This->userinfo_start+This->userinfo_len+1,
3072 (This->canon_len-(This->userinfo_start+This->userinfo_len+1))*sizeof(WCHAR));
3074 } else
3075 *pbstrProperty = SysAllocString(This->canon_uri);
3077 if(!(*pbstrProperty))
3078 hres = E_OUTOFMEMORY;
3079 else
3080 hres = S_OK;
3082 break;
3083 case Uri_PROPERTY_DOMAIN:
3084 if(This->domain_offset > -1) {
3085 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset,
3086 This->host_len-This->domain_offset);
3087 hres = S_OK;
3088 } else {
3089 *pbstrProperty = SysAllocStringLen(NULL, 0);
3090 hres = S_FALSE;
3093 if(!(*pbstrProperty))
3094 hres = E_OUTOFMEMORY;
3096 break;
3097 case Uri_PROPERTY_EXTENSION:
3098 if(This->extension_offset > -1) {
3099 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset,
3100 This->path_len-This->extension_offset);
3101 hres = S_OK;
3102 } else {
3103 *pbstrProperty = SysAllocStringLen(NULL, 0);
3104 hres = S_FALSE;
3107 if(!(*pbstrProperty))
3108 hres = E_OUTOFMEMORY;
3110 break;
3111 case Uri_PROPERTY_FRAGMENT:
3112 if(This->fragment_start > -1) {
3113 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len);
3114 hres = S_OK;
3115 } else {
3116 *pbstrProperty = SysAllocStringLen(NULL, 0);
3117 hres = S_FALSE;
3120 if(!(*pbstrProperty))
3121 hres = E_OUTOFMEMORY;
3123 break;
3124 case Uri_PROPERTY_HOST:
3125 if(This->host_start > -1) {
3126 /* The '[' and ']' aren't included for IPv6 addresses. */
3127 if(This->host_type == Uri_HOST_IPV6)
3128 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2);
3129 else
3130 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len);
3132 hres = S_OK;
3133 } else {
3134 *pbstrProperty = SysAllocStringLen(NULL, 0);
3135 hres = S_FALSE;
3138 if(!(*pbstrProperty))
3139 hres = E_OUTOFMEMORY;
3141 break;
3142 case Uri_PROPERTY_PASSWORD:
3143 if(This->userinfo_split > -1) {
3144 *pbstrProperty = SysAllocStringLen(
3145 This->canon_uri+This->userinfo_start+This->userinfo_split+1,
3146 This->userinfo_len-This->userinfo_split-1);
3147 hres = S_OK;
3148 } else {
3149 *pbstrProperty = SysAllocStringLen(NULL, 0);
3150 hres = S_FALSE;
3153 if(!(*pbstrProperty))
3154 return E_OUTOFMEMORY;
3156 break;
3157 case Uri_PROPERTY_PATH:
3158 if(This->path_start > -1) {
3159 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len);
3160 hres = S_OK;
3161 } else {
3162 *pbstrProperty = SysAllocStringLen(NULL, 0);
3163 hres = S_FALSE;
3166 if(!(*pbstrProperty))
3167 hres = E_OUTOFMEMORY;
3169 break;
3170 case Uri_PROPERTY_PATH_AND_QUERY:
3171 if(This->path_start > -1) {
3172 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len);
3173 hres = S_OK;
3174 } else if(This->query_start > -1) {
3175 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
3176 hres = S_OK;
3177 } else {
3178 *pbstrProperty = SysAllocStringLen(NULL, 0);
3179 hres = S_FALSE;
3182 if(!(*pbstrProperty))
3183 hres = E_OUTOFMEMORY;
3185 break;
3186 case Uri_PROPERTY_QUERY:
3187 if(This->query_start > -1) {
3188 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len);
3189 hres = S_OK;
3190 } else {
3191 *pbstrProperty = SysAllocStringLen(NULL, 0);
3192 hres = S_FALSE;
3195 if(!(*pbstrProperty))
3196 hres = E_OUTOFMEMORY;
3198 break;
3199 case Uri_PROPERTY_RAW_URI:
3200 *pbstrProperty = SysAllocString(This->raw_uri);
3201 if(!(*pbstrProperty))
3202 hres = E_OUTOFMEMORY;
3203 else
3204 hres = S_OK;
3205 break;
3206 case Uri_PROPERTY_SCHEME_NAME:
3207 if(This->scheme_start > -1) {
3208 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len);
3209 hres = S_OK;
3210 } else {
3211 *pbstrProperty = SysAllocStringLen(NULL, 0);
3212 hres = S_FALSE;
3215 if(!(*pbstrProperty))
3216 hres = E_OUTOFMEMORY;
3218 break;
3219 case Uri_PROPERTY_USER_INFO:
3220 if(This->userinfo_start > -1) {
3221 *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len);
3222 hres = S_OK;
3223 } else {
3224 *pbstrProperty = SysAllocStringLen(NULL, 0);
3225 hres = S_FALSE;
3228 if(!(*pbstrProperty))
3229 hres = E_OUTOFMEMORY;
3231 break;
3232 case Uri_PROPERTY_USER_NAME:
3233 if(This->userinfo_start > -1) {
3234 /* If userinfo_split is set, that means a password exists
3235 * so the username is only from userinfo_start to userinfo_split.
3237 if(This->userinfo_split > -1) {
3238 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split);
3239 hres = S_OK;
3240 } else {
3241 *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len);
3242 hres = S_OK;
3244 } else {
3245 *pbstrProperty = SysAllocStringLen(NULL, 0);
3246 hres = S_FALSE;
3249 if(!(*pbstrProperty))
3250 return E_OUTOFMEMORY;
3252 break;
3253 default:
3254 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pbstrProperty, dwFlags);
3255 hres = E_NOTIMPL;
3258 return hres;
3261 static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3263 Uri *This = URI_THIS(iface);
3264 HRESULT hres;
3265 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3267 if(!pcchProperty)
3268 return E_INVALIDARG;
3270 /* Can only return a length for a property if it's a string. */
3271 if(uriProp > Uri_PROPERTY_STRING_LAST)
3272 return E_INVALIDARG;
3274 /* Don't have support for flags yet. */
3275 if(dwFlags) {
3276 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3277 return E_NOTIMPL;
3280 switch(uriProp) {
3281 case Uri_PROPERTY_ABSOLUTE_URI:
3282 *pcchProperty = This->canon_len;
3283 hres = S_OK;
3284 break;
3285 case Uri_PROPERTY_AUTHORITY:
3286 *pcchProperty = This->authority_len;
3287 hres = (This->authority_start > -1) ? S_OK : S_FALSE;
3288 break;
3289 case Uri_PROPERTY_DISPLAY_URI:
3290 if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1)
3291 *pcchProperty = This->canon_len-This->userinfo_len-1;
3292 else
3293 *pcchProperty = This->canon_len;
3295 hres = S_OK;
3296 break;
3297 case Uri_PROPERTY_DOMAIN:
3298 if(This->domain_offset > -1)
3299 *pcchProperty = This->host_len - This->domain_offset;
3300 else
3301 *pcchProperty = 0;
3303 hres = (This->domain_offset > -1) ? S_OK : S_FALSE;
3304 break;
3305 case Uri_PROPERTY_EXTENSION:
3306 if(This->extension_offset > -1) {
3307 *pcchProperty = This->path_len - This->extension_offset;
3308 hres = S_OK;
3309 } else {
3310 *pcchProperty = 0;
3311 hres = S_FALSE;
3314 break;
3315 case Uri_PROPERTY_FRAGMENT:
3316 *pcchProperty = This->fragment_len;
3317 hres = (This->fragment_start > -1) ? S_OK : S_FALSE;
3318 break;
3319 case Uri_PROPERTY_HOST:
3320 *pcchProperty = This->host_len;
3322 /* '[' and ']' aren't included in the length. */
3323 if(This->host_type == Uri_HOST_IPV6)
3324 *pcchProperty -= 2;
3326 hres = (This->host_start > -1) ? S_OK : S_FALSE;
3327 break;
3328 case Uri_PROPERTY_PASSWORD:
3329 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0;
3330 hres = (This->userinfo_split > -1) ? S_OK : S_FALSE;
3331 break;
3332 case Uri_PROPERTY_PATH:
3333 *pcchProperty = This->path_len;
3334 hres = (This->path_start > -1) ? S_OK : S_FALSE;
3335 break;
3336 case Uri_PROPERTY_PATH_AND_QUERY:
3337 *pcchProperty = This->path_len+This->query_len;
3338 hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE;
3339 break;
3340 case Uri_PROPERTY_QUERY:
3341 *pcchProperty = This->query_len;
3342 hres = (This->query_start > -1) ? S_OK : S_FALSE;
3343 break;
3344 case Uri_PROPERTY_RAW_URI:
3345 *pcchProperty = SysStringLen(This->raw_uri);
3346 hres = S_OK;
3347 break;
3348 case Uri_PROPERTY_SCHEME_NAME:
3349 *pcchProperty = This->scheme_len;
3350 hres = (This->scheme_start > -1) ? S_OK : S_FALSE;
3351 break;
3352 case Uri_PROPERTY_USER_INFO:
3353 *pcchProperty = This->userinfo_len;
3354 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3355 break;
3356 case Uri_PROPERTY_USER_NAME:
3357 *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len;
3358 hres = (This->userinfo_start > -1) ? S_OK : S_FALSE;
3359 break;
3360 default:
3361 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3362 hres = E_NOTIMPL;
3365 return hres;
3368 static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags)
3370 Uri *This = URI_THIS(iface);
3371 HRESULT hres;
3373 TRACE("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3375 if(!pcchProperty)
3376 return E_INVALIDARG;
3378 /* Microsoft's implementation for the ZONE property of a URI seems to be lacking...
3379 * From what I can tell, instead of checking which URLZONE the URI belongs to it
3380 * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone
3381 * function.
3383 if(uriProp == Uri_PROPERTY_ZONE) {
3384 *pcchProperty = URLZONE_INVALID;
3385 return E_NOTIMPL;
3388 if(uriProp < Uri_PROPERTY_DWORD_START) {
3389 *pcchProperty = 0;
3390 return E_INVALIDARG;
3393 switch(uriProp) {
3394 case Uri_PROPERTY_HOST_TYPE:
3395 *pcchProperty = This->host_type;
3396 hres = S_OK;
3397 break;
3398 case Uri_PROPERTY_PORT:
3399 if(!This->has_port) {
3400 *pcchProperty = 0;
3401 hres = S_FALSE;
3402 } else {
3403 *pcchProperty = This->port;
3404 hres = S_OK;
3407 break;
3408 case Uri_PROPERTY_SCHEME:
3409 *pcchProperty = This->scheme_type;
3410 hres = S_OK;
3411 break;
3412 default:
3413 FIXME("(%p)->(%d %p %x)\n", This, uriProp, pcchProperty, dwFlags);
3414 hres = E_NOTIMPL;
3417 return hres;
3420 static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty)
3422 Uri *This = URI_THIS(iface);
3423 TRACE("(%p)->(%d %p)\n", This, uriProp, pfHasProperty);
3425 if(!pfHasProperty)
3426 return E_INVALIDARG;
3428 switch(uriProp) {
3429 case Uri_PROPERTY_ABSOLUTE_URI:
3430 *pfHasProperty = TRUE;
3431 break;
3432 case Uri_PROPERTY_AUTHORITY:
3433 *pfHasProperty = This->authority_start > -1;
3434 break;
3435 case Uri_PROPERTY_DISPLAY_URI:
3436 *pfHasProperty = TRUE;
3437 break;
3438 case Uri_PROPERTY_DOMAIN:
3439 *pfHasProperty = This->domain_offset > -1;
3440 break;
3441 case Uri_PROPERTY_EXTENSION:
3442 *pfHasProperty = This->extension_offset > -1;
3443 break;
3444 case Uri_PROPERTY_FRAGMENT:
3445 *pfHasProperty = This->fragment_start > -1;
3446 break;
3447 case Uri_PROPERTY_HOST:
3448 *pfHasProperty = This->host_start > -1;
3449 break;
3450 case Uri_PROPERTY_PASSWORD:
3451 *pfHasProperty = This->userinfo_split > -1;
3452 break;
3453 case Uri_PROPERTY_PATH:
3454 *pfHasProperty = This->path_start > -1;
3455 break;
3456 case Uri_PROPERTY_PATH_AND_QUERY:
3457 *pfHasProperty = (This->path_start > -1 || This->query_start > -1);
3458 break;
3459 case Uri_PROPERTY_QUERY:
3460 *pfHasProperty = This->query_start > -1;
3461 break;
3462 case Uri_PROPERTY_RAW_URI:
3463 *pfHasProperty = TRUE;
3464 break;
3465 case Uri_PROPERTY_SCHEME_NAME:
3466 *pfHasProperty = This->scheme_start > -1;
3467 break;
3468 case Uri_PROPERTY_USER_INFO:
3469 case Uri_PROPERTY_USER_NAME:
3470 *pfHasProperty = This->userinfo_start > -1;
3471 break;
3472 case Uri_PROPERTY_HOST_TYPE:
3473 *pfHasProperty = TRUE;
3474 break;
3475 case Uri_PROPERTY_PORT:
3476 *pfHasProperty = This->has_port;
3477 break;
3478 case Uri_PROPERTY_SCHEME:
3479 *pfHasProperty = TRUE;
3480 break;
3481 case Uri_PROPERTY_ZONE:
3482 *pfHasProperty = FALSE;
3483 break;
3484 default:
3485 FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty);
3486 return E_NOTIMPL;
3489 return S_OK;
3492 static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri)
3494 TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri);
3495 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0);
3498 static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority)
3500 TRACE("(%p)->(%p)\n", iface, pstrAuthority);
3501 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0);
3504 static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri)
3506 TRACE("(%p)->(%p)\n", iface, pstrDisplayUri);
3507 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0);
3510 static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain)
3512 TRACE("(%p)->(%p)\n", iface, pstrDomain);
3513 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0);
3516 static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension)
3518 TRACE("(%p)->(%p)\n", iface, pstrExtension);
3519 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0);
3522 static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment)
3524 TRACE("(%p)->(%p)\n", iface, pstrFragment);
3525 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0);
3528 static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost)
3530 TRACE("(%p)->(%p)\n", iface, pstrHost);
3531 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0);
3534 static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword)
3536 TRACE("(%p)->(%p)\n", iface, pstrPassword);
3537 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0);
3540 static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath)
3542 TRACE("(%p)->(%p)\n", iface, pstrPath);
3543 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0);
3546 static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery)
3548 TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery);
3549 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0);
3552 static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery)
3554 TRACE("(%p)->(%p)\n", iface, pstrQuery);
3555 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0);
3558 static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri)
3560 Uri *This = URI_THIS(iface);
3561 TRACE("(%p)->(%p)\n", This, pstrRawUri);
3563 /* Just forward the call to GetPropertyBSTR. */
3564 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0);
3567 static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName)
3569 Uri *This = URI_THIS(iface);
3570 TRACE("(%p)->(%p)\n", This, pstrSchemeName);
3571 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0);
3574 static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo)
3576 TRACE("(%p)->(%p)\n", iface, pstrUserInfo);
3577 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0);
3580 static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName)
3582 TRACE("(%p)->(%p)\n", iface, pstrUserName);
3583 return Uri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0);
3586 static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType)
3588 TRACE("(%p)->(%p)\n", iface, pdwHostType);
3589 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0);
3592 static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort)
3594 TRACE("(%p)->(%p)\n", iface, pdwPort);
3595 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0);
3598 static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme)
3600 Uri *This = URI_THIS(iface);
3601 TRACE("(%p)->(%p)\n", This, pdwScheme);
3602 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0);
3605 static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone)
3607 TRACE("(%p)->(%p)\n", iface, pdwZone);
3608 return Uri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0);
3611 static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties)
3613 Uri *This = URI_THIS(iface);
3614 TRACE("(%p)->(%p)\n", This, pdwProperties);
3616 if(!pdwProperties)
3617 return E_INVALIDARG;
3619 /* All URIs have these. */
3620 *pdwProperties = Uri_HAS_ABSOLUTE_URI|Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|
3621 Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE;
3623 if(This->scheme_start > -1)
3624 *pdwProperties |= Uri_HAS_SCHEME_NAME;
3626 if(This->authority_start > -1) {
3627 *pdwProperties |= Uri_HAS_AUTHORITY;
3628 if(This->userinfo_start > -1)
3629 *pdwProperties |= Uri_HAS_USER_INFO|Uri_HAS_USER_NAME;
3630 if(This->userinfo_split > -1)
3631 *pdwProperties |= Uri_HAS_PASSWORD;
3632 if(This->host_start > -1)
3633 *pdwProperties |= Uri_HAS_HOST;
3634 if(This->domain_offset > -1)
3635 *pdwProperties |= Uri_HAS_DOMAIN;
3636 if(This->has_port)
3637 *pdwProperties |= Uri_HAS_PORT;
3640 if(This->path_start > -1)
3641 *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY;
3642 if(This->query_start > -1)
3643 *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY;
3645 if(This->extension_offset > -1)
3646 *pdwProperties |= Uri_HAS_EXTENSION;
3648 if(This->fragment_start > -1)
3649 *pdwProperties |= Uri_HAS_FRAGMENT;
3651 return S_OK;
3654 static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual)
3656 Uri *This = URI_THIS(iface);
3657 TRACE("(%p)->(%p %p)\n", This, pUri, pfEqual);
3659 if(!pfEqual)
3660 return E_POINTER;
3662 if(!pUri) {
3663 *pfEqual = FALSE;
3665 /* For some reason Windows returns S_OK here... */
3666 return S_OK;
3669 FIXME("(%p)->(%p %p)\n", This, pUri, pfEqual);
3670 return E_NOTIMPL;
3673 #undef URI_THIS
3675 static const IUriVtbl UriVtbl = {
3676 Uri_QueryInterface,
3677 Uri_AddRef,
3678 Uri_Release,
3679 Uri_GetPropertyBSTR,
3680 Uri_GetPropertyLength,
3681 Uri_GetPropertyDWORD,
3682 Uri_HasProperty,
3683 Uri_GetAbsoluteUri,
3684 Uri_GetAuthority,
3685 Uri_GetDisplayUri,
3686 Uri_GetDomain,
3687 Uri_GetExtension,
3688 Uri_GetFragment,
3689 Uri_GetHost,
3690 Uri_GetPassword,
3691 Uri_GetPath,
3692 Uri_GetPathAndQuery,
3693 Uri_GetQuery,
3694 Uri_GetRawUri,
3695 Uri_GetSchemeName,
3696 Uri_GetUserInfo,
3697 Uri_GetUserName,
3698 Uri_GetHostType,
3699 Uri_GetPort,
3700 Uri_GetScheme,
3701 Uri_GetZone,
3702 Uri_GetProperties,
3703 Uri_IsEqual
3706 /***********************************************************************
3707 * CreateUri (urlmon.@)
3709 HRESULT WINAPI CreateUri(LPCWSTR pwzURI, DWORD dwFlags, DWORD_PTR dwReserved, IUri **ppURI)
3711 Uri *ret;
3712 HRESULT hr;
3713 parse_data data;
3715 TRACE("(%s %x %x %p)\n", debugstr_w(pwzURI), dwFlags, (DWORD)dwReserved, ppURI);
3717 if(!ppURI)
3718 return E_INVALIDARG;
3720 if(!pwzURI) {
3721 *ppURI = NULL;
3722 return E_INVALIDARG;
3725 ret = heap_alloc(sizeof(Uri));
3726 if(!ret)
3727 return E_OUTOFMEMORY;
3729 ret->lpIUriVtbl = &UriVtbl;
3730 ret->ref = 1;
3732 /* Create a copy of pwzURI and store it as the raw_uri. */
3733 ret->raw_uri = SysAllocString(pwzURI);
3734 if(!ret->raw_uri) {
3735 heap_free(ret);
3736 return E_OUTOFMEMORY;
3739 memset(&data, 0, sizeof(parse_data));
3740 data.uri = ret->raw_uri;
3742 /* Validate and parse the URI into it's components. */
3743 if(!parse_uri(&data, dwFlags)) {
3744 /* Encountered an unsupported or invalid URI */
3745 SysFreeString(ret->raw_uri);
3746 heap_free(ret);
3747 *ppURI = NULL;
3748 return E_INVALIDARG;
3751 /* Canonicalize the URI. */
3752 hr = canonicalize_uri(&data, ret, dwFlags);
3753 if(FAILED(hr)) {
3754 SysFreeString(ret->raw_uri);
3755 heap_free(ret);
3756 *ppURI = NULL;
3757 return hr;
3760 *ppURI = URI(ret);
3761 return S_OK;
3764 #define URIBUILDER_THIS(iface) DEFINE_THIS(UriBuilder, IUriBuilder, iface)
3766 static HRESULT WINAPI UriBuilder_QueryInterface(IUriBuilder *iface, REFIID riid, void **ppv)
3768 UriBuilder *This = URIBUILDER_THIS(iface);
3770 if(IsEqualGUID(&IID_IUnknown, riid)) {
3771 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
3772 *ppv = URIBUILDER(This);
3773 }else if(IsEqualGUID(&IID_IUriBuilder, riid)) {
3774 TRACE("(%p)->(IID_IUri %p)\n", This, ppv);
3775 *ppv = URIBUILDER(This);
3776 }else {
3777 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
3778 *ppv = NULL;
3779 return E_NOINTERFACE;
3782 IUnknown_AddRef((IUnknown*)*ppv);
3783 return S_OK;
3786 static ULONG WINAPI UriBuilder_AddRef(IUriBuilder *iface)
3788 UriBuilder *This = URIBUILDER_THIS(iface);
3789 LONG ref = InterlockedIncrement(&This->ref);
3791 TRACE("(%p) ref=%d\n", This, ref);
3793 return ref;
3796 static ULONG WINAPI UriBuilder_Release(IUriBuilder *iface)
3798 UriBuilder *This = URIBUILDER_THIS(iface);
3799 LONG ref = InterlockedDecrement(&This->ref);
3801 TRACE("(%p) ref=%d\n", This, ref);
3803 if(!ref)
3804 heap_free(This);
3806 return ref;
3809 static HRESULT WINAPI UriBuilder_CreateUriSimple(IUriBuilder *iface,
3810 DWORD dwAllowEncodingPropertyMask,
3811 DWORD_PTR dwReserved,
3812 IUri **ppIUri)
3814 UriBuilder *This = URIBUILDER_THIS(iface);
3815 FIXME("(%p)->(%d %d %p)\n", This, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3816 return E_NOTIMPL;
3819 static HRESULT WINAPI UriBuilder_CreateUri(IUriBuilder *iface,
3820 DWORD dwCreateFlags,
3821 DWORD dwAllowEncodingPropertyMask,
3822 DWORD_PTR dwReserved,
3823 IUri **ppIUri)
3825 UriBuilder *This = URIBUILDER_THIS(iface);
3826 FIXME("(%p)->(0x%08x %d %d %p)\n", This, dwCreateFlags, dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3827 return E_NOTIMPL;
3830 static HRESULT WINAPI UriBuilder_CreateUriWithFlags(IUriBuilder *iface,
3831 DWORD dwCreateFlags,
3832 DWORD dwUriBuilderFlags,
3833 DWORD dwAllowEncodingPropertyMask,
3834 DWORD_PTR dwReserved,
3835 IUri **ppIUri)
3837 UriBuilder *This = URIBUILDER_THIS(iface);
3838 FIXME("(%p)->(0x%08x 0x%08x %d %d %p)\n", This, dwCreateFlags, dwUriBuilderFlags,
3839 dwAllowEncodingPropertyMask, (DWORD)dwReserved, ppIUri);
3840 return E_NOTIMPL;
3843 static HRESULT WINAPI UriBuilder_GetIUri(IUriBuilder *iface, IUri **ppIUri)
3845 UriBuilder *This = URIBUILDER_THIS(iface);
3846 FIXME("(%p)->(%p)\n", This, ppIUri);
3847 return E_NOTIMPL;
3850 static HRESULT WINAPI UriBuilder_SetIUri(IUriBuilder *iface, IUri *pIUri)
3852 UriBuilder *This = URIBUILDER_THIS(iface);
3853 FIXME("(%p)->(%p)\n", This, pIUri);
3854 return E_NOTIMPL;
3857 static HRESULT WINAPI UriBuilder_GetFragment(IUriBuilder *iface, DWORD *pcchFragment, LPCWSTR *ppwzFragment)
3859 UriBuilder *This = URIBUILDER_THIS(iface);
3860 FIXME("(%p)->(%p %p)\n", This, pcchFragment, ppwzFragment);
3861 return E_NOTIMPL;
3864 static HRESULT WINAPI UriBuilder_GetHost(IUriBuilder *iface, DWORD *pcchHost, LPCWSTR *ppwzHost)
3866 UriBuilder *This = URIBUILDER_THIS(iface);
3867 FIXME("(%p)->(%p %p)\n", This, pcchHost, ppwzHost);
3868 return E_NOTIMPL;
3871 static HRESULT WINAPI UriBuilder_GetPassword(IUriBuilder *iface, DWORD *pcchPassword, LPCWSTR *ppwzPassword)
3873 UriBuilder *This = URIBUILDER_THIS(iface);
3874 FIXME("(%p)->(%p %p)\n", This, pcchPassword, ppwzPassword);
3875 return E_NOTIMPL;
3878 static HRESULT WINAPI UriBuilder_GetPath(IUriBuilder *iface, DWORD *pcchPath, LPCWSTR *ppwzPath)
3880 UriBuilder *This = URIBUILDER_THIS(iface);
3881 FIXME("(%p)->(%p %p)\n", This, pcchPath, ppwzPath);
3882 return E_NOTIMPL;
3885 static HRESULT WINAPI UriBuilder_GetPort(IUriBuilder *iface, BOOL *pfHasPort, DWORD *pdwPort)
3887 UriBuilder *This = URIBUILDER_THIS(iface);
3888 FIXME("(%p)->(%p %p)\n", This, pfHasPort, pdwPort);
3889 return E_NOTIMPL;
3892 static HRESULT WINAPI UriBuilder_GetQuery(IUriBuilder *iface, DWORD *pcchQuery, LPCWSTR *ppwzQuery)
3894 UriBuilder *This = URIBUILDER_THIS(iface);
3895 FIXME("(%p)->(%p %p)\n", This, pcchQuery, ppwzQuery);
3896 return E_NOTIMPL;
3899 static HRESULT WINAPI UriBuilder_GetSchemeName(IUriBuilder *iface, DWORD *pcchSchemeName, LPCWSTR *ppwzSchemeName)
3901 UriBuilder *This = URIBUILDER_THIS(iface);
3902 FIXME("(%p)->(%p %p)\n", This, pcchSchemeName, ppwzSchemeName);
3903 return E_NOTIMPL;
3906 static HRESULT WINAPI UriBuilder_GetUserName(IUriBuilder *iface, DWORD *pcchUserName, LPCWSTR *ppwzUserName)
3908 UriBuilder *This = URIBUILDER_THIS(iface);
3909 FIXME("(%p)->(%p %p)\n", This, pcchUserName, ppwzUserName);
3910 return E_NOTIMPL;
3913 static HRESULT WINAPI UriBuilder_SetFragment(IUriBuilder *iface, LPCWSTR pwzNewValue)
3915 UriBuilder *This = URIBUILDER_THIS(iface);
3916 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3917 return E_NOTIMPL;
3920 static HRESULT WINAPI UriBuilder_SetHost(IUriBuilder *iface, LPCWSTR pwzNewValue)
3922 UriBuilder *This = URIBUILDER_THIS(iface);
3923 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3924 return E_NOTIMPL;
3927 static HRESULT WINAPI UriBuilder_SetPassword(IUriBuilder *iface, LPCWSTR pwzNewValue)
3929 UriBuilder *This = URIBUILDER_THIS(iface);
3930 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3931 return E_NOTIMPL;
3934 static HRESULT WINAPI UriBuilder_SetPath(IUriBuilder *iface, LPCWSTR pwzNewValue)
3936 UriBuilder *This = URIBUILDER_THIS(iface);
3937 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3938 return E_NOTIMPL;
3941 static HRESULT WINAPI UriBuilder_SetPort(IUriBuilder *iface, BOOL fHasPort, DWORD dwNewValue)
3943 UriBuilder *This = URIBUILDER_THIS(iface);
3944 FIXME("(%p)->(%d %d)\n", This, fHasPort, dwNewValue);
3945 return E_NOTIMPL;
3948 static HRESULT WINAPI UriBuilder_SetQuery(IUriBuilder *iface, LPCWSTR pwzNewValue)
3950 UriBuilder *This = URIBUILDER_THIS(iface);
3951 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3952 return E_NOTIMPL;
3955 static HRESULT WINAPI UriBuilder_SetSchemeName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3957 UriBuilder *This = URIBUILDER_THIS(iface);
3958 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3959 return E_NOTIMPL;
3962 static HRESULT WINAPI UriBuilder_SetUserName(IUriBuilder *iface, LPCWSTR pwzNewValue)
3964 UriBuilder *This = URIBUILDER_THIS(iface);
3965 FIXME("(%p)->(%s)\n", This, debugstr_w(pwzNewValue));
3966 return E_NOTIMPL;
3969 static HRESULT WINAPI UriBuilder_RemoveProperties(IUriBuilder *iface, DWORD dwPropertyMask)
3971 UriBuilder *This = URIBUILDER_THIS(iface);
3972 FIXME("(%p)->(0x%08x)\n", This, dwPropertyMask);
3973 return E_NOTIMPL;
3976 static HRESULT WINAPI UriBuilder_HasBeenModified(IUriBuilder *iface, BOOL *pfModified)
3978 UriBuilder *This = URIBUILDER_THIS(iface);
3979 FIXME("(%p)->(%p)\n", This, pfModified);
3980 return E_NOTIMPL;
3983 #undef URIBUILDER_THIS
3985 static const IUriBuilderVtbl UriBuilderVtbl = {
3986 UriBuilder_QueryInterface,
3987 UriBuilder_AddRef,
3988 UriBuilder_Release,
3989 UriBuilder_CreateUriSimple,
3990 UriBuilder_CreateUri,
3991 UriBuilder_CreateUriWithFlags,
3992 UriBuilder_GetIUri,
3993 UriBuilder_SetIUri,
3994 UriBuilder_GetFragment,
3995 UriBuilder_GetHost,
3996 UriBuilder_GetPassword,
3997 UriBuilder_GetPath,
3998 UriBuilder_GetPort,
3999 UriBuilder_GetQuery,
4000 UriBuilder_GetSchemeName,
4001 UriBuilder_GetUserName,
4002 UriBuilder_SetFragment,
4003 UriBuilder_SetHost,
4004 UriBuilder_SetPassword,
4005 UriBuilder_SetPath,
4006 UriBuilder_SetPort,
4007 UriBuilder_SetQuery,
4008 UriBuilder_SetSchemeName,
4009 UriBuilder_SetUserName,
4010 UriBuilder_RemoveProperties,
4011 UriBuilder_HasBeenModified,
4014 /***********************************************************************
4015 * CreateIUriBuilder (urlmon.@)
4017 HRESULT WINAPI CreateIUriBuilder(IUri *pIUri, DWORD dwFlags, DWORD_PTR dwReserved, IUriBuilder **ppIUriBuilder)
4019 UriBuilder *ret;
4021 TRACE("(%p %x %x %p)\n", pIUri, dwFlags, (DWORD)dwReserved, ppIUriBuilder);
4023 ret = heap_alloc(sizeof(UriBuilder));
4024 if(!ret)
4025 return E_OUTOFMEMORY;
4027 ret->lpIUriBuilderVtbl = &UriBuilderVtbl;
4028 ret->ref = 1;
4030 *ppIUriBuilder = URIBUILDER(ret);
4031 return S_OK;