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

Last change on this file since 7502 was 7502, checked in by phaller, 24 years ago

Fixed out-of-scope FIXME,TRACE,WARN macros

File size: 11.1 KB
Line 
1/*
2 * Url functions
3 *
4 * Copyright 2000 Huw D M Davies for CodeWeavers.
5 */
6
7#include <string.h>
8#include "windef.h"
9#include "winbase.h"
10#include "winerror.h"
11#include "shlwapi.h"
12#include "debugtools.h"
13
14#ifdef __WIN32OS2__
15#undef FIXME
16#ifdef DEBUG
17// PH 2001-11-30
18// this macro definition causes the control leave the scope of a
19// non-curly-braced preceeding if statement. Therefore,
20// if (p!=NULL)
21// TRACE("p->a=%d", p->a)
22// crashes.
23//
24// !!! ENSURE TRACES AND FIXMES WITH PRECEEDING IF STATEMENT
25// !!! ARE PUT INTO CURLY BRACES
26#define FIXME WriteLog("FIXME %s", __FUNCTION__); WriteLog
27#else
28#define FIXME 1 ? (void)0 : (void)((int (*)(char *, ...)) NULL)
29#endif
30#endif
31
32DEFAULT_DEBUG_CHANNEL(shell);
33
34static const unsigned char HashDataLookup[256] = {
35 0x01, 0x0E, 0x6E, 0x19, 0x61, 0xAE, 0x84, 0x77, 0x8A, 0xAA, 0x7D, 0x76, 0x1B,
36 0xE9, 0x8C, 0x33, 0x57, 0xC5, 0xB1, 0x6B, 0xEA, 0xA9, 0x38, 0x44, 0x1E, 0x07,
37 0xAD, 0x49, 0xBC, 0x28, 0x24, 0x41, 0x31, 0xD5, 0x68, 0xBE, 0x39, 0xD3, 0x94,
38 0xDF, 0x30, 0x73, 0x0F, 0x02, 0x43, 0xBA, 0xD2, 0x1C, 0x0C, 0xB5, 0x67, 0x46,
39 0x16, 0x3A, 0x4B, 0x4E, 0xB7, 0xA7, 0xEE, 0x9D, 0x7C, 0x93, 0xAC, 0x90, 0xB0,
40 0xA1, 0x8D, 0x56, 0x3C, 0x42, 0x80, 0x53, 0x9C, 0xF1, 0x4F, 0x2E, 0xA8, 0xC6,
41 0x29, 0xFE, 0xB2, 0x55, 0xFD,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DWORD dwFlags)
57{
58
59 if (isalnum(ch))
60 return FALSE;
61
62 if(dwFlags & URL_ESCAPE_SPACES_ONLY) {
63 if(ch == ' ')
64 return TRUE;
65 else
66 return FALSE;
67 }
68
69 if (ch <= 31 || ch >= 127)
70 return TRUE;
71
72 else {
73 switch (ch) {
74 case ' ':
75 case '<':
76 case '>':
77 case '\"':
78 case '{':
79 case '}':
80 case '|':
81 case '\\':
82 case '^':
83 case ']':
84 case '[':
85 case '`':
86 case '&':
87 return TRUE;
88
89 default:
90 return FALSE;
91 }
92 }
93}
94
95/*************************************************************************
96 * UrlCanonicalizeA [SHLWAPI]
97 */
98HRESULT WINAPI UrlCanonicalizeA(LPCSTR pszUrl, LPSTR pszCanonicalized,
99 LPDWORD pcchCanonicalized, DWORD dwFlags)
100{
101 HRESULT hr = S_OK;
102
103 LPSTR lpszUrlCpy;
104 INT nLen;
105
106 TRACE("(%s %p %p 0x%08lx)\n", debugstr_a(pszUrl), pszCanonicalized,
107 pcchCanonicalized, dwFlags);
108
109 nLen = strlen(pszUrl);
110 lpszUrlCpy = HeapAlloc(GetProcessHeap(), 0, nLen + 1);
111
112 if (dwFlags & URL_DONT_SIMPLIFY)
113 memcpy(lpszUrlCpy, pszUrl, nLen + 1);
114 else {
115 FIXME("Simplify path\n");
116 memcpy(lpszUrlCpy, pszUrl, nLen + 1);
117 }
118
119 if(dwFlags & URL_UNESCAPE)
120 UrlUnescapeA(lpszUrlCpy, NULL, NULL, URL_UNESCAPE_INPLACE);
121
122 if(dwFlags & (URL_ESCAPE_UNSAFE | URL_ESCAPE_SPACES_ONLY)) {
123 DWORD EscapeFlags = dwFlags & (URL_ESCAPE_SPACES_ONLY
124 /* | URL_ESCAPE_PERCENT */);
125 hr = UrlEscapeA(lpszUrlCpy, pszCanonicalized, pcchCanonicalized,
126 EscapeFlags);
127 } else { /* No escapping needed, just copy the string */
128 nLen = strlen(lpszUrlCpy);
129 if(nLen < *pcchCanonicalized)
130 memcpy(pszCanonicalized, lpszUrlCpy, nLen + 1);
131 else {
132 hr = E_POINTER;
133 nLen++;
134 }
135 *pcchCanonicalized = nLen;
136 }
137
138 HeapFree(GetProcessHeap(), 0, lpszUrlCpy);
139
140 return hr;
141}
142
143/*************************************************************************
144 * UrlCanonicalizeW [SHLWAPI]
145 */
146HRESULT WINAPI UrlCanonicalizeW(LPCWSTR pszUrl, LPWSTR pszCanonicalized,
147 LPDWORD pcchCanonicalized, DWORD dwFlags)
148{
149 FIXME("(%s %p %p 0x%08lx): stub\n",debugstr_w(pszUrl),
150 pszCanonicalized, pcchCanonicalized, dwFlags);
151 return E_NOTIMPL;
152}
153
154/*************************************************************************
155 * UrlEscapeA [SHLWAPI]
156 *
157 * Converts unsafe characters into their escape sequences.
158 *
159 * The converted string is returned in pszEscaped if the buffer size
160 * (which should be supplied in pcchEscaped) is large enough, in this
161 * case the function returns S_OK and pcchEscaped contains the length
162 * of the escaped string. If the buffer is not large enough the
163 * function returns E_POINTER and pcchEscaped contains the required
164 * buffer size (including room for the '\0').
165 *
166 * By default the function stops converting at the first '?' or
167 * '#'. [MSDN says differently]. If URL_ESCAPE_SPACE_ONLY flag is set
168 * then only spaces are converted, but the conversion continues past a
169 * '?' or '#'.
170 *
171 * BUGS:
172 *
173 * None of the URL_ define values are documented, so they were
174 * determined by trial and error. MSDN mentions URL_ESCAPE_PERCENT
175 * but I can't find a value that does this under win2000.
176 * URL_DONT_ESCAPE_EXTRA_INFO appears to be the default which is what
177 * we assume here. URL_ESCAPE_SEGMENT_ONLY is not implemented
178 * (value??). A value of 0x2000 for dwFlags seems to escape
179 * '/'s too - this is neither documented on MSDN nor implemented here.
180 * For character values that are converted see URL_NeedEscape.
181 */
182HRESULT WINAPI UrlEscapeA(
183 LPCSTR pszUrl,
184 LPSTR pszEscaped,
185 LPDWORD pcchEscaped,
186 DWORD dwFlags)
187{
188 LPCSTR src;
189 DWORD needed = 0, ret;
190 BOOL stop_escapping = FALSE;
191 char next[3], *dst = pszEscaped;
192 char hex[] = "0123456789ABCDEF";
193 INT len;
194
195 TRACE("(%s %p %p 0x%08lx)\n", debugstr_a(pszUrl), pszEscaped,
196 pcchEscaped, dwFlags);
197
198 if(dwFlags & ~URL_ESCAPE_SPACES_ONLY)
199 {
200 FIXME("Unimplemented flags: %08lx\n", dwFlags);
201 }
202
203 for(src = pszUrl; *src; src++) {
204 if(!(dwFlags & URL_ESCAPE_SPACES_ONLY) &&
205 (*src == '#' || *src == '?'))
206 stop_escapping = TRUE;
207
208 if(URL_NeedEscape(*src, dwFlags) && stop_escapping == FALSE) {
209 next[0] = '%';
210 next[1] = hex[(*src >> 4) & 0xf];
211 next[2] = hex[*src & 0xf];
212 len = 3;
213 } else {
214 next[0] = *src;
215 len = 1;
216 }
217
218 if(needed + len <= *pcchEscaped) {
219 memcpy(dst, next, len);
220 dst += len;
221 }
222 needed += len;
223 }
224
225 if(needed < *pcchEscaped) {
226 *dst = '\0';
227 ret = S_OK;
228 } else {
229 needed++; /* add one for the '\0' */
230 ret = E_POINTER;
231 }
232 *pcchEscaped = needed;
233 return ret;
234}
235
236/*************************************************************************
237 * UrlEscapeW [SHLWAPI]
238 */
239HRESULT WINAPI UrlEscapeW(
240 LPCWSTR pszUrl,
241 LPWSTR pszEscaped,
242 LPDWORD pcchEscaped,
243 DWORD dwFlags)
244{
245 FIXME("(%s %p %p 0x%08lx): stub\n",debugstr_w(pszUrl),
246 pszEscaped, pcchEscaped, dwFlags);
247 return E_NOTIMPL;
248}
249
250
251/*************************************************************************
252 * UrlUnescapeA [SHLWAPI]
253 *
254 * Converts escape sequences back to ordinary characters.
255 *
256 * If URL_ESCAPE_INPLACE is set in dwFlags then pszUnescaped and
257 * pcchUnescaped are ignored and the converted string is returned in
258 * pszUrl, otherwise the string is returned in pszUnescaped.
259 * pcchUnescaped should contain the size of pszUnescaped on calling
260 * and will contain the length the the returned string on return if
261 * the buffer is big enough else it will contain the buffer size
262 * required (including room for the '\0'). The function returns S_OK
263 * on success or E_POINTER if the buffer is not large enough. If the
264 * URL_DONT_ESCAPE_EXTRA_INFO flag is set then the conversion stops at
265 * the first occurrence of either '?' or '#'.
266 *
267 */
268HRESULT WINAPI UrlUnescapeA(
269 LPCSTR pszUrl,
270 LPSTR pszUnescaped,
271 LPDWORD pcchUnescaped,
272 DWORD dwFlags)
273{
274 char *dst, next;
275 LPCSTR src;
276 HRESULT ret;
277 DWORD needed;
278 BOOL stop_unescapping = FALSE;
279
280 TRACE("(%s, %p, %p, %08lx): stub\n", debugstr_a(pszUrl), pszUnescaped,
281 pcchUnescaped, dwFlags);
282
283 if(dwFlags & URL_UNESCAPE_INPLACE)
284 dst = (char*)pszUrl;
285 else
286 dst = pszUnescaped;
287
288 for(src = pszUrl, needed = 0; *src; src++, needed++) {
289 if(dwFlags & URL_DONT_UNESCAPE_EXTRA_INFO &&
290 (*src == '#' || *src == '?')) {
291 stop_unescapping = TRUE;
292 next = *src;
293 } else if(*src == '%' && isxdigit(*(src + 1)) && isxdigit(*(src + 2))
294 && stop_unescapping == FALSE) {
295 INT ih;
296 char buf[3];
297 memcpy(buf, src + 1, 2);
298 buf[2] = '\0';
299 ih = strtol(buf, NULL, 16);
300 next = (CHAR) ih;
301 src += 2; /* Advance to end of escape */
302 } else
303 next = *src;
304
305 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped)
306 *dst++ = next;
307 }
308
309 if(dwFlags & URL_UNESCAPE_INPLACE || needed < *pcchUnescaped) {
310 *dst = '\0';
311 ret = S_OK;
312 } else {
313 needed++; /* add one for the '\0' */
314 ret = E_POINTER;
315 }
316 if(!(dwFlags & URL_UNESCAPE_INPLACE))
317 *pcchUnescaped = needed;
318
319 return ret;
320}
321
322/*************************************************************************
323 * UrlUnescapeW [SHLWAPI]
324 */
325HRESULT WINAPI UrlUnescapeW(
326 LPCWSTR pszUrl,
327 LPWSTR pszUnescaped,
328 LPDWORD pcchUnescaped,
329 DWORD dwFlags)
330{
331 FIXME("(%s, %p, %p, %08lx): stub\n", debugstr_w(pszUrl), pszUnescaped,
332 pcchUnescaped, dwFlags);
333 return E_NOTIMPL;
334}
335
336/*************************************************************************
337 * HashData [SHLWAPI]
338 *
339 * Hash an input block into a variable sized digest.
340 */
341BOOL WINAPI HashData(const unsigned char *lpSrc, INT nSrcLen,
342 unsigned char *lpDest, INT nDestLen)
343{
344 INT srcCount = nSrcLen - 1, destCount = nDestLen - 1;
345
346 if (IsBadReadPtr(lpSrc, nSrcLen) ||
347 IsBadWritePtr(lpDest, nDestLen))
348 return FALSE;
349
350 while (destCount >= 0)
351 {
352 lpDest[destCount] = (destCount & 0xff);
353 destCount--;
354 }
355
356 while (srcCount >= 0)
357 {
358 destCount = nDestLen - 1;
359 while (destCount >= 0)
360 {
361 lpDest[destCount] = HashDataLookup[lpSrc[srcCount] ^ lpDest[destCount]];
362 destCount--;
363 }
364 srcCount--;
365 }
366 return TRUE;
367}
368
369/*************************************************************************
370 * UrlHashA [SHLWAPI]
371 *
372 * Hash an ASCII URL.
373 */
374HRESULT WINAPI UrlHashA(LPCSTR pszUrl, unsigned char *lpDest, INT nDestLen)
375{
376 if (IsBadStringPtrA(pszUrl, -1) || IsBadWritePtr(lpDest, nDestLen))
377 return E_INVALIDARG;
378
379 HashData(pszUrl, strlen(pszUrl), lpDest, nDestLen);
380 return NOERROR;
381}
382
383/*************************************************************************
384 * UrlApplySchemeW [SHLWAPI.@]
385 */
386HRESULT WINAPI UrlApplySchemeW(LPCWSTR pszIn, LPWSTR pszOut, LPDWORD pcchOut, DWORD dwFlags)
387{
388 HRESULT err = NOERROR;
389 FIXME("(%s %p %p %08lx): stub !\n", debugstr_w(pszIn), pszOut, pcchOut, dwFlags);
390 lstrcpyW(pszOut, pszIn);
391 *pcchOut = (err != E_POINTER) ? lstrlenW(pszOut) : 0;
392 return err;
393}
Note: See TracBrowser for help on using the repository browser.