stg_stream.c 18.1 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
  This->parentStorage = 0;

  /*
   * Finally, free the memory used-up by the class.
   */
80
  HeapFree(GetProcessHeap(), 0, This);
81 82 83 84 85 86
}

/***
 * This implements the IUnknown method QueryInterface for this
 * class
 */
87
static HRESULT WINAPI StgStreamImpl_QueryInterface(
88
		  IStream*     iface,
89 90
		  REFIID         riid,	      /* [in] */
		  void**         ppvObject)   /* [iid_is][out] */
91
{
92 93
  StgStreamImpl* const This=(StgStreamImpl*)iface;

94 95 96 97 98
  /*
   * Perform a sanity check on the parameters.
   */
  if (ppvObject==0)
    return E_INVALIDARG;
99

100 101 102 103
  /*
   * Initialize the return parameter.
   */
  *ppvObject = 0;
104

105 106 107
  /*
   * Compare the riid with the interface IDs implemented by this object.
   */
108 109 110 111 112
  if (IsEqualIID(&IID_IUnknown, riid) ||
      IsEqualIID(&IID_IPersist, riid) ||
      IsEqualIID(&IID_IPersistStream, riid) ||
      IsEqualIID(&IID_ISequentialStream, riid) ||
      IsEqualIID(&IID_IStream, riid))
113
  {
114
    *ppvObject = This;
115
  }
116

117 118 119 120 121
  /*
   * Check that we obtained an interface.
   */
  if ((*ppvObject)==0)
    return E_NOINTERFACE;
122

123 124 125 126
  /*
   * Query Interface always increases the reference count by one when it is
   * successful
   */
127
  IStream_AddRef(iface);
128 129

  return S_OK;
130 131 132 133 134 135
}

/***
 * This implements the IUnknown method AddRef for this
 * class
 */
136
static ULONG WINAPI StgStreamImpl_AddRef(
137
		IStream* iface)
138
{
139
  StgStreamImpl* const This=(StgStreamImpl*)iface;
140
  return InterlockedIncrement(&This->ref);
141 142 143 144 145 146
}

/***
 * This implements the IUnknown method Release for this
 * class
 */
147
static ULONG WINAPI StgStreamImpl_Release(
148
		IStream* iface)
149
{
150 151
  StgStreamImpl* const This=(StgStreamImpl*)iface;

152
  ULONG ref;
153

154
  ref = InterlockedDecrement(&This->ref);
155

156 157 158
  /*
   * If the reference count goes down to 0, perform suicide.
   */
159
  if (ref==0)
160 161 162
  {
    StgStreamImpl_Destroy(This);
  }
163

164
  return ref;
165 166 167 168 169
}

/***
 * This method is part of the ISequentialStream interface.
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
170
 * It reads a block of information from the stream at the current
171 172 173 174 175
 * position. It then moves the current position at the end of the
 * read block
 *
 * 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 180
		  ULONG          cb,        /* [in] */
		  ULONG*         pcbRead)   /* [out] */
181
{
182 183
  StgStreamImpl* const This=(StgStreamImpl*)iface;

184
  ULONG bytesReadBuffer;
185
  HRESULT res;
186

187
  TRACE("(%p, %p, %d, %p)\n",
188 189
	iface, pv, cb, pcbRead);

190
  if (!This->parentStorage)
191 192
  {
    WARN("storage reverted\n");
193
    return STG_E_REVERTED;
194
  }
195

196
  /*
Alexandre Julliard's avatar
Alexandre Julliard committed
197
   * If the caller is not interested in the number of bytes read,
198 199 200 201
   * we use another buffer to avoid "if" statements in the code.
   */
  if (pcbRead==0)
    pcbRead = &bytesReadBuffer;
202

203 204 205 206 207 208
  res = StorageBaseImpl_StreamReadAt(This->parentStorage,
                                     This->dirEntry,
                                     This->currentPosition,
                                     cb,
                                     pv,
                                     pcbRead);
209

210
  if (SUCCEEDED(res))
211 212
  {
    /*
213
     * Advance the pointer for the number of positions read.
214
     */
215
    This->currentPosition.u.LowPart += *pcbRead;
Alexandre Julliard's avatar
Alexandre Julliard committed
216
  }
217

218
  TRACE("<-- %08x\n", res);
Alexandre Julliard's avatar
Alexandre Julliard committed
219
  return res;
220
}
221

222 223 224 225 226 227 228 229 230 231
/***
 * 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.
 */
232
static HRESULT WINAPI StgStreamImpl_Write(
233
	          IStream*     iface,
234 235 236
		  const void*    pv,          /* [size_is][in] */
		  ULONG          cb,          /* [in] */
		  ULONG*         pcbWritten)  /* [out] */
237
{
238 239
  StgStreamImpl* const This=(StgStreamImpl*)iface;

240
  ULONG bytesWritten = 0;
241
  HRESULT res;
242

243
  TRACE("(%p, %p, %d, %p)\n",
244
	iface, pv, cb, pcbWritten);
245

246 247 248
  /*
   * Do we have permission to write to this stream?
   */
249 250 251 252 253 254
  switch(STGM_ACCESS_MODE(This->grfMode))
  {
  case STGM_WRITE:
  case STGM_READWRITE:
      break;
  default:
255
      WARN("access denied by flags: 0x%x\n", STGM_ACCESS_MODE(This->grfMode));
256 257
      return STG_E_ACCESSDENIED;
  }
258

259 260 261
  if (!pv)
    return STG_E_INVALIDPOINTER;

262
  if (!This->parentStorage)
263 264
  {
    WARN("storage reverted\n");
265
    return STG_E_REVERTED;
266
  }
267
 
268 269 270 271 272 273
  /*
   * 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;
274

275 276 277 278 279
  /*
   * Initialize the out parameter
   */
  *pcbWritten = 0;

280 281
  if (cb == 0)
  {
282
    TRACE("<-- S_OK, written 0\n");
283 284
    return S_OK;
  }
285

286 287 288 289 290 291
  res = StorageBaseImpl_StreamWriteAt(This->parentStorage,
                                      This->dirEntry,
                                      This->currentPosition,
                                      cb,
                                      pv,
                                      pcbWritten);
292

293 294 295
  /*
   * Advance the position pointer for the number of positions written.
   */
296
  This->currentPosition.u.LowPart += *pcbWritten;
297

298 299 300
  if (SUCCEEDED(res))
    res = StorageBaseImpl_Flush(This->parentStorage);

301
  TRACE("<-- S_OK, written %u\n", *pcbWritten);
302
  return res;
303 304 305 306 307 308 309 310 311
}

/***
 * 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.
312
 */
313
static HRESULT WINAPI StgStreamImpl_Seek(
314
		  IStream*      iface,
315 316
		  LARGE_INTEGER   dlibMove,         /* [in] */
		  DWORD           dwOrigin,         /* [in] */
317 318
		  ULARGE_INTEGER* plibNewPosition) /* [out] */
{
319 320
  StgStreamImpl* const This=(StgStreamImpl*)iface;

321
  ULARGE_INTEGER newPosition;
322 323
  DirEntry currentEntry;
  HRESULT hr;
324

325
  TRACE("(%p, %d, %d, %p)\n",
326
	iface, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
327

328 329 330 331
  /*
   * fail if the stream has no parent (as does windows)
   */

332
  if (!This->parentStorage)
333 334
  {
    WARN("storage reverted\n");
335
    return STG_E_REVERTED;
336
  }
337

338
  /*
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
   * 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:
355 356
      plibNewPosition->u.HighPart = 0;
      plibNewPosition->u.LowPart  = 0;
357 358 359 360 361
      break;
    case STREAM_SEEK_CUR:
      *plibNewPosition = This->currentPosition;
      break;
    case STREAM_SEEK_END:
362 363 364
      hr = StorageBaseImpl_ReadDirEntry(This->parentStorage, This->dirEntry, &currentEntry);
      if (FAILED(hr)) return hr;
      *plibNewPosition = currentEntry.size;
365 366
      break;
    default:
367
      WARN("invalid dwOrigin %d\n", dwOrigin);
368 369 370
      return STG_E_INVALIDFUNCTION;
  }

371
  plibNewPosition->QuadPart += dlibMove.QuadPart;
372 373

  /*
374
   * tell the caller what we calculated
375 376
   */
  This->currentPosition = *plibNewPosition;
377

378 379 380 381 382 383 384 385 386 387
  return S_OK;
}

/***
 * This method is part of the IStream interface.
 *
 * It will change the size of a stream.
 *
 * See the documentation of IStream for more info.
 */
388
static HRESULT WINAPI StgStreamImpl_SetSize(
389
				     IStream*      iface,
390
				     ULARGE_INTEGER  libNewSize)   /* [in] */
391
{
392 393
  StgStreamImpl* const This=(StgStreamImpl*)iface;

394
  HRESULT      hr;
395

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

398
  if(!This->parentStorage)
399 400
  {
    WARN("storage reverted\n");
401
    return STG_E_REVERTED;
402
  }
403

404 405 406
  /*
   * As documented.
   */
407
  if (libNewSize.u.HighPart != 0)
408 409
  {
    WARN("invalid value for libNewSize.u.HighPart %d\n", libNewSize.u.HighPart);
410
    return STG_E_INVALIDFUNCTION;
411
  }
412 413 414 415 416

  /*
   * Do we have permission?
   */
  if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE)))
417 418
  {
    WARN("access denied\n");
419
    return STG_E_ACCESSDENIED;
420
  }
421

422
  hr = StorageBaseImpl_StreamSetSize(This->parentStorage, This->dirEntry, libNewSize);
423 424 425 426

  if (SUCCEEDED(hr))
    hr = StorageBaseImpl_Flush(This->parentStorage);

427
  return hr;
428
}
429

430 431 432 433 434 435 436
/***
 * 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.
 */
437
static HRESULT WINAPI StgStreamImpl_CopyTo(
438
				    IStream*      iface,
439 440 441 442
				    IStream*      pstm,         /* [unique][in] */
				    ULARGE_INTEGER  cb,           /* [in] */
				    ULARGE_INTEGER* pcbRead,      /* [out] */
				    ULARGE_INTEGER* pcbWritten)   /* [out] */
443
{
444
  StgStreamImpl* const This=(StgStreamImpl*)iface;
445 446 447 448 449 450
  HRESULT        hr = S_OK;
  BYTE           tmpBuffer[128];
  ULONG          bytesRead, bytesWritten, copySize;
  ULARGE_INTEGER totalBytesRead;
  ULARGE_INTEGER totalBytesWritten;

451
  TRACE("(%p, %p, %d, %p, %p)\n",
452
	iface, pstm, cb.u.LowPart, pcbRead, pcbWritten);
453

454 455 456
  /*
   * Sanity check
   */
457

458
  if (!This->parentStorage)
459 460
  {
    WARN("storage reverted\n");
461
    return STG_E_REVERTED;
462
  }
463

464 465 466
  if ( pstm == 0 )
    return STG_E_INVALIDPOINTER;

467 468
  totalBytesRead.QuadPart = 0;
  totalBytesWritten.QuadPart = 0;
469

470
  while ( cb.QuadPart > 0 )
471
  {
472 473
    if ( cb.QuadPart >= sizeof(tmpBuffer) )
      copySize = sizeof(tmpBuffer);
474
    else
475
      copySize = cb.u.LowPart;
476

477
    IStream_Read(iface, tmpBuffer, copySize, &bytesRead);
478

479
    totalBytesRead.QuadPart += bytesRead;
480

481
    IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten);
482

483
    totalBytesWritten.QuadPart += bytesWritten;
484 485

    /*
Alexandre Julliard's avatar
Alexandre Julliard committed
486
     * Check that read & write operations were successful
487
     */
488
    if (bytesRead != bytesWritten)
489 490
    {
      hr = STG_E_MEDIUMFULL;
491
      WARN("medium full\n");
492 493
      break;
    }
494

495
    if (bytesRead!=copySize)
496
      cb.QuadPart = 0;
497
    else
498
      cb.QuadPart -= bytesRead;
499 500
  }

501 502
  if (pcbRead) pcbRead->QuadPart = totalBytesRead.QuadPart;
  if (pcbWritten) pcbWritten->QuadPart = totalBytesWritten.QuadPart;
503 504

  return hr;
505 506 507 508 509 510 511 512 513
}

/***
 * 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.
514
 */
515
static HRESULT WINAPI StgStreamImpl_Commit(
516
		  IStream*      iface,
517
		  DWORD           grfCommitFlags)  /* [in] */
518
{
519 520
  StgStreamImpl* const This=(StgStreamImpl*)iface;

521
  if (!This->parentStorage)
522 523
  {
    WARN("storage reverted\n");
524
    return STG_E_REVERTED;
525
  }
526

527
  return StorageBaseImpl_Flush(This->parentStorage);
528 529 530 531 532 533 534 535 536
}

/***
 * 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.
537
 */
538
static HRESULT WINAPI StgStreamImpl_Revert(
539
		  IStream* iface)
540 541 542 543
{
  return S_OK;
}

544
static HRESULT WINAPI StgStreamImpl_LockRegion(
545
					IStream*     iface,
546 547 548
					ULARGE_INTEGER libOffset,   /* [in] */
					ULARGE_INTEGER cb,          /* [in] */
					DWORD          dwLockType)  /* [in] */
549
{
550 551
  StgStreamImpl* const This=(StgStreamImpl*)iface;

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

558
  FIXME("not implemented!\n");
559 560 561
  return E_NOTIMPL;
}

562
static HRESULT WINAPI StgStreamImpl_UnlockRegion(
563
					  IStream*     iface,
564 565 566
					  ULARGE_INTEGER libOffset,   /* [in] */
					  ULARGE_INTEGER cb,          /* [in] */
					  DWORD          dwLockType)  /* [in] */
567
{
568 569
  StgStreamImpl* const This=(StgStreamImpl*)iface;

570
  if (!This->parentStorage)
571 572
  {
    WARN("storage reverted\n");
573
    return STG_E_REVERTED;
574
  }
575

576
  FIXME("not implemented!\n");
577 578 579 580 581 582 583 584 585 586
  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.
587
 */
588
static HRESULT WINAPI StgStreamImpl_Stat(
589
		  IStream*     iface,
590
		  STATSTG*       pstatstg,     /* [out] */
591
		  DWORD          grfStatFlag)  /* [in] */
592
{
593 594
  StgStreamImpl* const This=(StgStreamImpl*)iface;

595
  DirEntry     currentEntry;
596
  HRESULT      hr;
597

598
  TRACE("%p %p %d\n", This, pstatstg, grfStatFlag);
599

600 601 602 603
  /*
   * if stream has no parent, return STG_E_REVERTED
   */

604
  if (!This->parentStorage)
605 606
  {
    WARN("storage reverted\n");
607
    return STG_E_REVERTED;
608
  }
609

610
  /*
611
   * Read the information from the directory entry.
612
   */
613
  hr = StorageBaseImpl_ReadDirEntry(This->parentStorage,
614
					     This->dirEntry,
615
					     &currentEntry);
616

617
  if (SUCCEEDED(hr))
618
  {
619
    StorageUtl_CopyDirEntryToSTATSTG(This->parentStorage,
620
                     pstatstg,
621
				     &currentEntry,
622
				     grfStatFlag);
623 624

    pstatstg->grfMode = This->grfMode;
625

626
    /* In simple create mode cbSize is the current pos */
627
    if((This->parentStorage->openFlags & STGM_SIMPLE) && This->parentStorage->create)
628 629
      pstatstg->cbSize = This->currentPosition;

630 631
    return S_OK;
  }
632

633
  WARN("failed to read entry\n");
634
  return hr;
635
}
636

637 638 639 640 641 642 643 644 645 646 647
/***
 * 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.
648
 */
649
static HRESULT WINAPI StgStreamImpl_Clone(
650
				   IStream*     iface,
651
				   IStream**    ppstm) /* [out] */
652
{
653 654 655 656 657
  StgStreamImpl* const This=(StgStreamImpl*)iface;
  HRESULT hres;
  StgStreamImpl* new_stream;
  LARGE_INTEGER seek_pos;

658 659
  TRACE("%p %p\n", This, ppstm);

660 661 662
  /*
   * Sanity check
   */
663

664
  if (!This->parentStorage)
665 666
    return STG_E_REVERTED;

667 668 669
  if ( ppstm == 0 )
    return STG_E_INVALIDPOINTER;

670
  new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->dirEntry);
671 672 673 674 675

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

  *ppstm = (IStream*) new_stream;
676 677
  IStream_AddRef(*ppstm);

678
  seek_pos.QuadPart = This->currentPosition.QuadPart;
679

680
  hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL);
681

682 683 684
  assert (SUCCEEDED(hres));

  return S_OK;
685
}
686 687 688 689

/*
 * Virtual function table for the StgStreamImpl class.
 */
690
static const IStreamVtbl StgStreamImpl_Vtbl =
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
{
    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
717
 *    dirEntry      - Index of the directory entry that points to this stream.
718 719 720 721
 */
StgStreamImpl* StgStreamImpl_Construct(
		StorageBaseImpl* parentStorage,
    DWORD            grfMode,
722
    DirRef           dirEntry)
723 724 725 726 727 728 729 730 731 732 733 734 735
{
  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;

736 737
    newStream->parentStorage = parentStorage;

738 739 740
    /*
     * We want to nail-down the reference to the storage in case the
     * stream out-lives the storage in the client application.
741 742 743 744 745 746 747
     *
     * -- 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)
748 749 750
     */

    newStream->grfMode = grfMode;
751
    newStream->dirEntry = dirEntry;
752 753 754 755 756 757 758

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

759 760
    /* add us to the storage's list of active streams */
    StorageBaseImpl_AddStream(parentStorage, newStream);
761 762 763 764
  }

  return newStream;
}