Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / ole32 / hglobalstream.c
blobef616defec50d34ad5b1c6f5ab93c90c3e1f19d4
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
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.
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.
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
24 #include "config.h"
26 #include <assert.h>
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
32 #define COBJMACROS
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winuser.h"
39 #include "objbase.h"
40 #include "ole2.h"
41 #include "winerror.h"
42 #include "winternl.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(storage);
48 /****************************************************************************
49 * HGLOBALStreamImpl definition.
51 * This class implements the IStream interface and represents a stream
52 * supported by an HGLOBAL pointer.
54 struct HGLOBALStreamImpl
56 const IStreamVtbl *lpVtbl; /* Needs to be the first item in the struct
57 * since we want to cast this in an IStream pointer */
60 * Reference count
62 LONG ref;
65 * Support for the stream
67 HGLOBAL supportHandle;
70 * This flag is TRUE if the HGLOBAL is destroyed when the stream
71 * is finally released.
73 BOOL deleteOnRelease;
76 * Helper variable that contains the size of the stream
78 ULARGE_INTEGER streamSize;
81 * This is the current position of the cursor in the stream
83 ULARGE_INTEGER currentPosition;
86 typedef struct HGLOBALStreamImpl HGLOBALStreamImpl;
88 /***
89 * This is the destructor of the HGLOBALStreamImpl class.
91 * This method will clean-up all the resources used-up by the given HGLOBALStreamImpl
92 * class. The pointer passed-in to this function will be freed and will not
93 * be valid anymore.
95 static void HGLOBALStreamImpl_Destroy(HGLOBALStreamImpl* This)
97 TRACE("(%p)\n", This);
100 * Release the HGlobal if the constructor asked for that.
102 if (This->deleteOnRelease)
104 GlobalFree(This->supportHandle);
105 This->supportHandle=0;
109 * Finally, free the memory used-up by the class.
111 HeapFree(GetProcessHeap(), 0, This);
114 /***
115 * This implements the IUnknown method AddRef for this
116 * class
118 static ULONG WINAPI HGLOBALStreamImpl_AddRef(
119 IStream* iface)
121 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
122 return InterlockedIncrement(&This->ref);
125 /***
126 * This implements the IUnknown method QueryInterface for this
127 * class
129 static HRESULT WINAPI HGLOBALStreamImpl_QueryInterface(
130 IStream* iface,
131 REFIID riid, /* [in] */
132 void** ppvObject) /* [iid_is][out] */
134 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
137 * Perform a sanity check on the parameters.
139 if (ppvObject==0)
140 return E_INVALIDARG;
143 * Initialize the return parameter.
145 *ppvObject = 0;
148 * Compare the riid with the interface IDs implemented by this object.
150 if (IsEqualIID(&IID_IUnknown, riid) ||
151 IsEqualIID(&IID_ISequentialStream, riid) ||
152 IsEqualIID(&IID_IStream, riid))
154 *ppvObject = This;
158 * Check that we obtained an interface.
160 if ((*ppvObject)==0)
161 return E_NOINTERFACE;
164 * Query Interface always increases the reference count by one when it is
165 * successful
167 HGLOBALStreamImpl_AddRef(iface);
169 return S_OK;
172 /***
173 * This implements the IUnknown method Release for this
174 * class
176 static ULONG WINAPI HGLOBALStreamImpl_Release(
177 IStream* iface)
179 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
180 ULONG newRef;
182 newRef = InterlockedDecrement(&This->ref);
185 * If the reference count goes down to 0, perform suicide.
187 if (newRef==0)
189 HGLOBALStreamImpl_Destroy(This);
192 return newRef;
195 /***
196 * This method is part of the ISequentialStream interface.
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
202 * See the documentation of ISequentialStream for more info.
204 static HRESULT WINAPI HGLOBALStreamImpl_Read(
205 IStream* iface,
206 void* pv, /* [length_is][size_is][out] */
207 ULONG cb, /* [in] */
208 ULONG* pcbRead) /* [out] */
210 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
212 void* supportBuffer;
213 ULONG bytesReadBuffer;
214 ULONG bytesToReadFromBuffer;
216 TRACE("(%p, %p, %d, %p)\n", iface,
217 pv, cb, pcbRead);
220 * If the caller is not interested in the nubmer of bytes read,
221 * we use another buffer to avoid "if" statements in the code.
223 if (pcbRead==0)
224 pcbRead = &bytesReadBuffer;
227 * Using the known size of the stream, calculate the number of bytes
228 * to read from the block chain
230 bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
233 * Lock the buffer in position and copy the data.
235 supportBuffer = GlobalLock(This->supportHandle);
236 if (!supportBuffer)
238 WARN("read from invalid hglobal %p\n", This->supportHandle);
239 *pcbRead = 0;
240 return S_OK;
243 memcpy(pv, (char *) supportBuffer+This->currentPosition.u.LowPart, bytesToReadFromBuffer);
246 * Move the current position to the new position
248 This->currentPosition.u.LowPart+=bytesToReadFromBuffer;
251 * Return the number of bytes read.
253 *pcbRead = bytesToReadFromBuffer;
256 * Cleanup
258 GlobalUnlock(This->supportHandle);
261 * Always returns S_OK even if the end of the stream is reached before the
262 * buffer is filled
265 return S_OK;
268 /***
269 * This method is part of the ISequentialStream interface.
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.
276 * See the documentation of ISequentialStream for more info.
278 static HRESULT WINAPI HGLOBALStreamImpl_Write(
279 IStream* iface,
280 const void* pv, /* [size_is][in] */
281 ULONG cb, /* [in] */
282 ULONG* pcbWritten) /* [out] */
284 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
286 void* supportBuffer;
287 ULARGE_INTEGER newSize;
288 ULONG bytesWritten = 0;
290 TRACE("(%p, %p, %d, %p)\n", iface, pv, cb, pcbWritten);
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.
296 if (pcbWritten == 0)
297 pcbWritten = &bytesWritten;
299 if (cb == 0)
300 goto out;
302 *pcbWritten = 0;
304 newSize.u.HighPart = 0;
305 newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
308 * Verify if we need to grow the stream
310 if (newSize.u.LowPart > This->streamSize.u.LowPart)
312 /* grow stream */
313 HRESULT hr = IStream_SetSize(iface, newSize);
314 if (FAILED(hr))
316 ERR("IStream_SetSize failed with error 0x%08x\n", hr);
317 return hr;
322 * Lock the buffer in position and copy the data.
324 supportBuffer = GlobalLock(This->supportHandle);
325 if (!supportBuffer)
327 WARN("write to invalid hglobal %p\n", This->supportHandle);
328 return S_OK;
331 memcpy((char *) supportBuffer+This->currentPosition.u.LowPart, pv, cb);
334 * Move the current position to the new position
336 This->currentPosition.u.LowPart+=cb;
339 * Cleanup
341 GlobalUnlock(This->supportHandle);
343 out:
345 * Return the number of bytes read.
347 *pcbWritten = cb;
349 return S_OK;
352 /***
353 * This method is part of the IStream interface.
355 * It will move the current stream pointer according to the parameters
356 * given.
358 * See the documentation of IStream for more info.
360 static HRESULT WINAPI HGLOBALStreamImpl_Seek(
361 IStream* iface,
362 LARGE_INTEGER dlibMove, /* [in] */
363 DWORD dwOrigin, /* [in] */
364 ULARGE_INTEGER* plibNewPosition) /* [out] */
366 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
368 ULARGE_INTEGER newPosition = This->currentPosition;
369 HRESULT hr = S_OK;
371 TRACE("(%p, %x%08x, %d, %p)\n", iface, dlibMove.u.HighPart,
372 dlibMove.u.LowPart, dwOrigin, plibNewPosition);
375 * The file pointer is moved depending on the given "function"
376 * parameter.
378 switch (dwOrigin)
380 case STREAM_SEEK_SET:
381 newPosition.u.HighPart = 0;
382 newPosition.u.LowPart = 0;
383 break;
384 case STREAM_SEEK_CUR:
385 break;
386 case STREAM_SEEK_END:
387 newPosition = This->streamSize;
388 break;
389 default:
390 hr = STG_E_SEEKERROR;
391 goto end;
395 * Move the actual file pointer
396 * If the file pointer ends-up after the end of the stream, the next Write operation will
397 * make the file larger. This is how it is documented.
399 newPosition.u.HighPart = 0;
400 newPosition.u.LowPart += dlibMove.QuadPart;
402 if (dlibMove.u.LowPart >= 0x80000000 &&
403 newPosition.u.LowPart >= dlibMove.u.LowPart)
405 /* We tried to seek backwards and went past the start. */
406 hr = STG_E_SEEKERROR;
407 goto end;
410 This->currentPosition = newPosition;
412 end:
413 if (plibNewPosition) *plibNewPosition = This->currentPosition;
415 return hr;
418 /***
419 * This method is part of the IStream interface.
421 * It will change the size of a stream.
423 * TODO: Switch from small blocks to big blocks and vice versa.
425 * See the documentation of IStream for more info.
427 static HRESULT WINAPI HGLOBALStreamImpl_SetSize(
428 IStream* iface,
429 ULARGE_INTEGER libNewSize) /* [in] */
431 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
432 HGLOBAL supportHandle;
434 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
437 * HighPart is ignored as shown in tests
440 if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
441 return S_OK;
444 * Re allocate the HGlobal to fit the new size of the stream.
446 supportHandle = GlobalReAlloc(This->supportHandle, libNewSize.u.LowPart, 0);
448 if (supportHandle == 0)
449 return E_OUTOFMEMORY;
451 This->supportHandle = supportHandle;
452 This->streamSize.u.LowPart = libNewSize.u.LowPart;
454 return S_OK;
457 /***
458 * This method is part of the IStream interface.
460 * It will copy the 'cb' Bytes to 'pstm' IStream.
462 * See the documentation of IStream for more info.
464 static HRESULT WINAPI HGLOBALStreamImpl_CopyTo(
465 IStream* iface,
466 IStream* pstm, /* [unique][in] */
467 ULARGE_INTEGER cb, /* [in] */
468 ULARGE_INTEGER* pcbRead, /* [out] */
469 ULARGE_INTEGER* pcbWritten) /* [out] */
471 HRESULT hr = S_OK;
472 BYTE tmpBuffer[128];
473 ULONG bytesRead, bytesWritten, copySize;
474 ULARGE_INTEGER totalBytesRead;
475 ULARGE_INTEGER totalBytesWritten;
477 TRACE("(%p, %p, %d, %p, %p)\n", iface, pstm,
478 cb.u.LowPart, pcbRead, pcbWritten);
480 if ( pstm == 0 )
481 return STG_E_INVALIDPOINTER;
483 totalBytesRead.QuadPart = 0;
484 totalBytesWritten.QuadPart = 0;
486 while ( cb.QuadPart > 0 )
488 if ( cb.QuadPart >= sizeof(tmpBuffer) )
489 copySize = sizeof(tmpBuffer);
490 else
491 copySize = cb.u.LowPart;
493 hr = IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
494 if (FAILED(hr))
495 break;
497 totalBytesRead.QuadPart += bytesRead;
499 if (bytesRead)
501 hr = IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
502 if (FAILED(hr))
503 break;
505 totalBytesWritten.QuadPart += bytesWritten;
508 if (bytesRead!=copySize)
509 cb.QuadPart = 0;
510 else
511 cb.QuadPart -= bytesRead;
514 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
515 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
517 return hr;
520 /***
521 * This method is part of the IStream interface.
523 * For streams supported by HGLOBALS, this function does nothing.
524 * This is what the documentation tells us.
526 * See the documentation of IStream for more info.
528 static HRESULT WINAPI HGLOBALStreamImpl_Commit(
529 IStream* iface,
530 DWORD grfCommitFlags) /* [in] */
532 return S_OK;
535 /***
536 * This method is part of the IStream interface.
538 * For streams supported by HGLOBALS, this function does nothing.
539 * This is what the documentation tells us.
541 * See the documentation of IStream for more info.
543 static HRESULT WINAPI HGLOBALStreamImpl_Revert(
544 IStream* iface)
546 return S_OK;
549 /***
550 * This method is part of the IStream interface.
552 * For streams supported by HGLOBALS, this function does nothing.
553 * This is what the documentation tells us.
555 * See the documentation of IStream for more info.
557 static HRESULT WINAPI HGLOBALStreamImpl_LockRegion(
558 IStream* iface,
559 ULARGE_INTEGER libOffset, /* [in] */
560 ULARGE_INTEGER cb, /* [in] */
561 DWORD dwLockType) /* [in] */
563 return STG_E_INVALIDFUNCTION;
567 * This method is part of the IStream interface.
569 * For streams supported by HGLOBALS, this function does nothing.
570 * This is what the documentation tells us.
572 * See the documentation of IStream for more info.
574 static HRESULT WINAPI HGLOBALStreamImpl_UnlockRegion(
575 IStream* iface,
576 ULARGE_INTEGER libOffset, /* [in] */
577 ULARGE_INTEGER cb, /* [in] */
578 DWORD dwLockType) /* [in] */
580 return S_OK;
583 /***
584 * This method is part of the IStream interface.
586 * This method returns information about the current
587 * stream.
589 * See the documentation of IStream for more info.
591 static HRESULT WINAPI HGLOBALStreamImpl_Stat(
592 IStream* iface,
593 STATSTG* pstatstg, /* [out] */
594 DWORD grfStatFlag) /* [in] */
596 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
598 memset(pstatstg, 0, sizeof(STATSTG));
600 pstatstg->pwcsName = NULL;
601 pstatstg->type = STGTY_STREAM;
602 pstatstg->cbSize = This->streamSize;
604 return S_OK;
607 static HRESULT WINAPI HGLOBALStreamImpl_Clone(
608 IStream* iface,
609 IStream** ppstm) /* [out] */
611 ULARGE_INTEGER dummy;
612 LARGE_INTEGER offset;
613 HRESULT hr;
614 HGLOBALStreamImpl* const This=(HGLOBALStreamImpl*)iface;
615 TRACE(" Cloning %p (deleteOnRelease=%d seek position=%ld)\n",iface,This->deleteOnRelease,(long)This->currentPosition.QuadPart);
616 hr=CreateStreamOnHGlobal(This->supportHandle, FALSE, ppstm);
617 if(FAILED(hr))
618 return hr;
619 offset.QuadPart=(LONGLONG)This->currentPosition.QuadPart;
620 HGLOBALStreamImpl_Seek(*ppstm,offset,STREAM_SEEK_SET,&dummy);
621 return S_OK;
625 * Virtual function table for the HGLOBALStreamImpl class.
627 static const IStreamVtbl HGLOBALStreamImpl_Vtbl =
629 HGLOBALStreamImpl_QueryInterface,
630 HGLOBALStreamImpl_AddRef,
631 HGLOBALStreamImpl_Release,
632 HGLOBALStreamImpl_Read,
633 HGLOBALStreamImpl_Write,
634 HGLOBALStreamImpl_Seek,
635 HGLOBALStreamImpl_SetSize,
636 HGLOBALStreamImpl_CopyTo,
637 HGLOBALStreamImpl_Commit,
638 HGLOBALStreamImpl_Revert,
639 HGLOBALStreamImpl_LockRegion,
640 HGLOBALStreamImpl_UnlockRegion,
641 HGLOBALStreamImpl_Stat,
642 HGLOBALStreamImpl_Clone
645 /******************************************************************************
646 ** HGLOBALStreamImpl implementation
649 /***
650 * This is the constructor for the HGLOBALStreamImpl class.
652 * Params:
653 * hGlobal - Handle that will support the stream. can be NULL.
654 * fDeleteOnRelease - Flag set to TRUE if the HGLOBAL will be released
655 * when the IStream object is destroyed.
657 static HGLOBALStreamImpl* HGLOBALStreamImpl_Construct(
658 HGLOBAL hGlobal,
659 BOOL fDeleteOnRelease)
661 HGLOBALStreamImpl* newStream;
663 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(HGLOBALStreamImpl));
665 if (newStream!=0)
668 * Set-up the virtual function table and reference count.
670 newStream->lpVtbl = &HGLOBALStreamImpl_Vtbl;
671 newStream->ref = 0;
674 * Initialize the support.
676 newStream->supportHandle = hGlobal;
677 newStream->deleteOnRelease = fDeleteOnRelease;
680 * This method will allocate a handle if one is not supplied.
682 if (!newStream->supportHandle)
684 newStream->supportHandle = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD |
685 GMEM_SHARE, 0);
689 * Start the stream at the beginning.
691 newStream->currentPosition.u.HighPart = 0;
692 newStream->currentPosition.u.LowPart = 0;
695 * Initialize the size of the stream to the size of the handle.
697 newStream->streamSize.u.HighPart = 0;
698 newStream->streamSize.u.LowPart = GlobalSize(newStream->supportHandle);
701 return newStream;
705 /***********************************************************************
706 * CreateStreamOnHGlobal [OLE32.@]
708 HRESULT WINAPI CreateStreamOnHGlobal(
709 HGLOBAL hGlobal,
710 BOOL fDeleteOnRelease,
711 LPSTREAM* ppstm)
713 HGLOBALStreamImpl* newStream;
715 if (!ppstm)
716 return E_INVALIDARG;
718 newStream = HGLOBALStreamImpl_Construct(hGlobal,
719 fDeleteOnRelease);
721 if (newStream!=NULL)
723 return IUnknown_QueryInterface((IUnknown*)newStream,
724 &IID_IStream,
725 (void**)ppstm);
728 return E_OUTOFMEMORY;
731 /***********************************************************************
732 * GetHGlobalFromStream [OLE32.@]
734 HRESULT WINAPI GetHGlobalFromStream(IStream* pstm, HGLOBAL* phglobal)
736 HGLOBALStreamImpl* pStream;
738 if (pstm == NULL)
739 return E_INVALIDARG;
741 pStream = (HGLOBALStreamImpl*) pstm;
744 * Verify that the stream object was created with CreateStreamOnHGlobal.
746 if (pStream->lpVtbl == &HGLOBALStreamImpl_Vtbl)
747 *phglobal = pStream->supportHandle;
748 else
750 *phglobal = 0;
751 return E_INVALIDARG;
754 return S_OK;