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
15 * This library is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Lesser General Public
17 * License as published by the Free Software Foundation; either
18 * version 2.1 of the License, or (at your option) any later version.
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * Lesser General Public License for more details.
25 * You should have received a copy of the GNU Lesser General Public
26 * License along with this library; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifndef __STORAGE32_H__
30 #define __STORAGE32_H__
42 * Definitions for the file format offsets.
44 static const ULONG OFFSET_BIGBLOCKSIZEBITS
= 0x0000001e;
45 static const ULONG OFFSET_SMALLBLOCKSIZEBITS
= 0x00000020;
46 static const ULONG OFFSET_BBDEPOTCOUNT
= 0x0000002C;
47 static const ULONG OFFSET_ROOTSTARTBLOCK
= 0x00000030;
48 static const ULONG OFFSET_SBDEPOTSTART
= 0x0000003C;
49 static const ULONG OFFSET_SBDEPOTCOUNT
= 0x00000040;
50 static const ULONG OFFSET_EXTBBDEPOTSTART
= 0x00000044;
51 static const ULONG OFFSET_EXTBBDEPOTCOUNT
= 0x00000048;
52 static const ULONG OFFSET_BBDEPOTSTART
= 0x0000004C;
53 static const ULONG OFFSET_PS_NAME
= 0x00000000;
54 static const ULONG OFFSET_PS_NAMELENGTH
= 0x00000040;
55 static const ULONG OFFSET_PS_PROPERTYTYPE
= 0x00000042;
56 static const ULONG OFFSET_PS_PREVIOUSPROP
= 0x00000044;
57 static const ULONG OFFSET_PS_NEXTPROP
= 0x00000048;
58 static const ULONG OFFSET_PS_DIRPROP
= 0x0000004C;
59 static const ULONG OFFSET_PS_GUID
= 0x00000050;
60 static const ULONG OFFSET_PS_TSS1
= 0x00000064;
61 static const ULONG OFFSET_PS_TSD1
= 0x00000068;
62 static const ULONG OFFSET_PS_TSS2
= 0x0000006C;
63 static const ULONG OFFSET_PS_TSD2
= 0x00000070;
64 static const ULONG OFFSET_PS_STARTBLOCK
= 0x00000074;
65 static const ULONG OFFSET_PS_SIZE
= 0x00000078;
66 static const WORD DEF_BIG_BLOCK_SIZE_BITS
= 0x0009;
67 static const WORD DEF_SMALL_BLOCK_SIZE_BITS
= 0x0006;
68 static const WORD DEF_BIG_BLOCK_SIZE
= 0x0200;
69 static const WORD DEF_SMALL_BLOCK_SIZE
= 0x0040;
70 static const ULONG BLOCK_EXTBBDEPOT
= 0xFFFFFFFC;
71 static const ULONG BLOCK_SPECIAL
= 0xFFFFFFFD;
72 static const ULONG BLOCK_END_OF_CHAIN
= 0xFFFFFFFE;
73 static const ULONG BLOCK_UNUSED
= 0xFFFFFFFF;
74 static const ULONG PROPERTY_NULL
= 0xFFFFFFFF;
76 #define PROPERTY_NAME_MAX_LEN 0x20
77 #define PROPERTY_NAME_BUFFER_LEN 0x40
79 #define PROPSET_BLOCK_SIZE 0x00000080
82 * Property type of relation
84 #define PROPERTY_RELATION_PREVIOUS 0
85 #define PROPERTY_RELATION_NEXT 1
86 #define PROPERTY_RELATION_DIR 2
89 * Property type constants
91 #define PROPTYPE_STORAGE 0x01
92 #define PROPTYPE_STREAM 0x02
93 #define PROPTYPE_ROOT 0x05
96 * These defines assume a hardcoded blocksize. The code will assert
97 * if the blocksize is different. Some changes will have to be done if it
100 #define BIG_BLOCK_SIZE 0x200
101 #define COUNT_BBDEPOTINHEADER 109
102 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
103 #define NUM_BLOCKS_PER_DEPOT_BLOCK 128
105 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
106 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
107 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
109 #define STGM_KNOWN_FLAGS (0xf0ff | \
110 STGM_TRANSACTED | STGM_CONVERT | STGM_PRIORITY | STGM_NOSCRATCH | \
111 STGM_NOSNAPSHOT | STGM_DIRECT_SWMR | STGM_DELETEONRELEASE | STGM_SIMPLE)
114 * These are signatures to detect the type of Document file.
116 static const BYTE STORAGE_magic
[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
117 static const BYTE STORAGE_oldmagic
[8] ={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
120 * Forward declarations of all the structures used by the storage
123 typedef struct StorageBaseImpl StorageBaseImpl
;
124 typedef struct StorageImpl StorageImpl
;
125 typedef struct StorageInternalImpl StorageInternalImpl
;
126 typedef struct BlockChainStream BlockChainStream
;
127 typedef struct SmallBlockChainStream SmallBlockChainStream
;
128 typedef struct IEnumSTATSTGImpl IEnumSTATSTGImpl
;
129 typedef struct StgProperty StgProperty
;
130 typedef struct StgStreamImpl StgStreamImpl
;
133 * This utility structure is used to read/write the information in a storage
138 WCHAR name
[PROPERTY_NAME_MAX_LEN
];
139 WORD sizeOfNameString
;
141 ULONG previousProperty
;
144 GUID propertyUniqueID
;
153 /*************************************************************************
154 * Big Block File support
156 * The big block file is an abstraction of a flat file separated in
157 * same sized blocks. The implementation for the methods described in
158 * this section appear in stg_bigblockfile.c
162 * Declaration of the data structures
164 typedef struct BigBlockFile BigBlockFile
,*LPBIGBLOCKFILE
;
165 typedef struct MappedPage MappedPage
,*LPMAPPEDPAGE
;
170 ULARGE_INTEGER filesize
;
176 MappedPage
*victimhead
, *victimtail
;
177 ULONG num_victim_pages
;
184 * Declaration of the functions used to manipulate the BigBlockFile
187 BigBlockFile
* BIGBLOCKFILE_Construct(HANDLE hFile
,
192 void BIGBLOCKFILE_Destructor(LPBIGBLOCKFILE This
);
193 void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This
, ULONG index
);
194 void* BIGBLOCKFILE_GetROBigBlock(LPBIGBLOCKFILE This
, ULONG index
);
195 void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This
, void *pBlock
);
196 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
);
197 ULARGE_INTEGER
BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This
);
199 /*************************************************************************
200 * Ole Convert support
203 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage
);
204 HRESULT
OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage
, LPCSTR strOleTypeName
);
206 /****************************************************************************
207 * Storage32BaseImpl definitions.
209 * This structure defines the base information contained in all implementations
210 * of IStorage32 contained in this file storage implementation.
212 * In OOP terms, this is the base class for all the IStorage32 implementations
213 * contained in this file.
215 struct StorageBaseImpl
217 IStorageVtbl
*lpVtbl
; /* Needs to be the first item in the struct
218 * since we want to cast this in a Storage32 pointer */
220 IPropertySetStorageVtbl
*pssVtbl
; /* interface for adding a properties stream */
223 * Reference count of this object
228 * Ancestor storage (top level)
230 StorageImpl
* ancestorStorage
;
233 * Index of the property for the root of
236 ULONG rootPropertySetIndex
;
239 * virtual Destructor method.
241 void (*v_destructor
)(StorageBaseImpl
*);
245 /****************************************************************************
246 * Storage32Impl definitions.
248 * This implementation of the IStorage32 interface represents a root
249 * storage. Basically, a document file.
253 struct StorageBaseImpl base
;
256 * The following data members are specific to the Storage32Impl
259 HANDLE hFile
; /* Physical support for the Docfile */
260 LPOLESTR pwcsName
; /* Full path of the document file */
262 /* FIXME: should this be in Storage32BaseImpl ? */
263 WCHAR filename
[PROPERTY_NAME_BUFFER_LEN
];
268 WORD bigBlockSizeBits
;
269 WORD smallBlockSizeBits
;
271 ULONG smallBlockSize
;
272 ULONG bigBlockDepotCount
;
273 ULONG rootStartBlock
;
274 ULONG smallBlockDepotStart
;
275 ULONG extBigBlockDepotStart
;
276 ULONG extBigBlockDepotCount
;
277 ULONG bigBlockDepotStart
[COUNT_BBDEPOTINHEADER
];
279 ULONG blockDepotCached
[NUM_BLOCKS_PER_DEPOT_BLOCK
];
280 ULONG indexBlockDepotCached
;
284 * Abstraction of the big block chains for the chains of the header.
286 BlockChainStream
* rootBlockChain
;
287 BlockChainStream
* smallBlockDepotChain
;
288 BlockChainStream
* smallBlockRootChain
;
291 * Pointer to the big block file abstraction
293 BigBlockFile
* bigBlockFile
;
296 void StorageImpl_Destroy(
297 StorageBaseImpl
* This
);
299 HRESULT
StorageImpl_Construct(
308 BOOL
StorageImpl_ReadBigBlock(
313 BOOL
StorageImpl_WriteBigBlock(
318 void* StorageImpl_GetROBigBlock(
322 void* StorageImpl_GetBigBlock(
326 void StorageImpl_ReleaseBigBlock(
330 ULONG
StorageImpl_GetNextFreeBigBlock(
333 void StorageImpl_FreeBigBlock(
337 HRESULT
StorageImpl_GetNextBlockInChain(
340 ULONG
* nextBlockIndex
);
342 void StorageImpl_SetNextBlockInChain(
347 HRESULT
StorageImpl_LoadFileHeader(
350 void StorageImpl_SaveFileHeader(
353 BOOL
StorageImpl_ReadProperty(
356 StgProperty
* buffer
);
358 BOOL
StorageImpl_WriteProperty(
361 StgProperty
* buffer
);
363 BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
365 SmallBlockChainStream
** ppsbChain
);
367 ULONG
Storage32Impl_GetNextExtendedBlock(StorageImpl
* This
,
370 void Storage32Impl_AddBlockDepot(StorageImpl
* This
,
373 ULONG
Storage32Impl_AddExtBlockDepot(StorageImpl
* This
);
375 ULONG
Storage32Impl_GetExtDepotBlock(StorageImpl
* This
,
378 void Storage32Impl_SetExtDepotBlock(StorageImpl
* This
,
381 /****************************************************************************
382 * Storage32InternalImpl definitions.
384 * Definition of the implementation structure for the IStorage32 interface.
385 * This one implements the IStorage32 interface for storage that are
386 * inside another storage.
388 struct StorageInternalImpl
390 struct StorageBaseImpl base
;
393 * There is no specific data for this class.
398 * Method definitions for the Storage32InternalImpl class.
400 StorageInternalImpl
* StorageInternalImpl_Construct(
401 StorageImpl
* ancestorStorage
,
402 ULONG rootTropertyIndex
);
404 void StorageInternalImpl_Destroy(
405 StorageBaseImpl
* This
);
407 HRESULT WINAPI
StorageInternalImpl_Commit(
409 DWORD grfCommitFlags
); /* [in] */
411 HRESULT WINAPI
StorageInternalImpl_Revert(
415 /****************************************************************************
416 * IEnumSTATSTGImpl definitions.
418 * Definition of the implementation structure for the IEnumSTATSTGImpl interface.
419 * This class allows iterating through the content of a storage and to find
420 * specific items inside it.
422 struct IEnumSTATSTGImpl
424 IEnumSTATSTGVtbl
*lpVtbl
; /* Needs to be the first item in the struct
425 * since we want to cast this in an IEnumSTATSTG pointer */
427 ULONG ref
; /* Reference count */
428 StorageImpl
* parentStorage
; /* Reference to the parent storage */
429 ULONG firstPropertyNode
; /* Index of the root of the storage to enumerate */
432 * The current implementation of the IEnumSTATSTGImpl class uses a stack
433 * to walk the property sets to get the content of a storage. This stack
434 * is implemented by the following 3 data members
440 #define ENUMSTATSGT_SIZE_INCREMENT 10
443 IEnumSTATSTGImpl
* IEnumSTATSTGImpl_Construct(
445 ULONG firstPropertyNode
);
447 void IEnumSTATSTGImpl_Destroy(
448 IEnumSTATSTGImpl
* This
);
450 void IEnumSTATSTGImpl_PushSearchNode(
451 IEnumSTATSTGImpl
* This
,
454 ULONG
IEnumSTATSTGImpl_PopSearchNode(
455 IEnumSTATSTGImpl
* This
,
458 ULONG
IEnumSTATSTGImpl_FindProperty(
459 IEnumSTATSTGImpl
* This
,
460 const OLECHAR
* lpszPropName
,
461 StgProperty
* buffer
);
463 INT
IEnumSTATSTGImpl_FindParentProperty(
464 IEnumSTATSTGImpl
*This
,
466 StgProperty
*currentProperty
,
470 /****************************************************************************
471 * StgStreamImpl definitions.
473 * This class imlements the IStream32 inteface and represents a stream
474 * located inside a storage object.
478 IStreamVtbl
*lpVtbl
; /* Needs to be the first item in the struct
479 * since we want to cast this to an IStream pointer */
487 * Storage that is the parent(owner) of the stream
489 StorageBaseImpl
* parentStorage
;
492 * Access mode of this stream.
497 * Index of the property that owns (points to) this stream.
502 * Helper variable that contains the size of the stream
504 ULARGE_INTEGER streamSize
;
507 * This is the current position of the cursor in the stream
509 ULARGE_INTEGER currentPosition
;
512 * The information in the stream is represented by a chain of small blocks
513 * or a chain of large blocks. Depending on the case, one of the two
514 * following variabled points to that information.
516 BlockChainStream
* bigBlockChain
;
517 SmallBlockChainStream
* smallBlockChain
;
521 * Method definition for the StgStreamImpl class.
523 StgStreamImpl
* StgStreamImpl_Construct(
524 StorageBaseImpl
* parentStorage
,
526 ULONG ownerProperty
);
529 /******************************************************************************
530 * Endian conversion macros
532 #ifdef WORDS_BIGENDIAN
534 #define htole32(x) RtlUlongByteSwap(x)
535 #define htole16(x) RtlUshortByteSwap(x)
536 #define le32toh(x) RtlUlongByteSwap(x)
537 #define le16toh(x) RtlUshortByteSwap(x)
541 #define htole32(x) (x)
542 #define htole16(x) (x)
543 #define le32toh(x) (x)
544 #define le16toh(x) (x)
548 /******************************************************************************
549 * The StorageUtl_ functions are miscellaneous utility functions. Most of which
550 * are abstractions used to read values from file buffers without having to
551 * worry about bit order
553 void StorageUtl_ReadWord(const BYTE
* buffer
, ULONG offset
, WORD
* value
);
554 void StorageUtl_WriteWord(BYTE
* buffer
, ULONG offset
, WORD value
);
555 void StorageUtl_ReadDWord(const BYTE
* buffer
, ULONG offset
, DWORD
* value
);
556 void StorageUtl_WriteDWord(BYTE
* buffer
, ULONG offset
, DWORD value
);
557 void StorageUtl_ReadGUID(const BYTE
* buffer
, ULONG offset
, GUID
* value
);
558 void StorageUtl_WriteGUID(BYTE
* buffer
, ULONG offset
, const GUID
* value
);
559 void StorageUtl_CopyPropertyToSTATSTG(STATSTG
* destination
,
563 /****************************************************************************
564 * BlockChainStream definitions.
566 * The BlockChainStream class is a utility class that is used to create an
567 * abstraction of the big block chains in the storage file.
569 struct BlockChainStream
571 StorageImpl
* parentStorage
;
572 ULONG
* headOfStreamPlaceHolder
;
573 ULONG ownerPropertyIndex
;
574 ULONG lastBlockNoInSequence
;
575 ULONG lastBlockNoInSequenceIndex
;
581 * Methods for the BlockChainStream class.
583 BlockChainStream
* BlockChainStream_Construct(
584 StorageImpl
* parentStorage
,
585 ULONG
* headOfStreamPlaceHolder
,
586 ULONG propertyIndex
);
588 void BlockChainStream_Destroy(
589 BlockChainStream
* This
);
591 ULONG
BlockChainStream_GetHeadOfChain(
592 BlockChainStream
* This
);
594 BOOL
BlockChainStream_ReadAt(
595 BlockChainStream
* This
,
596 ULARGE_INTEGER offset
,
601 BOOL
BlockChainStream_WriteAt(
602 BlockChainStream
* This
,
603 ULARGE_INTEGER offset
,
606 ULONG
* bytesWritten
);
608 BOOL
BlockChainStream_SetSize(
609 BlockChainStream
* This
,
610 ULARGE_INTEGER newSize
);
612 ULARGE_INTEGER
BlockChainStream_GetSize(
613 BlockChainStream
* This
);
615 ULONG
BlockChainStream_GetCount(
616 BlockChainStream
* This
);
618 /****************************************************************************
619 * SmallBlockChainStream definitions.
621 * The SmallBlockChainStream class is a utility class that is used to create an
622 * abstraction of the small block chains in the storage file.
624 struct SmallBlockChainStream
626 StorageImpl
* parentStorage
;
627 ULONG ownerPropertyIndex
;
631 * Methods of the SmallBlockChainStream class.
633 SmallBlockChainStream
* SmallBlockChainStream_Construct(
634 StorageImpl
* parentStorage
,
635 ULONG propertyIndex
);
637 void SmallBlockChainStream_Destroy(
638 SmallBlockChainStream
* This
);
640 ULONG
SmallBlockChainStream_GetHeadOfChain(
641 SmallBlockChainStream
* This
);
643 HRESULT
SmallBlockChainStream_GetNextBlockInChain(
644 SmallBlockChainStream
* This
,
646 ULONG
* nextBlockIndex
);
648 void SmallBlockChainStream_SetNextBlockInChain(
649 SmallBlockChainStream
* This
,
653 void SmallBlockChainStream_FreeBlock(
654 SmallBlockChainStream
* This
,
657 ULONG
SmallBlockChainStream_GetNextFreeBlock(
658 SmallBlockChainStream
* This
);
660 BOOL
SmallBlockChainStream_ReadAt(
661 SmallBlockChainStream
* This
,
662 ULARGE_INTEGER offset
,
667 BOOL
SmallBlockChainStream_WriteAt(
668 SmallBlockChainStream
* This
,
669 ULARGE_INTEGER offset
,
672 ULONG
* bytesWritten
);
674 BOOL
SmallBlockChainStream_SetSize(
675 SmallBlockChainStream
* This
,
676 ULARGE_INTEGER newSize
);
678 ULARGE_INTEGER
SmallBlockChainStream_GetSize(
679 SmallBlockChainStream
* This
);
681 ULONG
SmallBlockChainStream_GetCount(
682 SmallBlockChainStream
* This
);
685 #endif /* __STORAGE32_H__ */