winedbg: Don't dereference possibly NULL thread pointer.
[wine/zf.git] / dlls / itss / protocol.c
blob6d40ddf738f538f6622dad5d960ade9fd531d5eb
1 /*
2 * Copyright 2006-2007 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "urlmon.h"
29 #include "shlwapi.h"
30 #include "itsstor.h"
31 #include "chm_lib.h"
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(itss);
37 typedef struct {
38 IUnknown IUnknown_inner;
39 IInternetProtocol IInternetProtocol_iface;
40 IInternetProtocolInfo IInternetProtocolInfo_iface;
42 LONG ref;
43 IUnknown *outer;
45 ULONG offset;
46 struct chmFile *chm_file;
47 struct chmUnitInfo chm_object;
48 } ITSProtocol;
50 static inline ITSProtocol *impl_from_IUnknown(IUnknown *iface)
52 return CONTAINING_RECORD(iface, ITSProtocol, IUnknown_inner);
55 static inline ITSProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
57 return CONTAINING_RECORD(iface, ITSProtocol, IInternetProtocol_iface);
60 static inline ITSProtocol *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
62 return CONTAINING_RECORD(iface, ITSProtocol, IInternetProtocolInfo_iface);
65 static void release_chm(ITSProtocol *This)
67 if(This->chm_file) {
68 chm_close(This->chm_file);
69 This->chm_file = NULL;
71 This->offset = 0;
74 static HRESULT WINAPI ITSProtocol_QueryInterface(IUnknown *iface, REFIID riid, void **ppv)
76 ITSProtocol *This = impl_from_IUnknown(iface);
78 if(IsEqualGUID(&IID_IUnknown, riid)) {
79 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
80 *ppv = &This->IUnknown_inner;
81 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
82 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
83 *ppv = &This->IInternetProtocol_iface;
84 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
85 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
86 *ppv = &This->IInternetProtocol_iface;
87 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
88 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
89 *ppv = &This->IInternetProtocolInfo_iface;
90 }else {
91 *ppv = NULL;
92 WARN("not supported interface %s\n", debugstr_guid(riid));
93 return E_NOINTERFACE;
96 IUnknown_AddRef((IUnknown*)*ppv);
97 return S_OK;
100 static ULONG WINAPI ITSProtocol_AddRef(IUnknown *iface)
102 ITSProtocol *This = impl_from_IUnknown(iface);
103 LONG ref = InterlockedIncrement(&This->ref);
104 TRACE("(%p) ref=%d\n", This, ref);
105 return ref;
108 static ULONG WINAPI ITSProtocol_Release(IUnknown *iface)
110 ITSProtocol *This = impl_from_IUnknown(iface);
111 LONG ref = InterlockedDecrement(&This->ref);
113 TRACE("(%p) ref=%d\n", This, ref);
115 if(!ref) {
116 release_chm(This);
117 HeapFree(GetProcessHeap(), 0, This);
119 ITSS_UnlockModule();
122 return ref;
125 static const IUnknownVtbl ITSProtocolUnkVtbl = {
126 ITSProtocol_QueryInterface,
127 ITSProtocol_AddRef,
128 ITSProtocol_Release
131 static HRESULT WINAPI ITSInternetProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
133 ITSProtocol *This = impl_from_IInternetProtocol(iface);
134 return IUnknown_QueryInterface(This->outer, riid, ppv);
137 static ULONG WINAPI ITSInternetProtocol_AddRef(IInternetProtocol *iface)
139 ITSProtocol *This = impl_from_IInternetProtocol(iface);
140 return IUnknown_AddRef(This->outer);
143 static ULONG WINAPI ITSInternetProtocol_Release(IInternetProtocol *iface)
145 ITSProtocol *This = impl_from_IInternetProtocol(iface);
146 return IUnknown_Release(This->outer);
149 static LPCWSTR skip_schema(LPCWSTR url)
151 static const WCHAR its_schema[] = L"its:";
152 static const WCHAR msits_schema[] = L"ms-its:";
153 static const WCHAR mk_schema[] = L"mk:@MSITStore:";
155 if(!wcsnicmp(its_schema, url, ARRAY_SIZE(its_schema) - 1))
156 return url + ARRAY_SIZE(its_schema) - 1;
157 if(!wcsnicmp(msits_schema, url, ARRAY_SIZE(msits_schema) - 1))
158 return url + ARRAY_SIZE(msits_schema) - 1;
159 if(!wcsnicmp(mk_schema, url, ARRAY_SIZE(mk_schema) - 1))
160 return url + ARRAY_SIZE(mk_schema) - 1;
162 return NULL;
165 /* Adopted from urlmon */
166 static void remove_dot_segments(WCHAR *path) {
167 const WCHAR *in = path;
168 WCHAR *out = path;
170 while(1) {
171 /* Move the first path segment in the input buffer to the end of
172 * the output buffer, and any subsequent characters up to, including
173 * the next "/" character (if any) or the end of the input buffer.
175 while(*in != '/') {
176 if(!(*out++ = *in++))
177 return;
180 *out++ = *in++;
182 while(*in) {
183 if(*in != '.')
184 break;
186 /* Handle ending "/." */
187 if(!in[1]) {
188 ++in;
189 break;
192 /* Handle "/./" */
193 if(in[1] == '/') {
194 in += 2;
195 continue;
198 /* If we don't have "/../" or ending "/.." */
199 if(in[1] != '.' || (in[2] && in[2] != '/'))
200 break;
202 in += *in ? 3 : 2;
204 /* Find the slash preceding out pointer and move out pointer to it */
205 if(out > path+1 && *--out == '/')
206 --out;
207 while(out > path && *(--out) != '/');
208 if(*out == '/')
209 ++out;
214 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres)
216 IInternetProtocolSink_ReportResult(sink, hres, 0, NULL);
217 return hres;
220 static HRESULT WINAPI ITSProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
221 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
222 DWORD grfPI, HANDLE_PTR dwReserved)
224 ITSProtocol *This = impl_from_IInternetProtocol(iface);
225 BINDINFO bindinfo;
226 DWORD bindf = 0, len;
227 LPWSTR file_name, mime, object_name, p;
228 LPCWSTR ptr;
229 struct chmFile *chm_file;
230 struct chmUnitInfo chm_object;
231 int res;
232 HRESULT hres;
234 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
235 pOIBindInfo, grfPI, dwReserved);
237 ptr = skip_schema(szUrl);
238 if(!ptr)
239 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
241 memset(&bindinfo, 0, sizeof(bindinfo));
242 bindinfo.cbSize = sizeof(BINDINFO);
243 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
244 if(FAILED(hres)) {
245 WARN("GetBindInfo failed: %08x\n", hres);
246 return hres;
249 ReleaseBindInfo(&bindinfo);
251 len = lstrlenW(ptr)+3;
252 file_name = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
253 memcpy(file_name, ptr, len*sizeof(WCHAR));
254 hres = UrlUnescapeW(file_name, NULL, &len, URL_UNESCAPE_INPLACE);
255 if(FAILED(hres)) {
256 WARN("UrlUnescape failed: %08x\n", hres);
257 HeapFree(GetProcessHeap(), 0, file_name);
258 return hres;
261 p = wcsstr(file_name, L"::");
262 if(!p) {
263 WARN("invalid url\n");
264 HeapFree(GetProcessHeap(), 0, file_name);
265 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
268 *p = 0;
269 chm_file = chm_openW(file_name);
270 if(!chm_file) {
271 WARN("Could not open chm file\n");
272 HeapFree(GetProcessHeap(), 0, file_name);
273 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
276 object_name = p+2;
277 len = lstrlenW(object_name);
279 if(*object_name != '/' && *object_name != '\\') {
280 memmove(object_name+1, object_name, (len+1)*sizeof(WCHAR));
281 *object_name = '/';
282 len++;
285 if(object_name[len-1] == '/')
286 object_name[--len] = 0;
288 for(p=object_name; *p; p++) {
289 if(*p == '\\')
290 *p = '/';
293 remove_dot_segments(object_name);
295 TRACE("Resolving %s\n", debugstr_w(object_name));
297 memset(&chm_object, 0, sizeof(chm_object));
298 res = chm_resolve_object(chm_file, object_name, &chm_object);
299 if(res != CHM_RESOLVE_SUCCESS) {
300 WARN("Could not resolve chm object\n");
301 HeapFree(GetProcessHeap(), 0, file_name);
302 chm_close(chm_file);
303 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
306 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST,
307 wcsrchr(object_name, '/')+1);
309 /* FIXME: Native doesn't use FindMimeFromData */
310 hres = FindMimeFromData(NULL, object_name, NULL, 0, NULL, 0, &mime, 0);
311 HeapFree(GetProcessHeap(), 0, file_name);
312 if(SUCCEEDED(hres)) {
313 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
314 CoTaskMemFree(mime);
317 release_chm(This); /* Native leaks handle here */
318 This->chm_file = chm_file;
319 This->chm_object = chm_object;
321 hres = IInternetProtocolSink_ReportData(pOIProtSink,
322 BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE,
323 chm_object.length, chm_object.length);
324 if(FAILED(hres)) {
325 WARN("ReportData failed: %08x\n", hres);
326 release_chm(This);
327 return report_result(pOIProtSink, hres);
330 hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
332 return report_result(pOIProtSink, hres);
335 static HRESULT WINAPI ITSProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
337 ITSProtocol *This = impl_from_IInternetProtocol(iface);
338 FIXME("(%p)->(%p)\n", This, pProtocolData);
339 return E_NOTIMPL;
342 static HRESULT WINAPI ITSProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
343 DWORD dwOptions)
345 ITSProtocol *This = impl_from_IInternetProtocol(iface);
346 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
347 return E_NOTIMPL;
350 static HRESULT WINAPI ITSProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
352 ITSProtocol *This = impl_from_IInternetProtocol(iface);
354 TRACE("(%p)->(%08x)\n", This, dwOptions);
356 return S_OK;
359 static HRESULT WINAPI ITSProtocol_Suspend(IInternetProtocol *iface)
361 ITSProtocol *This = impl_from_IInternetProtocol(iface);
362 FIXME("(%p)\n", This);
363 return E_NOTIMPL;
366 static HRESULT WINAPI ITSProtocol_Resume(IInternetProtocol *iface)
368 ITSProtocol *This = impl_from_IInternetProtocol(iface);
369 FIXME("(%p)\n", This);
370 return E_NOTIMPL;
373 static HRESULT WINAPI ITSProtocol_Read(IInternetProtocol *iface, void *pv,
374 ULONG cb, ULONG *pcbRead)
376 ITSProtocol *This = impl_from_IInternetProtocol(iface);
378 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
380 if(!This->chm_file)
381 return INET_E_DATA_NOT_AVAILABLE;
383 *pcbRead = chm_retrieve_object(This->chm_file, &This->chm_object, pv, This->offset, cb);
384 This->offset += *pcbRead;
386 return *pcbRead ? S_OK : S_FALSE;
389 static HRESULT WINAPI ITSProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
390 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
392 ITSProtocol *This = impl_from_IInternetProtocol(iface);
393 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
394 return E_NOTIMPL;
397 static HRESULT WINAPI ITSProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
399 ITSProtocol *This = impl_from_IInternetProtocol(iface);
401 TRACE("(%p)->(%08x)\n", This, dwOptions);
403 return S_OK;
406 static HRESULT WINAPI ITSProtocol_UnlockRequest(IInternetProtocol *iface)
408 ITSProtocol *This = impl_from_IInternetProtocol(iface);
410 TRACE("(%p)\n", This);
412 return S_OK;
415 static const IInternetProtocolVtbl ITSProtocolVtbl = {
416 ITSInternetProtocol_QueryInterface,
417 ITSInternetProtocol_AddRef,
418 ITSInternetProtocol_Release,
419 ITSProtocol_Start,
420 ITSProtocol_Continue,
421 ITSProtocol_Abort,
422 ITSProtocol_Terminate,
423 ITSProtocol_Suspend,
424 ITSProtocol_Resume,
425 ITSProtocol_Read,
426 ITSProtocol_Seek,
427 ITSProtocol_LockRequest,
428 ITSProtocol_UnlockRequest
431 static HRESULT WINAPI ITSProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
432 REFIID riid, void **ppv)
434 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
435 return IInternetProtocol_QueryInterface(&This->IInternetProtocol_iface, riid, ppv);
438 static ULONG WINAPI ITSProtocolInfo_AddRef(IInternetProtocolInfo *iface)
440 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
441 return IInternetProtocol_AddRef(&This->IInternetProtocol_iface);
444 static ULONG WINAPI ITSProtocolInfo_Release(IInternetProtocolInfo *iface)
446 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
447 return IInternetProtocol_Release(&This->IInternetProtocol_iface);
450 static HRESULT WINAPI ITSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
451 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
452 DWORD *pcchResult, DWORD dwReserved)
454 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
456 TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), ParseAction,
457 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
459 switch(ParseAction) {
460 case PARSE_CANONICALIZE:
461 FIXME("PARSE_CANONICALIZE\n");
462 return E_NOTIMPL;
463 case PARSE_SECURITY_URL:
464 FIXME("PARSE_SECURITY_URL\n");
465 return E_NOTIMPL;
466 default:
467 return INET_E_DEFAULT_ACTION;
470 return S_OK;
473 static HRESULT WINAPI ITSProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
474 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
475 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
477 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
478 LPCWSTR base_end, ptr;
479 DWORD rel_len;
481 TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This, debugstr_w(pwzBaseUrl),
482 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
483 pcchResult, dwReserved);
485 base_end = wcsstr(pwzBaseUrl, L"::");
486 if(!base_end)
487 return 0x80041001;
488 base_end += 2;
490 if(!skip_schema(pwzBaseUrl))
491 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
493 if(wcschr(pwzRelativeUrl, ':'))
494 return STG_E_INVALIDNAME;
496 if(pwzRelativeUrl[0] == '#') {
497 base_end += lstrlenW(base_end);
498 }else if(pwzRelativeUrl[0] != '/') {
499 ptr = wcsrchr(base_end, '/');
500 if(ptr)
501 base_end = ptr+1;
502 else
503 base_end += lstrlenW(base_end);
506 rel_len = lstrlenW(pwzRelativeUrl)+1;
508 *pcchResult = rel_len + (base_end-pwzBaseUrl);
510 if(*pcchResult > cchResult)
511 return E_OUTOFMEMORY;
513 memcpy(pwzResult, pwzBaseUrl, (base_end-pwzBaseUrl)*sizeof(WCHAR));
514 lstrcpyW(pwzResult + (base_end-pwzBaseUrl), pwzRelativeUrl);
516 return S_OK;
519 static HRESULT WINAPI ITSProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
520 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
522 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
523 FIXME("%p)->(%s %s %08x)\n", This, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
524 return E_NOTIMPL;
527 static HRESULT WINAPI ITSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
528 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
529 DWORD dwReserved)
531 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
532 FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), QueryOption,
533 dwQueryFlags, pBuffer, cbBuffer, pcbBuf, dwReserved);
534 return E_NOTIMPL;
537 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl = {
538 ITSProtocolInfo_QueryInterface,
539 ITSProtocolInfo_AddRef,
540 ITSProtocolInfo_Release,
541 ITSProtocolInfo_ParseUrl,
542 ITSProtocolInfo_CombineUrl,
543 ITSProtocolInfo_CompareUrl,
544 ITSProtocolInfo_QueryInfo
547 HRESULT ITSProtocol_create(IUnknown *outer, void **ppv)
549 ITSProtocol *ret;
551 TRACE("(%p %p)\n", outer, ppv);
553 ITSS_LockModule();
555 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITSProtocol));
556 if(!ret)
557 return E_OUTOFMEMORY;
559 ret->IUnknown_inner.lpVtbl = &ITSProtocolUnkVtbl;
560 ret->IInternetProtocol_iface.lpVtbl = &ITSProtocolVtbl;
561 ret->IInternetProtocolInfo_iface.lpVtbl = &ITSProtocolInfoVtbl;
562 ret->ref = 1;
563 ret->outer = outer ? outer : &ret->IUnknown_inner;
565 *ppv = &ret->IUnknown_inner;
566 return S_OK;