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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
29 #ifndef __STORAGE32_H__
30 #define __STORAGE32_H__
40 #include "wine/list.h"
43 * Definitions for the file format offsets.
45 static const ULONG OFFSET_BIGBLOCKSIZEBITS
= 0x0000001e;
46 static const ULONG OFFSET_SMALLBLOCKSIZEBITS
= 0x00000020;
47 static const ULONG OFFSET_BBDEPOTCOUNT
= 0x0000002C;
48 static const ULONG OFFSET_ROOTSTARTBLOCK
= 0x00000030;
49 static const ULONG OFFSET_SMALLBLOCKLIMIT
= 0x00000038;
50 static const ULONG OFFSET_SBDEPOTSTART
= 0x0000003C;
51 static const ULONG OFFSET_SBDEPOTCOUNT
= 0x00000040;
52 static const ULONG OFFSET_EXTBBDEPOTSTART
= 0x00000044;
53 static const ULONG OFFSET_EXTBBDEPOTCOUNT
= 0x00000048;
54 static const ULONG OFFSET_BBDEPOTSTART
= 0x0000004C;
55 static const ULONG OFFSET_PS_NAME
= 0x00000000;
56 static const ULONG OFFSET_PS_NAMELENGTH
= 0x00000040;
57 static const ULONG OFFSET_PS_STGTYPE
= 0x00000042;
58 static const ULONG OFFSET_PS_LEFTCHILD
= 0x00000044;
59 static const ULONG OFFSET_PS_RIGHTCHILD
= 0x00000048;
60 static const ULONG OFFSET_PS_DIRROOT
= 0x0000004C;
61 static const ULONG OFFSET_PS_GUID
= 0x00000050;
62 static const ULONG OFFSET_PS_CTIMELOW
= 0x00000064;
63 static const ULONG OFFSET_PS_CTIMEHIGH
= 0x00000068;
64 static const ULONG OFFSET_PS_MTIMELOW
= 0x0000006C;
65 static const ULONG OFFSET_PS_MTIMEHIGH
= 0x00000070;
66 static const ULONG OFFSET_PS_STARTBLOCK
= 0x00000074;
67 static const ULONG OFFSET_PS_SIZE
= 0x00000078;
68 static const WORD DEF_BIG_BLOCK_SIZE_BITS
= 0x0009;
69 static const WORD MIN_BIG_BLOCK_SIZE_BITS
= 0x0009;
70 static const WORD MAX_BIG_BLOCK_SIZE_BITS
= 0x000c;
71 static const WORD DEF_SMALL_BLOCK_SIZE_BITS
= 0x0006;
72 static const WORD DEF_BIG_BLOCK_SIZE
= 0x0200;
73 static const WORD DEF_SMALL_BLOCK_SIZE
= 0x0040;
74 static const ULONG BLOCK_EXTBBDEPOT
= 0xFFFFFFFC;
75 static const ULONG BLOCK_SPECIAL
= 0xFFFFFFFD;
76 static const ULONG BLOCK_END_OF_CHAIN
= 0xFFFFFFFE;
77 static const ULONG BLOCK_UNUSED
= 0xFFFFFFFF;
78 static const ULONG DIRENTRY_NULL
= 0xFFFFFFFF;
80 #define DIRENTRY_NAME_MAX_LEN 0x20
81 #define DIRENTRY_NAME_BUFFER_LEN 0x40
83 #define RAW_DIRENTRY_SIZE 0x00000080
85 #define HEADER_SIZE 512
87 #define MIN_BIG_BLOCK_SIZE 0x200
88 #define MAX_BIG_BLOCK_SIZE 0x1000
91 * Type of child entry link
93 #define DIRENTRY_RELATION_PREVIOUS 0
94 #define DIRENTRY_RELATION_NEXT 1
95 #define DIRENTRY_RELATION_DIR 2
98 * type constant used in files for the root storage
100 #define STGTY_ROOT 0x05
102 #define COUNT_BBDEPOTINHEADER 109
104 /* FIXME: This value is stored in the header, but we hard-code it to 0x1000. */
105 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
107 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
108 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
109 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
111 #define STGM_KNOWN_FLAGS (0xf0ff | \
112 STGM_TRANSACTED | STGM_CONVERT | STGM_PRIORITY | STGM_NOSCRATCH | \
113 STGM_NOSNAPSHOT | STGM_DIRECT_SWMR | STGM_DELETEONRELEASE | STGM_SIMPLE)
116 * Forward declarations of all the structures used by the storage
119 typedef struct StorageBaseImpl StorageBaseImpl
;
120 typedef struct StorageBaseImplVtbl StorageBaseImplVtbl
;
121 typedef struct StorageImpl StorageImpl
;
122 typedef struct BlockChainStream BlockChainStream
;
123 typedef struct SmallBlockChainStream SmallBlockChainStream
;
124 typedef struct IEnumSTATSTGImpl IEnumSTATSTGImpl
;
125 typedef struct DirEntry DirEntry
;
126 typedef struct StgStreamImpl StgStreamImpl
;
129 * A reference to a directory entry in the file or a transacted cache.
131 typedef ULONG DirRef
;
134 * This utility structure is used to read/write the information in a directory
139 WCHAR name
[DIRENTRY_NAME_MAX_LEN
];
140 WORD sizeOfNameString
;
152 /*************************************************************************
153 * Big Block File support
155 * The big block file is an abstraction of a flat file separated in
156 * same sized blocks. The implementation for the methods described in
157 * this section appear in stg_bigblockfile.c
160 typedef struct BigBlockFile BigBlockFile
,*LPBIGBLOCKFILE
;
163 * Declaration of the functions used to manipulate the BigBlockFile
166 BigBlockFile
* BIGBLOCKFILE_Construct(HANDLE hFile
,
170 void BIGBLOCKFILE_Destructor(LPBIGBLOCKFILE This
);
171 HRESULT
BIGBLOCKFILE_Expand(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
);
172 HRESULT
BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This
, ULARGE_INTEGER newSize
);
173 HRESULT
BIGBLOCKFILE_ReadAt(LPBIGBLOCKFILE This
, ULARGE_INTEGER offset
,
174 void* buffer
, ULONG size
, ULONG
* bytesRead
);
175 HRESULT
BIGBLOCKFILE_WriteAt(LPBIGBLOCKFILE This
, ULARGE_INTEGER offset
,
176 const void* buffer
, ULONG size
, ULONG
* bytesRead
);
178 /*************************************************************************
179 * Ole Convert support
182 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage
);
183 HRESULT
OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage
, LPCSTR strOleTypeName
);
186 /****************************************************************************
187 * Storage32BaseImpl definitions.
189 * This structure defines the base information contained in all implementations
190 * of IStorage32 contained in this file storage implementation.
192 * In OOP terms, this is the base class for all the IStorage32 implementations
193 * contained in this file.
195 struct StorageBaseImpl
197 const IStorageVtbl
*lpVtbl
; /* Needs to be the first item in the struct
198 * since we want to cast this in a Storage32 pointer */
200 const IPropertySetStorageVtbl
*pssVtbl
; /* interface for adding a properties stream */
203 * Stream tracking list
206 struct list strmHead
;
209 * Storage tracking list
211 struct list storageHead
;
214 * Reference count of this object
219 * TRUE if this object has been invalidated
224 * Index of the directory entry of this storage
226 DirRef storageDirEntry
;
231 const StorageBaseImplVtbl
*baseVtbl
;
234 * flags that this storage was opened or created with
239 * State bits appear to only be preserved while running. No in the stream
243 /* If set, this overrides the root storage name returned by IStorage_Stat */
246 BOOL create
; /* Was the storage created or opened.
247 The behaviour of STGM_SIMPLE depends on this */
249 * If this storage was opened in transacted mode, the object that implements
250 * the transacted snapshot or cache.
252 StorageBaseImpl
*transactedChild
;
255 /* virtual methods for StorageBaseImpl objects */
256 struct StorageBaseImplVtbl
{
257 void (*Destroy
)(StorageBaseImpl
*);
258 void (*Invalidate
)(StorageBaseImpl
*);
259 HRESULT (*CreateDirEntry
)(StorageBaseImpl
*,const DirEntry
*,DirRef
*);
260 HRESULT (*WriteDirEntry
)(StorageBaseImpl
*,DirRef
,const DirEntry
*);
261 HRESULT (*ReadDirEntry
)(StorageBaseImpl
*,DirRef
,DirEntry
*);
262 HRESULT (*DestroyDirEntry
)(StorageBaseImpl
*,DirRef
);
263 HRESULT (*StreamReadAt
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
,ULONG
,void*,ULONG
*);
264 HRESULT (*StreamWriteAt
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
,ULONG
,const void*,ULONG
*);
265 HRESULT (*StreamSetSize
)(StorageBaseImpl
*,DirRef
,ULARGE_INTEGER
);
266 HRESULT (*StreamLink
)(StorageBaseImpl
*,DirRef
,DirRef
);
269 static inline void StorageBaseImpl_Destroy(StorageBaseImpl
*This
)
271 This
->baseVtbl
->Destroy(This
);
274 static inline void StorageBaseImpl_Invalidate(StorageBaseImpl
*This
)
276 This
->baseVtbl
->Invalidate(This
);
279 static inline HRESULT
StorageBaseImpl_CreateDirEntry(StorageBaseImpl
*This
,
280 const DirEntry
*newData
, DirRef
*index
)
282 return This
->baseVtbl
->CreateDirEntry(This
, newData
, index
);
285 static inline HRESULT
StorageBaseImpl_WriteDirEntry(StorageBaseImpl
*This
,
286 DirRef index
, const DirEntry
*data
)
288 return This
->baseVtbl
->WriteDirEntry(This
, index
, data
);
291 static inline HRESULT
StorageBaseImpl_ReadDirEntry(StorageBaseImpl
*This
,
292 DirRef index
, DirEntry
*data
)
294 return This
->baseVtbl
->ReadDirEntry(This
, index
, data
);
297 static inline HRESULT
StorageBaseImpl_DestroyDirEntry(StorageBaseImpl
*This
,
300 return This
->baseVtbl
->DestroyDirEntry(This
, index
);
303 /* Read up to size bytes from this directory entry's stream at the given offset. */
304 static inline HRESULT
StorageBaseImpl_StreamReadAt(StorageBaseImpl
*This
,
305 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, void *buffer
, ULONG
*bytesRead
)
307 return This
->baseVtbl
->StreamReadAt(This
, index
, offset
, size
, buffer
, bytesRead
);
310 /* Write size bytes to this directory entry's stream at the given offset,
311 * growing the stream if necessary. */
312 static inline HRESULT
StorageBaseImpl_StreamWriteAt(StorageBaseImpl
*This
,
313 DirRef index
, ULARGE_INTEGER offset
, ULONG size
, const void *buffer
, ULONG
*bytesWritten
)
315 return This
->baseVtbl
->StreamWriteAt(This
, index
, offset
, size
, buffer
, bytesWritten
);
318 static inline HRESULT
StorageBaseImpl_StreamSetSize(StorageBaseImpl
*This
,
319 DirRef index
, ULARGE_INTEGER newsize
)
321 return This
->baseVtbl
->StreamSetSize(This
, index
, newsize
);
324 /* Make dst point to the same stream that src points to. Other stream operations
325 * will not work properly for entries that point to the same stream, so this
326 * must be a very temporary state, and only one entry pointing to a given stream
327 * may be reachable at any given time. */
328 static inline HRESULT
StorageBaseImpl_StreamLink(StorageBaseImpl
*This
,
329 DirRef dst
, DirRef src
)
331 return This
->baseVtbl
->StreamLink(This
, dst
, src
);
334 /****************************************************************************
335 * StorageBaseImpl stream list handlers
338 void StorageBaseImpl_AddStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
);
339 void StorageBaseImpl_RemoveStream(StorageBaseImpl
* stg
, StgStreamImpl
* strm
);
341 /* Number of BlockChainStream objects to cache in a StorageImpl */
342 #define BLOCKCHAIN_CACHE_SIZE 4
344 /****************************************************************************
345 * Storage32Impl definitions.
347 * This implementation of the IStorage32 interface represents a root
348 * storage. Basically, a document file.
352 struct StorageBaseImpl base
;
355 * The following data members are specific to the Storage32Impl
358 HANDLE hFile
; /* Physical support for the Docfile */
359 LPOLESTR pwcsName
; /* Full path of the document file */
364 WORD bigBlockSizeBits
;
365 WORD smallBlockSizeBits
;
367 ULONG smallBlockSize
;
368 ULONG bigBlockDepotCount
;
369 ULONG rootStartBlock
;
370 ULONG smallBlockLimit
;
371 ULONG smallBlockDepotStart
;
372 ULONG extBigBlockDepotStart
;
373 ULONG extBigBlockDepotCount
;
374 ULONG bigBlockDepotStart
[COUNT_BBDEPOTINHEADER
];
376 ULONG blockDepotCached
[MAX_BIG_BLOCK_SIZE
/ 4];
377 ULONG indexBlockDepotCached
;
380 /* All small blocks before this one are known to be in use. */
381 ULONG firstFreeSmallBlock
;
384 * Abstraction of the big block chains for the chains of the header.
386 BlockChainStream
* rootBlockChain
;
387 BlockChainStream
* smallBlockDepotChain
;
388 BlockChainStream
* smallBlockRootChain
;
390 /* Cache of block chain streams objects for directory entries */
391 BlockChainStream
* blockChainCache
[BLOCKCHAIN_CACHE_SIZE
];
392 UINT blockChainToEvict
;
395 * Pointer to the big block file abstraction
397 BigBlockFile
* bigBlockFile
;
400 HRESULT
StorageImpl_ReadRawDirEntry(
405 void UpdateRawDirEntry(
407 const DirEntry
*newData
);
409 HRESULT
StorageImpl_WriteRawDirEntry(
414 HRESULT
StorageImpl_ReadDirEntry(
419 HRESULT
StorageImpl_WriteDirEntry(
422 const DirEntry
* buffer
);
424 BlockChainStream
* Storage32Impl_SmallBlocksToBigBlocks(
426 SmallBlockChainStream
** ppsbChain
);
428 SmallBlockChainStream
* Storage32Impl_BigBlocksToSmallBlocks(
430 BlockChainStream
** ppbbChain
);
432 /****************************************************************************
433 * StgStreamImpl definitions.
435 * This class implements the IStream32 interface and represents a stream
436 * located inside a storage object.
440 const IStreamVtbl
*lpVtbl
; /* Needs to be the first item in the struct
441 * since we want to cast this to an IStream pointer */
444 * We are an entry in the storage object's stream handler list
447 struct list StrmListEntry
;
455 * Storage that is the parent(owner) of the stream
457 StorageBaseImpl
* parentStorage
;
460 * Access mode of this stream.
465 * Index of the directory entry that owns (points to) this stream.
470 * This is the current position of the cursor in the stream
472 ULARGE_INTEGER currentPosition
;
476 * Method definition for the StgStreamImpl class.
478 StgStreamImpl
* StgStreamImpl_Construct(
479 StorageBaseImpl
* parentStorage
,
484 /******************************************************************************
485 * Endian conversion macros
487 #ifdef WORDS_BIGENDIAN
489 #define htole32(x) RtlUlongByteSwap(x)
490 #define htole16(x) RtlUshortByteSwap(x)
491 #define lendian32toh(x) RtlUlongByteSwap(x)
492 #define lendian16toh(x) RtlUshortByteSwap(x)
496 #define htole32(x) (x)
497 #define htole16(x) (x)
498 #define lendian32toh(x) (x)
499 #define lendian16toh(x) (x)
503 /******************************************************************************
504 * The StorageUtl_ functions are miscellaneous utility functions. Most of which
505 * are abstractions used to read values from file buffers without having to
506 * worry about bit order
508 void StorageUtl_ReadWord(const BYTE
* buffer
, ULONG offset
, WORD
* value
);
509 void StorageUtl_WriteWord(BYTE
* buffer
, ULONG offset
, WORD value
);
510 void StorageUtl_ReadDWord(const BYTE
* buffer
, ULONG offset
, DWORD
* value
);
511 void StorageUtl_WriteDWord(BYTE
* buffer
, ULONG offset
, DWORD value
);
512 void StorageUtl_ReadULargeInteger(const BYTE
* buffer
, ULONG offset
,
513 ULARGE_INTEGER
* value
);
514 void StorageUtl_WriteULargeInteger(BYTE
* buffer
, ULONG offset
,
515 const ULARGE_INTEGER
*value
);
516 void StorageUtl_ReadGUID(const BYTE
* buffer
, ULONG offset
, GUID
* value
);
517 void StorageUtl_WriteGUID(BYTE
* buffer
, ULONG offset
, const GUID
* value
);
518 void StorageUtl_CopyDirEntryToSTATSTG(StorageBaseImpl
*storage
,STATSTG
* destination
,
519 const DirEntry
* source
, int statFlags
);
521 /****************************************************************************
522 * BlockChainStream definitions.
524 * The BlockChainStream class is a utility class that is used to create an
525 * abstraction of the big block chains in the storage file.
529 /* This represents a range of blocks that happen reside in consecutive sectors. */
535 struct BlockChainStream
537 StorageImpl
* parentStorage
;
538 ULONG
* headOfStreamPlaceHolder
;
539 DirRef ownerDirEntry
;
540 struct BlockChainRun
* indexCache
;
542 ULONG indexCacheSize
;
548 * Methods for the BlockChainStream class.
550 BlockChainStream
* BlockChainStream_Construct(
551 StorageImpl
* parentStorage
,
552 ULONG
* headOfStreamPlaceHolder
,
555 void BlockChainStream_Destroy(
556 BlockChainStream
* This
);
558 HRESULT
BlockChainStream_ReadAt(
559 BlockChainStream
* This
,
560 ULARGE_INTEGER offset
,
565 HRESULT
BlockChainStream_WriteAt(
566 BlockChainStream
* This
,
567 ULARGE_INTEGER offset
,
570 ULONG
* bytesWritten
);
572 BOOL
BlockChainStream_SetSize(
573 BlockChainStream
* This
,
574 ULARGE_INTEGER newSize
);
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__ */