03328b29708e826315a08d6b7a2739ef91e691b7
[reactos.git] / reactos / dll / win32 / ole32 / storage32.h
1 /*
2 * Compound Storage (32 bit version)
3 *
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>)
7 *
8 * This include file contains definitions of types and function
9 * prototypes that are used in the many files implementing the
10 * storage functionality
11 *
12 * Copyright 1998,1999 Francis Beaudet
13 * Copyright 1998,1999 Thuy Nguyen
14 *
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.
19 *
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.
24 *
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
28 */
29 #ifndef __STORAGE32_H__
30 #define __STORAGE32_H__
31
32 #include <stdarg.h>
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winnt.h"
37 #include "objbase.h"
38 #include "winreg.h"
39 #include "winternl.h"
40 #include "wine/list.h"
41
42 /*
43 * Definitions for the file format offsets.
44 */
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_SBDEPOTSTART = 0x0000003C;
50 static const ULONG OFFSET_SBDEPOTCOUNT = 0x00000040;
51 static const ULONG OFFSET_EXTBBDEPOTSTART = 0x00000044;
52 static const ULONG OFFSET_EXTBBDEPOTCOUNT = 0x00000048;
53 static const ULONG OFFSET_BBDEPOTSTART = 0x0000004C;
54 static const ULONG OFFSET_PS_NAME = 0x00000000;
55 static const ULONG OFFSET_PS_NAMELENGTH = 0x00000040;
56 static const ULONG OFFSET_PS_STGTYPE = 0x00000042;
57 static const ULONG OFFSET_PS_LEFTCHILD = 0x00000044;
58 static const ULONG OFFSET_PS_RIGHTCHILD = 0x00000048;
59 static const ULONG OFFSET_PS_DIRROOT = 0x0000004C;
60 static const ULONG OFFSET_PS_GUID = 0x00000050;
61 static const ULONG OFFSET_PS_CTIMELOW = 0x00000064;
62 static const ULONG OFFSET_PS_CTIMEHIGH = 0x00000068;
63 static const ULONG OFFSET_PS_MTIMELOW = 0x0000006C;
64 static const ULONG OFFSET_PS_MTIMEHIGH = 0x00000070;
65 static const ULONG OFFSET_PS_STARTBLOCK = 0x00000074;
66 static const ULONG OFFSET_PS_SIZE = 0x00000078;
67 static const WORD DEF_BIG_BLOCK_SIZE_BITS = 0x0009;
68 static const WORD DEF_SMALL_BLOCK_SIZE_BITS = 0x0006;
69 static const WORD DEF_BIG_BLOCK_SIZE = 0x0200;
70 static const WORD DEF_SMALL_BLOCK_SIZE = 0x0040;
71 static const ULONG BLOCK_EXTBBDEPOT = 0xFFFFFFFC;
72 static const ULONG BLOCK_SPECIAL = 0xFFFFFFFD;
73 static const ULONG BLOCK_END_OF_CHAIN = 0xFFFFFFFE;
74 static const ULONG BLOCK_UNUSED = 0xFFFFFFFF;
75 static const ULONG DIRENTRY_NULL = 0xFFFFFFFF;
76
77 #define DIRENTRY_NAME_MAX_LEN 0x20
78 #define DIRENTRY_NAME_BUFFER_LEN 0x40
79
80 #define RAW_DIRENTRY_SIZE 0x00000080
81
82 /*
83 * Type of child entry link
84 */
85 #define DIRENTRY_RELATION_PREVIOUS 0
86 #define DIRENTRY_RELATION_NEXT 1
87 #define DIRENTRY_RELATION_DIR 2
88
89 /*
90 * type constant used in files for the root storage
91 */
92 #define STGTY_ROOT 0x05
93
94 /*
95 * These defines assume a hardcoded blocksize. The code will assert
96 * if the blocksize is different. Some changes will have to be done if it
97 * becomes the case.
98 */
99 #define BIG_BLOCK_SIZE 0x200
100 #define COUNT_BBDEPOTINHEADER 109
101 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
102 #define NUM_BLOCKS_PER_DEPOT_BLOCK 128
103
104 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
105 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
106 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
107
108 #define STGM_KNOWN_FLAGS (0xf0ff | \
109 STGM_TRANSACTED | STGM_CONVERT | STGM_PRIORITY | STGM_NOSCRATCH | \
110 STGM_NOSNAPSHOT | STGM_DIRECT_SWMR | STGM_DELETEONRELEASE | STGM_SIMPLE)
111
112 /*
113 * Forward declarations of all the structures used by the storage
114 * module.
115 */
116 typedef struct StorageBaseImpl StorageBaseImpl;
117 typedef struct StorageBaseImplVtbl StorageBaseImplVtbl;
118 typedef struct StorageImpl StorageImpl;
119 typedef struct BlockChainStream BlockChainStream;
120 typedef struct SmallBlockChainStream SmallBlockChainStream;
121 typedef struct IEnumSTATSTGImpl IEnumSTATSTGImpl;
122 typedef struct DirEntry DirEntry;
123 typedef struct StgStreamImpl StgStreamImpl;
124
125 /*
126 * A reference to a directory entry in the file or a transacted cache.
127 */
128 typedef ULONG DirRef;
129
130 /*
131 * This utility structure is used to read/write the information in a directory
132 * entry.
133 */
134 struct DirEntry
135 {
136 WCHAR name[DIRENTRY_NAME_MAX_LEN];
137 WORD sizeOfNameString;
138 BYTE stgType;
139 DirRef leftChild;
140 DirRef rightChild;
141 DirRef dirRootEntry;
142 GUID clsid;
143 FILETIME ctime;
144 FILETIME mtime;
145 ULONG startingBlock;
146 ULARGE_INTEGER size;
147 };
148
149 /*************************************************************************
150 * Big Block File support
151 *
152 * The big block file is an abstraction of a flat file separated in
153 * same sized blocks. The implementation for the methods described in
154 * this section appear in stg_bigblockfile.c
155 */
156
157 typedef struct BigBlockFile BigBlockFile,*LPBIGBLOCKFILE;
158
159 /*
160 * Declaration of the functions used to manipulate the BigBlockFile
161 * data structure.
162 */
163 BigBlockFile* BIGBLOCKFILE_Construct(HANDLE hFile,
164 ILockBytes* pLkByt,
165 DWORD openFlags,
166 ULONG blocksize,
167 BOOL fileBased);
168 void BIGBLOCKFILE_Destructor(LPBIGBLOCKFILE This);
169 HRESULT BIGBLOCKFILE_EnsureExists(LPBIGBLOCKFILE This, ULONG index);
170 HRESULT BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize);
171 HRESULT BIGBLOCKFILE_ReadAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
172 void* buffer, ULONG size, ULONG* bytesRead);
173 HRESULT BIGBLOCKFILE_WriteAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
174 const void* buffer, ULONG size, ULONG* bytesRead);
175
176 /*************************************************************************
177 * Ole Convert support
178 */
179
180 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage);
181 HRESULT OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage, LPCSTR strOleTypeName);
182
183
184 /****************************************************************************
185 * Storage32BaseImpl definitions.
186 *
187 * This structure defines the base information contained in all implementations
188 * of IStorage32 contained in this file storage implementation.
189 *
190 * In OOP terms, this is the base class for all the IStorage32 implementations
191 * contained in this file.
192 */
193 struct StorageBaseImpl
194 {
195 const IStorageVtbl *lpVtbl; /* Needs to be the first item in the struct
196 * since we want to cast this in a Storage32 pointer */
197
198 const IPropertySetStorageVtbl *pssVtbl; /* interface for adding a properties stream */
199
200 /*
201 * Stream tracking list
202 */
203
204 struct list strmHead;
205
206 /*
207 * Storage tracking list
208 */
209 struct list storageHead;
210
211 /*
212 * Reference count of this object
213 */
214 LONG ref;
215
216 /*
217 * TRUE if this object has been invalidated
218 */
219 int reverted;
220
221 /*
222 * Index of the directory entry of this storage
223 */
224 DirRef storageDirEntry;
225
226 /*
227 * virtual methods.
228 */
229 const StorageBaseImplVtbl *baseVtbl;
230
231 /*
232 * flags that this storage was opened or created with
233 */
234 DWORD openFlags;
235
236 /*
237 * State bits appear to only be preserved while running. No in the stream
238 */
239 DWORD stateBits;
240
241 /* If set, this overrides the root storage name returned by IStorage_Stat */
242 LPCWSTR filename;
243
244 BOOL create; /* Was the storage created or opened.
245 The behaviour of STGM_SIMPLE depends on this */
246 /*
247 * If this storage was opened in transacted mode, the object that implements
248 * the transacted snapshot or cache.
249 */
250 StorageBaseImpl *transactedChild;
251 };
252
253 /* virtual methods for StorageBaseImpl objects */
254 struct StorageBaseImplVtbl {
255 void (*Destroy)(StorageBaseImpl*);
256 void (*Invalidate)(StorageBaseImpl*);
257 HRESULT (*CreateDirEntry)(StorageBaseImpl*,const DirEntry*,DirRef*);
258 HRESULT (*WriteDirEntry)(StorageBaseImpl*,DirRef,const DirEntry*);
259 HRESULT (*ReadDirEntry)(StorageBaseImpl*,DirRef,DirEntry*);
260 HRESULT (*DestroyDirEntry)(StorageBaseImpl*,DirRef);
261 HRESULT (*StreamReadAt)(StorageBaseImpl*,DirRef,ULARGE_INTEGER,ULONG,void*,ULONG*);
262 HRESULT (*StreamWriteAt)(StorageBaseImpl*,DirRef,ULARGE_INTEGER,ULONG,const void*,ULONG*);
263 HRESULT (*StreamSetSize)(StorageBaseImpl*,DirRef,ULARGE_INTEGER);
264 };
265
266 static inline void StorageBaseImpl_Destroy(StorageBaseImpl *This)
267 {
268 This->baseVtbl->Destroy(This);
269 }
270
271 static inline void StorageBaseImpl_Invalidate(StorageBaseImpl *This)
272 {
273 This->baseVtbl->Invalidate(This);
274 }
275
276 static inline HRESULT StorageBaseImpl_CreateDirEntry(StorageBaseImpl *This,
277 const DirEntry *newData, DirRef *index)
278 {
279 return This->baseVtbl->CreateDirEntry(This, newData, index);
280 }
281
282 static inline HRESULT StorageBaseImpl_WriteDirEntry(StorageBaseImpl *This,
283 DirRef index, const DirEntry *data)
284 {
285 return This->baseVtbl->WriteDirEntry(This, index, data);
286 }
287
288 static inline HRESULT StorageBaseImpl_ReadDirEntry(StorageBaseImpl *This,
289 DirRef index, DirEntry *data)
290 {
291 return This->baseVtbl->ReadDirEntry(This, index, data);
292 }
293
294 static inline HRESULT StorageBaseImpl_DestroyDirEntry(StorageBaseImpl *This,
295 DirRef index)
296 {
297 return This->baseVtbl->DestroyDirEntry(This, index);
298 }
299
300 /* Read up to size bytes from this directory entry's stream at the given offset. */
301 static inline HRESULT StorageBaseImpl_StreamReadAt(StorageBaseImpl *This,
302 DirRef index, ULARGE_INTEGER offset, ULONG size, void *buffer, ULONG *bytesRead)
303 {
304 return This->baseVtbl->StreamReadAt(This, index, offset, size, buffer, bytesRead);
305 }
306
307 /* Write size bytes to this directory entry's stream at the given offset,
308 * growing the stream if necessary. */
309 static inline HRESULT StorageBaseImpl_StreamWriteAt(StorageBaseImpl *This,
310 DirRef index, ULARGE_INTEGER offset, ULONG size, const void *buffer, ULONG *bytesWritten)
311 {
312 return This->baseVtbl->StreamWriteAt(This, index, offset, size, buffer, bytesWritten);
313 }
314
315 static inline HRESULT StorageBaseImpl_StreamSetSize(StorageBaseImpl *This,
316 DirRef index, ULARGE_INTEGER newsize)
317 {
318 return This->baseVtbl->StreamSetSize(This, index, newsize);
319 }
320
321 /****************************************************************************
322 * StorageBaseImpl stream list handlers
323 */
324
325 void StorageBaseImpl_AddStream(StorageBaseImpl * stg, StgStreamImpl * strm);
326 void StorageBaseImpl_RemoveStream(StorageBaseImpl * stg, StgStreamImpl * strm);
327
328 /* Number of BlockChainStream objects to cache in a StorageImpl */
329 #define BLOCKCHAIN_CACHE_SIZE 4
330
331 /****************************************************************************
332 * Storage32Impl definitions.
333 *
334 * This implementation of the IStorage32 interface represents a root
335 * storage. Basically, a document file.
336 */
337 struct StorageImpl
338 {
339 struct StorageBaseImpl base;
340
341 /*
342 * The following data members are specific to the Storage32Impl
343 * class
344 */
345 HANDLE hFile; /* Physical support for the Docfile */
346 LPOLESTR pwcsName; /* Full path of the document file */
347
348 /*
349 * File header
350 */
351 WORD bigBlockSizeBits;
352 WORD smallBlockSizeBits;
353 ULONG bigBlockSize;
354 ULONG smallBlockSize;
355 ULONG bigBlockDepotCount;
356 ULONG rootStartBlock;
357 ULONG smallBlockDepotStart;
358 ULONG extBigBlockDepotStart;
359 ULONG extBigBlockDepotCount;
360 ULONG bigBlockDepotStart[COUNT_BBDEPOTINHEADER];
361
362 ULONG blockDepotCached[NUM_BLOCKS_PER_DEPOT_BLOCK];
363 ULONG indexBlockDepotCached;
364 ULONG prevFreeBlock;
365
366 /*
367 * Abstraction of the big block chains for the chains of the header.
368 */
369 BlockChainStream* rootBlockChain;
370 BlockChainStream* smallBlockDepotChain;
371 BlockChainStream* smallBlockRootChain;
372
373 /* Cache of block chain streams objects for directory entries */
374 BlockChainStream* blockChainCache[BLOCKCHAIN_CACHE_SIZE];
375 UINT blockChainToEvict;
376
377 /*
378 * Pointer to the big block file abstraction
379 */
380 BigBlockFile* bigBlockFile;
381 };
382
383 HRESULT StorageImpl_ReadRawDirEntry(
384 StorageImpl *This,
385 ULONG index,
386 BYTE *buffer);
387
388 void UpdateRawDirEntry(
389 BYTE *buffer,
390 const DirEntry *newData);
391
392 HRESULT StorageImpl_WriteRawDirEntry(
393 StorageImpl *This,
394 ULONG index,
395 const BYTE *buffer);
396
397 HRESULT StorageImpl_ReadDirEntry(
398 StorageImpl* This,
399 DirRef index,
400 DirEntry* buffer);
401
402 HRESULT StorageImpl_WriteDirEntry(
403 StorageImpl* This,
404 DirRef index,
405 const DirEntry* buffer);
406
407 BlockChainStream* Storage32Impl_SmallBlocksToBigBlocks(
408 StorageImpl* This,
409 SmallBlockChainStream** ppsbChain);
410
411 SmallBlockChainStream* Storage32Impl_BigBlocksToSmallBlocks(
412 StorageImpl* This,
413 BlockChainStream** ppbbChain);
414
415 /****************************************************************************
416 * StgStreamImpl definitions.
417 *
418 * This class implements the IStream32 interface and represents a stream
419 * located inside a storage object.
420 */
421 struct StgStreamImpl
422 {
423 const IStreamVtbl *lpVtbl; /* Needs to be the first item in the struct
424 * since we want to cast this to an IStream pointer */
425
426 /*
427 * We are an entry in the storage object's stream handler list
428 */
429
430 struct list StrmListEntry;
431
432 /*
433 * Reference count
434 */
435 LONG ref;
436
437 /*
438 * Storage that is the parent(owner) of the stream
439 */
440 StorageBaseImpl* parentStorage;
441
442 /*
443 * Access mode of this stream.
444 */
445 DWORD grfMode;
446
447 /*
448 * Index of the directory entry that owns (points to) this stream.
449 */
450 DirRef dirEntry;
451
452 /*
453 * This is the current position of the cursor in the stream
454 */
455 ULARGE_INTEGER currentPosition;
456 };
457
458 /*
459 * Method definition for the StgStreamImpl class.
460 */
461 StgStreamImpl* StgStreamImpl_Construct(
462 StorageBaseImpl* parentStorage,
463 DWORD grfMode,
464 DirRef dirEntry);
465
466
467 /******************************************************************************
468 * Endian conversion macros
469 */
470 #ifdef WORDS_BIGENDIAN
471
472 #define htole32(x) RtlUlongByteSwap(x)
473 #define htole16(x) RtlUshortByteSwap(x)
474 #define lendian32toh(x) RtlUlongByteSwap(x)
475 #define lendian16toh(x) RtlUshortByteSwap(x)
476
477 #else
478
479 #define htole32(x) (x)
480 #define htole16(x) (x)
481 #define lendian32toh(x) (x)
482 #define lendian16toh(x) (x)
483
484 #endif
485
486 /******************************************************************************
487 * The StorageUtl_ functions are miscellaneous utility functions. Most of which
488 * are abstractions used to read values from file buffers without having to
489 * worry about bit order
490 */
491 void StorageUtl_ReadWord(const BYTE* buffer, ULONG offset, WORD* value);
492 void StorageUtl_WriteWord(BYTE* buffer, ULONG offset, WORD value);
493 void StorageUtl_ReadDWord(const BYTE* buffer, ULONG offset, DWORD* value);
494 void StorageUtl_WriteDWord(BYTE* buffer, ULONG offset, DWORD value);
495 void StorageUtl_ReadULargeInteger(const BYTE* buffer, ULONG offset,
496 ULARGE_INTEGER* value);
497 void StorageUtl_WriteULargeInteger(BYTE* buffer, ULONG offset,
498 const ULARGE_INTEGER *value);
499 void StorageUtl_ReadGUID(const BYTE* buffer, ULONG offset, GUID* value);
500 void StorageUtl_WriteGUID(BYTE* buffer, ULONG offset, const GUID* value);
501 void StorageUtl_CopyDirEntryToSTATSTG(StorageBaseImpl *storage,STATSTG* destination,
502 const DirEntry* source, int statFlags);
503
504 /****************************************************************************
505 * BlockChainStream definitions.
506 *
507 * The BlockChainStream class is a utility class that is used to create an
508 * abstraction of the big block chains in the storage file.
509 */
510 struct BlockChainStream
511 {
512 StorageImpl* parentStorage;
513 ULONG* headOfStreamPlaceHolder;
514 DirRef ownerDirEntry;
515 ULONG lastBlockNoInSequence;
516 ULONG lastBlockNoInSequenceIndex;
517 ULONG tailIndex;
518 ULONG numBlocks;
519 };
520
521 /*
522 * Methods for the BlockChainStream class.
523 */
524 BlockChainStream* BlockChainStream_Construct(
525 StorageImpl* parentStorage,
526 ULONG* headOfStreamPlaceHolder,
527 DirRef dirEntry);
528
529 void BlockChainStream_Destroy(
530 BlockChainStream* This);
531
532 HRESULT BlockChainStream_ReadAt(
533 BlockChainStream* This,
534 ULARGE_INTEGER offset,
535 ULONG size,
536 void* buffer,
537 ULONG* bytesRead);
538
539 HRESULT BlockChainStream_WriteAt(
540 BlockChainStream* This,
541 ULARGE_INTEGER offset,
542 ULONG size,
543 const void* buffer,
544 ULONG* bytesWritten);
545
546 BOOL BlockChainStream_SetSize(
547 BlockChainStream* This,
548 ULARGE_INTEGER newSize);
549
550 /****************************************************************************
551 * SmallBlockChainStream definitions.
552 *
553 * The SmallBlockChainStream class is a utility class that is used to create an
554 * abstraction of the small block chains in the storage file.
555 */
556 struct SmallBlockChainStream
557 {
558 StorageImpl* parentStorage;
559 DirRef ownerDirEntry;
560 ULONG* headOfStreamPlaceHolder;
561 };
562
563 /*
564 * Methods of the SmallBlockChainStream class.
565 */
566 SmallBlockChainStream* SmallBlockChainStream_Construct(
567 StorageImpl* parentStorage,
568 ULONG* headOfStreamPlaceHolder,
569 DirRef dirEntry);
570
571 void SmallBlockChainStream_Destroy(
572 SmallBlockChainStream* This);
573
574 HRESULT SmallBlockChainStream_ReadAt(
575 SmallBlockChainStream* This,
576 ULARGE_INTEGER offset,
577 ULONG size,
578 void* buffer,
579 ULONG* bytesRead);
580
581 HRESULT SmallBlockChainStream_WriteAt(
582 SmallBlockChainStream* This,
583 ULARGE_INTEGER offset,
584 ULONG size,
585 const void* buffer,
586 ULONG* bytesWritten);
587
588 BOOL SmallBlockChainStream_SetSize(
589 SmallBlockChainStream* This,
590 ULARGE_INTEGER newSize);
591
592
593 #endif /* __STORAGE32_H__ */