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