2 * Compound Storage (32 bit version)
4 * Implemented using the documentation of the LAOLA project at
5 * <URL:http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/index.html>
6 * (Thanks to Martin Schwartz <schwartz@cs.tu-berlin.de>)
8 * This include file contains definitions of types and function
9 * prototypes that are used in the many files implementing the
10 * storage functionality
12 * Copyright 1998,1999 Francis Beaudet
13 * Copyright 1998,1999 Thuy Nguyen
14 * Copyright 2010 Vincent Povirk for CodeWeavers
16 * This library is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Lesser General Public
18 * License as published by the Free Software Foundation; either
19 * version 2.1 of the License, or (at your option) any later version.
21 * This library is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 * Lesser General Public License for more details.
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this library; if not, write to the Free Software
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #ifndef __STORAGE32_H__
31 #define __STORAGE32_H__
34 * Definitions for the file format offsets.
36 static const ULONG OFFSET_MINORVERSION
= 0x00000018;
37 static const ULONG OFFSET_MAJORVERSION
= 0x0000001a;
38 static const ULONG OFFSET_BYTEORDERMARKER
= 0x0000001c;
39 static const ULONG OFFSET_BIGBLOCKSIZEBITS
= 0x0000001e;
40 static const ULONG OFFSET_SMALLBLOCKSIZEBITS
= 0x00000020;
41 static const ULONG OFFSET_DIRSECTORCOUNT
= 0x00000028;
42 static const ULONG OFFSET_BBDEPOTCOUNT
= 0x0000002C;
43 static const ULONG OFFSET_ROOTSTARTBLOCK
= 0x00000030;
44 static const ULONG OFFSET_SMALLBLOCKLIMIT
= 0x00000038;
45 static const ULONG OFFSET_SBDEPOTSTART
= 0x0000003C;
46 static const ULONG OFFSET_SBDEPOTCOUNT
= 0x00000040;
47 static const ULONG OFFSET_EXTBBDEPOTSTART
= 0x00000044;
48 static const ULONG OFFSET_EXTBBDEPOTCOUNT
= 0x00000048;
49 static const ULONG OFFSET_BBDEPOTSTART
= 0x0000004C;
50 static const ULONG OFFSET_PS_NAME
= 0x00000000;
51 static const ULONG OFFSET_PS_NAMELENGTH
= 0x00000040;
52 static const ULONG OFFSET_PS_STGTYPE
= 0x00000042;
53 static const ULONG OFFSET_PS_LEFTCHILD
= 0x00000044;
54 static const ULONG OFFSET_PS_RIGHTCHILD
= 0x00000048;
55 static const ULONG OFFSET_PS_DIRROOT
= 0x0000004C;
56 static const ULONG OFFSET_PS_GUID
= 0x00000050;
57 static const ULONG OFFSET_PS_CTIMELOW
= 0x00000064;
58 static const ULONG OFFSET_PS_CTIMEHIGH
= 0x00000068;
59 static const ULONG OFFSET_PS_MTIMELOW
= 0x0000006C;
60 static const ULONG OFFSET_PS_MTIMEHIGH
= 0x00000070;
61 static const ULONG OFFSET_PS_STARTBLOCK
= 0x00000074;
62 static const ULONG OFFSET_PS_SIZE
= 0x00000078;
63 static const WORD DEF_BIG_BLOCK_SIZE_BITS
= 0x0009;
64 static const WORD MIN_BIG_BLOCK_SIZE_BITS
= 0x0009;
65 static const WORD MAX_BIG_BLOCK_SIZE_BITS
= 0x000c;
66 static const WORD DEF_SMALL_BLOCK_SIZE_BITS
= 0x0006;
67 static const WORD DEF_BIG_BLOCK_SIZE
= 0x0200;
68 static const WORD DEF_SMALL_BLOCK_SIZE
= 0x0040;
69 static const ULONG BLOCK_EXTBBDEPOT
= 0xFFFFFFFC;
70 static const ULONG BLOCK_SPECIAL
= 0xFFFFFFFD;
71 static const ULONG BLOCK_END_OF_CHAIN
= 0xFFFFFFFE;
72 static const ULONG BLOCK_UNUSED
= 0xFFFFFFFF;
73 static const ULONG DIRENTRY_NULL
= 0xFFFFFFFF;
75 #define DIRENTRY_NAME_MAX_LEN 0x20
76 #define DIRENTRY_NAME_BUFFER_LEN 0x40
78 #define RAW_DIRENTRY_SIZE 0x00000080
80 #define HEADER_SIZE 512
82 #define MIN_BIG_BLOCK_SIZE 0x200
83 #define MAX_BIG_BLOCK_SIZE 0x1000
86 * Type of child entry link
88 #define DIRENTRY_RELATION_PREVIOUS 0
89 #define DIRENTRY_RELATION_NEXT 1
90 #define DIRENTRY_RELATION_DIR 2
93 * type constant used in files for the root storage
95 #define STGTY_ROOT 0x05
97 #define COUNT_BBDEPOTINHEADER 109
99 /* FIXME: This value is stored in the header, but we hard-code it to 0x1000. */
100 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
102 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
103 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
104 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
106 #define STGM_KNOWN_FLAGS (0xf0ff | \
107 STGM_TRANSACTED | STGM_CONVERT | STGM_PRIORITY | STGM_NOSCRATCH | \
108 STGM_NOSNAPSHOT | STGM_DIRECT_SWMR | STGM_DELETEONRELEASE | STGM_SIMPLE)
111 * Forward declarations of all the structures used by the storage
114 typedef struct StorageBaseImpl StorageBaseImpl
;
115 typedef struct StorageBaseImplVtbl StorageBaseImplVtbl
;
116 typedef struct StorageImpl StorageImpl
;
117 typedef struct BlockChainStream BlockChainStream
;
118 typedef struct SmallBlockChainStream SmallBlockChainStream
;
119 typedef struct IEnumSTATSTGImpl IEnumSTATSTGImpl
;
120 typedef struct DirEntry DirEntry
;
121 typedef struct StgStreamImpl StgStreamImpl
;
124 * A reference to a directory entry in the file or a transacted cache.
126 typedef ULONG DirRef
;
129 * This utility structure is used to read/write the information in a directory
134 WCHAR name
[DIRENTRY_NAME_MAX_LEN
];
135 WORD sizeOfNameString
;
147 HRESULT
FileLockBytesImpl_Construct(HANDLE hFile
, DWORD openFlags
, LPCWSTR pwcsName
, ILockBytes
**pLockBytes
) DECLSPEC_HIDDEN
;
149 /*************************************************************************
150 * Ole Convert support
153 HRESULT
STORAGE_CreateOleStream(IStorage
*, DWORD
) DECLSPEC_HIDDEN
;
154 HRESULT
OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage
, LPCSTR strOleTypeName
) DECLSPEC_HIDDEN
;
163 /****************************************************************************
164 * StorageBaseImpl definitions.
166 * This structure defines the base information contained in all implementations
167 * of IStorage contained in this file storage implementation.
169 * In OOP terms, this is the base class for all the IStorage implementations
170 * contained in this file.
172 struct StorageBaseImpl
174 IStorage IStorage_iface
;
175 IPropertySetStorage IPropertySetStorage_iface
; /* interface for adding a properties stream */
176 IDirectWriterLock IDirectWriterLock_iface
;
180 * Stream tracking list
183 struct list strmHead
;
186 * Storage tracking list
188 struct list storageHead
;
191 * TRUE if this object has been invalidated
196 * Index of the directory entry of this storage
198 DirRef storageDirEntry
;
203 const StorageBaseImplVtbl
*baseVtbl
;
206 * flags that this storage was opened or created with
211 * State bits appear to only be preserved while running. No in the stream
215 BOOL create
; /* Was the storage created or opened.
216 The behaviour of STGM_SIMPLE depends on this */
218 * If this storage was opened in transacted mode, the object that implements
219 * the transacted snapshot or cache.
221 StorageBaseImpl
*transactedChild
;
222 enum swmr_mode lockingrole
;
225 /* virtual methods for StorageBaseImpl objects */
226 struct StorageBaseImplVtbl
{
227 void (*Destroy
)(StorageBaseImpl
*);
228 void (*Invalidate
)(StorageBaseImpl
*);
229 HRESULT (*Flush
)(StorageBaseImpl
*);
230 HRESULT (*GetFilename
)(StorageBaseImpl
*,LPWSTR
*);
231 HRESULT (*CreateDirEntry
)(StorageBaseImpl
*,const DirEntry
*,DirRef
*);
232 HRESULT (*WriteDirEntry
)(StorageBaseImpl
*,DirRef
,const DirEntry
*);
233 HRESULT (*ReadDirEntry
)(StorageBaseImpl
*,DirRef
,DirEntry
*);
234 HRESULT (*DestroyDirEntry
)(StorageBaseImpl
*,DirRef
);
235 HRESULT (*StreamReadAt
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
,ULONG
,void*,ULONG
*);
236 HRESULT (*StreamWriteAt
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
,ULONG
,const void*,ULONG
*);
237 HRESULT (*StreamSetSize
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
);
238 HRESULT (*StreamLink
)(StorageBaseImpl
*,DirRef
,DirRef
);
241 static inline void StorageBaseImpl_Destroy(StorageBaseImpl
*This
)
243 This
->baseVtbl
->Destroy(This
);
246 static inline void StorageBaseImpl_Invalidate(StorageBaseImpl
*This
)
248 This
->baseVtbl
->Invalidate(This
);
251 static inline HRESULT
StorageBaseImpl_Flush(StorageBaseImpl
*This
)
253 return This
->baseVtbl
->Flush(This
);
256 static inline HRESULT
StorageBaseImpl_GetFilename(StorageBaseImpl
*This
, LPWSTR
*result
)
258 return This
->baseVtbl
->GetFilename(This
, result
);
261 static inline HRESULT
StorageBaseImpl_CreateDirEntry(StorageBaseImpl
*This
,
262 const DirEntry
*newData
, DirRef
*index
)
264 return This
->baseVtbl
->CreateDirEntry(This
, newData
, index
);
267 static inline HRESULT
StorageBaseImpl_WriteDirEntry(StorageBaseImpl
*This
,
268 DirRef index
, const DirEntry
*data
)
270 return This
->baseVtbl
->WriteDirEntry(This
, index
, data
);
273 static inline HRESULT
StorageBaseImpl_ReadDirEntry(StorageBaseImpl
*This
,
274 DirRef index
, DirEntry
*data
)
276 return This
->baseVtbl
->ReadDirEntry(This
, index
, data
);
279 static inline HRESULT
StorageBaseImpl_DestroyDirEntry(StorageBaseImpl
*This
,
282 return This
->baseVtbl
->DestroyDirEntry(This
, index
);
285 /* Read up to size bytes from this directory entry's stream at the given offset. */
286 static inline HRESULT
StorageBaseImpl_StreamReadAt(StorageBaseImpl
*This
,
287 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
289 return This
->baseVtbl
->StreamReadAt(This
, index
, offset
, size
, buffer
, bytesRead
);
292 /* Write size bytes to this directory entry's stream at the given offset,
293 * growing the stream if necessary. */
294 static inline HRESULT
StorageBaseImpl_StreamWriteAt(StorageBaseImpl
*This
,
295 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
297 return This
->baseVtbl
->StreamWriteAt(This
, index
, offset
, size
, buffer
, bytesWritten
);
300 static inline HRESULT
StorageBaseImpl_StreamSetSize(StorageBaseImpl
*This
,
301 DirRef index
, ULARGE_INTEGER newsize
)
303 return This
->baseVtbl
->StreamSetSize(This
, index
, newsize
);
306 /* Make dst point to the same stream that src points to. Other stream operations
307 * will not work properly for entries that point to the same stream, so this
308 * must be a very temporary state, and only one entry pointing to a given stream
309 * may be reachable at any given time. */
310 static inline HRESULT
StorageBaseImpl_StreamLink(StorageBaseImpl
*This
,
311 DirRef dst
, DirRef src
)
313 return This
->baseVtbl
->StreamLink(This
, dst
, src
);
316 /****************************************************************************
317 * StorageBaseImpl stream list handlers
320 void StorageBaseImpl_AddStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
) DECLSPEC_HIDDEN
;
321 void StorageBaseImpl_RemoveStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
) DECLSPEC_HIDDEN
;
323 /* Number of BlockChainStream objects to cache in a StorageImpl */
324 #define BLOCKCHAIN_CACHE_SIZE 4
326 /****************************************************************************
327 * Storage32Impl definitions.
329 * This implementation of the IStorage32 interface represents a root
330 * storage. Basically, a document file.
334 struct StorageBaseImpl base
;
339 WORD bigBlockSizeBits
;
340 WORD smallBlockSizeBits
;
342 ULONG smallBlockSize
;
343 ULONG bigBlockDepotCount
;
344 ULONG rootStartBlock
;
345 ULONG smallBlockLimit
;
346 ULONG smallBlockDepotStart
;
347 ULONG extBigBlockDepotStart
;
348 ULONG
*extBigBlockDepotLocations
;
349 ULONG extBigBlockDepotLocationsSize
;
350 ULONG extBigBlockDepotCount
;
351 ULONG bigBlockDepotStart
[COUNT_BBDEPOTINHEADER
];
353 ULONG extBlockDepotCached
[MAX_BIG_BLOCK_SIZE
/ 4];
354 ULONG indexExtBlockDepotCached
;
356 ULONG blockDepotCached
[MAX_BIG_BLOCK_SIZE
/ 4];
357 ULONG indexBlockDepotCached
;
360 /* All small blocks before this one are known to be in use. */
361 ULONG firstFreeSmallBlock
;
364 * Abstraction of the big block chains for the chains of the header.
366 BlockChainStream
* rootBlockChain
;
367 BlockChainStream
* smallBlockDepotChain
;
368 BlockChainStream
* smallBlockRootChain
;
370 /* Cache of block chain streams objects for directory entries */
371 BlockChainStream
* blockChainCache
[BLOCKCHAIN_CACHE_SIZE
];
372 UINT blockChainToEvict
;
374 ILockBytes
* lockBytes
;
377 HRESULT
StorageImpl_ReadRawDirEntry(
380 BYTE
*buffer
) DECLSPEC_HIDDEN
;
382 void UpdateRawDirEntry(
384 const DirEntry
*newData
) DECLSPEC_HIDDEN
;
386 HRESULT
StorageImpl_WriteRawDirEntry(
389 const BYTE
*buffer
) DECLSPEC_HIDDEN
;
391 HRESULT
StorageImpl_ReadDirEntry(
394 DirEntry
* buffer
) DECLSPEC_HIDDEN
;
396 HRESULT
StorageImpl_WriteDirEntry(
399 const DirEntry
* buffer
) DECLSPEC_HIDDEN
;
401 BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
403 SmallBlockChainStream
** ppsbChain
) DECLSPEC_HIDDEN
;
405 SmallBlockChainStream
* Storage32Impl_BigBlocksToSmallBlocks(
407 BlockChainStream
** ppbbChain
,
408 ULARGE_INTEGER newSize
) DECLSPEC_HIDDEN
;
410 /****************************************************************************
411 * StgStreamImpl definitions.
413 * This class implements the IStream interface and represents a stream
414 * located inside a storage object.
418 IStream IStream_iface
;
422 * We are an entry in the storage object's stream handler list
424 struct list StrmListEntry
;
427 * Storage that is the parent(owner) of the stream
429 StorageBaseImpl
* parentStorage
;
432 * Access mode of this stream.
437 * Index of the directory entry that owns (points to) this stream.
442 * This is the current position of the cursor in the stream
444 ULARGE_INTEGER currentPosition
;
447 static inline StgStreamImpl
*impl_from_IStream( IStream
*iface
)
449 return CONTAINING_RECORD(iface
, StgStreamImpl
, IStream_iface
);
453 * Method definition for the StgStreamImpl class.
455 StgStreamImpl
* StgStreamImpl_Construct(
456 StorageBaseImpl
* parentStorage
,
458 DirRef dirEntry
) DECLSPEC_HIDDEN
;
461 /******************************************************************************
462 * Endian conversion macros
464 #ifdef WORDS_BIGENDIAN
466 #define htole32(x) RtlUlongByteSwap(x)
467 #define htole16(x) RtlUshortByteSwap(x)
468 #define lendian32toh(x) RtlUlongByteSwap(x)
469 #define lendian16toh(x) RtlUshortByteSwap(x)
473 #define htole32(x) (x)
474 #define htole16(x) (x)
475 #define lendian32toh(x) (x)
476 #define lendian16toh(x) (x)
480 /******************************************************************************
481 * The StorageUtl_ functions are miscellaneous utility functions. Most of which
482 * are abstractions used to read values from file buffers without having to
483 * worry about bit order
485 void StorageUtl_ReadWord(const BYTE
* buffer
, ULONG offset
, WORD
* value
) DECLSPEC_HIDDEN
;
486 void StorageUtl_WriteWord(BYTE
* buffer
, ULONG offset
, WORD value
) DECLSPEC_HIDDEN
;
487 void StorageUtl_ReadDWord(const BYTE
* buffer
, ULONG offset
, DWORD
* value
) DECLSPEC_HIDDEN
;
488 void StorageUtl_WriteDWord(BYTE
* buffer
, ULONG offset
, DWORD value
) DECLSPEC_HIDDEN
;
489 void StorageUtl_ReadULargeInteger(const BYTE
* buffer
, ULONG offset
,
490 ULARGE_INTEGER
* value
) DECLSPEC_HIDDEN
;
491 void StorageUtl_WriteULargeInteger(BYTE
* buffer
, ULONG offset
,
492 const ULARGE_INTEGER
*value
) DECLSPEC_HIDDEN
;
493 void StorageUtl_ReadGUID(const BYTE
* buffer
, ULONG offset
, GUID
* value
) DECLSPEC_HIDDEN
;
494 void StorageUtl_WriteGUID(BYTE
* buffer
, ULONG offset
, const GUID
* value
) DECLSPEC_HIDDEN
;
495 void StorageUtl_CopyDirEntryToSTATSTG(StorageBaseImpl
*storage
,STATSTG
* destination
,
496 const DirEntry
* source
, int statFlags
) DECLSPEC_HIDDEN
;
498 /****************************************************************************
499 * BlockChainStream definitions.
501 * The BlockChainStream class is a utility class that is used to create an
502 * abstraction of the big block chains in the storage file.
506 /* This represents a range of blocks that happen reside in consecutive sectors. */
512 typedef struct BlockChainBlock
518 BYTE data
[MAX_BIG_BLOCK_SIZE
];
521 struct BlockChainStream
523 StorageImpl
* parentStorage
;
524 ULONG
* headOfStreamPlaceHolder
;
525 DirRef ownerDirEntry
;
526 struct BlockChainRun
* indexCache
;
528 ULONG indexCacheSize
;
529 BlockChainBlock cachedBlocks
[2];
536 * Methods for the BlockChainStream class.
538 BlockChainStream
* BlockChainStream_Construct(
539 StorageImpl
* parentStorage
,
540 ULONG
* headOfStreamPlaceHolder
,
541 DirRef dirEntry
) DECLSPEC_HIDDEN
;
543 void BlockChainStream_Destroy(
544 BlockChainStream
* This
) DECLSPEC_HIDDEN
;
546 HRESULT
BlockChainStream_ReadAt(
547 BlockChainStream
* This
,
548 ULARGE_INTEGER offset
,
551 ULONG
* bytesRead
) DECLSPEC_HIDDEN
;
553 HRESULT
BlockChainStream_WriteAt(
554 BlockChainStream
* This
,
555 ULARGE_INTEGER offset
,
558 ULONG
* bytesWritten
) DECLSPEC_HIDDEN
;
560 BOOL
BlockChainStream_SetSize(
561 BlockChainStream
* This
,
562 ULARGE_INTEGER newSize
) DECLSPEC_HIDDEN
;
564 HRESULT
BlockChainStream_Flush(
565 BlockChainStream
* This
) DECLSPEC_HIDDEN
;
567 /****************************************************************************
568 * SmallBlockChainStream definitions.
570 * The SmallBlockChainStream class is a utility class that is used to create an
571 * abstraction of the small block chains in the storage file.
573 struct SmallBlockChainStream
575 StorageImpl
* parentStorage
;
576 DirRef ownerDirEntry
;
577 ULONG
* headOfStreamPlaceHolder
;
581 * Methods of the SmallBlockChainStream class.
583 SmallBlockChainStream
* SmallBlockChainStream_Construct(
584 StorageImpl
* parentStorage
,
585 ULONG
* headOfStreamPlaceHolder
,
586 DirRef dirEntry
) DECLSPEC_HIDDEN
;
588 void SmallBlockChainStream_Destroy(
589 SmallBlockChainStream
* This
) DECLSPEC_HIDDEN
;
591 HRESULT
SmallBlockChainStream_ReadAt(
592 SmallBlockChainStream
* This
,
593 ULARGE_INTEGER offset
,
596 ULONG
* bytesRead
) DECLSPEC_HIDDEN
;
598 HRESULT
SmallBlockChainStream_WriteAt(
599 SmallBlockChainStream
* This
,
600 ULARGE_INTEGER offset
,
603 ULONG
* bytesWritten
) DECLSPEC_HIDDEN
;
605 BOOL
SmallBlockChainStream_SetSize(
606 SmallBlockChainStream
* This
,
607 ULARGE_INTEGER newSize
) DECLSPEC_HIDDEN
;
610 #endif /* __STORAGE32_H__ */