stg_stream.c 23.5 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 "winreg.h"
40
#include "winternl.h"
41
#include "wine/debug.h"
42 43 44

#include "storage32.h"

45
WINE_DEFAULT_DEBUG_CHANNEL(storage);
46

47 48 49 50

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

59 60
  /*
   * Release the reference we are holding on the parent storage.
61 62 63 64 65 66 67
   * 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
68
   */
69 70 71 72 73 74 75

  if(This->parentStorage) {

    StorageBaseImpl_RemoveStream(This->parentStorage, This);

  }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  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.
   */
96
  HeapFree(GetProcessHeap(), 0, This);
97 98 99 100 101 102
}

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

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

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

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

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

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

  return S_OK;
144 145 146 147 148 149
}

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

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

166
  ULONG ref;
167

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

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

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

/***
 * 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.
 */
186
static void StgStreamImpl_OpenBlockChain(
187 188 189
        StgStreamImpl* This)
{
  StgProperty    curProperty;
190
  BOOL         readSuccessful;
191 192

  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
193
   * Make sure no old object is left over.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
   */
  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.
   */
210
  readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
211 212
					     This->ownerProperty,
					     &curProperty);
213

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

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

223 224
    if(curProperty.startingBlock == BLOCK_END_OF_CHAIN)
    {
225
      assert( (This->streamSize.u.HighPart == 0) && (This->streamSize.u.LowPart == 0) );
226 227 228
    }
    else
    {
229 230
      if ( (This->streamSize.u.HighPart == 0) &&
	   (This->streamSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
231 232
      {
	This->smallBlockChain = SmallBlockChainStream_Construct(
233
								This->parentStorage->ancestorStorage,
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
								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
250
 * It reads a block of information from the stream at the current
251 252 253 254 255
 * position. It then moves the current position at the end of the
 * read block
 *
 * See the documentation of ISequentialStream for more info.
 */
256
static HRESULT WINAPI StgStreamImpl_Read(
257
		  IStream*     iface,
258
		  void*          pv,        /* [length_is][size_is][out] */
259 260
		  ULONG          cb,        /* [in] */
		  ULONG*         pcbRead)   /* [out] */
261
{
262 263
  StgStreamImpl* const This=(StgStreamImpl*)iface;

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

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

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

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

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

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

302 303 304
  }
  else if (This->bigBlockChain!=0)
  {
305 306 307 308 309
    res = BlockChainStream_ReadAt(This->bigBlockChain,
                 This->currentPosition,
                 bytesToReadFromBuffer,
                 pv,
                 pcbRead);
310 311
  }
  else
312 313 314 315 316 317 318
  {
    /*
     * 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
319 320
    res = S_OK;
    goto end;
321
  }
322

323
  if (SUCCEEDED(res))
Alexandre Julliard's avatar
Alexandre Julliard committed
324
  {
325 326 327 328 329 330 331 332 333 334
      /*
       * 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
335
  }
336

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

342 343 344 345 346 347 348 349 350 351
/***
 * 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.
 */
352
static HRESULT WINAPI StgStreamImpl_Write(
353
	          IStream*     iface,
354 355 356
		  const void*    pv,          /* [size_is][in] */
		  ULONG          cb,          /* [in] */
		  ULONG*         pcbWritten)  /* [out] */
357
{
358 359
  StgStreamImpl* const This=(StgStreamImpl*)iface;

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

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

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

380 381 382
  if (!pv)
    return STG_E_INVALIDPOINTER;

383
  if (!This->parentStorage)
384 385
  {
    WARN("storage reverted\n");
386
    return STG_E_REVERTED;
387
  }
388
 
389 390 391 392 393 394
  /*
   * 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;
395

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

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

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

423 424 425 426 427 428
  /*
   * 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)
  {
429
    res = SmallBlockChainStream_WriteAt(This->smallBlockChain,
430 431 432 433
				  This->currentPosition,
				  cb,
				  pv,
				  pcbWritten);
434

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

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

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

/***
 * 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.
468
 */
469
static HRESULT WINAPI StgStreamImpl_Seek(
470
		  IStream*      iface,
471 472
		  LARGE_INTEGER   dlibMove,         /* [in] */
		  DWORD           dwOrigin,         /* [in] */
473 474
		  ULARGE_INTEGER* plibNewPosition) /* [out] */
{
475 476
  StgStreamImpl* const This=(StgStreamImpl*)iface;

477 478
  ULARGE_INTEGER newPosition;

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

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

486
  if (!This->parentStorage)
487 488
    return STG_E_REVERTED;

489
  /*
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
   * 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:
506 507
      plibNewPosition->u.HighPart = 0;
      plibNewPosition->u.LowPart  = 0;
508 509 510 511 512 513 514 515 516 517 518
      break;
    case STREAM_SEEK_CUR:
      *plibNewPosition = This->currentPosition;
      break;
    case STREAM_SEEK_END:
      *plibNewPosition = This->streamSize;
      break;
    default:
      return STG_E_INVALIDFUNCTION;
  }

519
  plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart );
520 521

  /*
522
   * tell the caller what we calculated
523 524
   */
  This->currentPosition = *plibNewPosition;
525

526 527 528 529 530 531 532 533 534 535 536 537
  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.
 */
538
static HRESULT WINAPI StgStreamImpl_SetSize(
539
				     IStream*      iface,
540
				     ULARGE_INTEGER  libNewSize)   /* [in] */
541
{
542 543
  StgStreamImpl* const This=(StgStreamImpl*)iface;

544
  StgProperty    curProperty;
545
  BOOL         Success;
546

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

549
  if(!This->parentStorage)
550 551
    return STG_E_REVERTED;

552 553 554
  /*
   * As documented.
   */
555
  if (libNewSize.u.HighPart != 0)
556
    return STG_E_INVALIDFUNCTION;
557 558 559 560 561 562 563

  /*
   * Do we have permission?
   */
  if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
    return STG_E_ACCESSDENIED;

564
  if (This->streamSize.u.LowPart == libNewSize.u.LowPart)
565 566 567 568 569 570 571
    return S_OK;

  /*
   * This will happen if we're creating a stream
   */
  if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0))
  {
572
    if (libNewSize.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK)
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
    {
      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
   */
590
  Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
591
                                       This->ownerProperty,
592
                                       &curProperty);
593
  /*
594
   * Determine if we have to switch from small to big blocks or vice versa
595 596
   */
  if ( (This->smallBlockChain!=0) &&
597
       (curProperty.size.u.LowPart < LIMIT_TO_USE_SMALL_BLOCK) )
598
  {
599
    if (libNewSize.u.LowPart >= LIMIT_TO_USE_SMALL_BLOCK)
600 601 602 603 604 605 606 607 608 609
    {
      /*
       * Transform the small block chain into a big block chain
       */
      This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks(
                                This->parentStorage->ancestorStorage,
                                &This->smallBlockChain);
    }
  }

610 611 612 613 614 615 616 617 618 619
  if (This->smallBlockChain!=0)
  {
    Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize);
  }
  else
  {
    Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize);
  }

  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
620
   * Write the new information about this stream to the property
621
   */
622
  Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
623 624 625
                                       This->ownerProperty,
                                       &curProperty);

626 627
  curProperty.size.u.HighPart = libNewSize.u.HighPart;
  curProperty.size.u.LowPart = libNewSize.u.LowPart;
628

629 630
  if (Success)
  {
631
    StorageImpl_WriteProperty(This->parentStorage->ancestorStorage,
632 633 634
				This->ownerProperty,
				&curProperty);
  }
635

636
  This->streamSize = libNewSize;
637

638 639
  return S_OK;
}
640

641 642 643 644 645 646 647
/***
 * 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.
 */
648
static HRESULT WINAPI StgStreamImpl_CopyTo(
649
				    IStream*      iface,
650 651 652 653
				    IStream*      pstm,         /* [unique][in] */
				    ULARGE_INTEGER  cb,           /* [in] */
				    ULARGE_INTEGER* pcbRead,      /* [out] */
				    ULARGE_INTEGER* pcbWritten)   /* [out] */
654
{
655
  StgStreamImpl* const This=(StgStreamImpl*)iface;
656 657 658 659 660 661
  HRESULT        hr = S_OK;
  BYTE           tmpBuffer[128];
  ULONG          bytesRead, bytesWritten, copySize;
  ULARGE_INTEGER totalBytesRead;
  ULARGE_INTEGER totalBytesWritten;

662
  TRACE("(%p, %p, %d, %p, %p)\n",
663
	iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
664

665 666 667
  /*
   * Sanity check
   */
668

669
  if (!This->parentStorage)
670 671
    return STG_E_REVERTED;

672 673 674
  if ( pstm == 0 )
    return STG_E_INVALIDPOINTER;

675 676
  totalBytesRead.u.LowPart = totalBytesRead.u.HighPart = 0;
  totalBytesWritten.u.LowPart = totalBytesWritten.u.HighPart = 0;
677 678

  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
679 680
   * use stack to store data temporarily
   * there is surely a more performant way of doing it, for now this basic
681 682
   * implementation will do the job
   */
683
  while ( cb.u.LowPart > 0 )
684
  {
685
    if ( cb.u.LowPart >= 128 )
686 687
      copySize = 128;
    else
688
      copySize = cb.u.LowPart;
689

690
    IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
691

692
    totalBytesRead.u.LowPart += bytesRead;
693

694
    IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
695

696
    totalBytesWritten.u.LowPart += bytesWritten;
697 698

    /*
Alexandre Julliard's avatar
Alexandre Julliard committed
699
     * Check that read & write operations were successful
700
     */
701
    if (bytesRead != bytesWritten)
702 703 704 705
    {
      hr = STG_E_MEDIUMFULL;
      break;
    }
706

707
    if (bytesRead!=copySize)
708
      cb.u.LowPart = 0;
709
    else
710
      cb.u.LowPart -= bytesRead;
711 712 713 714 715 716 717
  }

  /*
   * Update number of bytes read and written
   */
  if (pcbRead)
  {
718 719
    pcbRead->u.LowPart = totalBytesRead.u.LowPart;
    pcbRead->u.HighPart = totalBytesRead.u.HighPart;
720 721 722 723
  }

  if (pcbWritten)
  {
724 725
    pcbWritten->u.LowPart = totalBytesWritten.u.LowPart;
    pcbWritten->u.HighPart = totalBytesWritten.u.HighPart;
726 727
  }
  return hr;
728 729 730 731 732 733 734 735 736
}

/***
 * 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.
737
 */
738
static HRESULT WINAPI StgStreamImpl_Commit(
739
		  IStream*      iface,
740
		  DWORD           grfCommitFlags)  /* [in] */
741
{
742 743
  StgStreamImpl* const This=(StgStreamImpl*)iface;

744
  if (!This->parentStorage)
745
    return STG_E_REVERTED;
746

747 748 749 750 751 752 753 754 755 756
  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.
757
 */
758
static HRESULT WINAPI StgStreamImpl_Revert(
759
		  IStream* iface)
760 761 762 763
{
  return S_OK;
}

764
static HRESULT WINAPI StgStreamImpl_LockRegion(
765
					IStream*     iface,
766 767 768
					ULARGE_INTEGER libOffset,   /* [in] */
					ULARGE_INTEGER cb,          /* [in] */
					DWORD          dwLockType)  /* [in] */
769
{
770 771
  StgStreamImpl* const This=(StgStreamImpl*)iface;

772
  if (!This->parentStorage)
773 774
    return STG_E_REVERTED;

775
  FIXME("not implemented!\n");
776 777 778
  return E_NOTIMPL;
}

779
static HRESULT WINAPI StgStreamImpl_UnlockRegion(
780
					  IStream*     iface,
781 782 783
					  ULARGE_INTEGER libOffset,   /* [in] */
					  ULARGE_INTEGER cb,          /* [in] */
					  DWORD          dwLockType)  /* [in] */
784
{
785 786
  StgStreamImpl* const This=(StgStreamImpl*)iface;

787
  if (!This->parentStorage)
788 789
    return STG_E_REVERTED;

790
  FIXME("not implemented!\n");
791 792 793 794 795 796 797 798 799 800
  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.
801
 */
802
static HRESULT WINAPI StgStreamImpl_Stat(
803
		  IStream*     iface,
804
		  STATSTG*       pstatstg,     /* [out] */
805
		  DWORD          grfStatFlag)  /* [in] */
806
{
807 808
  StgStreamImpl* const This=(StgStreamImpl*)iface;

809
  StgProperty    curProperty;
810
  BOOL         readSuccessful;
811

812
  TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
813

814 815 816 817
  /*
   * if stream has no parent, return STG_E_REVERTED
   */

818 819
  if (!This->parentStorage)
    return STG_E_REVERTED;
820

821 822 823
  /*
   * Read the information from the property.
   */
824
  readSuccessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage,
825 826
					     This->ownerProperty,
					     &curProperty);
827

828
  if (readSuccessful)
829
  {
830 831
    StorageUtl_CopyPropertyToSTATSTG(pstatstg,
				     &curProperty,
832
				     grfStatFlag);
833 834

    pstatstg->grfMode = This->grfMode;
835

836 837
    return S_OK;
  }
838

839 840
  return E_FAIL;
}
841

842 843 844 845 846 847 848 849 850 851 852
/***
 * 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.
853
 */
854
static HRESULT WINAPI StgStreamImpl_Clone(
855
				   IStream*     iface,
856
				   IStream**    ppstm) /* [out] */
857
{
858 859 860 861 862
  StgStreamImpl* const This=(StgStreamImpl*)iface;
  HRESULT hres;
  StgStreamImpl* new_stream;
  LARGE_INTEGER seek_pos;

863 864
  TRACE("%p %p\n", This, ppstm);

865 866 867
  /*
   * Sanity check
   */
868

869
  if (!This->parentStorage)
870 871
    return STG_E_REVERTED;

872 873 874 875 876 877 878 879 880 881
  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;
  seek_pos.QuadPart = This->currentPosition.QuadPart;
882

883
  hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
884

885 886 887
  assert (SUCCEEDED(hres));

  return S_OK;
888
}
889 890 891 892

/*
 * Virtual function table for the StgStreamImpl class.
 */
893
static const IStreamVtbl StgStreamImpl_Vtbl =
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 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
{
    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;

939 940
    newStream->parentStorage = parentStorage;

941 942 943
    /*
     * We want to nail-down the reference to the storage in case the
     * stream out-lives the storage in the client application.
944 945 946 947 948 949 950
     *
     * -- 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)
951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978
     */

    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);
  }

  return newStream;
}