| 1 | /* | 
|---|
| 2 | * Compound Storage (32 bit version) | 
|---|
| 3 | * Stream implementation | 
|---|
| 4 | * | 
|---|
| 5 | * This file contains the implementation of the stream interface | 
|---|
| 6 | * for streams contained in a compound storage. | 
|---|
| 7 | * | 
|---|
| 8 | * Copyright 1999 Francis Beaudet | 
|---|
| 9 | * Copyright 1999 Thuy Nguyen | 
|---|
| 10 | * | 
|---|
| 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. | 
|---|
| 15 | * | 
|---|
| 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. | 
|---|
| 20 | * | 
|---|
| 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|---|
| 24 | */ | 
|---|
| 25 | #include <assert.h> | 
|---|
| 26 | #include <stdlib.h> | 
|---|
| 27 | #include <stdio.h> | 
|---|
| 28 | #include <string.h> | 
|---|
| 29 |  | 
|---|
| 30 | #include "winbase.h" | 
|---|
| 31 | #include "winerror.h" | 
|---|
| 32 | #include "winternl.h" | 
|---|
| 33 | #include "wine/debug.h" | 
|---|
| 34 |  | 
|---|
| 35 | #include "storage32.h" | 
|---|
| 36 |  | 
|---|
| 37 | WINE_DEFAULT_DEBUG_CHANNEL(storage); | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | /* | 
|---|
| 41 | * Virtual function table for the StgStreamImpl class. | 
|---|
| 42 | */ | 
|---|
| 43 | static ICOM_VTABLE(IStream) StgStreamImpl_Vtbl = | 
|---|
| 44 | { | 
|---|
| 45 | ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE | 
|---|
| 46 | StgStreamImpl_QueryInterface, | 
|---|
| 47 | StgStreamImpl_AddRef, | 
|---|
| 48 | StgStreamImpl_Release, | 
|---|
| 49 | StgStreamImpl_Read, | 
|---|
| 50 | StgStreamImpl_Write, | 
|---|
| 51 | StgStreamImpl_Seek, | 
|---|
| 52 | StgStreamImpl_SetSize, | 
|---|
| 53 | StgStreamImpl_CopyTo, | 
|---|
| 54 | StgStreamImpl_Commit, | 
|---|
| 55 | StgStreamImpl_Revert, | 
|---|
| 56 | StgStreamImpl_LockRegion, | 
|---|
| 57 | StgStreamImpl_UnlockRegion, | 
|---|
| 58 | StgStreamImpl_Stat, | 
|---|
| 59 | StgStreamImpl_Clone | 
|---|
| 60 | }; | 
|---|
| 61 |  | 
|---|
| 62 | /****************************************************************************** | 
|---|
| 63 | ** StgStreamImpl implementation | 
|---|
| 64 | */ | 
|---|
| 65 |  | 
|---|
| 66 | /*** | 
|---|
| 67 | * This is the constructor for the StgStreamImpl class. | 
|---|
| 68 | * | 
|---|
| 69 | * Params: | 
|---|
| 70 | *    parentStorage - Pointer to the storage that contains the stream to open | 
|---|
| 71 | *    ownerProperty - Index of the property that points to this stream. | 
|---|
| 72 | */ | 
|---|
| 73 | StgStreamImpl* StgStreamImpl_Construct( | 
|---|
| 74 | StorageBaseImpl* parentStorage, | 
|---|
| 75 | DWORD            grfMode, | 
|---|
| 76 | ULONG            ownerProperty) | 
|---|
| 77 | { | 
|---|
| 78 | StgStreamImpl* newStream; | 
|---|
| 79 |  | 
|---|
| 80 | newStream = HeapAlloc(GetProcessHeap(), 0, sizeof(StgStreamImpl)); | 
|---|
| 81 |  | 
|---|
| 82 | if (newStream!=0) | 
|---|
| 83 | { | 
|---|
| 84 | /* | 
|---|
| 85 | * Set-up the virtual function table and reference count. | 
|---|
| 86 | */ | 
|---|
| 87 | ICOM_VTBL(newStream) = &StgStreamImpl_Vtbl; | 
|---|
| 88 | newStream->ref       = 0; | 
|---|
| 89 |  | 
|---|
| 90 | /* | 
|---|
| 91 | * We want to nail-down the reference to the storage in case the | 
|---|
| 92 | * stream out-lives the storage in the client application. | 
|---|
| 93 | */ | 
|---|
| 94 | newStream->parentStorage = parentStorage; | 
|---|
| 95 | IStorage_AddRef((IStorage*)newStream->parentStorage); | 
|---|
| 96 |  | 
|---|
| 97 | newStream->grfMode = grfMode; | 
|---|
| 98 | newStream->ownerProperty = ownerProperty; | 
|---|
| 99 |  | 
|---|
| 100 | /* | 
|---|
| 101 | * Start the stream at the beginning. | 
|---|
| 102 | */ | 
|---|
| 103 | newStream->currentPosition.s.HighPart = 0; | 
|---|
| 104 | newStream->currentPosition.s.LowPart = 0; | 
|---|
| 105 |  | 
|---|
| 106 | /* | 
|---|
| 107 | * Initialize the rest of the data. | 
|---|
| 108 | */ | 
|---|
| 109 | newStream->streamSize.s.HighPart = 0; | 
|---|
| 110 | newStream->streamSize.s.LowPart  = 0; | 
|---|
| 111 | newStream->bigBlockChain       = 0; | 
|---|
| 112 | newStream->smallBlockChain     = 0; | 
|---|
| 113 |  | 
|---|
| 114 | /* | 
|---|
| 115 | * Read the size from the property and determine if the blocks forming | 
|---|
| 116 | * this stream are large or small. | 
|---|
| 117 | */ | 
|---|
| 118 | StgStreamImpl_OpenBlockChain(newStream); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | return newStream; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | /*** | 
|---|
| 125 | * This is the destructor of the StgStreamImpl class. | 
|---|
| 126 | * | 
|---|
| 127 | * This method will clean-up all the resources used-up by the given StgStreamImpl | 
|---|
| 128 | * class. The pointer passed-in to this function will be freed and will not | 
|---|
| 129 | * be valid anymore. | 
|---|
| 130 | */ | 
|---|
| 131 | void StgStreamImpl_Destroy(StgStreamImpl* This) | 
|---|
| 132 | { | 
|---|
| 133 | TRACE("(%p)\n", This); | 
|---|
| 134 |  | 
|---|
| 135 | /* | 
|---|
| 136 | * Release the reference we are holding on the parent storage. | 
|---|
| 137 | */ | 
|---|
| 138 | IStorage_Release((IStorage*)This->parentStorage); | 
|---|
| 139 | This->parentStorage = 0; | 
|---|
| 140 |  | 
|---|
| 141 | /* | 
|---|
| 142 | * Make sure we clean-up the block chain stream objects that we were using. | 
|---|
| 143 | */ | 
|---|
| 144 | if (This->bigBlockChain != 0) | 
|---|
| 145 | { | 
|---|
| 146 | BlockChainStream_Destroy(This->bigBlockChain); | 
|---|
| 147 | This->bigBlockChain = 0; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | if (This->smallBlockChain != 0) | 
|---|
| 151 | { | 
|---|
| 152 | SmallBlockChainStream_Destroy(This->smallBlockChain); | 
|---|
| 153 | This->smallBlockChain = 0; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | /* | 
|---|
| 157 | * Finally, free the memory used-up by the class. | 
|---|
| 158 | */ | 
|---|
| 159 | HeapFree(GetProcessHeap(), 0, This); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | /*** | 
|---|
| 163 | * This implements the IUnknown method QueryInterface for this | 
|---|
| 164 | * class | 
|---|
| 165 | */ | 
|---|
| 166 | HRESULT WINAPI StgStreamImpl_QueryInterface( | 
|---|
| 167 | IStream*     iface, | 
|---|
| 168 | REFIID         riid,        /* [in] */ | 
|---|
| 169 | void**         ppvObject)   /* [iid_is][out] */ | 
|---|
| 170 | { | 
|---|
| 171 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 172 |  | 
|---|
| 173 | /* | 
|---|
| 174 | * Perform a sanity check on the parameters. | 
|---|
| 175 | */ | 
|---|
| 176 | if (ppvObject==0) | 
|---|
| 177 | return E_INVALIDARG; | 
|---|
| 178 |  | 
|---|
| 179 | /* | 
|---|
| 180 | * Initialize the return parameter. | 
|---|
| 181 | */ | 
|---|
| 182 | *ppvObject = 0; | 
|---|
| 183 |  | 
|---|
| 184 | /* | 
|---|
| 185 | * Compare the riid with the interface IDs implemented by this object. | 
|---|
| 186 | */ | 
|---|
| 187 | if (memcmp(&IID_IUnknown, riid, sizeof(IID_IUnknown)) == 0) | 
|---|
| 188 | { | 
|---|
| 189 | *ppvObject = (IStream*)This; | 
|---|
| 190 | } | 
|---|
| 191 | else if (memcmp(&IID_IStream, riid, sizeof(IID_IStream)) == 0) | 
|---|
| 192 | { | 
|---|
| 193 | *ppvObject = (IStream*)This; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 | /* | 
|---|
| 197 | * Check that we obtained an interface. | 
|---|
| 198 | */ | 
|---|
| 199 | if ((*ppvObject)==0) | 
|---|
| 200 | return E_NOINTERFACE; | 
|---|
| 201 |  | 
|---|
| 202 | /* | 
|---|
| 203 | * Query Interface always increases the reference count by one when it is | 
|---|
| 204 | * successful | 
|---|
| 205 | */ | 
|---|
| 206 | StgStreamImpl_AddRef(iface); | 
|---|
| 207 |  | 
|---|
| 208 | return S_OK; | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | /*** | 
|---|
| 212 | * This implements the IUnknown method AddRef for this | 
|---|
| 213 | * class | 
|---|
| 214 | */ | 
|---|
| 215 | ULONG WINAPI StgStreamImpl_AddRef( | 
|---|
| 216 | IStream* iface) | 
|---|
| 217 | { | 
|---|
| 218 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 219 |  | 
|---|
| 220 | This->ref++; | 
|---|
| 221 |  | 
|---|
| 222 | return This->ref; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | /*** | 
|---|
| 226 | * This implements the IUnknown method Release for this | 
|---|
| 227 | * class | 
|---|
| 228 | */ | 
|---|
| 229 | ULONG WINAPI StgStreamImpl_Release( | 
|---|
| 230 | IStream* iface) | 
|---|
| 231 | { | 
|---|
| 232 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 233 |  | 
|---|
| 234 | ULONG newRef; | 
|---|
| 235 |  | 
|---|
| 236 | This->ref--; | 
|---|
| 237 |  | 
|---|
| 238 | newRef = This->ref; | 
|---|
| 239 |  | 
|---|
| 240 | /* | 
|---|
| 241 | * If the reference count goes down to 0, perform suicide. | 
|---|
| 242 | */ | 
|---|
| 243 | if (newRef==0) | 
|---|
| 244 | { | 
|---|
| 245 | StgStreamImpl_Destroy(This); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | return newRef; | 
|---|
| 249 | } | 
|---|
| 250 |  | 
|---|
| 251 | /*** | 
|---|
| 252 | * This method will open the block chain pointed by the property | 
|---|
| 253 | * that describes the stream. | 
|---|
| 254 | * If the stream's size is null, no chain is opened. | 
|---|
| 255 | */ | 
|---|
| 256 | void StgStreamImpl_OpenBlockChain( | 
|---|
| 257 | StgStreamImpl* This) | 
|---|
| 258 | { | 
|---|
| 259 | StgProperty    curProperty; | 
|---|
| 260 | BOOL         readSucessful; | 
|---|
| 261 |  | 
|---|
| 262 | /* | 
|---|
| 263 | * Make sure no old object is left over. | 
|---|
| 264 | */ | 
|---|
| 265 | if (This->smallBlockChain != 0) | 
|---|
| 266 | { | 
|---|
| 267 | SmallBlockChainStream_Destroy(This->smallBlockChain); | 
|---|
| 268 | This->smallBlockChain = 0; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 271 | if (This->bigBlockChain != 0) | 
|---|
| 272 | { | 
|---|
| 273 | BlockChainStream_Destroy(This->bigBlockChain); | 
|---|
| 274 | This->bigBlockChain = 0; | 
|---|
| 275 | } | 
|---|
| 276 |  | 
|---|
| 277 | /* | 
|---|
| 278 | * Read the information from the property. | 
|---|
| 279 | */ | 
|---|
| 280 | readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage, | 
|---|
| 281 | This->ownerProperty, | 
|---|
| 282 | &curProperty); | 
|---|
| 283 |  | 
|---|
| 284 | if (readSucessful) | 
|---|
| 285 | { | 
|---|
| 286 | This->streamSize = curProperty.size; | 
|---|
| 287 |  | 
|---|
| 288 | /* | 
|---|
| 289 | * This code supports only streams that are <32 bits in size. | 
|---|
| 290 | */ | 
|---|
| 291 | assert(This->streamSize.s.HighPart == 0); | 
|---|
| 292 |  | 
|---|
| 293 | if(curProperty.startingBlock == BLOCK_END_OF_CHAIN) | 
|---|
| 294 | { | 
|---|
| 295 | assert( (This->streamSize.s.HighPart == 0) && (This->streamSize.s.LowPart == 0) ); | 
|---|
| 296 | } | 
|---|
| 297 | else | 
|---|
| 298 | { | 
|---|
| 299 | if ( (This->streamSize.s.HighPart == 0) && | 
|---|
| 300 | (This->streamSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) ) | 
|---|
| 301 | { | 
|---|
| 302 | This->smallBlockChain = SmallBlockChainStream_Construct( | 
|---|
| 303 | This->parentStorage->ancestorStorage, | 
|---|
| 304 | This->ownerProperty); | 
|---|
| 305 | } | 
|---|
| 306 | else | 
|---|
| 307 | { | 
|---|
| 308 | This->bigBlockChain = BlockChainStream_Construct( | 
|---|
| 309 | This->parentStorage->ancestorStorage, | 
|---|
| 310 | NULL, | 
|---|
| 311 | This->ownerProperty); | 
|---|
| 312 | } | 
|---|
| 313 | } | 
|---|
| 314 | } | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | /*** | 
|---|
| 318 | * This method is part of the ISequentialStream interface. | 
|---|
| 319 | * | 
|---|
| 320 | * It reads a block of information from the stream at the current | 
|---|
| 321 | * position. It then moves the current position at the end of the | 
|---|
| 322 | * read block | 
|---|
| 323 | * | 
|---|
| 324 | * See the documentation of ISequentialStream for more info. | 
|---|
| 325 | */ | 
|---|
| 326 | HRESULT WINAPI StgStreamImpl_Read( | 
|---|
| 327 | IStream*     iface, | 
|---|
| 328 | void*          pv,        /* [length_is][size_is][out] */ | 
|---|
| 329 | ULONG          cb,        /* [in] */ | 
|---|
| 330 | ULONG*         pcbRead)   /* [out] */ | 
|---|
| 331 | { | 
|---|
| 332 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 333 |  | 
|---|
| 334 | ULONG bytesReadBuffer; | 
|---|
| 335 | ULONG bytesToReadFromBuffer; | 
|---|
| 336 | HRESULT res = S_FALSE; | 
|---|
| 337 |  | 
|---|
| 338 | TRACE("(%p, %p, %ld, %p)\n", | 
|---|
| 339 | iface, pv, cb, pcbRead); | 
|---|
| 340 |  | 
|---|
| 341 | /* | 
|---|
| 342 | * If the caller is not interested in the number of bytes read, | 
|---|
| 343 | * we use another buffer to avoid "if" statements in the code. | 
|---|
| 344 | */ | 
|---|
| 345 | if (pcbRead==0) | 
|---|
| 346 | pcbRead = &bytesReadBuffer; | 
|---|
| 347 |  | 
|---|
| 348 | /* | 
|---|
| 349 | * Using the known size of the stream, calculate the number of bytes | 
|---|
| 350 | * to read from the block chain | 
|---|
| 351 | */ | 
|---|
| 352 | bytesToReadFromBuffer = min( This->streamSize.s.LowPart - This->currentPosition.s.LowPart, cb); | 
|---|
| 353 |  | 
|---|
| 354 | /* | 
|---|
| 355 | * Depending on the type of chain that was opened when the stream was constructed, | 
|---|
| 356 | * we delegate the work to the method that reads the block chains. | 
|---|
| 357 | */ | 
|---|
| 358 | if (This->smallBlockChain!=0) | 
|---|
| 359 | { | 
|---|
| 360 | SmallBlockChainStream_ReadAt(This->smallBlockChain, | 
|---|
| 361 | This->currentPosition, | 
|---|
| 362 | bytesToReadFromBuffer, | 
|---|
| 363 | pv, | 
|---|
| 364 | pcbRead); | 
|---|
| 365 |  | 
|---|
| 366 | } | 
|---|
| 367 | else if (This->bigBlockChain!=0) | 
|---|
| 368 | { | 
|---|
| 369 | BlockChainStream_ReadAt(This->bigBlockChain, | 
|---|
| 370 | This->currentPosition, | 
|---|
| 371 | bytesToReadFromBuffer, | 
|---|
| 372 | pv, | 
|---|
| 373 | pcbRead); | 
|---|
| 374 | } | 
|---|
| 375 | else | 
|---|
| 376 | { | 
|---|
| 377 | /* | 
|---|
| 378 | * Small and big block chains are both NULL. This case will happen | 
|---|
| 379 | * when a stream starts with BLOCK_END_OF_CHAIN and has size zero. | 
|---|
| 380 | */ | 
|---|
| 381 |  | 
|---|
| 382 | *pcbRead = 0; | 
|---|
| 383 | res = S_OK; | 
|---|
| 384 | goto end; | 
|---|
| 385 | } | 
|---|
| 386 |  | 
|---|
| 387 | /* | 
|---|
| 388 | * We should always be able to read the proper amount of data from the | 
|---|
| 389 | * chain. | 
|---|
| 390 | */ | 
|---|
| 391 | assert(bytesToReadFromBuffer == *pcbRead); | 
|---|
| 392 |  | 
|---|
| 393 | /* | 
|---|
| 394 | * Advance the pointer for the number of positions read. | 
|---|
| 395 | */ | 
|---|
| 396 | This->currentPosition.s.LowPart += *pcbRead; | 
|---|
| 397 |  | 
|---|
| 398 | if(*pcbRead != cb) | 
|---|
| 399 | { | 
|---|
| 400 | WARN("read %ld instead of the required %ld bytes !\n", *pcbRead, cb); | 
|---|
| 401 | /* | 
|---|
| 402 | * this used to return S_FALSE, however MSDN docu says that an app should | 
|---|
| 403 | * be prepared to handle error in case of stream end reached, as *some* | 
|---|
| 404 | * implementations *might* return an error (IOW: most do *not*). | 
|---|
| 405 | * As some program fails on returning S_FALSE, I better use S_OK here. | 
|---|
| 406 | */ | 
|---|
| 407 | res = S_OK; | 
|---|
| 408 | } | 
|---|
| 409 | else | 
|---|
| 410 | res = S_OK; | 
|---|
| 411 |  | 
|---|
| 412 | end: | 
|---|
| 413 | TRACE("<-- %08lx\n", res); | 
|---|
| 414 | return res; | 
|---|
| 415 | } | 
|---|
| 416 |  | 
|---|
| 417 | /*** | 
|---|
| 418 | * This method is part of the ISequentialStream interface. | 
|---|
| 419 | * | 
|---|
| 420 | * It writes a block of information to the stream at the current | 
|---|
| 421 | * position. It then moves the current position at the end of the | 
|---|
| 422 | * written block. If the stream is too small to fit the block, | 
|---|
| 423 | * the stream is grown to fit. | 
|---|
| 424 | * | 
|---|
| 425 | * See the documentation of ISequentialStream for more info. | 
|---|
| 426 | */ | 
|---|
| 427 | HRESULT WINAPI StgStreamImpl_Write( | 
|---|
| 428 | IStream*     iface, | 
|---|
| 429 | const void*    pv,          /* [size_is][in] */ | 
|---|
| 430 | ULONG          cb,          /* [in] */ | 
|---|
| 431 | ULONG*         pcbWritten)  /* [out] */ | 
|---|
| 432 | { | 
|---|
| 433 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 434 |  | 
|---|
| 435 | ULARGE_INTEGER newSize; | 
|---|
| 436 | ULONG bytesWritten = 0; | 
|---|
| 437 |  | 
|---|
| 438 | TRACE("(%p, %p, %ld, %p)\n", | 
|---|
| 439 | iface, pv, cb, pcbWritten); | 
|---|
| 440 |  | 
|---|
| 441 | /* | 
|---|
| 442 | * Do we have permission to write to this stream? | 
|---|
| 443 | */ | 
|---|
| 444 | if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) { | 
|---|
| 445 | return STG_E_ACCESSDENIED; | 
|---|
| 446 | } | 
|---|
| 447 |  | 
|---|
| 448 | /* | 
|---|
| 449 | * If the caller is not interested in the number of bytes written, | 
|---|
| 450 | * we use another buffer to avoid "if" statements in the code. | 
|---|
| 451 | */ | 
|---|
| 452 | if (pcbWritten == 0) | 
|---|
| 453 | pcbWritten = &bytesWritten; | 
|---|
| 454 |  | 
|---|
| 455 | /* | 
|---|
| 456 | * Initialize the out parameter | 
|---|
| 457 | */ | 
|---|
| 458 | *pcbWritten = 0; | 
|---|
| 459 |  | 
|---|
| 460 | if (cb == 0) | 
|---|
| 461 | { | 
|---|
| 462 | return S_OK; | 
|---|
| 463 | } | 
|---|
| 464 | else | 
|---|
| 465 | { | 
|---|
| 466 | newSize.s.HighPart = 0; | 
|---|
| 467 | newSize.s.LowPart = This->currentPosition.s.LowPart + cb; | 
|---|
| 468 | } | 
|---|
| 469 |  | 
|---|
| 470 | /* | 
|---|
| 471 | * Verify if we need to grow the stream | 
|---|
| 472 | */ | 
|---|
| 473 | if (newSize.s.LowPart > This->streamSize.s.LowPart) | 
|---|
| 474 | { | 
|---|
| 475 | /* grow stream */ | 
|---|
| 476 | IStream_SetSize(iface, newSize); | 
|---|
| 477 | } | 
|---|
| 478 |  | 
|---|
| 479 | /* | 
|---|
| 480 | * Depending on the type of chain that was opened when the stream was constructed, | 
|---|
| 481 | * we delegate the work to the method that readwrites to the block chains. | 
|---|
| 482 | */ | 
|---|
| 483 | if (This->smallBlockChain!=0) | 
|---|
| 484 | { | 
|---|
| 485 | SmallBlockChainStream_WriteAt(This->smallBlockChain, | 
|---|
| 486 | This->currentPosition, | 
|---|
| 487 | cb, | 
|---|
| 488 | pv, | 
|---|
| 489 | pcbWritten); | 
|---|
| 490 |  | 
|---|
| 491 | } | 
|---|
| 492 | else if (This->bigBlockChain!=0) | 
|---|
| 493 | { | 
|---|
| 494 | BlockChainStream_WriteAt(This->bigBlockChain, | 
|---|
| 495 | This->currentPosition, | 
|---|
| 496 | cb, | 
|---|
| 497 | pv, | 
|---|
| 498 | pcbWritten); | 
|---|
| 499 | } | 
|---|
| 500 | else | 
|---|
| 501 | assert(FALSE); | 
|---|
| 502 |  | 
|---|
| 503 | /* | 
|---|
| 504 | * Advance the position pointer for the number of positions written. | 
|---|
| 505 | */ | 
|---|
| 506 | This->currentPosition.s.LowPart += *pcbWritten; | 
|---|
| 507 |  | 
|---|
| 508 | return S_OK; | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 | /*** | 
|---|
| 512 | * This method is part of the IStream interface. | 
|---|
| 513 | * | 
|---|
| 514 | * It will move the current stream pointer according to the parameters | 
|---|
| 515 | * given. | 
|---|
| 516 | * | 
|---|
| 517 | * See the documentation of IStream for more info. | 
|---|
| 518 | */ | 
|---|
| 519 | HRESULT WINAPI StgStreamImpl_Seek( | 
|---|
| 520 | IStream*      iface, | 
|---|
| 521 | LARGE_INTEGER   dlibMove,         /* [in] */ | 
|---|
| 522 | DWORD           dwOrigin,         /* [in] */ | 
|---|
| 523 | ULARGE_INTEGER* plibNewPosition) /* [out] */ | 
|---|
| 524 | { | 
|---|
| 525 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 526 |  | 
|---|
| 527 | ULARGE_INTEGER newPosition; | 
|---|
| 528 |  | 
|---|
| 529 | TRACE("(%p, %ld, %ld, %p)\n", | 
|---|
| 530 | iface, dlibMove.s.LowPart, dwOrigin, plibNewPosition); | 
|---|
| 531 |  | 
|---|
| 532 | /* | 
|---|
| 533 | * The caller is allowed to pass in NULL as the new position return value. | 
|---|
| 534 | * If it happens, we assign it to a dynamic variable to avoid special cases | 
|---|
| 535 | * in the code below. | 
|---|
| 536 | */ | 
|---|
| 537 | if (plibNewPosition == 0) | 
|---|
| 538 | { | 
|---|
| 539 | plibNewPosition = &newPosition; | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | /* | 
|---|
| 543 | * The file pointer is moved depending on the given "function" | 
|---|
| 544 | * parameter. | 
|---|
| 545 | */ | 
|---|
| 546 | switch (dwOrigin) | 
|---|
| 547 | { | 
|---|
| 548 | case STREAM_SEEK_SET: | 
|---|
| 549 | plibNewPosition->s.HighPart = 0; | 
|---|
| 550 | plibNewPosition->s.LowPart  = 0; | 
|---|
| 551 | break; | 
|---|
| 552 | case STREAM_SEEK_CUR: | 
|---|
| 553 | *plibNewPosition = This->currentPosition; | 
|---|
| 554 | break; | 
|---|
| 555 | case STREAM_SEEK_END: | 
|---|
| 556 | *plibNewPosition = This->streamSize; | 
|---|
| 557 | break; | 
|---|
| 558 | default: | 
|---|
| 559 | return STG_E_INVALIDFUNCTION; | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | #if defined(__WIN32OS2__) && defined(__IBMC__) | 
|---|
| 563 | *((LARGE_INTEGER*)plibNewPosition )= RtlpLargeIntegerAdd( (LARGE_INTEGER *)plibNewPosition, &dlibMove ); | 
|---|
| 564 | #else | 
|---|
| 565 | plibNewPosition->QuadPart = RtlLargeIntegerAdd( plibNewPosition->QuadPart, dlibMove.QuadPart ); | 
|---|
| 566 | #endif | 
|---|
| 567 | /* | 
|---|
| 568 | * tell the caller what we calculated | 
|---|
| 569 | */ | 
|---|
| 570 | This->currentPosition = *plibNewPosition; | 
|---|
| 571 |  | 
|---|
| 572 | return S_OK; | 
|---|
| 573 | } | 
|---|
| 574 |  | 
|---|
| 575 | /*** | 
|---|
| 576 | * This method is part of the IStream interface. | 
|---|
| 577 | * | 
|---|
| 578 | * It will change the size of a stream. | 
|---|
| 579 | * | 
|---|
| 580 | * TODO: Switch from small blocks to big blocks and vice versa. | 
|---|
| 581 | * | 
|---|
| 582 | * See the documentation of IStream for more info. | 
|---|
| 583 | */ | 
|---|
| 584 | HRESULT WINAPI StgStreamImpl_SetSize( | 
|---|
| 585 | IStream*      iface, | 
|---|
| 586 | ULARGE_INTEGER  libNewSize)   /* [in] */ | 
|---|
| 587 | { | 
|---|
| 588 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 589 |  | 
|---|
| 590 | StgProperty    curProperty; | 
|---|
| 591 | BOOL         Success; | 
|---|
| 592 |  | 
|---|
| 593 | TRACE("(%p, %ld)\n", iface, libNewSize.s.LowPart); | 
|---|
| 594 |  | 
|---|
| 595 | /* | 
|---|
| 596 | * As documented. | 
|---|
| 597 | */ | 
|---|
| 598 | if (libNewSize.s.HighPart != 0) | 
|---|
| 599 | return STG_E_INVALIDFUNCTION; | 
|---|
| 600 |  | 
|---|
| 601 | /* | 
|---|
| 602 | * Do we have permission? | 
|---|
| 603 | */ | 
|---|
| 604 | if (!(This->grfMode & (STGM_WRITE | STGM_READWRITE))) | 
|---|
| 605 | return STG_E_ACCESSDENIED; | 
|---|
| 606 |  | 
|---|
| 607 | if (This->streamSize.s.LowPart == libNewSize.s.LowPart) | 
|---|
| 608 | return S_OK; | 
|---|
| 609 |  | 
|---|
| 610 | /* | 
|---|
| 611 | * This will happen if we're creating a stream | 
|---|
| 612 | */ | 
|---|
| 613 | if ((This->smallBlockChain == 0) && (This->bigBlockChain == 0)) | 
|---|
| 614 | { | 
|---|
| 615 | if (libNewSize.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) | 
|---|
| 616 | { | 
|---|
| 617 | This->smallBlockChain = SmallBlockChainStream_Construct( | 
|---|
| 618 | This->parentStorage->ancestorStorage, | 
|---|
| 619 | This->ownerProperty); | 
|---|
| 620 | } | 
|---|
| 621 | else | 
|---|
| 622 | { | 
|---|
| 623 | This->bigBlockChain = BlockChainStream_Construct( | 
|---|
| 624 | This->parentStorage->ancestorStorage, | 
|---|
| 625 | NULL, | 
|---|
| 626 | This->ownerProperty); | 
|---|
| 627 | } | 
|---|
| 628 | } | 
|---|
| 629 |  | 
|---|
| 630 | /* | 
|---|
| 631 | * Read this stream's property to see if it's small blocks or big blocks | 
|---|
| 632 | */ | 
|---|
| 633 | Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage, | 
|---|
| 634 | This->ownerProperty, | 
|---|
| 635 | &curProperty); | 
|---|
| 636 | /* | 
|---|
| 637 | * Determine if we have to switch from small to big blocks or vice versa | 
|---|
| 638 | */ | 
|---|
| 639 | if ( (This->smallBlockChain!=0) && | 
|---|
| 640 | (curProperty.size.s.LowPart < LIMIT_TO_USE_SMALL_BLOCK) ) | 
|---|
| 641 | { | 
|---|
| 642 | if (libNewSize.s.LowPart >= LIMIT_TO_USE_SMALL_BLOCK) | 
|---|
| 643 | { | 
|---|
| 644 | /* | 
|---|
| 645 | * Transform the small block chain into a big block chain | 
|---|
| 646 | */ | 
|---|
| 647 | This->bigBlockChain = Storage32Impl_SmallBlocksToBigBlocks( | 
|---|
| 648 | This->parentStorage->ancestorStorage, | 
|---|
| 649 | &This->smallBlockChain); | 
|---|
| 650 | } | 
|---|
| 651 | } | 
|---|
| 652 |  | 
|---|
| 653 | if (This->smallBlockChain!=0) | 
|---|
| 654 | { | 
|---|
| 655 | Success = SmallBlockChainStream_SetSize(This->smallBlockChain, libNewSize); | 
|---|
| 656 | } | 
|---|
| 657 | else | 
|---|
| 658 | { | 
|---|
| 659 | Success = BlockChainStream_SetSize(This->bigBlockChain, libNewSize); | 
|---|
| 660 | } | 
|---|
| 661 |  | 
|---|
| 662 | /* | 
|---|
| 663 | * Write the new information about this stream to the property | 
|---|
| 664 | */ | 
|---|
| 665 | Success = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage, | 
|---|
| 666 | This->ownerProperty, | 
|---|
| 667 | &curProperty); | 
|---|
| 668 |  | 
|---|
| 669 | curProperty.size.s.HighPart = libNewSize.s.HighPart; | 
|---|
| 670 | curProperty.size.s.LowPart = libNewSize.s.LowPart; | 
|---|
| 671 |  | 
|---|
| 672 | if (Success) | 
|---|
| 673 | { | 
|---|
| 674 | StorageImpl_WriteProperty(This->parentStorage->ancestorStorage, | 
|---|
| 675 | This->ownerProperty, | 
|---|
| 676 | &curProperty); | 
|---|
| 677 | } | 
|---|
| 678 |  | 
|---|
| 679 | This->streamSize = libNewSize; | 
|---|
| 680 |  | 
|---|
| 681 | return S_OK; | 
|---|
| 682 | } | 
|---|
| 683 |  | 
|---|
| 684 | /*** | 
|---|
| 685 | * This method is part of the IStream interface. | 
|---|
| 686 | * | 
|---|
| 687 | * It will copy the 'cb' Bytes to 'pstm' IStream. | 
|---|
| 688 | * | 
|---|
| 689 | * See the documentation of IStream for more info. | 
|---|
| 690 | */ | 
|---|
| 691 | HRESULT WINAPI StgStreamImpl_CopyTo( | 
|---|
| 692 | IStream*      iface, | 
|---|
| 693 | IStream*      pstm,         /* [unique][in] */ | 
|---|
| 694 | ULARGE_INTEGER  cb,           /* [in] */ | 
|---|
| 695 | ULARGE_INTEGER* pcbRead,      /* [out] */ | 
|---|
| 696 | ULARGE_INTEGER* pcbWritten)   /* [out] */ | 
|---|
| 697 | { | 
|---|
| 698 | HRESULT        hr = S_OK; | 
|---|
| 699 | BYTE           tmpBuffer[128]; | 
|---|
| 700 | ULONG          bytesRead, bytesWritten, copySize; | 
|---|
| 701 | ULARGE_INTEGER totalBytesRead; | 
|---|
| 702 | ULARGE_INTEGER totalBytesWritten; | 
|---|
| 703 |  | 
|---|
| 704 | TRACE("(%p, %p, %ld, %p, %p)\n", | 
|---|
| 705 | iface, pstm, cb.s.LowPart, pcbRead, pcbWritten); | 
|---|
| 706 |  | 
|---|
| 707 | /* | 
|---|
| 708 | * Sanity check | 
|---|
| 709 | */ | 
|---|
| 710 | if ( pstm == 0 ) | 
|---|
| 711 | return STG_E_INVALIDPOINTER; | 
|---|
| 712 |  | 
|---|
| 713 | totalBytesRead.s.LowPart = totalBytesRead.s.HighPart = 0; | 
|---|
| 714 | totalBytesWritten.s.LowPart = totalBytesWritten.s.HighPart = 0; | 
|---|
| 715 |  | 
|---|
| 716 | /* | 
|---|
| 717 | * use stack to store data temporarily | 
|---|
| 718 | * there is surely a more performant way of doing it, for now this basic | 
|---|
| 719 | * implementation will do the job | 
|---|
| 720 | */ | 
|---|
| 721 | while ( cb.s.LowPart > 0 ) | 
|---|
| 722 | { | 
|---|
| 723 | if ( cb.s.LowPart >= 128 ) | 
|---|
| 724 | copySize = 128; | 
|---|
| 725 | else | 
|---|
| 726 | copySize = cb.s.LowPart; | 
|---|
| 727 |  | 
|---|
| 728 | IStream_Read(iface, tmpBuffer, copySize, &bytesRead); | 
|---|
| 729 |  | 
|---|
| 730 | totalBytesRead.s.LowPart += bytesRead; | 
|---|
| 731 |  | 
|---|
| 732 | IStream_Write(pstm, tmpBuffer, bytesRead, &bytesWritten); | 
|---|
| 733 |  | 
|---|
| 734 | totalBytesWritten.s.LowPart += bytesWritten; | 
|---|
| 735 |  | 
|---|
| 736 | /* | 
|---|
| 737 | * Check that read & write operations were successful | 
|---|
| 738 | */ | 
|---|
| 739 | if (bytesRead != bytesWritten) | 
|---|
| 740 | { | 
|---|
| 741 | hr = STG_E_MEDIUMFULL; | 
|---|
| 742 | break; | 
|---|
| 743 | } | 
|---|
| 744 |  | 
|---|
| 745 | if (bytesRead!=copySize) | 
|---|
| 746 | cb.s.LowPart = 0; | 
|---|
| 747 | else | 
|---|
| 748 | cb.s.LowPart -= bytesRead; | 
|---|
| 749 | } | 
|---|
| 750 |  | 
|---|
| 751 | /* | 
|---|
| 752 | * Update number of bytes read and written | 
|---|
| 753 | */ | 
|---|
| 754 | if (pcbRead) | 
|---|
| 755 | { | 
|---|
| 756 | pcbRead->s.LowPart = totalBytesRead.s.LowPart; | 
|---|
| 757 | pcbRead->s.HighPart = totalBytesRead.s.HighPart; | 
|---|
| 758 | } | 
|---|
| 759 |  | 
|---|
| 760 | if (pcbWritten) | 
|---|
| 761 | { | 
|---|
| 762 | pcbWritten->s.LowPart = totalBytesWritten.s.LowPart; | 
|---|
| 763 | pcbWritten->s.HighPart = totalBytesWritten.s.HighPart; | 
|---|
| 764 | } | 
|---|
| 765 | return hr; | 
|---|
| 766 | } | 
|---|
| 767 |  | 
|---|
| 768 | /*** | 
|---|
| 769 | * This method is part of the IStream interface. | 
|---|
| 770 | * | 
|---|
| 771 | * For streams contained in structured storages, this method | 
|---|
| 772 | * does nothing. This is what the documentation tells us. | 
|---|
| 773 | * | 
|---|
| 774 | * See the documentation of IStream for more info. | 
|---|
| 775 | */ | 
|---|
| 776 | HRESULT WINAPI StgStreamImpl_Commit( | 
|---|
| 777 | IStream*      iface, | 
|---|
| 778 | DWORD           grfCommitFlags)  /* [in] */ | 
|---|
| 779 | { | 
|---|
| 780 | return S_OK; | 
|---|
| 781 | } | 
|---|
| 782 |  | 
|---|
| 783 | /*** | 
|---|
| 784 | * This method is part of the IStream interface. | 
|---|
| 785 | * | 
|---|
| 786 | * For streams contained in structured storages, this method | 
|---|
| 787 | * does nothing. This is what the documentation tells us. | 
|---|
| 788 | * | 
|---|
| 789 | * See the documentation of IStream for more info. | 
|---|
| 790 | */ | 
|---|
| 791 | HRESULT WINAPI StgStreamImpl_Revert( | 
|---|
| 792 | IStream* iface) | 
|---|
| 793 | { | 
|---|
| 794 | return S_OK; | 
|---|
| 795 | } | 
|---|
| 796 |  | 
|---|
| 797 | HRESULT WINAPI StgStreamImpl_LockRegion( | 
|---|
| 798 | IStream*     iface, | 
|---|
| 799 | ULARGE_INTEGER libOffset,   /* [in] */ | 
|---|
| 800 | ULARGE_INTEGER cb,          /* [in] */ | 
|---|
| 801 | DWORD          dwLockType)  /* [in] */ | 
|---|
| 802 | { | 
|---|
| 803 | FIXME("not implemented!\n"); | 
|---|
| 804 | return E_NOTIMPL; | 
|---|
| 805 | } | 
|---|
| 806 |  | 
|---|
| 807 | HRESULT WINAPI StgStreamImpl_UnlockRegion( | 
|---|
| 808 | IStream*     iface, | 
|---|
| 809 | ULARGE_INTEGER libOffset,   /* [in] */ | 
|---|
| 810 | ULARGE_INTEGER cb,          /* [in] */ | 
|---|
| 811 | DWORD          dwLockType)  /* [in] */ | 
|---|
| 812 | { | 
|---|
| 813 | FIXME("not implemented!\n"); | 
|---|
| 814 | return E_NOTIMPL; | 
|---|
| 815 | } | 
|---|
| 816 |  | 
|---|
| 817 | /*** | 
|---|
| 818 | * This method is part of the IStream interface. | 
|---|
| 819 | * | 
|---|
| 820 | * This method returns information about the current | 
|---|
| 821 | * stream. | 
|---|
| 822 | * | 
|---|
| 823 | * See the documentation of IStream for more info. | 
|---|
| 824 | */ | 
|---|
| 825 | HRESULT WINAPI StgStreamImpl_Stat( | 
|---|
| 826 | IStream*     iface, | 
|---|
| 827 | STATSTG*       pstatstg,     /* [out] */ | 
|---|
| 828 | DWORD          grfStatFlag)  /* [in] */ | 
|---|
| 829 | { | 
|---|
| 830 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 831 |  | 
|---|
| 832 | StgProperty    curProperty; | 
|---|
| 833 | BOOL         readSucessful; | 
|---|
| 834 |  | 
|---|
| 835 | /* | 
|---|
| 836 | * Read the information from the property. | 
|---|
| 837 | */ | 
|---|
| 838 | readSucessful = StorageImpl_ReadProperty(This->parentStorage->ancestorStorage, | 
|---|
| 839 | This->ownerProperty, | 
|---|
| 840 | &curProperty); | 
|---|
| 841 |  | 
|---|
| 842 | if (readSucessful) | 
|---|
| 843 | { | 
|---|
| 844 | StorageUtl_CopyPropertyToSTATSTG(pstatstg, | 
|---|
| 845 | &curProperty, | 
|---|
| 846 | grfStatFlag); | 
|---|
| 847 |  | 
|---|
| 848 | pstatstg->grfMode = This->grfMode; | 
|---|
| 849 |  | 
|---|
| 850 | return S_OK; | 
|---|
| 851 | } | 
|---|
| 852 |  | 
|---|
| 853 | return E_FAIL; | 
|---|
| 854 | } | 
|---|
| 855 |  | 
|---|
| 856 | /*** | 
|---|
| 857 | * This method is part of the IStream interface. | 
|---|
| 858 | * | 
|---|
| 859 | * This method returns a clone of the interface that allows for | 
|---|
| 860 | * another seek pointer | 
|---|
| 861 | * | 
|---|
| 862 | * See the documentation of IStream for more info. | 
|---|
| 863 | * | 
|---|
| 864 | * I am not totally sure what I am doing here but I presume that this | 
|---|
| 865 | * should be basically as simple as creating a new stream with the same | 
|---|
| 866 | * parent etc and positioning its seek cursor. | 
|---|
| 867 | */ | 
|---|
| 868 | HRESULT WINAPI StgStreamImpl_Clone( | 
|---|
| 869 | IStream*     iface, | 
|---|
| 870 | IStream**    ppstm) /* [out] */ | 
|---|
| 871 | { | 
|---|
| 872 | StgStreamImpl* const This=(StgStreamImpl*)iface; | 
|---|
| 873 | HRESULT hres; | 
|---|
| 874 | StgStreamImpl* new_stream; | 
|---|
| 875 | LARGE_INTEGER seek_pos; | 
|---|
| 876 |  | 
|---|
| 877 | /* | 
|---|
| 878 | * Sanity check | 
|---|
| 879 | */ | 
|---|
| 880 | if ( ppstm == 0 ) | 
|---|
| 881 | return STG_E_INVALIDPOINTER; | 
|---|
| 882 |  | 
|---|
| 883 | new_stream = StgStreamImpl_Construct (This->parentStorage, This->grfMode, This->ownerProperty); | 
|---|
| 884 |  | 
|---|
| 885 | if (!new_stream) | 
|---|
| 886 | return STG_E_INSUFFICIENTMEMORY; /* Currently the only reason for new_stream=0 */ | 
|---|
| 887 |  | 
|---|
| 888 | *ppstm = (IStream*) new_stream; | 
|---|
| 889 | seek_pos.QuadPart = This->currentPosition.QuadPart; | 
|---|
| 890 |  | 
|---|
| 891 | hres=StgStreamImpl_Seek (*ppstm, seek_pos, STREAM_SEEK_SET, NULL); | 
|---|
| 892 |  | 
|---|
| 893 | assert (SUCCEEDED(hres)); | 
|---|
| 894 |  | 
|---|
| 895 | return S_OK; | 
|---|
| 896 | } | 
|---|