stg_stream.c 23.8 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Compound Storage (32 bit version)
 * Stream implementation
 *
 * This file contains the implementation of the stream interface
 * for streams contained in a compound storage.
 *
 * Copyright 1999 Francis Beaudet
 * Copyright 1999 Thuy Nguyen
10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
24
 */
25

26 27
#include <assert.h>
#include <stdlib.h>
28
#include <stdarg.h>
29 30 31
#include <stdio.h>
#include <string.h>

32
#define COBJMACROS
33 34
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
35

36
#include "windef.h"
37
#include "winbase.h"
38
#include "winerror.h"
39
#include "winternl.h"
40
#include "wine/debug.h"
41 42 43

#include "storage32.h"

44
WINE_DEFAULT_DEBUG_CHANNEL(storage);
45

46 47 48 49

/***
 * This is the destructor of the StgStreamImpl class.
 *
50
 * This method will clean-up all the resources used-up by the given StgStreamImpl
51 52 53
 * class. The pointer passed-in to this function will be freed and will not
 * be valid anymore.
 */
54
static void StgStreamImpl_Destroy(StgStreamImpl* This)
55
{
56
  TRACE("(%p)\n", This);
57

58 59
  /*
   * Release the reference we are holding on the parent storage.
60 61 62 63 64 65 66
   * IStorage_Release((IStorage*)This->parentStorage);
   *
   * No, don't do this. Some apps call IStorage_Release without
   * calling IStream_Release first. If we grab a reference the
   * file is not closed, and the app fails when it tries to
   * reopen the file (Easy-PC, for example). Just inform the
   * storage that we have closed the stream
67
   */
68 69 70 71 72 73 74

  if(This->parentStorage) {

    StorageBaseImpl_RemoveStream(This->parentStorage, This);

  }

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  This->parentStorage = 0;

  /*
   * Make sure we clean-up the block chain stream objects that we were using.
   */
  if (This->bigBlockChain != 0)
  {
    BlockChainStream_Destroy(This->bigBlockChain);
    This->bigBlockChain = 0;
  }

  if (This->smallBlockChain != 0)
  {
    SmallBlockChainStream_Destroy(This->smallBlockChain);
    This->smallBlockChain = 0;
  }

  /*
   * Finally, free the memory used-up by the class.
   */
95
  HeapFree(GetProcessHeap(), 0, This);
96 97 98 99 100 101
}

/***
 * This implements the IUnknown method QueryInterface for this
 * class
 */
102
static HRESULT WINAPI StgStreamImpl_QueryInterface(
103
		  IStream*     iface,
104 105
		  REFIID         riid,	      /* [in] */
		  void**         ppvObject)   /* [iid_is][out] */
106
{
107 108
  StgStreamImpl* const This=(StgStreamImpl*)iface;

109 110 111 112 113
  /*
   * Perform a sanity check on the parameters.
   */
  if (ppvObject==0)
    return E_INVALIDARG;
114

115 116 117 118
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
119

120 121 122
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
123 124 125 126 127
  if (IsEqualIID(&IID_IUnknown, riid) ||
      IsEqualIID(&IID_IPersist, riid) ||
      IsEqualIID(&IID_IPersistStream, riid) ||
      IsEqualIID(&IID_ISequentialStream, riid) ||
      IsEqualIID(&IID_IStream, riid))
128
  {
129
    *ppvObject = (IStream*)This;
130
  }
131

132 133 134 135 136
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
    return E_NOINTERFACE;
137

138 139 140 141
  /*
   * Query Interface always increases the reference count by one when it is
   * successful
   */
142
  IStream_AddRef(iface);
143 144

  return S_OK;
145 146 147 148 149 150
}

/***
 * This implements the IUnknown method AddRef for this
 * class
 */
151
static ULONG WINAPI StgStreamImpl_AddRef(
152
		IStream* iface)
153
{
154
  StgStreamImpl* const This=(StgStreamImpl*)iface;
155
  return InterlockedIncrement(&This->ref);
156 157 158 159 160 161
}

/***
 * This implements the IUnknown method Release for this
 * class
 */
162
static ULONG WINAPI StgStreamImpl_Release(
163
		IStream* iface)
164
{
165 166
  StgStreamImpl* const This=(StgStreamImpl*)iface;

167
  ULONG ref;
168

169
  ref = InterlockedDecrement(&This->ref);
170

171 172 173
  /*
   * If the reference count goes down to 0, perform suicide.
   */
174
  if (ref==0)
175 176 177
  {
    StgStreamImpl_Destroy(This);
  }
178

179
  return ref;
180 181 182 183 184 185 186
}

/***
 * This method will open the block chain pointed by the property
 * that describes the stream.
 * If the stream's size is null, no chain is opened.
 */
187
static void StgStreamImpl_OpenBlockChain(
188 189 190
        StgStreamImpl* This)
{
  StgProperty    curProperty;
191
  BOOL         readSuccessful;
192 193

  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
194
   * Make sure no old object is left over.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
   */
  if (This->smallBlockChain != 0)
  {
    SmallBlockChainStream_Destroy(This->smallBlockChain);
    This->smallBlockChain = 0;
  }

  if (This->bigBlockChain != 0)
  {
    BlockChainStream_Destroy(This->bigBlockChain);
    This->bigBlockChain = 0;
  }

  /*
   * Read the information from the property.
   */
211
  readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
212 213
					     This->ownerProperty,
					     &curProperty);
214

215
  if (readSuccessful)
216 217
  {
    This->streamSize = curProperty.size;
218

219 220 221
    /*
     * This code supports only streams that are <32 bits in size.
     */
222
    assert(This->streamSize.u.HighPart == 0);
223

224 225
    if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
    {
226
      assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
227 228 229
    }
    else
    {
230 231
      if ( (This->streamSize.u.HighPart == 0) &&
	   (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
232 233
      {
	This->smallBlockChain = SmallBlockChainStream_Construct(
234
								This->parentStorage->ancestorStorage,
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
								This->ownerProperty);
      }
      else
      {
	This->bigBlockChain = BlockChainStream_Construct(
							 This->parentStorage->ancestorStorage,
							 NULL,
							 This->ownerProperty);
      }
    }
  }
}

/***
 * This method is part of the ISequentialStream interface.
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
251
 * It reads a block of information from the stream at the current
252 253 254 255 256
 * position. It then moves the current position at the end of the
 * read block
 *
 * See the documentation of ISequentialStream for more info.
 */
257
static HRESULT WINAPI StgStreamImpl_Read(
258
		  IStream*     iface,
259
		  void*          pv,        /* [length_is][size_is][out] */
260 261
		  ULONG          cb,        /* [in] */
		  ULONG*         pcbRead)   /* [out] */
262
{
263 264
  StgStreamImpl* const This=(StgStreamImpl*)iface;

265 266
  ULONG bytesReadBuffer;
  ULONG bytesToReadFromBuffer;
267
  HRESULT res;
268

269
  TRACE("(%p, %p, %d, %p)\n",
270 271
	iface, pv, cb, pcbRead);

272
  if (!This->parentStorage)
273 274
  {
    WARN("storage reverted\n");
275
    return STG_E_REVERTED;
276
  }
277

278
  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
279
   * If the caller is not interested in the number of bytes read,
280 281 282 283
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbRead==0)
    pcbRead = &bytesReadBuffer;
284

285 286 287 288
  /*
   * Using the known size of the stream, calculate the number of bytes
   * to read from the block chain
   */
289
  bytesToReadFromBuffer = min( This->streamSize.u.LowPart - This->currentPosition.u.LowPart, cb);
290

291 292
  /*
   * Depending on the type of chain that was opened when the stream was constructed,
Alexandre Julliard's avatar
Alexandre Julliard committed
293
   * we delegate the work to the method that reads the block chains.
294 295 296
   */
  if (This->smallBlockChain!=0)
  {
297
    res = SmallBlockChainStream_ReadAt(This->smallBlockChain,
298 299 300 301
				 This->currentPosition,
				 bytesToReadFromBuffer,
				 pv,
				 pcbRead);
302

303 304 305
  }
  else if (This->bigBlockChain!=0)
  {
306 307 308 309 310
    res = BlockChainStream_ReadAt(This->bigBlockChain,
                 This->currentPosition,
                 bytesToReadFromBuffer,
                 pv,
                 pcbRead);
311 312
  }
  else
313 314 315 316 317 318 319
  {
    /*
     * Small and big block chains are both NULL. This case will happen
     * when a stream starts with BLOCK_END_OF_CHAIN and has size zero.
     */

    *pcbRead = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
320 321
    res = S_OK;
    goto end;
322
  }
323

324
  if (SUCCEEDED(res))
Alexandre Julliard's avatar
Alexandre Julliard committed
325
  {
326 327 328 329 330 331 332 333 334 335
      /*
       * We should always be able to read the proper amount of data from the
       * chain.
       */
      assert(bytesToReadFromBuffer == *pcbRead);

      /*
       * Advance the pointer for the number of positions read.
       */
      This->currentPosition.u.LowPart += *pcbRead;
Alexandre Julliard's avatar
Alexandre Julliard committed
336
  }
337

Alexandre Julliard's avatar
Alexandre Julliard committed
338
end:
339
  TRACE("<-- %08x\n", res);
Alexandre Julliard's avatar
Alexandre Julliard committed
340
  return res;
341
}
342

343 344 345 346 347 348 349 350 351 352
/***
 * This method is part of the ISequentialStream interface.
 *
 * It writes a block of information to the stream at the current
 * position. It then moves the current position at the end of the
 * written block. If the stream is too small to fit the block,
 * the stream is grown to fit.
 *
 * See the documentation of ISequentialStream for more info.
 */
353
static HRESULT WINAPI StgStreamImpl_Write(
354
	          IStream*     iface,
355 356 357
		  const void*    pv,          /* [size_is][in] */
		  ULONG          cb,          /* [in] */
		  ULONG*         pcbWritten)  /* [out] */
358
{
359 360
  StgStreamImpl* const This=(StgStreamImpl*)iface;

361 362
  ULARGE_INTEGER newSize;
  ULONG bytesWritten = 0;
363
  HRESULT res;
364

365
  TRACE("(%p, %p, %d, %p)\n",
366
	iface, pv, cb, pcbWritten);
367

368 369 370
  /*
   * Do we have permission to write to this stream?
   */
371 372 373 374 375 376
  switch(STGM_ACCESS_MODE(This->grfMode))
  {
  case STGM_WRITE:
  case STGM_READWRITE:
      break;
  default:
377
      WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
378 379
      return STG_E_ACCESSDENIED;
  }
380

381 382 383
  if (!pv)
    return STG_E_INVALIDPOINTER;

384
  if (!This->parentStorage)
385 386
  {
    WARN("storage reverted\n");
387
    return STG_E_REVERTED;
388
  }
389
 
390 391 392 393 394 395
  /*
   * If the caller is not interested in the number of bytes written,
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbWritten == 0)
    pcbWritten = &bytesWritten;
396

397 398 399 400 401
  /*
   * Initialize the out parameter
   */
  *pcbWritten = 0;

402 403
  if (cb == 0)
  {
404
    TRACE("<-- S_OK, written 0\n");
405 406 407 408
    return S_OK;
  }
  else
  {
409 410
    newSize.u.HighPart = 0;
    newSize.u.LowPart = This->currentPosition.u.LowPart + cb;
411
  }
412

413 414 415
  /*
   * Verify if we need to grow the stream
   */
416
  if (newSize.u.LowPart > This->streamSize.u.LowPart)
417 418
  {
    /* grow stream */
419 420 421
    res = IStream_SetSize(iface, newSize);
    if (FAILED(res))
      return res;
422
  }
423

424 425 426 427 428 429
  /*
   * Depending on the type of chain that was opened when the stream was constructed,
   * we delegate the work to the method that readwrites to the block chains.
   */
  if (This->smallBlockChain!=0)
  {
430
    res = SmallBlockChainStream_WriteAt(This->smallBlockChain,
431 432 433 434
				  This->currentPosition,
				  cb,
				  pv,
				  pcbWritten);
435

436 437 438
  }
  else if (This->bigBlockChain!=0)
  {
439
    res = BlockChainStream_WriteAt(This->bigBlockChain,
440 441 442 443 444 445
			     This->currentPosition,
			     cb,
			     pv,
			     pcbWritten);
  }
  else
446
  {
447 448
    /* this should never happen because the IStream_SetSize call above will
     * make sure a big or small block chain is created */
449
    assert(FALSE);
450 451
    res = 0;
  }
452

453 454 455
  /*
   * Advance the position pointer for the number of positions written.
   */
456
  This->currentPosition.u.LowPart += *pcbWritten;
457

458
  TRACE("<-- S_OK, written %u\n", *pcbWritten);
459
  return res;
460 461 462 463 464 465 466 467 468
}

/***
 * This method is part of the IStream interface.
 *
 * It will move the current stream pointer according to the parameters
 * given.
 *
 * See the documentation of IStream for more info.
469
 */
470
static HRESULT WINAPI StgStreamImpl_Seek(
471
		  IStream*      iface,
472 473
		  LARGE_INTEGER   dlibMove,         /* [in] */
		  DWORD           dwOrigin,         /* [in] */
474 475
		  ULARGE_INTEGER* plibNewPosition) /* [out] */
{
476 477
  StgStreamImpl* const This=(StgStreamImpl*)iface;

478 479
  ULARGE_INTEGER newPosition;

480
  TRACE("(%p, %d, %d, %p)\n",
481
	iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
482

483 484 485 486
  /*
   * fail if the stream has no parent (as does windows)
   */

487
  if (!This->parentStorage)
488 489
  {
    WARN("storage reverted\n");
490
    return STG_E_REVERTED;
491
  }
492

493
  /*
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
   * The caller is allowed to pass in NULL as the new position return value.
   * If it happens, we assign it to a dynamic variable to avoid special cases
   * in the code below.
   */
  if (plibNewPosition == 0)
  {
    plibNewPosition = &newPosition;
  }

  /*
   * The file pointer is moved depending on the given "function"
   * parameter.
   */
  switch (dwOrigin)
  {
    case STREAM_SEEK_SET:
510 511
      plibNewPosition->u.HighPart = 0;
      plibNewPosition->u.LowPart  = 0;
512 513 514 515 516 517 518 519
      break;
    case STREAM_SEEK_CUR:
      *plibNewPosition = This->currentPosition;
      break;
    case STREAM_SEEK_END:
      *plibNewPosition = This->streamSize;
      break;
    default:
520
      WARN("invalid dwOrigin %d\n", dwOrigin);
521 522 523
      return STG_E_INVALIDFUNCTION;
  }

524
  plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
525 526

  /*
527
   * tell the caller what we calculated
528 529
   */
  This->currentPosition = *plibNewPosition;
530

531 532 533 534 535 536 537 538 539 540 541 542
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
 * It will change the size of a stream.
 *
 * TODO: Switch from small blocks to big blocks and vice versa.
 *
 * See the documentation of IStream for more info.
 */
543
static HRESULT WINAPI StgStreamImpl_SetSize(
544
				     IStream*      iface,
545
				     ULARGE_INTEGER  libNewSize)   /* [in] */
546
{
547 548
  StgStreamImpl* const This=(StgStreamImpl*)iface;

549
  StgProperty    curProperty;
550
  BOOL         Success;
551

552
  TRACE("(%p, %d)\n", iface, libNewSize.u.LowPart);
553

554
  if(!This->parentStorage)
555 556
  {
    WARN("storage reverted\n");
557
    return STG_E_REVERTED;
558
  }
559

560 561 562
  /*
   * As documented.
   */
563
  if (libNewSize.u.HighPart != 0)
564 565
  {
    WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
566
    return STG_E_INVALIDFUNCTION;
567
  }
568 569 570 571 572

  /*
   * Do we have permission?
   */
  if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
573 574
  {
    WARN("access denied\n");
575
    return STG_E_ACCESSDENIED;
576
  }
577

578
  if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
579 580 581 582 583 584 585
    return S_OK;

  /*
   * This will happen if we're creating a stream
   */
  if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
  {
586
    if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
    {
      This->smallBlockChain = SmallBlockChainStream_Construct(
                                    This->parentStorage->ancestorStorage,
                                    This->ownerProperty);
    }
    else
    {
      This->bigBlockChain = BlockChainStream_Construct(
                                This->parentStorage->ancestorStorage,
                                NULL,
                                This->ownerProperty);
    }
  }

  /*
   * Read this stream's property to see if it's small blocks or big blocks
   */
604
  Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
605
                                       This->ownerProperty,
606
                                       &curProperty);
607
  /*
608
   * Determine if we have to switch from small to big blocks or vice versa
609 610
   */
  if ( (This->smallBlockChain!=0) &&
611
       (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
612
  {
613
    if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
614 615 616 617 618 619 620 621 622 623
    {
      /*
       * Transform the small block chain into a big block chain
       */
      This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
                                This->parentStorage->ancestorStorage,
                                &This->smallBlockChain);
    }
  }

624 625 626 627 628 629 630 631 632 633
  if (This->smallBlockChain!=0)
  {
    Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
  }
  else
  {
    Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
  }

  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
634
   * Write the new information about this stream to the property
635
   */
636
  Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
637 638 639
                                       This->ownerProperty,
                                       &curProperty);

640 641
  curProperty.size.u.HighPart = libNewSize.u.HighPart;
  curProperty.size.u.LowPart = libNewSize.u.LowPart;
642

643 644
  if (Success)
  {
645
    StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
646 647 648
				This->ownerProperty,
				&curProperty);
  }
649

650
  This->streamSize = libNewSize;
651

652 653
  return S_OK;
}
654

655 656 657 658 659 660 661
/***
 * This method is part of the IStream interface.
 *
 * It will copy the 'cb' Bytes to 'pstm' IStream.
 *
 * See the documentation of IStream for more info.
 */
662
static HRESULT WINAPI StgStreamImpl_CopyTo(
663
				    IStream*      iface,
664 665 666 667
				    IStream*      pstm,         /* [unique][in] */
				    ULARGE_INTEGER  cb,           /* [in] */
				    ULARGE_INTEGER* pcbRead,      /* [out] */
				    ULARGE_INTEGER* pcbWritten)   /* [out] */
668
{
669
  StgStreamImpl* const This=(StgStreamImpl*)iface;
670 671 672 673 674 675
  HRESULT        hr = S_OK;
  BYTE           tmpBuffer[128];
  ULONG          bytesRead, bytesWritten, copySize;
  ULARGE_INTEGER totalBytesRead;
  ULARGE_INTEGER totalBytesWritten;

676
  TRACE("(%p, %p, %d, %p, %p)\n",
677
	iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
678

679 680 681
  /*
   * Sanity check
   */
682

683
  if (!This->parentStorage)
684 685
  {
    WARN("storage reverted\n");
686
    return STG_E_REVERTED;
687
  }
688

689 690 691
  if ( pstm == 0 )
    return STG_E_INVALIDPOINTER;

692 693
  totalBytesRead.QuadPart = 0;
  totalBytesWritten.QuadPart = 0;
694

695
  while ( cb.QuadPart > 0 )
696
  {
697 698
    if ( cb.QuadPart >= sizeof(tmpBuffer) )
      copySize = sizeof(tmpBuffer);
699
    else
700
      copySize = cb.u.LowPart;
701

702
    IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
703

704
    totalBytesRead.QuadPart += bytesRead;
705

706
    IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
707

708
    totalBytesWritten.QuadPart += bytesWritten;
709 710

    /*
Alexandre Julliard's avatar
Alexandre Julliard committed
711
     * Check that read & write operations were successful
712
     */
713
    if (bytesRead != bytesWritten)
714 715
    {
      hr = STG_E_MEDIUMFULL;
716
      WARN("medium full\n");
717 718
      break;
    }
719

720
    if (bytesRead!=copySize)
721
      cb.QuadPart = 0;
722
    else
723
      cb.QuadPart -= bytesRead;
724 725
  }

726 727
  if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
  if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
728 729

  return hr;
730 731 732 733 734 735 736 737 738
}

/***
 * This method is part of the IStream interface.
 *
 * For streams contained in structured storages, this method
 * does nothing. This is what the documentation tells us.
 *
 * See the documentation of IStream for more info.
739
 */
740
static HRESULT WINAPI StgStreamImpl_Commit(
741
		  IStream*      iface,
742
		  DWORD           grfCommitFlags)  /* [in] */
743
{
744 745
  StgStreamImpl* const This=(StgStreamImpl*)iface;

746
  if (!This->parentStorage)
747 748
  {
    WARN("storage reverted\n");
749
    return STG_E_REVERTED;
750
  }
751

752 753 754 755 756 757 758 759 760 761
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
 * For streams contained in structured storages, this method
 * does nothing. This is what the documentation tells us.
 *
 * See the documentation of IStream for more info.
762
 */
763
static HRESULT WINAPI StgStreamImpl_Revert(
764
		  IStream* iface)
765 766 767 768
{
  return S_OK;
}

769
static HRESULT WINAPI StgStreamImpl_LockRegion(
770
					IStream*     iface,
771 772 773
					ULARGE_INTEGER libOffset,   /* [in] */
					ULARGE_INTEGER cb,          /* [in] */
					DWORD          dwLockType)  /* [in] */
774
{
775 776
  StgStreamImpl* const This=(StgStreamImpl*)iface;

777
  if (!This->parentStorage)
778 779
  {
    WARN("storage reverted\n");
780
    return STG_E_REVERTED;
781
  }
782

783
  FIXME("not implemented!\n");
784 785 786
  return E_NOTIMPL;
}

787
static HRESULT WINAPI StgStreamImpl_UnlockRegion(
788
					  IStream*     iface,
789 790 791
					  ULARGE_INTEGER libOffset,   /* [in] */
					  ULARGE_INTEGER cb,          /* [in] */
					  DWORD          dwLockType)  /* [in] */
792
{
793 794
  StgStreamImpl* const This=(StgStreamImpl*)iface;

795
  if (!This->parentStorage)
796 797
  {
    WARN("storage reverted\n");
798
    return STG_E_REVERTED;
799
  }
800

801
  FIXME("not implemented!\n");
802 803 804 805 806 807 808 809 810 811
  return E_NOTIMPL;
}

/***
 * This method is part of the IStream interface.
 *
 * This method returns information about the current
 * stream.
 *
 * See the documentation of IStream for more info.
812
 */
813
static HRESULT WINAPI StgStreamImpl_Stat(
814
		  IStream*     iface,
815
		  STATSTG*       pstatstg,     /* [out] */
816
		  DWORD          grfStatFlag)  /* [in] */
817
{
818 819
  StgStreamImpl* const This=(StgStreamImpl*)iface;

820
  StgProperty    curProperty;
821
  BOOL         readSuccessful;
822

823
  TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
824

825 826 827 828
  /*
   * if stream has no parent, return STG_E_REVERTED
   */

829
  if (!This->parentStorage)
830 831
  {
    WARN("storage reverted\n");
832
    return STG_E_REVERTED;
833
  }
834

835 836 837
  /*
   * Read the information from the property.
   */
838
  readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
839 840
					     This->ownerProperty,
					     &curProperty);
841

842
  if (readSuccessful)
843
  {
844 845
    StorageUtl_CopyPropertyToSTATSTG(pstatstg,
				     &curProperty,
846
				     grfStatFlag);
847 848

    pstatstg->grfMode = This->grfMode;
849

850 851
    return S_OK;
  }
852

853
  WARN("failed to read properties\n");
854 855
  return E_FAIL;
}
856

857 858 859 860 861 862 863 864 865 866 867
/***
 * This method is part of the IStream interface.
 *
 * This method returns a clone of the interface that allows for
 * another seek pointer
 *
 * See the documentation of IStream for more info.
 *
 * I am not totally sure what I am doing here but I presume that this
 * should be basically as simple as creating a new stream with the same
 * parent etc and positioning its seek cursor.
868
 */
869
static HRESULT WINAPI StgStreamImpl_Clone(
870
				   IStream*     iface,
871
				   IStream**    ppstm) /* [out] */
872
{
873 874 875 876 877
  StgStreamImpl* const This=(StgStreamImpl*)iface;
  HRESULT hres;
  StgStreamImpl* new_stream;
  LARGE_INTEGER seek_pos;

878 879
  TRACE("%p %p\n", This, ppstm);

880 881 882
  /*
   * Sanity check
   */
883

884
  if (!This->parentStorage)
885 886
    return STG_E_REVERTED;

887 888 889 890 891 892 893 894 895
  if ( ppstm == 0 )
    return STG_E_INVALIDPOINTER;

  new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty);

  if (!new_stream)
    return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */

  *ppstm = (IStream*) new_stream;
896 897
  IStream_AddRef(*ppstm);

898
  seek_pos.QuadPart = This->currentPosition.QuadPart;
899

900
  hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
901

902 903 904
  assert (SUCCEEDED(hres));

  return S_OK;
905
}
906 907 908 909

/*
 * Virtual function table for the StgStreamImpl class.
 */
910
static const IStreamVtbl StgStreamImpl_Vtbl =
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
{
    StgStreamImpl_QueryInterface,
    StgStreamImpl_AddRef,
    StgStreamImpl_Release,
    StgStreamImpl_Read,
    StgStreamImpl_Write,
    StgStreamImpl_Seek,
    StgStreamImpl_SetSize,
    StgStreamImpl_CopyTo,
    StgStreamImpl_Commit,
    StgStreamImpl_Revert,
    StgStreamImpl_LockRegion,
    StgStreamImpl_UnlockRegion,
    StgStreamImpl_Stat,
    StgStreamImpl_Clone
};

/******************************************************************************
** StgStreamImpl implementation
*/

/***
 * This is the constructor for the StgStreamImpl class.
 *
 * Params:
 *    parentStorage - Pointer to the storage that contains the stream to open
 *    ownerProperty - Index of the property that points to this stream.
 */
StgStreamImpl* StgStreamImpl_Construct(
		StorageBaseImpl* parentStorage,
    DWORD            grfMode,
    ULONG            ownerProperty)
{
  StgStreamImpl* newStream;

  newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl));

  if (newStream!=0)
  {
    /*
     * Set-up the virtual function table and reference count.
     */
    newStream->lpVtbl    = &StgStreamImpl_Vtbl;
    newStream->ref       = 0;

956 957
    newStream->parentStorage = parentStorage;

958 959 960
    /*
     * We want to nail-down the reference to the storage in case the
     * stream out-lives the storage in the client application.
961 962 963 964 965 966 967
     *
     * -- IStorage_AddRef((IStorage*)newStream->parentStorage);
     *
     * No, don't do this. Some apps call IStorage_Release without
     * calling IStream_Release first. If we grab a reference the
     * file is not closed, and the app fails when it tries to
     * reopen the file (Easy-PC, for example)
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
     */

    newStream->grfMode = grfMode;
    newStream->ownerProperty = ownerProperty;

    /*
     * Start the stream at the beginning.
     */
    newStream->currentPosition.u.HighPart = 0;
    newStream->currentPosition.u.LowPart = 0;

    /*
     * Initialize the rest of the data.
     */
    newStream->streamSize.u.HighPart = 0;
    newStream->streamSize.u.LowPart  = 0;
    newStream->bigBlockChain       = 0;
    newStream->smallBlockChain     = 0;

    /*
     * Read the size from the property and determine if the blocks forming
     * this stream are large or small.
     */
    StgStreamImpl_OpenBlockChain(newStream);
992 993 994

    /* add us to the storage's list of active streams */
    StorageBaseImpl_AddStream(parentStorage, newStream);
995 996 997 998
  }

  return newStream;
}