jscript: Removed unused do_*_tag_format arguments.
[wine/multimedia.git] / dlls / ole32 / stg_stream.c
blobb09ba5b854bc685610d4372abc48b882c0478fae
1 /*
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
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 "winerror.h"
39 #include "winternl.h"
40 #include "wine/debug.h"
42 #include "storage32.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(storage);
47 /***
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
52 * be valid anymore.
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);
83 /***
84 * This implements the IUnknown method QueryInterface for this
85 * class
87 static HRESULT WINAPI StgStreamImpl_QueryInterface(
88 IStream* iface,
89 REFIID riid, /* [in] */
90 void** ppvObject) /* [iid_is][out] */
92 StgStreamImpl* const This=(StgStreamImpl*)iface;
95 * Perform a sanity check on the parameters.
97 if (ppvObject==0)
98 return E_INVALIDARG;
101 * Initialize the return parameter.
103 *ppvObject = 0;
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))
114 *ppvObject = This;
118 * Check that we obtained an interface.
120 if ((*ppvObject)==0)
121 return E_NOINTERFACE;
124 * Query Interface always increases the reference count by one when it is
125 * successful
127 IStream_AddRef(iface);
129 return S_OK;
132 /***
133 * This implements the IUnknown method AddRef for this
134 * class
136 static ULONG WINAPI StgStreamImpl_AddRef(
137 IStream* iface)
139 StgStreamImpl* const This=(StgStreamImpl*)iface;
140 return InterlockedIncrement(&This->ref);
143 /***
144 * This implements the IUnknown method Release for this
145 * class
147 static ULONG WINAPI StgStreamImpl_Release(
148 IStream* iface)
150 StgStreamImpl* const This=(StgStreamImpl*)iface;
152 ULONG ref;
154 ref = InterlockedDecrement(&This->ref);
157 * If the reference count goes down to 0, perform suicide.
159 if (ref==0)
161 StgStreamImpl_Destroy(This);
164 return ref;
167 /***
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
172 * read block
174 * See the documentation of ISequentialStream for more info.
176 static HRESULT WINAPI StgStreamImpl_Read(
177 IStream* iface,
178 void* pv, /* [length_is][size_is][out] */
179 ULONG cb, /* [in] */
180 ULONG* pcbRead) /* [out] */
182 StgStreamImpl* const This=(StgStreamImpl*)iface;
184 ULONG bytesReadBuffer;
185 HRESULT res;
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.
200 if (pcbRead==0)
201 pcbRead = &bytesReadBuffer;
203 res = StorageBaseImpl_StreamReadAt(This->parentStorage,
204 This->dirEntry,
205 This->currentPosition,
208 pcbRead);
210 if (SUCCEEDED(res))
213 * Advance the pointer for the number of positions read.
215 This->currentPosition.u.LowPart += *pcbRead;
218 TRACE("<-- %08x\n", res);
219 return res;
222 /***
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(
233 IStream* iface,
234 const void* pv, /* [size_is][in] */
235 ULONG cb, /* [in] */
236 ULONG* pcbWritten) /* [out] */
238 StgStreamImpl* const This=(StgStreamImpl*)iface;
240 ULONG bytesWritten = 0;
241 HRESULT res;
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))
251 case STGM_WRITE:
252 case STGM_READWRITE:
253 break;
254 default:
255 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
256 return STG_E_ACCESSDENIED;
259 if (!pv)
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.
272 if (pcbWritten == 0)
273 pcbWritten = &bytesWritten;
276 * Initialize the out parameter
278 *pcbWritten = 0;
280 if (cb == 0)
282 TRACE("<-- S_OK, written 0\n");
283 return S_OK;
286 res = StorageBaseImpl_StreamWriteAt(This->parentStorage,
287 This->dirEntry,
288 This->currentPosition,
291 pcbWritten);
294 * Advance the position pointer for the number of positions written.
296 This->currentPosition.u.LowPart += *pcbWritten;
298 if (SUCCEEDED(res))
299 res = StorageBaseImpl_Flush(This->parentStorage);
301 TRACE("<-- S_OK, written %u\n", *pcbWritten);
302 return res;
305 /***
306 * This method is part of the IStream interface.
308 * It will move the current stream pointer according to the parameters
309 * given.
311 * See the documentation of IStream for more info.
313 static HRESULT WINAPI StgStreamImpl_Seek(
314 IStream* iface,
315 LARGE_INTEGER dlibMove, /* [in] */
316 DWORD dwOrigin, /* [in] */
317 ULARGE_INTEGER* plibNewPosition) /* [out] */
319 StgStreamImpl* const This=(StgStreamImpl*)iface;
321 ULARGE_INTEGER newPosition;
322 DirEntry currentEntry;
323 HRESULT hr;
325 TRACE("(%p, %d, %d, %p)\n",
326 iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
329 * fail if the stream has no parent (as does windows)
332 if (!This->parentStorage)
334 WARN("storage reverted\n");
335 return STG_E_REVERTED;
339 * The caller is allowed to pass in NULL as the new position return value.
340 * If it happens, we assign it to a dynamic variable to avoid special cases
341 * in the code below.
343 if (plibNewPosition == 0)
345 plibNewPosition = &newPosition;
349 * The file pointer is moved depending on the given "function"
350 * parameter.
352 switch (dwOrigin)
354 case STREAM_SEEK_SET:
355 plibNewPosition->u.HighPart = 0;
356 plibNewPosition->u.LowPart = 0;
357 break;
358 case STREAM_SEEK_CUR:
359 *plibNewPosition = This->currentPosition;
360 break;
361 case STREAM_SEEK_END:
362 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage, This->dirEntry, &currentEntry);
363 if (FAILED(hr)) return hr;
364 *plibNewPosition = currentEntry.size;
365 break;
366 default:
367 WARN("invalid dwOrigin %d\n", dwOrigin);
368 return STG_E_INVALIDFUNCTION;
371 plibNewPosition->QuadPart += dlibMove.QuadPart;
374 * tell the caller what we calculated
376 This->currentPosition = *plibNewPosition;
378 return S_OK;
381 /***
382 * This method is part of the IStream interface.
384 * It will change the size of a stream.
386 * See the documentation of IStream for more info.
388 static HRESULT WINAPI StgStreamImpl_SetSize(
389 IStream* iface,
390 ULARGE_INTEGER libNewSize) /* [in] */
392 StgStreamImpl* const This=(StgStreamImpl*)iface;
394 HRESULT hr;
396 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
398 if(!This->parentStorage)
400 WARN("storage reverted\n");
401 return STG_E_REVERTED;
405 * As documented.
407 if (libNewSize.u.HighPart != 0)
409 WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
410 return STG_E_INVALIDFUNCTION;
414 * Do we have permission?
416 if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
418 WARN("access denied\n");
419 return STG_E_ACCESSDENIED;
422 hr = StorageBaseImpl_StreamSetSize(This->parentStorage, This->dirEntry, libNewSize);
424 if (SUCCEEDED(hr))
425 hr = StorageBaseImpl_Flush(This->parentStorage);
427 return hr;
430 /***
431 * This method is part of the IStream interface.
433 * It will copy the 'cb' Bytes to 'pstm' IStream.
435 * See the documentation of IStream for more info.
437 static HRESULT WINAPI StgStreamImpl_CopyTo(
438 IStream* iface,
439 IStream* pstm, /* [unique][in] */
440 ULARGE_INTEGER cb, /* [in] */
441 ULARGE_INTEGER* pcbRead, /* [out] */
442 ULARGE_INTEGER* pcbWritten) /* [out] */
444 StgStreamImpl* const This=(StgStreamImpl*)iface;
445 HRESULT hr = S_OK;
446 BYTE tmpBuffer[128];
447 ULONG bytesRead, bytesWritten, copySize;
448 ULARGE_INTEGER totalBytesRead;
449 ULARGE_INTEGER totalBytesWritten;
451 TRACE("(%p, %p, %d, %p, %p)\n",
452 iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
455 * Sanity check
458 if (!This->parentStorage)
460 WARN("storage reverted\n");
461 return STG_E_REVERTED;
464 if ( pstm == 0 )
465 return STG_E_INVALIDPOINTER;
467 totalBytesRead.QuadPart = 0;
468 totalBytesWritten.QuadPart = 0;
470 while ( cb.QuadPart > 0 )
472 if ( cb.QuadPart >= sizeof(tmpBuffer) )
473 copySize = sizeof(tmpBuffer);
474 else
475 copySize = cb.u.LowPart;
477 IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
479 totalBytesRead.QuadPart += bytesRead;
481 IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
483 totalBytesWritten.QuadPart += bytesWritten;
486 * Check that read & write operations were successful
488 if (bytesRead != bytesWritten)
490 hr = STG_E_MEDIUMFULL;
491 WARN("medium full\n");
492 break;
495 if (bytesRead!=copySize)
496 cb.QuadPart = 0;
497 else
498 cb.QuadPart -= bytesRead;
501 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
502 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
504 return hr;
507 /***
508 * This method is part of the IStream interface.
510 * For streams contained in structured storages, this method
511 * does nothing. This is what the documentation tells us.
513 * See the documentation of IStream for more info.
515 static HRESULT WINAPI StgStreamImpl_Commit(
516 IStream* iface,
517 DWORD grfCommitFlags) /* [in] */
519 StgStreamImpl* const This=(StgStreamImpl*)iface;
521 if (!This->parentStorage)
523 WARN("storage reverted\n");
524 return STG_E_REVERTED;
527 return StorageBaseImpl_Flush(This->parentStorage);
530 /***
531 * This method is part of the IStream interface.
533 * For streams contained in structured storages, this method
534 * does nothing. This is what the documentation tells us.
536 * See the documentation of IStream for more info.
538 static HRESULT WINAPI StgStreamImpl_Revert(
539 IStream* iface)
541 return S_OK;
544 static HRESULT WINAPI StgStreamImpl_LockRegion(
545 IStream* iface,
546 ULARGE_INTEGER libOffset, /* [in] */
547 ULARGE_INTEGER cb, /* [in] */
548 DWORD dwLockType) /* [in] */
550 StgStreamImpl* const This=(StgStreamImpl*)iface;
552 if (!This->parentStorage)
554 WARN("storage reverted\n");
555 return STG_E_REVERTED;
558 FIXME("not implemented!\n");
559 return E_NOTIMPL;
562 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
563 IStream* iface,
564 ULARGE_INTEGER libOffset, /* [in] */
565 ULARGE_INTEGER cb, /* [in] */
566 DWORD dwLockType) /* [in] */
568 StgStreamImpl* const This=(StgStreamImpl*)iface;
570 if (!This->parentStorage)
572 WARN("storage reverted\n");
573 return STG_E_REVERTED;
576 FIXME("not implemented!\n");
577 return E_NOTIMPL;
580 /***
581 * This method is part of the IStream interface.
583 * This method returns information about the current
584 * stream.
586 * See the documentation of IStream for more info.
588 static HRESULT WINAPI StgStreamImpl_Stat(
589 IStream* iface,
590 STATSTG* pstatstg, /* [out] */
591 DWORD grfStatFlag) /* [in] */
593 StgStreamImpl* const This=(StgStreamImpl*)iface;
595 DirEntry currentEntry;
596 HRESULT hr;
598 TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
601 * if stream has no parent, return STG_E_REVERTED
604 if (!This->parentStorage)
606 WARN("storage reverted\n");
607 return STG_E_REVERTED;
611 * Read the information from the directory entry.
613 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage,
614 This->dirEntry,
615 &currentEntry);
617 if (SUCCEEDED(hr))
619 StorageUtl_CopyDirEntryToSTATSTG(This->parentStorage,
620 pstatstg,
621 &currentEntry,
622 grfStatFlag);
624 pstatstg->grfMode = This->grfMode;
626 /* In simple create mode cbSize is the current pos */
627 if((This->parentStorage->openFlags & STGM_SIMPLE) && This->parentStorage->create)
628 pstatstg->cbSize = This->currentPosition;
630 return S_OK;
633 WARN("failed to read entry\n");
634 return hr;
637 /***
638 * This method is part of the IStream interface.
640 * This method returns a clone of the interface that allows for
641 * another seek pointer
643 * See the documentation of IStream for more info.
645 * I am not totally sure what I am doing here but I presume that this
646 * should be basically as simple as creating a new stream with the same
647 * parent etc and positioning its seek cursor.
649 static HRESULT WINAPI StgStreamImpl_Clone(
650 IStream* iface,
651 IStream** ppstm) /* [out] */
653 StgStreamImpl* const This=(StgStreamImpl*)iface;
654 HRESULT hres;
655 StgStreamImpl* new_stream;
656 LARGE_INTEGER seek_pos;
658 TRACE("%p %p\n", This, ppstm);
661 * Sanity check
664 if (!This->parentStorage)
665 return STG_E_REVERTED;
667 if ( ppstm == 0 )
668 return STG_E_INVALIDPOINTER;
670 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->dirEntry);
672 if (!new_stream)
673 return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */
675 *ppstm = (IStream*) new_stream;
676 IStream_AddRef(*ppstm);
678 seek_pos.QuadPart = This->currentPosition.QuadPart;
680 hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
682 assert (SUCCEEDED(hres));
684 return S_OK;
688 * Virtual function table for the StgStreamImpl class.
690 static const IStreamVtbl StgStreamImpl_Vtbl =
692 StgStreamImpl_QueryInterface,
693 StgStreamImpl_AddRef,
694 StgStreamImpl_Release,
695 StgStreamImpl_Read,
696 StgStreamImpl_Write,
697 StgStreamImpl_Seek,
698 StgStreamImpl_SetSize,
699 StgStreamImpl_CopyTo,
700 StgStreamImpl_Commit,
701 StgStreamImpl_Revert,
702 StgStreamImpl_LockRegion,
703 StgStreamImpl_UnlockRegion,
704 StgStreamImpl_Stat,
705 StgStreamImpl_Clone
708 /******************************************************************************
709 ** StgStreamImpl implementation
712 /***
713 * This is the constructor for the StgStreamImpl class.
715 * Params:
716 * parentStorage - Pointer to the storage that contains the stream to open
717 * dirEntry - Index of the directory entry that points to this stream.
719 StgStreamImpl* StgStreamImpl_Construct(
720 StorageBaseImpl* parentStorage,
721 DWORD grfMode,
722 DirRef dirEntry)
724 StgStreamImpl* newStream;
726 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
728 if (newStream!=0)
731 * Set-up the virtual function table and reference count.
733 newStream->lpVtbl = &StgStreamImpl_Vtbl;
734 newStream->ref = 0;
736 newStream->parentStorage = parentStorage;
739 * We want to nail-down the reference to the storage in case the
740 * stream out-lives the storage in the client application.
742 * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
744 * No, don't do this. Some apps call IStorage_Release without
745 * calling IStream_Release first. If we grab a reference the
746 * file is not closed, and the app fails when it tries to
747 * reopen the file (Easy-PC, for example)
750 newStream->grfMode = grfMode;
751 newStream->dirEntry = dirEntry;
754 * Start the stream at the beginning.
756 newStream->currentPosition.u.HighPart = 0;
757 newStream->currentPosition.u.LowPart = 0;
759 /* add us to the storage's list of active streams */
760 StorageBaseImpl_AddStream(parentStorage, newStream);
763 return newStream;