source: trunk/src/shell32/memorystream.c

Last change on this file was 6709, checked in by sandervl, 24 years ago

restored old version

File size: 7.0 KB
RevLine 
[4121]1/*
[6709]2 * this class implements a pure IStream object
3 * and can be used for many purposes
[4121]4 *
[6709]5 * the main reason for implementing this was
6 * a cleaner implementation of IShellLink which
7 * needs to be able to load lnk's from a IStream
8 * interface so it was obvious to capsule the file
9 * access in a IStream to.
[4121]10 */
11
12#include <string.h>
13
[5618]14#include "winbase.h"
[4121]15#include "winerror.h"
[5618]16#include "shlobj.h"
[4121]17#include "debugtools.h"
[5618]18#include "heap.h"
[4121]19#include "shell32_main.h"
20
[5618]21DEFAULT_DEBUG_CHANNEL(shell);
[4121]22
23static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
24static ULONG WINAPI IStream_fnAddRef(IStream *iface);
25static ULONG WINAPI IStream_fnRelease(IStream *iface);
26static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
27static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
28static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
29static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
30static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
31static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
32static HRESULT WINAPI IStream_fnRevert (IStream * iface);
33static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
34static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
35static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
36static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
37
[6709]38static ICOM_VTABLE(IStream) stvt =
39{
40 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
41 IStream_fnQueryInterface,
42 IStream_fnAddRef,
43 IStream_fnRelease,
44 IStream_fnRead,
45 IStream_fnWrite,
46 IStream_fnSeek,
47 IStream_fnSetSize,
48 IStream_fnCopyTo,
49 IStream_fnCommit,
50 IStream_fnRevert,
51 IStream_fnLockRegion,
52 IStream_fnUnlockRegion,
53 IStream_fnStat,
54 IStream_fnClone
55
[4121]56};
57
[6709]58typedef struct
59{ ICOM_VTABLE(IStream) *lpvtst;
60 DWORD ref;
61 LPBYTE pImage;
62 HANDLE hMapping;
63 DWORD dwLength;
64 DWORD dwPos;
[4121]65} ISHFileStream;
66
67/**************************************************************************
68 * CreateStreamOnFile()
69 *
70 * similar to CreateStreamOnHGlobal
71 */
72HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
73{
[6709]74 ISHFileStream* fstr;
75 OFSTRUCT ofs;
76 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
77 HRESULT ret = E_FAIL;
78
79 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
80 fstr->lpvtst=&stvt;
81 fstr->ref = 1;
82 fstr->dwLength = GetFileSize (hFile, NULL);
[4121]83
[6709]84 shell32_ObjCount++;
[4121]85
[6709]86 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
87 {
88 WARN("failed to create filemap.\n");
89 goto end_2;
90 }
[4121]91
[6709]92 if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
93 {
94 WARN("failed to mmap filemap.\n");
95 goto end_3;
96 }
[4121]97
[6709]98 ret = S_OK;
99 goto end_1;
100
101end_3: CloseHandle(fstr->hMapping);
102end_2: HeapFree(GetProcessHeap(), 0, fstr);
103 fstr = NULL;
[4121]104
[6709]105end_1: _lclose(hFile);
106 (*ppstm) = (IStream*)fstr;
107 return ret;
[4121]108}
109
110/**************************************************************************
111* IStream_fnQueryInterface
112*/
113static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
114{
[6709]115 ICOM_THIS(ISHFileStream, iface);
[4121]116
[6709]117 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
[4121]118
[6709]119 *ppvObj = NULL;
[4121]120
[6709]121 if(IsEqualIID(riid, &IID_IUnknown) ||
122 IsEqualIID(riid, &IID_IStream))
123 {
124 *ppvObj = This;
125 }
[4121]126
[6709]127 if(*ppvObj)
128 {
129 IStream_AddRef((IStream*)*ppvObj);
130 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
131 return S_OK;
132 }
133 TRACE("-- Interface: E_NOINTERFACE\n");
134 return E_NOINTERFACE;
[4121]135}
136
137/**************************************************************************
138* IStream_fnAddRef
139*/
140static ULONG WINAPI IStream_fnAddRef(IStream *iface)
141{
[6709]142 ICOM_THIS(ISHFileStream, iface);
[4121]143
[6709]144 TRACE("(%p)->(count=%lu)\n",This, This->ref);
[4121]145
[6709]146 shell32_ObjCount++;
147 return ++(This->ref);
[4121]148}
149
150/**************************************************************************
151* IStream_fnRelease
152*/
153static ULONG WINAPI IStream_fnRelease(IStream *iface)
154{
[6709]155 ICOM_THIS(ISHFileStream, iface);
[4121]156
[6709]157 TRACE("(%p)->()\n",This);
[4121]158
[6709]159 shell32_ObjCount--;
[4121]160
[6709]161 if (!--(This->ref))
162 { TRACE(" destroying SHFileStream (%p)\n",This);
[4121]163
[6709]164 UnmapViewOfFile(This->pImage);
165 CloseHandle(This->hMapping);
[4121]166
[6709]167 HeapFree(GetProcessHeap(),0,This);
168 return 0;
169 }
170 return This->ref;
[4121]171}
172
173static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
174{
[6709]175 ICOM_THIS(ISHFileStream, iface);
[4121]176
[6709]177 DWORD dwBytesToRead, dwBytesLeft;
178
179 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
180
181 if ( !pv )
182 return STG_E_INVALIDPOINTER;
[4121]183
[6709]184 dwBytesLeft = This->dwLength - This->dwPos;
[4121]185
[6709]186 if ( 0 >= dwBytesLeft ) /* end of buffer */
187 return S_FALSE;
188
189 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
[4121]190
[6709]191 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
192
193 This->dwPos += dwBytesToRead; /* adjust pointer */
[4121]194
[6709]195 if (pcbRead)
196 *pcbRead = dwBytesToRead;
[4121]197
[6709]198 return S_OK;
[4121]199}
200static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
201{
[6709]202 ICOM_THIS(ISHFileStream, iface);
[4121]203
[6709]204 TRACE("(%p)\n",This);
[4121]205
[6709]206 return E_NOTIMPL;
[4121]207}
208static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
209{
[6709]210 ICOM_THIS(ISHFileStream, iface);
[4121]211
[6709]212 TRACE("(%p)\n",This);
[4121]213
[6709]214 return E_NOTIMPL;
[4121]215}
216static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
217{
[6709]218 ICOM_THIS(ISHFileStream, iface);
[4121]219
[6709]220 TRACE("(%p)\n",This);
[4121]221
[6709]222 return E_NOTIMPL;
[4121]223}
224static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
225{
[6709]226 ICOM_THIS(ISHFileStream, iface);
[4121]227
[6709]228 TRACE("(%p)\n",This);
[4121]229
[6709]230 return E_NOTIMPL;
[4121]231}
232static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
233{
[6709]234 ICOM_THIS(ISHFileStream, iface);
[4121]235
[6709]236 TRACE("(%p)\n",This);
[4121]237
[6709]238 return E_NOTIMPL;
[4121]239}
240static HRESULT WINAPI IStream_fnRevert (IStream * iface)
241{
[6709]242 ICOM_THIS(ISHFileStream, iface);
[4121]243
[6709]244 TRACE("(%p)\n",This);
[4121]245
[6709]246 return E_NOTIMPL;
[4121]247}
248static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
249{
[6709]250 ICOM_THIS(ISHFileStream, iface);
[4121]251
[6709]252 TRACE("(%p)\n",This);
[4121]253
[6709]254 return E_NOTIMPL;
[4121]255}
256static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
257{
[6709]258 ICOM_THIS(ISHFileStream, iface);
[4121]259
[6709]260 TRACE("(%p)\n",This);
[4121]261
[6709]262 return E_NOTIMPL;
[4121]263}
264static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
265{
[6709]266 ICOM_THIS(ISHFileStream, iface);
[4121]267
[6709]268 TRACE("(%p)\n",This);
[4121]269
[6709]270 return E_NOTIMPL;
[4121]271}
272static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
273{
[6709]274 ICOM_THIS(ISHFileStream, iface);
[4121]275
[6709]276 TRACE("(%p)\n",This);
[4121]277
[6709]278 return E_NOTIMPL;
[4121]279}
Note: See TracBrowser for help on using the repository browser.