2 * ITSS Storage implementation
4 * Copyright 2004 Mike McCormack
6 * see http://bonedaddy.net/pabs3/hhm/#chmspec
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
42 #include "wine/itss.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(itss);
48 /************************************************************************/
50 typedef struct _ITSS_IStorageImpl
52 const IStorageVtbl *vtbl_IStorage;
54 struct chmFile *chmfile;
60 struct enum_info *next, *prev;
61 struct chmUnitInfo ui;
64 typedef struct _IEnumSTATSTG_Impl
66 const IEnumSTATSTGVtbl *vtbl_IEnumSTATSTG;
68 struct enum_info *first, *last, *current;
71 typedef struct _IStream_Impl
73 const IStreamVtbl *vtbl_IStream;
75 ITSS_IStorageImpl *stg;
77 struct chmUnitInfo ui;
80 static HRESULT ITSS_create_chm_storage(
81 struct chmFile *chmfile, const WCHAR *dir, IStorage** ppstgOpen );
82 static IStream_Impl* ITSS_create_stream(
83 ITSS_IStorageImpl *stg, struct chmUnitInfo *ui );
85 /************************************************************************/
87 static HRESULT WINAPI ITSS_IEnumSTATSTG_QueryInterface(
92 IEnumSTATSTG_Impl *This = (IEnumSTATSTG_Impl *)iface;
94 if (IsEqualGUID(riid, &IID_IUnknown)
95 || IsEqualGUID(riid, &IID_IEnumSTATSTG))
97 IEnumSTATSTG_AddRef(iface);
102 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
103 return E_NOINTERFACE;
106 static ULONG WINAPI ITSS_IEnumSTATSTG_AddRef(
109 IEnumSTATSTG_Impl *This = (IEnumSTATSTG_Impl *)iface;
110 return InterlockedIncrement(&This->ref);
113 static ULONG WINAPI ITSS_IEnumSTATSTG_Release(
116 IEnumSTATSTG_Impl *This = (IEnumSTATSTG_Impl *)iface;
118 ULONG ref = InterlockedDecrement(&This->ref);
124 struct enum_info *t = This->first->next;
125 HeapFree( GetProcessHeap(), 0, This->first );
128 HeapFree(GetProcessHeap(), 0, This);
135 static HRESULT WINAPI ITSS_IEnumSTATSTG_Next(
141 IEnumSTATSTG_Impl *This = (IEnumSTATSTG_Impl *)iface;
143 struct enum_info *cur;
145 TRACE("%p %u %p %p\n", This, celt, rgelt, pceltFetched );
149 while( (n<celt) && cur)
153 memset( rgelt, 0, sizeof *rgelt );
159 len = strlenW( str ) + 1;
160 rgelt->pwcsName = CoTaskMemAlloc( len*sizeof(WCHAR) );
161 strcpyW( rgelt->pwcsName, str );
163 /* determine the type */
164 if( rgelt->pwcsName[len-2] == '/' )
166 rgelt->pwcsName[len-2] = 0;
167 rgelt->type = STGTY_STORAGE;
170 rgelt->type = STGTY_STREAM;
173 rgelt->cbSize.QuadPart = cur->ui.length;
175 /* advance to the next item if it exists */
189 static HRESULT WINAPI ITSS_IEnumSTATSTG_Skip(
193 IEnumSTATSTG_Impl *This = (IEnumSTATSTG_Impl *)iface;
195 struct enum_info *cur;
197 TRACE("%p %u\n", This, celt );
201 while( (n<celt) && cur)
214 static HRESULT WINAPI ITSS_IEnumSTATSTG_Reset(
217 IEnumSTATSTG_Impl *This = (IEnumSTATSTG_Impl *)iface;
219 TRACE("%p\n", This );
221 This->current = This->first;
226 static HRESULT WINAPI ITSS_IEnumSTATSTG_Clone(
228 IEnumSTATSTG** ppenum)
234 static const IEnumSTATSTGVtbl IEnumSTATSTG_vtbl =
236 ITSS_IEnumSTATSTG_QueryInterface,
237 ITSS_IEnumSTATSTG_AddRef,
238 ITSS_IEnumSTATSTG_Release,
239 ITSS_IEnumSTATSTG_Next,
240 ITSS_IEnumSTATSTG_Skip,
241 ITSS_IEnumSTATSTG_Reset,
242 ITSS_IEnumSTATSTG_Clone
245 static IEnumSTATSTG_Impl *ITSS_create_enum( void )
247 IEnumSTATSTG_Impl *stgenum;
249 stgenum = HeapAlloc( GetProcessHeap(), 0, sizeof (IEnumSTATSTG_Impl) );
250 stgenum->vtbl_IEnumSTATSTG = &IEnumSTATSTG_vtbl;
252 stgenum->first = NULL;
253 stgenum->last = NULL;
254 stgenum->current = NULL;
257 TRACE(" -> %p\n", stgenum );
262 /************************************************************************/
264 static HRESULT WINAPI ITSS_IStorageImpl_QueryInterface(
269 ITSS_IStorageImpl *This = (ITSS_IStorageImpl *)iface;
271 if (IsEqualGUID(riid, &IID_IUnknown)
272 || IsEqualGUID(riid, &IID_IStorage))
274 IStorage_AddRef(iface);
279 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
280 return E_NOINTERFACE;
283 static ULONG WINAPI ITSS_IStorageImpl_AddRef(
286 ITSS_IStorageImpl *This = (ITSS_IStorageImpl *)iface;
287 return InterlockedIncrement(&This->ref);
290 static ULONG WINAPI ITSS_IStorageImpl_Release(
293 ITSS_IStorageImpl *This = (ITSS_IStorageImpl *)iface;
295 ULONG ref = InterlockedDecrement(&This->ref);
299 chm_close(This->chmfile);
300 HeapFree(GetProcessHeap(), 0, This);
307 static HRESULT WINAPI ITSS_IStorageImpl_CreateStream(
319 static HRESULT WINAPI ITSS_IStorageImpl_OpenStream(
327 ITSS_IStorageImpl *This = (ITSS_IStorageImpl *)iface;
330 struct chmUnitInfo ui;
334 TRACE("%p %s %p %u %u %p\n", This, debugstr_w(pwcsName),
335 reserved1, grfMode, reserved2, ppstm );
337 len = strlenW( This->dir ) + strlenW( pwcsName ) + 1;
338 path = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
339 strcpyW( path, This->dir );
340 if( pwcsName[0] == '/' )
342 WCHAR *p = &path[strlenW( path ) - 1];
343 while( ( path <= p ) && ( *p == '/' ) )
346 strcatW( path, pwcsName );
348 TRACE("Resolving %s\n", debugstr_w(path));
350 r = chm_resolve_object(This->chmfile, path, &ui);
351 HeapFree( GetProcessHeap(), 0, path );
353 if( r != CHM_RESOLVE_SUCCESS )
354 return STG_E_FILENOTFOUND;
356 stm = ITSS_create_stream( This, &ui );
360 *ppstm = (IStream*) stm;
365 static HRESULT WINAPI ITSS_IStorageImpl_CreateStorage(
377 static HRESULT WINAPI ITSS_IStorageImpl_OpenStorage(
380 IStorage* pstgPriority,
386 ITSS_IStorageImpl *This = (ITSS_IStorageImpl *)iface;
388 FIXME("%p %s %p %u %p %u %p\n", This, debugstr_w(pwcsName),
389 pstgPriority, grfMode, snbExclude, reserved, ppstg);
393 static HRESULT WINAPI ITSS_IStorageImpl_CopyTo(
396 const IID* rgiidExclude,
404 static HRESULT WINAPI ITSS_IStorageImpl_MoveElementTo(
408 LPCOLESTR pwcsNewName,
415 static HRESULT WINAPI ITSS_IStorageImpl_Commit(
417 DWORD grfCommitFlags)
423 static HRESULT WINAPI ITSS_IStorageImpl_Revert(
430 static int ITSS_chm_enumerator(
432 struct chmUnitInfo *ui,
435 struct enum_info *info;
436 IEnumSTATSTG_Impl* stgenum = context;
438 TRACE("adding %s to enumeration\n", debugstr_w(ui->path) );
440 info = HeapAlloc( GetProcessHeap(), 0, sizeof (struct enum_info) );
441 memcpy( &info->ui, ui, sizeof info->ui );
444 info->prev = stgenum->last;
446 stgenum->last->next = info;
448 stgenum->first = info;
449 stgenum->last = info;
451 return CHM_ENUMERATOR_CONTINUE;
454 static HRESULT WINAPI ITSS_IStorageImpl_EnumElements(
459 IEnumSTATSTG** ppenum)
461 ITSS_IStorageImpl *This = (ITSS_IStorageImpl *)iface;
462 IEnumSTATSTG_Impl* stgenum;
464 TRACE("%p %d %p %d %p\n", This, reserved1, reserved2, reserved3, ppenum );
466 stgenum = ITSS_create_enum();
470 chm_enumerate_dir(This->chmfile,
476 stgenum->current = stgenum->first;
478 *ppenum = (IEnumSTATSTG*) stgenum;
483 static HRESULT WINAPI ITSS_IStorageImpl_DestroyElement(
491 static HRESULT WINAPI ITSS_IStorageImpl_RenameElement(
493 LPCOLESTR pwcsOldName,
494 LPCOLESTR pwcsNewName)
500 static HRESULT WINAPI ITSS_IStorageImpl_SetElementTimes(
503 const FILETIME* pctime,
504 const FILETIME* patime,
505 const FILETIME* pmtime)
511 static HRESULT WINAPI ITSS_IStorageImpl_SetClass(
519 static HRESULT WINAPI ITSS_IStorageImpl_SetStateBits(
528 static HRESULT WINAPI ITSS_IStorageImpl_Stat(
537 static const IStorageVtbl ITSS_IStorageImpl_Vtbl =
539 ITSS_IStorageImpl_QueryInterface,
540 ITSS_IStorageImpl_AddRef,
541 ITSS_IStorageImpl_Release,
542 ITSS_IStorageImpl_CreateStream,
543 ITSS_IStorageImpl_OpenStream,
544 ITSS_IStorageImpl_CreateStorage,
545 ITSS_IStorageImpl_OpenStorage,
546 ITSS_IStorageImpl_CopyTo,
547 ITSS_IStorageImpl_MoveElementTo,
548 ITSS_IStorageImpl_Commit,
549 ITSS_IStorageImpl_Revert,
550 ITSS_IStorageImpl_EnumElements,
551 ITSS_IStorageImpl_DestroyElement,
552 ITSS_IStorageImpl_RenameElement,
553 ITSS_IStorageImpl_SetElementTimes,
554 ITSS_IStorageImpl_SetClass,
555 ITSS_IStorageImpl_SetStateBits,
556 ITSS_IStorageImpl_Stat,
559 static HRESULT ITSS_create_chm_storage(
560 struct chmFile *chmfile, const WCHAR *dir, IStorage** ppstgOpen )
562 ITSS_IStorageImpl *stg;
565 TRACE("%p %s\n", chmfile, debugstr_w( dir ) );
567 len = strlenW( dir ) + 1;
568 stg = HeapAlloc( GetProcessHeap(), 0,
569 sizeof (ITSS_IStorageImpl) + len*sizeof(WCHAR) );
570 stg->vtbl_IStorage = &ITSS_IStorageImpl_Vtbl;
572 stg->chmfile = chmfile;
573 strcpyW( stg->dir, dir );
575 *ppstgOpen = (IStorage*) stg;
581 HRESULT ITSS_StgOpenStorage(
582 const WCHAR* pwcsName,
583 IStorage* pstgPriority,
587 IStorage** ppstgOpen)
589 struct chmFile *chmfile;
590 static const WCHAR szRoot[] = { '/', 0 };
592 TRACE("%s\n", debugstr_w(pwcsName) );
594 chmfile = chm_openW( pwcsName );
598 return ITSS_create_chm_storage( chmfile, szRoot, ppstgOpen );
601 /************************************************************************/
603 static HRESULT WINAPI ITSS_IStream_QueryInterface(
608 IStream_Impl *This = (IStream_Impl *)iface;
610 if (IsEqualGUID(riid, &IID_IUnknown)
611 || IsEqualGUID(riid, &IID_ISequentialStream)
612 || IsEqualGUID(riid, &IID_IStream))
614 IStream_AddRef(iface);
619 WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
620 return E_NOINTERFACE;
623 static ULONG WINAPI ITSS_IStream_AddRef(
626 IStream_Impl *This = (IStream_Impl *)iface;
627 return InterlockedIncrement(&This->ref);
630 static ULONG WINAPI ITSS_IStream_Release(
633 IStream_Impl *This = (IStream_Impl *)iface;
635 ULONG ref = InterlockedDecrement(&This->ref);
639 IStorage_Release( (IStorage*) This->stg );
640 HeapFree(GetProcessHeap(), 0, This);
647 static HRESULT WINAPI ITSS_IStream_Read(
653 IStream_Impl *This = (IStream_Impl *)iface;
656 TRACE("%p %p %u %p\n", This, pv, cb, pcbRead);
658 count = chm_retrieve_object(This->stg->chmfile,
659 &This->ui, pv, This->addr, cb);
664 return count ? S_OK : S_FALSE;
667 static HRESULT WINAPI ITSS_IStream_Write(
677 static HRESULT WINAPI ITSS_IStream_Seek(
679 LARGE_INTEGER dlibMove,
681 ULARGE_INTEGER* plibNewPosition)
683 IStream_Impl *This = (IStream_Impl *)iface;
686 TRACE("%p %s %u %p\n", This,
687 wine_dbgstr_longlong( dlibMove.QuadPart ), dwOrigin, plibNewPosition );
692 case STREAM_SEEK_CUR:
693 newpos = This->addr + dlibMove.QuadPart;
695 case STREAM_SEEK_SET:
696 newpos = dlibMove.QuadPart;
698 case STREAM_SEEK_END:
699 newpos = This->ui.length + dlibMove.QuadPart;
703 if( ( newpos < 0 ) || ( newpos > This->ui.length ) )
704 return STG_E_INVALIDPOINTER;
707 if( plibNewPosition )
708 plibNewPosition->QuadPart = This->addr;
713 static HRESULT WINAPI ITSS_IStream_SetSize(
715 ULARGE_INTEGER libNewSize)
721 static HRESULT WINAPI ITSS_IStream_CopyTo(
725 ULARGE_INTEGER* pcbRead,
726 ULARGE_INTEGER* pcbWritten)
732 static HRESULT WINAPI ITSS_IStream_Commit(
734 DWORD grfCommitFlags)
740 static HRESULT WINAPI ITSS_IStream_Revert(
747 static HRESULT WINAPI ITSS_IStream_LockRegion(
749 ULARGE_INTEGER libOffset,
757 static HRESULT WINAPI ITSS_IStream_UnlockRegion(
759 ULARGE_INTEGER libOffset,
767 static HRESULT WINAPI ITSS_IStream_Stat(
772 IStream_Impl *This = (IStream_Impl *)iface;
774 TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
776 memset( pstatstg, 0, sizeof *pstatstg );
777 if( !( grfStatFlag & STATFLAG_NONAME ) )
779 FIXME("copy the name\n");
781 pstatstg->type = STGTY_STREAM;
782 pstatstg->cbSize.QuadPart = This->ui.length;
783 pstatstg->grfMode = STGM_READ;
784 memcpy( &pstatstg->clsid, &CLSID_ITStorage, sizeof (CLSID) );
789 static HRESULT WINAPI ITSS_IStream_Clone(
797 static const IStreamVtbl ITSS_IStream_vtbl =
799 ITSS_IStream_QueryInterface,
801 ITSS_IStream_Release,
805 ITSS_IStream_SetSize,
809 ITSS_IStream_LockRegion,
810 ITSS_IStream_UnlockRegion,
815 static IStream_Impl *ITSS_create_stream(
816 ITSS_IStorageImpl *stg, struct chmUnitInfo *ui )
820 stm = HeapAlloc( GetProcessHeap(), 0, sizeof (IStream_Impl) );
821 stm->vtbl_IStream = &ITSS_IStream_vtbl;
824 memcpy( &stm->ui, ui, sizeof stm->ui );
826 IStorage_AddRef( (IStorage*) stg );
830 TRACE(" -> %p\n", stm );