[OLE32]
[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 HRESULT (*StreamLink)(StorageBaseImpl*,DirRef,DirRef);
267 };
268
269 static inline void StorageBaseImpl_Destroy(StorageBaseImpl *This)
270 {
271 This->baseVtbl->Destroy(This);
272 }
273
274 static inline void StorageBaseImpl_Invalidate(StorageBaseImpl *This)
275 {
276 This->baseVtbl->Invalidate(This);
277 }
278
279 static inline HRESULT StorageBaseImpl_CreateDirEntry(StorageBaseImpl *This,
280 const DirEntry *newData, DirRef *index)
281 {
282 return This->baseVtbl->CreateDirEntry(This, newData, index);
283 }
284
285 static inline HRESULT StorageBaseImpl_WriteDirEntry(StorageBaseImpl *This,
286 DirRef index, const DirEntry *data)
287 {
288 return This->baseVtbl->WriteDirEntry(This, index, data);
289 }
290
291 static inline HRESULT StorageBaseImpl_ReadDirEntry(StorageBaseImpl *This,
292 DirRef index, DirEntry *data)
293 {
294 return This->baseVtbl->ReadDirEntry(This, index, data);
295 }
296
297 static inline HRESULT StorageBaseImpl_DestroyDirEntry(StorageBaseImpl *This,
298 DirRef index)
299 {
300 return This->baseVtbl->DestroyDirEntry(This, index);
301 }
302
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)
306 {
307 return This->baseVtbl->StreamReadAt(This, index, offset, size, buffer, bytesRead);
308 }
309
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)
314 {
315 return This->baseVtbl->StreamWriteAt(This, index, offset, size, buffer, bytesWritten);
316 }
317
318 static inline HRESULT StorageBaseImpl_StreamSetSize(StorageBaseImpl *This,
319 DirRef index, ULARGE_INTEGER newsize)
320 {
321 return This->baseVtbl->StreamSetSize(This, index, newsize);
322 }
323
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)
330 {
331 return This->baseVtbl->StreamLink(This, dst, src);
332 }
333
334 /****************************************************************************
335 * StorageBaseImpl stream list handlers
336 */
337
338 void StorageBaseImpl_AddStream(StorageBaseImpl * stg, StgStreamImpl * strm);
339 void StorageBaseImpl_RemoveStream(StorageBaseImpl * stg, StgStreamImpl * strm);
340
341 /* Number of BlockChainStream objects to cache in a StorageImpl */
342 #define BLOCKCHAIN_CACHE_SIZE 4
343
344 /****************************************************************************
345 * Storage32Impl definitions.
346 *
347 * This implementation of the IStorage32 interface represents a root
348 * storage. Basically, a document file.
349 */
350 struct StorageImpl
351 {
352 struct StorageBaseImpl base;
353
354 /*
355 * The following data members are specific to the Storage32Impl
356 * class
357 */
358 HANDLE hFile; /* Physical support for the Docfile */
359 LPOLESTR pwcsName; /* Full path of the document file */
360
361 /*
362 * File header
363 */
364 WORD bigBlockSizeBits;
365 WORD smallBlockSizeBits;
366 ULONG bigBlockSize;
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];
375
376 ULONG blockDepotCached[MAX_BIG_BLOCK_SIZE / 4];
377 ULONG indexBlockDepotCached;
378 ULONG prevFreeBlock;
379
380 /* All small blocks before this one are known to be in use. */
381 ULONG firstFreeSmallBlock;
382
383 /*
384 * Abstraction of the big block chains for the chains of the header.
385 */
386 BlockChainStream* rootBlockChain;
387 BlockChainStream* smallBlockDepotChain;
388 BlockChainStream* smallBlockRootChain;
389
390 /* Cache of block chain streams objects for directory entries */
391 BlockChainStream* blockChainCache[BLOCKCHAIN_CACHE_SIZE];
392 UINT blockChainToEvict;
393
394 /*
395 * Pointer to the big block file abstraction
396 */
397 BigBlockFile* bigBlockFile;
398 };
399
400 HRESULT StorageImpl_ReadRawDirEntry(
401 StorageImpl *This,
402 ULONG index,
403 BYTE *buffer);
404
405 void UpdateRawDirEntry(
406 BYTE *buffer,
407 const DirEntry *newData);
408
409 HRESULT StorageImpl_WriteRawDirEntry(
410 StorageImpl *This,
411 ULONG index,
412 const BYTE *buffer);
413
414 HRESULT StorageImpl_ReadDirEntry(
415 StorageImpl* This,
416 DirRef index,
417 DirEntry* buffer);
418
419 HRESULT StorageImpl_WriteDirEntry(
420 StorageImpl* This,
421 DirRef index,
422 const DirEntry* buffer);
423
424 BlockChainStream* Storage32Impl_SmallBlocksToBigBlocks(
425 StorageImpl* This,
426 SmallBlockChainStream** ppsbChain);
427
428 SmallBlockChainStream* Storage32Impl_BigBlocksToSmallBlocks(
429 StorageImpl* This,
430 BlockChainStream** ppbbChain);
431
432 /****************************************************************************
433 * StgStreamImpl definitions.
434 *
435 * This class implements the IStream32 interface and represents a stream
436 * located inside a storage object.
437 */
438 struct StgStreamImpl
439 {
440 const IStreamVtbl *lpVtbl; /* Needs to be the first item in the struct
441 * since we want to cast this to an IStream pointer */
442
443 /*
444 * We are an entry in the storage object's stream handler list
445 */
446
447 struct list StrmListEntry;
448
449 /*
450 * Reference count
451 */
452 LONG ref;
453
454 /*
455 * Storage that is the parent(owner) of the stream
456 */
457 StorageBaseImpl* parentStorage;
458
459 /*
460 * Access mode of this stream.
461 */
462 DWORD grfMode;
463
464 /*
465 * Index of the directory entry that owns (points to) this stream.
466 */
467 DirRef dirEntry;
468
469 /*
470 * This is the current position of the cursor in the stream
471 */
472 ULARGE_INTEGER currentPosition;
473 };
474
475 /*
476 * Method definition for the StgStreamImpl class.
477 */
478 StgStreamImpl* StgStreamImpl_Construct(
479 StorageBaseImpl* parentStorage,
480 DWORD grfMode,
481 DirRef dirEntry);
482
483
484 /******************************************************************************
485 * Endian conversion macros
486 */
487 #ifdef WORDS_BIGENDIAN
488
489 #define htole32(x) RtlUlongByteSwap(x)
490 #define htole16(x) RtlUshortByteSwap(x)
491 #define lendian32toh(x) RtlUlongByteSwap(x)
492 #define lendian16toh(x) RtlUshortByteSwap(x)
493
494 #else
495
496 #define htole32(x) (x)
497 #define htole16(x) (x)
498 #define lendian32toh(x) (x)
499 #define lendian16toh(x) (x)
500
501 #endif
502
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
507 */
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);
520
521 /****************************************************************************
522 * BlockChainStream definitions.
523 *
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.
526 */
527 struct BlockChainRun
528 {
529 /* This represents a range of blocks that happen reside in consecutive sectors. */
530 ULONG firstSector;
531 ULONG firstOffset;
532 ULONG lastOffset;
533 };
534
535 struct BlockChainStream
536 {
537 StorageImpl* parentStorage;
538 ULONG* headOfStreamPlaceHolder;
539 DirRef ownerDirEntry;
540 struct BlockChainRun* indexCache;
541 ULONG indexCacheLen;
542 ULONG indexCacheSize;
543 ULONG tailIndex;
544 ULONG numBlocks;
545 };
546
547 /*
548 * Methods for the BlockChainStream class.
549 */
550 BlockChainStream* BlockChainStream_Construct(
551 StorageImpl* parentStorage,
552 ULONG* headOfStreamPlaceHolder,
553 DirRef dirEntry);
554
555 void BlockChainStream_Destroy(
556 BlockChainStream* This);
557
558 HRESULT BlockChainStream_ReadAt(
559 BlockChainStream* This,
560 ULARGE_INTEGER offset,
561 ULONG size,
562 void* buffer,
563 ULONG* bytesRead);
564
565 HRESULT BlockChainStream_WriteAt(
566 BlockChainStream* This,
567 ULARGE_INTEGER offset,
568 ULONG size,
569 const void* buffer,
570 ULONG* bytesWritten);
571
572 BOOL BlockChainStream_SetSize(
573 BlockChainStream* This,
574 ULARGE_INTEGER newSize);
575
576 /****************************************************************************
577 * SmallBlockChainStream definitions.
578 *
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.
581 */
582 struct SmallBlockChainStream
583 {
584 StorageImpl* parentStorage;
585 DirRef ownerDirEntry;
586 ULONG* headOfStreamPlaceHolder;
587 };
588
589 /*
590 * Methods of the SmallBlockChainStream class.
591 */
592 SmallBlockChainStream* SmallBlockChainStream_Construct(
593 StorageImpl* parentStorage,
594 ULONG* headOfStreamPlaceHolder,
595 DirRef dirEntry);
596
597 void SmallBlockChainStream_Destroy(
598 SmallBlockChainStream* This);
599
600 HRESULT SmallBlockChainStream_ReadAt(
601 SmallBlockChainStream* This,
602 ULARGE_INTEGER offset,
603 ULONG size,
604 void* buffer,
605 ULONG* bytesRead);
606
607 HRESULT SmallBlockChainStream_WriteAt(
608 SmallBlockChainStream* This,
609 ULARGE_INTEGER offset,
610 ULONG size,
611 const void* buffer,
612 ULONG* bytesWritten);
613
614 BOOL SmallBlockChainStream_SetSize(
615 SmallBlockChainStream* This,
616 ULARGE_INTEGER newSize);
617
618
619 #endif /* __STORAGE32_H__ */