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_IPersist, riid) ||
98 IsEqualIID(&IID_IPersistStream, riid) ||
99 IsEqualIID(&IID_ISequentialStream, riid) ||
100 IsEqualIID(&IID_IStream, riid))
102 *ppvObject = &This->IStream_iface;
105 return E_NOINTERFACE;
107 IStream_AddRef(iface);
113 * This implements the IUnknown method AddRef for this
116 static ULONG WINAPI StgStreamImpl_AddRef(
119 StgStreamImpl* This = impl_from_IStream(iface);
120 return InterlockedIncrement(&This->ref);
124 * This implements the IUnknown method Release for this
127 static ULONG WINAPI StgStreamImpl_Release(
130 StgStreamImpl* This = impl_from_IStream(iface);
134 ref = InterlockedDecrement(&This->ref);
137 * If the reference count goes down to 0, perform suicide.
141 StgStreamImpl_Destroy(This);
148 * This method is part of the ISequentialStream interface.
150 * It reads a block of information from the stream at the current
151 * position. It then moves the current position at the end of the
154 * See the documentation of ISequentialStream for more info.
156 static HRESULT WINAPI StgStreamImpl_Read(
158 void* pv, /* [length_is][size_is][out] */
160 ULONG* pcbRead) /* [out] */
162 StgStreamImpl* This = impl_from_IStream(iface);
164 ULONG bytesReadBuffer;
167 TRACE("(%p, %p, %d, %p)\n",
168 iface, pv, cb, pcbRead);
170 if (!This->parentStorage)
172 WARN("storage reverted\n");
173 return STG_E_REVERTED;
177 * If the caller is not interested in the number of bytes read,
178 * we use another buffer to avoid "if" statements in the code.
181 pcbRead = &bytesReadBuffer;
183 res = StorageBaseImpl_StreamReadAt(This->parentStorage,
185 This->currentPosition,
193 * Advance the pointer for the number of positions read.
195 This->currentPosition.u.LowPart += *pcbRead;
198 TRACE("<-- %08x\n", res);
203 * This method is part of the ISequentialStream interface.
205 * It writes a block of information to the stream at the current
206 * position. It then moves the current position at the end of the
207 * written block. If the stream is too small to fit the block,
208 * the stream is grown to fit.
210 * See the documentation of ISequentialStream for more info.
212 static HRESULT WINAPI StgStreamImpl_Write(
214 const void* pv, /* [size_is][in] */
216 ULONG* pcbWritten) /* [out] */
218 StgStreamImpl* This = impl_from_IStream(iface);
220 ULONG bytesWritten = 0;
223 TRACE("(%p, %p, %d, %p)\n",
224 iface, pv, cb, pcbWritten);
227 * Do we have permission to write to this stream?
229 switch(STGM_ACCESS_MODE(This->grfMode))
235 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
236 return STG_E_ACCESSDENIED;
240 return STG_E_INVALIDPOINTER;
242 if (!This->parentStorage)
244 WARN("storage reverted\n");
245 return STG_E_REVERTED;
249 * If the caller is not interested in the number of bytes written,
250 * we use another buffer to avoid "if" statements in the code.
253 pcbWritten = &bytesWritten;
256 * Initialize the out parameter
262 TRACE("<-- S_OK, written 0\n");
266 res = StorageBaseImpl_StreamWriteAt(This->parentStorage,
268 This->currentPosition,
274 * Advance the position pointer for the number of positions written.
276 This->currentPosition.u.LowPart += *pcbWritten;
279 res = StorageBaseImpl_Flush(This->parentStorage);
281 TRACE("<-- S_OK, written %u\n", *pcbWritten);
286 * This method is part of the IStream interface.
288 * It will move the current stream pointer according to the parameters
291 * See the documentation of IStream for more info.
293 static HRESULT WINAPI StgStreamImpl_Seek(
295 LARGE_INTEGER dlibMove, /* [in] */
296 DWORD dwOrigin, /* [in] */
297 ULARGE_INTEGER* plibNewPosition) /* [out] */
299 StgStreamImpl* This = impl_from_IStream(iface);
301 ULARGE_INTEGER newPosition;
302 DirEntry currentEntry;
305 TRACE("(%p, %d, %d, %p)\n",
306 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
309 * fail if the stream has no parent (as does windows)
312 if (!This->parentStorage)
314 WARN("storage reverted\n");
315 return STG_E_REVERTED;
319 * The caller is allowed to pass in NULL as the new position return value.
320 * If it happens, we assign it to a dynamic variable to avoid special cases
323 if (plibNewPosition == 0)
325 plibNewPosition = &newPosition;
329 * The file pointer is moved depending on the given "function"
334 case STREAM_SEEK_SET:
335 plibNewPosition->u.HighPart = 0;
336 plibNewPosition->u.LowPart = 0;
338 case STREAM_SEEK_CUR:
339 *plibNewPosition = This->currentPosition;
341 case STREAM_SEEK_END:
342 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage, This->dirEntry, ¤tEntry);
343 if (FAILED(hr)) return hr;
344 *plibNewPosition = currentEntry.size;
347 WARN("invalid dwOrigin %d\n", dwOrigin);
348 return STG_E_INVALIDFUNCTION;
351 plibNewPosition->QuadPart += dlibMove.QuadPart;
354 * tell the caller what we calculated
356 This->currentPosition = *plibNewPosition;
362 * This method is part of the IStream interface.
364 * It will change the size of a stream.
366 * See the documentation of IStream for more info.
368 static HRESULT WINAPI StgStreamImpl_SetSize(
370 ULARGE_INTEGER libNewSize) /* [in] */
372 StgStreamImpl* This = impl_from_IStream(iface);
376 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
378 if(!This->parentStorage)
380 WARN("storage reverted\n");
381 return STG_E_REVERTED;
387 if (libNewSize.u.HighPart != 0)
389 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
390 return STG_E_INVALIDFUNCTION;
394 * Do we have permission?
396 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
398 WARN("access denied\n");
399 return STG_E_ACCESSDENIED;
402 hr = StorageBaseImpl_StreamSetSize(This->parentStorage, This->dirEntry, libNewSize);
405 hr = StorageBaseImpl_Flush(This->parentStorage);
411 * This method is part of the IStream interface.
413 * It will copy the 'cb' Bytes to 'pstm' IStream.
415 * See the documentation of IStream for more info.
417 static HRESULT WINAPI StgStreamImpl_CopyTo(
419 IStream* pstm, /* [unique][in] */
420 ULARGE_INTEGER cb, /* [in] */
421 ULARGE_INTEGER* pcbRead, /* [out] */
422 ULARGE_INTEGER* pcbWritten) /* [out] */
424 StgStreamImpl* This = impl_from_IStream(iface);
427 ULONG bytesRead, bytesWritten, copySize;
428 ULARGE_INTEGER totalBytesRead;
429 ULARGE_INTEGER totalBytesWritten;
431 TRACE("(%p, %p, %d, %p, %p)\n",
432 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
438 if (!This->parentStorage)
440 WARN("storage reverted\n");
441 return STG_E_REVERTED;
445 return STG_E_INVALIDPOINTER;
447 totalBytesRead.QuadPart = 0;
448 totalBytesWritten.QuadPart = 0;
450 while ( cb.QuadPart > 0 )
452 if ( cb.QuadPart >= sizeof(tmpBuffer) )
453 copySize = sizeof(tmpBuffer);
455 copySize = cb.u.LowPart;
457 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
459 totalBytesRead.QuadPart += bytesRead;
461 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
463 totalBytesWritten.QuadPart += bytesWritten;
466 * Check that read & write operations were successful
468 if (bytesRead != bytesWritten)
470 hr = STG_E_MEDIUMFULL;
471 WARN("medium full\n");
475 if (bytesRead!=copySize)
478 cb.QuadPart -= bytesRead;
481 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
482 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
488 * This method is part of the IStream interface.
490 * For streams contained in structured storages, this method
491 * does nothing. This is what the documentation tells us.
493 * See the documentation of IStream for more info.
495 static HRESULT WINAPI StgStreamImpl_Commit(
497 DWORD grfCommitFlags) /* [in] */
499 StgStreamImpl* This = impl_from_IStream(iface);
501 if (!This->parentStorage)
503 WARN("storage reverted\n");
504 return STG_E_REVERTED;
507 return StorageBaseImpl_Flush(This->parentStorage);
511 * This method is part of the IStream interface.
513 * For streams contained in structured storages, this method
514 * does nothing. This is what the documentation tells us.
516 * See the documentation of IStream for more info.
518 static HRESULT WINAPI StgStreamImpl_Revert(
524 static HRESULT WINAPI StgStreamImpl_LockRegion(
526 ULARGE_INTEGER libOffset, /* [in] */
527 ULARGE_INTEGER cb, /* [in] */
528 DWORD dwLockType) /* [in] */
530 StgStreamImpl* This = impl_from_IStream(iface);
532 if (!This->parentStorage)
534 WARN("storage reverted\n");
535 return STG_E_REVERTED;
538 FIXME("not implemented!\n");
542 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
544 ULARGE_INTEGER libOffset, /* [in] */
545 ULARGE_INTEGER cb, /* [in] */
546 DWORD dwLockType) /* [in] */
548 StgStreamImpl* This = impl_from_IStream(iface);
550 if (!This->parentStorage)
552 WARN("storage reverted\n");
553 return STG_E_REVERTED;
556 FIXME("not implemented!\n");
561 * This method is part of the IStream interface.
563 * This method returns information about the current
566 * See the documentation of IStream for more info.
568 static HRESULT WINAPI StgStreamImpl_Stat(
570 STATSTG* pstatstg, /* [out] */
571 DWORD grfStatFlag) /* [in] */
573 StgStreamImpl* This = impl_from_IStream(iface);
575 DirEntry currentEntry;
578 TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
581 * if stream has no parent, return STG_E_REVERTED
584 if (!This->parentStorage)
586 WARN("storage reverted\n");
587 return STG_E_REVERTED;
591 * Read the information from the directory entry.
593 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage,
599 StorageUtl_CopyDirEntryToSTATSTG(This->parentStorage,
604 pstatstg->grfMode = This->grfMode;
606 /* In simple create mode cbSize is the current pos */
607 if((This->parentStorage->openFlags & STGM_SIMPLE) && This->parentStorage->create)
608 pstatstg->cbSize = This->currentPosition;
613 WARN("failed to read entry\n");
618 * This method is part of the IStream interface.
620 * This method returns a clone of the interface that allows for
621 * another seek pointer
623 * See the documentation of IStream for more info.
625 * I am not totally sure what I am doing here but I presume that this
626 * should be basically as simple as creating a new stream with the same
627 * parent etc and positioning its seek cursor.
629 static HRESULT WINAPI StgStreamImpl_Clone(
631 IStream** ppstm) /* [out] */
633 StgStreamImpl* This = impl_from_IStream(iface);
635 StgStreamImpl* new_stream;
636 LARGE_INTEGER seek_pos;
638 TRACE("%p %p\n", This, ppstm);
644 if (!This->parentStorage)
645 return STG_E_REVERTED;
648 return STG_E_INVALIDPOINTER;
650 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->dirEntry);
653 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
655 *ppstm = &new_stream->IStream_iface;
656 IStream_AddRef(*ppstm);
658 seek_pos.QuadPart = This->currentPosition.QuadPart;
660 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
662 assert (SUCCEEDED(hres));
668 * Virtual function table for the StgStreamImpl class.
670 static const IStreamVtbl StgStreamVtbl =
672 StgStreamImpl_QueryInterface,
673 StgStreamImpl_AddRef,
674 StgStreamImpl_Release,
678 StgStreamImpl_SetSize,
679 StgStreamImpl_CopyTo,
680 StgStreamImpl_Commit,
681 StgStreamImpl_Revert,
682 StgStreamImpl_LockRegion,
683 StgStreamImpl_UnlockRegion,
688 /******************************************************************************
689 ** StgStreamImpl implementation
693 * This is the constructor for the StgStreamImpl class.
696 * parentStorage - Pointer to the storage that contains the stream to open
697 * dirEntry - Index of the directory entry that points to this stream.
699 StgStreamImpl* StgStreamImpl_Construct(
700 StorageBaseImpl* parentStorage,
704 StgStreamImpl* newStream;
706 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
711 * Set-up the virtual function table and reference count.
713 newStream->IStream_iface.lpVtbl = &StgStreamVtbl;
716 newStream->parentStorage = parentStorage;
719 * We want to nail-down the reference to the storage in case the
720 * stream out-lives the storage in the client application.
722 * -- IStorage_AddRef(&newStream->parentStorage->IStorage_iface);
724 * No, don't do this. Some apps call IStorage_Release without
725 * calling IStream_Release first. If we grab a reference the
726 * file is not closed, and the app fails when it tries to
727 * reopen the file (Easy-PC, for example)
730 newStream->grfMode = grfMode;
731 newStream->dirEntry = dirEntry;
734 * Start the stream at the beginning.
736 newStream->currentPosition.u.HighPart = 0;
737 newStream->currentPosition.u.LowPart = 0;
739 /* add us to the storage's list of active streams */
740 StorageBaseImpl_AddStream(parentStorage, newStream);