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