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