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

Last change on this file since 6650 was 6650, checked in by bird, 24 years ago

Added $Id:$ keyword.

File size: 11.2 KB
Line 
1/* $Id: url.c,v 1.4 2001-09-05 13:48:39 bird Exp $ */
2/*
3 * Url functions
4 *
5 * Copyright 2000 Huw D M Davies for CodeWeavers.
6 */
7
8#include <string.h>
9#include "windef.h"
10#include "winbase.h"
11#include "winerror.h"
12#include "shlwapi.h"
13#include "debugtools.h"
14
15#ifdef __WIN32OS2__
16#undef FIXME
17#ifdef DEBUG
18#define FIXME WriteLog("FIXME %s", __FUNCTION__); WriteLog
19#else
20#define FIXME 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
21#endif
22#endif
23
24DEFAULT_DEBUG_CHANNEL(shell);
25
26static const unsigned char HashDataLookup[256] = {
27 0x01, 0x0E, 0x6E, 0x19, 0x61, 0xAE, 0x84, 0x77, 0x8A, 0xAA, 0x7D, 0x76, 0x1B,
28 0xE9, 0x8C, 0x33, 0x57, 0xC5, 0xB1, 0x6B, 0xEA, 0xA9, 0x38, 0x44, 0x1E, 0x07,
29 0xAD, 0x49, 0xBC, 0x28, 0x24, 0x41, 0x31, 0xD5, 0x68, 0xBE, 0x39, 0xD3, 0x94,
30 0xDF, 0x30, 0x73, 0x0F, 0x02, 0x43, 0xBA, 0xD2, 0x1C, 0x0C, 0xB5, 0x67, 0x46,
31 0x16, 0x3A, 0x4B, 0x4E, 0xB7, 0xA7, 0xEE, 0x9D, 0x7C, 0x93, 0xAC, 0x90, 0xB0,
32 0xA1, 0x8D, 0x56, 0x3C, 0x42, 0x80, 0x53, 0x9C, 0xF1, 0x4F, 0x2E, 0xA8, 0xC6,
33 0x29, 0xFE, 0xB2, 0x55, 0xFD, 0xED, 0xFA, 0x9A, 0x85, 0x58, 0x23, 0xCE, 0x5F,
34 0x74, 0xFC, 0xC0, 0x36, 0xDD, 0x66, 0xDA, 0xFF, 0xF0, 0x52, 0x6A, 0x9E, 0xC9,
35 0x3D, 0x03, 0x59, 0x09, 0x2A, 0x9B, 0x9F, 0x5D, 0xA6, 0x50, 0x32, 0x22, 0xAF,
36 0xC3, 0x64, 0x63, 0x1A, 0x96, 0x10, 0x91, 0x04, 0x21, 0x08, 0xBD, 0x79, 0x40,
37 0x4D, 0x48, 0xD0, 0xF5, 0x82, 0x7A, 0x8F, 0x37, 0x69, 0x86, 0x1D, 0xA4, 0xB9,
38 0xC2, 0xC1, 0xEF, 0x65, 0xF2, 0x05, 0xAB, 0x7E, 0x0B, 0x4A, 0x3B, 0x89, 0xE4,
39 0x6C, 0xBF, 0xE8, 0x8B, 0x06, 0x18, 0x51, 0x14, 0x7F, 0x11, 0x5B, 0x5C, 0xFB,
40 0x97, 0xE1, 0xCF, 0x15, 0x62, 0x71, 0x70, 0x54, 0xE2, 0x12, 0xD6, 0xC7, 0xBB,
41 0x0D, 0x20, 0x5E, 0xDC, 0xE0, 0xD4, 0xF7, 0xCC, 0xC4, 0x2B, 0xF9, 0xEC, 0x2D,
42 0xF4, 0x6F, 0xB6, 0x99, 0x88, 0x81, 0x5A, 0xD9, 0xCA, 0x13, 0xA5, 0xE7, 0x47,
43 0xE6, 0x8E, 0x60, 0xE3, 0x3E, 0xB3, 0xF6, 0x72, 0xA2, 0x35, 0xA0, 0xD7, 0xCD,
44 0xB4, 0x2F, 0x6D, 0x2C, 0x26, 0x1F, 0x95, 0x87, 0x00, 0xD8, 0x34, 0x3F, 0x17,
45 0x25, 0x45, 0x27, 0x75, 0x92, 0xB8, 0xA3, 0xC8, 0xDE, 0xEB, 0xF8, 0xF3, 0xDB,
46 0x0A, 0x98, 0x83, 0x7B, 0xE5, 0xCB, 0x4C, 0x78, 0xD1 };
47
48static BOOL URL_NeedEscape(CHAR ch, DWORD dwFlags)
49{
50
51 if (isalnum(ch))
52 return FALSE;
53
54 if(dwFlags & URL_ESCAPE_SPACES_ONLY) {
55 if(ch == ' ')
56 return TRUE;
57 else
58 return FALSE;
59 }
60
61 if (ch <= 31 || ch >= 127)
62 return TRUE;
63
64 else {
65 switch (ch) {
66 case ' ':
67 case '<':
68 case '>':
69 case '\"':
70 case '{':
71 case '}':
72 case '|':
73 case '\\':
74 case '^':
75 case ']':
76 case '[':
77 case '`':
78 case '&':
79 return TRUE;
80
81 default:
82 return FALSE;
83 }
84 }
85}
86
87/*************************************************************************
88 * UrlCanonicalizeA [SHLWAPI]
89 */
90HRESULT WINAPI UrlCanonicalizeA(LPCSTR pszUrl, LPSTR pszCanonicalized,
91 LPDWORD pcchCanonicalized, DWORD dwFlags)
92{
93 HRESULT hr = S_OK;
94
95 LPSTR lpszUrlCpy;
96 INT nLen;
97
98 TRACE("(%s %p %p 0x%08lx)\n", debugstr_a(pszUrl), pszCanonicalized,
99 pcchCanonicalized, dwFlags);
100
101 nLen = strlen(pszUrl);
102 lpszUrlCpy = HeapAlloc(GetProcessHeap(), 0, nLen + 1);
103
104 if (dwFlags & URL_DONT_SIMPLIFY)
105 memcpy(lpszUrlCpy, pszUrl, nLen + 1);
106 else {
107 FIXME("Simplify path\n");
108 memcpy(lpszUrlCpy, pszUrl, nLen + 1);
109 }
110
111 if(dwFlags & URL_UNESCAPE)
112 UrlUnescapeA(lpszUrlCpy, NULL, NULL, URL_UNESCAPE_INPLACE);
113
114 if(dwFlags & (URL_ESCAPE_UNSAFE | URL_ESCAPE_SPACES_ONLY)) {
115 DWORD EscapeFlags = dwFlags & (URL_ESCAPE_SPACES_ONLY
116 /* | URL_ESCAPE_PERCENT */);
117 hr = UrlEscapeA(lpszUrlCpy, pszCanonicalized, pcchCanonicalized,
118 EscapeFlags);
119 } else { /* No escapping needed, just copy the string */
120 nLen = strlen(lpszUrlCpy);
121 if(nLen < *pcchCanonicalized)
122 memcpy(pszCanonicalized, lpszUrlCpy, nLen + 1);
123 else {
124 hr = E_POINTER;
125 nLen++;
126 }
127 *pcchCanonicalized = nLen;
128 }
129
130 HeapFree(GetProcessHeap(), 0, lpszUrlCpy);
131
132 return hr;
133}
134
135/*************************************************************************
136 * UrlCanonicalizeW [SHLWAPI]
137 */
138HRESULT WINAPI UrlCanonicalizeW(LPCWSTR pszUrl, LPWSTR pszCanonicalized,
139 LPDWORD pcchCanonicalized, DWORD dwFlags)
140{
141 FIXME("(%s %p %p 0x%08lx): stub\n",debugstr_w(pszUrl),
142 pszCanonicalized, pcchCanonicalized, dwFlags);
143 return E_NOTIMPL;
144}
145
146/*************************************************************************
147 * UrlEscapeA [SHLWAPI]
148 *
149 * Converts unsafe characters into their escape sequences.
150 *
151 * The converted string is returned in pszEscaped if the buffer size
152 * (which should be supplied in pcchEscaped) is large enough, in this
153 * case the function returns S_OK and pcchEscaped contains the length
154 * of the escaped string. If the buffer is not large enough the
155 * function returns E_POINTER and pcchEscaped contains the required
156 * buffer size (including room for the '\0').
157 *
158 * By default the function stops converting at the first '?' or
159 * '#'. [MSDN says differently]. If URL_ESCAPE_SPACE_ONLY flag is set
160 * then only spaces are converted, but the conversion continues past a
161 * '?' or '#'.
162 *
163 * BUGS:
164 *
165 * None of the URL_ define values are documented, so they were
166 * determined by trial and error. MSDN mentions URL_ESCAPE_PERCENT
167 * but I can't find a value that does this under win2000.
168 * URL_DONT_ESCAPE_EXTRA_INFO appears to be the default which is what
169 * we assume here. URL_ESCAPE_SEGMENT_ONLY is not implemented
170 * (value??). A value of 0x2000 for dwFlags seems to escape
171 * '/'s too - this is neither documented on MSDN nor implemented here.
172 * For character values that are converted see URL_NeedEscape.
173 */
174HRESULT WINAPI UrlEscapeA(
175 LPCSTR pszUrl,
176 LPSTR pszEscaped,
177 LPDWORD pcchEscaped,
178 DWORD dwFlags)
179{
180 LPCSTR src;
181 DWORD needed = 0, ret;
182 BOOL stop_escapping = FALSE;
183 char next[3], *dst = pszEscaped;
184 char hex[] = "0123456789ABCDEF";
185 INT len;
186
187 TRACE("(%s %p %p 0x%08lx)\n", debugstr_a(pszUrl), pszEscaped,
188 pcchEscaped, dwFlags);
189
190 if(dwFlags & ~URL_ESCAPE_SPACES_ONLY)
191 FIXME("Unimplemented flags: %08lx\n", dwFlags);
192
193 for(src = pszUrl; *src; src++) {
194 if(!(dwFlags & URL_ESCAPE_SPACES_ONLY) &&
195 (*src == '#' || *src == '?'))
196 stop_escapping = TRUE;
197
198 if(URL_NeedEscape(*src, dwFlags) && stop_escapping == FALSE) {
199 next[0] = '%';
200 next[1] = hex[(*src >> 4) & 0xf];
201 next[2] = hex[*src & 0xf];
202 len = 3;
203 } else {
204 next[0] = *src;
205 len = 1;
206 }
207
208 if(needed + len <= *pcchEscaped) {
209 memcpy(dst, next, len);
210 dst += len;
211 }
212 needed += len;
213 }
214
215 if(needed < *pcchEscaped) {
216 *dst = '\0';
217 ret = S_OK;
218 } else {
219 needed++; /* add one for the '\0' */
220 ret = E_POINTER;
221 }
222 *pcchEscaped = needed;
223 return ret;
224}
225
226/*************************************************************************
227 * UrlEscapeW [SHLWAPI]
228 */
229HRESULT WINAPI UrlEscapeW(
230 LPCWSTR pszUrl,
231 LPWSTR pszEscaped,
232 LPDWORD pcchEscaped,
233 DWORD dwFlags)
234{
235 FIXME("(%s %p %p 0x%08lx): stub\n",debugstr_w(pszUrl),
236 pszEscaped, pcchEscaped, dwFlags);
237 return E_NOTIMPL;
238}
239
240
241/*************************************************************************
242 * UrlUnescapeA [SHLWAPI]
243 *
244 * Converts escape sequences back to ordinary characters.
245 *
246 * If URL_ESCAPE_INPLACE is set in dwFlags then pszUnescaped and
247 * pcchUnescaped are ignored and the converted string is returned in
248 * pszUrl, otherwise the string is returned in pszUnescaped.
249 * pcchUnescaped should contain the size of pszUnescaped on calling
250 * and will contain the length the the returned string on return if
251 * the buffer is big enough else it will contain the buffer size
252 * required (including room for the '\0'). The function returns S_OK
253 * on success or E_POINTER if the buffer is not large enough. If the
254 * URL_DONT_ESCAPE_EXTRA_INFO flag is set then the conversion stops at
255 * the first occurrence of either '?' or '#'.
256 *
257 */
258HRESULT WINAPI UrlUnescapeA(
259 LPCSTR pszUrl,
260 LPSTR pszUnescaped,
261 LPDWORD pcchUnescaped,
262 DWORD dwFlags)
263{
264 char *dst, next;
265 LPCSTR src;
266 HRESULT ret;
267 DWORD needed;
268 BOOL stop_unescapping = FALSE;
269
270 TRACE("(%s, %p, %p, %08lx): stub\n", debugstr_a(pszUrl), pszUnescaped,
271 pcchUnescaped, dwFlags);
272
273 if(dwFlags & URL_UNESCAPE_INPLACE)
274 dst = (char*)pszUrl;
275 else
276 dst = pszUnescaped;
277
278 for(src = pszUrl, needed = 0; *src; src++, needed++) {
279 if(dwFlags & URL_DONT_UNESCAPE_EXTRA_INFO &&
280 (*src == '#' || *src == '?')) {
281 stop_unescapping = TRUE;
282 next = *src;
283 } else if(*src == '%' && isxdigit(*(src + 1)) && isxdigit(*(src + 2))
284 && stop_unescapping == FALSE) {
285 INT ih;
286 char buf[3];
287 memcpy(buf, src + 1, 2);
288 buf[2] = '\0';
289 ih = strtol(buf, NULL, 16);
290 next = (CHAR) ih;
291 src += 2; /* Advance to end of escape */
292 } else
293 next = *src;
294
295 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped)
296 *dst++ = next;
297 }
298
299 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped) {
300 *dst = '\0';
301 ret = S_OK;
302 } else {
303 needed++; /* add one for the '\0' */
304 ret = E_POINTER;
305 }
306 if(!(dwFlags & URL_UNESCAPE_INPLACE))
307 *pcchUnescaped = needed;
308
309 return ret;
310}
311
312/*************************************************************************
313 * UrlUnescapeW [SHLWAPI]
314 */
315HRESULT WINAPI UrlUnescapeW(
316 LPCWSTR pszUrl,
317 LPWSTR pszUnescaped,
318 LPDWORD pcchUnescaped,
319 DWORD dwFlags)
320{
321 FIXME("(%s, %p, %p, %08lx): stub\n", debugstr_w(pszUrl), pszUnescaped,
322 pcchUnescaped, dwFlags);
323 return E_NOTIMPL;
324}
325
326/*************************************************************************
327 * HashData [SHLWAPI]
328 *
329 * Hash an input block into a variable sized digest.
330 */
331BOOL WINAPI HashData(const unsigned char *lpSrc, INT nSrcLen,
332 unsigned char *lpDest, INT nDestLen)
333{
334 INT srcCount = nSrcLen - 1, destCount = nDestLen - 1;
335
336 if (IsBadReadPtr(lpSrc, nSrcLen) ||
337 IsBadWritePtr(lpDest, nDestLen))
338 return FALSE;
339
340 while (destCount >= 0)
341 {
342 lpDest[destCount] = (destCount & 0xff);
343 destCount--;
344 }
345
346 while (srcCount >= 0)
347 {
348 destCount = nDestLen - 1;
349 while (destCount >= 0)
350 {
351 lpDest[destCount] = HashDataLookup[lpSrc[srcCount] ^ lpDest[destCount]];
352 destCount--;
353 }
354 srcCount--;
355 }
356 return TRUE;
357}
358
359/*************************************************************************
360 * UrlHashA [SHLWAPI]
361 *
362 * Hash an ASCII URL.
363 */
364HRESULT WINAPI UrlHashA(LPCSTR pszUrl, unsigned char *lpDest, INT nDestLen)
365{
366 if (IsBadStringPtrA(pszUrl, -1) || IsBadWritePtr(lpDest, nDestLen))
367 return E_INVALIDARG;
368
369 HashData(pszUrl, strlen(pszUrl), lpDest, nDestLen);
370 return NOERROR;
371}
372
373/*************************************************************************
374 * UrlApplySchemeW [SHLWAPI.@]
375 */
376HRESULT WINAPI UrlApplySchemeW(LPCWSTR pszIn, LPWSTR pszOut, LPDWORD pcchOut, DWORD dwFlags)
377{
378 HRESULT err = NOERROR;
379 FIXME("(%s %p %p %08lx): stub !\n", debugstr_w(pszIn), pszOut, pcchOut, dwFlags);
380 lstrcpyW(pszOut, pszIn);
381 *pcchOut = (err != E_POINTER) ? lstrlenW(pszOut) : 0;
382 return err;
383}
Note: See TracBrowser for help on using the repository browser.