source: trunk/src/shlwapi/url.c@ 21819

Last change on this file since 21819 was 21819, checked in by abwillis, 14 years ago

Ticket #51 Add SHAutoComplete stub.

File size: 47.2 KB
Line 
1/*
2 * Url functions
3 *
4 * Copyright 2000 Huw D M Davies for CodeWeavers.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <string.h>
22#include <stdlib.h>
23#include "windef.h"
24#include "winnls.h"
25#include "winbase.h"
26#include "winerror.h"
27#include "wine/unicode.h"
28#include "wininet.h"
29#include "winreg.h"
30#define NO_SHLWAPI_STREAM
31#include "shlwapi.h"
32#include "wine/debug.h"
33#include "ordinal.h"
34
35WINE_DEFAULT_DEBUG_CHANNEL(shell);
36
37typedef struct {
38 LPCWSTR pScheme; /* [out] start of scheme */
39 DWORD szScheme; /* [out] size of scheme (until colon) */
40 LPCWSTR pUserName; /* [out] start of Username */
41 DWORD szUserName; /* [out] size of Username (until ":" or "@") */
42 LPCWSTR pPassword; /* [out] start of Password */
43 DWORD szPassword; /* [out] size of Password (until "@") */
44 LPCWSTR pHostName; /* [out] start of Hostname */
45 DWORD szHostName; /* [out] size of Hostname (until ":" or "/") */
46 LPCWSTR pPort; /* [out] start of Port */
47 DWORD szPort; /* [out] size of Port (until "/" or eos) */
48 LPCWSTR pQuery; /* [out] start of Query */
49 DWORD szQuery; /* [out] size of Query (until eos) */
50} WINE_PARSE_URL;
51
52typedef enum {
53 SCHEME,
54 HOST,
55 PORT,
56 USERPASS,
57} WINE_URL_SCAN_TYPE;
58
59static const WCHAR fileW[] = {'f','i','l','e','\0'};
60
61static const unsigned char HashDataLookup[256] = {
62 0x01, 0x0E, 0x6E, 0x19, 0x61, 0xAE, 0x84, 0x77, 0x8A, 0xAA, 0x7D, 0x76, 0x1B,
63 0xE9, 0x8C, 0x33, 0x57, 0xC5, 0xB1, 0x6B, 0xEA, 0xA9, 0x38, 0x44, 0x1E, 0x07,
64 0xAD, 0x49, 0xBC, 0x28, 0x24, 0x41, 0x31, 0xD5, 0x68, 0xBE, 0x39, 0xD3, 0x94,
65 0xDF, 0x30, 0x73, 0x0F, 0x02, 0x43, 0xBA, 0xD2, 0x1C, 0x0C, 0xB5, 0x67, 0x46,
66 0x16, 0x3A, 0x4B, 0x4E, 0xB7, 0xA7, 0xEE, 0x9D, 0x7C, 0x93, 0xAC, 0x90, 0xB0,
67 0xA1, 0x8D, 0x56, 0x3C, 0x42, 0x80, 0x53, 0x9C, 0xF1, 0x4F, 0x2E, 0xA8, 0xC6,
68 0x29, 0xFE, 0xB2, 0x55, 0xFD, 0xED, 0xFA, 0x9A, 0x85, 0x58, 0x23, 0xCE, 0x5F,
69 0x74, 0xFC, 0xC0, 0x36, 0xDD, 0x66, 0xDA, 0xFF, 0xF0, 0x52, 0x6A, 0x9E, 0xC9,
70 0x3D, 0x03, 0x59, 0x09, 0x2A, 0x9B, 0x9F, 0x5D, 0xA6, 0x50, 0x32, 0x22, 0xAF,
71 0xC3, 0x64, 0x63, 0x1A, 0x96, 0x10, 0x91, 0x04, 0x21, 0x08, 0xBD, 0x79, 0x40,
72 0x4D, 0x48, 0xD0, 0xF5, 0x82, 0x7A, 0x8F, 0x37, 0x69, 0x86, 0x1D, 0xA4, 0xB9,
73 0xC2, 0xC1, 0xEF, 0x65, 0xF2, 0x05, 0xAB, 0x7E, 0x0B, 0x4A, 0x3B, 0x89, 0xE4,
74 0x6C, 0xBF, 0xE8, 0x8B, 0x06, 0x18, 0x51, 0x14, 0x7F, 0x11, 0x5B, 0x5C, 0xFB,
75 0x97, 0xE1, 0xCF, 0x15, 0x62, 0x71, 0x70, 0x54, 0xE2, 0x12, 0xD6, 0xC7, 0xBB,
76 0x0D, 0x20, 0x5E, 0xDC, 0xE0, 0xD4, 0xF7, 0xCC, 0xC4, 0x2B, 0xF9, 0xEC, 0x2D,
77 0xF4, 0x6F, 0xB6, 0x99, 0x88, 0x81, 0x5A, 0xD9, 0xCA, 0x13, 0xA5, 0xE7, 0x47,
78 0xE6, 0x8E, 0x60, 0xE3, 0x3E, 0xB3, 0xF6, 0x72, 0xA2, 0x35, 0xA0, 0xD7, 0xCD,
79 0xB4, 0x2F, 0x6D, 0x2C, 0x26, 0x1F, 0x95, 0x87, 0x00, 0xD8, 0x34, 0x3F, 0x17,
80 0x25, 0x45, 0x27, 0x75, 0x92, 0xB8, 0xA3, 0xC8, 0xDE, 0xEB, 0xF8, 0xF3, 0xDB,
81 0x0A, 0x98, 0x83, 0x7B, 0xE5, 0xCB, 0x4C, 0x78, 0xD1 };
82
83static BOOL URL_NeedEscapeA(CHAR ch, DWORD dwFlags)
84{
85
86 if (isalnum(ch))
87 return FALSE;
88
89 if(dwFlags & URL_ESCAPE_SPACES_ONLY) {
90 if(ch == ' ')
91 return TRUE;
92 else
93 return FALSE;
94 }
95
96 if ((dwFlags & URL_ESCAPE_PERCENT) && (ch == '%'))
97 return TRUE;
98
99 if (ch <= 31 || ch >= 127)
100 return TRUE;
101
102 else {
103 switch (ch) {
104 case ' ':
105 case '<':
106 case '>':
107 case '\"':
108 case '{':
109 case '}':
110 case '|':
111 case '\\':
112 case '^':
113 case ']':
114 case '[':
115 case '`':
116 case '&':
117 return TRUE;
118
119 case '/':
120 case '?':
121 if (dwFlags & URL_ESCAPE_SEGMENT_ONLY) return TRUE;
122 default:
123 return FALSE;
124 }
125 }
126}
127
128static BOOL URL_NeedEscapeW(WCHAR ch, DWORD dwFlags)
129{
130
131 if (isalnumW(ch))
132 return FALSE;
133
134 if(dwFlags & URL_ESCAPE_SPACES_ONLY) {
135 if(ch == L' ')
136 return TRUE;
137 else
138 return FALSE;
139 }
140
141 if ((dwFlags & URL_ESCAPE_PERCENT) && (ch == L'%'))
142 return TRUE;
143
144 if (ch <= 31 || ch >= 127)
145 return TRUE;
146
147 else {
148 switch (ch) {
149 case L' ':
150 case L'<':
151 case L'>':
152 case L'\"':
153 case L'{':
154 case L'}':
155 case L'|':
156 case L'\\':
157 case L'^':
158 case L']':
159 case L'[':
160 case L'`':
161 case L'&':
162 return TRUE;
163
164 case L'/':
165 case L'?':
166 if (dwFlags & URL_ESCAPE_SEGMENT_ONLY) return TRUE;
167 default:
168 return FALSE;
169 }
170 }
171}
172
173static BOOL URL_JustLocation(LPCWSTR str)
174{
175 while(*str && (*str == L'/')) str++;
176 if (*str) {
177 while (*str && ((*str == L'-') ||
178 (*str == L'.') ||
179 isalnumW(*str))) str++;
180 if (*str == L'/') return FALSE;
181 }
182 return TRUE;
183}
184
185
186/*************************************************************************
187 * SHAutoComplete [SHLWAPI.@]
188 */
189HRESULT WINAPI SHAutoComplete(HWND hwndEdit, DWORD dwFlags)
190{
191 FIXME("SHAutoComplete stub\n");
192 return S_FALSE;
193}
194
195/*************************************************************************
196 * UrlCanonicalizeA [SHLWAPI.@]
197 *
198 * Uses the W version to do job.
199 */
200HRESULT WINAPI UrlCanonicalizeA(LPCSTR pszUrl, LPSTR pszCanonicalized,
201 LPDWORD pcchCanonicalized, DWORD dwFlags)
202{
203 LPWSTR base, canonical;
204 DWORD ret, len, len2;
205
206 TRACE("(%s %p %p 0x%08lx) using W version\n",
207 debugstr_a(pszUrl), pszCanonicalized,
208 pcchCanonicalized, dwFlags);
209
210 base = (LPWSTR) HeapAlloc(GetProcessHeap(), 0,
211 (2*INTERNET_MAX_URL_LENGTH) * sizeof(WCHAR));
212 canonical = base + INTERNET_MAX_URL_LENGTH;
213
214 MultiByteToWideChar(0, 0, pszUrl, -1, base, INTERNET_MAX_URL_LENGTH);
215 len = INTERNET_MAX_URL_LENGTH;
216
217 ret = UrlCanonicalizeW(base, canonical, &len, dwFlags);
218 if (ret != S_OK) {
219 HeapFree(GetProcessHeap(), 0, base);
220 return ret;
221 }
222
223 len2 = WideCharToMultiByte(0, 0, canonical, len, 0, 0, 0, 0);
224 if (len2 > *pcchCanonicalized) {
225 *pcchCanonicalized = len;
226 HeapFree(GetProcessHeap(), 0, base);
227 return E_POINTER;
228 }
229 WideCharToMultiByte(0, 0, canonical, len+1, pszCanonicalized,
230 *pcchCanonicalized, 0, 0);
231 *pcchCanonicalized = len2;
232 HeapFree(GetProcessHeap(), 0, base);
233 return S_OK;
234}
235
236/*************************************************************************
237 * UrlCanonicalizeW [SHLWAPI.@]
238 *
239 *
240 * MSDN is wrong (at 10/30/01 - go figure). This should support the
241 * following flags: GLA
242 * URL_DONT_ESCAPE_EXTRA_INFO 0x02000000
243 * URL_ESCAPE_SPACES_ONLY 0x04000000
244 * URL_ESCAPE_PERCENT 0x00001000
245 * URL_ESCAPE_UNSAFE 0x10000000
246 * URL_UNESCAPE 0x10000000
247 * URL_DONT_SIMPLIFY 0x08000000
248 * URL_ESCAPE_SEGMENT_ONLY 0x00002000
249 */
250HRESULT WINAPI UrlCanonicalizeW(LPCWSTR pszUrl, LPWSTR pszCanonicalized,
251 LPDWORD pcchCanonicalized, DWORD dwFlags)
252{
253 HRESULT hr = S_OK;
254 DWORD EscapeFlags;
255 LPWSTR lpszUrlCpy, wk1, wk2, mp, root;
256 INT nLen, nByteLen, state;
257
258 TRACE("(%s %p %p 0x%08lx)\n", debugstr_w(pszUrl), pszCanonicalized,
259 pcchCanonicalized, dwFlags);
260
261 nByteLen = (lstrlenW(pszUrl) + 1) * sizeof(WCHAR); /* length in bytes */
262 lpszUrlCpy = HeapAlloc(GetProcessHeap(), 0, nByteLen);
263
264 if (dwFlags & URL_DONT_SIMPLIFY)
265 memcpy(lpszUrlCpy, pszUrl, nByteLen);
266 else {
267
268 /*
269 * state =
270 * 0 initial 1,3
271 * 1 have 2[+] alnum 2,3
272 * 2 have scheme (found :) 4,6,3
273 * 3 failed (no location)
274 * 4 have // 5,3
275 * 5 have 1[+] alnum 6,3
276 * 6 have location (found /) save root location
277 */
278
279 wk1 = (LPWSTR)pszUrl;
280 wk2 = lpszUrlCpy;
281 state = 0;
282 while (*wk1) {
283 switch (state) {
284 case 0:
285 if (!isalnumW(*wk1)) {state = 3; break;}
286 *wk2++ = *wk1++;
287 if (!isalnumW(*wk1)) {state = 3; break;}
288 *wk2++ = *wk1++;
289 state = 1;
290 break;
291 case 1:
292 *wk2++ = *wk1;
293 if (*wk1++ == L':') state = 2;
294 break;
295 case 2:
296 if (*wk1 != L'/') {state = 3; break;}
297 *wk2++ = *wk1++;
298 if (*wk1 != L'/') {state = 6; break;}
299 *wk2++ = *wk1++;
300 state = 4;
301 break;
302 case 3:
303 strcpyW(wk2, wk1);
304 wk1 += strlenW(wk1);
305 wk2 += strlenW(wk2);
306 break;
307 case 4:
308 if (!isalnumW(*wk1) && (*wk1 != L'-')) {state = 3; break;}
309 while(isalnumW(*wk1) || (*wk1 == L'-')) *wk2++ = *wk1++;
310 state = 5;
311 break;
312 case 5:
313 if (*wk1 != L'/') {state = 3; break;}
314 *wk2++ = *wk1++;
315 state = 6;
316 break;
317 case 6:
318 /* Now at root location, cannot back up any more. */
319 /* "root" will point at the '/' */
320 root = wk2-1;
321 while (*wk1) {
322 TRACE("wk1=%c\n", (CHAR)*wk1);
323 mp = strchrW(wk1, L'/');
324 if (!mp) {
325 strcpyW(wk2, wk1);
326 wk1 += strlenW(wk1);
327 wk2 += strlenW(wk2);
328 continue;
329 }
330 nLen = mp - wk1 + 1;
331 strncpyW(wk2, wk1, nLen);
332 wk2 += nLen;
333 wk1 += nLen;
334 if (*wk1 == L'.') {
335 TRACE("found '/.'\n");
336 if (*(wk1+1) == L'/') {
337 /* case of /./ -> skip the ./ */
338 wk1 += 2;
339 }
340 else if (*(wk1+1) == L'.') {
341 /* found /.. look for next / */
342 TRACE("found '/..'\n");
343 if (*(wk1+2) == L'/') {
344 /* case /../ -> need to backup wk2 */
345 TRACE("found '/../'\n");
346 *(wk2-1) = L'\0'; /* set end of string */
347 mp = strrchrW(root, L'/');
348 if (mp && (mp >= root)) {
349 /* found valid backup point */
350 wk2 = mp + 1;
351 wk1 += 3;
352 }
353 else {
354 /* did not find point, restore '/' */
355 *(wk2-1) = L'/';
356 }
357 }
358 }
359 }
360 }
361 *wk2 = L'\0';
362 break;
363 default:
364 FIXME("how did we get here - state=%d\n", state);
365 return E_INVALIDARG;
366 }
367 }
368 *wk2 = L'\0';
369 TRACE("Simplified, orig <%s>, simple <%s>\n",
370 debugstr_w(pszUrl), debugstr_w(lpszUrlCpy));
371 }
372
373 if(dwFlags & URL_UNESCAPE)
374 UrlUnescapeW(lpszUrlCpy, NULL, NULL, URL_UNESCAPE_INPLACE);
375
376 if((EscapeFlags = dwFlags & (URL_ESCAPE_UNSAFE |
377 URL_ESCAPE_SPACES_ONLY |
378 URL_ESCAPE_PERCENT |
379 URL_DONT_ESCAPE_EXTRA_INFO |
380 URL_ESCAPE_SEGMENT_ONLY ))) {
381 EscapeFlags &= ~URL_ESCAPE_UNSAFE;
382 hr = UrlEscapeW(lpszUrlCpy, pszCanonicalized, pcchCanonicalized,
383 EscapeFlags);
384 } else { /* No escaping needed, just copy the string */
385 nLen = lstrlenW(lpszUrlCpy);
386 if(nLen < *pcchCanonicalized)
387 memcpy(pszCanonicalized, lpszUrlCpy, (nLen + 1)*sizeof(WCHAR));
388 else {
389 hr = E_POINTER;
390 nLen++;
391 }
392 *pcchCanonicalized = nLen;
393 }
394
395 HeapFree(GetProcessHeap(), 0, lpszUrlCpy);
396
397 if (hr == S_OK)
398 TRACE("result %s\n", debugstr_w(pszCanonicalized));
399
400 return hr;
401}
402
403/*************************************************************************
404 * UrlCombineA [SHLWAPI.@]
405 *
406 * Uses the W version to do job.
407 */
408HRESULT WINAPI UrlCombineA(LPCSTR pszBase, LPCSTR pszRelative,
409 LPSTR pszCombined, LPDWORD pcchCombined,
410 DWORD dwFlags)
411{
412 LPWSTR base, relative, combined;
413 DWORD ret, len, len2;
414
415 TRACE("(base %s, Relative %s, Combine size %ld, flags %08lx) using W version\n",
416 debugstr_a(pszBase),debugstr_a(pszRelative),
417 *pcchCombined,dwFlags);
418
419 base = (LPWSTR) HeapAlloc(GetProcessHeap(), 0,
420 (3*INTERNET_MAX_URL_LENGTH) * sizeof(WCHAR));
421 relative = base + INTERNET_MAX_URL_LENGTH;
422 combined = relative + INTERNET_MAX_URL_LENGTH;
423
424 MultiByteToWideChar(0, 0, pszBase, -1, base, INTERNET_MAX_URL_LENGTH);
425 MultiByteToWideChar(0, 0, pszRelative, -1, relative, INTERNET_MAX_URL_LENGTH);
426 len = INTERNET_MAX_URL_LENGTH;
427
428 ret = UrlCombineW(base, relative, combined, &len, dwFlags);
429 if (ret != S_OK) {
430 HeapFree(GetProcessHeap(), 0, base);
431 return ret;
432 }
433
434 len2 = WideCharToMultiByte(0, 0, combined, len, 0, 0, 0, 0);
435 if (len2 > *pcchCombined) {
436 *pcchCombined = len2;
437 HeapFree(GetProcessHeap(), 0, base);
438 return E_POINTER;
439 }
440 WideCharToMultiByte(0, 0, combined, len+1, pszCombined, *pcchCombined,
441 0, 0);
442 *pcchCombined = len2;
443 HeapFree(GetProcessHeap(), 0, base);
444 return S_OK;
445}
446
447/*************************************************************************
448 * UrlCombineW [SHLWAPI.@]
449 */
450HRESULT WINAPI UrlCombineW(LPCWSTR pszBase, LPCWSTR pszRelative,
451 LPWSTR pszCombined, LPDWORD pcchCombined,
452 DWORD dwFlags)
453{
454 UNKNOWN_SHLWAPI_2 base, relative;
455 DWORD myflags, sizeloc = 0;
456 DWORD len, res1, res2, process_case = 0;
457 LPWSTR work, preliminary, mbase, mrelative;
458 WCHAR myfilestr[] = {'f','i','l','e',':','/','/','/','\0'};
459 WCHAR single_slash[] = {'/','\0'};
460 HRESULT ret;
461
462 TRACE("(base %s, Relative %s, Combine size %ld, flags %08lx)\n",
463 debugstr_w(pszBase),debugstr_w(pszRelative),
464 *pcchCombined,dwFlags);
465
466 base.size = 24;
467 relative.size = 24;
468
469 /* Get space for duplicates of the input and the output */
470 preliminary = HeapAlloc(GetProcessHeap(), 0, (3*INTERNET_MAX_URL_LENGTH) *
471 sizeof(WCHAR));
472 mbase = preliminary + INTERNET_MAX_URL_LENGTH;
473 mrelative = mbase + INTERNET_MAX_URL_LENGTH;
474 *preliminary = L'\0';
475
476 /* Canonicalize the base input prior to looking for the scheme */
477 myflags = dwFlags & (URL_DONT_SIMPLIFY | URL_UNESCAPE);
478 len = INTERNET_MAX_URL_LENGTH;
479 ret = UrlCanonicalizeW(pszBase, mbase, &len, myflags);
480
481 /* Canonicalize the relative input prior to looking for the scheme */
482 len = INTERNET_MAX_URL_LENGTH;
483 ret = UrlCanonicalizeW(pszRelative, mrelative, &len, myflags);
484
485 /* See if the base has a scheme */
486 res1 = SHLWAPI_2(mbase, &base);
487 if (res1) {
488 /* if pszBase has no scheme, then return pszRelative */
489 TRACE("no scheme detected in Base\n");
490 process_case = 1;
491 }
492 else do {
493
494 /* get size of location field (if it exists) */
495 work = (LPWSTR)base.ap2;
496 sizeloc = 0;
497 if (*work++ == L'/') {
498 if (*work++ == L'/') {
499 /* At this point have start of location and
500 * it ends at next '/' or end of string.
501 */
502 while(*work && (*work != L'/')) work++;
503 sizeloc = work - base.ap2;
504 }
505 }
506
507 /* Change .sizep2 to not have the last leaf in it,
508 * Note: we need to start after the location (if it exists)
509 */
510 work = strrchrW((base.ap2+sizeloc), L'/');
511 if (work) {
512 len = work - base.ap2 + 1;
513 base.sizep2 = len;
514 }
515 /*
516 * At this point:
517 * .ap2 points to location (starting with '//')
518 * .sizep2 length of location (above) and rest less the last
519 * leaf (if any)
520 * sizeloc length of location (above) up to but not including
521 * the last '/'
522 */
523
524 res2 = SHLWAPI_2(mrelative, &relative);
525 if (res2) {
526 /* no scheme in pszRelative */
527 TRACE("no scheme detected in Relative\n");
528 relative.ap2 = mrelative; /* case 3,4,5 depends on this */
529 relative.sizep2 = strlenW(mrelative);
530 if (*pszRelative == L':') {
531 /* case that is either left alone or uses pszBase */
532 if (dwFlags & URL_PLUGGABLE_PROTOCOL) {
533 process_case = 5;
534 break;
535 }
536 process_case = 1;
537 break;
538 }
539 if (isalnum(*mrelative) && (*(mrelative + 1) == L':')) {
540 /* case that becomes "file:///" */
541 strcpyW(preliminary, myfilestr);
542 process_case = 1;
543 break;
544 }
545 if ((*mrelative == L'/') && (*(mrelative+1) == L'/')) {
546 /* pszRelative has location and rest */
547 process_case = 3;
548 break;
549 }
550 if (*mrelative == L'/') {
551 /* case where pszRelative is root to location */
552 process_case = 4;
553 break;
554 }
555 process_case = (*base.ap2 == L'/') ? 5 : 3;
556 break;
557 }
558
559 /* handle cases where pszRelative has scheme */
560 if ((base.sizep1 == relative.sizep1) &&
561 (strncmpW(base.ap1, relative.ap1, base.sizep1) == 0)) {
562
563 /* since the schemes are the same */
564 if ((*relative.ap2 == L'/') && (*(relative.ap2+1) == L'/')) {
565 /* case where pszRelative replaces location and following */
566 process_case = 3;
567 break;
568 }
569 if (*relative.ap2 == L'/') {
570 /* case where pszRelative is root to location */
571 process_case = 4;
572 break;
573 }
574 /* case where scheme is followed by document path */
575 process_case = 5;
576 break;
577 }
578 if ((*relative.ap2 == L'/') && (*(relative.ap2+1) == L'/')) {
579 /* case where pszRelative replaces scheme, location,
580 * and following and handles PLUGGABLE
581 */
582 process_case = 2;
583 break;
584 }
585 process_case = 1;
586 break;
587 } while(FALSE); /* a litte trick to allow easy exit from nested if's */
588
589
590 ret = S_OK;
591 switch (process_case) {
592
593 case 1: /*
594 * Return pszRelative appended to what ever is in pszCombined,
595 * (which may the string "file:///"
596 */
597 len = strlenW(mrelative) + strlenW(preliminary);
598 if (len+1 > *pcchCombined) {
599 *pcchCombined = len;
600 ret = E_POINTER;
601 break;
602 }
603 strcatW(preliminary, mrelative);
604 break;
605
606 case 2: /*
607 * Same as case 1, but if URL_PLUGGABLE_PROTOCOL was specified
608 * and pszRelative starts with "//", then append a "/"
609 */
610 len = strlenW(mrelative) + 1;
611 if (len+1 > *pcchCombined) {
612 *pcchCombined = len;
613 ret = E_POINTER;
614 break;
615 }
616 strcpyW(preliminary, mrelative);
617 if (!(dwFlags & URL_PLUGGABLE_PROTOCOL) &&
618 URL_JustLocation(relative.ap2))
619 strcatW(preliminary, single_slash);
620 break;
621
622 case 3: /*
623 * Return the pszBase scheme with pszRelative. Basicly
624 * keeps the scheme and replaces the domain and following.
625 */
626 len = base.sizep1 + 1 + relative.sizep2 + 1;
627 if (len+1 > *pcchCombined) {
628 *pcchCombined = len;
629 ret = E_POINTER;
630 break;
631 }
632 strncpyW(preliminary, base.ap1, base.sizep1 + 1);
633 work = preliminary + base.sizep1 + 1;
634 strcpyW(work, relative.ap2);
635 if (!(dwFlags & URL_PLUGGABLE_PROTOCOL) &&
636 URL_JustLocation(relative.ap2))
637 strcatW(work, single_slash);
638 break;
639
640 case 4: /*
641 * Return the pszBase scheme and location but everything
642 * after the location is pszRelative. (Replace document
643 * from root on.)
644 */
645 len = base.sizep1 + 1 + sizeloc + relative.sizep2 + 1;
646 if (len+1 > *pcchCombined) {
647 *pcchCombined = len;
648 ret = E_POINTER;
649 break;
650 }
651 strncpyW(preliminary, base.ap1, base.sizep1+1+sizeloc);
652 work = preliminary + base.sizep1 + 1 + sizeloc;
653 if (dwFlags & URL_PLUGGABLE_PROTOCOL)
654 *(work++) = L'/';
655 strcpyW(work, relative.ap2);
656 break;
657
658 case 5: /*
659 * Return the pszBase without its document (if any) and
660 * append pszRelative after its scheme.
661 */
662 len = base.sizep1 + 1 + base.sizep2 + relative.sizep2;
663 if (len+1 > *pcchCombined) {
664 *pcchCombined = len;
665 ret = E_POINTER;
666 break;
667 }
668 strncpyW(preliminary, base.ap1, base.sizep1+1+base.sizep2);
669 work = preliminary + base.sizep1+1+base.sizep2 - 1;
670 if (*work++ != L'/')
671 *(work++) = L'/';
672 strcpyW(work, relative.ap2);
673 break;
674
675 default:
676 FIXME("How did we get here????? process_case=%ld\n", process_case);
677 ret = E_INVALIDARG;
678 }
679
680 if (ret == S_OK) {
681 /*
682 * Now that the combining is done, process the escape options if
683 * necessary, otherwise just copy the string.
684 */
685 myflags = dwFlags & (URL_ESCAPE_PERCENT |
686 URL_ESCAPE_SPACES_ONLY |
687 URL_DONT_ESCAPE_EXTRA_INFO |
688 URL_ESCAPE_SEGMENT_ONLY);
689 if (myflags)
690 ret = UrlEscapeW(preliminary, pszCombined,
691 pcchCombined, myflags);
692 else {
693 len = (strlenW(preliminary) + 1) * sizeof(WCHAR);
694 memcpy(pszCombined, preliminary, len);
695 *pcchCombined = strlenW(preliminary);
696 }
697 TRACE("return-%ld len=%ld, %s\n",
698 process_case, *pcchCombined, debugstr_w(pszCombined));
699 }
700 HeapFree(GetProcessHeap(), 0, preliminary);
701 return ret;
702}
703
704/*************************************************************************
705 * UrlEscapeA [SHLWAPI.@]
706 *
707 * Converts unsafe characters into their escape sequences.
708 *
709 * The converted string is returned in pszEscaped if the buffer size
710 * (which should be supplied in pcchEscaped) is large enough, in this
711 * case the function returns S_OK and pcchEscaped contains the length
712 * of the escaped string. If the buffer is not large enough the
713 * function returns E_POINTER and pcchEscaped contains the required
714 * buffer size (including room for the '\0').
715 *
716 * By default the function stops converting at the first '?' or
717 * '#'. [MSDN says differently]. If URL_ESCAPE_SPACE_ONLY flag is set
718 * then only spaces are converted, but the conversion continues past a
719 * '?' or '#'.
720 *
721 * BUGS:
722 * Have now implemented the following flags:
723 * URL_ESCAPE_SPACES_ONLY
724 * URL_DONT_ESCAPE_EXTRA_INFO
725 * URL_ESCAPE_SEGMENT_ONLY
726 * URL_ESCAPE_PERCENT
727 * Initial testing seems to indicate that this is now working like
728 * native shlwapi version 5. Note that these functions did not work
729 * well (or at all) in shlwapi version 4.
730 *
731 */
732HRESULT WINAPI UrlEscapeA(
733 LPCSTR pszUrl,
734 LPSTR pszEscaped,
735 LPDWORD pcchEscaped,
736 DWORD dwFlags)
737{
738 LPCSTR src;
739 DWORD needed = 0, ret;
740 BOOL stop_escaping = FALSE;
741 char next[3], *dst = pszEscaped;
742 char hex[] = "0123456789ABCDEF";
743 INT len;
744
745 TRACE("(%s %p %p 0x%08lx)\n", debugstr_a(pszUrl), pszEscaped,
746 pcchEscaped, dwFlags);
747
748 if(dwFlags & ~(URL_ESCAPE_SPACES_ONLY |
749 URL_ESCAPE_SEGMENT_ONLY |
750 URL_DONT_ESCAPE_EXTRA_INFO |
751 URL_ESCAPE_PERCENT))
752 FIXME("Unimplemented flags: %08lx\n", dwFlags);
753
754 /* fix up flags */
755 if (dwFlags & URL_ESCAPE_SPACES_ONLY)
756 /* if SPACES_ONLY specified, reset the other controls */
757 dwFlags &= ~(URL_DONT_ESCAPE_EXTRA_INFO |
758 URL_ESCAPE_PERCENT |
759 URL_ESCAPE_SEGMENT_ONLY);
760
761 else
762 /* if SPACES_ONLY *not* specified the assume DONT_ESCAPE_EXTRA_INFO */
763 dwFlags |= URL_DONT_ESCAPE_EXTRA_INFO;
764
765 for(src = pszUrl; *src; src++) {
766 if(!(dwFlags & URL_ESCAPE_SEGMENT_ONLY) &&
767 (dwFlags & URL_DONT_ESCAPE_EXTRA_INFO) &&
768 (*src == '#' || *src == '?'))
769 stop_escaping = TRUE;
770
771 if(URL_NeedEscapeA(*src, dwFlags) && stop_escaping == FALSE) {
772 /* TRACE("escaping %c\n", *src); */
773 next[0] = '%';
774 next[1] = hex[(*src >> 4) & 0xf];
775 next[2] = hex[*src & 0xf];
776 len = 3;
777 } else {
778 /* TRACE("passing %c\n", *src); */
779 next[0] = *src;
780 len = 1;
781 }
782
783 if(needed + len <= *pcchEscaped) {
784 memcpy(dst, next, len);
785 dst += len;
786 }
787 needed += len;
788 }
789
790 if(needed < *pcchEscaped) {
791 *dst = '\0';
792 ret = S_OK;
793 } else {
794 needed++; /* add one for the '\0' */
795 ret = E_POINTER;
796 }
797 *pcchEscaped = needed;
798 return ret;
799}
800
801/*************************************************************************
802 * UrlEscapeW [SHLWAPI.@]
803 *
804 * See UrlEscapeA for list of assumptions, bugs, and FIXMEs
805 */
806HRESULT WINAPI UrlEscapeW(
807 LPCWSTR pszUrl,
808 LPWSTR pszEscaped,
809 LPDWORD pcchEscaped,
810 DWORD dwFlags)
811{
812 LPCWSTR src;
813 DWORD needed = 0, ret;
814 BOOL stop_escaping = FALSE;
815 WCHAR next[5], *dst = pszEscaped;
816 CHAR hex[] = "0123456789ABCDEF";
817 INT len;
818
819 TRACE("(%s %p %p 0x%08lx)\n", debugstr_w(pszUrl), pszEscaped,
820 pcchEscaped, dwFlags);
821
822 if(dwFlags & ~(URL_ESCAPE_SPACES_ONLY |
823 URL_ESCAPE_SEGMENT_ONLY |
824 URL_DONT_ESCAPE_EXTRA_INFO |
825 URL_ESCAPE_PERCENT))
826 FIXME("Unimplemented flags: %08lx\n", dwFlags);
827
828 /* fix up flags */
829 if (dwFlags & URL_ESCAPE_SPACES_ONLY)
830 /* if SPACES_ONLY specified, reset the other controls */
831 dwFlags &= ~(URL_DONT_ESCAPE_EXTRA_INFO |
832 URL_ESCAPE_PERCENT |
833 URL_ESCAPE_SEGMENT_ONLY);
834
835 else
836 /* if SPACES_ONLY *not* specified the assume DONT_ESCAPE_EXTRA_INFO */
837 dwFlags |= URL_DONT_ESCAPE_EXTRA_INFO;
838
839 for(src = pszUrl; *src; src++) {
840 /*
841 * if(!(dwFlags & URL_ESCAPE_SPACES_ONLY) &&
842 * (*src == L'#' || *src == L'?'))
843 * stop_escaping = TRUE;
844 */
845 if(!(dwFlags & URL_ESCAPE_SEGMENT_ONLY) &&
846 (dwFlags & URL_DONT_ESCAPE_EXTRA_INFO) &&
847 (*src == L'#' || *src == L'?'))
848 stop_escaping = TRUE;
849
850 if(URL_NeedEscapeW(*src, dwFlags) && stop_escaping == FALSE) {
851 /* TRACE("escaping %c\n", *src); */
852 next[0] = L'%';
853 /*
854 * I would have assumed that the W form would escape
855 * the character with 4 hex digits (or even 8),
856 * however, experiments show that native shlwapi escapes
857 * with only 2 hex digits.
858 * next[1] = hex[(*src >> 12) & 0xf];
859 * next[2] = hex[(*src >> 8) & 0xf];
860 * next[3] = hex[(*src >> 4) & 0xf];
861 * next[4] = hex[*src & 0xf];
862 * len = 5;
863 */
864 next[1] = hex[(*src >> 4) & 0xf];
865 next[2] = hex[*src & 0xf];
866 len = 3;
867 } else {
868 /* TRACE("passing %c\n", *src); */
869 next[0] = *src;
870 len = 1;
871 }
872
873 if(needed + len <= *pcchEscaped) {
874 memcpy(dst, next, len*sizeof(WCHAR));
875 dst += len;
876 }
877 needed += len;
878 }
879
880 if(needed < *pcchEscaped) {
881 *dst = L'\0';
882 ret = S_OK;
883 } else {
884 needed++; /* add one for the '\0' */
885 ret = E_POINTER;
886 }
887 *pcchEscaped = needed;
888 return ret;
889}
890
891
892/*************************************************************************
893 * UrlUnescapeA [SHLWAPI.@]
894 *
895 * Converts escape sequences back to ordinary characters.
896 *
897 * If URL_ESCAPE_INPLACE is set in dwFlags then pszUnescaped and
898 * pcchUnescaped are ignored and the converted string is returned in
899 * pszUrl, otherwise the string is returned in pszUnescaped.
900 * pcchUnescaped should contain the size of pszUnescaped on calling
901 * and will contain the length the the returned string on return if
902 * the buffer is big enough else it will contain the buffer size
903 * required (including room for the '\0'). The function returns S_OK
904 * on success or E_POINTER if the buffer is not large enough. If the
905 * URL_DONT_ESCAPE_EXTRA_INFO flag is set then the conversion stops at
906 * the first occurrence of either '?' or '#'.
907 *
908 */
909HRESULT WINAPI UrlUnescapeA(
910 LPCSTR pszUrl,
911 LPSTR pszUnescaped,
912 LPDWORD pcchUnescaped,
913 DWORD dwFlags)
914{
915 char *dst, next;
916 LPCSTR src;
917 HRESULT ret;
918 DWORD needed;
919 BOOL stop_unescaping = FALSE;
920
921 TRACE("(%s, %p, %p, 0x%08lx)\n", debugstr_a(pszUrl), pszUnescaped,
922 pcchUnescaped, dwFlags);
923
924 if(dwFlags & URL_UNESCAPE_INPLACE)
925 dst = (char*)pszUrl;
926 else
927 dst = pszUnescaped;
928
929 for(src = pszUrl, needed = 0; *src; src++, needed++) {
930 if(dwFlags & URL_DONT_UNESCAPE_EXTRA_INFO &&
931 (*src == '#' || *src == '?')) {
932 stop_unescaping = TRUE;
933 next = *src;
934 } else if(*src == '%' && isxdigit(*(src + 1)) && isxdigit(*(src + 2))
935 && stop_unescaping == FALSE) {
936 INT ih;
937 char buf[3];
938 memcpy(buf, src + 1, 2);
939 buf[2] = '\0';
940 ih = strtol(buf, NULL, 16);
941 next = (CHAR) ih;
942 src += 2; /* Advance to end of escape */
943 } else
944 next = *src;
945
946 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped)
947 *dst++ = next;
948 }
949
950 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped) {
951 *dst = '\0';
952 ret = S_OK;
953 } else {
954 needed++; /* add one for the '\0' */
955 ret = E_POINTER;
956 }
957 if(!(dwFlags & URL_UNESCAPE_INPLACE))
958 *pcchUnescaped = needed;
959
960 if (ret == S_OK) {
961 TRACE("result %s\n", (dwFlags & URL_UNESCAPE_INPLACE) ?
962 debugstr_a(pszUrl) : debugstr_a(pszUnescaped));
963 }
964
965 return ret;
966}
967
968/*************************************************************************
969 * UrlUnescapeW [SHLWAPI.@]
970 *
971 * See UrlUnescapeA for list of assumptions, bugs, and FIXMEs
972 */
973HRESULT WINAPI UrlUnescapeW(
974 LPCWSTR pszUrl,
975 LPWSTR pszUnescaped,
976 LPDWORD pcchUnescaped,
977 DWORD dwFlags)
978{
979 WCHAR *dst, next;
980 LPCWSTR src;
981 HRESULT ret;
982 DWORD needed;
983 BOOL stop_unescaping = FALSE;
984
985 TRACE("(%s, %p, %p, 0x%08lx)\n", debugstr_w(pszUrl), pszUnescaped,
986 pcchUnescaped, dwFlags);
987
988 if(dwFlags & URL_UNESCAPE_INPLACE)
989 dst = (WCHAR*)pszUrl;
990 else
991 dst = pszUnescaped;
992
993 for(src = pszUrl, needed = 0; *src; src++, needed++) {
994 if(dwFlags & URL_DONT_UNESCAPE_EXTRA_INFO &&
995 (*src == L'#' || *src == L'?')) {
996 stop_unescaping = TRUE;
997 next = *src;
998 } else if(*src == L'%' && isxdigitW(*(src + 1)) && isxdigitW(*(src + 2))
999 && stop_unescaping == FALSE) {
1000 INT ih;
1001 WCHAR buf[3];
1002 memcpy(buf, src + 1, 2*sizeof(WCHAR));
1003 buf[2] = L'\0';
1004 ih = StrToIntW(buf);
1005 next = (WCHAR) ih;
1006 src += 2; /* Advance to end of escape */
1007 } else
1008 next = *src;
1009
1010 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped)
1011 *dst++ = next;
1012 }
1013
1014 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped) {
1015 *dst = L'\0';
1016 ret = S_OK;
1017 } else {
1018 needed++; /* add one for the '\0' */
1019 ret = E_POINTER;
1020 }
1021 if(!(dwFlags & URL_UNESCAPE_INPLACE))
1022 *pcchUnescaped = needed;
1023
1024 if (ret == S_OK) {
1025 TRACE("result %s\n", (dwFlags & URL_UNESCAPE_INPLACE) ?
1026 debugstr_w(pszUrl) : debugstr_w(pszUnescaped));
1027 }
1028
1029 return ret;
1030}
1031
1032/*************************************************************************
1033 * UrlGetLocationA [SHLWAPI.@]
1034 *
1035 * Bugs/Features:
1036 * MSDN (as of 2001-11-01) says that:
1037 * "The location is the segment of the URL starting with a ?
1038 * or # character."
1039 * Neither V4 nor V5 of shlwapi.dll implement the '?' and always return
1040 * a NULL.
1041 * MSDN further states that:
1042 * "If a file URL has a query string, ther returned string
1043 * the query string."
1044 * In all test cases if the scheme starts with "fi" then a NULL is
1045 * returned. V5 gives the following results:
1046 * NULL file://aa/b/cd#hohoh
1047 * #hohoh http://aa/b/cd#hohoh
1048 * NULL fi://aa/b/cd#hohoh
1049 * #hohoh ff://aa/b/cd#hohoh
1050 */
1051LPCSTR WINAPI UrlGetLocationA(
1052 LPCSTR pszUrl)
1053{
1054 UNKNOWN_SHLWAPI_1 base;
1055 DWORD res1;
1056
1057 base.size = 24;
1058 res1 = SHLWAPI_1(pszUrl, &base);
1059 if (res1) return NULL; /* invalid scheme */
1060
1061 /* if scheme is file: then never return pointer */
1062 if (strncmp(base.ap1, "file", min(4,base.sizep1)) == 0) return NULL;
1063
1064 /* Look for '#' and return its addr */
1065 return strchr(base.ap2, '#');
1066}
1067
1068/*************************************************************************
1069 * UrlGetLocationW [SHLWAPI.@]
1070 *
1071 * See UrlGetLocationA for list of assumptions, bugs, and FIXMEs
1072 */
1073LPCWSTR WINAPI UrlGetLocationW(
1074 LPCWSTR pszUrl)
1075{
1076 UNKNOWN_SHLWAPI_2 base;
1077 DWORD res1;
1078
1079 base.size = 24;
1080 res1 = SHLWAPI_2(pszUrl, &base);
1081 if (res1) return NULL; /* invalid scheme */
1082
1083 /* if scheme is file: then never return pointer */
1084 if (strncmpW(base.ap1, fileW, min(4,base.sizep1)) == 0) return NULL;
1085
1086 /* Look for '#' and return its addr */
1087 return strchrW(base.ap2, L'#');
1088}
1089
1090/*************************************************************************
1091 * UrlCompareA [SHLWAPI.@]
1092 */
1093INT WINAPI UrlCompareA(
1094 LPCSTR pszUrl1,
1095 LPCSTR pszUrl2,
1096 BOOL fIgnoreSlash)
1097{
1098 INT ret, len, len1, len2;
1099
1100 if (!fIgnoreSlash)
1101 return strcmp(pszUrl1, pszUrl2);
1102 len1 = strlen(pszUrl1);
1103 if (pszUrl1[len1-1] == L'/') len1--;
1104 len2 = strlen(pszUrl2);
1105 if (pszUrl2[len2-1] == L'/') len2--;
1106 if (len1 == len2)
1107 return strncmp(pszUrl1, pszUrl2, len1);
1108 len = min(len1, len2);
1109 ret = strncmp(pszUrl1, pszUrl2, len);
1110 if (ret) return ret;
1111 if (len1 > len2) return 1;
1112 return -1;
1113}
1114
1115/*************************************************************************
1116 * UrlCompareW [SHLWAPI.@]
1117 */
1118INT WINAPI UrlCompareW(
1119 LPCWSTR pszUrl1,
1120 LPCWSTR pszUrl2,
1121 BOOL fIgnoreSlash)
1122{
1123 INT ret, len, len1, len2;
1124
1125 if (!fIgnoreSlash)
1126 return strcmpW(pszUrl1, pszUrl2);
1127 len1 = strlenW(pszUrl1);
1128 if (pszUrl1[len1-1] == L'/') len1--;
1129 len2 = strlenW(pszUrl2);
1130 if (pszUrl2[len2-1] == L'/') len2--;
1131 if (len1 == len2)
1132 return strncmpW(pszUrl1, pszUrl2, len1);
1133 len = min(len1, len2);
1134 ret = strncmpW(pszUrl1, pszUrl2, len);
1135 if (ret) return ret;
1136 if (len1 > len2) return 1;
1137 return -1;
1138}
1139
1140/*************************************************************************
1141 * HashData [SHLWAPI.@]
1142 *
1143 * Hash an input block into a variable sized digest.
1144 */
1145BOOL WINAPI HashData(const unsigned char *lpSrc, INT nSrcLen,
1146 unsigned char *lpDest, INT nDestLen)
1147{
1148 INT srcCount = nSrcLen - 1, destCount = nDestLen - 1;
1149
1150 if (IsBadReadPtr(lpSrc, nSrcLen) ||
1151 IsBadWritePtr(lpDest, nDestLen))
1152 return FALSE;
1153
1154 while (destCount >= 0)
1155 {
1156 lpDest[destCount] = (destCount & 0xff);
1157 destCount--;
1158 }
1159
1160 while (srcCount >= 0)
1161 {
1162 destCount = nDestLen - 1;
1163 while (destCount >= 0)
1164 {
1165 lpDest[destCount] = HashDataLookup[lpSrc[srcCount] ^ lpDest[destCount]];
1166 destCount--;
1167 }
1168 srcCount--;
1169 }
1170 return TRUE;
1171}
1172
1173/*************************************************************************
1174 * UrlHashA [SHLWAPI.@]
1175 *
1176 * Hash an ASCII URL.
1177 */
1178HRESULT WINAPI UrlHashA(LPCSTR pszUrl, unsigned char *lpDest, INT nDestLen)
1179{
1180 if (IsBadStringPtrA(pszUrl, -1) || IsBadWritePtr(lpDest, nDestLen))
1181 return E_INVALIDARG;
1182
1183 HashData(pszUrl, strlen(pszUrl), lpDest, nDestLen);
1184 return NOERROR;
1185}
1186
1187/*************************************************************************
1188 * UrlApplySchemeA [SHLWAPI.@]
1189 */
1190HRESULT WINAPI UrlApplySchemeA(LPCSTR pszIn, LPSTR pszOut, LPDWORD pcchOut, DWORD dwFlags)
1191{
1192 LPWSTR in, out;
1193 DWORD ret, len, len2;
1194
1195 TRACE("(in %s, out size %ld, flags %08lx) using W version\n",
1196 debugstr_a(pszIn), *pcchOut, dwFlags);
1197
1198 in = (LPWSTR) HeapAlloc(GetProcessHeap(), 0,
1199 (2*INTERNET_MAX_URL_LENGTH) * sizeof(WCHAR));
1200 out = in + INTERNET_MAX_URL_LENGTH;
1201
1202 MultiByteToWideChar(0, 0, pszIn, -1, in, INTERNET_MAX_URL_LENGTH);
1203 len = INTERNET_MAX_URL_LENGTH;
1204
1205 ret = UrlApplySchemeW(in, out, &len, dwFlags);
1206 if ((ret != S_OK) && (ret != S_FALSE)) {
1207 HeapFree(GetProcessHeap(), 0, in);
1208 return ret;
1209 }
1210
1211 len2 = WideCharToMultiByte(0, 0, out, len+1, 0, 0, 0, 0);
1212 if (len2 > *pcchOut) {
1213 *pcchOut = len2;
1214 HeapFree(GetProcessHeap(), 0, in);
1215 return E_POINTER;
1216 }
1217 WideCharToMultiByte(0, 0, out, len+1, pszOut, *pcchOut, 0, 0);
1218 *pcchOut = len2;
1219 HeapFree(GetProcessHeap(), 0, in);
1220 return ret;
1221}
1222
1223HRESULT URL_GuessScheme(LPCWSTR pszIn, LPWSTR pszOut, LPDWORD pcchOut)
1224{
1225 HKEY newkey;
1226 BOOL j;
1227 INT index, i;
1228 DWORD value_len, data_len, dwType;
1229 WCHAR reg_path[MAX_PATH];
1230 WCHAR value[MAX_PATH], data[MAX_PATH];
1231 WCHAR Wxx, Wyy;
1232
1233 MultiByteToWideChar(0, 0,
1234 "Software\\Microsoft\\Windows\\CurrentVersion\\URL\\Prefixes",
1235 -1, reg_path, MAX_PATH);
1236 RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, 1, &newkey);
1237 index = 0;
1238 while(value_len = data_len = MAX_PATH,
1239 RegEnumValueW(newkey, index, value, &value_len,
1240 0, &dwType, (LPVOID)data, &data_len) == 0) {
1241 TRACE("guess %d %s is %s\n",
1242 index, debugstr_w(value), debugstr_w(data));
1243
1244 j = FALSE;
1245 for(i=0; i<value_len; i++) {
1246 Wxx = pszIn[i];
1247 Wyy = value[i];
1248 /* remember that TRUE is not-equal */
1249 j = ChrCmpIW(Wxx, Wyy);
1250 if (j) break;
1251 }
1252 if ((i == value_len) && !j) {
1253 if (strlenW(data) + strlenW(pszIn) + 1 > *pcchOut) {
1254 *pcchOut = strlenW(data) + strlenW(pszIn) + 1;
1255 RegCloseKey(newkey);
1256 return E_POINTER;
1257 }
1258 strcpyW(pszOut, data);
1259 strcatW(pszOut, pszIn);
1260 *pcchOut = strlenW(pszOut);
1261 TRACE("matched and set to %s\n", debugstr_w(pszOut));
1262 RegCloseKey(newkey);
1263 return S_OK;
1264 }
1265 index++;
1266 }
1267 RegCloseKey(newkey);
1268 return -1;
1269}
1270
1271HRESULT URL_ApplyDefault(LPCWSTR pszIn, LPWSTR pszOut, LPDWORD pcchOut)
1272{
1273 HKEY newkey;
1274 DWORD data_len, dwType;
1275 WCHAR reg_path[MAX_PATH];
1276 WCHAR value[MAX_PATH], data[MAX_PATH];
1277
1278 /* get and prepend default */
1279 MultiByteToWideChar(0, 0,
1280 "Software\\Microsoft\\Windows\\CurrentVersion\\URL\\DefaultPrefix",
1281 -1, reg_path, MAX_PATH);
1282 RegOpenKeyExW(HKEY_LOCAL_MACHINE, reg_path, 0, 1, &newkey);
1283 data_len = MAX_PATH;
1284 value[0] = L'@';
1285 value[1] = L'\0';
1286 RegQueryValueExW(newkey, value, 0, &dwType, (LPBYTE)data, &data_len);
1287 RegCloseKey(newkey);
1288 if (strlenW(data) + strlenW(pszIn) + 1 > *pcchOut) {
1289 *pcchOut = strlenW(data) + strlenW(pszIn) + 1;
1290 return E_POINTER;
1291 }
1292 strcpyW(pszOut, data);
1293 strcatW(pszOut, pszIn);
1294 *pcchOut = strlenW(pszOut);
1295 TRACE("used default %s\n", debugstr_w(pszOut));
1296 return S_OK;
1297}
1298
1299/*************************************************************************
1300 * UrlApplySchemeW [SHLWAPI.@]
1301 */
1302HRESULT WINAPI UrlApplySchemeW(LPCWSTR pszIn, LPWSTR pszOut, LPDWORD pcchOut, DWORD dwFlags)
1303{
1304 UNKNOWN_SHLWAPI_2 in_scheme;
1305 DWORD res1;
1306 HRESULT ret;
1307
1308 TRACE("(in %s, out size %ld, flags %08lx)\n",
1309 debugstr_w(pszIn), *pcchOut, dwFlags);
1310
1311 if (dwFlags & URL_APPLY_GUESSFILE) {
1312 FIXME("(%s %p %p(%ld) 0x%08lx): stub URL_APPLY_GUESSFILE not implemented\n",
1313 debugstr_w(pszIn), pszOut, pcchOut, *pcchOut, dwFlags);
1314 strcpyW(pszOut, pszIn);
1315 *pcchOut = strlenW(pszOut);
1316 return S_FALSE;
1317 }
1318
1319 in_scheme.size = 24;
1320 /* See if the base has a scheme */
1321 res1 = SHLWAPI_2(pszIn, &in_scheme);
1322 if (res1) {
1323 /* no scheme in input, need to see if we need to guess */
1324 if (dwFlags & URL_APPLY_GUESSSCHEME) {
1325 if ((ret = URL_GuessScheme(pszIn, pszOut, pcchOut)) != -1)
1326 return ret;
1327 }
1328 }
1329 else {
1330 /* we have a scheme, see if valid (known scheme) */
1331 if (in_scheme.fcncde) {
1332 /* have valid scheme, so just copy and exit */
1333 if (strlenW(pszIn) + 1 > *pcchOut) {
1334 *pcchOut = strlenW(pszIn) + 1;
1335 return E_POINTER;
1336 }
1337 strcpyW(pszOut, pszIn);
1338 *pcchOut = strlenW(pszOut);
1339 TRACE("valid scheme, returing copy\n");
1340 return S_OK;
1341 }
1342 }
1343
1344 /* If we are here, then either invalid scheme,
1345 * or no scheme and can't/failed guess.
1346 */
1347 if ( ( ((res1 == 0) && (dwFlags & URL_APPLY_FORCEAPPLY)) ||
1348 ((res1 != 0)) ) &&
1349 (dwFlags & URL_APPLY_DEFAULT)) {
1350 /* find and apply default scheme */
1351 return URL_ApplyDefault(pszIn, pszOut, pcchOut);
1352 }
1353
1354 /* just copy and give proper return code */
1355 if (strlenW(pszIn) + 1 > *pcchOut) {
1356 *pcchOut = strlenW(pszIn) + 1;
1357 return E_POINTER;
1358 }
1359 strcpyW(pszOut, pszIn);
1360 *pcchOut = strlenW(pszOut);
1361 TRACE("returing copy, left alone\n");
1362 return S_FALSE;
1363}
1364
1365/*************************************************************************
1366 * UrlIsA [SHLWAPI.@]
1367 */
1368BOOL WINAPI UrlIsA(LPCSTR pszUrl, URLIS Urlis)
1369{
1370 UNKNOWN_SHLWAPI_1 base;
1371 DWORD res1;
1372
1373 switch (Urlis) {
1374
1375 case URLIS_OPAQUE:
1376 base.size = 24;
1377 res1 = SHLWAPI_1(pszUrl, &base);
1378 if (res1) return FALSE; /* invalid scheme */
1379 if ((*base.ap2 == '/') && (*(base.ap2+1) == '/'))
1380 /* has scheme followed by 2 '/' */
1381 return FALSE;
1382 return TRUE;
1383
1384 case URLIS_URL:
1385 case URLIS_NOHISTORY:
1386 case URLIS_FILEURL:
1387 case URLIS_APPLIABLE:
1388 case URLIS_DIRECTORY:
1389 case URLIS_HASQUERY:
1390 default:
1391 FIXME("(%s %d): stub\n", debugstr_a(pszUrl), Urlis);
1392 }
1393 return FALSE;
1394}
1395
1396/*************************************************************************
1397 * UrlIsW [SHLWAPI.@]
1398 */
1399BOOL WINAPI UrlIsW(LPCWSTR pszUrl, URLIS Urlis)
1400{
1401 UNKNOWN_SHLWAPI_2 base;
1402 DWORD res1;
1403
1404 switch (Urlis) {
1405
1406 case URLIS_OPAQUE:
1407 base.size = 24;
1408 res1 = SHLWAPI_2(pszUrl, &base);
1409 if (res1) return FALSE; /* invalid scheme */
1410 if ((*base.ap2 == L'/') && (*(base.ap2+1) == L'/'))
1411 /* has scheme followed by 2 '/' */
1412 return FALSE;
1413 return TRUE;
1414
1415 case URLIS_URL:
1416 case URLIS_NOHISTORY:
1417 case URLIS_FILEURL:
1418 case URLIS_APPLIABLE:
1419 case URLIS_DIRECTORY:
1420 case URLIS_HASQUERY:
1421 default:
1422 FIXME("(%s %d): stub\n", debugstr_w(pszUrl), Urlis);
1423 }
1424 return FALSE;
1425}
1426
1427/*************************************************************************
1428 * UrlIsNoHistoryA [SHLWAPI.@]
1429 */
1430BOOL WINAPI UrlIsNoHistoryA(LPCSTR pszUrl)
1431{
1432 return UrlIsA(pszUrl, URLIS_NOHISTORY);
1433}
1434
1435/*************************************************************************
1436 * UrlIsNoHistoryW [SHLWAPI.@]
1437 */
1438BOOL WINAPI UrlIsNoHistoryW(LPCWSTR pszUrl)
1439{
1440 return UrlIsW(pszUrl, URLIS_NOHISTORY);
1441}
1442
1443/*************************************************************************
1444 * UrlIsOpaqueA [SHLWAPI.@]
1445 */
1446BOOL WINAPI UrlIsOpaqueA(LPCSTR pszUrl)
1447{
1448 return UrlIsA(pszUrl, URLIS_OPAQUE);
1449}
1450
1451/*************************************************************************
1452 * UrlIsOpaqueW [SHLWAPI.@]
1453 */
1454BOOL WINAPI UrlIsOpaqueW(LPCWSTR pszUrl)
1455{
1456 return UrlIsW(pszUrl, URLIS_OPAQUE);
1457}
1458
1459/*************************************************************************
1460 * Scans for characters of type "type" and when not matching found,
1461 * returns pointer to it and length in size.
1462 *
1463 * Characters tested based on RFC 1738
1464 */
1465LPCWSTR URL_ScanID(LPCWSTR start, LPDWORD size, WINE_URL_SCAN_TYPE type)
1466{
1467 static DWORD alwayszero = 0;
1468 BOOL cont = TRUE;
1469
1470 *size = 0;
1471
1472 switch(type){
1473
1474 case SCHEME:
1475 while (cont) {
1476 if ( (islowerW(*start) && isalphaW(*start)) ||
1477 isdigitW(*start) ||
1478 (*start == L'+') ||
1479 (*start == L'-') ||
1480 (*start == L'.')) {
1481 start++;
1482 (*size)++;
1483 }
1484 else
1485 cont = FALSE;
1486 }
1487 break;
1488
1489 case USERPASS:
1490 while (cont) {
1491 if ( isalphaW(*start) ||
1492 isdigitW(*start) ||
1493 /* user/password only characters */
1494 (*start == L';') ||
1495 (*start == L'?') ||
1496 (*start == L'&') ||
1497 (*start == L'=') ||
1498 /* *extra* characters */
1499 (*start == L'!') ||
1500 (*start == L'*') ||
1501 (*start == L'\'') ||
1502 (*start == L'(') ||
1503 (*start == L')') ||
1504 (*start == L',') ||
1505 /* *safe* characters */
1506 (*start == L'$') ||
1507 (*start == L'_') ||
1508 (*start == L'+') ||
1509 (*start == L'-') ||
1510 (*start == L'.')) {
1511 start++;
1512 (*size)++;
1513 } else if (*start == L'%') {
1514 if (isxdigitW(*(start+1)) &&
1515 isxdigitW(*(start+2))) {
1516 start += 3;
1517 *size += 3;
1518 } else
1519 cont = FALSE;
1520 } else
1521 cont = FALSE;
1522 }
1523 break;
1524
1525 case PORT:
1526 while (cont) {
1527 if (isdigitW(*start)) {
1528 start++;
1529 (*size)++;
1530 }
1531 else
1532 cont = FALSE;
1533 }
1534 break;
1535
1536 case HOST:
1537 while (cont) {
1538 if (isalnumW(*start) ||
1539 (*start == L'-') ||
1540 (*start == L'.') ) {
1541 start++;
1542 (*size)++;
1543 }
1544 else
1545 cont = FALSE;
1546 }
1547 break;
1548 default:
1549 FIXME("unknown type %d\n", type);
1550 return (LPWSTR)&alwayszero;
1551 }
1552 /* TRACE("scanned %ld characters next char %p<%c>\n",
1553 *size, start, *start); */
1554 return start;
1555}
1556
1557/*************************************************************************
1558 * Attempt to parse URL into pieces.
1559 */
1560LONG URL_ParseUrl(LPCWSTR pszUrl, WINE_PARSE_URL *pl)
1561{
1562 LPCWSTR work;
1563
1564 memset(pl, 0, sizeof(WINE_PARSE_URL));
1565 pl->pScheme = pszUrl;
1566 work = URL_ScanID(pl->pScheme, &pl->szScheme, SCHEME);
1567#ifdef __WIN32OS2__
1568 if (!*work || (*work != L':')) goto __ERROR;
1569 work++;
1570 if ((*work != L'/') || (*(work+1) != L'/')) goto __ERROR;
1571#else
1572 if (!*work || (*work != L':')) goto ERROR;
1573 work++;
1574 if ((*work != L'/') || (*(work+1) != L'/')) goto ERROR;
1575#endif
1576 pl->pUserName = work + 2;
1577 work = URL_ScanID(pl->pUserName, &pl->szUserName, USERPASS);
1578 if (*work == L':' ) {
1579 /* parse password */
1580 work++;
1581 pl->pPassword = work;
1582 work = URL_ScanID(pl->pPassword, &pl->szPassword, USERPASS);
1583 if (*work != L'@') {
1584 /* what we just parsed must be the hostname and port
1585 * so reset pointers and clear then let it parse */
1586 pl->szUserName = pl->szPassword = 0;
1587 work = pl->pUserName - 1;
1588 pl->pUserName = pl->pPassword = 0;
1589 }
1590 } else if (*work == L'@') {
1591 /* no password */
1592 pl->szPassword = 0;
1593 pl->pPassword = 0;
1594 } else if (!*work || (*work == L'/') || (*work == L'.')) {
1595 /* what was parsed was hostname, so reset pointers and let it parse */
1596 pl->szUserName = pl->szPassword = 0;
1597 work = pl->pUserName - 1;
1598 pl->pUserName = pl->pPassword = 0;
1599#ifdef __WIN32OS2__
1600 } else goto __ERROR;
1601#else
1602 } else goto ERROR;
1603#endif
1604 /* now start parsing hostname or hostnumber */
1605 work++;
1606 pl->pHostName = work;
1607 work = URL_ScanID(pl->pHostName, &pl->szHostName, HOST);
1608 if (*work == L':') {
1609 /* parse port */
1610 work++;
1611 pl->pPort = work;
1612 work = URL_ScanID(pl->pPort, &pl->szPort, PORT);
1613 }
1614 if (*work == L'/') {
1615 /* see if query string */
1616 pl->pQuery = strchrW(work, L'?');
1617 if (pl->pQuery) pl->szQuery = strlenW(pl->pQuery);
1618 }
1619 TRACE("parse successful: scheme=%p(%ld), user=%p(%ld), pass=%p(%ld), host=%p(%ld), port=%p(%ld), query=%p(%ld)\n",
1620 pl->pScheme, pl->szScheme,
1621 pl->pUserName, pl->szUserName,
1622 pl->pPassword, pl->szPassword,
1623 pl->pHostName, pl->szHostName,
1624 pl->pPort, pl->szPort,
1625 pl->pQuery, pl->szQuery);
1626 return S_OK;
1627#ifdef __WIN32OS2__
1628 __ERROR:
1629#else
1630 ERROR:
1631#endif
1632 FIXME("failed to parse %s\n", debugstr_w(pszUrl));
1633 return E_INVALIDARG;
1634}
1635
1636/*************************************************************************
1637 * UrlGetPartA [SHLWAPI.@]
1638 */
1639HRESULT WINAPI UrlGetPartA(LPCSTR pszIn, LPSTR pszOut, LPDWORD pcchOut,
1640 DWORD dwPart, DWORD dwFlags)
1641{
1642 LPWSTR in, out;
1643 DWORD ret, len, len2;
1644
1645 in = (LPWSTR) HeapAlloc(GetProcessHeap(), 0,
1646 (2*INTERNET_MAX_URL_LENGTH) * sizeof(WCHAR));
1647 out = in + INTERNET_MAX_URL_LENGTH;
1648
1649 MultiByteToWideChar(0, 0, pszIn, -1, in, INTERNET_MAX_URL_LENGTH);
1650
1651 len = INTERNET_MAX_URL_LENGTH;
1652 ret = UrlGetPartW(in, out, &len, dwPart, dwFlags);
1653
1654 if (ret != S_OK) {
1655 HeapFree(GetProcessHeap(), 0, in);
1656 return ret;
1657 }
1658
1659 len2 = WideCharToMultiByte(0, 0, out, len, 0, 0, 0, 0);
1660 if (len2 > *pcchOut) {
1661 *pcchOut = len2;
1662 HeapFree(GetProcessHeap(), 0, in);
1663 return E_POINTER;
1664 }
1665 WideCharToMultiByte(0, 0, out, len+1, pszOut, *pcchOut, 0, 0);
1666 *pcchOut = len2;
1667 HeapFree(GetProcessHeap(), 0, in);
1668 return S_OK;
1669}
1670
1671/*************************************************************************
1672 * UrlGetPartW [SHLWAPI.@]
1673 */
1674HRESULT WINAPI UrlGetPartW(LPCWSTR pszIn, LPWSTR pszOut, LPDWORD pcchOut,
1675 DWORD dwPart, DWORD dwFlags)
1676{
1677 WINE_PARSE_URL pl;
1678 HRESULT ret;
1679 DWORD size, schsize;
1680 LPCWSTR addr, schaddr;
1681 LPWSTR work;
1682
1683 TRACE("(%s %p %p(%ld) %08lx %08lx)\n",
1684 debugstr_w(pszIn), pszOut, pcchOut, *pcchOut, dwPart, dwFlags);
1685
1686 ret = URL_ParseUrl(pszIn, &pl);
1687 if (!ret) {
1688 schaddr = pl.pScheme;
1689 schsize = pl.szScheme;
1690
1691 switch (dwPart) {
1692 case URL_PART_SCHEME:
1693 if (!pl.szScheme) return E_INVALIDARG;
1694 addr = pl.pScheme;
1695 size = pl.szScheme;
1696 break;
1697
1698 case URL_PART_HOSTNAME:
1699 if (!pl.szHostName) return E_INVALIDARG;
1700 addr = pl.pHostName;
1701 size = pl.szHostName;
1702 break;
1703
1704 case URL_PART_USERNAME:
1705 if (!pl.szUserName) return E_INVALIDARG;
1706 addr = pl.pUserName;
1707 size = pl.szUserName;
1708 break;
1709
1710 case URL_PART_PASSWORD:
1711 if (!pl.szPassword) return E_INVALIDARG;
1712 addr = pl.pPassword;
1713 size = pl.szPassword;
1714 break;
1715
1716 case URL_PART_PORT:
1717 if (!pl.szPort) return E_INVALIDARG;
1718 addr = pl.pPort;
1719 size = pl.szPort;
1720 break;
1721
1722 case URL_PART_QUERY:
1723 if (!pl.szQuery) return E_INVALIDARG;
1724 addr = pl.pQuery;
1725 size = pl.szQuery;
1726 break;
1727
1728 default:
1729 return E_INVALIDARG;
1730 }
1731
1732 if (dwFlags == URL_PARTFLAG_KEEPSCHEME) {
1733 if (*pcchOut < size + schsize + 2) {
1734 *pcchOut = size + schsize + 2;
1735 return E_POINTER;
1736 }
1737 strncpyW(pszOut, schaddr, schsize);
1738 work = pszOut + schsize;
1739 *work = L':';
1740 strncpyW(work+1, addr, size);
1741 *pcchOut = size + schsize + 1;
1742 work += (size + 1);
1743 *work = L'\0';
1744 }
1745 else {
1746 if (*pcchOut < size + 1) {*pcchOut = size+1; return E_POINTER;}
1747 strncpyW(pszOut, addr, size);
1748 *pcchOut = size;
1749 work = pszOut + size;
1750 *work = L'\0';
1751 }
1752 TRACE("len=%ld %s\n", *pcchOut, debugstr_w(pszOut));
1753 }
1754 return ret;
1755}
Note: See TracBrowser for help on using the repository browser.