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((IStorage*)This->parentStorage);
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;
78 * Finally, free the memory used-up by the class.
80 HeapFree(GetProcessHeap(), 0, This
);
84 * This implements the IUnknown method QueryInterface for this
87 static HRESULT WINAPI
StgStreamImpl_QueryInterface(
89 REFIID riid
, /* [in] */
90 void** ppvObject
) /* [iid_is][out] */
92 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
95 * Perform a sanity check on the parameters.
101 * Initialize the return parameter.
106 * Compare the riid with the interface IDs implemented by this object.
108 if (IsEqualIID(&IID_IUnknown
, riid
) ||
109 IsEqualIID(&IID_IPersist
, riid
) ||
110 IsEqualIID(&IID_IPersistStream
, riid
) ||
111 IsEqualIID(&IID_ISequentialStream
, riid
) ||
112 IsEqualIID(&IID_IStream
, riid
))
118 * Check that we obtained an interface.
121 return E_NOINTERFACE
;
124 * Query Interface always increases the reference count by one when it is
127 IStream_AddRef(iface
);
133 * This implements the IUnknown method AddRef for this
136 static ULONG WINAPI
StgStreamImpl_AddRef(
139 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
140 return InterlockedIncrement(&This
->ref
);
144 * This implements the IUnknown method Release for this
147 static ULONG WINAPI
StgStreamImpl_Release(
150 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
154 ref
= InterlockedDecrement(&This
->ref
);
157 * If the reference count goes down to 0, perform suicide.
161 StgStreamImpl_Destroy(This
);
168 * This method is part of the ISequentialStream interface.
170 * It reads a block of information from the stream at the current
171 * position. It then moves the current position at the end of the
174 * See the documentation of ISequentialStream for more info.
176 static HRESULT WINAPI
StgStreamImpl_Read(
178 void* pv
, /* [length_is][size_is][out] */
180 ULONG
* pcbRead
) /* [out] */
182 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
184 ULONG bytesReadBuffer
;
187 TRACE("(%p, %p, %d, %p)\n",
188 iface
, pv
, cb
, pcbRead
);
190 if (!This
->parentStorage
)
192 WARN("storage reverted\n");
193 return STG_E_REVERTED
;
197 * If the caller is not interested in the number of bytes read,
198 * we use another buffer to avoid "if" statements in the code.
201 pcbRead
= &bytesReadBuffer
;
203 res
= StorageBaseImpl_StreamReadAt(This
->parentStorage
,
205 This
->currentPosition
,
213 * Advance the pointer for the number of positions read.
215 This
->currentPosition
.u
.LowPart
+= *pcbRead
;
218 TRACE("<-- %08x\n", res
);
223 * This method is part of the ISequentialStream interface.
225 * It writes a block of information to the stream at the current
226 * position. It then moves the current position at the end of the
227 * written block. If the stream is too small to fit the block,
228 * the stream is grown to fit.
230 * See the documentation of ISequentialStream for more info.
232 static HRESULT WINAPI
StgStreamImpl_Write(
234 const void* pv
, /* [size_is][in] */
236 ULONG
* pcbWritten
) /* [out] */
238 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
240 ULONG bytesWritten
= 0;
243 TRACE("(%p, %p, %d, %p)\n",
244 iface
, pv
, cb
, pcbWritten
);
247 * Do we have permission to write to this stream?
249 switch(STGM_ACCESS_MODE(This
->grfMode
))
255 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This
->grfMode
));
256 return STG_E_ACCESSDENIED
;
260 return STG_E_INVALIDPOINTER
;
262 if (!This
->parentStorage
)
264 WARN("storage reverted\n");
265 return STG_E_REVERTED
;
269 * If the caller is not interested in the number of bytes written,
270 * we use another buffer to avoid "if" statements in the code.
273 pcbWritten
= &bytesWritten
;
276 * Initialize the out parameter
282 TRACE("<-- S_OK, written 0\n");
286 res
= StorageBaseImpl_StreamWriteAt(This
->parentStorage
,
288 This
->currentPosition
,
294 * Advance the position pointer for the number of positions written.
296 This
->currentPosition
.u
.LowPart
+= *pcbWritten
;
298 TRACE("<-- S_OK, written %u\n", *pcbWritten
);
303 * This method is part of the IStream interface.
305 * It will move the current stream pointer according to the parameters
308 * See the documentation of IStream for more info.
310 static HRESULT WINAPI
StgStreamImpl_Seek(
312 LARGE_INTEGER dlibMove
, /* [in] */
313 DWORD dwOrigin
, /* [in] */
314 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
316 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
318 ULARGE_INTEGER newPosition
;
319 DirEntry currentEntry
;
322 TRACE("(%p, %d, %d, %p)\n",
323 iface
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
326 * fail if the stream has no parent (as does windows)
329 if (!This
->parentStorage
)
331 WARN("storage reverted\n");
332 return STG_E_REVERTED
;
336 * The caller is allowed to pass in NULL as the new position return value.
337 * If it happens, we assign it to a dynamic variable to avoid special cases
340 if (plibNewPosition
== 0)
342 plibNewPosition
= &newPosition
;
346 * The file pointer is moved depending on the given "function"
351 case STREAM_SEEK_SET
:
352 plibNewPosition
->u
.HighPart
= 0;
353 plibNewPosition
->u
.LowPart
= 0;
355 case STREAM_SEEK_CUR
:
356 *plibNewPosition
= This
->currentPosition
;
358 case STREAM_SEEK_END
:
359 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
, This
->dirEntry
, ¤tEntry
);
360 if (FAILED(hr
)) return hr
;
361 *plibNewPosition
= currentEntry
.size
;
364 WARN("invalid dwOrigin %d\n", dwOrigin
);
365 return STG_E_INVALIDFUNCTION
;
368 plibNewPosition
->QuadPart
+= dlibMove
.QuadPart
;
371 * tell the caller what we calculated
373 This
->currentPosition
= *plibNewPosition
;
379 * This method is part of the IStream interface.
381 * It will change the size of a stream.
383 * See the documentation of IStream for more info.
385 static HRESULT WINAPI
StgStreamImpl_SetSize(
387 ULARGE_INTEGER libNewSize
) /* [in] */
389 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
393 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
395 if(!This
->parentStorage
)
397 WARN("storage reverted\n");
398 return STG_E_REVERTED
;
404 if (libNewSize
.u
.HighPart
!= 0)
406 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize
.u
.HighPart
);
407 return STG_E_INVALIDFUNCTION
;
411 * Do we have permission?
413 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
415 WARN("access denied\n");
416 return STG_E_ACCESSDENIED
;
419 hr
= StorageBaseImpl_StreamSetSize(This
->parentStorage
, This
->dirEntry
, libNewSize
);
424 * This method is part of the IStream interface.
426 * It will copy the 'cb' Bytes to 'pstm' IStream.
428 * See the documentation of IStream for more info.
430 static HRESULT WINAPI
StgStreamImpl_CopyTo(
432 IStream
* pstm
, /* [unique][in] */
433 ULARGE_INTEGER cb
, /* [in] */
434 ULARGE_INTEGER
* pcbRead
, /* [out] */
435 ULARGE_INTEGER
* pcbWritten
) /* [out] */
437 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
440 ULONG bytesRead
, bytesWritten
, copySize
;
441 ULARGE_INTEGER totalBytesRead
;
442 ULARGE_INTEGER totalBytesWritten
;
444 TRACE("(%p, %p, %d, %p, %p)\n",
445 iface
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
451 if (!This
->parentStorage
)
453 WARN("storage reverted\n");
454 return STG_E_REVERTED
;
458 return STG_E_INVALIDPOINTER
;
460 totalBytesRead
.QuadPart
= 0;
461 totalBytesWritten
.QuadPart
= 0;
463 while ( cb
.QuadPart
> 0 )
465 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
466 copySize
= sizeof(tmpBuffer
);
468 copySize
= cb
.u
.LowPart
;
470 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
472 totalBytesRead
.QuadPart
+= bytesRead
;
474 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
476 totalBytesWritten
.QuadPart
+= bytesWritten
;
479 * Check that read & write operations were successful
481 if (bytesRead
!= bytesWritten
)
483 hr
= STG_E_MEDIUMFULL
;
484 WARN("medium full\n");
488 if (bytesRead
!=copySize
)
491 cb
.QuadPart
-= bytesRead
;
494 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
495 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
501 * This method is part of the IStream interface.
503 * For streams contained in structured storages, this method
504 * does nothing. This is what the documentation tells us.
506 * See the documentation of IStream for more info.
508 static HRESULT WINAPI
StgStreamImpl_Commit(
510 DWORD grfCommitFlags
) /* [in] */
512 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
514 if (!This
->parentStorage
)
516 WARN("storage reverted\n");
517 return STG_E_REVERTED
;
524 * This method is part of the IStream interface.
526 * For streams contained in structured storages, this method
527 * does nothing. This is what the documentation tells us.
529 * See the documentation of IStream for more info.
531 static HRESULT WINAPI
StgStreamImpl_Revert(
537 static HRESULT WINAPI
StgStreamImpl_LockRegion(
539 ULARGE_INTEGER libOffset
, /* [in] */
540 ULARGE_INTEGER cb
, /* [in] */
541 DWORD dwLockType
) /* [in] */
543 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
545 if (!This
->parentStorage
)
547 WARN("storage reverted\n");
548 return STG_E_REVERTED
;
551 FIXME("not implemented!\n");
555 static HRESULT WINAPI
StgStreamImpl_UnlockRegion(
557 ULARGE_INTEGER libOffset
, /* [in] */
558 ULARGE_INTEGER cb
, /* [in] */
559 DWORD dwLockType
) /* [in] */
561 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
563 if (!This
->parentStorage
)
565 WARN("storage reverted\n");
566 return STG_E_REVERTED
;
569 FIXME("not implemented!\n");
574 * This method is part of the IStream interface.
576 * This method returns information about the current
579 * See the documentation of IStream for more info.
581 static HRESULT WINAPI
StgStreamImpl_Stat(
583 STATSTG
* pstatstg
, /* [out] */
584 DWORD grfStatFlag
) /* [in] */
586 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
588 DirEntry currentEntry
;
591 TRACE("%p %p %d\n", This
, pstatstg
, grfStatFlag
);
594 * if stream has no parent, return STG_E_REVERTED
597 if (!This
->parentStorage
)
599 WARN("storage reverted\n");
600 return STG_E_REVERTED
;
604 * Read the information from the directory entry.
606 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
,
612 StorageUtl_CopyDirEntryToSTATSTG(This
->parentStorage
,
617 pstatstg
->grfMode
= This
->grfMode
;
619 /* In simple create mode cbSize is the current pos */
620 if((This
->parentStorage
->openFlags
& STGM_SIMPLE
) && This
->parentStorage
->create
)
621 pstatstg
->cbSize
= This
->currentPosition
;
626 WARN("failed to read entry\n");
631 * This method is part of the IStream interface.
633 * This method returns a clone of the interface that allows for
634 * another seek pointer
636 * See the documentation of IStream for more info.
638 * I am not totally sure what I am doing here but I presume that this
639 * should be basically as simple as creating a new stream with the same
640 * parent etc and positioning its seek cursor.
642 static HRESULT WINAPI
StgStreamImpl_Clone(
644 IStream
** ppstm
) /* [out] */
646 StgStreamImpl
* const This
=(StgStreamImpl
*)iface
;
648 StgStreamImpl
* new_stream
;
649 LARGE_INTEGER seek_pos
;
651 TRACE("%p %p\n", This
, ppstm
);
657 if (!This
->parentStorage
)
658 return STG_E_REVERTED
;
661 return STG_E_INVALIDPOINTER
;
663 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->dirEntry
);
666 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
668 *ppstm
= (IStream
*) new_stream
;
669 IStream_AddRef(*ppstm
);
671 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
673 hres
=StgStreamImpl_Seek (*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
675 assert (SUCCEEDED(hres
));
681 * Virtual function table for the StgStreamImpl class.
683 static const IStreamVtbl StgStreamImpl_Vtbl
=
685 StgStreamImpl_QueryInterface
,
686 StgStreamImpl_AddRef
,
687 StgStreamImpl_Release
,
691 StgStreamImpl_SetSize
,
692 StgStreamImpl_CopyTo
,
693 StgStreamImpl_Commit
,
694 StgStreamImpl_Revert
,
695 StgStreamImpl_LockRegion
,
696 StgStreamImpl_UnlockRegion
,
701 /******************************************************************************
702 ** StgStreamImpl implementation
706 * This is the constructor for the StgStreamImpl class.
709 * parentStorage - Pointer to the storage that contains the stream to open
710 * dirEntry - Index of the directory entry that points to this stream.
712 StgStreamImpl
* StgStreamImpl_Construct(
713 StorageBaseImpl
* parentStorage
,
717 StgStreamImpl
* newStream
;
719 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
724 * Set-up the virtual function table and reference count.
726 newStream
->lpVtbl
= &StgStreamImpl_Vtbl
;
729 newStream
->parentStorage
= parentStorage
;
732 * We want to nail-down the reference to the storage in case the
733 * stream out-lives the storage in the client application.
735 * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
737 * No, don't do this. Some apps call IStorage_Release without
738 * calling IStream_Release first. If we grab a reference the
739 * file is not closed, and the app fails when it tries to
740 * reopen the file (Easy-PC, for example)
743 newStream
->grfMode
= grfMode
;
744 newStream
->dirEntry
= dirEntry
;
747 * Start the stream at the beginning.
749 newStream
->currentPosition
.u
.HighPart
= 0;
750 newStream
->currentPosition
.u
.LowPart
= 0;
752 /* add us to the storage's list of active streams */
753 StorageBaseImpl_AddStream(parentStorage
, newStream
);