qmgr: Use proper method macros.
[wine.git] / dlls / ole32 / stg_stream.c
blobe9fc0c0b76e22c021463516fe824c5e1af4577c7
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(&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);
80 /***
81 * This implements the IUnknown method QueryInterface for this
82 * class
84 static HRESULT WINAPI StgStreamImpl_QueryInterface(
85 IStream* iface,
86 REFIID riid, /* [in] */
87 void** ppvObject) /* [iid_is][out] */
89 StgStreamImpl* This = impl_from_IStream(iface);
91 if (ppvObject==0)
92 return E_INVALIDARG;
94 *ppvObject = 0;
96 if (IsEqualIID(&IID_IUnknown, riid) ||
97 IsEqualIID(&IID_ISequentialStream, riid) ||
98 IsEqualIID(&IID_IStream, riid))
100 *ppvObject = &This->IStream_iface;
102 else
103 return E_NOINTERFACE;
105 IStream_AddRef(iface);
107 return S_OK;
110 /***
111 * This implements the IUnknown method AddRef for this
112 * class
114 static ULONG WINAPI StgStreamImpl_AddRef(
115 IStream* iface)
117 StgStreamImpl* This = impl_from_IStream(iface);
118 return InterlockedIncrement(&This->ref);
121 /***
122 * This implements the IUnknown method Release for this
123 * class
125 static ULONG WINAPI StgStreamImpl_Release(
126 IStream* iface)
128 StgStreamImpl* This = impl_from_IStream(iface);
130 ULONG ref;
132 ref = InterlockedDecrement(&This->ref);
135 * If the reference count goes down to 0, perform suicide.
137 if (ref==0)
139 StgStreamImpl_Destroy(This);
142 return ref;
145 /***
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
150 * read block
152 * See the documentation of ISequentialStream for more info.
154 static HRESULT WINAPI StgStreamImpl_Read(
155 IStream* iface,
156 void* pv, /* [length_is][size_is][out] */
157 ULONG cb, /* [in] */
158 ULONG* pcbRead) /* [out] */
160 StgStreamImpl* This = impl_from_IStream(iface);
162 ULONG bytesReadBuffer;
163 HRESULT res;
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.
178 if (pcbRead==0)
179 pcbRead = &bytesReadBuffer;
181 res = StorageBaseImpl_StreamReadAt(This->parentStorage,
182 This->dirEntry,
183 This->currentPosition,
186 pcbRead);
188 if (SUCCEEDED(res))
191 * Advance the pointer for the number of positions read.
193 This->currentPosition.u.LowPart += *pcbRead;
196 TRACE("<-- %08x\n", res);
197 return res;
200 /***
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(
211 IStream* iface,
212 const void* pv, /* [size_is][in] */
213 ULONG cb, /* [in] */
214 ULONG* pcbWritten) /* [out] */
216 StgStreamImpl* This = impl_from_IStream(iface);
218 ULONG bytesWritten = 0;
219 HRESULT res;
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))
229 case STGM_WRITE:
230 case STGM_READWRITE:
231 break;
232 default:
233 WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
234 return STG_E_ACCESSDENIED;
237 if (!pv)
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.
250 if (pcbWritten == 0)
251 pcbWritten = &bytesWritten;
254 * Initialize the out parameter
256 *pcbWritten = 0;
258 if (cb == 0)
260 TRACE("<-- S_OK, written 0\n");
261 return S_OK;
264 res = StorageBaseImpl_StreamWriteAt(This->parentStorage,
265 This->dirEntry,
266 This->currentPosition,
269 pcbWritten);
272 * Advance the position pointer for the number of positions written.
274 This->currentPosition.u.LowPart += *pcbWritten;
276 if (SUCCEEDED(res))
277 res = StorageBaseImpl_Flush(This->parentStorage);
279 TRACE("<-- S_OK, written %u\n", *pcbWritten);
280 return res;
283 /***
284 * This method is part of the IStream interface.
286 * It will move the current stream pointer according to the parameters
287 * given.
289 * See the documentation of IStream for more info.
291 static HRESULT WINAPI StgStreamImpl_Seek(
292 IStream* iface,
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;
301 HRESULT hr;
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
319 * in the code below.
321 if (plibNewPosition == 0)
323 plibNewPosition = &newPosition;
327 * The file pointer is moved depending on the given "function"
328 * parameter.
330 switch (dwOrigin)
332 case STREAM_SEEK_SET:
333 plibNewPosition->u.HighPart = 0;
334 plibNewPosition->u.LowPart = 0;
335 break;
336 case STREAM_SEEK_CUR:
337 *plibNewPosition = This->currentPosition;
338 break;
339 case STREAM_SEEK_END:
340 hr = StorageBaseImpl_ReadDirEntry(This->parentStorage, This->dirEntry, &currentEntry);
341 if (FAILED(hr)) return hr;
342 *plibNewPosition = currentEntry.size;
343 break;
344 default:
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;
356 return S_OK;
359 /***
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(
367 IStream* iface,
368 ULARGE_INTEGER libNewSize) /* [in] */
370 StgStreamImpl* This = impl_from_IStream(iface);
372 HRESULT hr;
374 TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
376 if(!This->parentStorage)
378 WARN("storage reverted\n");
379 return STG_E_REVERTED;
383 * As documented.
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);
402 if (SUCCEEDED(hr))
403 hr = StorageBaseImpl_Flush(This->parentStorage);
405 return hr;
408 /***
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(
416 IStream* iface,
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);
423 HRESULT hr = S_OK;
424 BYTE tmpBuffer[128];
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);
433 * Sanity check
436 if (!This->parentStorage)
438 WARN("storage reverted\n");
439 return STG_E_REVERTED;
442 if ( pstm == 0 )
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);
452 else
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");
470 break;
473 if (bytesRead!=copySize)
474 cb.QuadPart = 0;
475 else
476 cb.QuadPart -= bytesRead;
479 if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
480 if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
482 return hr;
485 /***
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(
494 IStream* iface,
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);
508 /***
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(
517 IStream* iface)
519 return S_OK;
522 static HRESULT WINAPI StgStreamImpl_LockRegion(
523 IStream* iface,
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");
537 return E_NOTIMPL;
540 static HRESULT WINAPI StgStreamImpl_UnlockRegion(
541 IStream* iface,
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");
555 return E_NOTIMPL;
558 /***
559 * This method is part of the IStream interface.
561 * This method returns information about the current
562 * stream.
564 * See the documentation of IStream for more info.
566 static HRESULT WINAPI StgStreamImpl_Stat(
567 IStream* iface,
568 STATSTG* pstatstg, /* [out] */
569 DWORD grfStatFlag) /* [in] */
571 StgStreamImpl* This = impl_from_IStream(iface);
573 DirEntry currentEntry;
574 HRESULT hr;
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,
592 This->dirEntry,
593 &currentEntry);
595 if (SUCCEEDED(hr))
597 StorageUtl_CopyDirEntryToSTATSTG(This->parentStorage,
598 pstatstg,
599 &currentEntry,
600 grfStatFlag);
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;
608 return S_OK;
611 WARN("failed to read entry\n");
612 return hr;
615 /***
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(
628 IStream* iface,
629 IStream** ppstm) /* [out] */
631 StgStreamImpl* This = impl_from_IStream(iface);
632 HRESULT hres;
633 StgStreamImpl* new_stream;
634 LARGE_INTEGER seek_pos;
636 TRACE("%p %p\n", This, ppstm);
639 * Sanity check
642 if (!This->parentStorage)
643 return STG_E_REVERTED;
645 if ( ppstm == 0 )
646 return STG_E_INVALIDPOINTER;
648 new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->dirEntry);
650 if (!new_stream)
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));
662 return S_OK;
666 * Virtual function table for the StgStreamImpl class.
668 static const IStreamVtbl StgStreamVtbl =
670 StgStreamImpl_QueryInterface,
671 StgStreamImpl_AddRef,
672 StgStreamImpl_Release,
673 StgStreamImpl_Read,
674 StgStreamImpl_Write,
675 StgStreamImpl_Seek,
676 StgStreamImpl_SetSize,
677 StgStreamImpl_CopyTo,
678 StgStreamImpl_Commit,
679 StgStreamImpl_Revert,
680 StgStreamImpl_LockRegion,
681 StgStreamImpl_UnlockRegion,
682 StgStreamImpl_Stat,
683 StgStreamImpl_Clone
686 /******************************************************************************
687 ** StgStreamImpl implementation
690 /***
691 * This is the constructor for the StgStreamImpl class.
693 * Params:
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,
699 DWORD grfMode,
700 DirRef dirEntry)
702 StgStreamImpl* newStream;
704 newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));
706 if (newStream)
709 * Set-up the virtual function table and reference count.
711 newStream->IStream_iface.lpVtbl = &StgStreamVtbl;
712 newStream->ref = 0;
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);
741 return newStream;