blob: 5742f99619a9a6e66a7a4c9dcecd49bb9d08b7a5 [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 {
Michael Stefaniuc04497012009-02-11 10:48:38 +0100154 *ppvObject = This;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000155 }
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
Reece Dunnf08206b2010-02-25 09:14:37 +0000368 ULARGE_INTEGER newPosition = This->currentPosition;
369 HRESULT hr = S_OK;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000370
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200371 TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
Ge van Geldorp399901e2004-01-23 01:51:33 +0000372 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
Francis Beaudetec2edc71999-05-02 11:41:10 +0000373
Reece Dunnf08206b2010-02-25 09:14:37 +0000374 if (dlibMove.u.LowPart >= 0x80000000)
375 {
376 hr = STG_E_SEEKERROR;
377 goto end;
378 }
379
Vincent Béron9a624912002-05-31 23:06:46 +0000380 /*
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000381 * The file pointer is moved depending on the given "function"
382 * parameter.
383 */
384 switch (dwOrigin)
385 {
386 case STREAM_SEEK_SET:
Ge van Geldorp399901e2004-01-23 01:51:33 +0000387 newPosition.u.HighPart = 0;
388 newPosition.u.LowPart = 0;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000389 break;
390 case STREAM_SEEK_CUR:
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000391 break;
392 case STREAM_SEEK_END:
Juergen Schmied33817372002-07-01 18:10:34 +0000393 newPosition = This->streamSize;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000394 break;
395 default:
Reece Dunnf08206b2010-02-25 09:14:37 +0000396 hr = STG_E_SEEKERROR;
397 goto end;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000398 }
399
400 /*
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000401 * Move the actual file pointer
402 * If the file pointer ends-up after the end of the stream, the next Write operation will
403 * make the file larger. This is how it is documented.
404 */
Reece Dunnf08206b2010-02-25 09:14:37 +0000405 newPosition.u.HighPart = 0;
406 newPosition.u.LowPart += dlibMove.QuadPart;
Robert Shearman398a5952004-12-14 11:35:12 +0000407
Reece Dunnf08206b2010-02-25 09:14:37 +0000408end:
Juergen Schmied33817372002-07-01 18:10:34 +0000409 if (plibNewPosition) *plibNewPosition = newPosition;
410 This->currentPosition = newPosition;
Vincent Béron9a624912002-05-31 23:06:46 +0000411
Reece Dunnf08206b2010-02-25 09:14:37 +0000412 return hr;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000413}
414
415/***
416 * This method is part of the IStream interface.
417 *
418 * It will change the size of a stream.
419 *
420 * TODO: Switch from small blocks to big blocks and vice versa.
421 *
422 * See the documentation of IStream for more info.
423 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000424static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000425 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000426 ULARGE_INTEGER libNewSize) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000427{
428 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
Dimitrie O. Pauna1cee572003-11-26 03:36:18 +0000429 HGLOBAL supportHandle;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000430
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200431 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
Francis Beaudetec2edc71999-05-02 11:41:10 +0000432
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000433 /*
Robert Shearmanbfc1bdc2006-09-11 11:13:34 +0100434 * HighPart is ignored as shown in tests
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000435 */
Vincent Béron9a624912002-05-31 23:06:46 +0000436
Ge van Geldorp399901e2004-01-23 01:51:33 +0000437 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000438 return S_OK;
439
440 /*
441 * Re allocate the HGlobal to fit the new size of the stream.
442 */
Ge van Geldorp399901e2004-01-23 01:51:33 +0000443 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000444
Dimitrie O. Pauna1cee572003-11-26 03:36:18 +0000445 if (supportHandle == 0)
Rob Shearmanfb883d82006-12-17 23:48:25 +0000446 return E_OUTOFMEMORY;
Dimitrie O. Pauna1cee572003-11-26 03:36:18 +0000447
448 This->supportHandle = supportHandle;
Ge van Geldorp399901e2004-01-23 01:51:33 +0000449 This->streamSize.u.LowPart = libNewSize.u.LowPart;
Vincent Béron9a624912002-05-31 23:06:46 +0000450
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000451 return S_OK;
452}
Vincent Béron9a624912002-05-31 23:06:46 +0000453
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000454/***
455 * This method is part of the IStream interface.
456 *
457 * It will copy the 'cb' Bytes to 'pstm' IStream.
458 *
459 * See the documentation of IStream for more info.
460 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000461static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000462 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000463 IStream* pstm, /* [unique][in] */
464 ULARGE_INTEGER cb, /* [in] */
465 ULARGE_INTEGER* pcbRead, /* [out] */
466 ULARGE_INTEGER* pcbWritten) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000467{
468 HRESULT hr = S_OK;
469 BYTE tmpBuffer[128];
470 ULONG bytesRead, bytesWritten, copySize;
471 ULARGE_INTEGER totalBytesRead;
472 ULARGE_INTEGER totalBytesWritten;
473
Michael Stefaniuc21ff87b2006-10-15 12:28:08 +0200474 TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
Ge van Geldorp399901e2004-01-23 01:51:33 +0000475 cb.u.LowPart, pcbRead, pcbWritten);
Francis Beaudetec2edc71999-05-02 11:41:10 +0000476
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000477 if ( pstm == 0 )
478 return STG_E_INVALIDPOINTER;
479
Huw Davies0aedac32008-03-06 12:05:52 +0000480 totalBytesRead.QuadPart = 0;
481 totalBytesWritten.QuadPart = 0;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000482
Huw Davies0aedac32008-03-06 12:05:52 +0000483 while ( cb.QuadPart > 0 )
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000484 {
Huw Davies0aedac32008-03-06 12:05:52 +0000485 if ( cb.QuadPart >= sizeof(tmpBuffer) )
486 copySize = sizeof(tmpBuffer);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000487 else
Ge van Geldorp399901e2004-01-23 01:51:33 +0000488 copySize = cb.u.LowPart;
Vincent Béron9a624912002-05-31 23:06:46 +0000489
Rob Shearmanfd071912007-01-09 17:16:07 +0000490 hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
491 if (FAILED(hr))
492 break;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000493
Huw Davies0aedac32008-03-06 12:05:52 +0000494 totalBytesRead.QuadPart += bytesRead;
Vincent Béron9a624912002-05-31 23:06:46 +0000495
Rob Shearmanfd071912007-01-09 17:16:07 +0000496 if (bytesRead)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000497 {
Rob Shearmanfd071912007-01-09 17:16:07 +0000498 hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
499 if (FAILED(hr))
500 break;
501
Huw Davies0aedac32008-03-06 12:05:52 +0000502 totalBytesWritten.QuadPart += bytesWritten;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000503 }
Vincent Béron9a624912002-05-31 23:06:46 +0000504
Francis Beaudetec2edc71999-05-02 11:41:10 +0000505 if (bytesRead!=copySize)
Huw Davies0aedac32008-03-06 12:05:52 +0000506 cb.QuadPart = 0;
Francis Beaudetec2edc71999-05-02 11:41:10 +0000507 else
Huw Davies0aedac32008-03-06 12:05:52 +0000508 cb.QuadPart -= bytesRead;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000509 }
510
Huw Davies0aedac32008-03-06 12:05:52 +0000511 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
512 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000513
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000514 return hr;
515}
516
517/***
518 * This method is part of the IStream interface.
519 *
Vincent Béron9a624912002-05-31 23:06:46 +0000520 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000521 * This is what the documentation tells us.
522 *
523 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000524 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000525static HRESULT WINAPI HGLOBALStreamImpl_Commit(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000526 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000527 DWORD grfCommitFlags) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000528{
529 return S_OK;
530}
531
532/***
533 * This method is part of the IStream interface.
534 *
Vincent Béron9a624912002-05-31 23:06:46 +0000535 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000536 * This is what the documentation tells us.
537 *
538 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000539 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000540static HRESULT WINAPI HGLOBALStreamImpl_Revert(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000541 IStream* iface)
542{
543 return S_OK;
544}
545
546/***
547 * This method is part of the IStream interface.
548 *
Vincent Béron9a624912002-05-31 23:06:46 +0000549 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000550 * This is what the documentation tells us.
551 *
552 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000553 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000554static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000555 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000556 ULARGE_INTEGER libOffset, /* [in] */
557 ULARGE_INTEGER cb, /* [in] */
558 DWORD dwLockType) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000559{
Robert Shearman355903d2006-09-11 11:12:32 +0100560 return STG_E_INVALIDFUNCTION;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000561}
562
563/*
564 * This method is part of the IStream interface.
565 *
Vincent Béron9a624912002-05-31 23:06:46 +0000566 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000567 * This is what the documentation tells us.
568 *
569 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000570 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000571static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000572 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000573 ULARGE_INTEGER libOffset, /* [in] */
574 ULARGE_INTEGER cb, /* [in] */
575 DWORD dwLockType) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000576{
577 return S_OK;
578}
579
580/***
581 * This method is part of the IStream interface.
582 *
583 * This method returns information about the current
584 * stream.
585 *
586 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000587 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000588static HRESULT WINAPI HGLOBALStreamImpl_Stat(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000589 IStream* iface,
590 STATSTG* pstatstg, /* [out] */
Vincent Béron9a624912002-05-31 23:06:46 +0000591 DWORD grfStatFlag) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000592{
593 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
594
595 memset(pstatstg, 0, sizeof(STATSTG));
596
597 pstatstg->pwcsName = NULL;
598 pstatstg->type = STGTY_STREAM;
599 pstatstg->cbSize = This->streamSize;
600
601 return S_OK;
602}
Vincent Béron9a624912002-05-31 23:06:46 +0000603
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000604static HRESULT WINAPI HGLOBALStreamImpl_Clone(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000605 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000606 IStream** ppstm) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000607{
Alberto Massari544efc82002-11-12 02:13:49 +0000608 ULARGE_INTEGER dummy;
609 LARGE_INTEGER offset;
610 HRESULT hr;
611 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
612 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
613 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
614 if(FAILED(hr))
615 return hr;
616 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
617 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
618 return S_OK;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000619}
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000620
621/*
622 * Virtual function table for the HGLOBALStreamImpl class.
623 */
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +0000624static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000625{
626 HGLOBALStreamImpl_QueryInterface,
627 HGLOBALStreamImpl_AddRef,
628 HGLOBALStreamImpl_Release,
629 HGLOBALStreamImpl_Read,
630 HGLOBALStreamImpl_Write,
631 HGLOBALStreamImpl_Seek,
632 HGLOBALStreamImpl_SetSize,
633 HGLOBALStreamImpl_CopyTo,
634 HGLOBALStreamImpl_Commit,
635 HGLOBALStreamImpl_Revert,
636 HGLOBALStreamImpl_LockRegion,
637 HGLOBALStreamImpl_UnlockRegion,
638 HGLOBALStreamImpl_Stat,
639 HGLOBALStreamImpl_Clone
640};
641
642/******************************************************************************
643** HGLOBALStreamImpl implementation
644*/
645
646/***
647 * This is the constructor for the HGLOBALStreamImpl class.
648 *
649 * Params:
650 * hGlobal - Handle that will support the stream. can be NULL.
651 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
652 * when the IStream object is destroyed.
653 */
Andrew Talbot02a5f732007-01-18 21:50:56 +0000654static HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000655 HGLOBAL hGlobal,
656 BOOL fDeleteOnRelease)
657{
658 HGLOBALStreamImpl* newStream;
659
660 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
661
662 if (newStream!=0)
663 {
664 /*
665 * Set-up the virtual function table and reference count.
666 */
667 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
668 newStream->ref = 0;
669
670 /*
671 * Initialize the support.
672 */
673 newStream->supportHandle = hGlobal;
674 newStream->deleteOnRelease = fDeleteOnRelease;
675
676 /*
677 * This method will allocate a handle if one is not supplied.
678 */
679 if (!newStream->supportHandle)
680 {
681 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
682 GMEM_SHARE, 0);
683 }
684
685 /*
686 * Start the stream at the beginning.
687 */
688 newStream->currentPosition.u.HighPart = 0;
689 newStream->currentPosition.u.LowPart = 0;
690
691 /*
692 * Initialize the size of the stream to the size of the handle.
693 */
694 newStream->streamSize.u.HighPart = 0;
695 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
696 }
697
698 return newStream;
699}
700
701
702/***********************************************************************
703 * CreateStreamOnHGlobal [OLE32.@]
704 */
705HRESULT WINAPI CreateStreamOnHGlobal(
706 HGLOBAL hGlobal,
707 BOOL fDeleteOnRelease,
708 LPSTREAM* ppstm)
709{
710 HGLOBALStreamImpl* newStream;
711
Rob Shearman4edca3c2007-11-12 20:10:32 +0000712 if (!ppstm)
713 return E_INVALIDARG;
714
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000715 newStream = HGLOBALStreamImpl_Construct(hGlobal,
716 fDeleteOnRelease);
717
718 if (newStream!=NULL)
719 {
720 return IUnknown_QueryInterface((IUnknown*)newStream,
721 &IID_IStream,
722 (void**)ppstm);
723 }
724
725 return E_OUTOFMEMORY;
726}
727
728/***********************************************************************
729 * GetHGlobalFromStream [OLE32.@]
730 */
731HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
732{
733 HGLOBALStreamImpl* pStream;
734
735 if (pstm == NULL)
736 return E_INVALIDARG;
737
738 pStream = (HGLOBALStreamImpl*) pstm;
739
740 /*
741 * Verify that the stream object was created with CreateStreamOnHGlobal.
742 */
743 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
744 *phglobal = pStream->supportHandle;
745 else
746 {
747 *phglobal = 0;
748 return E_INVALIDARG;
749 }
750
751 return S_OK;
752}