TESTING -- override pthreads to fix gstreamer v5
[wine/multimedia.git] / dlls / itss / protocol.c
blob1463518c2cf98b99cf0b2e26cbb6b4b4745e5a91
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"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(itss);
38 typedef struct {
39 IInternetProtocol IInternetProtocol_iface;
40 IInternetProtocolInfo IInternetProtocolInfo_iface;
42 LONG ref;
44 ULONG offset;
45 struct chmFile *chm_file;
46 struct chmUnitInfo chm_object;
47 } ITSProtocol;
49 static inline ITSProtocol *impl_from_IInternetProtocol(IInternetProtocol *iface)
51 return CONTAINING_RECORD(iface, ITSProtocol, IInternetProtocol_iface);
54 static inline ITSProtocol *impl_from_IInternetProtocolInfo(IInternetProtocolInfo *iface)
56 return CONTAINING_RECORD(iface, ITSProtocol, IInternetProtocolInfo_iface);
59 static void release_chm(ITSProtocol *This)
61 if(This->chm_file) {
62 chm_close(This->chm_file);
63 This->chm_file = NULL;
65 This->offset = 0;
68 static HRESULT WINAPI ITSProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
70 ITSProtocol *This = impl_from_IInternetProtocol(iface);
72 *ppv = NULL;
73 if(IsEqualGUID(&IID_IUnknown, riid)) {
74 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
75 *ppv = &This->IInternetProtocol_iface;
76 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
77 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
78 *ppv = &This->IInternetProtocol_iface;
79 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
80 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
81 *ppv = &This->IInternetProtocol_iface;
82 }else if(IsEqualGUID(&IID_IInternetProtocolInfo, riid)) {
83 TRACE("(%p)->(IID_IInternetProtocolInfo %p)\n", This, ppv);
84 *ppv = &This->IInternetProtocolInfo_iface;
87 if(*ppv) {
88 IInternetProtocol_AddRef(iface);
89 return S_OK;
92 WARN("not supported interface %s\n", debugstr_guid(riid));
93 return E_NOINTERFACE;
96 static ULONG WINAPI ITSProtocol_AddRef(IInternetProtocol *iface)
98 ITSProtocol *This = impl_from_IInternetProtocol(iface);
99 LONG ref = InterlockedIncrement(&This->ref);
100 TRACE("(%p) ref=%d\n", This, ref);
101 return ref;
104 static ULONG WINAPI ITSProtocol_Release(IInternetProtocol *iface)
106 ITSProtocol *This = impl_from_IInternetProtocol(iface);
107 LONG ref = InterlockedDecrement(&This->ref);
109 TRACE("(%p) ref=%d\n", This, ref);
111 if(!ref) {
112 release_chm(This);
113 HeapFree(GetProcessHeap(), 0, This);
115 ITSS_UnlockModule();
118 return ref;
121 static LPCWSTR skip_schema(LPCWSTR url)
123 static const WCHAR its_schema[] = {'i','t','s',':'};
124 static const WCHAR msits_schema[] = {'m','s','-','i','t','s',':'};
125 static const WCHAR mk_schema[] = {'m','k',':','@','M','S','I','T','S','t','o','r','e',':'};
127 if(!strncmpiW(its_schema, url, sizeof(its_schema)/sizeof(WCHAR)))
128 return url+sizeof(its_schema)/sizeof(WCHAR);
129 if(!strncmpiW(msits_schema, url, sizeof(msits_schema)/sizeof(WCHAR)))
130 return url+sizeof(msits_schema)/sizeof(WCHAR);
131 if(!strncmpiW(mk_schema, url, sizeof(mk_schema)/sizeof(WCHAR)))
132 return url+sizeof(mk_schema)/sizeof(WCHAR);
134 return NULL;
137 /* Adopted from urlmon */
138 static void remove_dot_segments(WCHAR *path) {
139 const WCHAR *in = path;
140 WCHAR *out = path;
142 while(1) {
143 /* Move the first path segment in the input buffer to the end of
144 * the output buffer, and any subsequent characters up to, including
145 * the next "/" character (if any) or the end of the input buffer.
147 while(*in != '/') {
148 if(!(*out++ = *in++))
149 return;
152 *out++ = *in++;
154 while(*in) {
155 if(*in != '.')
156 break;
158 /* Handle ending "/." */
159 if(!in[1]) {
160 ++in;
161 break;
164 /* Handle "/./" */
165 if(in[1] == '/') {
166 in += 2;
167 continue;
170 /* If we don't have "/../" or ending "/.." */
171 if(in[1] != '.' || (in[2] && in[2] != '/'))
172 break;
174 in += *in ? 3 : 2;
176 /* Find the slash preceding out pointer and move out pointer to it */
177 if(out > path+1 && *--out == '/')
178 --out;
179 while(out > path && *(--out) != '/');
180 if(*out == '/')
181 ++out;
186 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres)
188 IInternetProtocolSink_ReportResult(sink, hres, 0, NULL);
189 return hres;
192 static HRESULT WINAPI ITSProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
193 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
194 DWORD grfPI, HANDLE_PTR dwReserved)
196 ITSProtocol *This = impl_from_IInternetProtocol(iface);
197 BINDINFO bindinfo;
198 DWORD bindf = 0, len;
199 LPWSTR file_name, mime, object_name, p;
200 LPCWSTR ptr;
201 struct chmFile *chm_file;
202 struct chmUnitInfo chm_object;
203 int res;
204 HRESULT hres;
206 static const WCHAR separator[] = {':',':',0};
208 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
209 pOIBindInfo, grfPI, dwReserved);
211 ptr = skip_schema(szUrl);
212 if(!ptr)
213 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
215 memset(&bindinfo, 0, sizeof(bindinfo));
216 bindinfo.cbSize = sizeof(BINDINFO);
217 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
218 if(FAILED(hres)) {
219 WARN("GetBindInfo failed: %08x\n", hres);
220 return hres;
223 ReleaseBindInfo(&bindinfo);
225 len = strlenW(ptr)+3;
226 file_name = HeapAlloc(GetProcessHeap(), 0, len*sizeof(WCHAR));
227 memcpy(file_name, ptr, len*sizeof(WCHAR));
228 hres = UrlUnescapeW(file_name, NULL, &len, URL_UNESCAPE_INPLACE);
229 if(FAILED(hres)) {
230 WARN("UrlUnescape failed: %08x\n", hres);
231 HeapFree(GetProcessHeap(), 0, file_name);
232 return hres;
235 p = strstrW(file_name, separator);
236 if(!p) {
237 WARN("invalid url\n");
238 HeapFree(GetProcessHeap(), 0, file_name);
239 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
242 *p = 0;
243 chm_file = chm_openW(file_name);
244 if(!chm_file) {
245 WARN("Could not open chm file\n");
246 HeapFree(GetProcessHeap(), 0, file_name);
247 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
250 object_name = p+2;
251 len = strlenW(object_name);
253 if(*object_name != '/' && *object_name != '\\') {
254 memmove(object_name+1, object_name, (len+1)*sizeof(WCHAR));
255 *object_name = '/';
256 len++;
259 if(object_name[len-1] == '/')
260 object_name[--len] = 0;
262 for(p=object_name; *p; p++) {
263 if(*p == '\\')
264 *p = '/';
267 remove_dot_segments(object_name);
269 TRACE("Resolving %s\n", debugstr_w(object_name));
271 memset(&chm_object, 0, sizeof(chm_object));
272 res = chm_resolve_object(chm_file, object_name, &chm_object);
273 if(res != CHM_RESOLVE_SUCCESS) {
274 WARN("Could not resolve chm object\n");
275 HeapFree(GetProcessHeap(), 0, file_name);
276 chm_close(chm_file);
277 return report_result(pOIProtSink, STG_E_FILENOTFOUND);
280 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST,
281 strrchrW(object_name, '/')+1);
283 /* FIXME: Native doesn't use FindMimeFromData */
284 hres = FindMimeFromData(NULL, object_name, NULL, 0, NULL, 0, &mime, 0);
285 HeapFree(GetProcessHeap(), 0, file_name);
286 if(SUCCEEDED(hres)) {
287 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
288 CoTaskMemFree(mime);
291 release_chm(This); /* Native leaks handle here */
292 This->chm_file = chm_file;
293 This->chm_object = chm_object;
295 hres = IInternetProtocolSink_ReportData(pOIProtSink,
296 BSCF_FIRSTDATANOTIFICATION|BSCF_DATAFULLYAVAILABLE,
297 chm_object.length, chm_object.length);
298 if(FAILED(hres)) {
299 WARN("ReportData failed: %08x\n", hres);
300 release_chm(This);
301 return report_result(pOIProtSink, hres);
304 hres = IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_BEGINDOWNLOADDATA, NULL);
306 return report_result(pOIProtSink, hres);
309 static HRESULT WINAPI ITSProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
311 ITSProtocol *This = impl_from_IInternetProtocol(iface);
312 FIXME("(%p)->(%p)\n", This, pProtocolData);
313 return E_NOTIMPL;
316 static HRESULT WINAPI ITSProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
317 DWORD dwOptions)
319 ITSProtocol *This = impl_from_IInternetProtocol(iface);
320 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
321 return E_NOTIMPL;
324 static HRESULT WINAPI ITSProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
326 ITSProtocol *This = impl_from_IInternetProtocol(iface);
328 TRACE("(%p)->(%08x)\n", This, dwOptions);
330 return S_OK;
333 static HRESULT WINAPI ITSProtocol_Suspend(IInternetProtocol *iface)
335 ITSProtocol *This = impl_from_IInternetProtocol(iface);
336 FIXME("(%p)\n", This);
337 return E_NOTIMPL;
340 static HRESULT WINAPI ITSProtocol_Resume(IInternetProtocol *iface)
342 ITSProtocol *This = impl_from_IInternetProtocol(iface);
343 FIXME("(%p)\n", This);
344 return E_NOTIMPL;
347 static HRESULT WINAPI ITSProtocol_Read(IInternetProtocol *iface, void *pv,
348 ULONG cb, ULONG *pcbRead)
350 ITSProtocol *This = impl_from_IInternetProtocol(iface);
352 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
354 if(!This->chm_file)
355 return INET_E_DATA_NOT_AVAILABLE;
357 *pcbRead = chm_retrieve_object(This->chm_file, &This->chm_object, pv, This->offset, cb);
358 This->offset += *pcbRead;
360 return *pcbRead ? S_OK : S_FALSE;
363 static HRESULT WINAPI ITSProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
364 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
366 ITSProtocol *This = impl_from_IInternetProtocol(iface);
367 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
368 return E_NOTIMPL;
371 static HRESULT WINAPI ITSProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
373 ITSProtocol *This = impl_from_IInternetProtocol(iface);
375 TRACE("(%p)->(%08x)\n", This, dwOptions);
377 return S_OK;
380 static HRESULT WINAPI ITSProtocol_UnlockRequest(IInternetProtocol *iface)
382 ITSProtocol *This = impl_from_IInternetProtocol(iface);
384 TRACE("(%p)\n", This);
386 return S_OK;
389 static const IInternetProtocolVtbl ITSProtocolVtbl = {
390 ITSProtocol_QueryInterface,
391 ITSProtocol_AddRef,
392 ITSProtocol_Release,
393 ITSProtocol_Start,
394 ITSProtocol_Continue,
395 ITSProtocol_Abort,
396 ITSProtocol_Terminate,
397 ITSProtocol_Suspend,
398 ITSProtocol_Resume,
399 ITSProtocol_Read,
400 ITSProtocol_Seek,
401 ITSProtocol_LockRequest,
402 ITSProtocol_UnlockRequest
405 static HRESULT WINAPI ITSProtocolInfo_QueryInterface(IInternetProtocolInfo *iface,
406 REFIID riid, void **ppv)
408 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
409 return IInternetProtocol_QueryInterface(&This->IInternetProtocol_iface, riid, ppv);
412 static ULONG WINAPI ITSProtocolInfo_AddRef(IInternetProtocolInfo *iface)
414 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
415 return IInternetProtocol_AddRef(&This->IInternetProtocol_iface);
418 static ULONG WINAPI ITSProtocolInfo_Release(IInternetProtocolInfo *iface)
420 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
421 return IInternetProtocol_Release(&This->IInternetProtocol_iface);
424 static HRESULT WINAPI ITSProtocolInfo_ParseUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
425 PARSEACTION ParseAction, DWORD dwParseFlags, LPWSTR pwzResult, DWORD cchResult,
426 DWORD *pcchResult, DWORD dwReserved)
428 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
430 TRACE("(%p)->(%s %x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), ParseAction,
431 dwParseFlags, pwzResult, cchResult, pcchResult, dwReserved);
433 switch(ParseAction) {
434 case PARSE_CANONICALIZE:
435 FIXME("PARSE_CANONICALIZE\n");
436 return E_NOTIMPL;
437 case PARSE_SECURITY_URL:
438 FIXME("PARSE_SECURITY_URL\n");
439 return E_NOTIMPL;
440 default:
441 return INET_E_DEFAULT_ACTION;
444 return S_OK;
447 static HRESULT WINAPI ITSProtocolInfo_CombineUrl(IInternetProtocolInfo *iface,
448 LPCWSTR pwzBaseUrl, LPCWSTR pwzRelativeUrl, DWORD dwCombineFlags, LPWSTR pwzResult,
449 DWORD cchResult, DWORD* pcchResult, DWORD dwReserved)
451 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
452 LPCWSTR base_end, ptr;
453 DWORD rel_len;
455 static const WCHAR separator[] = {':',':',0};
457 TRACE("(%p)->(%s %s %08x %p %d %p %d)\n", This, debugstr_w(pwzBaseUrl),
458 debugstr_w(pwzRelativeUrl), dwCombineFlags, pwzResult, cchResult,
459 pcchResult, dwReserved);
461 base_end = strstrW(pwzBaseUrl, separator);
462 if(!base_end)
463 return 0x80041001;
464 base_end += 2;
466 if(!skip_schema(pwzBaseUrl))
467 return INET_E_USE_DEFAULT_PROTOCOLHANDLER;
469 if(strchrW(pwzRelativeUrl, ':'))
470 return STG_E_INVALIDNAME;
472 if(pwzRelativeUrl[0] == '#') {
473 base_end += strlenW(base_end);
474 }else if(pwzRelativeUrl[0] != '/') {
475 ptr = strrchrW(base_end, '/');
476 if(ptr)
477 base_end = ptr+1;
478 else
479 base_end += strlenW(base_end);
482 rel_len = strlenW(pwzRelativeUrl)+1;
484 *pcchResult = rel_len + (base_end-pwzBaseUrl);
486 if(*pcchResult > cchResult)
487 return E_OUTOFMEMORY;
489 memcpy(pwzResult, pwzBaseUrl, (base_end-pwzBaseUrl)*sizeof(WCHAR));
490 strcpyW(pwzResult + (base_end-pwzBaseUrl), pwzRelativeUrl);
492 return S_OK;
495 static HRESULT WINAPI ITSProtocolInfo_CompareUrl(IInternetProtocolInfo *iface, LPCWSTR pwzUrl1,
496 LPCWSTR pwzUrl2, DWORD dwCompareFlags)
498 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
499 FIXME("%p)->(%s %s %08x)\n", This, debugstr_w(pwzUrl1), debugstr_w(pwzUrl2), dwCompareFlags);
500 return E_NOTIMPL;
503 static HRESULT WINAPI ITSProtocolInfo_QueryInfo(IInternetProtocolInfo *iface, LPCWSTR pwzUrl,
504 QUERYOPTION QueryOption, DWORD dwQueryFlags, LPVOID pBuffer, DWORD cbBuffer, DWORD* pcbBuf,
505 DWORD dwReserved)
507 ITSProtocol *This = impl_from_IInternetProtocolInfo(iface);
508 FIXME("(%p)->(%s %08x %08x %p %d %p %d)\n", This, debugstr_w(pwzUrl), QueryOption,
509 dwQueryFlags, pBuffer, cbBuffer, pcbBuf, dwReserved);
510 return E_NOTIMPL;
513 static const IInternetProtocolInfoVtbl ITSProtocolInfoVtbl = {
514 ITSProtocolInfo_QueryInterface,
515 ITSProtocolInfo_AddRef,
516 ITSProtocolInfo_Release,
517 ITSProtocolInfo_ParseUrl,
518 ITSProtocolInfo_CombineUrl,
519 ITSProtocolInfo_CompareUrl,
520 ITSProtocolInfo_QueryInfo
523 HRESULT ITSProtocol_create(IUnknown *pUnkOuter, LPVOID *ppobj)
525 ITSProtocol *ret;
527 TRACE("(%p %p)\n", pUnkOuter, ppobj);
529 ITSS_LockModule();
531 ret = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(ITSProtocol));
533 ret->IInternetProtocol_iface.lpVtbl = &ITSProtocolVtbl;
534 ret->IInternetProtocolInfo_iface.lpVtbl = &ITSProtocolInfoVtbl;
535 ret->ref = 1;
537 *ppobj = &ret->IInternetProtocol_iface;
539 return S_OK;