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
);
47 * This implements the IUnknown method QueryInterface for this
50 static HRESULT WINAPI
StgStreamImpl_QueryInterface(
52 REFIID riid
, /* [in] */
53 void** ppvObject
) /* [iid_is][out] */
55 StgStreamImpl
* This
= impl_from_IStream(iface
);
62 if (IsEqualIID(&IID_IUnknown
, riid
) ||
63 IsEqualIID(&IID_ISequentialStream
, riid
) ||
64 IsEqualIID(&IID_IStream
, riid
))
66 *ppvObject
= &This
->IStream_iface
;
71 IStream_AddRef(iface
);
77 * This implements the IUnknown method AddRef for this
80 static ULONG WINAPI
StgStreamImpl_AddRef(
83 StgStreamImpl
* This
= impl_from_IStream(iface
);
84 return InterlockedIncrement(&This
->ref
);
88 * This implements the IUnknown method Release for this
91 static ULONG WINAPI
StgStreamImpl_Release(
94 StgStreamImpl
* This
= impl_from_IStream(iface
);
95 ULONG ref
= InterlockedDecrement(&This
->ref
);
99 TRACE("(%p)\n", This
);
102 * Release the reference we are holding on the parent storage.
103 * IStorage_Release(&This->parentStorage->IStorage_iface);
105 * No, don't do this. Some apps call IStorage_Release without
106 * calling IStream_Release first. If we grab a reference the
107 * file is not closed, and the app fails when it tries to
108 * reopen the file (Easy-PC, for example). Just inform the
109 * storage that we have closed the stream
112 if (This
->parentStorage
)
113 StorageBaseImpl_RemoveStream(This
->parentStorage
, This
);
114 This
->parentStorage
= 0;
115 HeapFree(GetProcessHeap(), 0, This
);
122 * This method is part of the ISequentialStream interface.
124 * It reads a block of information from the stream at the current
125 * position. It then moves the current position at the end of the
128 * See the documentation of ISequentialStream for more info.
130 static HRESULT WINAPI
StgStreamImpl_Read(
132 void* pv
, /* [length_is][size_is][out] */
134 ULONG
* pcbRead
) /* [out] */
136 StgStreamImpl
* This
= impl_from_IStream(iface
);
138 ULONG bytesReadBuffer
;
141 TRACE("(%p, %p, %d, %p)\n",
142 iface
, pv
, cb
, pcbRead
);
144 if (!This
->parentStorage
)
146 WARN("storage reverted\n");
147 return STG_E_REVERTED
;
151 * If the caller is not interested in the number of bytes read,
152 * we use another buffer to avoid "if" statements in the code.
155 pcbRead
= &bytesReadBuffer
;
157 res
= StorageBaseImpl_StreamReadAt(This
->parentStorage
,
159 This
->currentPosition
,
167 * Advance the pointer for the number of positions read.
169 This
->currentPosition
.QuadPart
+= *pcbRead
;
172 TRACE("<-- %08x\n", res
);
177 * This method is part of the ISequentialStream interface.
179 * It writes a block of information to the stream at the current
180 * position. It then moves the current position at the end of the
181 * written block. If the stream is too small to fit the block,
182 * the stream is grown to fit.
184 * See the documentation of ISequentialStream for more info.
186 static HRESULT WINAPI
StgStreamImpl_Write(
188 const void* pv
, /* [size_is][in] */
190 ULONG
* pcbWritten
) /* [out] */
192 StgStreamImpl
* This
= impl_from_IStream(iface
);
194 ULONG bytesWritten
= 0;
197 TRACE("(%p, %p, %d, %p)\n",
198 iface
, pv
, cb
, pcbWritten
);
201 * Do we have permission to write to this stream?
203 switch(STGM_ACCESS_MODE(This
->grfMode
))
209 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This
->grfMode
));
210 return STG_E_ACCESSDENIED
;
214 return STG_E_INVALIDPOINTER
;
216 if (!This
->parentStorage
)
218 WARN("storage reverted\n");
219 return STG_E_REVERTED
;
223 * If the caller is not interested in the number of bytes written,
224 * we use another buffer to avoid "if" statements in the code.
227 pcbWritten
= &bytesWritten
;
230 * Initialize the out parameter
236 TRACE("<-- S_OK, written 0\n");
240 res
= StorageBaseImpl_StreamWriteAt(This
->parentStorage
,
242 This
->currentPosition
,
248 * Advance the position pointer for the number of positions written.
250 This
->currentPosition
.QuadPart
+= *pcbWritten
;
253 res
= StorageBaseImpl_Flush(This
->parentStorage
);
255 TRACE("<-- %08x, written %u\n", res
, *pcbWritten
);
260 * This method is part of the IStream interface.
262 * It will move the current stream pointer according to the parameters
265 * See the documentation of IStream for more info.
267 static HRESULT WINAPI
StgStreamImpl_Seek(
269 LARGE_INTEGER dlibMove
, /* [in] */
270 DWORD dwOrigin
, /* [in] */
271 ULARGE_INTEGER
* plibNewPosition
) /* [out] */
273 StgStreamImpl
* This
= impl_from_IStream(iface
);
275 ULARGE_INTEGER newPosition
;
276 DirEntry currentEntry
;
279 TRACE("(%p, %d, %d, %p)\n",
280 iface
, dlibMove
.u
.LowPart
, dwOrigin
, plibNewPosition
);
283 * fail if the stream has no parent (as does windows)
286 if (!This
->parentStorage
)
288 WARN("storage reverted\n");
289 return STG_E_REVERTED
;
293 * The caller is allowed to pass in NULL as the new position return value.
294 * If it happens, we assign it to a dynamic variable to avoid special cases
297 if (plibNewPosition
== 0)
299 plibNewPosition
= &newPosition
;
303 * The file pointer is moved depending on the given "function"
308 case STREAM_SEEK_SET
:
309 plibNewPosition
->u
.HighPart
= 0;
310 plibNewPosition
->u
.LowPart
= 0;
312 case STREAM_SEEK_CUR
:
313 *plibNewPosition
= This
->currentPosition
;
315 case STREAM_SEEK_END
:
316 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
, This
->dirEntry
, ¤tEntry
);
317 if (FAILED(hr
)) return hr
;
318 *plibNewPosition
= currentEntry
.size
;
321 WARN("invalid dwOrigin %d\n", dwOrigin
);
322 return STG_E_INVALIDFUNCTION
;
325 plibNewPosition
->QuadPart
+= dlibMove
.QuadPart
;
328 * tell the caller what we calculated
330 This
->currentPosition
= *plibNewPosition
;
336 * This method is part of the IStream interface.
338 * It will change the size of a stream.
340 * See the documentation of IStream for more info.
342 static HRESULT WINAPI
StgStreamImpl_SetSize(
344 ULARGE_INTEGER libNewSize
) /* [in] */
346 StgStreamImpl
* This
= impl_from_IStream(iface
);
350 TRACE("(%p, %d)\n", iface
, libNewSize
.u
.LowPart
);
352 if(!This
->parentStorage
)
354 WARN("storage reverted\n");
355 return STG_E_REVERTED
;
361 if (libNewSize
.u
.HighPart
!= 0)
363 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize
.u
.HighPart
);
364 return STG_E_INVALIDFUNCTION
;
368 * Do we have permission?
370 if (!(This
->grfMode
& (STGM_WRITE
| STGM_READWRITE
)))
372 WARN("access denied\n");
373 return STG_E_ACCESSDENIED
;
376 hr
= StorageBaseImpl_StreamSetSize(This
->parentStorage
, This
->dirEntry
, libNewSize
);
379 hr
= StorageBaseImpl_Flush(This
->parentStorage
);
385 * This method is part of the IStream interface.
387 * It will copy the 'cb' Bytes to 'pstm' IStream.
389 * See the documentation of IStream for more info.
391 static HRESULT WINAPI
StgStreamImpl_CopyTo(
393 IStream
* pstm
, /* [unique][in] */
394 ULARGE_INTEGER cb
, /* [in] */
395 ULARGE_INTEGER
* pcbRead
, /* [out] */
396 ULARGE_INTEGER
* pcbWritten
) /* [out] */
398 StgStreamImpl
* This
= impl_from_IStream(iface
);
401 ULONG bytesRead
, bytesWritten
, copySize
;
402 ULARGE_INTEGER totalBytesRead
;
403 ULARGE_INTEGER totalBytesWritten
;
405 TRACE("(%p, %p, %d, %p, %p)\n",
406 iface
, pstm
, cb
.u
.LowPart
, pcbRead
, pcbWritten
);
412 if (!This
->parentStorage
)
414 WARN("storage reverted\n");
415 return STG_E_REVERTED
;
419 return STG_E_INVALIDPOINTER
;
421 totalBytesRead
.QuadPart
= 0;
422 totalBytesWritten
.QuadPart
= 0;
424 while ( cb
.QuadPart
> 0 )
426 if ( cb
.QuadPart
>= sizeof(tmpBuffer
) )
427 copySize
= sizeof(tmpBuffer
);
429 copySize
= cb
.u
.LowPart
;
431 IStream_Read(iface
, tmpBuffer
, copySize
, &bytesRead
);
433 totalBytesRead
.QuadPart
+= bytesRead
;
435 IStream_Write(pstm
, tmpBuffer
, bytesRead
, &bytesWritten
);
437 totalBytesWritten
.QuadPart
+= bytesWritten
;
440 * Check that read & write operations were successful
442 if (bytesRead
!= bytesWritten
)
444 hr
= STG_E_MEDIUMFULL
;
445 WARN("medium full\n");
449 if (bytesRead
!=copySize
)
452 cb
.QuadPart
-= bytesRead
;
455 if (pcbRead
) pcbRead
->QuadPart
= totalBytesRead
.QuadPart
;
456 if (pcbWritten
) pcbWritten
->QuadPart
= totalBytesWritten
.QuadPart
;
462 * This method is part of the IStream interface.
464 * For streams contained in structured storages, this method
465 * does nothing. This is what the documentation tells us.
467 * See the documentation of IStream for more info.
469 static HRESULT WINAPI
StgStreamImpl_Commit(
471 DWORD grfCommitFlags
) /* [in] */
473 StgStreamImpl
* This
= impl_from_IStream(iface
);
475 if (!This
->parentStorage
)
477 WARN("storage reverted\n");
478 return STG_E_REVERTED
;
481 return StorageBaseImpl_Flush(This
->parentStorage
);
485 * This method is part of the IStream interface.
487 * For streams contained in structured storages, this method
488 * does nothing. This is what the documentation tells us.
490 * See the documentation of IStream for more info.
492 static HRESULT WINAPI
StgStreamImpl_Revert(
498 static HRESULT WINAPI
StgStreamImpl_LockRegion(
500 ULARGE_INTEGER libOffset
, /* [in] */
501 ULARGE_INTEGER cb
, /* [in] */
502 DWORD dwLockType
) /* [in] */
504 StgStreamImpl
* This
= impl_from_IStream(iface
);
506 if (!This
->parentStorage
)
508 WARN("storage reverted\n");
509 return STG_E_REVERTED
;
512 FIXME("not implemented!\n");
516 static HRESULT WINAPI
StgStreamImpl_UnlockRegion(
518 ULARGE_INTEGER libOffset
, /* [in] */
519 ULARGE_INTEGER cb
, /* [in] */
520 DWORD dwLockType
) /* [in] */
522 StgStreamImpl
* This
= impl_from_IStream(iface
);
524 if (!This
->parentStorage
)
526 WARN("storage reverted\n");
527 return STG_E_REVERTED
;
530 FIXME("not implemented!\n");
535 * This method is part of the IStream interface.
537 * This method returns information about the current
540 * See the documentation of IStream for more info.
542 static HRESULT WINAPI
StgStreamImpl_Stat(
544 STATSTG
* pstatstg
, /* [out] */
545 DWORD grfStatFlag
) /* [in] */
547 StgStreamImpl
* This
= impl_from_IStream(iface
);
549 DirEntry currentEntry
;
552 TRACE("%p %p %d\n", This
, pstatstg
, grfStatFlag
);
555 * if stream has no parent, return STG_E_REVERTED
558 if (!This
->parentStorage
)
560 WARN("storage reverted\n");
561 return STG_E_REVERTED
;
565 * Read the information from the directory entry.
567 hr
= StorageBaseImpl_ReadDirEntry(This
->parentStorage
,
573 StorageUtl_CopyDirEntryToSTATSTG(This
->parentStorage
,
578 pstatstg
->grfMode
= This
->grfMode
;
580 /* In simple create mode cbSize is the current pos */
581 if((This
->parentStorage
->openFlags
& STGM_SIMPLE
) && This
->parentStorage
->create
)
582 pstatstg
->cbSize
= This
->currentPosition
;
587 WARN("failed to read entry\n");
592 * This method is part of the IStream interface.
594 * This method returns a clone of the interface that allows for
595 * another seek pointer
597 * See the documentation of IStream for more info.
599 * I am not totally sure what I am doing here but I presume that this
600 * should be basically as simple as creating a new stream with the same
601 * parent etc and positioning its seek cursor.
603 static HRESULT WINAPI
StgStreamImpl_Clone(
605 IStream
** ppstm
) /* [out] */
607 StgStreamImpl
* This
= impl_from_IStream(iface
);
609 StgStreamImpl
* new_stream
;
610 LARGE_INTEGER seek_pos
;
612 TRACE("%p %p\n", This
, ppstm
);
618 if (!This
->parentStorage
)
619 return STG_E_REVERTED
;
622 return STG_E_INVALIDPOINTER
;
624 new_stream
= StgStreamImpl_Construct (This
->parentStorage
, This
->grfMode
, This
->dirEntry
);
627 return STG_E_INSUFFICIENTMEMORY
; /* Currently the only reason for new_stream=0 */
629 *ppstm
= &new_stream
->IStream_iface
;
630 IStream_AddRef(*ppstm
);
632 seek_pos
.QuadPart
= This
->currentPosition
.QuadPart
;
634 hres
= IStream_Seek(*ppstm
, seek_pos
, STREAM_SEEK_SET
, NULL
);
636 assert (SUCCEEDED(hres
));
642 * Virtual function table for the StgStreamImpl class.
644 static const IStreamVtbl StgStreamVtbl
=
646 StgStreamImpl_QueryInterface
,
647 StgStreamImpl_AddRef
,
648 StgStreamImpl_Release
,
652 StgStreamImpl_SetSize
,
653 StgStreamImpl_CopyTo
,
654 StgStreamImpl_Commit
,
655 StgStreamImpl_Revert
,
656 StgStreamImpl_LockRegion
,
657 StgStreamImpl_UnlockRegion
,
662 /******************************************************************************
663 ** StgStreamImpl implementation
667 * This is the constructor for the StgStreamImpl class.
670 * parentStorage - Pointer to the storage that contains the stream to open
671 * dirEntry - Index of the directory entry that points to this stream.
673 StgStreamImpl
* StgStreamImpl_Construct(
674 StorageBaseImpl
* parentStorage
,
678 StgStreamImpl
* newStream
;
680 newStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl
));
685 * Set-up the virtual function table and reference count.
687 newStream
->IStream_iface
.lpVtbl
= &StgStreamVtbl
;
690 newStream
->parentStorage
= parentStorage
;
693 * We want to nail-down the reference to the storage in case the
694 * stream out-lives the storage in the client application.
696 * -- IStorage_AddRef(&newStream->parentStorage->IStorage_iface);
698 * No, don't do this. Some apps call IStorage_Release without
699 * calling IStream_Release first. If we grab a reference the
700 * file is not closed, and the app fails when it tries to
701 * reopen the file (Easy-PC, for example)
704 newStream
->grfMode
= grfMode
;
705 newStream
->dirEntry
= dirEntry
;
708 * Start the stream at the beginning.
710 newStream
->currentPosition
.u
.HighPart
= 0;
711 newStream
->currentPosition
.u
.LowPart
= 0;
713 /* add us to the storage's list of active streams */
714 StorageBaseImpl_AddStream(parentStorage
, newStream
);