5da53e12ed4ce85354238ccaacbd9883f5df94f5
[reactos.git] / reactos / dll / win32 / ole32 / hglobalstream.c
1 /*
2 * HGLOBAL Stream implementation
3 *
4 * This file contains the implementation of the stream interface
5 * for streams contained supported by an HGLOBAL pointer.
6 *
7 * Copyright 1999 Francis Beaudet
8 *
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
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #define WIN32_NO_STATUS
25 #define _INC_WINDOWS
26
27 #include <config.h>
28
29 //#include <assert.h>
30 //#include <stdlib.h>
31 #include <stdarg.h>
32 //#include <stdio.h>
33 //#include <string.h>
34
35 #define COBJMACROS
36 #define NONAMELESSUNION
37 #define NONAMELESSSTRUCT
38
39 #include <windef.h>
40 #include <winbase.h>
41 //#include "winuser.h"
42 //#include "objbase.h"
43 #include <ole2.h>
44 //#include "winerror.h"
45 //#include "winternl.h"
46
47 #include <wine/debug.h>
48
49 WINE_DEFAULT_DEBUG_CHANNEL(storage);
50
51 /****************************************************************************
52 * HGLOBALStreamImpl definition.
53 *
54 * This class implements the IStream interface and represents a stream
55 * supported by an HGLOBAL pointer.
56 */
57 typedef struct
58 {
59 IStream IStream_iface;
60 LONG ref;
61
62 /* support for the stream */
63 HGLOBAL supportHandle;
64
65 /* if TRUE the HGLOBAL is destroyed when the stream is finally released */
66 BOOL deleteOnRelease;
67
68 /* size of the stream */
69 ULARGE_INTEGER streamSize;
70
71 /* current position of the cursor */
72 ULARGE_INTEGER currentPosition;
73 } HGLOBALStreamImpl;
74
75 static inline HGLOBALStreamImpl *impl_from_IStream(IStream *iface)
76 {
77 return CONTAINING_RECORD(iface, HGLOBALStreamImpl, IStream_iface);
78 }
79
80 static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
81 IStream* iface,
82 REFIID riid, /* [in] */
83 void** ppvObject) /* [iid_is][out] */
84 {
85 HGLOBALStreamImpl* This = impl_from_IStream(iface);
86
87 if (ppvObject==0)
88 return E_INVALIDARG;
89
90 *ppvObject = 0;
91
92 if (IsEqualIID(&IID_IUnknown, riid) ||
93 IsEqualIID(&IID_ISequentialStream, riid) ||
94 IsEqualIID(&IID_IStream, riid))
95 {
96 *ppvObject = This;
97 }
98
99 if ((*ppvObject)==0)
100 return E_NOINTERFACE;
101
102 IStream_AddRef(iface);
103
104 return S_OK;
105 }
106
107 static ULONG WINAPI HGLOBALStreamImpl_AddRef(IStream* iface)
108 {
109 HGLOBALStreamImpl* This = impl_from_IStream(iface);
110 return InterlockedIncrement(&This->ref);
111 }
112
113 static ULONG WINAPI HGLOBALStreamImpl_Release(
114 IStream* iface)
115 {
116 HGLOBALStreamImpl* This= impl_from_IStream(iface);
117 ULONG ref = InterlockedDecrement(&This->ref);
118
119 if (!ref)
120 {
121 if (This->deleteOnRelease)
122 {
123 GlobalFree(This->supportHandle);
124 This->supportHandle = NULL;
125 }
126
127 HeapFree(GetProcessHeap(), 0, This);
128 }
129
130 return ref;
131 }
132
133 /***
134 * This method is part of the ISequentialStream interface.
135 *
136 * If reads a block of information from the stream at the current
137 * position. It then moves the current position at the end of the
138 * read block
139 *
140 * See the documentation of ISequentialStream for more info.
141 */
142 static HRESULT WINAPI HGLOBALStreamImpl_Read(
143 IStream* iface,
144 void* pv, /* [length_is][size_is][out] */
145 ULONG cb, /* [in] */
146 ULONG* pcbRead) /* [out] */
147 {
148 HGLOBALStreamImpl* This = impl_from_IStream(iface);
149
150 void* supportBuffer;
151 ULONG bytesReadBuffer;
152 ULONG bytesToReadFromBuffer;
153
154 TRACE("(%p, %p, %d, %p)\n", iface,
155 pv, cb, pcbRead);
156
157 /*
158 * If the caller is not interested in the number of bytes read,
159 * we use another buffer to avoid "if" statements in the code.
160 */
161 if (pcbRead==0)
162 pcbRead = &bytesReadBuffer;
163
164 /*
165 * Using the known size of the stream, calculate the number of bytes
166 * to read from the block chain
167 */
168 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
169
170 /*
171 * Lock the buffer in position and copy the data.
172 */
173 supportBuffer = GlobalLock(This->supportHandle);
174 if (!supportBuffer)
175 {
176 WARN("read from invalid hglobal %p\n", This->supportHandle);
177 *pcbRead = 0;
178 return S_OK;
179 }
180
181 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
182
183 /*
184 * Move the current position to the new position
185 */
186 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
187
188 /*
189 * Return the number of bytes read.
190 */
191 *pcbRead = bytesToReadFromBuffer;
192
193 /*
194 * Cleanup
195 */
196 GlobalUnlock(This->supportHandle);
197
198 /*
199 * Always returns S_OK even if the end of the stream is reached before the
200 * buffer is filled
201 */
202
203 return S_OK;
204 }
205
206 /***
207 * This method is part of the ISequentialStream interface.
208 *
209 * It writes a block of information to the stream at the current
210 * position. It then moves the current position at the end of the
211 * written block. If the stream is too small to fit the block,
212 * the stream is grown to fit.
213 *
214 * See the documentation of ISequentialStream for more info.
215 */
216 static HRESULT WINAPI HGLOBALStreamImpl_Write(
217 IStream* iface,
218 const void* pv, /* [size_is][in] */
219 ULONG cb, /* [in] */
220 ULONG* pcbWritten) /* [out] */
221 {
222 HGLOBALStreamImpl* This = impl_from_IStream(iface);
223
224 void* supportBuffer;
225 ULARGE_INTEGER newSize;
226 ULONG bytesWritten = 0;
227
228 TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
229
230 /*
231 * If the caller is not interested in the number of bytes written,
232 * we use another buffer to avoid "if" statements in the code.
233 */
234 if (pcbWritten == 0)
235 pcbWritten = &bytesWritten;
236
237 if (cb == 0)
238 goto out;
239
240 *pcbWritten = 0;
241
242 newSize.u.HighPart = 0;
243 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
244
245 /*
246 * Verify if we need to grow the stream
247 */
248 if (newSize.u.LowPart > This->streamSize.u.LowPart)
249 {
250 /* grow stream */
251 HRESULT hr = IStream_SetSize(iface, newSize);
252 if (FAILED(hr))
253 {
254 ERR("IStream_SetSize failed with error 0x%08x\n", hr);
255 return hr;
256 }
257 }
258
259 /*
260 * Lock the buffer in position and copy the data.
261 */
262 supportBuffer = GlobalLock(This->supportHandle);
263 if (!supportBuffer)
264 {
265 WARN("write to invalid hglobal %p\n", This->supportHandle);
266 return S_OK;
267 }
268
269 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
270
271 /*
272 * Move the current position to the new position
273 */
274 This->currentPosition.u.LowPart+=cb;
275
276 /*
277 * Cleanup
278 */
279 GlobalUnlock(This->supportHandle);
280
281 out:
282 /*
283 * Return the number of bytes read.
284 */
285 *pcbWritten = cb;
286
287 return S_OK;
288 }
289
290 /***
291 * This method is part of the IStream interface.
292 *
293 * It will move the current stream pointer according to the parameters
294 * given.
295 *
296 * See the documentation of IStream for more info.
297 */
298 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
299 IStream* iface,
300 LARGE_INTEGER dlibMove, /* [in] */
301 DWORD dwOrigin, /* [in] */
302 ULARGE_INTEGER* plibNewPosition) /* [out] */
303 {
304 HGLOBALStreamImpl* This = impl_from_IStream(iface);
305
306 ULARGE_INTEGER newPosition = This->currentPosition;
307 HRESULT hr = S_OK;
308
309 TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
310 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
311
312 /*
313 * The file pointer is moved depending on the given "function"
314 * parameter.
315 */
316 switch (dwOrigin)
317 {
318 case STREAM_SEEK_SET:
319 newPosition.u.HighPart = 0;
320 newPosition.u.LowPart = 0;
321 break;
322 case STREAM_SEEK_CUR:
323 break;
324 case STREAM_SEEK_END:
325 newPosition = This->streamSize;
326 break;
327 default:
328 hr = STG_E_SEEKERROR;
329 goto end;
330 }
331
332 /*
333 * Move the actual file pointer
334 * If the file pointer ends-up after the end of the stream, the next Write operation will
335 * make the file larger. This is how it is documented.
336 */
337 newPosition.u.HighPart = 0;
338 newPosition.u.LowPart += dlibMove.QuadPart;
339
340 if (dlibMove.u.LowPart >= 0x80000000 &&
341 newPosition.u.LowPart >= dlibMove.u.LowPart)
342 {
343 /* We tried to seek backwards and went past the start. */
344 hr = STG_E_SEEKERROR;
345 goto end;
346 }
347
348 This->currentPosition = newPosition;
349
350 end:
351 if (plibNewPosition) *plibNewPosition = This->currentPosition;
352
353 return hr;
354 }
355
356 /***
357 * This method is part of the IStream interface.
358 *
359 * It will change the size of a stream.
360 *
361 * TODO: Switch from small blocks to big blocks and vice versa.
362 *
363 * See the documentation of IStream for more info.
364 */
365 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
366 IStream* iface,
367 ULARGE_INTEGER libNewSize) /* [in] */
368 {
369 HGLOBALStreamImpl* This = impl_from_IStream(iface);
370 HGLOBAL supportHandle;
371
372 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
373
374 /*
375 * HighPart is ignored as shown in tests
376 */
377
378 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
379 return S_OK;
380
381 /*
382 * Re allocate the HGlobal to fit the new size of the stream.
383 */
384 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
385
386 if (supportHandle == 0)
387 return E_OUTOFMEMORY;
388
389 This->supportHandle = supportHandle;
390 This->streamSize.u.LowPart = libNewSize.u.LowPart;
391
392 return S_OK;
393 }
394
395 /***
396 * This method is part of the IStream interface.
397 *
398 * It will copy the 'cb' Bytes to 'pstm' IStream.
399 *
400 * See the documentation of IStream for more info.
401 */
402 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
403 IStream* iface,
404 IStream* pstm, /* [unique][in] */
405 ULARGE_INTEGER cb, /* [in] */
406 ULARGE_INTEGER* pcbRead, /* [out] */
407 ULARGE_INTEGER* pcbWritten) /* [out] */
408 {
409 HRESULT hr = S_OK;
410 BYTE tmpBuffer[128];
411 ULONG bytesRead, bytesWritten, copySize;
412 ULARGE_INTEGER totalBytesRead;
413 ULARGE_INTEGER totalBytesWritten;
414
415 TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
416 cb.u.LowPart, pcbRead, pcbWritten);
417
418 if ( pstm == 0 )
419 return STG_E_INVALIDPOINTER;
420
421 totalBytesRead.QuadPart = 0;
422 totalBytesWritten.QuadPart = 0;
423
424 while ( cb.QuadPart > 0 )
425 {
426 if ( cb.QuadPart >= sizeof(tmpBuffer) )
427 copySize = sizeof(tmpBuffer);
428 else
429 copySize = cb.u.LowPart;
430
431 hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
432 if (FAILED(hr))
433 break;
434
435 totalBytesRead.QuadPart += bytesRead;
436
437 if (bytesRead)
438 {
439 hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
440 if (FAILED(hr))
441 break;
442
443 totalBytesWritten.QuadPart += bytesWritten;
444 }
445
446 if (bytesRead!=copySize)
447 cb.QuadPart = 0;
448 else
449 cb.QuadPart -= bytesRead;
450 }
451
452 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
453 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
454
455 return hr;
456 }
457
458 /***
459 * This method is part of the IStream interface.
460 *
461 * For streams supported by HGLOBALS, this function does nothing.
462 * This is what the documentation tells us.
463 *
464 * See the documentation of IStream for more info.
465 */
466 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
467 IStream* iface,
468 DWORD grfCommitFlags) /* [in] */
469 {
470 return S_OK;
471 }
472
473 /***
474 * This method is part of the IStream interface.
475 *
476 * For streams supported by HGLOBALS, this function does nothing.
477 * This is what the documentation tells us.
478 *
479 * See the documentation of IStream for more info.
480 */
481 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
482 IStream* iface)
483 {
484 return S_OK;
485 }
486
487 /***
488 * This method is part of the IStream interface.
489 *
490 * For streams supported by HGLOBALS, this function does nothing.
491 * This is what the documentation tells us.
492 *
493 * See the documentation of IStream for more info.
494 */
495 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
496 IStream* iface,
497 ULARGE_INTEGER libOffset, /* [in] */
498 ULARGE_INTEGER cb, /* [in] */
499 DWORD dwLockType) /* [in] */
500 {
501 return STG_E_INVALIDFUNCTION;
502 }
503
504 /*
505 * This method is part of the IStream interface.
506 *
507 * For streams supported by HGLOBALS, this function does nothing.
508 * This is what the documentation tells us.
509 *
510 * See the documentation of IStream for more info.
511 */
512 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
513 IStream* iface,
514 ULARGE_INTEGER libOffset, /* [in] */
515 ULARGE_INTEGER cb, /* [in] */
516 DWORD dwLockType) /* [in] */
517 {
518 return S_OK;
519 }
520
521 /***
522 * This method is part of the IStream interface.
523 *
524 * This method returns information about the current
525 * stream.
526 *
527 * See the documentation of IStream for more info.
528 */
529 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
530 IStream* iface,
531 STATSTG* pstatstg, /* [out] */
532 DWORD grfStatFlag) /* [in] */
533 {
534 HGLOBALStreamImpl* This = impl_from_IStream(iface);
535
536 memset(pstatstg, 0, sizeof(STATSTG));
537
538 pstatstg->pwcsName = NULL;
539 pstatstg->type = STGTY_STREAM;
540 pstatstg->cbSize = This->streamSize;
541
542 return S_OK;
543 }
544
545 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
546 IStream* iface,
547 IStream** ppstm) /* [out] */
548 {
549 HGLOBALStreamImpl* This = impl_from_IStream(iface);
550 ULARGE_INTEGER dummy;
551 LARGE_INTEGER offset;
552 HRESULT hr;
553
554 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
555 hr = CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
556 if(FAILED(hr))
557 return hr;
558 offset.QuadPart = (LONGLONG)This->currentPosition.QuadPart;
559 IStream_Seek(*ppstm, offset, STREAM_SEEK_SET, &dummy);
560 return S_OK;
561 }
562
563 static const IStreamVtbl HGLOBALStreamImplVtbl =
564 {
565 HGLOBALStreamImpl_QueryInterface,
566 HGLOBALStreamImpl_AddRef,
567 HGLOBALStreamImpl_Release,
568 HGLOBALStreamImpl_Read,
569 HGLOBALStreamImpl_Write,
570 HGLOBALStreamImpl_Seek,
571 HGLOBALStreamImpl_SetSize,
572 HGLOBALStreamImpl_CopyTo,
573 HGLOBALStreamImpl_Commit,
574 HGLOBALStreamImpl_Revert,
575 HGLOBALStreamImpl_LockRegion,
576 HGLOBALStreamImpl_UnlockRegion,
577 HGLOBALStreamImpl_Stat,
578 HGLOBALStreamImpl_Clone
579 };
580
581 /***********************************************************************
582 * CreateStreamOnHGlobal [OLE32.@]
583 */
584 HRESULT WINAPI CreateStreamOnHGlobal(
585 HGLOBAL hGlobal,
586 BOOL fDeleteOnRelease,
587 LPSTREAM* ppstm)
588 {
589 HGLOBALStreamImpl* This;
590
591 if (!ppstm)
592 return E_INVALIDARG;
593
594 This = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
595 if (!This) return E_OUTOFMEMORY;
596
597 This->IStream_iface.lpVtbl = &HGLOBALStreamImplVtbl;
598 This->ref = 1;
599
600 /* initialize the support */
601 This->supportHandle = hGlobal;
602 This->deleteOnRelease = fDeleteOnRelease;
603
604 /* allocate a handle if one is not supplied */
605 if (!This->supportHandle)
606 This->supportHandle = GlobalAlloc(GMEM_MOVEABLE|GMEM_NODISCARD|GMEM_SHARE, 0);
607
608 /* start at the beginning */
609 This->currentPosition.u.HighPart = 0;
610 This->currentPosition.u.LowPart = 0;
611
612 /* initialize the size of the stream to the size of the handle */
613 This->streamSize.u.HighPart = 0;
614 This->streamSize.u.LowPart = GlobalSize(This->supportHandle);
615
616 *ppstm = &This->IStream_iface;
617
618 return S_OK;
619 }
620
621 /***********************************************************************
622 * GetHGlobalFromStream [OLE32.@]
623 */
624 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
625 {
626 HGLOBALStreamImpl* pStream;
627
628 if (pstm == NULL)
629 return E_INVALIDARG;
630
631 pStream = (HGLOBALStreamImpl*) pstm;
632
633 /*
634 * Verify that the stream object was created with CreateStreamOnHGlobal.
635 */
636 if (pStream->IStream_iface.lpVtbl == &HGLOBALStreamImplVtbl)
637 *phglobal = pStream->supportHandle;
638 else
639 {
640 *phglobal = 0;
641 return E_INVALIDARG;
642 }
643
644 return S_OK;
645 }