2 * Compound Storage (32 bit version)
3 * Stream implementation
5 * This file contains the implementation of the stream interface
6 * for streams contained in a compound storage.
8 * Copyright 1999 Francis Beaudet
9 * Copyright 1999 Thuy Nguyen
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
33 #define NONAMELESSUNION
34 #define NONAMELESSSTRUCT
40 #include "wine/debug.h"
42 #include "storage32.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(storage);
48 * This is the destructor of the StgStreamImpl class.
50 * This method will clean-up all the resources used-up by the given StgStreamImpl
51 * class. The pointer passed-in to this function will be freed and will not
54 static void StgStreamImpl_Destroy(StgStreamImpl* This)
56 TRACE("(%p)\n", This);
59 * Release the reference we are holding on the parent storage.
60 * IStorage_Release(&This->parentStorage->IStorage_iface);
62 * No, don't do this. Some apps call IStorage_Release without
63 * calling IStream_Release first. If we grab a reference the
64 * file is not closed, and the app fails when it tries to
65 * reopen the file (Easy-PC, for example). Just inform the
66 * storage that we have closed the stream
69 if(This->parentStorage) {
71 StorageBaseImpl_RemoveStream(This->parentStorage, This);
75 This->parentStorage = 0;
77 HeapFree(GetProcessHeap(), 0, This);
81 * This implements the IUnknown method QueryInterface for this
84 static HRESULT WINAPI StgStreamImpl_QueryInterface(
86 REFIID riid, /* [in] */
87 void** ppvObject) /* [iid_is][out] */
89 StgStreamImpl* This = impl_from_IStream(iface);
96 if (IsEqualIID(&IID_IUnknown, riid) ||
97 IsEqualIID(&IID_ISequentialStream, riid) ||
98 IsEqualIID(&IID_IStream, riid))
100 *ppvObject = &This->IStream_iface;
103 return E_NOINTERFACE;
105 IStream_AddRef(iface);
111 * This implements the IUnknown method AddRef for this
114 static ULONG WINAPI StgStreamImpl_AddRef(
117 StgStreamImpl* This = impl_from_IStream(iface);
118 return InterlockedIncrement(&This->ref);
122 * This implements the IUnknown method Release for this
125 static ULONG WINAPI StgStreamImpl_Release(
128 StgStreamImpl* This = impl_from_IStream(iface);
132 ref = InterlockedDecrement(&This->ref);
135 * If the reference count goes down to 0, perform suicide.
139 StgStreamImpl_Destroy(This);
146 * This method is part of the ISequentialStream interface.
148 * It reads a block of information from the stream at the current
149 * position. It then moves the current position at the end of the
152 * See the documentation of ISequentialStream for more info.
154 static HRESULT WINAPI StgStreamImpl_Read(
156 void* pv, /* [length_is][size_is][out] */
158 ULONG* pcbRead) /* [out] */
160 StgStreamImpl* This = impl_from_IStream(iface);
162 ULONG bytesReadBuffer;
165 TRACE("(%p, %p, %d, %p)\n",
166 iface, pv, cb, pcbRead);
168 if (!This->parentStorage)
170 WARN("storage reverted\n");
171 return STG_E_REVERTED;
175 * If the caller is not interested in the number of bytes read,
176 * we use another buffer to avoid "if" statements in the code.
179 pcbRead = &bytesReadBuffer;
181 res = StorageBaseImpl_StreamReadAt(This->parentStorage,
183 This->currentPosition,
191 * Advance the pointer for the number of positions read.
193 This->currentPosition.u.LowPart += *pcbRead;
196 TRACE("<-- %08x\n", res);
201 * This method is part of the ISequentialStream interface.
203 * It writes a block of information to the stream at the current
204 * position. It then moves the current position at the end of the
205 * written block. If the stream is too small to fit the block,
206 * the stream is grown to fit.
208 * See the documentation of ISequentialStream for more info.
210 static HRESULT WINAPI StgStreamImpl_Write(
212 const void* pv, /* [size_is][in] */
214 ULONG* pcbWritten) /* [out] */
216 StgStreamImpl* This = impl_from_IStream(iface);
218 ULONG bytesWritten = 0;
221 TRACE("(%p, %p, %d, %p)\n",
222 iface, pv, cb, pcbWritten);
225 * Do we have permission to write to this stream?
227 switch(STGM_ACCESS_MODE(This->grfMode))
233 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
234 return STG_E_ACCESSDENIED;
238 return STG_E_INVALIDPOINTER;
240 if (!This->parentStorage)
242 WARN("storage reverted\n");
243 return STG_E_REVERTED;
247 * If the caller is not interested in the number of bytes written,
248 * we use another buffer to avoid "if" statements in the code.
251 pcbWritten = &bytesWritten;
254 * Initialize the out parameter
260 TRACE("<-- S_OK, written 0\n");
264 res = StorageBaseImpl_StreamWriteAt(This->parentStorage,
266 This->currentPosition,
272 * Advance the position pointer for the number of positions written.
274 This->currentPosition.u.LowPart += *pcbWritten;
277 res = StorageBaseImpl_Flush(This->parentStorage);
279 TRACE("<-- S_OK, written %u\n", *pcbWritten);
284 * This method is part of the IStream interface.
286 * It will move the current stream pointer according to the parameters
289 * See the documentation of IStream for more info.
291 static HRESULT WINAPI StgStreamImpl_Seek(
293 LARGE_INTEGER dlibMove, /* [in] */
294 DWORD dwOrigin, /* [in] */
295 ULARGE_INTEGER* plibNewPosition) /* [out] */
297 StgStreamImpl* This = impl_from_IStream(iface);
299 ULARGE_INTEGER newPosition;
300 DirEntry currentEntry;
303 TRACE("(%p, %d, %d, %p)\n",
304 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
307 * fail if the stream has no parent (as does windows)
310 if (!This->parentStorage)
312 WARN("storage reverted\n");
313 return STG_E_REVERTED;
317 * The caller is allowed to pass in NULL as the new position return value.
318 * If it happens, we assign it to a dynamic variable to avoid special cases
321 if (plibNewPosition == 0)
323 plibNewPosition = &newPosition;
327 * The file pointer is moved depending on the given "function"
332 case STREAM_SEEK_SET:
333 plibNewPosition->u.HighPart = 0;
334 plibNewPosition->u.LowPart = 0;
336 case STREAM_SEEK_CUR:
337 *plibNewPosition = This->currentPosition;
339 case STREAM_SEEK_END:
340 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage, This->dirEntry, ¤tEntry);
341 if (FAILED(hr)) return hr;
342 *plibNewPosition = currentEntry.size;
345 WARN("invalid dwOrigin %d\n", dwOrigin);
346 return STG_E_INVALIDFUNCTION;
349 plibNewPosition->QuadPart += dlibMove.QuadPart;
352 * tell the caller what we calculated
354 This->currentPosition = *plibNewPosition;
360 * This method is part of the IStream interface.
362 * It will change the size of a stream.
364 * See the documentation of IStream for more info.
366 static HRESULT WINAPI StgStreamImpl_SetSize(
368 ULARGE_INTEGER libNewSize) /* [in] */
370 StgStreamImpl* This = impl_from_IStream(iface);
374 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
376 if(!This->parentStorage)
378 WARN("storage reverted\n");
379 return STG_E_REVERTED;
385 if (libNewSize.u.HighPart != 0)
387 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
388 return STG_E_INVALIDFUNCTION;
392 * Do we have permission?
394 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
396 WARN("access denied\n");
397 return STG_E_ACCESSDENIED;
400 hr = StorageBaseImpl_StreamSetSize(This->parentStorage, This->dirEntry, libNewSize);
403 hr = StorageBaseImpl_Flush(This->parentStorage);
409 * This method is part of the IStream interface.
411 * It will copy the 'cb' Bytes to 'pstm' IStream.
413 * See the documentation of IStream for more info.
415 static HRESULT WINAPI StgStreamImpl_CopyTo(
417 IStream* pstm, /* [unique][in] */
418 ULARGE_INTEGER cb, /* [in] */
419 ULARGE_INTEGER* pcbRead, /* [out] */
420 ULARGE_INTEGER* pcbWritten) /* [out] */
422 StgStreamImpl* This = impl_from_IStream(iface);
425 ULONG bytesRead, bytesWritten, copySize;
426 ULARGE_INTEGER totalBytesRead;
427 ULARGE_INTEGER totalBytesWritten;
429 TRACE("(%p, %p, %d, %p, %p)\n",
430 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
436 if (!This->parentStorage)
438 WARN("storage reverted\n");
439 return STG_E_REVERTED;
443 return STG_E_INVALIDPOINTER;
445 totalBytesRead.QuadPart = 0;
446 totalBytesWritten.QuadPart = 0;
448 while ( cb.QuadPart > 0 )
450 if ( cb.QuadPart >= sizeof(tmpBuffer) )
451 copySize = sizeof(tmpBuffer);
453 copySize = cb.u.LowPart;
455 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
457 totalBytesRead.QuadPart += bytesRead;
459 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
461 totalBytesWritten.QuadPart += bytesWritten;
464 * Check that read & write operations were successful
466 if (bytesRead != bytesWritten)
468 hr = STG_E_MEDIUMFULL;
469 WARN("medium full\n");
473 if (bytesRead!=copySize)
476 cb.QuadPart -= bytesRead;
479 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
480 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
486 * This method is part of the IStream interface.
488 * For streams contained in structured storages, this method
489 * does nothing. This is what the documentation tells us.
491 * See the documentation of IStream for more info.
493 static HRESULT WINAPI StgStreamImpl_Commit(
495 DWORD grfCommitFlags) /* [in] */
497 StgStreamImpl* This = impl_from_IStream(iface);
499 if (!This->parentStorage)
501 WARN("storage reverted\n");
502 return STG_E_REVERTED;
505 return StorageBaseImpl_Flush(This->parentStorage);
509 * This method is part of the IStream interface.
511 * For streams contained in structured storages, this method
512 * does nothing. This is what the documentation tells us.
514 * See the documentation of IStream for more info.
516 static HRESULT WINAPI StgStreamImpl_Revert(
522 static HRESULT WINAPI StgStreamImpl_LockRegion(
524 ULARGE_INTEGER libOffset, /* [in] */
525 ULARGE_INTEGER cb, /* [in] */
526 DWORD dwLockType) /* [in] */
528 StgStreamImpl* This = impl_from_IStream(iface);
530 if (!This->parentStorage)
532 WARN("storage reverted\n");
533 return STG_E_REVERTED;
536 FIXME("not implemented!\n");
540 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
542 ULARGE_INTEGER libOffset, /* [in] */
543 ULARGE_INTEGER cb, /* [in] */
544 DWORD dwLockType) /* [in] */
546 StgStreamImpl* This = impl_from_IStream(iface);
548 if (!This->parentStorage)
550 WARN("storage reverted\n");
551 return STG_E_REVERTED;
554 FIXME("not implemented!\n");
559 * This method is part of the IStream interface.
561 * This method returns information about the current
564 * See the documentation of IStream for more info.
566 static HRESULT WINAPI StgStreamImpl_Stat(
568 STATSTG* pstatstg, /* [out] */
569 DWORD grfStatFlag) /* [in] */
571 StgStreamImpl* This = impl_from_IStream(iface);
573 DirEntry currentEntry;
576 TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
579 * if stream has no parent, return STG_E_REVERTED
582 if (!This->parentStorage)
584 WARN("storage reverted\n");
585 return STG_E_REVERTED;
589 * Read the information from the directory entry.
591 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage,
597 StorageUtl_CopyDirEntryToSTATSTG(This->parentStorage,
602 pstatstg->grfMode = This->grfMode;
604 /* In simple create mode cbSize is the current pos */
605 if((This->parentStorage->openFlags & STGM_SIMPLE) && This->parentStorage->create)
606 pstatstg->cbSize = This->currentPosition;
611 WARN("failed to read entry\n");
616 * This method is part of the IStream interface.
618 * This method returns a clone of the interface that allows for
619 * another seek pointer
621 * See the documentation of IStream for more info.
623 * I am not totally sure what I am doing here but I presume that this
624 * should be basically as simple as creating a new stream with the same
625 * parent etc and positioning its seek cursor.
627 static HRESULT WINAPI StgStreamImpl_Clone(
629 IStream** ppstm) /* [out] */
631 StgStreamImpl* This = impl_from_IStream(iface);
633 StgStreamImpl* new_stream;
634 LARGE_INTEGER seek_pos;
636 TRACE("%p %p\n", This, ppstm);
642 if (!This->parentStorage)
643 return STG_E_REVERTED;
646 return STG_E_INVALIDPOINTER;
648 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->dirEntry);
651 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
653 *ppstm = &new_stream->IStream_iface;
654 IStream_AddRef(*ppstm);
656 seek_pos.QuadPart = This->currentPosition.QuadPart;
658 hres = IStream_Seek(*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
660 assert (SUCCEEDED(hres));
666 * Virtual function table for the StgStreamImpl class.
668 static const IStreamVtbl StgStreamVtbl =
670 StgStreamImpl_QueryInterface,
671 StgStreamImpl_AddRef,
672 StgStreamImpl_Release,
676 StgStreamImpl_SetSize,
677 StgStreamImpl_CopyTo,
678 StgStreamImpl_Commit,
679 StgStreamImpl_Revert,
680 StgStreamImpl_LockRegion,
681 StgStreamImpl_UnlockRegion,
686 /******************************************************************************
687 ** StgStreamImpl implementation
691 * This is the constructor for the StgStreamImpl class.
694 * parentStorage - Pointer to the storage that contains the stream to open
695 * dirEntry - Index of the directory entry that points to this stream.
697 StgStreamImpl* StgStreamImpl_Construct(
698 StorageBaseImpl* parentStorage,
702 StgStreamImpl* newStream;
704 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
709 * Set-up the virtual function table and reference count.
711 newStream->IStream_iface.lpVtbl = &StgStreamVtbl;
714 newStream->parentStorage = parentStorage;
717 * We want to nail-down the reference to the storage in case the
718 * stream out-lives the storage in the client application.
720 * -- IStorage_AddRef(&newStream->parentStorage->IStorage_iface);
722 * No, don't do this. Some apps call IStorage_Release without
723 * calling IStream_Release first. If we grab a reference the
724 * file is not closed, and the app fails when it tries to
725 * reopen the file (Easy-PC, for example)
728 newStream->grfMode = grfMode;
729 newStream->dirEntry = dirEntry;
732 * Start the stream at the beginning.
734 newStream->currentPosition.u.HighPart = 0;
735 newStream->currentPosition.u.LowPart = 0;
737 /* add us to the storage's list of active streams */
738 StorageBaseImpl_AddStream(parentStorage, newStream);