quartz: Use proper alloc/free functions for COM objects.
[wine] / dlls / itss / protocol.c
1 /*
2  * Copyright 2006-2007 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
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"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(itss);
37
38 typedef struct {
39     const IInternetProtocolVtbl     *lpInternetProtocolVtbl;
40     const IInternetProtocolInfoVtbl *lpInternetProtocolInfoVtbl;
41
42     LONG ref;
43
44     ULONG offset;
45     struct chmFile *chm_file;
46     struct chmUnitInfo chm_object;
47 } ITSProtocol;
48
49 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
50 #define PROTINFO(x)  ((IInternetProtocolInfo*) &(x)->lpInternetProtocolInfoVtbl)
51
52 static void release_chm(ITSProtocol *This)
53 {
54     if(This->chm_file) {
55         chm_close(This->chm_file);
56         This->chm_file = NULL;
57     }
58     This->offset = 0;
59 }
60
61 #define PROTOCOL_THIS(iface) DEFINE_THIS(ITSProtocol, InternetProtocol, iface)
62
63 static HRESULT WINAPI ITSProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
64 {
65     ITSProtocol *This = PROTOCOL_THIS(iface);
66
67     *ppv = NULL;
68     if(IsEqualGUID(&IID_IUnknown, riid)) {
69         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
70         *ppv = PROTOCOL(This);
71     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
72         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
73         *ppv = PROTOCOL(This);
74     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
75         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
76         *ppv = PROTOCOL(This);
77     }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
78         TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
79         *ppv = PROTINFO(This);
80     }
81
82     if(*ppv) {
83         IInternetProtocol_AddRef(iface);
84         return S_OK;
85     }
86
87     WARN("not supported interface %s\n", debugstr_guid(riid));
88     return E_NOINTERFACE;
89 }
90
91 static ULONG WINAPI ITSProtocol_AddRef(IInternetProtocol *iface)
92 {
93     ITSProtocol *This = PROTOCOL_THIS(iface);
94     LONG ref = InterlockedIncrement(&This->ref);
95     TRACE("(%p) ref=%d\n", This, ref);
96     return ref;
97 }
98
99 static ULONG WINAPI ITSProtocol_Release(IInternetProtocol *iface)
100 {
101     ITSProtocol *This = PROTOCOL_THIS(iface);
102     LONG ref = InterlockedDecrement(&This->ref);
103
104     TRACE("(%p) ref=%d\n", This, ref);
105
106     if(!ref) {
107         release_chm(This);
108         HeapFree(GetProcessHeap(), 0, This);
109
110         ITSS_UnlockModule();
111     }
112
113     return ref;
114 }
115
116 static LPCWSTR skip_schema(LPCWSTR url)
117 {
118     static const WCHAR its_schema[] = {'i','t','s',':'};
119     static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
120     static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
121
122     if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
123         return url+sizeof(its_schema)/sizeof(WCHAR);
124     if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
125         return url+sizeof(msits_schema)/sizeof(WCHAR);
126     if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
127         return url+sizeof(mk_schema)/sizeof(WCHAR);
128
129     return NULL;
130 }
131
132 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres)
133 {
134     IInternetProtocolSink_ReportResult(sink, hres, 0, NULL);
135     return hres;
136 }
137
138 static HRESULT WINAPI ITSProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
139         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
140         DWORD grfPI, DWORD dwReserved)
141 {
142     ITSProtocol *This = PROTOCOL_THIS(iface);
143     BINDINFO bindinfo;
144     DWORD bindf = 0, len;
145     LPWSTR file_name, mime, object_name, p;
146     LPCWSTR ptr;
147     struct chmFile *chm_file;
148     struct chmUnitInfo chm_object;
149     int res;
150     HRESULT hres;
151
152     static const WCHAR separator[] = {':',':',0};
153
154     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
155             pOIBindInfo, grfPI, dwReserved);
156
157     ptr = skip_schema(szUrl);
158     if(!ptr)
159         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
160
161     memset(&bindinfo, 0, sizeof(bindinfo));
162     bindinfo.cbSize = sizeof(BINDINFO);
163     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
164     if(FAILED(hres)) {
165         WARN("GetBindInfo failed: %08x\n", hres);
166         return hres;
167     }
168
169     ReleaseBindInfo(&bindinfo);
170
171     len = strlenW(ptr)+3;
172     file_name = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
173     memcpy(file_name, ptr, len*sizeof(WCHAR));
174     hres = UrlUnescapeW(file_name, NULL, &len, URL_UNESCAPE_INPLACE);
175     if(FAILED(hres)) {
176         WARN("UrlUnescape failed: %08x\n", hres);
177         HeapFree(GetProcessHeap(), 0, file_name);
178         return hres;
179     }
180
181     p = strstrW(file_name, separator);
182     if(!p) {
183         WARN("invalid url\n");
184         HeapFree(GetProcessHeap(), 0, file_name);
185         return report_result(pOIProtSink, STG_E_FILENOTFOUND);
186     }
187
188     *p = 0;
189     chm_file = chm_openW(file_name);
190     if(!chm_file) {
191         WARN("Could not open chm file\n");
192         HeapFree(GetProcessHeap(), 0, file_name);
193         return report_result(pOIProtSink, STG_E_FILENOTFOUND);
194     }
195
196     object_name = p+2;
197     if(*object_name != '/' && *object_name != '\\') {
198         int len = strlenW(object_name)+1;
199         memmove(object_name+1, object_name, len*sizeof(WCHAR));
200         *object_name = '/';
201     }
202
203     for(p=object_name; *p; p++) {
204         if(*p == '\\')
205             *p = '/';
206     }
207
208     TRACE("Resolving %s\n", debugstr_w(object_name));
209
210     memset(&chm_object, 0, sizeof(chm_object));
211     res = chm_resolve_object(chm_file, object_name, &chm_object);
212     if(res != CHM_RESOLVE_SUCCESS) {
213         WARN("Could not resolve chm object\n");
214         HeapFree(GetProcessHeap(), 0, object_name);
215         chm_close(chm_file);
216         return report_result(pOIProtSink, STG_E_FILENOTFOUND);
217     }
218
219     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST,
220                                          strrchrW(object_name, '/')+1);
221     HeapFree(GetProcessHeap(), 0, file_name);
222
223     /* FIXME: Native doesn't use FindMimeFromData */
224     hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
225     if(SUCCEEDED(hres)) {
226         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
227         CoTaskMemFree(mime);
228     }
229
230     release_chm(This); /* Native leaks handle here */
231     This->chm_file = chm_file;
232     memcpy(&This->chm_object, &chm_object, sizeof(chm_object));
233
234     hres = IInternetProtocolSink_ReportData(pOIProtSink,
235             BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE,
236             chm_object.length, chm_object.length);
237     if(FAILED(hres)) {
238         WARN("ReportData failed: %08x\n", hres);
239         release_chm(This);
240         return report_result(pOIProtSink, hres);
241     }
242
243     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
244
245     return report_result(pOIProtSink, hres);
246 }
247
248 static HRESULT WINAPI ITSProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
249 {
250     ITSProtocol *This = PROTOCOL_THIS(iface);
251     FIXME("(%p)->(%p)\n", This, pProtocolData);
252     return E_NOTIMPL;
253 }
254
255 static HRESULT WINAPI ITSProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
256         DWORD dwOptions)
257 {
258     ITSProtocol *This = PROTOCOL_THIS(iface);
259     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
260     return E_NOTIMPL;
261 }
262
263 static HRESULT WINAPI ITSProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
264 {
265     ITSProtocol *This = PROTOCOL_THIS(iface);
266
267     TRACE("(%p)->(%08x)\n", This, dwOptions);
268
269     return S_OK;
270 }
271
272 static HRESULT WINAPI ITSProtocol_Suspend(IInternetProtocol *iface)
273 {
274     ITSProtocol *This = PROTOCOL_THIS(iface);
275     FIXME("(%p)\n", This);
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI ITSProtocol_Resume(IInternetProtocol *iface)
280 {
281     ITSProtocol *This = PROTOCOL_THIS(iface);
282     FIXME("(%p)\n", This);
283     return E_NOTIMPL;
284 }
285
286 static HRESULT WINAPI ITSProtocol_Read(IInternetProtocol *iface, void *pv,
287         ULONG cb, ULONG *pcbRead)
288 {
289     ITSProtocol *This = PROTOCOL_THIS(iface);
290
291     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
292
293     if(!This->chm_file)
294         return INET_E_DATA_NOT_AVAILABLE;
295
296     *pcbRead = chm_retrieve_object(This->chm_file, &This->chm_object, pv, This->offset, cb);
297     This->offset += *pcbRead;
298
299     return *pcbRead ? S_OK : S_FALSE;
300 }
301
302 static HRESULT WINAPI ITSProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
303         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
304 {
305     ITSProtocol *This = PROTOCOL_THIS(iface);
306     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
307     return E_NOTIMPL;
308 }
309
310 static HRESULT WINAPI ITSProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
311 {
312     ITSProtocol *This = PROTOCOL_THIS(iface);
313
314     TRACE("(%p)->(%08x)\n", This, dwOptions);
315
316     return S_OK;
317 }
318
319 static HRESULT WINAPI ITSProtocol_UnlockRequest(IInternetProtocol *iface)
320 {
321     ITSProtocol *This = PROTOCOL_THIS(iface);
322
323     TRACE("(%p)\n", This);
324
325     return S_OK;
326 }
327
328 #undef PROTOCOL_THIS
329
330 static const IInternetProtocolVtbl ITSProtocolVtbl = {
331     ITSProtocol_QueryInterface,
332     ITSProtocol_AddRef,
333     ITSProtocol_Release,
334     ITSProtocol_Start,
335     ITSProtocol_Continue,
336     ITSProtocol_Abort,
337     ITSProtocol_Terminate,
338     ITSProtocol_Suspend,
339     ITSProtocol_Resume,
340     ITSProtocol_Read,
341     ITSProtocol_Seek,
342     ITSProtocol_LockRequest,
343     ITSProtocol_UnlockRequest
344 };
345
346 #define PROTINFO_THIS(iface) DEFINE_THIS(ITSProtocol, InternetProtocolInfo, iface)
347
348 static HRESULT WINAPI ITSProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
349                                               REFIID riid, void **ppv)
350 {
351     ITSProtocol *This = PROTINFO_THIS(iface);
352     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
353 }
354
355 static ULONG WINAPI ITSProtocolInfo_AddRef(IInternetProtocolInfo *iface)
356 {
357     ITSProtocol *This = PROTINFO_THIS(iface);
358     return IInternetProtocol_AddRef(PROTOCOL(This));
359 }
360
361 static ULONG WINAPI ITSProtocolInfo_Release(IInternetProtocolInfo *iface)
362 {
363     ITSProtocol *This = PROTINFO_THIS(iface);
364     return IInternetProtocol_Release(PROTOCOL(This));
365 }
366
367 static HRESULT WINAPI ITSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
368         PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
369         DWORD *pcchResult, DWORD dwReserved)
370 {
371     ITSProtocol *This = PROTINFO_THIS(iface);
372
373     TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), ParseAction,
374           dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
375
376     switch(ParseAction) {
377     case PARSE_CANONICALIZE:
378         FIXME("PARSE_CANONICALIZE\n");
379         return E_NOTIMPL;
380     case PARSE_SECURITY_URL:
381         FIXME("PARSE_SECURITY_URL\n");
382         return E_NOTIMPL;
383     default:
384         return INET_E_DEFAULT_ACTION;
385     }
386
387     return S_OK;
388 }
389
390 static HRESULT WINAPI ITSProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
391         LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
392         DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
393 {
394     ITSProtocol *This = PROTINFO_THIS(iface);
395     LPCWSTR base_end, ptr;
396     DWORD rel_len;
397
398     static const WCHAR separator[] = {':',':',0};
399
400     TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This, debugstr_w(pwzBaseUrl),
401             debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
402             pcchResult, dwReserved);
403
404     base_end = strstrW(pwzBaseUrl, separator);
405     if(!base_end)
406         return 0x80041001;
407     base_end += 2;
408
409     if(!skip_schema(pwzBaseUrl))
410         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
411
412     if(strchrW(pwzRelativeUrl, ':'))
413         return STG_E_INVALIDNAME;
414
415     if(pwzRelativeUrl[0] != '/') {
416         ptr = strrchrW(base_end, '/');
417         if(ptr)
418             base_end = ptr+1;
419         else
420             base_end += strlenW(base_end);
421     }
422
423     rel_len = strlenW(pwzRelativeUrl)+1;
424
425     *pcchResult = rel_len + (base_end-pwzBaseUrl);
426
427     if(*pcchResult > cchResult)
428         return E_OUTOFMEMORY;
429
430     memcpy(pwzResult, pwzBaseUrl, (base_end-pwzBaseUrl)*sizeof(WCHAR));
431     strcpyW(pwzResult + (base_end-pwzBaseUrl), pwzRelativeUrl);
432
433     return S_OK;
434 }
435
436 static HRESULT WINAPI ITSProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
437         LPCWSTR pwzUrl2, DWORD dwCompareFlags)
438 {
439     ITSProtocol *This = PROTINFO_THIS(iface);
440     FIXME("%p)->(%s %s %08x)\n", This, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
441     return E_NOTIMPL;
442 }
443
444 static HRESULT WINAPI ITSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
445         QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
446         DWORD dwReserved)
447 {
448     ITSProtocol *This = PROTINFO_THIS(iface);
449     FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), QueryOption,
450           dwQueryFlags, pBuffer, cbBuffer, pcbBuf, dwReserved);
451     return E_NOTIMPL;
452 }
453
454 #undef PROTINFO_THIS
455
456 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl = {
457     ITSProtocolInfo_QueryInterface,
458     ITSProtocolInfo_AddRef,
459     ITSProtocolInfo_Release,
460     ITSProtocolInfo_ParseUrl,
461     ITSProtocolInfo_CombineUrl,
462     ITSProtocolInfo_CompareUrl,
463     ITSProtocolInfo_QueryInfo
464 };
465
466 HRESULT ITSProtocol_create(IUnknown *pUnkOuter, LPVOID *ppobj)
467 {
468     ITSProtocol *ret;
469
470     TRACE("(%p %p)\n", pUnkOuter, ppobj);
471
472     ITSS_LockModule();
473
474     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITSProtocol));
475
476     ret->lpInternetProtocolVtbl = &ITSProtocolVtbl;
477     ret->lpInternetProtocolInfoVtbl = &ITSProtocolInfoVtbl;
478     ret->ref = 1;
479
480     *ppobj = PROTOCOL(ret);
481
482     return S_OK;
483 }