blob: e6d2f731dfc7daf5c18eed024899aaf01a530b53 [file] [log] [blame]
Francis Beaudet4f8b5a81999-04-24 12:00:31 +00001/*
2 * HGLOBAL Stream implementation
3 *
4 * This file contains the implementation of the stream interface
Alexandre Julliard81ee21d1999-12-27 05:26:00 +00005 * for streams contained supported by an HGLOBAL pointer.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +00006 *
7 * Copyright 1999 Francis Beaudet
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00008 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
Jonathan Ernst360a3f92006-05-18 14:49:52 +020021 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000022 */
Patrik Stridvallbc38d6b2001-07-20 18:00:00 +000023
24#include "config.h"
25
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000026#include <assert.h>
27#include <stdlib.h>
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000028#include <stdarg.h>
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000029#include <stdio.h>
30#include <string.h>
31
Francois Gouget486d0202004-10-07 03:06:48 +000032#define COBJMACROS
Dimitrie O. Paun297f3d82003-01-07 20:36:20 +000033#define NONAMELESSUNION
34#define NONAMELESSSTRUCT
Francois Gouget486d0202004-10-07 03:06:48 +000035
Patrik Stridvallbc38d6b2001-07-20 18:00:00 +000036#include "windef.h"
Alexandre Julliarde37c6e12003-09-05 23:08:26 +000037#include "winbase.h"
Alexandre Julliard20c169e2003-09-09 19:39:31 +000038#include "winuser.h"
Patrik Stridvallbc38d6b2001-07-20 18:00:00 +000039#include "objbase.h"
40#include "ole2.h"
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000041#include "winerror.h"
Patrik Stridvall9c1de6d2002-09-12 22:07:02 +000042#include "winternl.h"
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000043
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000044#include "wine/debug.h"
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000045
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000046WINE_DEFAULT_DEBUG_CHANNEL(storage);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000047
48/****************************************************************************
49 * HGLOBALStreamImpl definition.
50 *
Huw Davies8519a6c2006-11-21 20:44:54 +000051 * This class implements the IStream interface and represents a stream
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000052 * supported by an HGLOBAL pointer.
53 */
54struct HGLOBALStreamImpl
55{
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +000056 const IStreamVtbl *lpVtbl; /* Needs to be the first item in the struct
Francois Gouget93416cd2005-03-23 13:15:18 +000057 * since we want to cast this in an IStream pointer */
Vincent Béron9a624912002-05-31 23:06:46 +000058
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000059 /*
60 * Reference count
61 */
Mike McCormackc7fdb452005-07-05 11:02:54 +000062 LONG ref;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000063
64 /*
65 * Support for the stream
66 */
67 HGLOBAL supportHandle;
68
69 /*
70 * This flag is TRUE if the HGLOBAL is destroyed when the stream
71 * is finally released.
72 */
73 BOOL deleteOnRelease;
74
75 /*
76 * Helper variable that contains the size of the stream
77 */
78 ULARGE_INTEGER streamSize;
79
80 /*
81 * This is the current position of the cursor in the stream
82 */
83 ULARGE_INTEGER currentPosition;
84};
85
86typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
87
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000088/***
89 * This is the destructor of the HGLOBALStreamImpl class.
90 *
Vincent Béron9a624912002-05-31 23:06:46 +000091 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000092 * class. The pointer passed-in to this function will be freed and will not
93 * be valid anymore.
94 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +000095static void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000096{
Alexandre Julliard359f497e1999-07-04 16:02:24 +000097 TRACE("(%p)\n", This);
Francis Beaudetec2edc71999-05-02 11:41:10 +000098
Francis Beaudet4f8b5a81999-04-24 12:00:31 +000099 /*
100 * Release the HGlobal if the constructor asked for that.
101 */
102 if (This->deleteOnRelease)
103 {
104 GlobalFree(This->supportHandle);
105 This->supportHandle=0;
106 }
107
108 /*
109 * Finally, free the memory used-up by the class.
110 */
Vincent Béron9a624912002-05-31 23:06:46 +0000111 HeapFree(GetProcessHeap(), 0, This);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000112}
113
114/***
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000115 * This implements the IUnknown method AddRef for this
116 * class
117 */
118static ULONG WINAPI HGLOBALStreamImpl_AddRef(
119 IStream* iface)
120{
121 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
122 return InterlockedIncrement(&This->ref);
123}
124
125/***
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000126 * This implements the IUnknown method QueryInterface for this
127 * class
128 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000129static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000130 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000131 REFIID riid, /* [in] */
132 void** ppvObject) /* [iid_is][out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000133{
134 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
135
136 /*
137 * Perform a sanity check on the parameters.
138 */
139 if (ppvObject==0)
140 return E_INVALIDARG;
Vincent Béron9a624912002-05-31 23:06:46 +0000141
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000142 /*
143 * Initialize the return parameter.
144 */
145 *ppvObject = 0;
Vincent Béron9a624912002-05-31 23:06:46 +0000146
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000147 /*
148 * Compare the riid with the interface IDs implemented by this object.
149 */
Rob Shearman482063b2007-01-07 12:15:59 +0000150 if (IsEqualIID(&IID_IUnknown, riid) ||
151 IsEqualIID(&IID_ISequentialStream, riid) ||
152 IsEqualIID(&IID_IStream, riid))
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000153 {
154 *ppvObject = (IStream*)This;
155 }
Vincent Béron9a624912002-05-31 23:06:46 +0000156
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000157 /*
158 * Check that we obtained an interface.
159 */
160 if ((*ppvObject)==0)
161 return E_NOINTERFACE;
Vincent Béron9a624912002-05-31 23:06:46 +0000162
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000163 /*
164 * Query Interface always increases the reference count by one when it is
165 * successful
166 */
167 HGLOBALStreamImpl_AddRef(iface);
Vincent Béron9a624912002-05-31 23:06:46 +0000168
169 return S_OK;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000170}
171
172/***
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000173 * This implements the IUnknown method Release for this
174 * class
175 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000176static ULONG WINAPI HGLOBALStreamImpl_Release(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000177 IStream* iface)
178{
179 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000180 ULONG newRef;
Vincent Béron9a624912002-05-31 23:06:46 +0000181
Joris Huizer34cffce2004-09-24 01:16:53 +0000182 newRef = InterlockedDecrement(&This->ref);
Vincent Béron9a624912002-05-31 23:06:46 +0000183
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000184 /*
185 * If the reference count goes down to 0, perform suicide.
186 */
187 if (newRef==0)
188 {
189 HGLOBALStreamImpl_Destroy(This);
190 }
Vincent Béron9a624912002-05-31 23:06:46 +0000191
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000192 return newRef;
193}
194
195/***
196 * This method is part of the ISequentialStream interface.
197 *
198 * If reads a block of information from the stream at the current
199 * position. It then moves the current position at the end of the
200 * read block
201 *
202 * See the documentation of ISequentialStream for more info.
203 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000204static HRESULT WINAPI HGLOBALStreamImpl_Read(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000205 IStream* iface,
206 void* pv, /* [length_is][size_is][out] */
Vincent Béron9a624912002-05-31 23:06:46 +0000207 ULONG cb, /* [in] */
208 ULONG* pcbRead) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000209{
210 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
211
212 void* supportBuffer;
213 ULONG bytesReadBuffer;
214 ULONG bytesToReadFromBuffer;
Francis Beaudetec2edc71999-05-02 11:41:10 +0000215
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200216 TRACE("(%p, %p, %d, %p)\n", iface,
Francis Beaudetec2edc71999-05-02 11:41:10 +0000217 pv, cb, pcbRead);
Vincent Béron9a624912002-05-31 23:06:46 +0000218
219 /*
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000220 * If the caller is not interested in the nubmer of bytes read,
221 * we use another buffer to avoid "if" statements in the code.
222 */
223 if (pcbRead==0)
224 pcbRead = &bytesReadBuffer;
Vincent Béron9a624912002-05-31 23:06:46 +0000225
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000226 /*
227 * Using the known size of the stream, calculate the number of bytes
228 * to read from the block chain
229 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000230 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000231
232 /*
233 * Lock the buffer in position and copy the data.
234 */
235 supportBuffer = GlobalLock(This->supportHandle);
Rob Shearman82cfed92007-05-17 17:06:56 +0100236 if (!supportBuffer)
237 {
238 WARN("read from invalid hglobal %p\n", This->supportHandle);
239 *pcbRead = 0;
240 return S_OK;
241 }
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000242
Ge van Geldorp399901e2004-01-23 01:51:33 +0000243 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000244
245 /*
246 * Move the current position to the new position
247 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000248 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000249
250 /*
251 * Return the number of bytes read.
252 */
253 *pcbRead = bytesToReadFromBuffer;
254
255 /*
256 * Cleanup
257 */
258 GlobalUnlock(This->supportHandle);
Vincent Béron9a624912002-05-31 23:06:46 +0000259
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000260 /*
Robert Shearmana552c2e2006-09-11 11:11:56 +0100261 * Always returns S_OK even if the end of the stream is reached before the
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000262 * buffer is filled
263 */
Vincent Béron9a624912002-05-31 23:06:46 +0000264
Robert Shearmana552c2e2006-09-11 11:11:56 +0100265 return S_OK;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000266}
Vincent Béron9a624912002-05-31 23:06:46 +0000267
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000268/***
269 * This method is part of the ISequentialStream interface.
270 *
271 * It writes a block of information to the stream at the current
272 * position. It then moves the current position at the end of the
273 * written block. If the stream is too small to fit the block,
274 * the stream is grown to fit.
275 *
276 * See the documentation of ISequentialStream for more info.
277 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000278static HRESULT WINAPI HGLOBALStreamImpl_Write(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000279 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000280 const void* pv, /* [size_is][in] */
281 ULONG cb, /* [in] */
282 ULONG* pcbWritten) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000283{
284 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
285
286 void* supportBuffer;
287 ULARGE_INTEGER newSize;
288 ULONG bytesWritten = 0;
Francis Beaudetec2edc71999-05-02 11:41:10 +0000289
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200290 TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
Vincent Béron9a624912002-05-31 23:06:46 +0000291
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000292 /*
293 * If the caller is not interested in the number of bytes written,
294 * we use another buffer to avoid "if" statements in the code.
295 */
296 if (pcbWritten == 0)
297 pcbWritten = &bytesWritten;
Vincent Béron9a624912002-05-31 23:06:46 +0000298
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000299 if (cb == 0)
Mike McCormack8dc5bd82006-08-14 14:06:53 +0900300 goto out;
301
Rob Shearman82cfed92007-05-17 17:06:56 +0100302 *pcbWritten = 0;
303
Mike McCormack8dc5bd82006-08-14 14:06:53 +0900304 newSize.u.HighPart = 0;
305 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
Vincent Béron9a624912002-05-31 23:06:46 +0000306
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000307 /*
308 * Verify if we need to grow the stream
309 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000310 if (newSize.u.LowPart > This->streamSize.u.LowPart)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000311 {
312 /* grow stream */
Robert Shearmandfa74b92006-01-03 12:07:49 +0100313 HRESULT hr = IStream_SetSize(iface, newSize);
314 if (FAILED(hr))
315 {
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200316 ERR("IStream_SetSize failed with error 0x%08x\n", hr);
Robert Shearmandfa74b92006-01-03 12:07:49 +0100317 return hr;
318 }
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000319 }
Vincent Béron9a624912002-05-31 23:06:46 +0000320
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000321 /*
322 * Lock the buffer in position and copy the data.
323 */
324 supportBuffer = GlobalLock(This->supportHandle);
Rob Shearman82cfed92007-05-17 17:06:56 +0100325 if (!supportBuffer)
326 {
327 WARN("write to invalid hglobal %p\n", This->supportHandle);
328 return S_OK;
329 }
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000330
Ge van Geldorp399901e2004-01-23 01:51:33 +0000331 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000332
333 /*
334 * Move the current position to the new position
335 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000336 This->currentPosition.u.LowPart+=cb;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000337
338 /*
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000339 * Cleanup
340 */
341 GlobalUnlock(This->supportHandle);
Vincent Béron9a624912002-05-31 23:06:46 +0000342
Mike McCormack8dc5bd82006-08-14 14:06:53 +0900343out:
344 /*
345 * Return the number of bytes read.
346 */
347 *pcbWritten = cb;
348
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000349 return S_OK;
350}
351
352/***
353 * This method is part of the IStream interface.
354 *
355 * It will move the current stream pointer according to the parameters
356 * given.
357 *
358 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000359 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000360static HRESULT WINAPI HGLOBALStreamImpl_Seek(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000361 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000362 LARGE_INTEGER dlibMove, /* [in] */
363 DWORD dwOrigin, /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000364 ULARGE_INTEGER* plibNewPosition) /* [out] */
365{
366 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
367
368 ULARGE_INTEGER newPosition;
369
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200370 TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
Ge van Geldorp399901e2004-01-23 01:51:33 +0000371 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
Francis Beaudetec2edc71999-05-02 11:41:10 +0000372
Vincent Béron9a624912002-05-31 23:06:46 +0000373 /*
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000374 * The file pointer is moved depending on the given "function"
375 * parameter.
376 */
377 switch (dwOrigin)
378 {
379 case STREAM_SEEK_SET:
Ge van Geldorp399901e2004-01-23 01:51:33 +0000380 newPosition.u.HighPart = 0;
381 newPosition.u.LowPart = 0;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000382 break;
383 case STREAM_SEEK_CUR:
Juergen Schmied33817372002-07-01 18:10:34 +0000384 newPosition = This->currentPosition;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000385 break;
386 case STREAM_SEEK_END:
Juergen Schmied33817372002-07-01 18:10:34 +0000387 newPosition = This->streamSize;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000388 break;
389 default:
390 return STG_E_INVALIDFUNCTION;
391 }
392
393 /*
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000394 * Move the actual file pointer
395 * If the file pointer ends-up after the end of the stream, the next Write operation will
396 * make the file larger. This is how it is documented.
397 */
Robert Shearman398a5952004-12-14 11:35:12 +0000398 if (dlibMove.QuadPart < 0 && newPosition.QuadPart < -dlibMove.QuadPart) return STG_E_INVALIDFUNCTION;
399
Juergen Schmied33817372002-07-01 18:10:34 +0000400 newPosition.QuadPart = RtlLargeIntegerAdd(newPosition.QuadPart, dlibMove.QuadPart);
Juergen Schmied33817372002-07-01 18:10:34 +0000401
402 if (plibNewPosition) *plibNewPosition = newPosition;
403 This->currentPosition = newPosition;
Vincent Béron9a624912002-05-31 23:06:46 +0000404
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000405 return S_OK;
406}
407
408/***
409 * This method is part of the IStream interface.
410 *
411 * It will change the size of a stream.
412 *
413 * TODO: Switch from small blocks to big blocks and vice versa.
414 *
415 * See the documentation of IStream for more info.
416 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000417static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000418 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000419 ULARGE_INTEGER libNewSize) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000420{
421 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
Dimitrie O. Pauna1cee572003-11-26 03:36:18 +0000422 HGLOBAL supportHandle;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000423
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200424 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
Francis Beaudetec2edc71999-05-02 11:41:10 +0000425
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000426 /*
Robert Shearmanbfc1bdc2006-09-11 11:13:34 +0100427 * HighPart is ignored as shown in tests
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000428 */
Vincent Béron9a624912002-05-31 23:06:46 +0000429
Ge van Geldorp399901e2004-01-23 01:51:33 +0000430 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000431 return S_OK;
432
433 /*
434 * Re allocate the HGlobal to fit the new size of the stream.
435 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000436 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000437
Dimitrie O. Pauna1cee572003-11-26 03:36:18 +0000438 if (supportHandle == 0)
Rob Shearmanfb883d82006-12-17 23:48:25 +0000439 return E_OUTOFMEMORY;
Dimitrie O. Pauna1cee572003-11-26 03:36:18 +0000440
441 This->supportHandle = supportHandle;
Ge van Geldorp399901e2004-01-23 01:51:33 +0000442 This->streamSize.u.LowPart = libNewSize.u.LowPart;
Vincent Béron9a624912002-05-31 23:06:46 +0000443
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000444 return S_OK;
445}
Vincent Béron9a624912002-05-31 23:06:46 +0000446
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000447/***
448 * This method is part of the IStream interface.
449 *
450 * It will copy the 'cb' Bytes to 'pstm' IStream.
451 *
452 * See the documentation of IStream for more info.
453 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000454static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000455 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000456 IStream* pstm, /* [unique][in] */
457 ULARGE_INTEGER cb, /* [in] */
458 ULARGE_INTEGER* pcbRead, /* [out] */
459 ULARGE_INTEGER* pcbWritten) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000460{
461 HRESULT hr = S_OK;
462 BYTE tmpBuffer[128];
463 ULONG bytesRead, bytesWritten, copySize;
464 ULARGE_INTEGER totalBytesRead;
465 ULARGE_INTEGER totalBytesWritten;
466
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200467 TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
Ge van Geldorp399901e2004-01-23 01:51:33 +0000468 cb.u.LowPart, pcbRead, pcbWritten);
Francis Beaudetec2edc71999-05-02 11:41:10 +0000469
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000470 /*
471 * Sanity check
472 */
473 if ( pstm == 0 )
474 return STG_E_INVALIDPOINTER;
475
Ge van Geldorp399901e2004-01-23 01:51:33 +0000476 totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
477 totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000478
479 /*
480 * use stack to store data temporarly
481 * there is surely more performant way of doing it, for now this basic
482 * implementation will do the job
483 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000484 while ( cb.u.LowPart > 0 )
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000485 {
Ge van Geldorp399901e2004-01-23 01:51:33 +0000486 if ( cb.u.LowPart >= 128 )
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000487 copySize = 128;
488 else
Ge van Geldorp399901e2004-01-23 01:51:33 +0000489 copySize = cb.u.LowPart;
Vincent Béron9a624912002-05-31 23:06:46 +0000490
Rob Shearmanfd071912007-01-09 17:16:07 +0000491 hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
492 if (FAILED(hr))
493 break;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000494
Ge van Geldorp399901e2004-01-23 01:51:33 +0000495 totalBytesRead.u.LowPart += bytesRead;
Vincent Béron9a624912002-05-31 23:06:46 +0000496
Rob Shearmanfd071912007-01-09 17:16:07 +0000497 if (bytesRead)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000498 {
Rob Shearmanfd071912007-01-09 17:16:07 +0000499 hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
500 if (FAILED(hr))
501 break;
502
503 totalBytesWritten.u.LowPart += bytesWritten;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000504 }
Vincent Béron9a624912002-05-31 23:06:46 +0000505
Francis Beaudetec2edc71999-05-02 11:41:10 +0000506 if (bytesRead!=copySize)
Ge van Geldorp399901e2004-01-23 01:51:33 +0000507 cb.u.LowPart = 0;
Francis Beaudetec2edc71999-05-02 11:41:10 +0000508 else
Ge van Geldorp399901e2004-01-23 01:51:33 +0000509 cb.u.LowPart -= bytesRead;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000510 }
511
512 /*
513 * Update number of bytes read and written
514 */
515 if (pcbRead)
516 {
Ge van Geldorp399901e2004-01-23 01:51:33 +0000517 pcbRead->u.LowPart = totalBytesRead.u.LowPart;
518 pcbRead->u.HighPart = totalBytesRead.u.HighPart;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000519 }
520
521 if (pcbWritten)
522 {
Ge van Geldorp399901e2004-01-23 01:51:33 +0000523 pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
524 pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000525 }
526 return hr;
527}
528
529/***
530 * This method is part of the IStream interface.
531 *
Vincent Béron9a624912002-05-31 23:06:46 +0000532 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000533 * This is what the documentation tells us.
534 *
535 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000536 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000537static HRESULT WINAPI HGLOBALStreamImpl_Commit(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000538 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000539 DWORD grfCommitFlags) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000540{
541 return S_OK;
542}
543
544/***
545 * This method is part of the IStream interface.
546 *
Vincent Béron9a624912002-05-31 23:06:46 +0000547 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000548 * This is what the documentation tells us.
549 *
550 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000551 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000552static HRESULT WINAPI HGLOBALStreamImpl_Revert(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000553 IStream* iface)
554{
555 return S_OK;
556}
557
558/***
559 * This method is part of the IStream interface.
560 *
Vincent Béron9a624912002-05-31 23:06:46 +0000561 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000562 * This is what the documentation tells us.
563 *
564 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000565 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000566static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000567 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000568 ULARGE_INTEGER libOffset, /* [in] */
569 ULARGE_INTEGER cb, /* [in] */
570 DWORD dwLockType) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000571{
Robert Shearman355903d2006-09-11 11:12:32 +0100572 return STG_E_INVALIDFUNCTION;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000573}
574
575/*
576 * This method is part of the IStream interface.
577 *
Vincent Béron9a624912002-05-31 23:06:46 +0000578 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000579 * This is what the documentation tells us.
580 *
581 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000582 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000583static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000584 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000585 ULARGE_INTEGER libOffset, /* [in] */
586 ULARGE_INTEGER cb, /* [in] */
587 DWORD dwLockType) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000588{
589 return S_OK;
590}
591
592/***
593 * This method is part of the IStream interface.
594 *
595 * This method returns information about the current
596 * stream.
597 *
598 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000599 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000600static HRESULT WINAPI HGLOBALStreamImpl_Stat(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000601 IStream* iface,
602 STATSTG* pstatstg, /* [out] */
Vincent Béron9a624912002-05-31 23:06:46 +0000603 DWORD grfStatFlag) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000604{
605 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
606
607 memset(pstatstg, 0, sizeof(STATSTG));
608
609 pstatstg->pwcsName = NULL;
610 pstatstg->type = STGTY_STREAM;
611 pstatstg->cbSize = This->streamSize;
612
613 return S_OK;
614}
Vincent Béron9a624912002-05-31 23:06:46 +0000615
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000616static HRESULT WINAPI HGLOBALStreamImpl_Clone(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000617 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000618 IStream** ppstm) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000619{
Alberto Massari544efc82002-11-12 02:13:49 +0000620 ULARGE_INTEGER dummy;
621 LARGE_INTEGER offset;
622 HRESULT hr;
623 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
624 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
625 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
626 if(FAILED(hr))
627 return hr;
628 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
629 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
630 return S_OK;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000631}
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000632
633/*
634 * Virtual function table for the HGLOBALStreamImpl class.
635 */
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +0000636static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000637{
638 HGLOBALStreamImpl_QueryInterface,
639 HGLOBALStreamImpl_AddRef,
640 HGLOBALStreamImpl_Release,
641 HGLOBALStreamImpl_Read,
642 HGLOBALStreamImpl_Write,
643 HGLOBALStreamImpl_Seek,
644 HGLOBALStreamImpl_SetSize,
645 HGLOBALStreamImpl_CopyTo,
646 HGLOBALStreamImpl_Commit,
647 HGLOBALStreamImpl_Revert,
648 HGLOBALStreamImpl_LockRegion,
649 HGLOBALStreamImpl_UnlockRegion,
650 HGLOBALStreamImpl_Stat,
651 HGLOBALStreamImpl_Clone
652};
653
654/******************************************************************************
655** HGLOBALStreamImpl implementation
656*/
657
658/***
659 * This is the constructor for the HGLOBALStreamImpl class.
660 *
661 * Params:
662 * hGlobal - Handle that will support the stream. can be NULL.
663 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
664 * when the IStream object is destroyed.
665 */
Andrew Talbot02a5f732007-01-18 21:50:56 +0000666static HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000667 HGLOBAL hGlobal,
668 BOOL fDeleteOnRelease)
669{
670 HGLOBALStreamImpl* newStream;
671
672 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
673
674 if (newStream!=0)
675 {
676 /*
677 * Set-up the virtual function table and reference count.
678 */
679 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
680 newStream->ref = 0;
681
682 /*
683 * Initialize the support.
684 */
685 newStream->supportHandle = hGlobal;
686 newStream->deleteOnRelease = fDeleteOnRelease;
687
688 /*
689 * This method will allocate a handle if one is not supplied.
690 */
691 if (!newStream->supportHandle)
692 {
693 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
694 GMEM_SHARE, 0);
695 }
696
697 /*
698 * Start the stream at the beginning.
699 */
700 newStream->currentPosition.u.HighPart = 0;
701 newStream->currentPosition.u.LowPart = 0;
702
703 /*
704 * Initialize the size of the stream to the size of the handle.
705 */
706 newStream->streamSize.u.HighPart = 0;
707 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
708 }
709
710 return newStream;
711}
712
713
714/***********************************************************************
715 * CreateStreamOnHGlobal [OLE32.@]
716 */
717HRESULT WINAPI CreateStreamOnHGlobal(
718 HGLOBAL hGlobal,
719 BOOL fDeleteOnRelease,
720 LPSTREAM* ppstm)
721{
722 HGLOBALStreamImpl* newStream;
723
724 newStream = HGLOBALStreamImpl_Construct(hGlobal,
725 fDeleteOnRelease);
726
727 if (newStream!=NULL)
728 {
729 return IUnknown_QueryInterface((IUnknown*)newStream,
730 &IID_IStream,
731 (void**)ppstm);
732 }
733
734 return E_OUTOFMEMORY;
735}
736
737/***********************************************************************
738 * GetHGlobalFromStream [OLE32.@]
739 */
740HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
741{
742 HGLOBALStreamImpl* pStream;
743
744 if (pstm == NULL)
745 return E_INVALIDARG;
746
747 pStream = (HGLOBALStreamImpl*) pstm;
748
749 /*
750 * Verify that the stream object was created with CreateStreamOnHGlobal.
751 */
752 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
753 *phglobal = pStream->supportHandle;
754 else
755 {
756 *phglobal = 0;
757 return E_INVALIDARG;
758 }
759
760 return S_OK;
761}