From: Zhiyi Zhang <zzhang@codeweavers.com> --- dlls/iertutil/Makefile.in | 1 + dlls/iertutil/iertutil_classes.idl | 31 + dlls/iertutil/main.c | 83 +- dlls/iertutil/private.h | 2 + dlls/iertutil/uri.c | 3 +- dlls/urlmon/uri.c | 4499 ---------------------------- dlls/urlmon/urlmon_main.c | 5 - dlls/urlmon/urlmon_main.h | 2 - dlls/urlmon/urlmon_urlmon.idl | 7 - 9 files changed, 117 insertions(+), 4516 deletions(-) create mode 100644 dlls/iertutil/iertutil_classes.idl diff --git a/dlls/iertutil/Makefile.in b/dlls/iertutil/Makefile.in index 78f9d0bbb50..769dea3cea6 100644 --- a/dlls/iertutil/Makefile.in +++ b/dlls/iertutil/Makefile.in @@ -4,5 +4,6 @@ IMPORTS = ole32 oleaut32 rpcrt4 shlwapi combase SOURCES = \ classes.idl \ + iertutil_classes.idl \ main.c \ uri.c diff --git a/dlls/iertutil/iertutil_classes.idl b/dlls/iertutil/iertutil_classes.idl new file mode 100644 index 00000000000..61beaa42869 --- /dev/null +++ b/dlls/iertutil/iertutil_classes.idl @@ -0,0 +1,31 @@ +/* + * COM classes for iertutil.dll + * + * Copyright 2026 Zhiyi Zhang for CodeWeavers + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#pragma makedep header +#pragma makedep register + +import "urlmon.idl"; + +[ + helpstring("CUri"), + threading(apartment), + uuid(df2fce13-25ec-45bb-9d4c-cecd47c2430c) +] +coclass CUri {interface IUri;} diff --git a/dlls/iertutil/main.c b/dlls/iertutil/main.c index 70f8d4dcb99..fd6dd3c9372 100644 --- a/dlls/iertutil/main.c +++ b/dlls/iertutil/main.c @@ -17,6 +17,7 @@ */ #include "private.h" +#include "iertutil_classes.h" WINE_DEFAULT_DEBUG_CHANNEL(iertutil); @@ -390,9 +391,89 @@ static struct iertutil iertutil = 1 }; +typedef struct +{ + IClassFactory IClassFactory_iface; + HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj); +} ClassFactory; + +static inline ClassFactory *impl_from_IClassFactory(IClassFactory *iface) +{ + return CONTAINING_RECORD(iface, ClassFactory, IClassFactory_iface); +} + +static HRESULT WINAPI CF_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv) +{ + *ppv = NULL; + + if (IsEqualGUID(riid, &IID_IUnknown) || IsEqualGUID(riid, &IID_IClassFactory)) + { + *ppv = iface; + return S_OK; + } + + WARN("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv); + return E_NOINTERFACE; +} + +static ULONG WINAPI CF_AddRef(IClassFactory *iface) +{ + return 2; +} + +static ULONG WINAPI CF_Release(IClassFactory *iface) +{ + return 1; +} + +static HRESULT WINAPI CF_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, + void **ppv) +{ + ClassFactory *This = impl_from_IClassFactory(iface); + IUnknown *unk; + HRESULT hr; + + TRACE("(%p)->(%p %s %p)\n", iface, outer, debugstr_guid(riid), ppv); + + *ppv = NULL; + + if (outer && !IsEqualGUID(riid, &IID_IUnknown)) + return CLASS_E_NOAGGREGATION; + + hr = This->pfnCreateInstance(outer, (void **)&unk); + if (FAILED(hr)) + return hr; + + hr = IUnknown_QueryInterface(unk, riid, ppv); + IUnknown_Release(unk); + return hr; +} + +static HRESULT WINAPI CF_LockServer(IClassFactory *iface, BOOL dolock) +{ + TRACE("(%d)\n", dolock); + return S_OK; +} + +static const IClassFactoryVtbl ClassFactoryVtbl = +{ + CF_QueryInterface, + CF_AddRef, + CF_Release, + CF_CreateInstance, + CF_LockServer +}; + +static ClassFactory CUriCF = {{&ClassFactoryVtbl}, Uri_Construct}; + HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID riid, void **out) { - FIXME("clsid %s, riid %s, out %p stub!\n", debugstr_guid(clsid), debugstr_guid(riid), out); + TRACE("clsid %s, riid %s, out %p.\n", debugstr_guid(clsid), debugstr_guid(riid), out); + + if (IsEqualGUID(clsid, &CLSID_CUri)) + return IClassFactory_QueryInterface(&CUriCF.IClassFactory_iface, riid, out); + + FIXME("%s: no class found.\n", debugstr_guid(clsid)); return CLASS_E_CLASSNOTAVAILABLE; } diff --git a/dlls/iertutil/private.h b/dlls/iertutil/private.h index 3ace0e0888d..5c416aba80a 100644 --- a/dlls/iertutil/private.h +++ b/dlls/iertutil/private.h @@ -71,4 +71,6 @@ DEFINE_IINSPECTABLE_(pfx, iface_type, impl_type, impl_from_##iface_type, iface_type##_iface, \ &impl->base_iface) +extern HRESULT Uri_Construct(IUnknown *pUnkOuter, LPVOID *ppobj); + #endif /* __WINE_IERTUTIL_PRIVATE_H */ diff --git a/dlls/iertutil/uri.c b/dlls/iertutil/uri.c index 71b3a713db0..bdf9f104e42 100644 --- a/dlls/iertutil/uri.c +++ b/dlls/iertutil/uri.c @@ -41,6 +41,7 @@ #include "inaddr.h" #include "in6addr.h" #include "ip2string.h" +#include "iertutil_classes.h" WINE_DEFAULT_DEBUG_CHANNEL(iertutil); @@ -59,8 +60,6 @@ WINE_DEFAULT_DEBUG_CHANNEL(iertutil); #define COMBINE_URI_FORCE_FLAG_USE 0x1 -static const GUID CLSID_CUri = {0xDF2FCE13,0x25EC,0x45BB,{0x9D,0x4C,0xCE,0xCD,0x47,0xC2,0x43,0x0C}}; - static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}}; typedef struct { diff --git a/dlls/urlmon/uri.c b/dlls/urlmon/uri.c index 5a914d080be..8051c61a01e 100644 --- a/dlls/urlmon/uri.c +++ b/dlls/urlmon/uri.c @@ -17,34 +17,12 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#include <limits.h> -#include <wchar.h> - #include "urlmon_main.h" #include "wine/debug.h" #define NO_SHLWAPI_REG #include "shlwapi.h" -#include "strsafe.h" -#include "winternl.h" -#include "inaddr.h" -#include "in6addr.h" -#include "ip2string.h" - -#define URI_DISPLAY_NO_ABSOLUTE_URI 0x1 -#define URI_DISPLAY_NO_DEFAULT_PORT_AUTH 0x2 - -#define ALLOW_NULL_TERM_SCHEME 0x01 -#define ALLOW_NULL_TERM_USER_NAME 0x02 -#define ALLOW_NULL_TERM_PASSWORD 0x04 -#define ALLOW_BRACKETLESS_IP_LITERAL 0x08 -#define SKIP_IP_FUTURE_CHECK 0x10 -#define IGNORE_PORT_DELIMITER 0x20 - -#define RAW_URI_FORCE_PORT_DISP 0x1 -#define RAW_URI_CONVERT_TO_DOS_PATH 0x2 - #define COMBINE_URI_FORCE_FLAG_USE 0x1 WINE_DEFAULT_DEBUG_CHANNEL(urlmon); @@ -56,175 +34,6 @@ HRESULT WINAPI PrivateCoInternetParseIUri(IUri *pIUri, PARSEACTION ParseAction, LPWSTR pwzResult, DWORD cchResult, DWORD *pcchResult, DWORD_PTR dwReserved); -static const IID IID_IUriObj = {0x4b364760,0x9f51,0x11df,{0x98,0x1c,0x08,0x00,0x20,0x0c,0x9a,0x66}}; - -typedef struct { - IUri IUri_iface; - IUriBuilderFactory IUriBuilderFactory_iface; - IPersistStream IPersistStream_iface; - IMarshal IMarshal_iface; - - LONG ref; - - BSTR raw_uri; - - /* Information about the canonicalized URI's buffer. */ - WCHAR *canon_uri; - DWORD canon_size; - DWORD canon_len; - BOOL display_modifiers; - DWORD create_flags; - - INT scheme_start; - DWORD scheme_len; - URL_SCHEME scheme_type; - - INT userinfo_start; - DWORD userinfo_len; - INT userinfo_split; - - INT host_start; - DWORD host_len; - Uri_HOST_TYPE host_type; - - INT port_offset; - DWORD port; - BOOL has_port; - - INT authority_start; - DWORD authority_len; - - INT domain_offset; - - INT path_start; - DWORD path_len; - INT extension_offset; - - INT query_start; - DWORD query_len; - - INT fragment_start; - DWORD fragment_len; -} Uri; - -typedef struct { - IUriBuilder IUriBuilder_iface; - LONG ref; - - Uri *uri; - DWORD modified_props; - - WCHAR *fragment; - DWORD fragment_len; - - WCHAR *host; - DWORD host_len; - - WCHAR *password; - DWORD password_len; - - WCHAR *path; - DWORD path_len; - - BOOL has_port; - DWORD port; - - WCHAR *query; - DWORD query_len; - - WCHAR *scheme; - DWORD scheme_len; - - WCHAR *username; - DWORD username_len; -} UriBuilder; - -typedef struct { - BSTR uri; - - BOOL is_relative; - BOOL is_opaque; - BOOL has_implicit_scheme; - BOOL has_implicit_ip; - UINT implicit_ipv4; - BOOL must_have_path; - - const WCHAR *scheme; - DWORD scheme_len; - URL_SCHEME scheme_type; - - const WCHAR *username; - DWORD username_len; - - const WCHAR *password; - DWORD password_len; - - const WCHAR *host; - DWORD host_len; - Uri_HOST_TYPE host_type; - - IN6_ADDR ipv6_address; - - BOOL has_port; - const WCHAR *port; - DWORD port_len; - DWORD port_value; - - const WCHAR *path; - DWORD path_len; - - const WCHAR *query; - DWORD query_len; - - const WCHAR *fragment; - DWORD fragment_len; -} parse_data; - -static const CHAR hexDigits[] = "0123456789ABCDEF"; - -/* List of scheme types/scheme names that are recognized by the IUri interface as of IE 7. */ -static const struct { - URL_SCHEME scheme; - WCHAR scheme_name[16]; -} recognized_schemes[] = { - {URL_SCHEME_FTP, L"ftp"}, - {URL_SCHEME_HTTP, L"http"}, - {URL_SCHEME_GOPHER, L"gopher"}, - {URL_SCHEME_MAILTO, L"mailto"}, - {URL_SCHEME_NEWS, L"news"}, - {URL_SCHEME_NNTP, L"nntp"}, - {URL_SCHEME_TELNET, L"telnet"}, - {URL_SCHEME_WAIS, L"wais"}, - {URL_SCHEME_FILE, L"file"}, - {URL_SCHEME_MK, L"mk"}, - {URL_SCHEME_HTTPS, L"https"}, - {URL_SCHEME_SHELL, L"shell"}, - {URL_SCHEME_SNEWS, L"snews"}, - {URL_SCHEME_LOCAL, L"local"}, - {URL_SCHEME_JAVASCRIPT, L"javascript"}, - {URL_SCHEME_VBSCRIPT, L"vbscript"}, - {URL_SCHEME_ABOUT, L"about"}, - {URL_SCHEME_RES, L"res"}, - {URL_SCHEME_MSSHELLROOTED, L"ms-shell-rooted"}, - {URL_SCHEME_MSSHELLIDLIST, L"ms-shell-idlist"}, - {URL_SCHEME_MSHELP, L"hcp"}, - {URL_SCHEME_WILDCARD, L"*"} -}; - -/* List of default ports Windows recognizes. */ -static const struct { - URL_SCHEME scheme; - USHORT port; -} default_ports[] = { - {URL_SCHEME_FTP, 21}, - {URL_SCHEME_HTTP, 80}, - {URL_SCHEME_GOPHER, 70}, - {URL_SCHEME_NNTP, 119}, - {URL_SCHEME_TELNET, 23}, - {URL_SCHEME_WAIS, 210}, - {URL_SCHEME_HTTPS, 443}, -}; - /* List of 3-character top level domain names Windows seems to recognize. * There might be more, but, these are the only ones I've found so far. */ @@ -240,212 +49,11 @@ static const struct { {L"org"} }; -static Uri *get_uri_obj(IUri *uri) -{ - Uri *ret; - HRESULT hres; - - hres = IUri_QueryInterface(uri, &IID_IUriObj, (void**)&ret); - return SUCCEEDED(hres) ? ret : NULL; -} - -static inline BOOL is_alpha(WCHAR val) { - return ((val >= 'a' && val <= 'z') || (val >= 'A' && val <= 'Z')); -} - -static inline BOOL is_num(WCHAR val) { - return (val >= '0' && val <= '9'); -} - -static inline BOOL is_drive_path(const WCHAR *str) { - return (is_alpha(str[0]) && (str[1] == ':' || str[1] == '|')); -} - -static inline BOOL is_unc_path(const WCHAR *str) { - return (str[0] == '\\' && str[1] == '\\'); -} - -static inline BOOL is_forbidden_dos_path_char(WCHAR val) { - return (val == '>' || val == '<' || val == '\"'); -} - -/* A URI is implicitly a file path if it begins with - * a drive letter (e.g. X:) or starts with "\\" (UNC path). - */ -static inline BOOL is_implicit_file_path(const WCHAR *str) { - return (is_unc_path(str) || (is_alpha(str[0]) && str[1] == ':')); -} - -/* Checks if the URI is a hierarchical URI. A hierarchical - * URI is one that has "//" after the scheme. - */ -static BOOL check_hierarchical(const WCHAR **ptr) { - const WCHAR *start = *ptr; - - if(**ptr != '/') - return FALSE; - - ++(*ptr); - if(**ptr != '/') { - *ptr = start; - return FALSE; - } - - ++(*ptr); - return TRUE; -} - -/* unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~" */ -static inline BOOL is_unreserved(WCHAR val) { - return (is_alpha(val) || is_num(val) || val == '-' || val == '.' || - val == '_' || val == '~'); -} - -/* sub-delims = "!" / "$" / "&" / "'" / "(" / ")" - * / "*" / "+" / "," / ";" / "=" - */ -static inline BOOL is_subdelim(WCHAR val) { - return (val == '!' || val == '$' || val == '&' || - val == '\'' || val == '(' || val == ')' || - val == '*' || val == '+' || val == ',' || - val == ';' || val == '='); -} - -/* gen-delims = ":" / "/" / "?" / "#" / "[" / "]" / "@" */ -static inline BOOL is_gendelim(WCHAR val) { - return (val == ':' || val == '/' || val == '?' || - val == '#' || val == '[' || val == ']' || - val == '@'); -} - -/* Characters that delimit the end of the authority - * section of a URI. Sometimes a '\\' is considered - * an authority delimiter. - */ -static inline BOOL is_auth_delim(WCHAR val, BOOL acceptSlash) { - return (val == '#' || val == '/' || val == '?' || - val == '\0' || (acceptSlash && val == '\\')); -} - -/* reserved = gen-delims / sub-delims */ -static inline BOOL is_reserved(WCHAR val) { - return (is_subdelim(val) || is_gendelim(val)); -} - -static inline BOOL is_hexdigit(WCHAR val) { - return ((val >= 'a' && val <= 'f') || - (val >= 'A' && val <= 'F') || - (val >= '0' && val <= '9')); -} - -static inline BOOL is_path_delim(URL_SCHEME scheme, WCHAR val) { - return (!val || (val == '#' && scheme != URL_SCHEME_FILE) || val == '?'); -} - -static inline BOOL is_slash(WCHAR c) -{ - return c == '/' || c == '\\'; -} - static inline BOOL is_ascii(WCHAR c) { return c < 0x80; } -static BOOL is_default_port(URL_SCHEME scheme, DWORD port) { - DWORD i; - - for(i = 0; i < ARRAY_SIZE(default_ports); ++i) { - if(default_ports[i].scheme == scheme && default_ports[i].port) - return TRUE; - } - - return FALSE; -} - -/* List of schemes types Windows seems to expect to be hierarchical. */ -static inline BOOL is_hierarchical_scheme(URL_SCHEME type) { - return(type == URL_SCHEME_HTTP || type == URL_SCHEME_FTP || - type == URL_SCHEME_GOPHER || type == URL_SCHEME_NNTP || - type == URL_SCHEME_TELNET || type == URL_SCHEME_WAIS || - type == URL_SCHEME_FILE || type == URL_SCHEME_HTTPS || - type == URL_SCHEME_RES); -} - -/* Determines if the URI is hierarchical using the information already parsed into - * data and using the current location of parsing in the URI string. - * - * Windows considers a URI hierarchical if one of the following is true: - * A.) It's a wildcard scheme. - * B.) It's an implicit file scheme. - * C.) It's a known hierarchical scheme and it has two '\\' after the scheme name. - * (the '\\' will be converted into "//" during canonicalization). - * D.) "//" appears after the scheme name (or at the beginning if no scheme is given). - */ -static inline BOOL is_hierarchical_uri(const WCHAR **ptr, const parse_data *data) { - const WCHAR *start = *ptr; - - if(data->scheme_type == URL_SCHEME_WILDCARD) - return TRUE; - else if(data->scheme_type == URL_SCHEME_FILE && data->has_implicit_scheme) - return TRUE; - else if(is_hierarchical_scheme(data->scheme_type) && (*ptr)[0] == '\\' && (*ptr)[1] == '\\') { - *ptr += 2; - return TRUE; - } else if(data->scheme_type != URL_SCHEME_MAILTO && check_hierarchical(ptr)) - return TRUE; - - *ptr = start; - return FALSE; -} - -/* Taken from dlls/jscript/lex.c */ -static int hex_to_int(WCHAR val) { - if(val >= '0' && val <= '9') - return val - '0'; - else if(val >= 'a' && val <= 'f') - return val - 'a' + 10; - else if(val >= 'A' && val <= 'F') - return val - 'A' + 10; - - return -1; -} - -/* Helper function for converting a percent encoded string - * representation of a WCHAR value into its actual WCHAR value. If - * the two characters following the '%' aren't valid hex values then - * this function returns the NULL character. - * - * E.g. - * "%2E" will result in '.' being returned by this function. - */ -static WCHAR decode_pct_val(const WCHAR *ptr) { - WCHAR ret = '\0'; - - if(*ptr == '%' && is_hexdigit(*(ptr + 1)) && is_hexdigit(*(ptr + 2))) { - INT a = hex_to_int(*(ptr + 1)); - INT b = hex_to_int(*(ptr + 2)); - - ret = a << 4; - ret += b; - } - - return ret; -} - -/* Helper function for percent encoding a given character - * and storing the encoded value into a given buffer (dest). - * - * It's up to the calling function to ensure that there is - * at least enough space in 'dest' for the percent encoded - * value to be stored (so dest + 3 spaces available). - */ -static inline void pct_encode_val(WCHAR val, WCHAR *dest) { - dest[0] = '%'; - dest[1] = hexDigits[(val >> 4) & 0xf]; - dest[2] = hexDigits[val & 0xf]; -} - /* Attempts to parse the domain name from the host. * * This function also includes the Top-level Domain (TLD) name @@ -569,4113 +177,6 @@ void find_domain_name(const WCHAR *host, DWORD host_len, (host+host_len)-(host+*domain_start))); } -/* Removes the dot segments from a hierarchical URIs path component. This - * function performs the removal in place. - * - * This function returns the new length of the path string. - */ -static DWORD remove_dot_segments(WCHAR *path, DWORD path_len) { - WCHAR *out = path; - const WCHAR *in = out; - const WCHAR *end = out + path_len; - DWORD len; - - while(in < end) { - /* Move the first path segment in the input buffer to the end of - * the output buffer, and any subsequent characters up to, including - * the next "/" character (if any) or the end of the input buffer. - */ - while(in < end && !is_slash(*in)) - *out++ = *in++; - if(in == end) - break; - *out++ = *in++; - - while(in < end) { - if(*in != '.') - break; - - /* Handle ending "/." */ - if(in + 1 == end) { - ++in; - break; - } - - /* Handle "/./" */ - if(is_slash(in[1])) { - in += 2; - continue; - } - - /* If we don't have "/../" or ending "/.." */ - if(in[1] != '.' || (in + 2 != end && !is_slash(in[2]))) - break; - - /* Find the slash preceding out pointer and move out pointer to it */ - if(out > path+1 && is_slash(*--out)) - --out; - while(out > path && !is_slash(*(--out))); - if(is_slash(*out)) - ++out; - in += 2; - if(in != end) - ++in; - } - } - - len = out - path; - TRACE("(%p %ld): Path after dot segments removed %s len=%ld\n", path, path_len, - debugstr_wn(path, len), len); - return len; -} - -/* Attempts to find the file extension in a given path. */ -static INT find_file_extension(const WCHAR *path, DWORD path_len) { - const WCHAR *end; - - for(end = path+path_len-1; end >= path && *end != '/' && *end != '\\'; --end) { - if(*end == '.') - return end-path; - } - - return -1; -} - -/* Converts an IPv4 address in numerical form into its fully qualified - * string form. This function returns the number of characters written - * to 'dest'. If 'dest' is NULL this function will return the number of - * characters that would have been written. - * - * It's up to the caller to ensure there's enough space in 'dest' for the - * address. - */ -static DWORD ui2ipv4(WCHAR *dest, UINT address) { - DWORD ret = 0; - UCHAR digits[4]; - - digits[0] = (address >> 24) & 0xff; - digits[1] = (address >> 16) & 0xff; - digits[2] = (address >> 8) & 0xff; - digits[3] = address & 0xff; - - if(!dest) { - WCHAR tmp[16]; - ret = swprintf(tmp, ARRAY_SIZE(tmp), L"%u.%u.%u.%u", digits[0], digits[1], digits[2], digits[3]); - } else - ret = swprintf(dest, 16, L"%u.%u.%u.%u", digits[0], digits[1], digits[2], digits[3]); - - return ret; -} - -static DWORD ui2str(WCHAR *dest, UINT value) { - DWORD ret = 0; - - if(!dest) { - WCHAR tmp[11]; - ret = swprintf(tmp, ARRAY_SIZE(tmp), L"%u", value); - } else - ret = swprintf(dest, 11, L"%u", value); - - return ret; -} - -/* Checks if the characters pointed to by 'ptr' are - * a percent encoded data octet. - * - * pct-encoded = "%" HEXDIG HEXDIG - */ -static BOOL check_pct_encoded(const WCHAR **ptr) { - const WCHAR *start = *ptr; - - if(**ptr != '%') - return FALSE; - - ++(*ptr); - if(!is_hexdigit(**ptr)) { - *ptr = start; - return FALSE; - } - - ++(*ptr); - if(!is_hexdigit(**ptr)) { - *ptr = start; - return FALSE; - } - - ++(*ptr); - return TRUE; -} - -/* dec-octet = DIGIT ; 0-9 - * / %x31-39 DIGIT ; 10-99 - * / "1" 2DIGIT ; 100-199 - * / "2" %x30-34 DIGIT ; 200-249 - * / "25" %x30-35 ; 250-255 - */ -static BOOL check_dec_octet(const WCHAR **ptr) { - const WCHAR *c1, *c2, *c3; - - c1 = *ptr; - /* A dec-octet must be at least 1 digit long. */ - if(*c1 < '0' || *c1 > '9') - return FALSE; - - ++(*ptr); - - c2 = *ptr; - /* Since the 1-digit requirement was met, it doesn't - * matter if this is a DIGIT value, it's considered a - * dec-octet. - */ - if(*c2 < '0' || *c2 > '9') - return TRUE; - - ++(*ptr); - - c3 = *ptr; - /* Same explanation as above. */ - if(*c3 < '0' || *c3 > '9') - return TRUE; - - /* Anything > 255 isn't a valid IP dec-octet. */ - if(*c1 >= '2' && *c2 >= '5' && *c3 >= '5') { - *ptr = c1; - return FALSE; - } - - ++(*ptr); - return TRUE; -} - -/* Checks if there is an implicit IPv4 address in the host component of the URI. - * The max value of an implicit IPv4 address is UINT_MAX. - * - * Ex: - * "234567" would be considered an implicit IPv4 address. - */ -static BOOL check_implicit_ipv4(const WCHAR **ptr, UINT *val) { - const WCHAR *start = *ptr; - ULONGLONG ret = 0; - *val = 0; - - while(is_num(**ptr)) { - ret = ret*10 + (**ptr - '0'); - - if(ret > UINT_MAX) { - *ptr = start; - return FALSE; - } - ++(*ptr); - } - - if(*ptr == start) - return FALSE; - - *val = ret; - return TRUE; -} - -/* Checks if the string contains an IPv4 address. - * - * This function has a strict mode or a non-strict mode of operation - * When 'strict' is set to FALSE this function will return TRUE if - * the string contains at least 'dec-octet "." dec-octet' since partial - * IPv4 addresses will be normalized out into full IPv4 addresses. When - * 'strict' is set this function expects there to be a full IPv4 address. - * - * IPv4address = dec-octet "." dec-octet "." dec-octet "." dec-octet - */ -static BOOL check_ipv4address(const WCHAR **ptr, BOOL strict) { - const WCHAR *start = *ptr; - - if(!check_dec_octet(ptr)) { - *ptr = start; - return FALSE; - } - - if(**ptr != '.') { - *ptr = start; - return FALSE; - } - - ++(*ptr); - if(!check_dec_octet(ptr)) { - *ptr = start; - return FALSE; - } - - if(**ptr != '.') { - if(strict) { - *ptr = start; - return FALSE; - } else - return TRUE; - } - - ++(*ptr); - if(!check_dec_octet(ptr)) { - *ptr = start; - return FALSE; - } - - if(**ptr != '.') { - if(strict) { - *ptr = start; - return FALSE; - } else - return TRUE; - } - - ++(*ptr); - if(!check_dec_octet(ptr)) { - *ptr = start; - return FALSE; - } - - /* Found a four digit ip address. */ - return TRUE; -} -/* Tries to parse the scheme name of the URI. - * - * scheme = ALPHA *(ALPHA | NUM | '+' | '-' | '.') as defined by RFC 3896. - * NOTE: Windows accepts a number as the first character of a scheme. - */ -static BOOL parse_scheme_name(const WCHAR **ptr, parse_data *data, DWORD extras) { - const WCHAR *start = *ptr; - - data->scheme = NULL; - data->scheme_len = 0; - - while(**ptr) { - if(**ptr == '*' && *ptr == start) { - /* Might have found a wildcard scheme. If it is the next - * char has to be a ':' for it to be a valid URI - */ - ++(*ptr); - break; - } else if(!is_num(**ptr) && !is_alpha(**ptr) && **ptr != '+' && - **ptr != '-' && **ptr != '.') - break; - - (*ptr)++; - } - - if(*ptr == start) - return FALSE; - - /* Schemes must end with a ':' */ - if(**ptr != ':' && !((extras & ALLOW_NULL_TERM_SCHEME) && !**ptr)) { - *ptr = start; - return FALSE; - } - - data->scheme = start; - data->scheme_len = *ptr - start; - - ++(*ptr); - return TRUE; -} - -/* Tries to deduce the corresponding URL_SCHEME for the given URI. Stores - * the deduced URL_SCHEME in data->scheme_type. - */ -static BOOL parse_scheme_type(parse_data *data) { - /* If there's scheme data then see if it's a recognized scheme. */ - if(data->scheme && data->scheme_len) { - DWORD i; - - for(i = 0; i < ARRAY_SIZE(recognized_schemes); ++i) { - if(lstrlenW(recognized_schemes[i].scheme_name) == data->scheme_len) { - /* Has to be a case insensitive compare. */ - if(!StrCmpNIW(recognized_schemes[i].scheme_name, data->scheme, data->scheme_len)) { - data->scheme_type = recognized_schemes[i].scheme; - return TRUE; - } - } - } - - /* If we get here it means it's not a recognized scheme. */ - data->scheme_type = URL_SCHEME_UNKNOWN; - return TRUE; - } else if(data->is_relative) { - /* Relative URI's have no scheme. */ - data->scheme_type = URL_SCHEME_UNKNOWN; - return TRUE; - } else { - /* Should never reach here! what happened... */ - FIXME("(%p): Unable to determine scheme type for URI %s\n", data, debugstr_w(data->uri)); - return FALSE; - } -} - -/* Tries to parse (or deduce) the scheme_name of a URI. If it can't - * parse a scheme from the URI it will try to deduce the scheme_name and scheme_type - * using the flags specified in 'flags' (if any). Flags that affect how this function - * operates are the Uri_CREATE_ALLOW_* flags. - * - * All parsed/deduced information will be stored in 'data' when the function returns. - * - * Returns TRUE if it was able to successfully parse the information. - */ -static BOOL parse_scheme(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) { - /* First check to see if the uri could implicitly be a file path. */ - if(is_implicit_file_path(*ptr)) { - if(flags & Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME) { - data->scheme = L"file"; - data->scheme_len = lstrlenW(L"file"); - data->has_implicit_scheme = TRUE; - - TRACE("(%p %p %lx): URI is an implicit file path.\n", ptr, data, flags); - } else { - /* Windows does not consider anything that can implicitly be a file - * path to be a valid URI if the ALLOW_IMPLICIT_FILE_SCHEME flag is not set... - */ - TRACE("(%p %p %lx): URI is implicitly a file path, but, the ALLOW_IMPLICIT_FILE_SCHEME flag wasn't set.\n", - ptr, data, flags); - return FALSE; - } - } else if(!parse_scheme_name(ptr, data, extras)) { - /* No scheme was found, this means it could be: - * a) an implicit Wildcard scheme - * b) a relative URI - * c) an invalid URI. - */ - if(flags & Uri_CREATE_ALLOW_IMPLICIT_WILDCARD_SCHEME) { - data->scheme = L"*"; - data->scheme_len = lstrlenW(L"*"); - data->has_implicit_scheme = TRUE; - - TRACE("(%p %p %lx): URI is an implicit wildcard scheme.\n", ptr, data, flags); - } else if (flags & Uri_CREATE_ALLOW_RELATIVE) { - data->is_relative = TRUE; - TRACE("(%p %p %lx): URI is relative.\n", ptr, data, flags); - } else { - TRACE("(%p %p %lx): Malformed URI found. Unable to deduce scheme name.\n", ptr, data, flags); - return FALSE; - } - } - - if(!data->is_relative) - TRACE("(%p %p %lx): Found scheme=%s scheme_len=%ld\n", ptr, data, flags, - debugstr_wn(data->scheme, data->scheme_len), data->scheme_len); - - if(!parse_scheme_type(data)) - return FALSE; - - TRACE("(%p %p %lx): Assigned %d as the URL_SCHEME.\n", ptr, data, flags, data->scheme_type); - return TRUE; -} - -static BOOL parse_username(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) { - data->username = *ptr; - - while(**ptr != ':' && **ptr != '@') { - if(**ptr == '%') { - if(!check_pct_encoded(ptr)) { - if(data->scheme_type != URL_SCHEME_UNKNOWN) { - *ptr = data->username; - data->username = NULL; - return FALSE; - } - } else - continue; - } else if(extras & ALLOW_NULL_TERM_USER_NAME && !**ptr) - break; - else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) { - *ptr = data->username; - data->username = NULL; - return FALSE; - } - - ++(*ptr); - } - - data->username_len = *ptr - data->username; - return TRUE; -} - -static BOOL parse_password(const WCHAR **ptr, parse_data *data, DWORD flags, DWORD extras) { - data->password = *ptr; - - while(**ptr != '@') { - if(**ptr == '%') { - if(!check_pct_encoded(ptr)) { - if(data->scheme_type != URL_SCHEME_UNKNOWN) { - *ptr = data->password; - data->password = NULL; - return FALSE; - } - } else - continue; - } else if(extras & ALLOW_NULL_TERM_PASSWORD && !**ptr) - break; - else if(is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) { - *ptr = data->password; - data->password = NULL; - return FALSE; - } - - ++(*ptr); - } - - data->password_len = *ptr - data->password; - return TRUE; -} - -/* Parses the userinfo part of the URI (if it exists). The userinfo field of - * a URI can consist of "username:password@", or just "username@". - * - * RFC def: - * userinfo = *( unreserved / pct-encoded / sub-delims / ":" ) - * - * NOTES: - * 1) If there is more than one ':' in the userinfo part of the URI Windows - * uses the first occurrence of ':' to delimit the username and password - * components. - * - * ex: - * ftp://user:pass:word@winehq.org - * - * would yield "user" as the username and "pass:word" as the password. - * - * 2) Windows allows any character to appear in the "userinfo" part of - * a URI, as long as it's not an authority delimiter character set. - */ -static void parse_userinfo(const WCHAR **ptr, parse_data *data, DWORD flags) { - const WCHAR *start = *ptr; - - if(!parse_username(ptr, data, flags, 0)) { - TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr, data, flags); - return; - } - - if(**ptr == ':') { - ++(*ptr); - if(!parse_password(ptr, data, flags, 0)) { - *ptr = start; - data->username = NULL; - data->username_len = 0; - TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr, data, flags); - return; - } - } - - if(**ptr != '@') { - *ptr = start; - data->username = NULL; - data->username_len = 0; - data->password = NULL; - data->password_len = 0; - - TRACE("(%p %p %lx): URI contained no userinfo.\n", ptr, data, flags); - return; - } - - if(data->username) - TRACE("(%p %p %lx): Found username %s len=%ld.\n", ptr, data, flags, - debugstr_wn(data->username, data->username_len), data->username_len); - - if(data->password) - TRACE("(%p %p %lx): Found password %s len=%ld.\n", ptr, data, flags, - debugstr_wn(data->password, data->password_len), data->password_len); - - ++(*ptr); -} - -/* Attempts to parse a port from the URI. - * - * NOTES: - * Windows seems to have a cap on what the maximum value - * for a port can be. The max value is USHORT_MAX. - * - * port = *DIGIT - */ -static BOOL parse_port(const WCHAR **ptr, parse_data *data) { - UINT port = 0; - data->port = *ptr; - - while(!is_auth_delim(**ptr, data->scheme_type != URL_SCHEME_UNKNOWN)) { - if(!is_num(**ptr)) { - *ptr = data->port; - data->port = NULL; - return FALSE; - } - - port = port*10 + (**ptr-'0'); - - if(port > USHRT_MAX) { - *ptr = data->port; - data->port = NULL; - return FALSE; - } - - ++(*ptr); - } - - data->has_port = TRUE; - data->port_value = port; - data->port_len = *ptr - data->port; - - TRACE("(%p %p): Found port %s len=%ld value=%lu\n", ptr, data, - debugstr_wn(data->port, data->port_len), data->port_len, data->port_value); - return TRUE; -} - -/* Attempts to parse a IPv4 address from the URI. - * - * NOTES: - * Windows normalizes IPv4 addresses, This means there are three - * possibilities for the URI to contain an IPv4 address. - * 1) A well formed address (ex. 192.2.2.2). - * 2) A partially formed address. For example "192.0" would - * normalize to "192.0.0.0" during canonicalization. - * 3) An implicit IPv4 address. For example "256" would - * normalize to "0.0.1.0" during canonicalization. Also - * note that the maximum value for an implicit IP address - * is UINT_MAX, if the value in the URI exceeds this then - * it is not considered an IPv4 address. - */ -static BOOL parse_ipv4address(const WCHAR **ptr, parse_data *data) { - const BOOL is_unknown = data->scheme_type == URL_SCHEME_UNKNOWN; - data->host = *ptr; - - if(!check_ipv4address(ptr, FALSE)) { - if(!check_implicit_ipv4(ptr, &data->implicit_ipv4)) { - TRACE("(%p %p): URI didn't contain anything looking like an IPv4 address.\n", ptr, data); - *ptr = data->host; - data->host = NULL; - return FALSE; - } else - data->has_implicit_ip = TRUE; - } - - data->host_len = *ptr - data->host; - data->host_type = Uri_HOST_IPV4; - - /* Check if what we found is the only part of the host name (if it isn't - * we don't have an IPv4 address). - */ - if(**ptr == ':') { - ++(*ptr); - if(!parse_port(ptr, data)) { - *ptr = data->host; - data->host = NULL; - return FALSE; - } - } else if(!is_auth_delim(**ptr, !is_unknown)) { - /* Found more data which belongs to the host, so this isn't an IPv4. */ - *ptr = data->host; - data->host = NULL; - data->has_implicit_ip = FALSE; - return FALSE; - } - - TRACE("(%p %p): IPv4 address found. host=%s host_len=%ld host_type=%d\n", - ptr, data, debugstr_wn(data->host, data->host_len), - data->host_len, data->host_type); - return TRUE; -} - -/* Attempts to parse the reg-name from the URI. - * - * Because of the way Windows handles ':' this function also - * handles parsing the port. - * - * reg-name = *( unreserved / pct-encoded / sub-delims ) - * - * NOTE: - * Windows allows everything, but, the characters in "auth_delims" and ':' - * to appear in a reg-name, unless it's an unknown scheme type then ':' is - * allowed to appear (even if a valid port isn't after it). - * - * Windows doesn't like host names which start with '[' and end with ']' - * and don't contain a valid IP literal address in between them. - * - * On Windows if a '[' is encountered in the host name the ':' no longer - * counts as a delimiter until you reach the next ']' or an "authority delimiter". - * - * A reg-name CAN be empty. - */ -static BOOL parse_reg_name(const WCHAR **ptr, parse_data *data, DWORD extras) { - const BOOL has_start_bracket = **ptr == '['; - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - const BOOL is_res = data->scheme_type == URL_SCHEME_RES; - BOOL inside_brackets = has_start_bracket; - - /* res URIs don't have ports. */ - BOOL ignore_col = (extras & IGNORE_PORT_DELIMITER) || is_res; - - /* We have to be careful with file schemes. */ - if(data->scheme_type == URL_SCHEME_FILE) { - /* This is because an implicit file scheme could be "C:\\test" and it - * would trick this function into thinking the host is "C", when after - * canonicalization the host would end up being an empty string. A drive - * path can also have a '|' instead of a ':' after the drive letter. - */ - if(is_drive_path(*ptr)) { - /* Regular old drive paths have no host type (or host name). */ - data->host_type = Uri_HOST_UNKNOWN; - data->host = *ptr; - data->host_len = 0; - return TRUE; - } else if(is_unc_path(*ptr)) - /* Skip past the "\\" of a UNC path. */ - *ptr += 2; - } - - data->host = *ptr; - - /* For res URIs, everything before the first '/' is - * considered the host. - */ - while((!is_res && !is_auth_delim(**ptr, known_scheme)) || - (is_res && **ptr && **ptr != '/')) { - if(**ptr == ':' && !ignore_col) { - /* We can ignore ':' if we are inside brackets.*/ - if(!inside_brackets) { - const WCHAR *tmp = (*ptr)++; - - /* Attempt to parse the port. */ - if(!parse_port(ptr, data)) { - /* Windows expects there to be a valid port for known scheme types. */ - if(data->scheme_type != URL_SCHEME_UNKNOWN) { - *ptr = data->host; - data->host = NULL; - TRACE("(%p %p %lx): Expected valid port\n", ptr, data, extras); - return FALSE; - } else - /* Windows gives up on trying to parse a port when it - * encounters an invalid port. - */ - ignore_col = TRUE; - } else { - data->host_len = tmp - data->host; - break; - } - } - } else if(**ptr == '%' && (known_scheme && !is_res)) { - /* Has to be a legit % encoded value. */ - if(!check_pct_encoded(ptr)) { - *ptr = data->host; - data->host = NULL; - return FALSE; - } else - continue; - } else if(is_res && is_forbidden_dos_path_char(**ptr)) { - *ptr = data->host; - data->host = NULL; - return FALSE; - } else if(**ptr == ']') - inside_brackets = FALSE; - else if(**ptr == '[') - inside_brackets = TRUE; - - ++(*ptr); - } - - if(has_start_bracket) { - /* Make sure the last character of the host wasn't a ']'. */ - if(*(*ptr-1) == ']') { - TRACE("(%p %p %lx): Expected an IP literal inside of the host\n", ptr, data, extras); - *ptr = data->host; - data->host = NULL; - return FALSE; - } - } - - /* Don't overwrite our length if we found a port earlier. */ - if(!data->port) - data->host_len = *ptr - data->host; - - /* If the host is empty, then it's an unknown host type. */ - if(data->host_len == 0 || is_res) - data->host_type = Uri_HOST_UNKNOWN; - else { - unsigned int i; - - data->host_type = Uri_HOST_DNS; - - for(i = 0; i < data->host_len; i++) { - if(!is_ascii(data->host[i])) { - data->host_type = Uri_HOST_IDN; - break; - } - } - } - - TRACE("(%p %p %lx): Parsed reg-name. host=%s len=%ld type=%d\n", ptr, data, extras, - debugstr_wn(data->host, data->host_len), data->host_len, data->host_type); - return TRUE; -} - -/* Attempts to parse an IPv6 address out of the URI. - * - * IPv6address = 6( h16 ":" ) ls32 - * / "::" 5( h16 ":" ) ls32 - * / [ h16 ] "::" 4( h16 ":" ) ls32 - * / [ *1( h16 ":" ) h16 ] "::" 3( h16 ":" ) ls32 - * / [ *2( h16 ":" ) h16 ] "::" 2( h16 ":" ) ls32 - * / [ *3( h16 ":" ) h16 ] "::" h16 ":" ls32 - * / [ *4( h16 ":" ) h16 ] "::" ls32 - * / [ *5( h16 ":" ) h16 ] "::" h16 - * / [ *6( h16 ":" ) h16 ] "::" - * - * ls32 = ( h16 ":" h16 ) / IPv4address - * ; least-significant 32 bits of address. - * - * h16 = 1*4HEXDIG - * ; 16 bits of address represented in hexadecimal. - */ -static BOOL parse_ipv6address(const WCHAR **ptr, parse_data *data) { - const WCHAR *terminator; - - if(RtlIpv6StringToAddressW(*ptr, &terminator, &data->ipv6_address)) - return FALSE; - if(*terminator != ']' && !is_auth_delim(*terminator, data->scheme_type != URL_SCHEME_UNKNOWN)) - return FALSE; - - *ptr = terminator; - data->host_type = Uri_HOST_IPV6; - return TRUE; -} - -/* IPvFuture = "v" 1*HEXDIG "." 1*( unreserved / sub-delims / ":" ) */ -static BOOL parse_ipvfuture(const WCHAR **ptr, parse_data *data) { - const WCHAR *start = *ptr; - - /* IPvFuture has to start with a 'v' or 'V'. */ - if(**ptr != 'v' && **ptr != 'V') - return FALSE; - - /* Following the v there must be at least 1 hex digit. */ - ++(*ptr); - if(!is_hexdigit(**ptr)) { - *ptr = start; - return FALSE; - } - - ++(*ptr); - while(is_hexdigit(**ptr)) - ++(*ptr); - - /* End of the hexdigit sequence must be a '.' */ - if(**ptr != '.') { - *ptr = start; - return FALSE; - } - - ++(*ptr); - if(!is_unreserved(**ptr) && !is_subdelim(**ptr) && **ptr != ':') { - *ptr = start; - return FALSE; - } - - ++(*ptr); - while(is_unreserved(**ptr) || is_subdelim(**ptr) || **ptr == ':') - ++(*ptr); - - data->host_type = Uri_HOST_UNKNOWN; - - TRACE("(%p %p): Parsed IPvFuture address %s len=%d\n", ptr, data, - debugstr_wn(start, *ptr-start), (int)(*ptr-start)); - - return TRUE; -} - -/* IP-literal = "[" ( IPv6address / IPvFuture ) "]" */ -static BOOL parse_ip_literal(const WCHAR **ptr, parse_data *data, DWORD extras) { - data->host = *ptr; - - if(**ptr != '[' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) { - data->host = NULL; - return FALSE; - } else if(**ptr == '[') - ++(*ptr); - - if(!parse_ipv6address(ptr, data)) { - if(extras & SKIP_IP_FUTURE_CHECK || !parse_ipvfuture(ptr, data)) { - *ptr = data->host; - data->host = NULL; - return FALSE; - } - } - - if(**ptr != ']' && !(extras & ALLOW_BRACKETLESS_IP_LITERAL)) { - *ptr = data->host; - data->host = NULL; - return FALSE; - } else if(!**ptr && extras & ALLOW_BRACKETLESS_IP_LITERAL) { - /* The IP literal didn't contain brackets and was followed by - * a NULL terminator, so no reason to even check the port. - */ - data->host_len = *ptr - data->host; - return TRUE; - } - - ++(*ptr); - if(**ptr == ':') { - ++(*ptr); - /* If a valid port is not found, then let it trickle down to - * parse_reg_name. - */ - if(!parse_port(ptr, data)) { - *ptr = data->host; - data->host = NULL; - return FALSE; - } - } else - data->host_len = *ptr - data->host; - - return TRUE; -} - -/* Parses the host information from the URI. - * - * host = IP-literal / IPv4address / reg-name - */ -static BOOL parse_host(const WCHAR **ptr, parse_data *data, DWORD extras) { - if(!parse_ip_literal(ptr, data, extras)) { - if(!parse_ipv4address(ptr, data)) { - if(!parse_reg_name(ptr, data, extras)) { - TRACE("(%p %p %lx): Malformed URI, Unknown host type.\n", ptr, data, extras); - return FALSE; - } - } - } - - return TRUE; -} - -/* Parses the authority information from the URI. - * - * authority = [ userinfo "@" ] host [ ":" port ] - */ -static BOOL parse_authority(const WCHAR **ptr, parse_data *data, DWORD flags) { - parse_userinfo(ptr, data, flags); - - /* Parsing the port will happen during one of the host parsing - * routines (if the URI has a port). - */ - if(!parse_host(ptr, data, 0)) - return FALSE; - - return TRUE; -} - -/* Attempts to parse the path information of a hierarchical URI. */ -static BOOL parse_path_hierarchical(const WCHAR **ptr, parse_data *data, DWORD flags) { - const WCHAR *start = *ptr; - const BOOL is_file = data->scheme_type == URL_SCHEME_FILE; - - if(is_path_delim(data->scheme_type, **ptr)) { - if(data->scheme_type == URL_SCHEME_WILDCARD && !data->must_have_path) { - data->path = NULL; - data->path_len = 0; - } else if(!(flags & Uri_CREATE_NO_CANONICALIZE)) { - /* If the path component is empty, then a '/' is added. */ - data->path = L"/"; - data->path_len = 1; - } - } else { - while(!is_path_delim(data->scheme_type, **ptr)) { - if(**ptr == '%' && data->scheme_type != URL_SCHEME_UNKNOWN && !is_file) { - if(!check_pct_encoded(ptr)) { - *ptr = start; - return FALSE; - } else - continue; - } else if(is_forbidden_dos_path_char(**ptr) && is_file && - (flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - /* File schemes with USE_DOS_PATH set aren't allowed to have - * a '<' or '>' or '\"' appear in them. - */ - *ptr = start; - return FALSE; - } else if(**ptr == '\\') { - /* Not allowed to have a backslash if NO_CANONICALIZE is set - * and the scheme is known type (but not a file scheme). - */ - if(flags & Uri_CREATE_NO_CANONICALIZE) { - if(data->scheme_type != URL_SCHEME_FILE && - data->scheme_type != URL_SCHEME_UNKNOWN) { - *ptr = start; - return FALSE; - } - } - } - - ++(*ptr); - } - - /* The only time a URI doesn't have a path is when - * the NO_CANONICALIZE flag is set and the raw URI - * didn't contain one. - */ - if(*ptr == start) { - data->path = NULL; - data->path_len = 0; - } else { - data->path = start; - data->path_len = *ptr - start; - } - } - - if(data->path) - TRACE("(%p %p %lx): Parsed path %s len=%ld\n", ptr, data, flags, - debugstr_wn(data->path, data->path_len), data->path_len); - else - TRACE("(%p %p %lx): The URI contained no path\n", ptr, data, flags); - - return TRUE; -} - -/* Parses the path of an opaque URI (much less strict than the parser - * for a hierarchical URI). - * - * NOTE: - * Windows allows invalid % encoded data to appear in opaque URI paths - * for unknown scheme types. - * - * File schemes with USE_DOS_PATH set aren't allowed to have '<', '>', or '\"' - * appear in them. - */ -static BOOL parse_path_opaque(const WCHAR **ptr, parse_data *data, DWORD flags) { - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - const BOOL is_file = data->scheme_type == URL_SCHEME_FILE; - const BOOL is_mailto = data->scheme_type == URL_SCHEME_MAILTO; - - if (is_mailto && (*ptr)[0] == '/' && (*ptr)[1] == '/') - { - if ((*ptr)[2]) data->path = *ptr + 2; - else data->path = NULL; - } - else - data->path = *ptr; - - while(!is_path_delim(data->scheme_type, **ptr)) { - if(**ptr == '%' && known_scheme) { - if(!check_pct_encoded(ptr)) { - *ptr = data->path; - data->path = NULL; - return FALSE; - } else - continue; - } else if(is_forbidden_dos_path_char(**ptr) && is_file && - (flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - *ptr = data->path; - data->path = NULL; - return FALSE; - } - - ++(*ptr); - } - - if (data->path) data->path_len = *ptr - data->path; - TRACE("(%p %p %lx): Parsed opaque URI path %s len=%ld\n", ptr, data, flags, - debugstr_wn(data->path, data->path_len), data->path_len); - return TRUE; -} - -/* Determines how the URI should be parsed after the scheme information. - * - * If the scheme is followed by "//", then it is treated as a hierarchical URI - * which then the authority and path information will be parsed out. Otherwise, the - * URI will be treated as an opaque URI which the authority information is not parsed - * out. - * - * RFC 3896 definition of hier-part: - * - * hier-part = "//" authority path-abempty - * / path-absolute - * / path-rootless - * / path-empty - * - * MSDN opaque URI definition: - * scheme ":" path [ "#" fragment ] - * - * NOTES: - * If the URI is of an unknown scheme type and has a "//" following the scheme then it - * is treated as a hierarchical URI, but, if the CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is - * set then it is considered an opaque URI regardless of what follows the scheme information - * (per MSDN documentation). - */ -static BOOL parse_hierpart(const WCHAR **ptr, parse_data *data, DWORD flags) { - const WCHAR *start = *ptr; - - data->must_have_path = FALSE; - - /* For javascript: URIs, simply set everything as a path */ - if(data->scheme_type == URL_SCHEME_JAVASCRIPT) { - data->path = *ptr; - data->path_len = lstrlenW(*ptr); - data->is_opaque = TRUE; - *ptr += data->path_len; - return TRUE; - } - - /* Checks if the authority information needs to be parsed. */ - if(is_hierarchical_uri(ptr, data)) { - /* Only treat it as a hierarchical URI if the scheme_type is known or - * the Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES flag is not set. - */ - if(data->scheme_type != URL_SCHEME_UNKNOWN || - !(flags & Uri_CREATE_NO_CRACK_UNKNOWN_SCHEMES)) { - TRACE("(%p %p %lx): Treating URI as an hierarchical URI.\n", ptr, data, flags); - data->is_opaque = FALSE; - - if(data->scheme_type == URL_SCHEME_WILDCARD && !data->has_implicit_scheme) { - if(**ptr == '/' && *(*ptr+1) == '/') { - data->must_have_path = TRUE; - *ptr += 2; - } - } - - /* TODO: Handle hierarchical URI's, parse authority then parse the path. */ - if(!parse_authority(ptr, data, flags)) - return FALSE; - - return parse_path_hierarchical(ptr, data, flags); - } else - /* Reset ptr to its starting position so opaque path parsing - * begins at the correct location. - */ - *ptr = start; - } - - /* If it reaches here, then the URI will be treated as an opaque - * URI. - */ - - TRACE("(%p %p %lx): Treating URI as an opaque URI.\n", ptr, data, flags); - - data->is_opaque = TRUE; - if(!parse_path_opaque(ptr, data, flags)) - return FALSE; - - return TRUE; -} - -/* Attempts to parse the query string from the URI. - * - * NOTES: - * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded - * data is allowed to appear in the query string. For unknown scheme types - * invalid percent encoded data is allowed to appear regardless. - */ -static BOOL parse_query(const WCHAR **ptr, parse_data *data, DWORD flags) { - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - - if(**ptr != '?') { - TRACE("(%p %p %lx): URI didn't contain a query string.\n", ptr, data, flags); - return TRUE; - } - - data->query = *ptr; - - ++(*ptr); - while(**ptr && **ptr != '#') { - if(**ptr == '%' && known_scheme && - !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) { - if(!check_pct_encoded(ptr)) { - *ptr = data->query; - data->query = NULL; - return FALSE; - } else - continue; - } - - ++(*ptr); - } - - data->query_len = *ptr - data->query; - - TRACE("(%p %p %lx): Parsed query string %s len=%ld\n", ptr, data, flags, - debugstr_wn(data->query, data->query_len), data->query_len); - return TRUE; -} - -/* Attempts to parse the fragment from the URI. - * - * NOTES: - * If NO_DECODE_EXTRA_INFO flag is set, then invalid percent encoded - * data is allowed to appear in the query string. For unknown scheme types - * invalid percent encoded data is allowed to appear regardless. - */ -static BOOL parse_fragment(const WCHAR **ptr, parse_data *data, DWORD flags) { - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - - if(**ptr != '#') { - TRACE("(%p %p %lx): URI didn't contain a fragment.\n", ptr, data, flags); - return TRUE; - } - - data->fragment = *ptr; - - ++(*ptr); - while(**ptr) { - if(**ptr == '%' && known_scheme && - !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) { - if(!check_pct_encoded(ptr)) { - *ptr = data->fragment; - data->fragment = NULL; - return FALSE; - } else - continue; - } - - ++(*ptr); - } - - data->fragment_len = *ptr - data->fragment; - - TRACE("(%p %p %lx): Parsed fragment %s len=%ld\n", ptr, data, flags, - debugstr_wn(data->fragment, data->fragment_len), data->fragment_len); - return TRUE; -} - -/* Parses and validates the components of the specified by data->uri - * and stores the information it parses into 'data'. - * - * Returns TRUE if it successfully parsed the URI. False otherwise. - */ -static BOOL parse_uri(parse_data *data, DWORD flags) { - const WCHAR *ptr; - const WCHAR **pptr; - - ptr = data->uri; - pptr = &ptr; - - TRACE("(%p %lx): BEGINNING TO PARSE URI %s.\n", data, flags, debugstr_w(data->uri)); - - if(!parse_scheme(pptr, data, flags, 0)) - return FALSE; - - if(!parse_hierpart(pptr, data, flags)) - return FALSE; - - if(!parse_query(pptr, data, flags)) - return FALSE; - - if(!parse_fragment(pptr, data, flags)) - return FALSE; - - TRACE("(%p %lx): FINISHED PARSING URI.\n", data, flags); - return TRUE; -} - -static BOOL canonicalize_username(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - const WCHAR *ptr; - - if(!data->username) { - uri->userinfo_start = -1; - return TRUE; - } - - uri->userinfo_start = uri->canon_len; - for(ptr = data->username; ptr < data->username+data->username_len; ++ptr) { - if(*ptr == '%') { - /* Only decode % encoded values for known scheme types. */ - if(data->scheme_type != URL_SCHEME_UNKNOWN) { - /* See if the value really needs decoding. */ - WCHAR val = decode_pct_val(ptr); - if(is_unreserved(val)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = val; - - ++uri->canon_len; - - /* Move pass the hex characters. */ - ptr += 2; - continue; - } - } - } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') { - /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag - * is NOT set. - */ - if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) { - if(!computeOnly) - pct_encode_val(*ptr, uri->canon_uri + uri->canon_len); - - uri->canon_len += 3; - continue; - } - } - - if(!computeOnly) - /* Nothing special, so just copy the character over. */ - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } - - return TRUE; -} - -static BOOL canonicalize_password(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - const WCHAR *ptr; - - if(!data->password) { - uri->userinfo_split = -1; - return TRUE; - } - - if(uri->userinfo_start == -1) - /* Has a password, but, doesn't have a username. */ - uri->userinfo_start = uri->canon_len; - - uri->userinfo_split = uri->canon_len - uri->userinfo_start; - - /* Add the ':' to the userinfo component. */ - if(!computeOnly) - uri->canon_uri[uri->canon_len] = ':'; - ++uri->canon_len; - - for(ptr = data->password; ptr < data->password+data->password_len; ++ptr) { - if(*ptr == '%') { - /* Only decode % encoded values for known scheme types. */ - if(data->scheme_type != URL_SCHEME_UNKNOWN) { - /* See if the value really needs decoding. */ - WCHAR val = decode_pct_val(ptr); - if(is_unreserved(val)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = val; - - ++uri->canon_len; - - /* Move pass the hex characters. */ - ptr += 2; - continue; - } - } - } else if(is_ascii(*ptr) && !is_reserved(*ptr) && !is_unreserved(*ptr) && *ptr != '\\') { - /* Only percent encode forbidden characters if the NO_ENCODE_FORBIDDEN_CHARACTERS flag - * is NOT set. - */ - if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) { - if(!computeOnly) - pct_encode_val(*ptr, uri->canon_uri + uri->canon_len); - - uri->canon_len += 3; - continue; - } - } - - if(!computeOnly) - /* Nothing special, so just copy the character over. */ - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } - - return TRUE; -} - -/* Canonicalizes the userinfo of the URI represented by the parse_data. - * - * Canonicalization of the userinfo is a simple process. If there are any percent - * encoded characters that fall in the "unreserved" character set, they are decoded - * to their actual value. If a character is not in the "unreserved" or "reserved" sets - * then it is percent encoded. Other than that the characters are copied over without - * change. - */ -static BOOL canonicalize_userinfo(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - uri->userinfo_start = uri->userinfo_split = -1; - uri->userinfo_len = 0; - - if(!data->username && !data->password) - /* URI doesn't have userinfo, so nothing to do here. */ - return TRUE; - - if(!canonicalize_username(data, uri, flags, computeOnly)) - return FALSE; - - if(!canonicalize_password(data, uri, flags, computeOnly)) - return FALSE; - - uri->userinfo_len = uri->canon_len - uri->userinfo_start; - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalized userinfo, userinfo_start=%d, userinfo=%s, userinfo_split=%d userinfo_len=%ld.\n", - data, uri, flags, computeOnly, uri->userinfo_start, debugstr_wn(uri->canon_uri + uri->userinfo_start, uri->userinfo_len), - uri->userinfo_split, uri->userinfo_len); - - /* Now insert the '@' after the userinfo. */ - if(!computeOnly) - uri->canon_uri[uri->canon_len] = '@'; - ++uri->canon_len; - - return TRUE; -} - -/* Attempts to canonicalize a reg_name. - * - * Things that happen: - * 1) If Uri_CREATE_NO_CANONICALIZE flag is not set, then the reg_name is - * lower cased. Unless it's an unknown scheme type, which case it's - * no lower cased regardless. - * - * 2) Unreserved % encoded characters are decoded for known - * scheme types. - * - * 3) Forbidden characters are % encoded as long as - * Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS flag is not set and - * it isn't an unknown scheme type. - * - * 4) If it's a file scheme and the host is "localhost" it's removed. - * - * 5) If it's a file scheme and Uri_CREATE_FILE_USE_DOS_PATH is set, - * then the UNC path characters are added before the host name. - */ -static BOOL canonicalize_reg_name(const parse_data *data, Uri *uri, - DWORD flags, BOOL computeOnly) { - const WCHAR *ptr; - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - - if(data->scheme_type == URL_SCHEME_FILE && - data->host_len == lstrlenW(L"localhost")) { - if(!StrCmpNIW(data->host, L"localhost", data->host_len)) { - uri->host_start = -1; - uri->host_len = 0; - uri->host_type = Uri_HOST_UNKNOWN; - return TRUE; - } - } - - if(data->scheme_type == URL_SCHEME_FILE && flags & Uri_CREATE_FILE_USE_DOS_PATH) { - if(!computeOnly) { - uri->canon_uri[uri->canon_len] = '\\'; - uri->canon_uri[uri->canon_len+1] = '\\'; - } - uri->canon_len += 2; - uri->authority_start = uri->canon_len; - } - - uri->host_start = uri->canon_len; - - for(ptr = data->host; ptr < data->host+data->host_len; ++ptr) { - if(*ptr == '%' && known_scheme) { - WCHAR val = decode_pct_val(ptr); - if(is_unreserved(val)) { - /* If NO_CANONICALIZE is not set, then windows lower cases the - * decoded value. - */ - if(!(flags & Uri_CREATE_NO_CANONICALIZE) && iswupper(val)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = towlower(val); - } else { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = val; - } - ++uri->canon_len; - - /* Skip past the % encoded character. */ - ptr += 2; - continue; - } else { - /* Just copy the % over. */ - if(!computeOnly) - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } - } else if(*ptr == '\\') { - /* Only unknown scheme types could have made it here with a '\\' in the host name. */ - if(!computeOnly) - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } else if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) && is_ascii(*ptr) && - !is_unreserved(*ptr) && !is_reserved(*ptr) && known_scheme) { - if(!computeOnly) { - pct_encode_val(*ptr, uri->canon_uri+uri->canon_len); - - /* The percent encoded value gets lower cased also. */ - if(!(flags & Uri_CREATE_NO_CANONICALIZE)) { - uri->canon_uri[uri->canon_len+1] = towlower(uri->canon_uri[uri->canon_len+1]); - uri->canon_uri[uri->canon_len+2] = towlower(uri->canon_uri[uri->canon_len+2]); - } - } - - uri->canon_len += 3; - } else { - if(!computeOnly) { - if(!(flags & Uri_CREATE_NO_CANONICALIZE) && known_scheme) - uri->canon_uri[uri->canon_len] = towlower(*ptr); - else - uri->canon_uri[uri->canon_len] = *ptr; - } - - ++uri->canon_len; - } - } - - uri->host_len = uri->canon_len - uri->host_start; - - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalize reg_name=%s len=%ld\n", data, uri, flags, - computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len), - uri->host_len); - - if(!computeOnly) - find_domain_name(uri->canon_uri+uri->host_start, uri->host_len, - &(uri->domain_offset)); - - return TRUE; -} - -/* Attempts to canonicalize an implicit IPv4 address. */ -static BOOL canonicalize_implicit_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - uri->host_start = uri->canon_len; - - TRACE("%u\n", data->implicit_ipv4); - /* For unknown scheme types Windows doesn't convert - * the value into an IP address, but it still considers - * it an IPv4 address. - */ - if(data->scheme_type == URL_SCHEME_UNKNOWN) { - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR)); - uri->canon_len += data->host_len; - } else { - if(!computeOnly) - uri->canon_len += ui2ipv4(uri->canon_uri+uri->canon_len, data->implicit_ipv4); - else - uri->canon_len += ui2ipv4(NULL, data->implicit_ipv4); - } - - uri->host_len = uri->canon_len - uri->host_start; - uri->host_type = Uri_HOST_IPV4; - - if(!computeOnly) - TRACE("%p %p %lx %d): Canonicalized implicit IP address=%s len=%ld\n", - data, uri, flags, computeOnly, - debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len), - uri->host_len); - - return TRUE; -} - -/* Attempts to canonicalize an IPv4 address. - * - * If the parse_data represents a URI that has an implicit IPv4 address - * (ex. http://256/, this function will convert 256 into 0.0.1.0). If - * the implicit IP address exceeds the value of UINT_MAX (maximum value - * for an IPv4 address) it's canonicalized as if it were a reg-name. - * - * If the parse_data contains a partial or full IPv4 address it normalizes it. - * A partial IPv4 address is something like "192.0" and would be normalized to - * "192.0.0.0". With a full (or partial) IPv4 address like "192.002.01.003" would - * be normalized to "192.2.1.3". - * - * NOTES: - * Windows ONLY normalizes IPv4 address for known scheme types (one that isn't - * URL_SCHEME_UNKNOWN). For unknown scheme types, it simply copies the data from - * the original URI into the canonicalized URI, but, it still recognizes URI's - * host type as HOST_IPV4. - */ -static BOOL canonicalize_ipv4address(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - if(data->has_implicit_ip) - return canonicalize_implicit_ipv4address(data, uri, flags, computeOnly); - else { - uri->host_start = uri->canon_len; - - /* Windows only normalizes for known scheme types. */ - if(data->scheme_type != URL_SCHEME_UNKNOWN) { - /* parse_data contains a partial or full IPv4 address, so normalize it. */ - DWORD i, octetDigitCount = 0, octetCount = 0; - BOOL octetHasDigit = FALSE; - - for(i = 0; i < data->host_len; ++i) { - if(data->host[i] == '0' && !octetHasDigit) { - /* Can ignore leading zeros if: - * 1) It isn't the last digit of the octet. - * 2) i+1 != data->host_len - * 3) i+1 != '.' - */ - if(octetDigitCount == 2 || - i+1 == data->host_len || - data->host[i+1] == '.') { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = data->host[i]; - ++uri->canon_len; - TRACE("Adding zero\n"); - } - } else if(data->host[i] == '.') { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = data->host[i]; - ++uri->canon_len; - - octetDigitCount = 0; - octetHasDigit = FALSE; - ++octetCount; - } else { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = data->host[i]; - ++uri->canon_len; - - ++octetDigitCount; - octetHasDigit = TRUE; - } - } - - /* Make sure the canonicalized IP address has 4 dec-octets. - * If doesn't add "0" ones until there is 4; - */ - for( ; octetCount < 3; ++octetCount) { - if(!computeOnly) { - uri->canon_uri[uri->canon_len] = '.'; - uri->canon_uri[uri->canon_len+1] = '0'; - } - - uri->canon_len += 2; - } - } else { - /* Windows doesn't normalize addresses in unknown schemes. */ - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR)); - uri->canon_len += data->host_len; - } - - uri->host_len = uri->canon_len - uri->host_start; - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalized IPv4 address, ip=%s len=%ld\n", - data, uri, flags, computeOnly, - debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len), - uri->host_len); - } - - return TRUE; -} - -/* Attempts to canonicalize the IPv6 address of the URI. - * - * Multiple things happen during the canonicalization of an IPv6 address: - * 1) Any leading zero's in a h16 component are removed. - * Ex: [0001:0022::] -> [1:22::] - * - * 2) The longest sequence of zero h16 components are compressed - * into a "::" (elision). If there's a tie, the first is chosen. - * - * Ex: [0:0:0:0:1:6:7:8] -> [::1:6:7:8] - * [0:0:0:0:1:2::] -> [::1:2:0:0] - * [0:0:1:2:0:0:7:8] -> [::1:2:0:0:7:8] - * - * 3) If an IPv4 address is attached to the IPv6 address, it's - * also normalized. - * Ex: [::001.002.022.000] -> [::1.2.22.0] - * - * 4) If an elision is present, but, only represents one h16 component - * it's expanded. - * - * Ex: [1::2:3:4:5:6:7] -> [1:0:2:3:4:5:6:7] - * - * 5) If the IPv6 address contains an IPv4 address and there exists - * at least 1 non-zero h16 component the IPv4 address is converted - * into two h16 components, otherwise it's normalized and kept as is. - * - * Ex: [::192.200.003.4] -> [::192.200.3.4] - * [ffff::192.200.003.4] -> [ffff::c0c8:3041] - * - * NOTE: - * For unknown scheme types Windows simply copies the address over without any - * changes. - * - * IPv4 address can be included in an elision if all its components are 0's. - */ -static BOOL canonicalize_ipv6address(const parse_data *data, Uri *uri, - DWORD flags, BOOL computeOnly) { - uri->host_start = uri->canon_len; - - if(data->scheme_type == URL_SCHEME_UNKNOWN) { - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR)); - uri->canon_len += data->host_len; - } else { - WCHAR buffer[46]; - ULONG size = ARRAY_SIZE(buffer); - - if(computeOnly) { - RtlIpv6AddressToStringExW(&data->ipv6_address, 0, 0, buffer, &size); - uri->canon_len += size + 1; - } else { - uri->canon_uri[uri->canon_len++] = '['; - RtlIpv6AddressToStringExW(&data->ipv6_address, 0, 0, uri->canon_uri + uri->canon_len, &size); - uri->canon_len += size - 1; - uri->canon_uri[uri->canon_len++] = ']'; - } - } - - uri->host_len = uri->canon_len - uri->host_start; - - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalized IPv6 address %s, len=%ld\n", data, uri, flags, - computeOnly, debugstr_wn(uri->canon_uri+uri->host_start, uri->host_len), - uri->host_len); - - return TRUE; -} - -/* Attempts to canonicalize the host of the URI (if any). */ -static BOOL canonicalize_host(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - uri->host_start = -1; - uri->host_len = 0; - uri->domain_offset = -1; - - if(data->host) { - switch(data->host_type) { - case Uri_HOST_DNS: - uri->host_type = Uri_HOST_DNS; - if(!canonicalize_reg_name(data, uri, flags, computeOnly)) - return FALSE; - - break; - case Uri_HOST_IPV4: - uri->host_type = Uri_HOST_IPV4; - if(!canonicalize_ipv4address(data, uri, flags, computeOnly)) - return FALSE; - - break; - case Uri_HOST_IPV6: - if(!canonicalize_ipv6address(data, uri, flags, computeOnly)) - return FALSE; - - uri->host_type = Uri_HOST_IPV6; - break; - - case Uri_HOST_IDN: - uri->host_type = Uri_HOST_IDN; - if(!canonicalize_reg_name(data, uri, flags, computeOnly)) - return FALSE; - - break; - case Uri_HOST_UNKNOWN: - if(data->host_len > 0 || data->scheme_type != URL_SCHEME_FILE) { - uri->host_start = uri->canon_len; - - /* Nothing happens to unknown host types. */ - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->host, data->host_len*sizeof(WCHAR)); - uri->canon_len += data->host_len; - uri->host_len = data->host_len; - } - - uri->host_type = Uri_HOST_UNKNOWN; - break; - default: - FIXME("(%p %p %lx %d): Canonicalization for host type %d not supported.\n", data, - uri, flags, computeOnly, data->host_type); - return FALSE; - } - } - - return TRUE; -} - -static BOOL canonicalize_port(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - BOOL has_default_port = FALSE; - USHORT default_port = 0; - DWORD i; - - uri->port_offset = -1; - - /* Check if the scheme has a default port. */ - for(i = 0; i < ARRAY_SIZE(default_ports); ++i) { - if(default_ports[i].scheme == data->scheme_type) { - has_default_port = TRUE; - default_port = default_ports[i].port; - break; - } - } - - uri->has_port = data->has_port || has_default_port; - - /* Possible cases: - * 1) Has a port which is the default port. - * 2) Has a port (not the default). - * 3) Doesn't have a port, but, scheme has a default port. - * 4) No port. - */ - if(has_default_port && data->has_port && data->port_value == default_port) { - /* If it's the default port and this flag isn't set, don't do anything. */ - if(flags & Uri_CREATE_NO_CANONICALIZE) { - uri->port_offset = uri->canon_len-uri->authority_start; - if(!computeOnly) - uri->canon_uri[uri->canon_len] = ':'; - ++uri->canon_len; - - if(data->port) { - /* Copy the original port over. */ - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR)); - uri->canon_len += data->port_len; - } else { - if(!computeOnly) - uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value); - else - uri->canon_len += ui2str(NULL, data->port_value); - } - } - - uri->port = default_port; - } else if(data->has_port) { - uri->port_offset = uri->canon_len-uri->authority_start; - if(!computeOnly) - uri->canon_uri[uri->canon_len] = ':'; - ++uri->canon_len; - - if(flags & Uri_CREATE_NO_CANONICALIZE && data->port) { - /* Copy the original over without changes. */ - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->port, data->port_len*sizeof(WCHAR)); - uri->canon_len += data->port_len; - } else { - if(!computeOnly) - uri->canon_len += ui2str(uri->canon_uri+uri->canon_len, data->port_value); - else - uri->canon_len += ui2str(NULL, data->port_value); - } - - uri->port = data->port_value; - } else if(has_default_port) - uri->port = default_port; - - return TRUE; -} - -/* Canonicalizes the authority of the URI represented by the parse_data. */ -static BOOL canonicalize_authority(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - uri->authority_start = uri->canon_len; - uri->authority_len = 0; - - if(!canonicalize_userinfo(data, uri, flags, computeOnly)) - return FALSE; - - if(!canonicalize_host(data, uri, flags, computeOnly)) - return FALSE; - - if(!canonicalize_port(data, uri, flags, computeOnly)) - return FALSE; - - if(uri->host_start != -1 || (data->is_relative && (data->password || data->username))) - uri->authority_len = uri->canon_len - uri->authority_start; - else - uri->authority_start = -1; - - return TRUE; -} - -/* Attempts to canonicalize the path of a hierarchical URI. - * - * Things that happen: - * 1). Forbidden characters are percent encoded, unless the NO_ENCODE_FORBIDDEN - * flag is set or it's a file URI. Forbidden characters are always encoded - * for file schemes regardless and forbidden characters are never encoded - * for unknown scheme types. - * - * 2). For known scheme types '\\' are changed to '/'. - * - * 3). Percent encoded, unreserved characters are decoded to their actual values. - * Unless the scheme type is unknown. For file schemes any percent encoded - * character in the unreserved or reserved set is decoded. - * - * 4). For File schemes if the path is starts with a drive letter and doesn't - * start with a '/' then one is appended. - * Ex: file://c:/test.mp3 -> file:///c:/test.mp3 - * - * 5). Dot segments are removed from the path for all scheme types - * unless NO_CANONICALIZE flag is set. Dot segments aren't removed - * for wildcard scheme types. - * - * NOTES: - * file://c:/test%20test -> file:///c:/test%2520test - * file://c:/test%3Etest -> file:///c:/test%253Etest - * if Uri_CREATE_FILE_USE_DOS_PATH is not set: - * file:///c:/test%20test -> file:///c:/test%20test - * file:///c:/test%test -> file:///c:/test%25test - */ -static DWORD canonicalize_path_hierarchical(const WCHAR *path, DWORD path_len, URL_SCHEME scheme_type, BOOL has_host, DWORD flags, - BOOL is_implicit_scheme, WCHAR *ret_path) { - const BOOL known_scheme = scheme_type != URL_SCHEME_UNKNOWN; - const BOOL is_file = scheme_type == URL_SCHEME_FILE; - const BOOL is_res = scheme_type == URL_SCHEME_RES; - const WCHAR *ptr; - BOOL escape_pct = FALSE; - DWORD len = 0; - - if(!path) - return 0; - - ptr = path; - - if(is_file && !has_host) { - /* Check if a '/' needs to be appended for the file scheme. */ - if(path_len > 1 && is_drive_path(ptr) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - if(ret_path) - ret_path[len] = '/'; - len++; - escape_pct = TRUE; - } else if(*ptr == '/') { - if(!(flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - /* Copy the extra '/' over. */ - if(ret_path) - ret_path[len] = '/'; - len++; - } - ++ptr; - } - - if(is_drive_path(ptr)) { - if(ret_path) { - ret_path[len] = *ptr; - /* If there's a '|' after the drive letter, convert it to a ':'. */ - ret_path[len+1] = ':'; - } - ptr += 2; - len += 2; - } - } - - if(!is_file && *path && *path != '/') { - /* Prepend a '/' to the path if it doesn't have one. */ - if(ret_path) - ret_path[len] = '/'; - len++; - } - - for(; ptr < path+path_len; ++ptr) { - BOOL do_default_action = TRUE; - - if(*ptr == '%' && !is_res) { - const WCHAR *tmp = ptr; - WCHAR val; - - /* Check if the % represents a valid encoded char, or if it needs encoding. */ - BOOL force_encode = !check_pct_encoded(&tmp) && is_file && !(flags&Uri_CREATE_FILE_USE_DOS_PATH); - val = decode_pct_val(ptr); - - if(force_encode || escape_pct) { - /* Escape the percent sign in the file URI. */ - if(ret_path) - pct_encode_val(*ptr, ret_path+len); - len += 3; - do_default_action = FALSE; - } else if((is_unreserved(val) && known_scheme) || - (is_file && !is_implicit_scheme && (is_unreserved(val) || is_reserved(val) || - (val && flags&Uri_CREATE_FILE_USE_DOS_PATH && !is_forbidden_dos_path_char(val))))) { - if(ret_path) - ret_path[len] = val; - len++; - - ptr += 2; - continue; - } - } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - /* Convert the '/' back to a '\\'. */ - if(ret_path) - ret_path[len] = '\\'; - len++; - do_default_action = FALSE; - } else if(*ptr == '\\' && known_scheme) { - if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) { - /* Convert '\\' into a '/'. */ - if(ret_path) - ret_path[len] = '/'; - len++; - do_default_action = FALSE; - } - } else if(known_scheme && !is_res && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr) && - (!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) || is_file)) { - if(!is_file || !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - /* Escape the forbidden character. */ - if(ret_path) - pct_encode_val(*ptr, ret_path+len); - len += 3; - do_default_action = FALSE; - } - } - - if(do_default_action) { - if(ret_path) - ret_path[len] = *ptr; - len++; - } - } - - /* Removing the dot segments only happens when it's not in - * computeOnly mode and it's not a wildcard scheme. File schemes - * with USE_DOS_PATH set don't get dot segments removed. - */ - if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) && - scheme_type != URL_SCHEME_WILDCARD) { - if(!(flags & Uri_CREATE_NO_CANONICALIZE) && ret_path) { - /* Remove the dot segments (if any) and reset everything to the new - * correct length. - */ - len = remove_dot_segments(ret_path, len); - } - } - - if(ret_path) - TRACE("Canonicalized path %s len=%ld\n", debugstr_wn(ret_path, len), len); - return len; -} - -/* Attempts to canonicalize the path for an opaque URI. - * - * For known scheme types: - * 1) forbidden characters are percent encoded if - * NO_ENCODE_FORBIDDEN_CHARACTERS isn't set. - * - * 2) Percent encoded, unreserved characters are decoded - * to their actual values, for known scheme types. - * - * 3) '\\' are changed to '/' for known scheme types - * except for mailto schemes. - * - * 4) For file schemes, if USE_DOS_PATH is set all '/' - * are converted to backslashes. - * - * 5) For file schemes, if USE_DOS_PATH isn't set all '\' - * are converted to forward slashes. - */ -static BOOL canonicalize_path_opaque(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - const WCHAR *ptr; - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - const BOOL is_file = data->scheme_type == URL_SCHEME_FILE; - const BOOL is_mk = data->scheme_type == URL_SCHEME_MK; - - if(!data->path) { - uri->path_start = -1; - uri->path_len = 0; - return TRUE; - } - - uri->path_start = uri->canon_len; - - if(is_mk){ - /* hijack this flag for SCHEME_MK to tell the function when to start - * converting slashes */ - flags |= Uri_CREATE_FILE_USE_DOS_PATH; - } - - /* For javascript: URIs, simply copy path part without any canonicalization */ - if(data->scheme_type == URL_SCHEME_JAVASCRIPT) { - if(!computeOnly) - memcpy(uri->canon_uri+uri->canon_len, data->path, data->path_len*sizeof(WCHAR)); - uri->path_len = data->path_len; - uri->canon_len += data->path_len; - return TRUE; - } - - /* Windows doesn't allow a "//" to appear after the scheme - * of a URI, if it's an opaque URI. - */ - if(data->scheme && *(data->path) == '/' && *(data->path+1) == '/') { - /* So it inserts a "/." before the "//" if it exists. */ - if(!computeOnly) { - uri->canon_uri[uri->canon_len] = '/'; - uri->canon_uri[uri->canon_len+1] = '.'; - } - - uri->canon_len += 2; - } - - for(ptr = data->path; ptr < data->path+data->path_len; ++ptr) { - BOOL do_default_action = TRUE; - - if(*ptr == '%' && known_scheme) { - WCHAR val = decode_pct_val(ptr); - - if(is_unreserved(val)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = val; - ++uri->canon_len; - - ptr += 2; - continue; - } - } else if(*ptr == '/' && is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = '\\'; - ++uri->canon_len; - do_default_action = FALSE; - } else if(*ptr == '\\') { - if((data->is_relative || is_mk || is_file) && !(flags & Uri_CREATE_FILE_USE_DOS_PATH)) { - /* Convert to a '/'. */ - if(!computeOnly) - uri->canon_uri[uri->canon_len] = '/'; - ++uri->canon_len; - do_default_action = FALSE; - } - } else if(is_mk && *ptr == ':' && ptr + 1 < data->path + data->path_len && *(ptr + 1) == ':') { - flags &= ~Uri_CREATE_FILE_USE_DOS_PATH; - } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr) && - !(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS)) { - if(!(is_file && (flags & Uri_CREATE_FILE_USE_DOS_PATH))) { - if(!computeOnly) - pct_encode_val(*ptr, uri->canon_uri+uri->canon_len); - uri->canon_len += 3; - do_default_action = FALSE; - } - } - - if(do_default_action) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } - } - - if(is_mk && !computeOnly && !(flags & Uri_CREATE_NO_CANONICALIZE)) { - DWORD new_len = remove_dot_segments(uri->canon_uri + uri->path_start, - uri->canon_len - uri->path_start); - uri->canon_len = uri->path_start + new_len; - } - - uri->path_len = uri->canon_len - uri->path_start; - - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalized opaque URI path %s len=%ld\n", data, uri, flags, computeOnly, - debugstr_wn(uri->canon_uri+uri->path_start, uri->path_len), uri->path_len); - return TRUE; -} - -/* Determines how the URI represented by the parse_data should be canonicalized. - * - * Essentially, if the parse_data represents an hierarchical URI then it calls - * canonicalize_authority and the canonicalization functions for the path. If the - * URI is opaque it canonicalizes the path of the URI. - */ -static BOOL canonicalize_hierpart(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - if(!data->is_opaque || (data->is_relative && (data->password || data->username))) { - /* "//" is only added for non-wildcard scheme types. - * - * A "//" is only added to a relative URI if it has a - * host or port component (this only happens if a IUriBuilder - * is generating an IUri). - */ - if((data->is_relative && (data->host || data->has_port)) || - (!data->is_relative && data->scheme_type != URL_SCHEME_WILDCARD)) { - if(data->scheme_type == URL_SCHEME_WILDCARD) - FIXME("Here\n"); - - if(!computeOnly) { - INT pos = uri->canon_len; - - uri->canon_uri[pos] = '/'; - uri->canon_uri[pos+1] = '/'; - } - uri->canon_len += 2; - } - - if(!canonicalize_authority(data, uri, flags, computeOnly)) - return FALSE; - - if(data->is_relative && (data->password || data->username)) { - if(!canonicalize_path_opaque(data, uri, flags, computeOnly)) - return FALSE; - } else { - if(!computeOnly) - uri->path_start = uri->canon_len; - uri->path_len = canonicalize_path_hierarchical(data->path, data->path_len, data->scheme_type, data->host_len != 0, - flags, data->has_implicit_scheme, computeOnly ? NULL : uri->canon_uri+uri->canon_len); - uri->canon_len += uri->path_len; - if(!computeOnly && !uri->path_len) - uri->path_start = -1; - } - } else { - /* Opaque URI's don't have an authority. */ - uri->userinfo_start = uri->userinfo_split = -1; - uri->userinfo_len = 0; - uri->host_start = -1; - uri->host_len = 0; - uri->host_type = Uri_HOST_UNKNOWN; - uri->has_port = FALSE; - uri->authority_start = -1; - uri->authority_len = 0; - uri->domain_offset = -1; - uri->port_offset = -1; - - if(is_hierarchical_scheme(data->scheme_type)) { - DWORD i; - - /* Absolute URIs aren't displayed for known scheme types - * which should be hierarchical URIs. - */ - uri->display_modifiers |= URI_DISPLAY_NO_ABSOLUTE_URI; - - /* Windows also sets the port for these (if they have one). */ - for(i = 0; i < ARRAY_SIZE(default_ports); ++i) { - if(data->scheme_type == default_ports[i].scheme) { - uri->has_port = TRUE; - uri->port = default_ports[i].port; - break; - } - } - } - - if(!canonicalize_path_opaque(data, uri, flags, computeOnly)) - return FALSE; - } - - if(uri->path_start > -1 && !computeOnly) - /* Finding file extensions happens for both types of URIs. */ - uri->extension_offset = find_file_extension(uri->canon_uri+uri->path_start, uri->path_len); - else - uri->extension_offset = -1; - - return TRUE; -} - -/* Attempts to canonicalize the query string of the URI. - * - * Things that happen: - * 1) For known scheme types forbidden characters - * are percent encoded, unless the NO_DECODE_EXTRA_INFO flag is set - * or NO_ENCODE_FORBIDDEN_CHARACTERS is set. - * - * 2) For known scheme types, percent encoded, unreserved characters - * are decoded as long as the NO_DECODE_EXTRA_INFO flag isn't set. - */ -static BOOL canonicalize_query(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - const WCHAR *ptr, *end; - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - - if(!data->query) { - uri->query_start = -1; - uri->query_len = 0; - return TRUE; - } - - uri->query_start = uri->canon_len; - - end = data->query+data->query_len; - for(ptr = data->query; ptr < end; ++ptr) { - if(*ptr == '%') { - if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) { - WCHAR val = decode_pct_val(ptr); - if(is_unreserved(val)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = val; - ++uri->canon_len; - - ptr += 2; - continue; - } - } - } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr)) { - if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) && - !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) { - if(!computeOnly) - pct_encode_val(*ptr, uri->canon_uri+uri->canon_len); - uri->canon_len += 3; - continue; - } - } - - if(!computeOnly) - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } - - uri->query_len = uri->canon_len - uri->query_start; - - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalized query string %s len=%ld\n", data, uri, flags, - computeOnly, debugstr_wn(uri->canon_uri+uri->query_start, uri->query_len), - uri->query_len); - return TRUE; -} - -static BOOL canonicalize_fragment(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - const WCHAR *ptr, *end; - const BOOL known_scheme = data->scheme_type != URL_SCHEME_UNKNOWN; - - if(!data->fragment) { - uri->fragment_start = -1; - uri->fragment_len = 0; - return TRUE; - } - - uri->fragment_start = uri->canon_len; - - end = data->fragment + data->fragment_len; - for(ptr = data->fragment; ptr < end; ++ptr) { - if(*ptr == '%') { - if(known_scheme && !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) { - WCHAR val = decode_pct_val(ptr); - if(is_unreserved(val)) { - if(!computeOnly) - uri->canon_uri[uri->canon_len] = val; - ++uri->canon_len; - - ptr += 2; - continue; - } - } - } else if(known_scheme && is_ascii(*ptr) && !is_unreserved(*ptr) && !is_reserved(*ptr)) { - if(!(flags & Uri_CREATE_NO_ENCODE_FORBIDDEN_CHARACTERS) && - !(flags & Uri_CREATE_NO_DECODE_EXTRA_INFO)) { - if(!computeOnly) - pct_encode_val(*ptr, uri->canon_uri+uri->canon_len); - uri->canon_len += 3; - continue; - } - } - - if(!computeOnly) - uri->canon_uri[uri->canon_len] = *ptr; - ++uri->canon_len; - } - - uri->fragment_len = uri->canon_len - uri->fragment_start; - - if(!computeOnly) - TRACE("(%p %p %lx %d): Canonicalized fragment %s len=%ld\n", data, uri, flags, - computeOnly, debugstr_wn(uri->canon_uri+uri->fragment_start, uri->fragment_len), - uri->fragment_len); - return TRUE; -} - -/* Canonicalizes the scheme information specified in the parse_data using the specified flags. */ -static BOOL canonicalize_scheme(const parse_data *data, Uri *uri, DWORD flags, BOOL computeOnly) { - uri->scheme_start = -1; - uri->scheme_len = 0; - - if(!data->scheme) { - /* The only type of URI that doesn't have to have a scheme is a relative - * URI. - */ - if(!data->is_relative) { - FIXME("(%p %p %lx): Unable to determine the scheme type of %s.\n", data, - uri, flags, debugstr_w(data->uri)); - return FALSE; - } - } else { - if(!computeOnly) { - DWORD i; - INT pos = uri->canon_len; - - for(i = 0; i < data->scheme_len; ++i) { - /* Scheme name must be lower case after canonicalization. */ - uri->canon_uri[i + pos] = towlower(data->scheme[i]); - } - - uri->canon_uri[i + pos] = ':'; - uri->scheme_start = pos; - - TRACE("(%p %p %lx): Canonicalized scheme=%s, len=%ld.\n", data, uri, flags, - debugstr_wn(uri->canon_uri+uri->scheme_start, data->scheme_len), data->scheme_len); - } - - /* This happens in both computation modes. */ - uri->canon_len += data->scheme_len + 1; - uri->scheme_len = data->scheme_len; - } - return TRUE; -} - -/* Computes what the length of the URI specified by the parse_data will be - * after canonicalization occurs using the specified flags. - * - * This function will return a non-zero value indicating the length of the canonicalized - * URI, or -1 on error. - */ -static int compute_canonicalized_length(const parse_data *data, DWORD flags) { - Uri uri; - - memset(&uri, 0, sizeof(Uri)); - - TRACE("(%p %lx): Beginning to compute canonicalized length for URI %s\n", data, flags, - debugstr_w(data->uri)); - - if(!canonicalize_scheme(data, &uri, flags, TRUE)) { - ERR("(%p %lx): Failed to compute URI scheme length.\n", data, flags); - return -1; - } - - if(!canonicalize_hierpart(data, &uri, flags, TRUE)) { - ERR("(%p %lx): Failed to compute URI hierpart length.\n", data, flags); - return -1; - } - - if(!canonicalize_query(data, &uri, flags, TRUE)) { - ERR("(%p %lx): Failed to compute query string length.\n", data, flags); - return -1; - } - - if(!canonicalize_fragment(data, &uri, flags, TRUE)) { - ERR("(%p %lx): Failed to compute fragment length.\n", data, flags); - return -1; - } - - TRACE("(%p %lx): Finished computing canonicalized URI length. length=%ld\n", data, flags, uri.canon_len); - - return uri.canon_len; -} - -/* Canonicalizes the URI data specified in the parse_data, using the given flags. If the - * canonicalization succeeds it will store all the canonicalization information - * in the pointer to the Uri. - * - * To canonicalize a URI this function first computes what the length of the URI - * specified by the parse_data will be. Once this is done it will then perform the actual - * canonicalization of the URI. - */ -static HRESULT canonicalize_uri(const parse_data *data, Uri *uri, DWORD flags) { - INT len; - - uri->canon_uri = NULL; - uri->canon_size = uri->canon_len = 0; - - TRACE("(%p %p %lx): beginning to canonicalize URI %s.\n", data, uri, flags, debugstr_w(data->uri)); - - /* First try to compute the length of the URI. */ - len = compute_canonicalized_length(data, flags); - if(len == -1) { - ERR("(%p %p %lx): Could not compute the canonicalized length of %s.\n", data, uri, flags, - debugstr_w(data->uri)); - return E_INVALIDARG; - } - - uri->canon_uri = malloc((len + 1) * sizeof(WCHAR)); - if(!uri->canon_uri) - return E_OUTOFMEMORY; - - uri->canon_size = len; - if(!canonicalize_scheme(data, uri, flags, FALSE)) { - ERR("(%p %p %lx): Unable to canonicalize the scheme of the URI.\n", data, uri, flags); - return E_INVALIDARG; - } - uri->scheme_type = data->scheme_type; - - if(!canonicalize_hierpart(data, uri, flags, FALSE)) { - ERR("(%p %p %lx): Unable to canonicalize the hierpart of the URI\n", data, uri, flags); - return E_INVALIDARG; - } - - if(!canonicalize_query(data, uri, flags, FALSE)) { - ERR("(%p %p %lx): Unable to canonicalize query string of the URI.\n", - data, uri, flags); - return E_INVALIDARG; - } - - if(!canonicalize_fragment(data, uri, flags, FALSE)) { - ERR("(%p %p %lx): Unable to canonicalize fragment of the URI.\n", - data, uri, flags); - return E_INVALIDARG; - } - - /* There's a possibility we didn't use all the space we allocated - * earlier. - */ - if(uri->canon_len < uri->canon_size) { - /* This happens if the URI is hierarchical and dot - * segments were removed from its path. - */ - WCHAR *tmp = realloc(uri->canon_uri, (uri->canon_len + 1) * sizeof(WCHAR)); - if(!tmp) - return E_OUTOFMEMORY; - - uri->canon_uri = tmp; - uri->canon_size = uri->canon_len; - } - - uri->canon_uri[uri->canon_len] = '\0'; - TRACE("(%p %p %lx): finished canonicalizing the URI. uri=%s\n", data, uri, flags, debugstr_w(uri->canon_uri)); - - return S_OK; -} - -static HRESULT compare_file_paths(const Uri *a, const Uri *b, BOOL *ret) -{ - WCHAR *canon_path_a, *canon_path_b; - DWORD len_a, len_b; - - if(!a->path_len) { - *ret = !b->path_len; - return S_OK; - } - - if(!b->path_len) { - *ret = FALSE; - return S_OK; - } - - /* Fast path */ - if(a->path_len == b->path_len && !wcsnicmp(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len)) { - *ret = TRUE; - return S_OK; - } - - len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, FALSE, NULL); - len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, FALSE, NULL); - - canon_path_a = malloc(len_a * sizeof(WCHAR)); - if(!canon_path_a) - return E_OUTOFMEMORY; - canon_path_b = malloc(len_b * sizeof(WCHAR)); - if(!canon_path_b) { - free(canon_path_a); - return E_OUTOFMEMORY; - } - - len_a = canonicalize_path_hierarchical(a->canon_uri+a->path_start, a->path_len, a->scheme_type, FALSE, 0, FALSE, canon_path_a); - len_b = canonicalize_path_hierarchical(b->canon_uri+b->path_start, b->path_len, b->scheme_type, FALSE, 0, FALSE, canon_path_b); - - *ret = len_a == len_b && !wcsnicmp(canon_path_a, canon_path_b, len_a); - - free(canon_path_a); - free(canon_path_b); - return S_OK; -} - -/* Checks if the two Uri's are logically equivalent. It's a simple - * comparison, since they are both of type Uri, and it can access - * the properties of each Uri directly without the need to go - * through the "IUri_Get*" interface calls. - */ -static HRESULT compare_uris(const Uri *a, const Uri *b, BOOL *ret) { - const BOOL known_scheme = a->scheme_type != URL_SCHEME_UNKNOWN; - const BOOL are_hierarchical = a->authority_start > -1 && b->authority_start > -1; - HRESULT hres; - - *ret = FALSE; - - if(a->scheme_type != b->scheme_type) - return S_OK; - - /* Only compare the scheme names (if any) if their unknown scheme types. */ - if(!known_scheme) { - if((a->scheme_start > -1 && b->scheme_start > -1) && - (a->scheme_len == b->scheme_len)) { - /* Make sure the schemes are the same. */ - if(StrCmpNW(a->canon_uri+a->scheme_start, b->canon_uri+b->scheme_start, a->scheme_len)) - return S_OK; - } else if(a->scheme_len != b->scheme_len) - /* One of the Uri's has a scheme name, while the other doesn't. */ - return S_OK; - } - - /* If they have a userinfo component, perform case sensitive compare. */ - if((a->userinfo_start > -1 && b->userinfo_start > -1) && - (a->userinfo_len == b->userinfo_len)) { - if(StrCmpNW(a->canon_uri+a->userinfo_start, b->canon_uri+b->userinfo_start, a->userinfo_len)) - return S_OK; - } else if(a->userinfo_len != b->userinfo_len) - /* One of the Uri's had a userinfo, while the other one doesn't. */ - return S_OK; - - /* Check if they have a host name. */ - if((a->host_start > -1 && b->host_start > -1) && - (a->host_len == b->host_len)) { - /* Perform a case insensitive compare if they are a known scheme type. */ - if(known_scheme) { - if(StrCmpNIW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len)) - return S_OK; - } else if(StrCmpNW(a->canon_uri+a->host_start, b->canon_uri+b->host_start, a->host_len)) - return S_OK; - } else if(a->host_len != b->host_len) - /* One of the Uri's had a host, while the other one didn't. */ - return S_OK; - - if(a->has_port && b->has_port) { - if(a->port != b->port) - return S_OK; - } else if(a->has_port || b->has_port) - /* One had a port, while the other one didn't. */ - return S_OK; - - /* Windows is weird with how it handles paths. For example - * One URI could be "http://google.com" (after canonicalization) - * and one could be "http://google.com/" and the IsEqual function - * would still evaluate to TRUE, but, only if they are both hierarchical - * URIs. - */ - if(a->scheme_type == URL_SCHEME_FILE) { - BOOL cmp; - - hres = compare_file_paths(a, b, &cmp); - if(FAILED(hres) || !cmp) - return hres; - } else if((a->path_start > -1 && b->path_start > -1) && - (a->path_len == b->path_len)) { - if(StrCmpNW(a->canon_uri+a->path_start, b->canon_uri+b->path_start, a->path_len)) - return S_OK; - } else if(are_hierarchical && a->path_len == -1 && b->path_len == 0) { - if(*(a->canon_uri+a->path_start) != '/') - return S_OK; - } else if(are_hierarchical && b->path_len == 1 && a->path_len == 0) { - if(*(b->canon_uri+b->path_start) != '/') - return S_OK; - } else if(a->path_len != b->path_len) - return S_OK; - - /* Compare the query strings of the two URIs. */ - if((a->query_start > -1 && b->query_start > -1) && - (a->query_len == b->query_len)) { - if(StrCmpNW(a->canon_uri+a->query_start, b->canon_uri+b->query_start, a->query_len)) - return S_OK; - } else if(a->query_len != b->query_len) - return S_OK; - - if((a->fragment_start > -1 && b->fragment_start > -1) && - (a->fragment_len == b->fragment_len)) { - if(StrCmpNW(a->canon_uri+a->fragment_start, b->canon_uri+b->fragment_start, a->fragment_len)) - return S_OK; - } else if(a->fragment_len != b->fragment_len) - return S_OK; - - /* If we get here, the two URIs are equivalent. */ - *ret = TRUE; - return S_OK; -} - -static inline Uri* impl_from_IUri(IUri *iface) -{ - return CONTAINING_RECORD(iface, Uri, IUri_iface); -} - -static inline void destroy_uri_obj(Uri *This) -{ - SysFreeString(This->raw_uri); - free(This->canon_uri); - free(This); -} - -static HRESULT WINAPI Uri_QueryInterface(IUri *iface, REFIID riid, void **ppv) -{ - Uri *This = impl_from_IUri(iface); - - if(IsEqualGUID(&IID_IUnknown, riid)) { - TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv); - *ppv = &This->IUri_iface; - }else if(IsEqualGUID(&IID_IUri, riid)) { - TRACE("(%p)->(IID_IUri %p)\n", This, ppv); - *ppv = &This->IUri_iface; - }else if(IsEqualGUID(&IID_IUriBuilderFactory, riid)) { - TRACE("(%p)->(IID_IUriBuilderFactory %p)\n", This, ppv); - *ppv = &This->IUriBuilderFactory_iface; - }else if(IsEqualGUID(&IID_IPersistStream, riid)) { - TRACE("(%p)->(IID_IPersistStream %p)\n", This, ppv); - *ppv = &This->IPersistStream_iface; - }else if(IsEqualGUID(&IID_IMarshal, riid)) { - TRACE("(%p)->(IID_IMarshal %p)\n", This, ppv); - *ppv = &This->IMarshal_iface; - }else if(IsEqualGUID(&IID_IUriObj, riid)) { - TRACE("(%p)->(IID_IUriObj %p)\n", This, ppv); - *ppv = This; - return S_OK; - }else { - TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv); - *ppv = NULL; - return E_NOINTERFACE; - } - - IUnknown_AddRef((IUnknown*)*ppv); - return S_OK; -} - -static ULONG WINAPI Uri_AddRef(IUri *iface) -{ - Uri *This = impl_from_IUri(iface); - LONG ref = InterlockedIncrement(&This->ref); - - TRACE("(%p) ref=%ld\n", This, ref); - - return ref; -} - -static ULONG WINAPI Uri_Release(IUri *iface) -{ - Uri *This = impl_from_IUri(iface); - LONG ref = InterlockedDecrement(&This->ref); - - TRACE("(%p) ref=%ld\n", This, ref); - - if(!ref) - destroy_uri_obj(This); - - return ref; -} - -static HRESULT WINAPI Uri_GetPropertyBSTR(IUri *iface, Uri_PROPERTY uriProp, BSTR *pbstrProperty, DWORD dwFlags) -{ - Uri *This = impl_from_IUri(iface); - HRESULT hres; - TRACE("(%p %s)->(%d %p %lx)\n", This, debugstr_w(This->canon_uri), uriProp, pbstrProperty, dwFlags); - - if(!This->create_flags) - return E_UNEXPECTED; - if(!pbstrProperty) - return E_POINTER; - - if(uriProp > Uri_PROPERTY_STRING_LAST) { - /* It only returns S_FALSE for the ZONE property... */ - if(uriProp == Uri_PROPERTY_ZONE) { - *pbstrProperty = SysAllocStringLen(NULL, 0); - if(!(*pbstrProperty)) - return E_OUTOFMEMORY; - return S_FALSE; - } - - *pbstrProperty = NULL; - return E_INVALIDARG; - } - - if(dwFlags != 0 && dwFlags != Uri_DISPLAY_NO_FRAGMENT && dwFlags != Uri_PUNYCODE_IDN_HOST - && dwFlags != Uri_DISPLAY_IDN_HOST) - return E_INVALIDARG; - - if((dwFlags == Uri_DISPLAY_NO_FRAGMENT && uriProp != Uri_PROPERTY_DISPLAY_URI) - || (dwFlags == Uri_PUNYCODE_IDN_HOST && uriProp != Uri_PROPERTY_ABSOLUTE_URI - && uriProp != Uri_PROPERTY_DOMAIN && uriProp != Uri_PROPERTY_HOST) - || (dwFlags == Uri_DISPLAY_IDN_HOST && uriProp != Uri_PROPERTY_ABSOLUTE_URI - && uriProp != Uri_PROPERTY_DOMAIN && uriProp != Uri_PROPERTY_HOST)) - return E_INVALIDARG; - - switch(uriProp) { - case Uri_PROPERTY_ABSOLUTE_URI: - if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - /* Uri_PUNYCODE_IDN_HOST doesn't remove user info containing only "@" and ":@" */ - else if (dwFlags == Uri_PUNYCODE_IDN_HOST && This->host_type == Uri_HOST_IDN && This->host_start > -1) { - unsigned int punycode_host_len; - - punycode_host_len = IdnToAscii(0, This->canon_uri+This->host_start, This->host_len, NULL, 0); - *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-This->host_len+punycode_host_len); - hres = S_OK; - if(*pbstrProperty) { - memcpy(*pbstrProperty, This->canon_uri, This->host_start*sizeof(WCHAR)); - IdnToAscii(0, This->canon_uri+This->host_start, This->host_len, *pbstrProperty+This->host_start, punycode_host_len); - memcpy(*pbstrProperty+This->host_start+punycode_host_len, - This->canon_uri+This->host_start+This->host_len, - (This->canon_len-This->host_start-This->host_len)*sizeof(WCHAR)); - } - } else { - if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) { - if(This->userinfo_len == 0) { - /* Don't include the '@' after the userinfo component. */ - *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-1); - hres = S_OK; - if(*pbstrProperty) { - /* Copy everything before it. */ - memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR)); - - /* And everything after it. */ - memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+1, - (This->canon_len-This->userinfo_start-1)*sizeof(WCHAR)); - } - } else if(This->userinfo_split == 0 && This->userinfo_len == 1) { - /* Don't include the ":@" */ - *pbstrProperty = SysAllocStringLen(NULL, This->canon_len-2); - hres = S_OK; - if(*pbstrProperty) { - memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR)); - memcpy(*pbstrProperty+This->userinfo_start, This->canon_uri+This->userinfo_start+2, - (This->canon_len-This->userinfo_start-2)*sizeof(WCHAR)); - } - } else { - *pbstrProperty = SysAllocString(This->canon_uri); - hres = S_OK; - } - } else { - *pbstrProperty = SysAllocString(This->canon_uri); - hres = S_OK; - } - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_AUTHORITY: - if(This->authority_start > -1) { - if(This->port_offset > -1 && is_default_port(This->scheme_type, This->port) && - This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH) - /* Don't include the port in the authority component. */ - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->port_offset); - else - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->authority_start, This->authority_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_DISPLAY_URI: - /* The Display URI contains everything except for the userinfo for known - * scheme types. - */ - if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) { - unsigned int length = This->canon_len-This->userinfo_len; - - /* Skip fragment if Uri_DISPLAY_NO_FRAGMENT is specified */ - if(dwFlags == Uri_DISPLAY_NO_FRAGMENT && This->fragment_start > -1) - length -= This->fragment_len; - - *pbstrProperty = SysAllocStringLen(NULL, length); - - if(*pbstrProperty) { - /* Copy everything before the userinfo over. */ - memcpy(*pbstrProperty, This->canon_uri, This->userinfo_start*sizeof(WCHAR)); - - /* Copy everything after the userinfo over. */ - length -= This->userinfo_start+1; - memcpy(*pbstrProperty+This->userinfo_start, - This->canon_uri+This->userinfo_start+This->userinfo_len+1, length*sizeof(WCHAR)); - } - } else { - unsigned int length = This->canon_len; - - /* Skip fragment if Uri_DISPLAY_NO_FRAGMENT is specified */ - if(dwFlags == Uri_DISPLAY_NO_FRAGMENT && This->fragment_start > -1) - length -= This->fragment_len; - - *pbstrProperty = SysAllocStringLen(This->canon_uri, length); - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - else - hres = S_OK; - - break; - case Uri_PROPERTY_DOMAIN: - if(This->domain_offset > -1) { - if(dwFlags == Uri_PUNYCODE_IDN_HOST && This->host_type == Uri_HOST_IDN) { - unsigned int punycode_length; - - punycode_length = IdnToAscii(0, This->canon_uri+This->host_start+This->domain_offset, - This->host_len-This->domain_offset, NULL, 0); - *pbstrProperty = SysAllocStringLen(NULL, punycode_length); - if (*pbstrProperty) - IdnToAscii(0, This->canon_uri+This->host_start+This->domain_offset, - This->host_len-This->domain_offset, *pbstrProperty, punycode_length); - } else { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+This->domain_offset, - This->host_len-This->domain_offset); - } - - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_EXTENSION: - if(This->extension_offset > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start+This->extension_offset, - This->path_len-This->extension_offset); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_FRAGMENT: - if(This->fragment_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->fragment_start, This->fragment_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_HOST: - if(This->host_start > -1) { - /* The '[' and ']' aren't included for IPv6 addresses. */ - if(This->host_type == Uri_HOST_IPV6) - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start+1, This->host_len-2); - else if(dwFlags == Uri_PUNYCODE_IDN_HOST && This->host_type == Uri_HOST_IDN) { - unsigned int punycode_length; - - punycode_length = IdnToAscii(0, This->canon_uri+This->host_start, This->host_len, NULL, 0); - *pbstrProperty = SysAllocStringLen(NULL, punycode_length); - if (*pbstrProperty) - IdnToAscii(0, This->canon_uri+This->host_start, This->host_len, *pbstrProperty, punycode_length); - } - else - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->host_start, This->host_len); - - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_PASSWORD: - if(This->userinfo_split > -1) { - *pbstrProperty = SysAllocStringLen( - This->canon_uri+This->userinfo_start+This->userinfo_split+1, - This->userinfo_len-This->userinfo_split-1); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - return E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_PATH: - if(This->path_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_PATH_AND_QUERY: - if(This->path_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->path_start, This->path_len+This->query_len); - hres = S_OK; - } else if(This->query_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_QUERY: - if(This->query_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->query_start, This->query_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_RAW_URI: - *pbstrProperty = SysAllocString(This->raw_uri); - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - else - hres = S_OK; - break; - case Uri_PROPERTY_SCHEME_NAME: - if(This->scheme_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri + This->scheme_start, This->scheme_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_USER_INFO: - if(This->userinfo_start > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri+This->userinfo_start, This->userinfo_len); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - hres = E_OUTOFMEMORY; - - break; - case Uri_PROPERTY_USER_NAME: - if(This->userinfo_start > -1 && This->userinfo_split != 0) { - /* If userinfo_split is set, that means a password exists - * so the username is only from userinfo_start to userinfo_split. - */ - if(This->userinfo_split > -1) { - *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_split); - hres = S_OK; - } else { - *pbstrProperty = SysAllocStringLen(This->canon_uri + This->userinfo_start, This->userinfo_len); - hres = S_OK; - } - } else { - *pbstrProperty = SysAllocStringLen(NULL, 0); - hres = S_FALSE; - } - - if(!(*pbstrProperty)) - return E_OUTOFMEMORY; - - break; - default: - FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pbstrProperty, dwFlags); - hres = E_NOTIMPL; - } - - return hres; -} - -static HRESULT WINAPI Uri_GetPropertyLength(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags) -{ - Uri *This = impl_from_IUri(iface); - HRESULT hres; - TRACE("(%p %s)->(%d %p %lx)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags); - - if(!This->create_flags) - return E_UNEXPECTED; - if(!pcchProperty) - return E_INVALIDARG; - - /* Can only return a length for a property if it's a string. */ - if(uriProp > Uri_PROPERTY_STRING_LAST) - return E_INVALIDARG; - - if(dwFlags != 0 && dwFlags != Uri_DISPLAY_NO_FRAGMENT && dwFlags != Uri_PUNYCODE_IDN_HOST - && dwFlags != Uri_DISPLAY_IDN_HOST) { - *pcchProperty = 0; - return E_INVALIDARG; - } - - switch(uriProp) { - case Uri_PROPERTY_ABSOLUTE_URI: - if(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI) { - *pcchProperty = 0; - hres = S_FALSE; - } - /* Uri_PUNYCODE_IDN_HOST doesn't remove user info containing only "@" and ":@" */ - else if(dwFlags == Uri_PUNYCODE_IDN_HOST && This->host_type == Uri_HOST_IDN && This->host_start > -1) { - unsigned int punycode_host_len = IdnToAscii(0, This->canon_uri+This->host_start, This->host_len, NULL, 0); - *pcchProperty = This->canon_len - This->host_len + punycode_host_len; - hres = S_OK; - } else { - if(This->scheme_type != URL_SCHEME_UNKNOWN) { - if(This->userinfo_start > -1 && This->userinfo_len == 0) - /* Don't include the '@' in the length. */ - *pcchProperty = This->canon_len-1; - else if(This->userinfo_start > -1 && This->userinfo_len == 1 && - This->userinfo_split == 0) - /* Don't include the ":@" in the length. */ - *pcchProperty = This->canon_len-2; - else - *pcchProperty = This->canon_len; - } else - *pcchProperty = This->canon_len; - - hres = S_OK; - } - - break; - case Uri_PROPERTY_AUTHORITY: - if(This->port_offset > -1 && - This->display_modifiers & URI_DISPLAY_NO_DEFAULT_PORT_AUTH && - is_default_port(This->scheme_type, This->port)) - /* Only count up until the port in the authority. */ - *pcchProperty = This->port_offset; - else - *pcchProperty = This->authority_len; - hres = (This->authority_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_DISPLAY_URI: - if(This->scheme_type != URL_SCHEME_UNKNOWN && This->userinfo_start > -1) - *pcchProperty = This->canon_len-This->userinfo_len-1; - else - *pcchProperty = This->canon_len; - - if(dwFlags == Uri_DISPLAY_NO_FRAGMENT && This->fragment_start > -1) - *pcchProperty -= This->fragment_len; - - hres = S_OK; - break; - case Uri_PROPERTY_DOMAIN: - if(This->domain_offset > -1) { - if(dwFlags == Uri_PUNYCODE_IDN_HOST && This->host_type == Uri_HOST_IDN) - *pcchProperty = IdnToAscii(0, This->canon_uri+This->host_start+This->domain_offset, This->host_len-This->domain_offset, NULL, 0); - else - *pcchProperty = This->host_len - This->domain_offset; - } - else - *pcchProperty = 0; - - hres = (This->domain_offset > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_EXTENSION: - if(This->extension_offset > -1) { - *pcchProperty = This->path_len - This->extension_offset; - hres = S_OK; - } else { - *pcchProperty = 0; - hres = S_FALSE; - } - - break; - case Uri_PROPERTY_FRAGMENT: - *pcchProperty = This->fragment_len; - hres = (This->fragment_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_HOST: - *pcchProperty = This->host_len; - - /* '[' and ']' aren't included in the length. */ - if(This->host_type == Uri_HOST_IPV6) - *pcchProperty -= 2; - else if(dwFlags == Uri_PUNYCODE_IDN_HOST && This->host_type == Uri_HOST_IDN && This->host_start > -1) - *pcchProperty = IdnToAscii(0, This->canon_uri+This->host_start, This->host_len, NULL, 0); - - hres = (This->host_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_PASSWORD: - *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_len-This->userinfo_split-1 : 0; - hres = (This->userinfo_split > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_PATH: - *pcchProperty = This->path_len; - hres = (This->path_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_PATH_AND_QUERY: - *pcchProperty = This->path_len+This->query_len; - hres = (This->path_start > -1 || This->query_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_QUERY: - *pcchProperty = This->query_len; - hres = (This->query_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_RAW_URI: - *pcchProperty = SysStringLen(This->raw_uri); - hres = S_OK; - break; - case Uri_PROPERTY_SCHEME_NAME: - *pcchProperty = This->scheme_len; - hres = (This->scheme_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_USER_INFO: - *pcchProperty = This->userinfo_len; - hres = (This->userinfo_start > -1) ? S_OK : S_FALSE; - break; - case Uri_PROPERTY_USER_NAME: - *pcchProperty = (This->userinfo_split > -1) ? This->userinfo_split : This->userinfo_len; - if(This->userinfo_split == 0) - hres = S_FALSE; - else - hres = (This->userinfo_start > -1) ? S_OK : S_FALSE; - break; - default: - FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pcchProperty, dwFlags); - hres = E_NOTIMPL; - } - - if(hres == S_OK - && ((dwFlags == Uri_DISPLAY_NO_FRAGMENT && uriProp != Uri_PROPERTY_DISPLAY_URI) - || (dwFlags == Uri_PUNYCODE_IDN_HOST && uriProp != Uri_PROPERTY_ABSOLUTE_URI - && uriProp != Uri_PROPERTY_DOMAIN && uriProp != Uri_PROPERTY_HOST) - || (dwFlags == Uri_DISPLAY_IDN_HOST && uriProp != Uri_PROPERTY_ABSOLUTE_URI - && uriProp != Uri_PROPERTY_DOMAIN && uriProp != Uri_PROPERTY_HOST))) { - *pcchProperty = 0; - hres = E_INVALIDARG; - } - - return hres; -} - -static HRESULT WINAPI Uri_GetPropertyDWORD(IUri *iface, Uri_PROPERTY uriProp, DWORD *pcchProperty, DWORD dwFlags) -{ - Uri *This = impl_from_IUri(iface); - HRESULT hres; - - TRACE("(%p %s)->(%d %p %lx)\n", This, debugstr_w(This->canon_uri), uriProp, pcchProperty, dwFlags); - - if(!This->create_flags) - return E_UNEXPECTED; - if(!pcchProperty) - return E_INVALIDARG; - - /* Microsoft's implementation for the ZONE property of a URI seems to be lacking... - * From what I can tell, instead of checking which URLZONE the URI belongs to it - * simply assigns URLZONE_INVALID and returns E_NOTIMPL. This also applies to the GetZone - * function. - */ - if(uriProp == Uri_PROPERTY_ZONE) { - *pcchProperty = URLZONE_INVALID; - return E_NOTIMPL; - } - - if(uriProp < Uri_PROPERTY_DWORD_START) { - *pcchProperty = 0; - return E_INVALIDARG; - } - - switch(uriProp) { - case Uri_PROPERTY_HOST_TYPE: - *pcchProperty = This->host_type; - hres = S_OK; - break; - case Uri_PROPERTY_PORT: - if(!This->has_port) { - *pcchProperty = 0; - hres = S_FALSE; - } else { - *pcchProperty = This->port; - hres = S_OK; - } - - break; - case Uri_PROPERTY_SCHEME: - *pcchProperty = This->scheme_type; - hres = S_OK; - break; - default: - FIXME("(%p)->(%d %p %lx)\n", This, uriProp, pcchProperty, dwFlags); - hres = E_NOTIMPL; - } - - return hres; -} - -static HRESULT WINAPI Uri_HasProperty(IUri *iface, Uri_PROPERTY uriProp, BOOL *pfHasProperty) -{ - Uri *This = impl_from_IUri(iface); - - TRACE("(%p %s)->(%d %p)\n", This, debugstr_w(This->canon_uri), uriProp, pfHasProperty); - - if(!pfHasProperty) - return E_INVALIDARG; - - switch(uriProp) { - case Uri_PROPERTY_ABSOLUTE_URI: - *pfHasProperty = !(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI); - break; - case Uri_PROPERTY_AUTHORITY: - *pfHasProperty = This->authority_start > -1; - break; - case Uri_PROPERTY_DISPLAY_URI: - *pfHasProperty = TRUE; - break; - case Uri_PROPERTY_DOMAIN: - *pfHasProperty = This->domain_offset > -1; - break; - case Uri_PROPERTY_EXTENSION: - *pfHasProperty = This->extension_offset > -1; - break; - case Uri_PROPERTY_FRAGMENT: - *pfHasProperty = This->fragment_start > -1; - break; - case Uri_PROPERTY_HOST: - *pfHasProperty = This->host_start > -1; - break; - case Uri_PROPERTY_PASSWORD: - *pfHasProperty = This->userinfo_split > -1; - break; - case Uri_PROPERTY_PATH: - *pfHasProperty = This->path_start > -1; - break; - case Uri_PROPERTY_PATH_AND_QUERY: - *pfHasProperty = (This->path_start > -1 || This->query_start > -1); - break; - case Uri_PROPERTY_QUERY: - *pfHasProperty = This->query_start > -1; - break; - case Uri_PROPERTY_RAW_URI: - *pfHasProperty = TRUE; - break; - case Uri_PROPERTY_SCHEME_NAME: - *pfHasProperty = This->scheme_start > -1; - break; - case Uri_PROPERTY_USER_INFO: - *pfHasProperty = This->userinfo_start > -1; - break; - case Uri_PROPERTY_USER_NAME: - if(This->userinfo_split == 0) - *pfHasProperty = FALSE; - else - *pfHasProperty = This->userinfo_start > -1; - break; - case Uri_PROPERTY_HOST_TYPE: - *pfHasProperty = TRUE; - break; - case Uri_PROPERTY_PORT: - *pfHasProperty = This->has_port; - break; - case Uri_PROPERTY_SCHEME: - *pfHasProperty = TRUE; - break; - case Uri_PROPERTY_ZONE: - *pfHasProperty = FALSE; - break; - default: - FIXME("(%p)->(%d %p): Unsupported property type.\n", This, uriProp, pfHasProperty); - return E_NOTIMPL; - } - - return S_OK; -} - -static HRESULT WINAPI Uri_GetAbsoluteUri(IUri *iface, BSTR *pstrAbsoluteUri) -{ - TRACE("(%p)->(%p)\n", iface, pstrAbsoluteUri); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_ABSOLUTE_URI, pstrAbsoluteUri, 0); -} - -static HRESULT WINAPI Uri_GetAuthority(IUri *iface, BSTR *pstrAuthority) -{ - TRACE("(%p)->(%p)\n", iface, pstrAuthority); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_AUTHORITY, pstrAuthority, 0); -} - -static HRESULT WINAPI Uri_GetDisplayUri(IUri *iface, BSTR *pstrDisplayUri) -{ - TRACE("(%p)->(%p)\n", iface, pstrDisplayUri); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DISPLAY_URI, pstrDisplayUri, 0); -} - -static HRESULT WINAPI Uri_GetDomain(IUri *iface, BSTR *pstrDomain) -{ - TRACE("(%p)->(%p)\n", iface, pstrDomain); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_DOMAIN, pstrDomain, 0); -} - -static HRESULT WINAPI Uri_GetExtension(IUri *iface, BSTR *pstrExtension) -{ - TRACE("(%p)->(%p)\n", iface, pstrExtension); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_EXTENSION, pstrExtension, 0); -} - -static HRESULT WINAPI Uri_GetFragment(IUri *iface, BSTR *pstrFragment) -{ - TRACE("(%p)->(%p)\n", iface, pstrFragment); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_FRAGMENT, pstrFragment, 0); -} - -static HRESULT WINAPI Uri_GetHost(IUri *iface, BSTR *pstrHost) -{ - TRACE("(%p)->(%p)\n", iface, pstrHost); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_HOST, pstrHost, 0); -} - -static HRESULT WINAPI Uri_GetPassword(IUri *iface, BSTR *pstrPassword) -{ - TRACE("(%p)->(%p)\n", iface, pstrPassword); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PASSWORD, pstrPassword, 0); -} - -static HRESULT WINAPI Uri_GetPath(IUri *iface, BSTR *pstrPath) -{ - TRACE("(%p)->(%p)\n", iface, pstrPath); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH, pstrPath, 0); -} - -static HRESULT WINAPI Uri_GetPathAndQuery(IUri *iface, BSTR *pstrPathAndQuery) -{ - TRACE("(%p)->(%p)\n", iface, pstrPathAndQuery); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_PATH_AND_QUERY, pstrPathAndQuery, 0); -} - -static HRESULT WINAPI Uri_GetQuery(IUri *iface, BSTR *pstrQuery) -{ - TRACE("(%p)->(%p)\n", iface, pstrQuery); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_QUERY, pstrQuery, 0); -} - -static HRESULT WINAPI Uri_GetRawUri(IUri *iface, BSTR *pstrRawUri) -{ - TRACE("(%p)->(%p)\n", iface, pstrRawUri); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_RAW_URI, pstrRawUri, 0); -} - -static HRESULT WINAPI Uri_GetSchemeName(IUri *iface, BSTR *pstrSchemeName) -{ - TRACE("(%p)->(%p)\n", iface, pstrSchemeName); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_SCHEME_NAME, pstrSchemeName, 0); -} - -static HRESULT WINAPI Uri_GetUserInfo(IUri *iface, BSTR *pstrUserInfo) -{ - TRACE("(%p)->(%p)\n", iface, pstrUserInfo); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_INFO, pstrUserInfo, 0); -} - -static HRESULT WINAPI Uri_GetUserName(IUri *iface, BSTR *pstrUserName) -{ - TRACE("(%p)->(%p)\n", iface, pstrUserName); - return IUri_GetPropertyBSTR(iface, Uri_PROPERTY_USER_NAME, pstrUserName, 0); -} - -static HRESULT WINAPI Uri_GetHostType(IUri *iface, DWORD *pdwHostType) -{ - TRACE("(%p)->(%p)\n", iface, pdwHostType); - return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_HOST_TYPE, pdwHostType, 0); -} - -static HRESULT WINAPI Uri_GetPort(IUri *iface, DWORD *pdwPort) -{ - TRACE("(%p)->(%p)\n", iface, pdwPort); - return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_PORT, pdwPort, 0); -} - -static HRESULT WINAPI Uri_GetScheme(IUri *iface, DWORD *pdwScheme) -{ - TRACE("(%p)->(%p)\n", iface, pdwScheme); - return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_SCHEME, pdwScheme, 0); -} - -static HRESULT WINAPI Uri_GetZone(IUri *iface, DWORD *pdwZone) -{ - TRACE("(%p)->(%p)\n", iface, pdwZone); - return IUri_GetPropertyDWORD(iface, Uri_PROPERTY_ZONE,pdwZone, 0); -} - -static HRESULT WINAPI Uri_GetProperties(IUri *iface, DWORD *pdwProperties) -{ - Uri *This = impl_from_IUri(iface); - TRACE("(%p %s)->(%p)\n", This, debugstr_w(This->canon_uri), pdwProperties); - - if(!This->create_flags) - return E_UNEXPECTED; - if(!pdwProperties) - return E_INVALIDARG; - - /* All URIs have these. */ - *pdwProperties = Uri_HAS_DISPLAY_URI|Uri_HAS_RAW_URI|Uri_HAS_SCHEME|Uri_HAS_HOST_TYPE; - - if(!(This->display_modifiers & URI_DISPLAY_NO_ABSOLUTE_URI)) - *pdwProperties |= Uri_HAS_ABSOLUTE_URI; - - if(This->scheme_start > -1) - *pdwProperties |= Uri_HAS_SCHEME_NAME; - - if(This->authority_start > -1) { - *pdwProperties |= Uri_HAS_AUTHORITY; - if(This->userinfo_start > -1) { - *pdwProperties |= Uri_HAS_USER_INFO; - if(This->userinfo_split != 0) - *pdwProperties |= Uri_HAS_USER_NAME; - } - if(This->userinfo_split > -1) - *pdwProperties |= Uri_HAS_PASSWORD; - if(This->host_start > -1) - *pdwProperties |= Uri_HAS_HOST; - if(This->domain_offset > -1) - *pdwProperties |= Uri_HAS_DOMAIN; - } - - if(This->has_port) - *pdwProperties |= Uri_HAS_PORT; - if(This->path_start > -1) - *pdwProperties |= Uri_HAS_PATH|Uri_HAS_PATH_AND_QUERY; - if(This->query_start > -1) - *pdwProperties |= Uri_HAS_QUERY|Uri_HAS_PATH_AND_QUERY; - - if(This->extension_offset > -1) - *pdwProperties |= Uri_HAS_EXTENSION; - - if(This->fragment_start > -1) - *pdwProperties |= Uri_HAS_FRAGMENT; - - return S_OK; -} - -static HRESULT WINAPI Uri_IsEqual(IUri *iface, IUri *pUri, BOOL *pfEqual) -{ - Uri *This = impl_from_IUri(iface); - Uri *other; - - TRACE("(%p %s)->(%p %p)\n", This, debugstr_w(This->canon_uri), pUri, pfEqual); - - if(!This->create_flags) - return E_UNEXPECTED; - if(!pfEqual) - return E_POINTER; - - if(!pUri) { - *pfEqual = FALSE; - - /* For some reason Windows returns S_OK here... */ - return S_OK; - } - - /* Try to convert it to a Uri (allows for a more simple comparison). */ - if(!(other = get_uri_obj(pUri))) { - FIXME("(%p)->(%p %p) No support for unknown IUri's yet.\n", iface, pUri, pfEqual); - return E_NOTIMPL; - } - - TRACE("comparing to %s\n", debugstr_w(other->canon_uri)); - return compare_uris(This, other, pfEqual); -} - -static const IUriVtbl UriVtbl = { - Uri_QueryInterface, - Uri_AddRef, - Uri_Release, - Uri_GetPropertyBSTR, - Uri_GetPropertyLength, - Uri_GetPropertyDWORD, - Uri_HasProperty, - Uri_GetAbsoluteUri, - Uri_GetAuthority, - Uri_GetDisplayUri, - Uri_GetDomain, - Uri_GetExtension, - Uri_GetFragment, - Uri_GetHost, - Uri_GetPassword, - Uri_GetPath, - Uri_GetPathAndQuery, - Uri_GetQuery, - Uri_GetRawUri, - Uri_GetSchemeName, - Uri_GetUserInfo, - Uri_GetUserName, - Uri_GetHostType, - Uri_GetPort, - Uri_GetScheme, - Uri_GetZone, - Uri_GetProperties, - Uri_IsEqual -}; - -static inline Uri* impl_from_IUriBuilderFactory(IUriBuilderFactory *iface) -{ - return CONTAINING_RECORD(iface, Uri, IUriBuilderFactory_iface); -} - -static HRESULT WINAPI UriBuilderFactory_QueryInterface(IUriBuilderFactory *iface, REFIID riid, void **ppv) -{ - Uri *This = impl_from_IUriBuilderFactory(iface); - return IUri_QueryInterface(&This->IUri_iface, riid, ppv); -} - -static ULONG WINAPI UriBuilderFactory_AddRef(IUriBuilderFactory *iface) -{ - Uri *This = impl_from_IUriBuilderFactory(iface); - return IUri_AddRef(&This->IUri_iface); -} - -static ULONG WINAPI UriBuilderFactory_Release(IUriBuilderFactory *iface) -{ - Uri *This = impl_from_IUriBuilderFactory(iface); - return IUri_Release(&This->IUri_iface); -} - -static HRESULT WINAPI UriBuilderFactory_CreateIUriBuilder(IUriBuilderFactory *iface, - DWORD dwFlags, - DWORD_PTR dwReserved, - IUriBuilder **ppIUriBuilder) -{ - Uri *This = impl_from_IUriBuilderFactory(iface); - TRACE("(%p)->(%08lx %08Ix %p)\n", This, dwFlags, dwReserved, ppIUriBuilder); - - if(!ppIUriBuilder) - return E_POINTER; - - if(dwFlags || dwReserved) { - *ppIUriBuilder = NULL; - return E_INVALIDARG; - } - - return CreateIUriBuilder(NULL, 0, 0, ppIUriBuilder); -} - -static HRESULT WINAPI UriBuilderFactory_CreateInitializedIUriBuilder(IUriBuilderFactory *iface, - DWORD dwFlags, - DWORD_PTR dwReserved, - IUriBuilder **ppIUriBuilder) -{ - Uri *This = impl_from_IUriBuilderFactory(iface); - TRACE("(%p)->(%08lx %08Ix %p)\n", This, dwFlags, dwReserved, ppIUriBuilder); - - if(!ppIUriBuilder) - return E_POINTER; - - if(dwFlags || dwReserved) { - *ppIUriBuilder = NULL; - return E_INVALIDARG; - } - - return CreateIUriBuilder(&This->IUri_iface, 0, 0, ppIUriBuilder); -} - -static const IUriBuilderFactoryVtbl UriBuilderFactoryVtbl = { - UriBuilderFactory_QueryInterface, - UriBuilderFactory_AddRef, - UriBuilderFactory_Release, - UriBuilderFactory_CreateIUriBuilder, - UriBuilderFactory_CreateInitializedIUriBuilder -}; - -static inline Uri* impl_from_IPersistStream(IPersistStream *iface) -{ - return CONTAINING_RECORD(iface, Uri, IPersistStream_iface); -} - -static HRESULT WINAPI PersistStream_QueryInterface(IPersistStream *iface, REFIID riid, void **ppvObject) -{ - Uri *This = impl_from_IPersistStream(iface); - return IUri_QueryInterface(&This->IUri_iface, riid, ppvObject); -} - -static ULONG WINAPI PersistStream_AddRef(IPersistStream *iface) -{ - Uri *This = impl_from_IPersistStream(iface); - return IUri_AddRef(&This->IUri_iface); -} - -static ULONG WINAPI PersistStream_Release(IPersistStream *iface) -{ - Uri *This = impl_from_IPersistStream(iface); - return IUri_Release(&This->IUri_iface); -} - -static HRESULT WINAPI PersistStream_GetClassID(IPersistStream *iface, CLSID *pClassID) -{ - Uri *This = impl_from_IPersistStream(iface); - TRACE("(%p)->(%p)\n", This, pClassID); - - if(!pClassID) - return E_INVALIDARG; - - *pClassID = CLSID_CUri; - return S_OK; -} - -static HRESULT WINAPI PersistStream_IsDirty(IPersistStream *iface) -{ - Uri *This = impl_from_IPersistStream(iface); - TRACE("(%p)\n", This); - return S_FALSE; -} - -struct persist_uri { - DWORD size; - DWORD unk1[2]; - DWORD create_flags; - DWORD unk2[3]; - DWORD fields_no; - BYTE data[1]; -}; - -static HRESULT WINAPI PersistStream_Load(IPersistStream *iface, IStream *pStm) -{ - Uri *This = impl_from_IPersistStream(iface); - struct persist_uri *data; - parse_data parse; - DWORD size; - HRESULT hr; - - TRACE("(%p)->(%p)\n", This, pStm); - - if(This->create_flags) - return E_UNEXPECTED; - if(!pStm) - return E_INVALIDARG; - - hr = IStream_Read(pStm, &size, sizeof(DWORD), NULL); - if(FAILED(hr)) - return hr; - data = malloc(size); - if(!data) - return E_OUTOFMEMORY; - hr = IStream_Read(pStm, data->unk1, size-sizeof(DWORD)-2, NULL); - if(FAILED(hr)) { - free(data); - return hr; - } - - if(size < sizeof(struct persist_uri)) { - free(data); - return S_OK; - } - - if(*(DWORD*)data->data != Uri_PROPERTY_RAW_URI) { - free(data); - ERR("Can't find raw_uri\n"); - return E_UNEXPECTED; - } - - This->raw_uri = SysAllocString((WCHAR*)(data->data+sizeof(DWORD)*2)); - if(!This->raw_uri) { - free(data); - return E_OUTOFMEMORY; - } - This->create_flags = data->create_flags; - free(data); - TRACE("%lx %s\n", This->create_flags, debugstr_w(This->raw_uri)); - - memset(&parse, 0, sizeof(parse_data)); - parse.uri = This->raw_uri; - if(!parse_uri(&parse, This->create_flags)) { - SysFreeString(This->raw_uri); - This->create_flags = 0; - return E_UNEXPECTED; - } - - hr = canonicalize_uri(&parse, This, This->create_flags); - if(FAILED(hr)) { - SysFreeString(This->raw_uri); - This->create_flags = 0; - return hr; - } - - return S_OK; -} - -static inline BYTE* persist_stream_add_strprop(Uri *This, BYTE *p, DWORD type, DWORD len, WCHAR *data) -{ - len *= sizeof(WCHAR); - *(DWORD*)p = type; - p += sizeof(DWORD); - *(DWORD*)p = len+sizeof(WCHAR); - p += sizeof(DWORD); - memcpy(p, data, len); - p += len; - *(WCHAR*)p = 0; - return p+sizeof(WCHAR); -} - -static inline void persist_stream_save(Uri *This, IStream *pStm, BOOL marshal, struct persist_uri *data) -{ - BYTE *p = NULL; - - data->create_flags = This->create_flags; - - if(This->create_flags) { - data->fields_no = 1; - p = persist_stream_add_strprop(This, data->data, Uri_PROPERTY_RAW_URI, - SysStringLen(This->raw_uri), This->raw_uri); - } - if(This->scheme_type!=URL_SCHEME_HTTP && This->scheme_type!=URL_SCHEME_HTTPS - && This->scheme_type!=URL_SCHEME_FTP) - return; - - if(This->fragment_len) { - data->fields_no++; - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_FRAGMENT, - This->fragment_len, This->canon_uri+This->fragment_start); - } - - if(This->host_len) { - data->fields_no++; - if(This->host_type == Uri_HOST_IPV6) - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_HOST, - This->host_len-2, This->canon_uri+This->host_start+1); - else - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_HOST, - This->host_len, This->canon_uri+This->host_start); - } - - if(This->userinfo_split > -1) { - data->fields_no++; - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PASSWORD, - This->userinfo_len-This->userinfo_split-1, - This->canon_uri+This->userinfo_start+This->userinfo_split+1); - } - - if(This->path_len) { - data->fields_no++; - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PATH, - This->path_len, This->canon_uri+This->path_start); - } else if(marshal) { - WCHAR no_path = '/'; - data->fields_no++; - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_PATH, 1, &no_path); - } - - if(This->has_port) { - data->fields_no++; - *(DWORD*)p = Uri_PROPERTY_PORT; - p += sizeof(DWORD); - *(DWORD*)p = sizeof(DWORD); - p += sizeof(DWORD); - *(DWORD*)p = This->port; - p += sizeof(DWORD); - } - - if(This->query_len) { - data->fields_no++; - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_QUERY, - This->query_len, This->canon_uri+This->query_start); - } - - if(This->scheme_len) { - data->fields_no++; - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_SCHEME_NAME, - This->scheme_len, This->canon_uri+This->scheme_start); - } - - if(This->userinfo_start>-1 && This->userinfo_split!=0) { - data->fields_no++; - if(This->userinfo_split > -1) - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_USER_NAME, - This->userinfo_split, This->canon_uri+This->userinfo_start); - else - p = persist_stream_add_strprop(This, p, Uri_PROPERTY_USER_NAME, - This->userinfo_len, This->canon_uri+This->userinfo_start); - } -} - -static HRESULT WINAPI PersistStream_Save(IPersistStream *iface, IStream *pStm, BOOL fClearDirty) -{ - Uri *This = impl_from_IPersistStream(iface); - struct persist_uri *data; - ULARGE_INTEGER size; - HRESULT hres; - - TRACE("(%p)->(%p %x)\n", This, pStm, fClearDirty); - - if(!pStm) - return E_INVALIDARG; - - hres = IPersistStream_GetSizeMax(&This->IPersistStream_iface, &size); - if(FAILED(hres)) - return hres; - - data = calloc(1, size.u.LowPart); - if(!data) - return E_OUTOFMEMORY; - data->size = size.u.LowPart; - persist_stream_save(This, pStm, FALSE, data); - - hres = IStream_Write(pStm, data, data->size-2, NULL); - free(data); - return hres; -} - -static HRESULT WINAPI PersistStream_GetSizeMax(IPersistStream *iface, ULARGE_INTEGER *pcbSize) -{ - Uri *This = impl_from_IPersistStream(iface); - TRACE("(%p)->(%p)\n", This, pcbSize); - - if(!pcbSize) - return E_INVALIDARG; - - pcbSize->u.LowPart = 2+sizeof(struct persist_uri); - pcbSize->u.HighPart = 0; - if(This->create_flags) - pcbSize->u.LowPart += (SysStringLen(This->raw_uri)+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - else /* there's no place for fields no */ - pcbSize->u.LowPart -= sizeof(DWORD); - if(This->scheme_type!=URL_SCHEME_HTTP && This->scheme_type!=URL_SCHEME_HTTPS - && This->scheme_type!=URL_SCHEME_FTP) - return S_OK; - - if(This->fragment_len) - pcbSize->u.LowPart += (This->fragment_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - if(This->host_len) { - if(This->host_type == Uri_HOST_IPV6) - pcbSize->u.LowPart += (This->host_len-1)*sizeof(WCHAR) + 2*sizeof(DWORD); - else - pcbSize->u.LowPart += (This->host_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - } - if(This->userinfo_split > -1) - pcbSize->u.LowPart += (This->userinfo_len-This->userinfo_split)*sizeof(WCHAR) + 2*sizeof(DWORD); - if(This->path_len) - pcbSize->u.LowPart += (This->path_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - if(This->has_port) - pcbSize->u.LowPart += 3*sizeof(DWORD); - if(This->query_len) - pcbSize->u.LowPart += (This->query_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - if(This->scheme_len) - pcbSize->u.LowPart += (This->scheme_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - if(This->userinfo_start>-1 && This->userinfo_split!=0) { - if(This->userinfo_split > -1) - pcbSize->u.LowPart += (This->userinfo_split+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - else - pcbSize->u.LowPart += (This->userinfo_len+1)*sizeof(WCHAR) + 2*sizeof(DWORD); - } - return S_OK; -} - -static const IPersistStreamVtbl PersistStreamVtbl = { - PersistStream_QueryInterface, - PersistStream_AddRef, - PersistStream_Release, - PersistStream_GetClassID, - PersistStream_IsDirty, - PersistStream_Load, - PersistStream_Save, - PersistStream_GetSizeMax -}; - -static inline Uri* impl_from_IMarshal(IMarshal *iface) -{ - return CONTAINING_RECORD(iface, Uri, IMarshal_iface); -} - -static HRESULT WINAPI Marshal_QueryInterface(IMarshal *iface, REFIID riid, void **ppvObject) -{ - Uri *This = impl_from_IMarshal(iface); - return IUri_QueryInterface(&This->IUri_iface, riid, ppvObject); -} - -static ULONG WINAPI Marshal_AddRef(IMarshal *iface) -{ - Uri *This = impl_from_IMarshal(iface); - return IUri_AddRef(&This->IUri_iface); -} - -static ULONG WINAPI Marshal_Release(IMarshal *iface) -{ - Uri *This = impl_from_IMarshal(iface); - return IUri_Release(&This->IUri_iface); -} - -static HRESULT WINAPI Marshal_GetUnmarshalClass(IMarshal *iface, REFIID riid, void *pv, - DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, CLSID *pCid) -{ - Uri *This = impl_from_IMarshal(iface); - TRACE("(%p)->(%s %p %lx %p %lx %p)\n", This, debugstr_guid(riid), pv, - dwDestContext, pvDestContext, mshlflags, pCid); - - if(!pCid || (dwDestContext!=MSHCTX_LOCAL && dwDestContext!=MSHCTX_NOSHAREDMEM - && dwDestContext!=MSHCTX_INPROC)) - return E_INVALIDARG; - - *pCid = CLSID_CUri; - return S_OK; -} - -struct inproc_marshal_uri { - DWORD size; - DWORD mshlflags; - DWORD unk[4]; /* process identifier? */ - Uri *uri; -}; - -static HRESULT WINAPI Marshal_GetMarshalSizeMax(IMarshal *iface, REFIID riid, void *pv, - DWORD dwDestContext, void *pvDestContext, DWORD mshlflags, DWORD *pSize) -{ - Uri *This = impl_from_IMarshal(iface); - ULARGE_INTEGER size; - HRESULT hres; - TRACE("(%p)->(%s %p %lx %p %lx %p)\n", This, debugstr_guid(riid), pv, - dwDestContext, pvDestContext, mshlflags, pSize); - - if(!pSize || (dwDestContext!=MSHCTX_LOCAL && dwDestContext!=MSHCTX_NOSHAREDMEM - && dwDestContext!=MSHCTX_INPROC)) - return E_INVALIDARG; - - if(dwDestContext == MSHCTX_INPROC) { - *pSize = sizeof(struct inproc_marshal_uri); - return S_OK; - } - - hres = IPersistStream_GetSizeMax(&This->IPersistStream_iface, &size); - if(FAILED(hres)) - return hres; - if(!This->path_len && (This->scheme_type==URL_SCHEME_HTTP - || This->scheme_type==URL_SCHEME_HTTPS - || This->scheme_type==URL_SCHEME_FTP)) - size.u.LowPart += 3*sizeof(DWORD); - *pSize = size.u.LowPart+2*sizeof(DWORD); - return S_OK; -} - -static HRESULT WINAPI Marshal_MarshalInterface(IMarshal *iface, IStream *pStm, REFIID riid, - void *pv, DWORD dwDestContext, void *pvDestContext, DWORD mshlflags) -{ - Uri *This = impl_from_IMarshal(iface); - DWORD *data; - DWORD size; - HRESULT hres; - - TRACE("(%p)->(%p %s %p %lx %p %lx)\n", This, pStm, debugstr_guid(riid), pv, - dwDestContext, pvDestContext, mshlflags); - - if(!pStm || mshlflags!=MSHLFLAGS_NORMAL || (dwDestContext!=MSHCTX_LOCAL - && dwDestContext!=MSHCTX_NOSHAREDMEM && dwDestContext!=MSHCTX_INPROC)) - return E_INVALIDARG; - - if(dwDestContext == MSHCTX_INPROC) { - struct inproc_marshal_uri data; - - data.size = sizeof(data); - data.mshlflags = MSHCTX_INPROC; - data.unk[0] = 0; - data.unk[1] = 0; - data.unk[2] = 0; - data.unk[3] = 0; - data.uri = This; - - hres = IStream_Write(pStm, &data, data.size, NULL); - if(FAILED(hres)) - return hres; - - IUri_AddRef(&This->IUri_iface); - return S_OK; - } - - hres = IMarshal_GetMarshalSizeMax(iface, riid, pv, dwDestContext, - pvDestContext, mshlflags, &size); - if(FAILED(hres)) - return hres; - - data = calloc(1, size); - if(!data) - return E_OUTOFMEMORY; - - data[0] = size; - data[1] = dwDestContext; - data[2] = size-2*sizeof(DWORD); - persist_stream_save(This, pStm, TRUE, (struct persist_uri*)(data+2)); - - hres = IStream_Write(pStm, data, data[0]-2, NULL); - free(data); - return hres; -} - -static HRESULT WINAPI Marshal_UnmarshalInterface(IMarshal *iface, - IStream *pStm, REFIID riid, void **ppv) -{ - Uri *This = impl_from_IMarshal(iface); - DWORD header[2]; - HRESULT hres; - - TRACE("(%p)->(%p %s %p)\n", This, pStm, debugstr_guid(riid), ppv); - - if(This->create_flags) - return E_UNEXPECTED; - if(!pStm || !riid || !ppv) - return E_INVALIDARG; - - hres = IStream_Read(pStm, header, sizeof(header), NULL); - if(FAILED(hres)) - return hres; - - if(header[1]!=MSHCTX_LOCAL && header[1]!=MSHCTX_NOSHAREDMEM - && header[1]!=MSHCTX_INPROC) - return E_UNEXPECTED; - - if(header[1] == MSHCTX_INPROC) { - struct inproc_marshal_uri data; - parse_data parse; - - hres = IStream_Read(pStm, data.unk, sizeof(data)-2*sizeof(DWORD), NULL); - if(FAILED(hres)) - return hres; - - This->raw_uri = SysAllocString(data.uri->raw_uri); - if(!This->raw_uri) { - return E_OUTOFMEMORY; - } - - memset(&parse, 0, sizeof(parse_data)); - parse.uri = This->raw_uri; - - if(!parse_uri(&parse, data.uri->create_flags)) - return E_INVALIDARG; - - hres = canonicalize_uri(&parse, This, data.uri->create_flags); - if(FAILED(hres)) - return hres; - - This->create_flags = data.uri->create_flags; - IUri_Release(&data.uri->IUri_iface); - - return IUri_QueryInterface(&This->IUri_iface, riid, ppv); - } - - hres = IPersistStream_Load(&This->IPersistStream_iface, pStm); - if(FAILED(hres)) - return hres; - - return IUri_QueryInterface(&This->IUri_iface, riid, ppv); -} - -static HRESULT WINAPI Marshal_ReleaseMarshalData(IMarshal *iface, IStream *pStm) -{ - Uri *This = impl_from_IMarshal(iface); - LARGE_INTEGER off; - DWORD header[2]; - HRESULT hres; - - TRACE("(%p)->(%p)\n", This, pStm); - - if(!pStm) - return E_INVALIDARG; - - hres = IStream_Read(pStm, header, 2*sizeof(DWORD), NULL); - if(FAILED(hres)) - return hres; - - if(header[1] == MSHCTX_INPROC) { - struct inproc_marshal_uri data; - - hres = IStream_Read(pStm, data.unk, sizeof(data)-2*sizeof(DWORD), NULL); - if(FAILED(hres)) - return hres; - - IUri_Release(&data.uri->IUri_iface); - return S_OK; - } - - off.u.LowPart = header[0]-sizeof(header)-2; - off.u.HighPart = 0; - return IStream_Seek(pStm, off, STREAM_SEEK_CUR, NULL); -} - -static HRESULT WINAPI Marshal_DisconnectObject(IMarshal *iface, DWORD dwReserved) -{ - Uri *This = impl_from_IMarshal(iface); - TRACE("(%p)->(%lx)\n", This, dwReserved); - return S_OK; -} - -static const IMarshalVtbl MarshalVtbl = { - Marshal_QueryInterface, - Marshal_AddRef, - Marshal_Release, - Marshal_GetUnmarshalClass, - Marshal_GetMarshalSizeMax, - Marshal_MarshalInterface, - Marshal_UnmarshalInterface, - Marshal_ReleaseMarshalData, - Marshal_DisconnectObject -}; - -HRESULT Uri_Construct(IUnknown *pUnkOuter, LPVOID *ppobj) -{ - Uri *ret = calloc(1, sizeof(Uri)); - - TRACE("(%p %p)\n", pUnkOuter, ppobj); - - *ppobj = ret; - if(!ret) - return E_OUTOFMEMORY; - - ret->IUri_iface.lpVtbl = &UriVtbl; - ret->IUriBuilderFactory_iface.lpVtbl = &UriBuilderFactoryVtbl; - ret->IPersistStream_iface.lpVtbl = &PersistStreamVtbl; - ret->IMarshal_iface.lpVtbl = &MarshalVtbl; - ret->ref = 1; - - *ppobj = &ret->IUri_iface; - return S_OK; -} - /*********************************************************************** * CoInternetCombineIUri (urlmon.@) */ diff --git a/dlls/urlmon/urlmon_main.c b/dlls/urlmon/urlmon_main.c index 806a893d7d1..6901dc87771 100644 --- a/dlls/urlmon/urlmon_main.c +++ b/dlls/urlmon/urlmon_main.c @@ -35,8 +35,6 @@ WINE_DEFAULT_DEBUG_CHANNEL(urlmon); -DEFINE_GUID(CLSID_CUri, 0xDF2FCE13, 0x25EC, 0x45BB, 0x9D,0x4C, 0xCE,0xCD,0x47,0xC2,0x43,0x0C); - LONG URLMON_refCount = 0; HINSTANCE urlmon_instance; @@ -373,8 +371,6 @@ static ClassFactory StdURLMonikerCF = { { &ClassFactoryVtbl }, StdURLMoniker_Construct}; static ClassFactory MimeFilterCF = { { &ClassFactoryVtbl }, MimeFilter_Construct}; -static ClassFactory CUriCF = - { { &ClassFactoryVtbl }, Uri_Construct}; struct object_creation_info { @@ -396,7 +392,6 @@ static const struct object_creation_info object_creation[] = { &CLSID_InternetZoneManager, &ZoneManagerCF.IClassFactory_iface, NULL }, { &CLSID_StdURLMoniker, &StdURLMonikerCF.IClassFactory_iface, NULL }, { &CLSID_DeCompMimeFilter, &MimeFilterCF.IClassFactory_iface, NULL }, - { &CLSID_CUri, &CUriCF.IClassFactory_iface, NULL } }; static void init_session(void) diff --git a/dlls/urlmon/urlmon_main.h b/dlls/urlmon/urlmon_main.h index ceb3e09b761..20e5c82390e 100644 --- a/dlls/urlmon/urlmon_main.h +++ b/dlls/urlmon/urlmon_main.h @@ -45,7 +45,6 @@ extern HRESULT FtpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj); extern HRESULT GopherProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj); extern HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj); extern HRESULT MimeFilter_Construct(IUnknown *pUnkOuter, LPVOID *ppobj); -extern HRESULT Uri_Construct(IUnknown *pUnkOuter, LPVOID *ppobj); extern BOOL WINAPI URLMON_DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); extern HRESULT WINAPI URLMON_DllGetClassObject(REFCLSID rclsid, REFIID iid,LPVOID *ppv); @@ -53,7 +52,6 @@ extern HRESULT WINAPI URLMON_DllRegisterServer(void); extern HRESULT WINAPI URLMON_DllUnregisterServer(void); extern GUID const CLSID_PSFactoryBuffer; -extern GUID const CLSID_CUri; /********************************************************************** * Dll lifetime tracking declaration for urlmon.dll diff --git a/dlls/urlmon/urlmon_urlmon.idl b/dlls/urlmon/urlmon_urlmon.idl index d557708c6ca..cf4b4f76a70 100644 --- a/dlls/urlmon/urlmon_urlmon.idl +++ b/dlls/urlmon/urlmon_urlmon.idl @@ -105,13 +105,6 @@ coclass PSFactoryBuffer { interface IPSFactoryBuffer; } ] coclass DeCompMimeFilter { interface IInternetProtocol; interface IInternetProtocolSink; } -[ - helpstring("CUri"), - threading(apartment), - uuid(df2fce13-25ec-45bb-9d4c-cecd47c2430c) -] -coclass CUri { interface IUri; } - [ helpstring("Persistent Zone Identifier"), threading(both), -- GitLab https://gitlab.winehq.org/wine/wine/-/merge_requests/9131