Fix remaining signed/unsigned mismatches.
[wine.git] / dlls / ole32 / hglobalstream.c
blob9d68ecbb33a235bfd3ae9a4eb4d7ae7a21e53949
1 /*
2 * HGLOBAL Stream implementation
4 * This file contains the implementation of the stream interface
5 * for streams contained supported by an HGLOBAL pointer.
7 * Copyright 1999 Francis Beaudet
8 */
9 #include <assert.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
14 #include "winbase.h"
15 #include "winerror.h"
16 #include "debugtools.h"
18 #include "objbase.h"
20 DEFAULT_DEBUG_CHANNEL(storage);
22 /****************************************************************************
23 * HGLOBALStreamImpl definition.
25 * This class imlements the IStream inteface and represents a stream
26 * supported by an HGLOBAL pointer.
28 struct HGLOBALStreamImpl
30 ICOM_VFIELD(IStream); /* Needs to be the first item in the stuct
31 * since we want to cast this in a IStream pointer */
34 * Reference count
36 ULONG ref;
39 * Support for the stream
41 HGLOBAL supportHandle;
44 * This flag is TRUE if the HGLOBAL is destroyed when the stream
45 * is finally released.
47 BOOL deleteOnRelease;
50 * Helper variable that contains the size of the stream
52 ULARGE_INTEGER streamSize;
55 * This is the current position of the cursor in the stream
57 ULARGE_INTEGER currentPosition;
60 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
63 * Method definition for the StgStreamImpl class.
65 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
66 HGLOBAL hGlobal,
67 BOOL fDeleteOnRelease);
69 void HGLOBALStreamImpl_Destroy(
70 HGLOBALStreamImpl* This);
72 void HGLOBALStreamImpl_OpenBlockChain(
73 HGLOBALStreamImpl* This);
75 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
76 IStream* iface,
77 REFIID riid, /* [in] */
78 void** ppvObject); /* [iid_is][out] */
80 ULONG WINAPI HGLOBALStreamImpl_AddRef(
81 IStream* iface);
83 ULONG WINAPI HGLOBALStreamImpl_Release(
84 IStream* iface);
86 HRESULT WINAPI HGLOBALStreamImpl_Read(
87 IStream* iface,
88 void* pv, /* [length_is][size_is][out] */
89 ULONG cb, /* [in] */
90 ULONG* pcbRead); /* [out] */
92 HRESULT WINAPI HGLOBALStreamImpl_Write(
93 IStream* iface,
94 const void* pv, /* [size_is][in] */
95 ULONG cb, /* [in] */
96 ULONG* pcbWritten); /* [out] */
98 HRESULT WINAPI HGLOBALStreamImpl_Seek(
99 IStream* iface,
100 LARGE_INTEGER dlibMove, /* [in] */
101 DWORD dwOrigin, /* [in] */
102 ULARGE_INTEGER* plibNewPosition); /* [out] */
104 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
105 IStream* iface,
106 ULARGE_INTEGER libNewSize); /* [in] */
108 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
109 IStream* iface,
110 IStream* pstm, /* [unique][in] */
111 ULARGE_INTEGER cb, /* [in] */
112 ULARGE_INTEGER* pcbRead, /* [out] */
113 ULARGE_INTEGER* pcbWritten); /* [out] */
115 HRESULT WINAPI HGLOBALStreamImpl_Commit(
116 IStream* iface,
117 DWORD grfCommitFlags); /* [in] */
119 HRESULT WINAPI HGLOBALStreamImpl_Revert(
120 IStream* iface);
122 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
123 IStream* iface,
124 ULARGE_INTEGER libOffset, /* [in] */
125 ULARGE_INTEGER cb, /* [in] */
126 DWORD dwLockType); /* [in] */
128 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
129 IStream* iface,
130 ULARGE_INTEGER libOffset, /* [in] */
131 ULARGE_INTEGER cb, /* [in] */
132 DWORD dwLockType); /* [in] */
134 HRESULT WINAPI HGLOBALStreamImpl_Stat(
135 IStream* iface,
136 STATSTG* pstatstg, /* [out] */
137 DWORD grfStatFlag); /* [in] */
139 HRESULT WINAPI HGLOBALStreamImpl_Clone(
140 IStream* iface,
141 IStream** ppstm); /* [out] */
145 * Virtual function table for the HGLOBALStreamImpl class.
147 static ICOM_VTABLE(IStream) HGLOBALStreamImpl_Vtbl =
149 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
150 HGLOBALStreamImpl_QueryInterface,
151 HGLOBALStreamImpl_AddRef,
152 HGLOBALStreamImpl_Release,
153 HGLOBALStreamImpl_Read,
154 HGLOBALStreamImpl_Write,
155 HGLOBALStreamImpl_Seek,
156 HGLOBALStreamImpl_SetSize,
157 HGLOBALStreamImpl_CopyTo,
158 HGLOBALStreamImpl_Commit,
159 HGLOBALStreamImpl_Revert,
160 HGLOBALStreamImpl_LockRegion,
161 HGLOBALStreamImpl_UnlockRegion,
162 HGLOBALStreamImpl_Stat,
163 HGLOBALStreamImpl_Clone
166 /***********************************************************************
167 * CreateStreamOnHGlobal [OLE32.61]
169 HRESULT WINAPI CreateStreamOnHGlobal(
170 HGLOBAL hGlobal,
171 BOOL fDeleteOnRelease,
172 LPSTREAM* ppstm)
174 HGLOBALStreamImpl* newStream;
176 newStream = HGLOBALStreamImpl_Construct(hGlobal,
177 fDeleteOnRelease);
179 if (newStream!=NULL)
181 return IUnknown_QueryInterface((IUnknown*)newStream,
182 &IID_IStream,
183 (void**)ppstm);
186 return E_OUTOFMEMORY;
189 /***********************************************************************
190 * GetHGlobalFromStream [OLE32.71]
192 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
194 HGLOBALStreamImpl* pStream;
196 if (pstm == NULL)
197 return E_INVALIDARG;
199 pStream = (HGLOBALStreamImpl*) pstm;
202 * Verify that the stream object was created with CreateStreamOnHGlobal.
204 if (ICOM_VTBL(pStream) == &HGLOBALStreamImpl_Vtbl)
205 *phglobal = pStream->supportHandle;
206 else
208 *phglobal = 0;
209 return E_INVALIDARG;
212 return S_OK;
215 /******************************************************************************
216 ** HGLOBALStreamImpl implementation
219 /***
220 * This is the constructor for the HGLOBALStreamImpl class.
222 * Params:
223 * hGlobal - Handle that will support the stream. can be NULL.
224 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
225 * when the IStream object is destroyed.
227 HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
228 HGLOBAL hGlobal,
229 BOOL fDeleteOnRelease)
231 HGLOBALStreamImpl* newStream;
233 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
235 if (newStream!=0)
238 * Set-up the virtual function table and reference count.
240 ICOM_VTBL(newStream) = &HGLOBALStreamImpl_Vtbl;
241 newStream->ref = 0;
244 * Initialize the support.
246 newStream->supportHandle = hGlobal;
247 newStream->deleteOnRelease = fDeleteOnRelease;
250 * This method will allocate a handle if one is not supplied.
252 if (!newStream->supportHandle)
254 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
255 GMEM_SHARE, 0);
259 * Start the stream at the begining.
261 newStream->currentPosition.s.HighPart = 0;
262 newStream->currentPosition.s.LowPart = 0;
265 * Initialize the size of the stream to the size of the handle.
267 newStream->streamSize.s.HighPart = 0;
268 newStream->streamSize.s.LowPart = GlobalSize(newStream->supportHandle);
271 return newStream;
274 /***
275 * This is the destructor of the HGLOBALStreamImpl class.
277 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
278 * class. The pointer passed-in to this function will be freed and will not
279 * be valid anymore.
281 void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
283 TRACE("(%p)\n", This);
286 * Release the HGlobal if the constructor asked for that.
288 if (This->deleteOnRelease)
290 GlobalFree(This->supportHandle);
291 This->supportHandle=0;
295 * Finally, free the memory used-up by the class.
297 HeapFree(GetProcessHeap(), 0, This);
300 /***
301 * This implements the IUnknown method QueryInterface for this
302 * class
304 HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
305 IStream* iface,
306 REFIID riid, /* [in] */
307 void** ppvObject) /* [iid_is][out] */
309 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
312 * Perform a sanity check on the parameters.
314 if (ppvObject==0)
315 return E_INVALIDARG;
318 * Initialize the return parameter.
320 *ppvObject = 0;
323 * Compare the riid with the interface IDs implemented by this object.
325 if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0)
327 *ppvObject = (IStream*)This;
329 else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0)
331 *ppvObject = (IStream*)This;
335 * Check that we obtained an interface.
337 if ((*ppvObject)==0)
338 return E_NOINTERFACE;
341 * Query Interface always increases the reference count by one when it is
342 * successful
344 HGLOBALStreamImpl_AddRef(iface);
346 return S_OK;;
349 /***
350 * This implements the IUnknown method AddRef for this
351 * class
353 ULONG WINAPI HGLOBALStreamImpl_AddRef(
354 IStream* iface)
356 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
358 This->ref++;
360 return This->ref;
363 /***
364 * This implements the IUnknown method Release for this
365 * class
367 ULONG WINAPI HGLOBALStreamImpl_Release(
368 IStream* iface)
370 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
372 ULONG newRef;
374 This->ref--;
376 newRef = This->ref;
379 * If the reference count goes down to 0, perform suicide.
381 if (newRef==0)
383 HGLOBALStreamImpl_Destroy(This);
386 return newRef;
389 /***
390 * This method is part of the ISequentialStream interface.
392 * If reads a block of information from the stream at the current
393 * position. It then moves the current position at the end of the
394 * read block
396 * See the documentation of ISequentialStream for more info.
398 HRESULT WINAPI HGLOBALStreamImpl_Read(
399 IStream* iface,
400 void* pv, /* [length_is][size_is][out] */
401 ULONG cb, /* [in] */
402 ULONG* pcbRead) /* [out] */
404 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
406 void* supportBuffer;
407 ULONG bytesReadBuffer;
408 ULONG bytesToReadFromBuffer;
410 TRACE("(%p, %p, %ld, %p)\n", iface,
411 pv, cb, pcbRead);
414 * If the caller is not interested in the nubmer of bytes read,
415 * we use another buffer to avoid "if" statements in the code.
417 if (pcbRead==0)
418 pcbRead = &bytesReadBuffer;
421 * Using the known size of the stream, calculate the number of bytes
422 * to read from the block chain
424 bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb);
427 * Lock the buffer in position and copy the data.
429 supportBuffer = GlobalLock(This->supportHandle);
431 memcpy(pv, (char *) supportBuffer+This->currentPosition.s.LowPart, bytesToReadFromBuffer);
434 * Move the current position to the new position
436 This->currentPosition.s.LowPart+=bytesToReadFromBuffer;
439 * Return the number of bytes read.
441 *pcbRead = bytesToReadFromBuffer;
444 * Cleanup
446 GlobalUnlock(This->supportHandle);
449 * The function returns S_OK if the buffer was filled completely
450 * it returns S_FALSE if the end of the stream is reached before the
451 * buffer is filled
453 if(*pcbRead == cb)
454 return S_OK;
456 return S_FALSE;
459 /***
460 * This method is part of the ISequentialStream interface.
462 * It writes a block of information to the stream at the current
463 * position. It then moves the current position at the end of the
464 * written block. If the stream is too small to fit the block,
465 * the stream is grown to fit.
467 * See the documentation of ISequentialStream for more info.
469 HRESULT WINAPI HGLOBALStreamImpl_Write(
470 IStream* iface,
471 const void* pv, /* [size_is][in] */
472 ULONG cb, /* [in] */
473 ULONG* pcbWritten) /* [out] */
475 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
477 void* supportBuffer;
478 ULARGE_INTEGER newSize;
479 ULONG bytesWritten = 0;
481 TRACE("(%p, %p, %ld, %p)\n", iface,
482 pv, cb, pcbWritten);
485 * If the caller is not interested in the number of bytes written,
486 * we use another buffer to avoid "if" statements in the code.
488 if (pcbWritten == 0)
489 pcbWritten = &bytesWritten;
491 if (cb == 0)
493 return S_OK;
495 else
497 newSize.s.HighPart = 0;
498 newSize.s.LowPart = This->currentPosition.s.LowPart + cb;
502 * Verify if we need to grow the stream
504 if (newSize.s.LowPart > This->streamSize.s.LowPart)
506 /* grow stream */
507 IStream_SetSize(iface, newSize);
511 * Lock the buffer in position and copy the data.
513 supportBuffer = GlobalLock(This->supportHandle);
515 memcpy((char *) supportBuffer+This->currentPosition.s.LowPart, pv, cb);
518 * Move the current position to the new position
520 This->currentPosition.s.LowPart+=cb;
523 * Return the number of bytes read.
525 *pcbWritten = cb;
528 * Cleanup
530 GlobalUnlock(This->supportHandle);
532 return S_OK;
535 /***
536 * This method is part of the IStream interface.
538 * It will move the current stream pointer according to the parameters
539 * given.
541 * See the documentation of IStream for more info.
543 HRESULT WINAPI HGLOBALStreamImpl_Seek(
544 IStream* iface,
545 LARGE_INTEGER dlibMove, /* [in] */
546 DWORD dwOrigin, /* [in] */
547 ULARGE_INTEGER* plibNewPosition) /* [out] */
549 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
551 ULARGE_INTEGER newPosition;
553 TRACE("(%p, %ld, %ld, %p)\n", iface,
554 dlibMove.s.LowPart, dwOrigin, plibNewPosition);
557 * The caller is allowed to pass in NULL as the new position return value.
558 * If it happens, we assign it to a dynamic variable to avoid special cases
559 * in the code below.
561 if (plibNewPosition == 0)
563 plibNewPosition = &newPosition;
567 * The file pointer is moved depending on the given "function"
568 * parameter.
570 switch (dwOrigin)
572 case STREAM_SEEK_SET:
573 plibNewPosition->s.HighPart = 0;
574 plibNewPosition->s.LowPart = 0;
575 break;
576 case STREAM_SEEK_CUR:
577 *plibNewPosition = This->currentPosition;
578 break;
579 case STREAM_SEEK_END:
580 *plibNewPosition = This->streamSize;
581 break;
582 default:
583 return STG_E_INVALIDFUNCTION;
587 * We don't support files with offsets of 64 bits.
589 assert(dlibMove.s.HighPart == 0);
592 * Check if we end-up before the beginning of the file. That should trigger an
593 * error.
595 if ( (dlibMove.s.LowPart<0) && (plibNewPosition->s.LowPart < (ULONG)(-dlibMove.s.LowPart)) )
598 * I don't know what error to send there.
600 return E_FAIL;
604 * Move the actual file pointer
605 * If the file pointer ends-up after the end of the stream, the next Write operation will
606 * make the file larger. This is how it is documented.
608 plibNewPosition->s.LowPart += dlibMove.s.LowPart;
609 This->currentPosition = *plibNewPosition;
611 return S_OK;
614 /***
615 * This method is part of the IStream interface.
617 * It will change the size of a stream.
619 * TODO: Switch from small blocks to big blocks and vice versa.
621 * See the documentation of IStream for more info.
623 HRESULT WINAPI HGLOBALStreamImpl_SetSize(
624 IStream* iface,
625 ULARGE_INTEGER libNewSize) /* [in] */
627 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
629 TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart);
632 * As documented.
634 if (libNewSize.s.HighPart != 0)
635 return STG_E_INVALIDFUNCTION;
637 if (This->streamSize.s.LowPart == libNewSize.s.LowPart)
638 return S_OK;
641 * Re allocate the HGlobal to fit the new size of the stream.
643 This->supportHandle = GlobalReAlloc(This->supportHandle,
644 libNewSize.s.LowPart,
647 This->streamSize.s.LowPart = libNewSize.s.LowPart;
649 return S_OK;
652 /***
653 * This method is part of the IStream interface.
655 * It will copy the 'cb' Bytes to 'pstm' IStream.
657 * See the documentation of IStream for more info.
659 HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
660 IStream* iface,
661 IStream* pstm, /* [unique][in] */
662 ULARGE_INTEGER cb, /* [in] */
663 ULARGE_INTEGER* pcbRead, /* [out] */
664 ULARGE_INTEGER* pcbWritten) /* [out] */
666 HRESULT hr = S_OK;
667 BYTE tmpBuffer[128];
668 ULONG bytesRead, bytesWritten, copySize;
669 ULARGE_INTEGER totalBytesRead;
670 ULARGE_INTEGER totalBytesWritten;
672 TRACE("(%p, %p, %ld, %p, %p)\n", iface, pstm,
673 cb.s.LowPart, pcbRead, pcbWritten);
676 * Sanity check
678 if ( pstm == 0 )
679 return STG_E_INVALIDPOINTER;
681 totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0;
682 totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0;
685 * use stack to store data temporarly
686 * there is surely more performant way of doing it, for now this basic
687 * implementation will do the job
689 while ( cb.s.LowPart > 0 )
691 if ( cb.s.LowPart >= 128 )
692 copySize = 128;
693 else
694 copySize = cb.s.LowPart;
696 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
698 totalBytesRead.s.LowPart += bytesRead;
700 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
702 totalBytesWritten.s.LowPart += bytesWritten;
705 * Check that read & write operations were succesfull
707 if (bytesRead != bytesWritten)
709 hr = STG_E_MEDIUMFULL;
710 break;
713 if (bytesRead!=copySize)
714 cb.s.LowPart = 0;
715 else
716 cb.s.LowPart -= bytesRead;
720 * Update number of bytes read and written
722 if (pcbRead)
724 pcbRead->s.LowPart = totalBytesRead.s.LowPart;
725 pcbRead->s.HighPart = totalBytesRead.s.HighPart;
728 if (pcbWritten)
730 pcbWritten->s.LowPart = totalBytesWritten.s.LowPart;
731 pcbWritten->s.HighPart = totalBytesWritten.s.HighPart;
733 return hr;
736 /***
737 * This method is part of the IStream interface.
739 * For streams supported by HGLOBALS, this function does nothing.
740 * This is what the documentation tells us.
742 * See the documentation of IStream for more info.
744 HRESULT WINAPI HGLOBALStreamImpl_Commit(
745 IStream* iface,
746 DWORD grfCommitFlags) /* [in] */
748 return S_OK;
751 /***
752 * This method is part of the IStream interface.
754 * For streams supported by HGLOBALS, this function does nothing.
755 * This is what the documentation tells us.
757 * See the documentation of IStream for more info.
759 HRESULT WINAPI HGLOBALStreamImpl_Revert(
760 IStream* iface)
762 return S_OK;
765 /***
766 * This method is part of the IStream interface.
768 * For streams supported by HGLOBALS, this function does nothing.
769 * This is what the documentation tells us.
771 * See the documentation of IStream for more info.
773 HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
774 IStream* iface,
775 ULARGE_INTEGER libOffset, /* [in] */
776 ULARGE_INTEGER cb, /* [in] */
777 DWORD dwLockType) /* [in] */
779 return S_OK;
783 * This method is part of the IStream interface.
785 * For streams supported by HGLOBALS, this function does nothing.
786 * This is what the documentation tells us.
788 * See the documentation of IStream for more info.
790 HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
791 IStream* iface,
792 ULARGE_INTEGER libOffset, /* [in] */
793 ULARGE_INTEGER cb, /* [in] */
794 DWORD dwLockType) /* [in] */
796 return S_OK;
799 /***
800 * This method is part of the IStream interface.
802 * This method returns information about the current
803 * stream.
805 * See the documentation of IStream for more info.
807 HRESULT WINAPI HGLOBALStreamImpl_Stat(
808 IStream* iface,
809 STATSTG* pstatstg, /* [out] */
810 DWORD grfStatFlag) /* [in] */
812 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
814 memset(pstatstg, 0, sizeof(STATSTG));
816 pstatstg->pwcsName = NULL;
817 pstatstg->type = STGTY_STREAM;
818 pstatstg->cbSize = This->streamSize;
820 return S_OK;
823 HRESULT WINAPI HGLOBALStreamImpl_Clone(
824 IStream* iface,
825 IStream** ppstm) /* [out] */
827 FIXME("not implemented!\n");
828 return E_NOTIMPL;