ole32: Move the storage signatures to the appropriate file that uses them.
[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_PROPERTYTYPE    = 0x00000042;
57 static const ULONG OFFSET_PS_PREVIOUSPROP    = 0x00000044;
58 static const ULONG OFFSET_PS_NEXTPROP        = 0x00000048;
59 static const ULONG OFFSET_PS_DIRPROP         = 0x0000004C;
60 static const ULONG OFFSET_PS_GUID            = 0x00000050;
61 static const ULONG OFFSET_PS_TSS1            = 0x00000064;
62 static const ULONG OFFSET_PS_TSD1            = 0x00000068;
63 static const ULONG OFFSET_PS_TSS2            = 0x0000006C;
64 static const ULONG OFFSET_PS_TSD2            = 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 PROPERTY_NULL             = 0xFFFFFFFF;
76
77 #define PROPERTY_NAME_MAX_LEN    0x20
78 #define PROPERTY_NAME_BUFFER_LEN 0x40
79
80 #define PROPSET_BLOCK_SIZE 0x00000080
81
82 /*
83  * Property type of relation
84  */
85 #define PROPERTY_RELATION_PREVIOUS 0
86 #define PROPERTY_RELATION_NEXT     1
87 #define PROPERTY_RELATION_DIR      2
88
89 /*
90  * Property type constants
91  */
92 #define PROPTYPE_STORAGE 0x01
93 #define PROPTYPE_STREAM  0x02
94 #define PROPTYPE_ROOT    0x05
95
96 /*
97  * These defines assume a hardcoded blocksize. The code will assert
98  * if the blocksize is different. Some changes will have to be done if it
99  * becomes the case.
100  */
101 #define BIG_BLOCK_SIZE           0x200
102 #define COUNT_BBDEPOTINHEADER    109
103 #define LIMIT_TO_USE_SMALL_BLOCK 0x1000
104 #define NUM_BLOCKS_PER_DEPOT_BLOCK 128
105
106 #define STGM_ACCESS_MODE(stgm)   ((stgm)&0x0000f)
107 #define STGM_SHARE_MODE(stgm)    ((stgm)&0x000f0)
108 #define STGM_CREATE_MODE(stgm)   ((stgm)&0x0f000)
109
110 #define STGM_KNOWN_FLAGS (0xf0ff | \
111      STGM_TRANSACTED | STGM_CONVERT | STGM_PRIORITY | STGM_NOSCRATCH | \
112      STGM_NOSNAPSHOT | STGM_DIRECT_SWMR | STGM_DELETEONRELEASE | STGM_SIMPLE)
113
114 /*
115  * Forward declarations of all the structures used by the storage
116  * module.
117  */
118 typedef struct StorageBaseImpl     StorageBaseImpl;
119 typedef struct StorageImpl         StorageImpl;
120 typedef struct BlockChainStream      BlockChainStream;
121 typedef struct SmallBlockChainStream SmallBlockChainStream;
122 typedef struct IEnumSTATSTGImpl      IEnumSTATSTGImpl;
123 typedef struct StgProperty           StgProperty;
124 typedef struct StgStreamImpl         StgStreamImpl;
125
126 /*
127  * This utility structure is used to read/write the information in a storage
128  * property.
129  */
130 struct StgProperty
131 {
132   WCHAR          name[PROPERTY_NAME_MAX_LEN];
133   WORD           sizeOfNameString;
134   BYTE           propertyType;
135   ULONG          previousProperty;
136   ULONG          nextProperty;
137   ULONG          dirProperty;
138   GUID           propertyUniqueID;
139   ULONG          timeStampS1;
140   ULONG          timeStampD1;
141   ULONG          timeStampS2;
142   ULONG          timeStampD2;
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 /*
156  * Declaration of the data structures
157  */
158 typedef struct BigBlockFile BigBlockFile,*LPBIGBLOCKFILE;
159 typedef struct MappedPage   MappedPage,*LPMAPPEDPAGE;
160
161 struct BigBlockFile
162 {
163   BOOL fileBased;
164   ULARGE_INTEGER filesize;
165   ULONG blocksize;
166   HANDLE hfile;
167   HANDLE hfilemap;
168   DWORD flProtect;
169   MappedPage *maplist;
170   MappedPage *victimhead, *victimtail;
171   ULONG num_victim_pages;
172   ILockBytes *pLkbyt;
173   HGLOBAL hbytearray;
174   LPVOID pbytearray;
175 };
176
177 /*
178  * Declaration of the functions used to manipulate the BigBlockFile
179  * data structure.
180  */
181 BigBlockFile*  BIGBLOCKFILE_Construct(HANDLE hFile,
182                                       ILockBytes* pLkByt,
183                                       DWORD openFlags,
184                                       ULONG blocksize,
185                                       BOOL fileBased);
186 void           BIGBLOCKFILE_Destructor(LPBIGBLOCKFILE This);
187 void           BIGBLOCKFILE_EnsureExists(LPBIGBLOCKFILE This, ULONG index);
188 void           BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize);
189 HRESULT        BIGBLOCKFILE_ReadAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
190            void* buffer, ULONG size, ULONG* bytesRead);
191 HRESULT        BIGBLOCKFILE_WriteAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
192            void* buffer, const ULONG size, ULONG* bytesRead);
193
194 /*************************************************************************
195  * Ole Convert support
196  */
197
198 void OLECONVERT_CreateOleStream(LPSTORAGE pStorage);
199 HRESULT OLECONVERT_CreateCompObjStream(LPSTORAGE pStorage, LPCSTR strOleTypeName);
200
201 /****************************************************************************
202  * Storage32BaseImpl definitions.
203  *
204  * This structure defines the base information contained in all implementations
205  * of IStorage32 contained in this file storage implementation.
206  *
207  * In OOP terms, this is the base class for all the IStorage32 implementations
208  * contained in this file.
209  */
210 struct StorageBaseImpl
211 {
212   const IStorageVtbl *lpVtbl;    /* Needs to be the first item in the struct
213                             * since we want to cast this in a Storage32 pointer */
214
215   const IPropertySetStorageVtbl *pssVtbl; /* interface for adding a properties stream */
216
217   /*
218    * Stream tracking list
219    */
220
221   struct list strmHead;
222
223   /*
224    * Reference count of this object
225    */
226   LONG ref;
227
228   /*
229    * Ancestor storage (top level)
230    */
231   StorageImpl* ancestorStorage;
232
233   /*
234    * Index of the property for the root of
235    * this storage
236    */
237   ULONG rootPropertySetIndex;
238
239   /*
240    * virtual Destructor method.
241    */
242   void (*v_destructor)(StorageBaseImpl*);
243
244   /*
245    * flags that this storage was opened or created with
246    */
247   DWORD openFlags;
248 };
249
250 /****************************************************************************
251  * StorageBaseImpl stream list handlers
252  */
253
254 void StorageBaseImpl_AddStream(StorageBaseImpl * stg, StgStreamImpl * strm);
255 void StorageBaseImpl_RemoveStream(StorageBaseImpl * stg, StgStreamImpl * strm);
256
257 /****************************************************************************
258  * Storage32Impl definitions.
259  *
260  * This implementation of the IStorage32 interface represents a root
261  * storage. Basically, a document file.
262  */
263 struct StorageImpl
264 {
265   struct StorageBaseImpl base;
266
267   /*
268    * The following data members are specific to the Storage32Impl
269    * class
270    */
271   HANDLE           hFile;      /* Physical support for the Docfile */
272   LPOLESTR         pwcsName;   /* Full path of the document file */
273
274   /* FIXME: should this be in Storage32BaseImpl ? */
275   WCHAR            filename[PROPERTY_NAME_BUFFER_LEN];
276
277   /*
278    * File header
279    */
280   WORD  bigBlockSizeBits;
281   WORD  smallBlockSizeBits;
282   ULONG bigBlockSize;
283   ULONG smallBlockSize;
284   ULONG bigBlockDepotCount;
285   ULONG rootStartBlock;
286   ULONG smallBlockDepotStart;
287   ULONG extBigBlockDepotStart;
288   ULONG extBigBlockDepotCount;
289   ULONG bigBlockDepotStart[COUNT_BBDEPOTINHEADER];
290
291   ULONG blockDepotCached[NUM_BLOCKS_PER_DEPOT_BLOCK];
292   ULONG indexBlockDepotCached;
293   ULONG prevFreeBlock;
294
295   /*
296    * Abstraction of the big block chains for the chains of the header.
297    */
298   BlockChainStream* rootBlockChain;
299   BlockChainStream* smallBlockDepotChain;
300   BlockChainStream* smallBlockRootChain;
301
302   /*
303    * Pointer to the big block file abstraction
304    */
305   BigBlockFile* bigBlockFile;
306 };
307
308 BOOL StorageImpl_ReadProperty(
309             StorageImpl* This,
310             ULONG          index,
311             StgProperty*    buffer);
312
313 BOOL StorageImpl_WriteProperty(
314             StorageImpl* This,
315             ULONG          index,
316             StgProperty*   buffer);
317
318 BlockChainStream* Storage32Impl_SmallBlocksToBigBlocks(
319                       StorageImpl* This,
320                       SmallBlockChainStream** ppsbChain);
321
322 /****************************************************************************
323  * StgStreamImpl definitions.
324  *
325  * This class implements the IStream32 interface and represents a stream
326  * located inside a storage object.
327  */
328 struct StgStreamImpl
329 {
330   const IStreamVtbl *lpVtbl;  /* Needs to be the first item in the struct
331                          * since we want to cast this to an IStream pointer */
332
333   /*
334    * We are an entry in the storage object's stream handler list
335    */
336
337   struct list StrmListEntry;
338
339   /*
340    * Reference count
341    */
342   LONG               ref;
343
344   /*
345    * Storage that is the parent(owner) of the stream
346    */
347   StorageBaseImpl* parentStorage;
348
349   /*
350    * Access mode of this stream.
351    */
352   DWORD grfMode;
353
354   /*
355    * Index of the property that owns (points to) this stream.
356    */
357   ULONG              ownerProperty;
358
359   /*
360    * Helper variable that contains the size of the stream
361    */
362   ULARGE_INTEGER     streamSize;
363
364   /*
365    * This is the current position of the cursor in the stream
366    */
367   ULARGE_INTEGER     currentPosition;
368
369   /*
370    * The information in the stream is represented by a chain of small blocks
371    * or a chain of large blocks. Depending on the case, one of the two
372    * following variabled points to that information.
373    */
374   BlockChainStream*      bigBlockChain;
375   SmallBlockChainStream* smallBlockChain;
376 };
377
378 /*
379  * Method definition for the StgStreamImpl class.
380  */
381 StgStreamImpl* StgStreamImpl_Construct(
382                 StorageBaseImpl* parentStorage,
383     DWORD            grfMode,
384     ULONG            ownerProperty);
385
386
387 /******************************************************************************
388  * Endian conversion macros
389  */
390 #ifdef WORDS_BIGENDIAN
391
392 #define htole32(x) RtlUlongByteSwap(x)
393 #define htole16(x) RtlUshortByteSwap(x)
394 #define le32toh(x) RtlUlongByteSwap(x)
395 #define le16toh(x) RtlUshortByteSwap(x)
396
397 #else
398
399 #define htole32(x) (x)
400 #define htole16(x) (x)
401 #define le32toh(x) (x)
402 #define le16toh(x) (x)
403
404 #endif
405
406 /******************************************************************************
407  * The StorageUtl_ functions are miscellaneous utility functions. Most of which
408  * are abstractions used to read values from file buffers without having to
409  * worry about bit order
410  */
411 void StorageUtl_ReadWord(const BYTE* buffer, ULONG offset, WORD* value);
412 void StorageUtl_WriteWord(BYTE* buffer, ULONG offset, WORD value);
413 void StorageUtl_ReadDWord(const BYTE* buffer, ULONG offset, DWORD* value);
414 void StorageUtl_WriteDWord(BYTE* buffer, ULONG offset, DWORD value);
415 void StorageUtl_ReadULargeInteger(const BYTE* buffer, ULONG offset,
416  ULARGE_INTEGER* value);
417 void StorageUtl_WriteULargeInteger(BYTE* buffer, ULONG offset,
418  const ULARGE_INTEGER *value);
419 void StorageUtl_ReadGUID(const BYTE* buffer, ULONG offset, GUID* value);
420 void StorageUtl_WriteGUID(BYTE* buffer, ULONG offset, const GUID* value);
421 void StorageUtl_CopyPropertyToSTATSTG(STATSTG*     destination,
422                                              StgProperty* source,
423                                              int          statFlags);
424
425 /****************************************************************************
426  * BlockChainStream definitions.
427  *
428  * The BlockChainStream class is a utility class that is used to create an
429  * abstraction of the big block chains in the storage file.
430  */
431 struct BlockChainStream
432 {
433   StorageImpl* parentStorage;
434   ULONG*       headOfStreamPlaceHolder;
435   ULONG        ownerPropertyIndex;
436   ULONG        lastBlockNoInSequence;
437   ULONG        lastBlockNoInSequenceIndex;
438   ULONG        tailIndex;
439   ULONG        numBlocks;
440 };
441
442 /*
443  * Methods for the BlockChainStream class.
444  */
445 BlockChainStream* BlockChainStream_Construct(
446                 StorageImpl* parentStorage,
447                 ULONG*         headOfStreamPlaceHolder,
448                 ULONG          propertyIndex);
449
450 void BlockChainStream_Destroy(
451                 BlockChainStream* This);
452
453 HRESULT BlockChainStream_ReadAt(
454                 BlockChainStream* This,
455                 ULARGE_INTEGER offset,
456                 ULONG          size,
457                 void*          buffer,
458                 ULONG*         bytesRead);
459
460 HRESULT BlockChainStream_WriteAt(
461                 BlockChainStream* This,
462                 ULARGE_INTEGER offset,
463                 ULONG          size,
464                 const void*    buffer,
465                 ULONG*         bytesWritten);
466
467 BOOL BlockChainStream_SetSize(
468                 BlockChainStream* This,
469                 ULARGE_INTEGER    newSize);
470
471 /****************************************************************************
472  * SmallBlockChainStream definitions.
473  *
474  * The SmallBlockChainStream class is a utility class that is used to create an
475  * abstraction of the small block chains in the storage file.
476  */
477 struct SmallBlockChainStream
478 {
479   StorageImpl* parentStorage;
480   ULONG          ownerPropertyIndex;
481 };
482
483 /*
484  * Methods of the SmallBlockChainStream class.
485  */
486 SmallBlockChainStream* SmallBlockChainStream_Construct(
487                StorageImpl* parentStorage,
488                ULONG          propertyIndex);
489
490 void SmallBlockChainStream_Destroy(
491                SmallBlockChainStream* This);
492
493 HRESULT SmallBlockChainStream_ReadAt(
494                SmallBlockChainStream* This,
495                ULARGE_INTEGER offset,
496                ULONG          size,
497                void*          buffer,
498                ULONG*         bytesRead);
499
500 HRESULT SmallBlockChainStream_WriteAt(
501                SmallBlockChainStream* This,
502                ULARGE_INTEGER offset,
503                ULONG          size,
504                const void*    buffer,
505                ULONG*         bytesWritten);
506
507 BOOL SmallBlockChainStream_SetSize(
508                SmallBlockChainStream* This,
509                ULARGE_INTEGER          newSize);
510
511
512 #endif /* __STORAGE32_H__ */