itss: Declare a function static.
[wine] / dlls / itss / protocol.c
1 /*
2  * Copyright 2006 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 "ole2.h"
27 #include "urlmon.h"
28 #include "itsstor.h"
29 #include "chm_lib.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(itss);
35
36 typedef struct {
37     const IInternetProtocolVtbl  *lpInternetProtocolVtbl;
38
39     LONG ref;
40
41     ULONG offset;
42     struct chmFile *chm_file;
43     struct chmUnitInfo chm_object;
44 } ITSProtocol;
45
46 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
47
48 static void release_chm(ITSProtocol *This)
49 {
50     if(This->chm_file) {
51         chm_close(This->chm_file);
52         This->chm_file = NULL;
53     }
54     This->offset = 0;
55 }
56
57 #define PROTOCOL_THIS(iface) DEFINE_THIS(ITSProtocol, InternetProtocol, iface)
58
59 static HRESULT WINAPI ITSProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
60 {
61     ITSProtocol *This = PROTOCOL_THIS(iface);
62
63     *ppv = NULL;
64     if(IsEqualGUID(&IID_IUnknown, riid)) {
65         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
66         *ppv = PROTOCOL(This);
67     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
68         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
69         *ppv = PROTOCOL(This);
70     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
71         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
72         *ppv = PROTOCOL(This);
73     }
74
75     if(*ppv) {
76         IInternetProtocol_AddRef(iface);
77         return S_OK;
78     }
79
80     WARN("not supported interface %s\n", debugstr_guid(riid));
81     return E_NOINTERFACE;
82 }
83
84 static ULONG WINAPI ITSProtocol_AddRef(IInternetProtocol *iface)
85 {
86     ITSProtocol *This = PROTOCOL_THIS(iface);
87     LONG ref = InterlockedIncrement(&This->ref);
88     TRACE("(%p) ref=%d\n", This, ref);
89     return ref;
90 }
91
92 static ULONG WINAPI ITSProtocol_Release(IInternetProtocol *iface)
93 {
94     ITSProtocol *This = PROTOCOL_THIS(iface);
95     LONG ref = InterlockedDecrement(&This->ref);
96
97     TRACE("(%p) ref=%d\n", This, ref);
98
99     if(!ref) {
100         release_chm(This);
101         HeapFree(GetProcessHeap(), 0, This);
102
103         ITSS_UnlockModule();
104     }
105
106     return ref;
107 }
108
109 static LPCWSTR skip_schema(LPCWSTR url)
110 {
111     static const WCHAR its_schema[] = {'i','t','s',':'};
112     static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
113     static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
114
115     if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
116         return url+sizeof(its_schema)/sizeof(WCHAR);
117     if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
118         return url+sizeof(msits_schema)/sizeof(WCHAR);
119     if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
120         return url+sizeof(mk_schema)/sizeof(WCHAR);
121
122     return NULL;
123 }
124
125 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres)
126 {
127     IInternetProtocolSink_ReportResult(sink, hres, 0, NULL);
128     return hres;
129 }
130
131 static HRESULT WINAPI ITSProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
132         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
133         DWORD grfPI, DWORD dwReserved)
134 {
135     ITSProtocol *This = PROTOCOL_THIS(iface);
136     BINDINFO bindinfo;
137     DWORD bindf = 0, len;
138     LPWSTR file_name, mime;
139     LPCWSTR object_name, path;
140     struct chmFile *chm_file;
141     struct chmUnitInfo chm_object;
142     int res;
143     HRESULT hres;
144
145     static const WCHAR separator[] = {':',':',0};
146
147     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
148             pOIBindInfo, grfPI, dwReserved);
149
150     path = skip_schema(szUrl);
151     if(!path)
152         return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
153
154     memset(&bindinfo, 0, sizeof(bindinfo));
155     bindinfo.cbSize = sizeof(BINDINFO);
156     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
157     if(FAILED(hres)) {
158         WARN("GetBindInfo failed: %08x\n", hres);
159         return hres;
160     }
161
162     ReleaseBindInfo(&bindinfo);
163
164     object_name = strstrW(path, separator);
165     if(!object_name) {
166         WARN("invalid url\n");
167         return report_result(pOIProtSink, STG_E_FILENOTFOUND);
168     }
169
170     len = object_name-path;
171     file_name = HeapAlloc(GetProcessHeap(), 0, (len+1)*sizeof(WCHAR));
172     memcpy(file_name, path, len*sizeof(WCHAR));
173     file_name[len] = 0;
174     chm_file = chm_openW(file_name);
175     if(!chm_file) {
176         WARN("Could not open chm file\n");
177         return report_result(pOIProtSink, STG_E_FILENOTFOUND);
178     }
179
180     object_name += 2;
181     memset(&chm_object, 0, sizeof(chm_object));
182     res = chm_resolve_object(chm_file, object_name, &chm_object);
183     if(res != CHM_RESOLVE_SUCCESS) {
184         WARN("Could not resolve chm object\n");
185         chm_close(chm_file);
186         return report_result(pOIProtSink, STG_E_FILENOTFOUND);
187     }
188
189     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, object_name+1);
190
191     /* FIXME: Native doesn't use FindMimeFromData */
192     hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
193     if(SUCCEEDED(hres)) {
194         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
195         CoTaskMemFree(mime);
196     }
197
198     hres = IInternetProtocolSink_ReportData(pOIProtSink,
199             BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE,
200             chm_object.length, chm_object.length);
201     if(FAILED(hres)) {
202         WARN("ReportData failed: %08x\n", hres);
203         chm_close(chm_file);
204         return report_result(pOIProtSink, hres);
205     }
206
207     hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
208
209     release_chm(This); /* Native leaks handle here */
210     This->chm_file = chm_file;
211     memcpy(&This->chm_object, &chm_object, sizeof(chm_object));
212
213     return report_result(pOIProtSink, hres);
214 }
215
216 static HRESULT WINAPI ITSProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
217 {
218     ITSProtocol *This = PROTOCOL_THIS(iface);
219     FIXME("(%p)->(%p)\n", This, pProtocolData);
220     return E_NOTIMPL;
221 }
222
223 static HRESULT WINAPI ITSProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
224         DWORD dwOptions)
225 {
226     ITSProtocol *This = PROTOCOL_THIS(iface);
227     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
228     return E_NOTIMPL;
229 }
230
231 static HRESULT WINAPI ITSProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
232 {
233     ITSProtocol *This = PROTOCOL_THIS(iface);
234
235     TRACE("(%p)->(%08x)\n", This, dwOptions);
236
237     return S_OK;
238 }
239
240 static HRESULT WINAPI ITSProtocol_Suspend(IInternetProtocol *iface)
241 {
242     ITSProtocol *This = PROTOCOL_THIS(iface);
243     FIXME("(%p)\n", This);
244     return E_NOTIMPL;
245 }
246
247 static HRESULT WINAPI ITSProtocol_Resume(IInternetProtocol *iface)
248 {
249     ITSProtocol *This = PROTOCOL_THIS(iface);
250     FIXME("(%p)\n", This);
251     return E_NOTIMPL;
252 }
253
254 static HRESULT WINAPI ITSProtocol_Read(IInternetProtocol *iface, void *pv,
255         ULONG cb, ULONG *pcbRead)
256 {
257     ITSProtocol *This = PROTOCOL_THIS(iface);
258
259     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
260
261     if(!This->chm_file)
262         return INET_E_DATA_NOT_AVAILABLE;
263
264     *pcbRead = chm_retrieve_object(This->chm_file, &This->chm_object, pv, This->offset, cb);
265     This->offset += *pcbRead;
266
267     return *pcbRead ? S_OK : S_FALSE;
268 }
269
270 static HRESULT WINAPI ITSProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
271         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
272 {
273     ITSProtocol *This = PROTOCOL_THIS(iface);
274     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
275     return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI ITSProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
279 {
280     ITSProtocol *This = PROTOCOL_THIS(iface);
281
282     TRACE("(%p)->(%08x)\n", This, dwOptions);
283
284     return S_OK;
285 }
286
287 static HRESULT WINAPI ITSProtocol_UnlockRequest(IInternetProtocol *iface)
288 {
289     ITSProtocol *This = PROTOCOL_THIS(iface);
290
291     TRACE("(%p)\n", This);
292
293     return S_OK;
294 }
295
296 #undef PROTOCOL_THIS
297
298 static const IInternetProtocolVtbl ITSProtocolVtbl = {
299     ITSProtocol_QueryInterface,
300     ITSProtocol_AddRef,
301     ITSProtocol_Release,
302     ITSProtocol_Start,
303     ITSProtocol_Continue,
304     ITSProtocol_Abort,
305     ITSProtocol_Terminate,
306     ITSProtocol_Suspend,
307     ITSProtocol_Resume,
308     ITSProtocol_Read,
309     ITSProtocol_Seek,
310     ITSProtocol_LockRequest,
311     ITSProtocol_UnlockRequest
312 };
313
314 HRESULT ITSProtocol_create(IUnknown *pUnkOuter, LPVOID *ppobj)
315 {
316     ITSProtocol *ret;
317
318     TRACE("(%p %p)\n", pUnkOuter, ppobj);
319
320     ITSS_LockModule();
321
322     ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITSProtocol));
323
324     ret->lpInternetProtocolVtbl = &ITSProtocolVtbl;
325     ret->ref = 1;
326
327     *ppobj = PROTOCOL(ret);
328
329     return S_OK;
330 }