36a7d679af6b22200e9cc2e2d18d8288351f9c37
[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_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;
79
80 #define DIRENTRY_NAME_MAX_LEN 0x20
81 #define DIRENTRY_NAME_BUFFER_LEN 0x40
82
83 #define RAW_DIRENTRY_SIZE 0x00000080
84
85 #define HEADER_SIZE 512
86
87 #define MIN_BIG_BLOCK_SIZE 0x200
88 #define MAX_BIG_BLOCK_SIZE 0x1000
89
90 /*
91 * Type of child entry link
92 */
93 #define DIRENTRY_RELATION_PREVIOUS 0
94 #define DIRENTRY_RELATION_NEXT 1
95 #define DIRENTRY_RELATION_DIR 2
96
97 /*
98 * type constant used in files for the root storage
99 */
100 #define STGTY_ROOT 0x05
101
102 #define COUNT_BBDEPOTINHEADER 109
103
104 /* FIXME: This value is stored in the header, but we hard-code it to 0x1000. */
105 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
106
107 #define STGM_ACCESS_MODE(stgm) ((stgm)&0x0000f)
108 #define STGM_SHARE_MODE(stgm) ((stgm)&0x000f0)
109 #define STGM_CREATE_MODE(stgm) ((stgm)&0x0f000)
110
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)
114
115 /*
116 * Forward declarations of all the structures used by the storage
117 * module.
118 */
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;
127
128 /*
129 * A reference to a directory entry in the file or a transacted cache.
130 */
131 typedef ULONG DirRef;
132
133 /*
134 * This utility structure is used to read/write the information in a directory
135 * entry.
136 */
137 struct DirEntry
138 {
139 WCHAR name[DIRENTRY_NAME_MAX_LEN];
140 WORD sizeOfNameString;
141 BYTE stgType;
142 DirRef leftChild;
143 DirRef rightChild;
144 DirRef dirRootEntry;
145 GUID clsid;
146 FILETIME ctime;
147 FILETIME mtime;
148 ULONG startingBlock;
149 ULARGE_INTEGER size;
150 };
151
152 /*************************************************************************
153 * Big Block File support
154 *
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
158 */
159
160 typedef struct BigBlockFile BigBlockFile,*LPBIGBLOCKFILE;
161
162 /*
163 * Declaration of the functions used to manipulate the BigBlockFile
164 * data structure.
165 */
166 BigBlockFile* BIGBLOCKFILE_Construct(HANDLE hFile,
167 ILockBytes* pLkByt,
168 DWORD openFlags,
169 BOOL fileBased);
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);
177
178 /*************************************************************************
179 * Ole Convert support
180 */
181
182 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage);
183 HRESULT OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage, LPCSTR strOleTypeName);
184
185
186 /****************************************************************************
187 * Storage32BaseImpl definitions.
188 *
189 * This structure defines the base information contained in all implementations
190 * of IStorage32 contained in this file storage implementation.
191 *
192 * In OOP terms, this is the base class for all the IStorage32 implementations
193 * contained in this file.
194 */
195 struct StorageBaseImpl
196 {
197 const IStorageVtbl *lpVtbl; /* Needs to be the first item in the struct
198 * since we want to cast this in a Storage32 pointer */
199
200 const IPropertySetStorageVtbl *pssVtbl; /* interface for adding a properties stream */
201
202 /*
203 * Stream tracking list
204 */
205
206 struct list strmHead;
207
208 /*
209 * Storage tracking list
210 */
211 struct list storageHead;
212
213 /*
214 * Reference count of this object
215 */
216 LONG ref;
217
218 /*
219 * TRUE if this object has been invalidated
220 */
221 int reverted;
222
223 /*
224 * Index of the directory entry of this storage
225 */
226 DirRef storageDirEntry;
227
228 /*
229 * virtual methods.
230 */
231 const StorageBaseImplVtbl *baseVtbl;
232
233 /*
234 * flags that this storage was opened or created with
235 */
236 DWORD openFlags;
237
238 /*
239 * State bits appear to only be preserved while running. No in the stream
240 */
241 DWORD stateBits;
242
243 /* If set, this overrides the root storage name returned by IStorage_Stat */
244 LPCWSTR filename;
245
246 BOOL create; /* Was the storage created or opened.
247 The behaviour of STGM_SIMPLE depends on this */
248 /*
249 * If this storage was opened in transacted mode, the object that implements
250 * the transacted snapshot or cache.
251 */
252 StorageBaseImpl *transactedChild;
253 };
254
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 };
267
268 static inline void StorageBaseImpl_Destroy(StorageBaseImpl *This)
269 {
270 This->baseVtbl->Destroy(This);
271 }
272
273 static inline void StorageBaseImpl_Invalidate(StorageBaseImpl *This)
274 {
275 This->baseVtbl->Invalidate(This);
276 }
277
278 static inline HRESULT StorageBaseImpl_CreateDirEntry(StorageBaseImpl *This,
279 const DirEntry *newData, DirRef *index)
280 {
281 return This->baseVtbl->CreateDirEntry(This, newData, index);
282 }
283
284 static inline HRESULT StorageBaseImpl_WriteDirEntry(StorageBaseImpl *This,
285 DirRef index, const DirEntry *data)
286 {
287 return This->baseVtbl->WriteDirEntry(This, index, data);
288 }
289
290 static inline HRESULT StorageBaseImpl_ReadDirEntry(StorageBaseImpl *This,
291 DirRef index, DirEntry *data)
292 {
293 return This->baseVtbl->ReadDirEntry(This, index, data);
294 }
295
296 static inline HRESULT StorageBaseImpl_DestroyDirEntry(StorageBaseImpl *This,
297 DirRef index)
298 {
299 return This->baseVtbl->DestroyDirEntry(This, index);
300 }
301
302 /* Read up to size bytes from this directory entry's stream at the given offset. */
303 static inline HRESULT StorageBaseImpl_StreamReadAt(StorageBaseImpl *This,
304 DirRef index, ULARGE_INTEGER offset, ULONG size, void *buffer, ULONG *bytesRead)
305 {
306 return This->baseVtbl->StreamReadAt(This, index, offset, size, buffer, bytesRead);
307 }
308
309 /* Write size bytes to this directory entry's stream at the given offset,
310 * growing the stream if necessary. */
311 static inline HRESULT StorageBaseImpl_StreamWriteAt(StorageBaseImpl *This,
312 DirRef index, ULARGE_INTEGER offset, ULONG size, const void *buffer, ULONG *bytesWritten)
313 {
314 return This->baseVtbl->StreamWriteAt(This, index, offset, size, buffer, bytesWritten);
315 }
316
317 static inline HRESULT StorageBaseImpl_StreamSetSize(StorageBaseImpl *This,
318 DirRef index, ULARGE_INTEGER newsize)
319 {
320 return This->baseVtbl->StreamSetSize(This, index, newsize);
321 }
322
323 /****************************************************************************
324 * StorageBaseImpl stream list handlers
325 */
326
327 void StorageBaseImpl_AddStream(StorageBaseImpl * stg, StgStreamImpl * strm);
328 void StorageBaseImpl_RemoveStream(StorageBaseImpl * stg, StgStreamImpl * strm);
329
330 /* Number of BlockChainStream objects to cache in a StorageImpl */
331 #define BLOCKCHAIN_CACHE_SIZE 4
332
333 /****************************************************************************
334 * Storage32Impl definitions.
335 *
336 * This implementation of the IStorage32 interface represents a root
337 * storage. Basically, a document file.
338 */
339 struct StorageImpl
340 {
341 struct StorageBaseImpl base;
342
343 /*
344 * The following data members are specific to the Storage32Impl
345 * class
346 */
347 HANDLE hFile; /* Physical support for the Docfile */
348 LPOLESTR pwcsName; /* Full path of the document file */
349
350 /*
351 * File header
352 */
353 WORD bigBlockSizeBits;
354 WORD smallBlockSizeBits;
355 ULONG bigBlockSize;
356 ULONG smallBlockSize;
357 ULONG bigBlockDepotCount;
358 ULONG rootStartBlock;
359 ULONG smallBlockLimit;
360 ULONG smallBlockDepotStart;
361 ULONG extBigBlockDepotStart;
362 ULONG extBigBlockDepotCount;
363 ULONG bigBlockDepotStart[COUNT_BBDEPOTINHEADER];
364
365 ULONG blockDepotCached[MAX_BIG_BLOCK_SIZE / 4];
366 ULONG indexBlockDepotCached;
367 ULONG prevFreeBlock;
368
369 /* All small blocks before this one are known to be in use. */
370 ULONG firstFreeSmallBlock;
371
372 /*
373 * Abstraction of the big block chains for the chains of the header.
374 */
375 BlockChainStream* rootBlockChain;
376 BlockChainStream* smallBlockDepotChain;
377 BlockChainStream* smallBlockRootChain;
378
379 /* Cache of block chain streams objects for directory entries */
380 BlockChainStream* blockChainCache[BLOCKCHAIN_CACHE_SIZE];
381 UINT blockChainToEvict;
382
383 /*
384 * Pointer to the big block file abstraction
385 */
386 BigBlockFile* bigBlockFile;
387 };
388
389 HRESULT StorageImpl_ReadRawDirEntry(
390 StorageImpl *This,
391 ULONG index,
392 BYTE *buffer);
393
394 void UpdateRawDirEntry(
395 BYTE *buffer,
396 const DirEntry *newData);
397
398 HRESULT StorageImpl_WriteRawDirEntry(
399 StorageImpl *This,
400 ULONG index,
401 const BYTE *buffer);
402
403 HRESULT StorageImpl_ReadDirEntry(
404 StorageImpl* This,
405 DirRef index,
406 DirEntry* buffer);
407
408 HRESULT StorageImpl_WriteDirEntry(
409 StorageImpl* This,
410 DirRef index,
411 const DirEntry* buffer);
412
413 BlockChainStream* Storage32Impl_SmallBlocksToBigBlocks(
414 StorageImpl* This,
415 SmallBlockChainStream** ppsbChain);
416
417 SmallBlockChainStream* Storage32Impl_BigBlocksToSmallBlocks(
418 StorageImpl* This,
419 BlockChainStream** ppbbChain);
420
421 /****************************************************************************
422 * StgStreamImpl definitions.
423 *
424 * This class implements the IStream32 interface and represents a stream
425 * located inside a storage object.
426 */
427 struct StgStreamImpl
428 {
429 const IStreamVtbl *lpVtbl; /* Needs to be the first item in the struct
430 * since we want to cast this to an IStream pointer */
431
432 /*
433 * We are an entry in the storage object's stream handler list
434 */
435
436 struct list StrmListEntry;
437
438 /*
439 * Reference count
440 */
441 LONG ref;
442
443 /*
444 * Storage that is the parent(owner) of the stream
445 */
446 StorageBaseImpl* parentStorage;
447
448 /*
449 * Access mode of this stream.
450 */
451 DWORD grfMode;
452
453 /*
454 * Index of the directory entry that owns (points to) this stream.
455 */
456 DirRef dirEntry;
457
458 /*
459 * This is the current position of the cursor in the stream
460 */
461 ULARGE_INTEGER currentPosition;
462 };
463
464 /*
465 * Method definition for the StgStreamImpl class.
466 */
467 StgStreamImpl* StgStreamImpl_Construct(
468 StorageBaseImpl* parentStorage,
469 DWORD grfMode,
470 DirRef dirEntry);
471
472
473 /******************************************************************************
474 * Endian conversion macros
475 */
476 #ifdef WORDS_BIGENDIAN
477
478 #define htole32(x) RtlUlongByteSwap(x)
479 #define htole16(x) RtlUshortByteSwap(x)
480 #define lendian32toh(x) RtlUlongByteSwap(x)
481 #define lendian16toh(x) RtlUshortByteSwap(x)
482
483 #else
484
485 #define htole32(x) (x)
486 #define htole16(x) (x)
487 #define lendian32toh(x) (x)
488 #define lendian16toh(x) (x)
489
490 #endif
491
492 /******************************************************************************
493 * The StorageUtl_ functions are miscellaneous utility functions. Most of which
494 * are abstractions used to read values from file buffers without having to
495 * worry about bit order
496 */
497 void StorageUtl_ReadWord(const BYTE* buffer, ULONG offset, WORD* value);
498 void StorageUtl_WriteWord(BYTE* buffer, ULONG offset, WORD value);
499 void StorageUtl_ReadDWord(const BYTE* buffer, ULONG offset, DWORD* value);
500 void StorageUtl_WriteDWord(BYTE* buffer, ULONG offset, DWORD value);
501 void StorageUtl_ReadULargeInteger(const BYTE* buffer, ULONG offset,
502 ULARGE_INTEGER* value);
503 void StorageUtl_WriteULargeInteger(BYTE* buffer, ULONG offset,
504 const ULARGE_INTEGER *value);
505 void StorageUtl_ReadGUID(const BYTE* buffer, ULONG offset, GUID* value);
506 void StorageUtl_WriteGUID(BYTE* buffer, ULONG offset, const GUID* value);
507 void StorageUtl_CopyDirEntryToSTATSTG(StorageBaseImpl *storage,STATSTG* destination,
508 const DirEntry* source, int statFlags);
509
510 /****************************************************************************
511 * BlockChainStream definitions.
512 *
513 * The BlockChainStream class is a utility class that is used to create an
514 * abstraction of the big block chains in the storage file.
515 */
516 struct BlockChainStream
517 {
518 StorageImpl* parentStorage;
519 ULONG* headOfStreamPlaceHolder;
520 DirRef ownerDirEntry;
521 ULONG lastBlockNoInSequence;
522 ULONG lastBlockNoInSequenceIndex;
523 ULONG tailIndex;
524 ULONG numBlocks;
525 };
526
527 /*
528 * Methods for the BlockChainStream class.
529 */
530 BlockChainStream* BlockChainStream_Construct(
531 StorageImpl* parentStorage,
532 ULONG* headOfStreamPlaceHolder,
533 DirRef dirEntry);
534
535 void BlockChainStream_Destroy(
536 BlockChainStream* This);
537
538 HRESULT BlockChainStream_ReadAt(
539 BlockChainStream* This,
540 ULARGE_INTEGER offset,
541 ULONG size,
542 void* buffer,
543 ULONG* bytesRead);
544
545 HRESULT BlockChainStream_WriteAt(
546 BlockChainStream* This,
547 ULARGE_INTEGER offset,
548 ULONG size,
549 const void* buffer,
550 ULONG* bytesWritten);
551
552 BOOL BlockChainStream_SetSize(
553 BlockChainStream* This,
554 ULARGE_INTEGER newSize);
555
556 /****************************************************************************
557 * SmallBlockChainStream definitions.
558 *
559 * The SmallBlockChainStream class is a utility class that is used to create an
560 * abstraction of the small block chains in the storage file.
561 */
562 struct SmallBlockChainStream
563 {
564 StorageImpl* parentStorage;
565 DirRef ownerDirEntry;
566 ULONG* headOfStreamPlaceHolder;
567 };
568
569 /*
570 * Methods of the SmallBlockChainStream class.
571 */
572 SmallBlockChainStream* SmallBlockChainStream_Construct(
573 StorageImpl* parentStorage,
574 ULONG* headOfStreamPlaceHolder,
575 DirRef dirEntry);
576
577 void SmallBlockChainStream_Destroy(
578 SmallBlockChainStream* This);
579
580 HRESULT SmallBlockChainStream_ReadAt(
581 SmallBlockChainStream* This,
582 ULARGE_INTEGER offset,
583 ULONG size,
584 void* buffer,
585 ULONG* bytesRead);
586
587 HRESULT SmallBlockChainStream_WriteAt(
588 SmallBlockChainStream* This,
589 ULARGE_INTEGER offset,
590 ULONG size,
591 const void* buffer,
592 ULONG* bytesWritten);
593
594 BOOL SmallBlockChainStream_SetSize(
595 SmallBlockChainStream* This,
596 ULARGE_INTEGER newSize);
597
598
599 #endif /* __STORAGE32_H__ */