Avoid using the MAKEPOINTS macro, it's broken on big endian.
[wine/wine-kai.git] / dlls / shell32 / memorystream.c
blobff7ea0b00c821560e7fd32abd49a1f6c9a957368
1 /*
2 * this class implements a pure IStream object
3 * and can be used for many purposes
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.
11 * Copyright 1999 Juergen Schmied
12 * Copyright 2003 Mike McCormack for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include <stdarg.h>
30 #include <string.h>
32 #define COBJMACROS
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winuser.h"
38 #include "wingdi.h"
39 #include "shlobj.h"
40 #include "wine/debug.h"
41 #include "shell32_main.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45 static const IStreamVtbl stvt;
47 typedef struct
49 const IStreamVtbl *lpvtst;
50 DWORD ref;
51 HANDLE handle;
52 } ISHFileStream;
54 /**************************************************************************
55 * CreateStreamOnFile()
57 * similar to CreateStreamOnHGlobal
59 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
61 ISHFileStream* fstr;
62 HANDLE handle;
63 DWORD access = GENERIC_READ, creat;
65 if( grfMode & STGM_TRANSACTED )
66 return E_INVALIDARG;
68 if( grfMode & STGM_WRITE )
69 access |= GENERIC_WRITE;
70 if( grfMode & STGM_READWRITE )
71 access = GENERIC_WRITE | GENERIC_READ;
73 if( grfMode & STGM_CREATE )
74 creat = CREATE_ALWAYS;
75 else
76 creat = OPEN_EXISTING;
78 TRACE("Opening %s\n", debugstr_w(pszFilename) );
80 handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
81 if( handle == INVALID_HANDLE_VALUE )
82 return HRESULT_FROM_WIN32(GetLastError());
84 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
85 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
86 if( !fstr )
87 return E_OUTOFMEMORY;
88 fstr->lpvtst=&stvt;
89 fstr->ref = 1;
90 fstr->handle = handle;
92 (*ppstm) = (IStream*)fstr;
94 return S_OK;
97 /**************************************************************************
98 * IStream_fnQueryInterface
100 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
102 ISHFileStream *This = (ISHFileStream *)iface;
104 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
106 *ppvObj = NULL;
108 if(IsEqualIID(riid, &IID_IUnknown) ||
109 IsEqualIID(riid, &IID_IStream))
111 *ppvObj = This;
114 if(*ppvObj)
116 IStream_AddRef((IStream*)*ppvObj);
117 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
118 return S_OK;
120 TRACE("-- Interface: E_NOINTERFACE\n");
121 return E_NOINTERFACE;
124 /**************************************************************************
125 * IStream_fnAddRef
127 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
129 ISHFileStream *This = (ISHFileStream *)iface;
131 TRACE("(%p)->(count=%lu)\n",This, This->ref);
133 return ++(This->ref);
136 /**************************************************************************
137 * IStream_fnRelease
139 static ULONG WINAPI IStream_fnRelease(IStream *iface)
141 ISHFileStream *This = (ISHFileStream *)iface;
143 TRACE("(%p)->()\n",This);
145 if (!--(This->ref))
147 TRACE(" destroying SHFileStream (%p)\n",This);
148 CloseHandle(This->handle);
149 HeapFree(GetProcessHeap(),0,This);
151 return This->ref;
154 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
156 ISHFileStream *This = (ISHFileStream *)iface;
158 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
160 if ( !pv )
161 return STG_E_INVALIDPOINTER;
163 if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
164 return S_FALSE;
166 return S_OK;
169 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
171 DWORD dummy_count;
172 ISHFileStream *This = (ISHFileStream *)iface;
174 TRACE("(%p)\n",This);
176 if( !pv )
177 return STG_E_INVALIDPOINTER;
179 /* WriteFile() doesn't allow to specify NULL as write count pointer */
180 if (!pcbWritten)
181 pcbWritten = &dummy_count;
183 if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
184 return E_FAIL;
186 return S_OK;
189 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
191 DWORD pos, newposlo, newposhi;
193 ISHFileStream *This = (ISHFileStream *)iface;
195 TRACE("(%p)\n",This);
197 pos = dlibMove.QuadPart; /* FIXME: truncates */
198 newposhi = 0;
199 newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
200 if( newposlo == INVALID_SET_FILE_POINTER )
201 return E_FAIL;
203 plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
205 return S_OK;
208 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
210 ISHFileStream *This = (ISHFileStream *)iface;
212 TRACE("(%p)\n",This);
214 if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
215 return E_FAIL;
217 if( ! SetEndOfFile( This->handle ) )
218 return E_FAIL;
220 return S_OK;
222 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
224 ISHFileStream *This = (ISHFileStream *)iface;
226 TRACE("(%p)\n",This);
228 return E_NOTIMPL;
230 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
232 ISHFileStream *This = (ISHFileStream *)iface;
234 TRACE("(%p)\n",This);
236 return E_NOTIMPL;
238 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
240 ISHFileStream *This = (ISHFileStream *)iface;
242 TRACE("(%p)\n",This);
244 return E_NOTIMPL;
246 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
248 ISHFileStream *This = (ISHFileStream *)iface;
250 TRACE("(%p)\n",This);
252 return E_NOTIMPL;
254 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
256 ISHFileStream *This = (ISHFileStream *)iface;
258 TRACE("(%p)\n",This);
260 return E_NOTIMPL;
262 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
264 ISHFileStream *This = (ISHFileStream *)iface;
266 TRACE("(%p)\n",This);
268 return E_NOTIMPL;
270 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
272 ISHFileStream *This = (ISHFileStream *)iface;
274 TRACE("(%p)\n",This);
276 return E_NOTIMPL;
279 static const IStreamVtbl stvt =
281 IStream_fnQueryInterface,
282 IStream_fnAddRef,
283 IStream_fnRelease,
284 IStream_fnRead,
285 IStream_fnWrite,
286 IStream_fnSeek,
287 IStream_fnSetSize,
288 IStream_fnCopyTo,
289 IStream_fnCommit,
290 IStream_fnRevert,
291 IStream_fnLockRegion,
292 IStream_fnUnlockRegion,
293 IStream_fnStat,
294 IStream_fnClone