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__
41 #include "wine/list.h"
44 * Definitions for the file format offsets.
46 static const ULONG OFFSET_MINORVERSION
= 0x00000018;
47 static const ULONG OFFSET_MAJORVERSION
= 0x0000001a;
48 static const ULONG OFFSET_BYTEORDERMARKER
= 0x0000001c;
49 static const ULONG OFFSET_BIGBLOCKSIZEBITS
= 0x0000001e;
50 static const ULONG OFFSET_SMALLBLOCKSIZEBITS
= 0x00000020;
51 static const ULONG OFFSET_DIRSECTORCOUNT
= 0x00000028;
52 static const ULONG OFFSET_BBDEPOTCOUNT
= 0x0000002C;
53 static const ULONG OFFSET_ROOTSTARTBLOCK
= 0x00000030;
54 static const ULONG OFFSET_SMALLBLOCKLIMIT
= 0x00000038;
55 static const ULONG OFFSET_SBDEPOTSTART
= 0x0000003C;
56 static const ULONG OFFSET_SBDEPOTCOUNT
= 0x00000040;
57 static const ULONG OFFSET_EXTBBDEPOTSTART
= 0x00000044;
58 static const ULONG OFFSET_EXTBBDEPOTCOUNT
= 0x00000048;
59 static const ULONG OFFSET_BBDEPOTSTART
= 0x0000004C;
60 static const ULONG OFFSET_PS_NAME
= 0x00000000;
61 static const ULONG OFFSET_PS_NAMELENGTH
= 0x00000040;
62 static const ULONG OFFSET_PS_STGTYPE
= 0x00000042;
63 static const ULONG OFFSET_PS_LEFTCHILD
= 0x00000044;
64 static const ULONG OFFSET_PS_RIGHTCHILD
= 0x00000048;
65 static const ULONG OFFSET_PS_DIRROOT
= 0x0000004C;
66 static const ULONG OFFSET_PS_GUID
= 0x00000050;
67 static const ULONG OFFSET_PS_CTIMELOW
= 0x00000064;
68 static const ULONG OFFSET_PS_CTIMEHIGH
= 0x00000068;
69 static const ULONG OFFSET_PS_MTIMELOW
= 0x0000006C;
70 static const ULONG OFFSET_PS_MTIMEHIGH
= 0x00000070;
71 static const ULONG OFFSET_PS_STARTBLOCK
= 0x00000074;
72 static const ULONG OFFSET_PS_SIZE
= 0x00000078;
73 static const WORD DEF_BIG_BLOCK_SIZE_BITS
= 0x0009;
74 static const WORD MIN_BIG_BLOCK_SIZE_BITS
= 0x0009;
75 static const WORD MAX_BIG_BLOCK_SIZE_BITS
= 0x000c;
76 static const WORD DEF_SMALL_BLOCK_SIZE_BITS
= 0x0006;
77 static const WORD DEF_BIG_BLOCK_SIZE
= 0x0200;
78 static const WORD DEF_SMALL_BLOCK_SIZE
= 0x0040;
79 static const ULONG BLOCK_EXTBBDEPOT
= 0xFFFFFFFC;
80 static const ULONG BLOCK_SPECIAL
= 0xFFFFFFFD;
81 static const ULONG BLOCK_END_OF_CHAIN
= 0xFFFFFFFE;
82 static const ULONG BLOCK_UNUSED
= 0xFFFFFFFF;
83 static const ULONG DIRENTRY_NULL
= 0xFFFFFFFF;
85 #define DIRENTRY_NAME_MAX_LEN 0x20
86 #define DIRENTRY_NAME_BUFFER_LEN 0x40
88 #define RAW_DIRENTRY_SIZE 0x00000080
90 #define HEADER_SIZE 512
92 #define MIN_BIG_BLOCK_SIZE 0x200
93 #define MAX_BIG_BLOCK_SIZE 0x1000
96 * Type of child entry link
98 #define DIRENTRY_RELATION_PREVIOUS 0
99 #define DIRENTRY_RELATION_NEXT 1
100 #define DIRENTRY_RELATION_DIR 2
103 * type constant used in files for the root storage
105 #define STGTY_ROOT 0x05
107 #define COUNT_BBDEPOTINHEADER 109
109 /* FIXME: This value is stored in the header, but we hard-code it to 0x1000. */
110 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
112 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
113 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
114 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
116 #define STGM_KNOWN_FLAGS (0xf0ff | \
117 STGM_TRANSACTED | STGM_CONVERT | STGM_PRIORITY | STGM_NOSCRATCH | \
118 STGM_NOSNAPSHOT | STGM_DIRECT_SWMR | STGM_DELETEONRELEASE | STGM_SIMPLE)
121 * Forward declarations of all the structures used by the storage
124 typedef struct StorageBaseImpl StorageBaseImpl
;
125 typedef struct StorageBaseImplVtbl StorageBaseImplVtbl
;
126 typedef struct StorageImpl StorageImpl
;
127 typedef struct BlockChainStream BlockChainStream
;
128 typedef struct SmallBlockChainStream SmallBlockChainStream
;
129 typedef struct IEnumSTATSTGImpl IEnumSTATSTGImpl
;
130 typedef struct DirEntry DirEntry
;
131 typedef struct StgStreamImpl StgStreamImpl
;
134 * A reference to a directory entry in the file or a transacted cache.
136 typedef ULONG DirRef
;
139 * This utility structure is used to read/write the information in a directory
144 WCHAR name
[DIRENTRY_NAME_MAX_LEN
];
145 WORD sizeOfNameString
;
157 HRESULT
FileLockBytesImpl_Construct(HANDLE hFile
, DWORD openFlags
, LPCWSTR pwcsName
, ILockBytes
**pLockBytes
);
159 /*************************************************************************
160 * Ole Convert support
163 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage
);
164 HRESULT
OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage
, LPCSTR strOleTypeName
);
167 /****************************************************************************
168 * Storage32BaseImpl definitions.
170 * This structure defines the base information contained in all implementations
171 * of IStorage32 contained in this file storage implementation.
173 * In OOP terms, this is the base class for all the IStorage32 implementations
174 * contained in this file.
176 struct StorageBaseImpl
178 const IStorageVtbl
*lpVtbl
; /* Needs to be the first item in the struct
179 * since we want to cast this in a Storage32 pointer */
181 const IPropertySetStorageVtbl
*pssVtbl
; /* interface for adding a properties stream */
184 * Stream tracking list
187 struct list strmHead
;
190 * Storage tracking list
192 struct list storageHead
;
195 * Reference count of this object
200 * TRUE if this object has been invalidated
205 * Index of the directory entry of this storage
207 DirRef storageDirEntry
;
212 const StorageBaseImplVtbl
*baseVtbl
;
215 * flags that this storage was opened or created with
220 * State bits appear to only be preserved while running. No in the stream
224 BOOL create
; /* Was the storage created or opened.
225 The behaviour of STGM_SIMPLE depends on this */
227 * If this storage was opened in transacted mode, the object that implements
228 * the transacted snapshot or cache.
230 StorageBaseImpl
*transactedChild
;
233 /* virtual methods for StorageBaseImpl objects */
234 struct StorageBaseImplVtbl
{
235 void (*Destroy
)(StorageBaseImpl
*);
236 void (*Invalidate
)(StorageBaseImpl
*);
237 HRESULT (*Flush
)(StorageBaseImpl
*);
238 HRESULT (*GetFilename
)(StorageBaseImpl
*,LPWSTR
*);
239 HRESULT (*CreateDirEntry
)(StorageBaseImpl
*,const DirEntry
*,DirRef
*);
240 HRESULT (*WriteDirEntry
)(StorageBaseImpl
*,DirRef
,const DirEntry
*);
241 HRESULT (*ReadDirEntry
)(StorageBaseImpl
*,DirRef
,DirEntry
*);
242 HRESULT (*DestroyDirEntry
)(StorageBaseImpl
*,DirRef
);
243 HRESULT (*StreamReadAt
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
,ULONG
,void*,ULONG
*);
244 HRESULT (*StreamWriteAt
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
,ULONG
,const void*,ULONG
*);
245 HRESULT (*StreamSetSize
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
);
246 HRESULT (*StreamLink
)(StorageBaseImpl
*,DirRef
,DirRef
);
249 static inline void StorageBaseImpl_Destroy(StorageBaseImpl
*This
)
251 This
->baseVtbl
->Destroy(This
);
254 static inline void StorageBaseImpl_Invalidate(StorageBaseImpl
*This
)
256 This
->baseVtbl
->Invalidate(This
);
259 static inline HRESULT
StorageBaseImpl_Flush(StorageBaseImpl
*This
)
261 return This
->baseVtbl
->Flush(This
);
264 static inline HRESULT
StorageBaseImpl_GetFilename(StorageBaseImpl
*This
, LPWSTR
*result
)
266 return This
->baseVtbl
->GetFilename(This
, result
);
269 static inline HRESULT
StorageBaseImpl_CreateDirEntry(StorageBaseImpl
*This
,
270 const DirEntry
*newData
, DirRef
*index
)
272 return This
->baseVtbl
->CreateDirEntry(This
, newData
, index
);
275 static inline HRESULT
StorageBaseImpl_WriteDirEntry(StorageBaseImpl
*This
,
276 DirRef index
, const DirEntry
*data
)
278 return This
->baseVtbl
->WriteDirEntry(This
, index
, data
);
281 static inline HRESULT
StorageBaseImpl_ReadDirEntry(StorageBaseImpl
*This
,
282 DirRef index
, DirEntry
*data
)
284 return This
->baseVtbl
->ReadDirEntry(This
, index
, data
);
287 static inline HRESULT
StorageBaseImpl_DestroyDirEntry(StorageBaseImpl
*This
,
290 return This
->baseVtbl
->DestroyDirEntry(This
, index
);
293 /* Read up to size bytes from this directory entry's stream at the given offset. */
294 static inline HRESULT
StorageBaseImpl_StreamReadAt(StorageBaseImpl
*This
,
295 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
297 return This
->baseVtbl
->StreamReadAt(This
, index
, offset
, size
, buffer
, bytesRead
);
300 /* Write size bytes to this directory entry's stream at the given offset,
301 * growing the stream if necessary. */
302 static inline HRESULT
StorageBaseImpl_StreamWriteAt(StorageBaseImpl
*This
,
303 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
305 return This
->baseVtbl
->StreamWriteAt(This
, index
, offset
, size
, buffer
, bytesWritten
);
308 static inline HRESULT
StorageBaseImpl_StreamSetSize(StorageBaseImpl
*This
,
309 DirRef index
, ULARGE_INTEGER newsize
)
311 return This
->baseVtbl
->StreamSetSize(This
, index
, newsize
);
314 /* Make dst point to the same stream that src points to. Other stream operations
315 * will not work properly for entries that point to the same stream, so this
316 * must be a very temporary state, and only one entry pointing to a given stream
317 * may be reachable at any given time. */
318 static inline HRESULT
StorageBaseImpl_StreamLink(StorageBaseImpl
*This
,
319 DirRef dst
, DirRef src
)
321 return This
->baseVtbl
->StreamLink(This
, dst
, src
);
324 /****************************************************************************
325 * StorageBaseImpl stream list handlers
328 void StorageBaseImpl_AddStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
);
329 void StorageBaseImpl_RemoveStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
);
331 /* Number of BlockChainStream objects to cache in a StorageImpl */
332 #define BLOCKCHAIN_CACHE_SIZE 4
334 /****************************************************************************
335 * Storage32Impl definitions.
337 * This implementation of the IStorage32 interface represents a root
338 * storage. Basically, a document file.
342 struct StorageBaseImpl base
;
347 WORD bigBlockSizeBits
;
348 WORD smallBlockSizeBits
;
350 ULONG smallBlockSize
;
351 ULONG bigBlockDepotCount
;
352 ULONG rootStartBlock
;
353 ULONG smallBlockLimit
;
354 ULONG smallBlockDepotStart
;
355 ULONG extBigBlockDepotStart
;
356 ULONG
*extBigBlockDepotLocations
;
357 ULONG extBigBlockDepotLocationsSize
;
358 ULONG extBigBlockDepotCount
;
359 ULONG bigBlockDepotStart
[COUNT_BBDEPOTINHEADER
];
361 ULONG extBlockDepotCached
[MAX_BIG_BLOCK_SIZE
/ 4];
362 ULONG indexExtBlockDepotCached
;
364 ULONG blockDepotCached
[MAX_BIG_BLOCK_SIZE
/ 4];
365 ULONG indexBlockDepotCached
;
368 /* All small blocks before this one are known to be in use. */
369 ULONG firstFreeSmallBlock
;
372 * Abstraction of the big block chains for the chains of the header.
374 BlockChainStream
* rootBlockChain
;
375 BlockChainStream
* smallBlockDepotChain
;
376 BlockChainStream
* smallBlockRootChain
;
378 /* Cache of block chain streams objects for directory entries */
379 BlockChainStream
* blockChainCache
[BLOCKCHAIN_CACHE_SIZE
];
380 UINT blockChainToEvict
;
382 ILockBytes
* lockBytes
;
385 HRESULT
StorageImpl_ReadRawDirEntry(
390 void UpdateRawDirEntry(
392 const DirEntry
*newData
);
394 HRESULT
StorageImpl_WriteRawDirEntry(
399 HRESULT
StorageImpl_ReadDirEntry(
404 HRESULT
StorageImpl_WriteDirEntry(
407 const DirEntry
* buffer
);
409 BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
411 SmallBlockChainStream
** ppsbChain
);
413 SmallBlockChainStream
* Storage32Impl_BigBlocksToSmallBlocks(
415 BlockChainStream
** ppbbChain
,
416 ULARGE_INTEGER newSize
);
418 /****************************************************************************
419 * StgStreamImpl definitions.
421 * This class implements the IStream32 interface and represents a stream
422 * located inside a storage object.
426 const IStreamVtbl
*lpVtbl
; /* Needs to be the first item in the struct
427 * since we want to cast this to an IStream pointer */
430 * We are an entry in the storage object's stream handler list
433 struct list StrmListEntry
;
441 * Storage that is the parent(owner) of the stream
443 StorageBaseImpl
* parentStorage
;
446 * Access mode of this stream.
451 * Index of the directory entry that owns (points to) this stream.
456 * This is the current position of the cursor in the stream
458 ULARGE_INTEGER currentPosition
;
462 * Method definition for the StgStreamImpl class.
464 StgStreamImpl
* StgStreamImpl_Construct(
465 StorageBaseImpl
* parentStorage
,
470 /******************************************************************************
471 * Endian conversion macros
473 #ifdef WORDS_BIGENDIAN
475 #define htole32(x) RtlUlongByteSwap(x)
476 #define htole16(x) RtlUshortByteSwap(x)
477 #define lendian32toh(x) RtlUlongByteSwap(x)
478 #define lendian16toh(x) RtlUshortByteSwap(x)
482 #define htole32(x) (x)
483 #define htole16(x) (x)
484 #define lendian32toh(x) (x)
485 #define lendian16toh(x) (x)
489 /******************************************************************************
490 * The StorageUtl_ functions are miscellaneous utility functions. Most of which
491 * are abstractions used to read values from file buffers without having to
492 * worry about bit order
494 void StorageUtl_ReadWord(const BYTE
* buffer
, ULONG offset
, WORD
* value
);
495 void StorageUtl_WriteWord(BYTE
* buffer
, ULONG offset
, WORD value
);
496 void StorageUtl_ReadDWord(const BYTE
* buffer
, ULONG offset
, DWORD
* value
);
497 void StorageUtl_WriteDWord(BYTE
* buffer
, ULONG offset
, DWORD value
);
498 void StorageUtl_ReadULargeInteger(const BYTE
* buffer
, ULONG offset
,
499 ULARGE_INTEGER
* value
);
500 void StorageUtl_WriteULargeInteger(BYTE
* buffer
, ULONG offset
,
501 const ULARGE_INTEGER
*value
);
502 void StorageUtl_ReadGUID(const BYTE
* buffer
, ULONG offset
, GUID
* value
);
503 void StorageUtl_WriteGUID(BYTE
* buffer
, ULONG offset
, const GUID
* value
);
504 void StorageUtl_CopyDirEntryToSTATSTG(StorageBaseImpl
*storage
,STATSTG
* destination
,
505 const DirEntry
* source
, int statFlags
);
507 /****************************************************************************
508 * BlockChainStream definitions.
510 * The BlockChainStream class is a utility class that is used to create an
511 * abstraction of the big block chains in the storage file.
515 /* This represents a range of blocks that happen reside in consecutive sectors. */
521 typedef struct BlockChainBlock
527 BYTE data
[MAX_BIG_BLOCK_SIZE
];
530 struct BlockChainStream
532 StorageImpl
* parentStorage
;
533 ULONG
* headOfStreamPlaceHolder
;
534 DirRef ownerDirEntry
;
535 struct BlockChainRun
* indexCache
;
537 ULONG indexCacheSize
;
538 BlockChainBlock cachedBlocks
[2];
545 * Methods for the BlockChainStream class.
547 BlockChainStream
* BlockChainStream_Construct(
548 StorageImpl
* parentStorage
,
549 ULONG
* headOfStreamPlaceHolder
,
552 void BlockChainStream_Destroy(
553 BlockChainStream
* This
);
555 HRESULT
BlockChainStream_ReadAt(
556 BlockChainStream
* This
,
557 ULARGE_INTEGER offset
,
562 HRESULT
BlockChainStream_WriteAt(
563 BlockChainStream
* This
,
564 ULARGE_INTEGER offset
,
567 ULONG
* bytesWritten
);
569 BOOL
BlockChainStream_SetSize(
570 BlockChainStream
* This
,
571 ULARGE_INTEGER newSize
);
573 HRESULT
BlockChainStream_Flush(
574 BlockChainStream
* This
);
576 /****************************************************************************
577 * SmallBlockChainStream definitions.
579 * The SmallBlockChainStream class is a utility class that is used to create an
580 * abstraction of the small block chains in the storage file.
582 struct SmallBlockChainStream
584 StorageImpl
* parentStorage
;
585 DirRef ownerDirEntry
;
586 ULONG
* headOfStreamPlaceHolder
;
590 * Methods of the SmallBlockChainStream class.
592 SmallBlockChainStream
* SmallBlockChainStream_Construct(
593 StorageImpl
* parentStorage
,
594 ULONG
* headOfStreamPlaceHolder
,
597 void SmallBlockChainStream_Destroy(
598 SmallBlockChainStream
* This
);
600 HRESULT
SmallBlockChainStream_ReadAt(
601 SmallBlockChainStream
* This
,
602 ULARGE_INTEGER offset
,
607 HRESULT
SmallBlockChainStream_WriteAt(
608 SmallBlockChainStream
* This
,
609 ULARGE_INTEGER offset
,
612 ULONG
* bytesWritten
);
614 BOOL
SmallBlockChainStream_SetSize(
615 SmallBlockChainStream
* This
,
616 ULARGE_INTEGER newSize
);
619 #endif /* __STORAGE32_H__ */