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

Last change on this file since 7647 was 7647, checked in by sandervl, 24 years ago

Yellow: compile fix

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