blob: 68a1527a01285ac7455c608e2f874eb11c0f4aea [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 if ( pstm == 0 )
471 return STG_E_INVALIDPOINTER;
472
Huw Davies0aedac32008-03-06 12:05:52 +0000473 totalBytesRead.QuadPart = 0;
474 totalBytesWritten.QuadPart = 0;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000475
Huw Davies0aedac32008-03-06 12:05:52 +0000476 while ( cb.QuadPart > 0 )
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000477 {
Huw Davies0aedac32008-03-06 12:05:52 +0000478 if ( cb.QuadPart >= sizeof(tmpBuffer) )
479 copySize = sizeof(tmpBuffer);
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000480 else
Ge van Geldorp399901e2004-01-23 01:51:33 +0000481 copySize = cb.u.LowPart;
Vincent Béron9a624912002-05-31 23:06:46 +0000482
Rob Shearmanfd071912007-01-09 17:16:07 +0000483 hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
484 if (FAILED(hr))
485 break;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000486
Huw Davies0aedac32008-03-06 12:05:52 +0000487 totalBytesRead.QuadPart += bytesRead;
Vincent Béron9a624912002-05-31 23:06:46 +0000488
Rob Shearmanfd071912007-01-09 17:16:07 +0000489 if (bytesRead)
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000490 {
Rob Shearmanfd071912007-01-09 17:16:07 +0000491 hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
492 if (FAILED(hr))
493 break;
494
Huw Davies0aedac32008-03-06 12:05:52 +0000495 totalBytesWritten.QuadPart += bytesWritten;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000496 }
Vincent Béron9a624912002-05-31 23:06:46 +0000497
Francis Beaudetec2edc71999-05-02 11:41:10 +0000498 if (bytesRead!=copySize)
Huw Davies0aedac32008-03-06 12:05:52 +0000499 cb.QuadPart = 0;
Francis Beaudetec2edc71999-05-02 11:41:10 +0000500 else
Huw Davies0aedac32008-03-06 12:05:52 +0000501 cb.QuadPart -= bytesRead;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000502 }
503
Huw Davies0aedac32008-03-06 12:05:52 +0000504 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
505 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000506
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000507 return hr;
508}
509
510/***
511 * This method is part of the IStream interface.
512 *
Vincent Béron9a624912002-05-31 23:06:46 +0000513 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000514 * This is what the documentation tells us.
515 *
516 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000517 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000518static HRESULT WINAPI HGLOBALStreamImpl_Commit(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000519 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000520 DWORD grfCommitFlags) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000521{
522 return S_OK;
523}
524
525/***
526 * This method is part of the IStream interface.
527 *
Vincent Béron9a624912002-05-31 23:06:46 +0000528 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000529 * This is what the documentation tells us.
530 *
531 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000532 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000533static HRESULT WINAPI HGLOBALStreamImpl_Revert(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000534 IStream* iface)
535{
536 return S_OK;
537}
538
539/***
540 * This method is part of the IStream interface.
541 *
Vincent Béron9a624912002-05-31 23:06:46 +0000542 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000543 * This is what the documentation tells us.
544 *
545 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000546 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000547static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000548 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000549 ULARGE_INTEGER libOffset, /* [in] */
550 ULARGE_INTEGER cb, /* [in] */
551 DWORD dwLockType) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000552{
Robert Shearman355903d2006-09-11 11:12:32 +0100553 return STG_E_INVALIDFUNCTION;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000554}
555
556/*
557 * This method is part of the IStream interface.
558 *
Vincent Béron9a624912002-05-31 23:06:46 +0000559 * For streams supported by HGLOBALS, this function does nothing.
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000560 * This is what the documentation tells us.
561 *
562 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000563 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000564static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000565 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000566 ULARGE_INTEGER libOffset, /* [in] */
567 ULARGE_INTEGER cb, /* [in] */
568 DWORD dwLockType) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000569{
570 return S_OK;
571}
572
573/***
574 * This method is part of the IStream interface.
575 *
576 * This method returns information about the current
577 * stream.
578 *
579 * See the documentation of IStream for more info.
Vincent Béron9a624912002-05-31 23:06:46 +0000580 */
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000581static HRESULT WINAPI HGLOBALStreamImpl_Stat(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000582 IStream* iface,
583 STATSTG* pstatstg, /* [out] */
Vincent Béron9a624912002-05-31 23:06:46 +0000584 DWORD grfStatFlag) /* [in] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000585{
586 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
587
588 memset(pstatstg, 0, sizeof(STATSTG));
589
590 pstatstg->pwcsName = NULL;
591 pstatstg->type = STGTY_STREAM;
592 pstatstg->cbSize = This->streamSize;
593
594 return S_OK;
595}
Vincent Béron9a624912002-05-31 23:06:46 +0000596
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000597static HRESULT WINAPI HGLOBALStreamImpl_Clone(
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000598 IStream* iface,
Vincent Béron9a624912002-05-31 23:06:46 +0000599 IStream** ppstm) /* [out] */
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000600{
Alberto Massari544efc82002-11-12 02:13:49 +0000601 ULARGE_INTEGER dummy;
602 LARGE_INTEGER offset;
603 HRESULT hr;
604 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
605 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
606 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
607 if(FAILED(hr))
608 return hr;
609 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
610 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
611 return S_OK;
Francis Beaudet4f8b5a81999-04-24 12:00:31 +0000612}
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000613
614/*
615 * Virtual function table for the HGLOBALStreamImpl class.
616 */
Dmitry Timoshkoveba47f12005-06-06 19:50:35 +0000617static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000618{
619 HGLOBALStreamImpl_QueryInterface,
620 HGLOBALStreamImpl_AddRef,
621 HGLOBALStreamImpl_Release,
622 HGLOBALStreamImpl_Read,
623 HGLOBALStreamImpl_Write,
624 HGLOBALStreamImpl_Seek,
625 HGLOBALStreamImpl_SetSize,
626 HGLOBALStreamImpl_CopyTo,
627 HGLOBALStreamImpl_Commit,
628 HGLOBALStreamImpl_Revert,
629 HGLOBALStreamImpl_LockRegion,
630 HGLOBALStreamImpl_UnlockRegion,
631 HGLOBALStreamImpl_Stat,
632 HGLOBALStreamImpl_Clone
633};
634
635/******************************************************************************
636** HGLOBALStreamImpl implementation
637*/
638
639/***
640 * This is the constructor for the HGLOBALStreamImpl class.
641 *
642 * Params:
643 * hGlobal - Handle that will support the stream. can be NULL.
644 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
645 * when the IStream object is destroyed.
646 */
Andrew Talbot02a5f732007-01-18 21:50:56 +0000647static HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000648 HGLOBAL hGlobal,
649 BOOL fDeleteOnRelease)
650{
651 HGLOBALStreamImpl* newStream;
652
653 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
654
655 if (newStream!=0)
656 {
657 /*
658 * Set-up the virtual function table and reference count.
659 */
660 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
661 newStream->ref = 0;
662
663 /*
664 * Initialize the support.
665 */
666 newStream->supportHandle = hGlobal;
667 newStream->deleteOnRelease = fDeleteOnRelease;
668
669 /*
670 * This method will allocate a handle if one is not supplied.
671 */
672 if (!newStream->supportHandle)
673 {
674 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
675 GMEM_SHARE, 0);
676 }
677
678 /*
679 * Start the stream at the beginning.
680 */
681 newStream->currentPosition.u.HighPart = 0;
682 newStream->currentPosition.u.LowPart = 0;
683
684 /*
685 * Initialize the size of the stream to the size of the handle.
686 */
687 newStream->streamSize.u.HighPart = 0;
688 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
689 }
690
691 return newStream;
692}
693
694
695/***********************************************************************
696 * CreateStreamOnHGlobal [OLE32.@]
697 */
698HRESULT WINAPI CreateStreamOnHGlobal(
699 HGLOBAL hGlobal,
700 BOOL fDeleteOnRelease,
701 LPSTREAM* ppstm)
702{
703 HGLOBALStreamImpl* newStream;
704
Rob Shearman4edca3c2007-11-12 20:10:32 +0000705 if (!ppstm)
706 return E_INVALIDARG;
707
Marcus Meissner45bc1c22005-05-16 08:47:14 +0000708 newStream = HGLOBALStreamImpl_Construct(hGlobal,
709 fDeleteOnRelease);
710
711 if (newStream!=NULL)
712 {
713 return IUnknown_QueryInterface((IUnknown*)newStream,
714 &IID_IStream,
715 (void**)ppstm);
716 }
717
718 return E_OUTOFMEMORY;
719}
720
721/***********************************************************************
722 * GetHGlobalFromStream [OLE32.@]
723 */
724HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
725{
726 HGLOBALStreamImpl* pStream;
727
728 if (pstm == NULL)
729 return E_INVALIDARG;
730
731 pStream = (HGLOBALStreamImpl*) pstm;
732
733 /*
734 * Verify that the stream object was created with CreateStreamOnHGlobal.
735 */
736 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
737 *phglobal = pStream->supportHandle;
738 else
739 {
740 *phglobal = 0;
741 return E_INVALIDARG;
742 }
743
744 return S_OK;
745}