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