3 * Implemented using the documentation of the LAOLA project at
4 * <URL:http://wwwwbs.cs.tu-berlin.de/~schwartz/pmh/index.html>
5 * (Thanks to Martin Schwartz <schwartz@cs.tu-berlin.de>)
7 * Copyright 1998 Marcus Meissner
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/types.h>
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
42 #include "wine/winbase16.h"
44 #include "wine/unicode.h"
46 #include "wine/debug.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
51 WINE_DECLARE_DEBUG_CHANNEL(relay);
53 struct storage_header {
54 BYTE magic[8]; /* 00: magic */
55 BYTE unknown1[36]; /* 08: unknown */
56 DWORD num_of_bbd_blocks;/* 2C: length of big datablocks */
57 DWORD root_startblock;/* 30: root storage first big block */
58 DWORD unknown2[2]; /* 34: unknown */
59 DWORD sbd_startblock; /* 3C: small block depot first big block */
60 DWORD unknown3[3]; /* 40: unknown */
61 DWORD bbd_list[109]; /* 4C: big data block list (up to end of sector)*/
63 struct storage_pps_entry {
64 WCHAR pps_rawname[32];/* 00: \0 terminated widechar name */
65 WORD pps_sizeofname; /* 40: namelength in bytes */
66 BYTE pps_type; /* 42: flags, 1 storage/dir, 2 stream, 5 root */
67 BYTE pps_unknown0; /* 43: unknown */
68 DWORD pps_prev; /* 44: previous pps */
69 DWORD pps_next; /* 48: next pps */
70 DWORD pps_dir; /* 4C: directory pps */
71 GUID pps_guid; /* 50: class ID */
72 DWORD pps_unknown1; /* 60: unknown */
73 FILETIME pps_ft1; /* 64: filetime1 */
74 FILETIME pps_ft2; /* 70: filetime2 */
75 DWORD pps_sb; /* 74: data startblock */
76 DWORD pps_size; /* 78: datalength. (<0x1000)?small:big blocks*/
77 DWORD pps_unknown2; /* 7C: unknown */
80 #define STORAGE_CHAINENTRY_FAT 0xfffffffd
81 #define STORAGE_CHAINENTRY_ENDOFCHAIN 0xfffffffe
82 #define STORAGE_CHAINENTRY_FREE 0xffffffff
85 static const BYTE STORAGE_magic[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
90 #define SMALLBLOCKS_PER_BIGBLOCK (BIGSIZE/SMALLSIZE)
92 #define READ_HEADER STORAGE_get_big_block(hf,-1,(LPBYTE)&sth);assert(!memcmp(STORAGE_magic,sth.magic,sizeof(STORAGE_magic)));
93 static ICOM_VTABLE(IStorage16) stvt16;
94 static ICOM_VTABLE(IStorage16) *segstvt16 = NULL;
95 static ICOM_VTABLE(IStream16) strvt16;
96 static ICOM_VTABLE(IStream16) *segstrvt16 = NULL;
98 /*ULONG WINAPI IStorage16_AddRef(LPSTORAGE16 this);*/
99 static void _create_istorage16(LPSTORAGE16 *stg);
100 static void _create_istream16(LPSTREAM16 *str);
102 #define IMPLEMENTED 1
104 /* The following is taken from the CorVu implementation of docfiles, and
105 * documents things about the file format that are not implemented here, and
106 * not documented by the LAOLA project. The CorVu implementation was posted
107 * to wine-devel in February 2004, and released under the LGPL at the same
108 * time. Because that implementation is in C++, it's not directly usable in
109 * Wine, but does have documentation value.
112 * #define DF_EXT_VTOC -4
113 * #define DF_VTOC_VTOC -3
114 * #define DF_VTOC_EOF -2
115 * #define DF_VTOC_FREE -1
116 * #define DF_NAMELEN 0x20 // Maximum entry name length - 31 characters plus
117 * // a NUL terminator
119 * #define DF_FT_STORAGE 1
120 * #define DF_FT_STREAM 2
121 * #define DF_FT_LOCKBYTES 3 // Not used -- How the bloody hell did I manage
122 * #define DF_FT_PROPERTY 4 // Not Used -- to figure these two out?
123 * #define DF_FT_ROOT 5
125 * #define DF_BLOCK_SIZE 0x200
126 * #define DF_VTOC_SIZE 0x80
127 * #define DF_DE_PER_BLOCK 4
128 * #define DF_STREAM_BLOCK_SIZE 0x40
130 * A DocFile is divided into blocks of 512 bytes.
131 * The first block contains the header.
133 * The file header contains The first 109 entries in the VTOC of VTOCs.
135 * Each block pointed to by a VTOC of VTOCs contains a VTOC, which
136 * includes block chains - just like FAT. This is a somewhat poor
137 * design for the following reasons:
139 * 1. FAT was a poor file system design to begin with, and
140 * has long been known to be horrendously inefficient
141 * for day to day operations.
143 * 2. The problem is compounded here, since the file
144 * level streams are generally *not* read sequentially.
145 * This means that a significant percentage of reads
146 * require seeking from the start of the chain.
148 * Data chains also contain an internal VTOC. The block size for
149 * the standard VTOC is 512. The block size for the internal VTOC
152 * Now, the 109 blocks in the VTOC of VTOCs allows for files of
153 * up to around 7MB. So what do you think happens if that's
154 * exceeded? Well, there's an entry in the header block which
155 * points to the first block used as additional storage for
158 * Now we can get up to around 15MB. Now, guess how the file
159 * format adds in another block to the VTOC of VTOCs. Come on,
160 * it's no big surprise. That's right - the last entry in each
161 * block extending the VTOC of VTOCs is, you guessed it, the
162 * block number of the next block containing an extension to
163 * the VTOC of VTOCs. The VTOC of VTOCs is chained!!!!
167 * 1. If you are using a FAT file system, the location of
168 * your file's blocks is stored in chains.
170 * 2. At the abstract level, the file contains a VTOC of VTOCs,
171 * which is stored in the most inefficient possible format for
172 * random access - a chain (AKA list).
174 * 3. The VTOC of VTOCs contains descriptions of three file level
177 * a. The Directory stream
179 * c. The Data VTOC stream
181 * These are, of course, represented as chains.
183 * 4. The Data VTOC contains data describing the chains of blocks
184 * within the Data stream.
186 * That's right - we have a total of four levels of block chains!
188 * Now, is that complicated enough for you? No? OK, there's another
189 * complication. If an individual stream (ie. an IStream) reaches
190 * 4096 bytes in size, it gets moved from the Data Stream to
191 * a new file level stream. Now, if the stream then gets truncated
192 * back to less than 4096 bytes, it returns to the data stream.
194 * The effect of using this format can be seen very easily. Pick
195 * an arbitrary application with a grid data representation that
196 * can export to both Lotus 123 and Excel 5 or higher. Export
197 * a large file to Lotus 123 and time it. Export the same thing
198 * to Excel 5 and time that. The difference is the inefficiency
199 * of the Microsoft DocFile format.
202 * #define TOTAL_SIMPLE_VTOCS 109
204 * struct DocFile_Header
206 * df_byte iMagic1; // 0xd0
207 * df_byte iMagic2; // 0xcf
208 * df_byte iMagic3; // 0x11
209 * df_byte iMagic4; // 0xe0 - Spells D0CF11E0, or DocFile
210 * df_byte iMagic5; // 161 (igi upside down)
211 * df_byte iMagic6; // 177 (lli upside down - see below
212 * df_byte iMagic7; // 26 (gz upside down)
213 * df_byte iMagic8; // 225 (szz upside down) - see below
214 * df_int4 aiUnknown1[4];
215 * df_int4 iVersion; // DocFile Version - 0x03003E
216 * df_int4 aiUnknown2[4];
217 * df_int4 nVTOCs; // Number of VTOCs
218 * df_int4 iFirstDirBlock; // First Directory Block
219 * df_int4 aiUnknown3[2];
220 * df_int4 iFirstDataVTOC; // First data VTOC block
221 * df_int4 iHasData; // 1 if there is data in the file - yes, this is important
222 * df_int4 iExtendedVTOC; // Extended VTOC location
223 * df_int4 iExtendedVTOCSize; // Size of extended VTOC (+1?)
224 * df_int4 aiVTOCofVTOCs[TOTAL_SIMPLE_VTOCS];
227 * struct DocFile_VTOC
229 * df_int4 aiBlocks[DF_VTOC_SIZE];
233 * The meaning of the magic numbers
235 * 0xd0cf11e0 is DocFile with a zero on the end (sort of)
237 * If you key 177161 into a calculator, then turn the calculator
238 * upside down, you get igilli, which may be a reference to
239 * somebody's name, or to the Hebrew word for "angel".
241 * If you key 26225 into a calculator, then turn it upside down, you
242 * get szzgz. Microsoft has a tradition of creating nonsense words
243 * using the letters s, g, z and y. We think szzgz may be one of the
244 * Microsoft placeholder variables, along the lines of foo, bar and baz.
245 * Alternatively, it could be 22526, which would be gzszz.
248 * struct DocFile_DirEnt
250 * df_char achEntryName[DF_NAMELEN]; // Entry Name
251 * df_int2 iNameLen; // Name length in bytes, including NUL terminator
252 * df_byte iFileType; // Entry type
253 * df_byte iColour; // 1 = Black, 0 = Red
254 * df_int4 iLeftSibling; // Next Left Sibling Entry - See below
255 * df_int4 iRightSibling; // Next Right Sibling Entry
256 * df_int4 iFirstChild; // First Child Entry
257 * df_byte achClassID[16]; // Class ID
258 * df_int4 iStateBits; // [GS]etStateBits value
259 * df_int4 iCreatedLow; // Low DWORD of creation time
260 * df_int4 iCreatedHigh; // High DWORD of creation time
261 * df_int4 iModifiedLow; // Low DWORD of modification time
262 * df_int4 iModifiedHigh; // High DWORD of modification time
263 * df_int4 iVTOCPosition; // VTOC Position
264 * df_int4 iFileSize; // Size of the stream
265 * df_int4 iZero; // We think this is part of the 64 bit stream size - must be 0
271 * Siblings are stored in an obscure but incredibly elegant
272 * data structure called a red-black tree. This is generally
273 * defined as a 2-3-4 tree stored in a binary tree.
275 * A red-black tree can always be balanced very easily. The rules
276 * for a red-black tree are as follows:
278 * 1. The root node is always black.
279 * 2. The parent of a red node is always black.
281 * There is a Java demo of red-black trees at:
283 * http://langevin.usc.edu/BST/RedBlackTree-Example.html
285 * This demo is an excellent tool for learning how red-black
286 * trees work, without having to go through the process of
287 * learning how they were derived.
289 * Within the tree, elements are ordered by the length of the
290 * name and within that, ASCII order by name. This causes the
291 * apparently bizarre reordering you see when you use dfview.
293 * This is a somewhat bizarre choice. It suggests that the
294 * designer of the DocFile format was trying to optimise
295 * searching through the directory entries. However searching
296 * through directory entries is a relatively rare operation.
297 * Reading and seeking within a stream are much more common
298 * operations, especially within the file level streams, yet
299 * these use the horrendously inefficient FAT chains.
301 * This suggests that the designer was probably somebody
302 * fresh out of university, who had some basic knowledge of
303 * basic data structures, but little knowledge of anything
304 * more practical. It is bizarre to attempt to optimise
305 * directory searches while not using a more efficient file
306 * block locating system than FAT (seedling/sapling/tree
307 * would result in a massive improvement - in fact we have
308 * an alternative to DocFiles that we use internally that
309 * uses seedling/sapling/tree and *is* far more efficient).
311 * It is worth noting that the MS implementation of red-black
312 * trees is incorrect (I can tell you're surprised) and
313 * actually causes more operations to occur than are really
314 * needed. Fortunately the fact that our implementation is
315 * correct will not cause any problems - the MS implementation
316 * still appears to cause the tree to satisfy the rules, albeit
317 * a sequence of the same insertions in the different
318 * implementations may result in a different, and possibly
319 * deeper (but never shallower) tree.
323 /******************************************************************************
324 * STORAGE_get_big_block [Internal]
326 * Reading OLE compound storage
329 STORAGE_get_big_block(HANDLE hf,int n,BYTE *block)
334 if ((SetFilePointer( hf, (n+1)*BIGSIZE, NULL,
335 SEEK_SET ) == INVALID_SET_FILE_POINTER) && GetLastError())
337 WARN(" seek failed (%ld)\n",GetLastError());
340 if (!ReadFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE)
342 WARN("(block size %d): read didn't read (%ld)\n",n,GetLastError());
348 /******************************************************************************
349 * STORAGE_put_big_block [INTERNAL]
352 STORAGE_put_big_block(HANDLE hf,int n,BYTE *block)
357 if ((SetFilePointer( hf, (n+1)*BIGSIZE, NULL,
358 SEEK_SET ) == INVALID_SET_FILE_POINTER) && GetLastError())
360 WARN("seek failed (%ld)\n",GetLastError());
363 if (!WriteFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE)
365 WARN(" write failed (%ld)\n",GetLastError());
371 /******************************************************************************
372 * STORAGE_get_next_big_blocknr [INTERNAL]
375 STORAGE_get_next_big_blocknr(HANDLE hf,int blocknr) {
376 INT bbs[BIGSIZE/sizeof(INT)];
377 struct storage_header sth;
381 assert(blocknr>>7<sth.num_of_bbd_blocks);
382 if (sth.bbd_list[blocknr>>7]==0xffffffff)
384 if (!STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs))
386 assert(bbs[blocknr&0x7f]!=STORAGE_CHAINENTRY_FREE);
387 return bbs[blocknr&0x7f];
390 /******************************************************************************
391 * STORAGE_get_nth_next_big_blocknr [INTERNAL]
394 STORAGE_get_nth_next_big_blocknr(HANDLE hf,int blocknr,int nr) {
395 INT bbs[BIGSIZE/sizeof(INT)];
397 struct storage_header sth;
403 assert((blocknr>>7)<sth.num_of_bbd_blocks);
404 assert(sth.bbd_list[blocknr>>7]!=0xffffffff);
406 /* simple caching... */
407 if (lastblock!=sth.bbd_list[blocknr>>7]) {
408 BOOL ret = STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs);
410 lastblock = sth.bbd_list[blocknr>>7];
412 blocknr = bbs[blocknr&0x7f];
417 /******************************************************************************
418 * STORAGE_get_root_pps_entry [Internal]
421 STORAGE_get_root_pps_entry(HANDLE hf,struct storage_pps_entry *pstde) {
424 struct storage_pps_entry *stde=(struct storage_pps_entry*)block;
425 struct storage_header sth;
428 blocknr = sth.root_startblock;
430 BOOL ret = STORAGE_get_big_block(hf,blocknr,block);
433 if (!stde[i].pps_sizeofname)
435 if (stde[i].pps_type==5) {
440 blocknr=STORAGE_get_next_big_blocknr(hf,blocknr);
445 /******************************************************************************
446 * STORAGE_get_small_block [INTERNAL]
449 STORAGE_get_small_block(HANDLE hf,int blocknr,BYTE *sblock) {
452 struct storage_pps_entry root;
456 ret = STORAGE_get_root_pps_entry(hf,&root);
458 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
459 assert(bigblocknr>=0);
460 ret = STORAGE_get_big_block(hf,bigblocknr,block);
463 memcpy(sblock,((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),SMALLSIZE);
467 /******************************************************************************
468 * STORAGE_put_small_block [INTERNAL]
471 STORAGE_put_small_block(HANDLE hf,int blocknr,BYTE *sblock) {
474 struct storage_pps_entry root;
479 ret = STORAGE_get_root_pps_entry(hf,&root);
481 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
482 assert(bigblocknr>=0);
483 ret = STORAGE_get_big_block(hf,bigblocknr,block);
486 memcpy(((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),sblock,SMALLSIZE);
487 ret = STORAGE_put_big_block(hf,bigblocknr,block);
492 /******************************************************************************
493 * STORAGE_get_next_small_blocknr [INTERNAL]
496 STORAGE_get_next_small_blocknr(HANDLE hf,int blocknr) {
498 LPINT sbd = (LPINT)block;
500 struct storage_header sth;
505 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
506 assert(bigblocknr>=0);
507 ret = STORAGE_get_big_block(hf,bigblocknr,block);
509 assert(sbd[blocknr & 127]!=STORAGE_CHAINENTRY_FREE);
510 return sbd[blocknr & (128-1)];
513 /******************************************************************************
514 * STORAGE_get_nth_next_small_blocknr [INTERNAL]
517 STORAGE_get_nth_next_small_blocknr(HANDLE hf,int blocknr,int nr) {
520 LPINT sbd = (LPINT)block;
521 struct storage_header sth;
526 while ((nr--) && (blocknr>=0)) {
527 if (lastblocknr/128!=blocknr/128) {
529 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
530 assert(bigblocknr>=0);
531 ret = STORAGE_get_big_block(hf,bigblocknr,block);
533 lastblocknr = blocknr;
535 assert(lastblocknr>=0);
537 blocknr=sbd[blocknr & (128-1)];
538 assert(blocknr!=STORAGE_CHAINENTRY_FREE);
543 /******************************************************************************
544 * STORAGE_get_pps_entry [INTERNAL]
547 STORAGE_get_pps_entry(HANDLE hf,int n,struct storage_pps_entry *pstde) {
550 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
551 struct storage_header sth;
555 /* we have 4 pps entries per big block */
556 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
558 ret = STORAGE_get_big_block(hf,blocknr,block);
565 /******************************************************************************
566 * STORAGE_put_pps_entry [Internal]
569 STORAGE_put_pps_entry(HANDLE hf,int n,struct storage_pps_entry *pstde) {
572 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
573 struct storage_header sth;
578 /* we have 4 pps entries per big block */
579 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
581 ret = STORAGE_get_big_block(hf,blocknr,block);
584 ret = STORAGE_put_big_block(hf,blocknr,block);
589 /******************************************************************************
590 * STORAGE_look_for_named_pps [Internal]
593 STORAGE_look_for_named_pps(HANDLE hf,int n,LPOLESTR name) {
594 struct storage_pps_entry stde;
599 if (1!=STORAGE_get_pps_entry(hf,n,&stde))
602 if (!lstrcmpW(name,stde.pps_rawname))
604 if (stde.pps_prev != -1) {
605 ret=STORAGE_look_for_named_pps(hf,stde.pps_prev,name);
609 if (stde.pps_next != -1) {
610 ret=STORAGE_look_for_named_pps(hf,stde.pps_next,name);
617 /******************************************************************************
618 * STORAGE_dump_pps_entry [Internal]
624 STORAGE_dump_pps_entry(struct storage_pps_entry *stde) {
627 WideCharToMultiByte( CP_ACP, 0, stde->pps_rawname, -1, name, sizeof(name), NULL, NULL);
628 if (!stde->pps_sizeofname)
630 DPRINTF("name: %s\n",name);
631 DPRINTF("type: %d\n",stde->pps_type);
632 DPRINTF("prev pps: %ld\n",stde->pps_prev);
633 DPRINTF("next pps: %ld\n",stde->pps_next);
634 DPRINTF("dir pps: %ld\n",stde->pps_dir);
635 DPRINTF("guid: %s\n",debugstr_guid(&(stde->pps_guid)));
636 if (stde->pps_type !=2) {
639 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&(stde->pps_ft1),&dw);
641 DPRINTF("ts1: %s\n",ctime(&t));
642 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&(stde->pps_ft2),&dw);
644 DPRINTF("ts2: %s\n",ctime(&t));
646 DPRINTF("startblock: %ld\n",stde->pps_sb);
647 DPRINTF("size: %ld\n",stde->pps_size);
650 /******************************************************************************
651 * STORAGE_init_storage [INTERNAL]
654 STORAGE_init_storage(HANDLE hf) {
657 struct storage_header *sth;
658 struct storage_pps_entry *stde;
661 SetFilePointer( hf, 0, NULL, SEEK_SET );
662 /* block -1 is the storage header */
663 sth = (struct storage_header*)block;
664 memcpy(sth->magic,STORAGE_magic,8);
665 memset(sth->unknown1,0,sizeof(sth->unknown1));
666 memset(sth->unknown2,0,sizeof(sth->unknown2));
667 memset(sth->unknown3,0,sizeof(sth->unknown3));
668 sth->num_of_bbd_blocks = 1;
669 sth->root_startblock = 1;
670 sth->sbd_startblock = 0xffffffff;
671 memset(sth->bbd_list,0xff,sizeof(sth->bbd_list));
672 sth->bbd_list[0] = 0;
673 if (!WriteFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE) return FALSE;
674 /* block 0 is the big block directory */
676 memset(block,0xff,sizeof(block)); /* mark all blocks as free */
677 bbs[0]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for this block */
678 bbs[1]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for directory entry */
679 if (!WriteFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE) return FALSE;
680 /* block 1 is the root directory entry */
681 memset(block,0x00,sizeof(block));
682 stde = (struct storage_pps_entry*)block;
683 MultiByteToWideChar( CP_ACP, 0, "RootEntry", -1, stde->pps_rawname,
684 sizeof(stde->pps_rawname)/sizeof(WCHAR));
685 stde->pps_sizeofname = (strlenW(stde->pps_rawname)+1) * sizeof(WCHAR);
690 stde->pps_sb = 0xffffffff;
692 return (WriteFile( hf, block, BIGSIZE, &result, NULL ) && result == BIGSIZE);
695 /******************************************************************************
696 * STORAGE_set_big_chain [Internal]
699 STORAGE_set_big_chain(HANDLE hf,int blocknr,INT type) {
701 LPINT bbd = (LPINT)block;
702 int nextblocknr,bigblocknr;
703 struct storage_header sth;
707 assert(blocknr!=type);
709 bigblocknr = sth.bbd_list[blocknr/128];
710 assert(bigblocknr>=0);
711 ret = STORAGE_get_big_block(hf,bigblocknr,block);
714 nextblocknr = bbd[blocknr&(128-1)];
715 bbd[blocknr&(128-1)] = type;
718 ret = STORAGE_put_big_block(hf,bigblocknr,block);
720 type = STORAGE_CHAINENTRY_FREE;
721 blocknr = nextblocknr;
726 /******************************************************************************
727 * STORAGE_set_small_chain [Internal]
730 STORAGE_set_small_chain(HANDLE hf,int blocknr,INT type) {
732 LPINT sbd = (LPINT)block;
733 int lastblocknr,nextsmallblocknr,bigblocknr;
734 struct storage_header sth;
739 assert(blocknr!=type);
740 lastblocknr=-129;bigblocknr=-2;
742 /* cache block ... */
743 if (lastblocknr/128!=blocknr/128) {
744 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
745 assert(bigblocknr>=0);
746 ret = STORAGE_get_big_block(hf,bigblocknr,block);
749 lastblocknr = blocknr;
750 nextsmallblocknr = sbd[blocknr&(128-1)];
751 sbd[blocknr&(128-1)] = type;
752 ret = STORAGE_put_big_block(hf,bigblocknr,block);
756 type = STORAGE_CHAINENTRY_FREE;
757 blocknr = nextsmallblocknr;
762 /******************************************************************************
763 * STORAGE_get_free_big_blocknr [Internal]
766 STORAGE_get_free_big_blocknr(HANDLE hf) {
768 LPINT sbd = (LPINT)block;
769 int lastbigblocknr,i,curblock,bigblocknr;
770 struct storage_header sth;
776 bigblocknr = sth.bbd_list[curblock];
777 while (curblock<sth.num_of_bbd_blocks) {
778 assert(bigblocknr>=0);
779 ret = STORAGE_get_big_block(hf,bigblocknr,block);
782 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
783 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
784 ret = STORAGE_put_big_block(hf,bigblocknr,block);
786 memset(block,0x42,sizeof(block));
787 ret = STORAGE_put_big_block(hf,i+curblock*128,block);
789 return i+curblock*128;
791 lastbigblocknr = bigblocknr;
792 bigblocknr = sth.bbd_list[++curblock];
794 bigblocknr = curblock*128;
795 /* since we have marked all blocks from 0 up to curblock*128-1
796 * the next free one is curblock*128, where we happily put our
797 * next large block depot.
799 memset(block,0xff,sizeof(block));
800 /* mark the block allocated and returned by this function */
801 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
802 ret = STORAGE_put_big_block(hf,bigblocknr,block);
805 /* if we had a bbd block already (mostlikely) we need
806 * to link the new one into the chain
808 if (lastbigblocknr!=-1) {
809 ret = STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr);
812 sth.bbd_list[curblock]=bigblocknr;
813 sth.num_of_bbd_blocks++;
814 assert(sth.num_of_bbd_blocks==curblock+1);
815 ret = STORAGE_put_big_block(hf,-1,(LPBYTE)&sth);
818 /* Set the end of the chain for the bigblockdepots */
819 ret = STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN);
821 /* add 1, for the first entry is used for the additional big block
822 * depot. (means we already used bigblocknr) */
823 memset(block,0x42,sizeof(block));
824 /* allocate this block (filled with 0x42) */
825 ret = STORAGE_put_big_block(hf,bigblocknr+1,block);
831 /******************************************************************************
832 * STORAGE_get_free_small_blocknr [Internal]
835 STORAGE_get_free_small_blocknr(HANDLE hf) {
837 LPINT sbd = (LPINT)block;
838 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
839 struct storage_pps_entry root;
840 struct storage_header sth;
843 bigblocknr = sth.sbd_startblock;
847 while (bigblocknr>=0) {
848 if (!STORAGE_get_big_block(hf,bigblocknr,block))
851 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
852 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
853 newblocknr = i+curblock*128;
858 lastbigblocknr = bigblocknr;
859 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
862 if (newblocknr==-1) {
863 bigblocknr = STORAGE_get_free_big_blocknr(hf);
867 memset(block,0xff,sizeof(block));
868 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
869 if (!STORAGE_put_big_block(hf,bigblocknr,block))
871 if (lastbigblocknr==-1) {
872 sth.sbd_startblock = bigblocknr;
873 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
876 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
879 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
881 newblocknr = curblock*128;
883 /* allocate enough big blocks for storing the allocated small block */
884 if (!STORAGE_get_root_pps_entry(hf,&root))
889 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
890 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
891 /* we need to allocate more stuff */
892 bigblocknr = STORAGE_get_free_big_blocknr(hf);
896 if (root.pps_sb==-1) {
897 root.pps_sb = bigblocknr;
898 root.pps_size += BIGSIZE;
900 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
902 root.pps_size += BIGSIZE;
904 lastbigblocknr = bigblocknr;
906 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
908 if (!STORAGE_put_pps_entry(hf,0,&root))
913 /******************************************************************************
914 * STORAGE_get_free_pps_entry [Internal]
917 STORAGE_get_free_pps_entry(HANDLE hf) {
918 int blocknr, i, curblock, lastblocknr=-1;
920 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
921 struct storage_header sth;
924 blocknr = sth.root_startblock;
928 if (!STORAGE_get_big_block(hf,blocknr,block))
931 if (stde[i].pps_sizeofname==0) /* free */
933 lastblocknr = blocknr;
934 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
937 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
938 blocknr = STORAGE_get_free_big_blocknr(hf);
939 /* sth invalidated */
943 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
945 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
947 memset(block,0,sizeof(block));
948 STORAGE_put_big_block(hf,blocknr,block);
952 /* --- IStream16 implementation */
956 /* IUnknown fields */
957 ICOM_VFIELD(IStream16);
959 /* IStream16 fields */
960 SEGPTR thisptr; /* pointer to this struct as segmented */
961 struct storage_pps_entry stde;
964 ULARGE_INTEGER offset;
967 /******************************************************************************
968 * IStream16_QueryInterface [STORAGE.518]
970 HRESULT WINAPI IStream16_fnQueryInterface(
971 IStream16* iface,REFIID refiid,LPVOID *obj
973 ICOM_THIS(IStream16Impl,iface);
974 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
975 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
979 return OLE_E_ENUM_NOMORE;
983 /******************************************************************************
984 * IStream16_AddRef [STORAGE.519]
986 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
987 ICOM_THIS(IStream16Impl,iface);
988 return ++(This->ref);
991 /******************************************************************************
992 * IStream16_Release [STORAGE.520]
994 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
995 ICOM_THIS(IStream16Impl,iface);
996 FlushFileBuffers(This->hf);
999 CloseHandle(This->hf);
1000 UnMapLS( This->thisptr );
1001 HeapFree( GetProcessHeap(), 0, This );
1007 /******************************************************************************
1008 * IStream16_Seek [STORAGE.523]
1011 * Does not handle 64 bits
1013 HRESULT WINAPI IStream16_fnSeek(
1014 IStream16* iface,LARGE_INTEGER offset,DWORD whence,ULARGE_INTEGER *newpos
1016 ICOM_THIS(IStream16Impl,iface);
1017 TRACE_(relay)("(%p)->([%ld.%ld],%ld,%p)\n",This,offset.u.HighPart,offset.u.LowPart,whence,newpos);
1020 /* unix SEEK_xx should be the same as win95 ones */
1022 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
1025 assert(offset.u.HighPart==0);
1026 This->offset.u.HighPart = offset.u.HighPart;
1027 This->offset.u.LowPart = offset.u.LowPart;
1030 if (offset.u.HighPart < 0) {
1031 /* FIXME: is this negation correct ? */
1032 offset.u.HighPart = -offset.u.HighPart;
1033 offset.u.LowPart = (0xffffffff ^ offset.u.LowPart)+1;
1035 assert(offset.u.HighPart==0);
1036 assert(This->offset.u.LowPart >= offset.u.LowPart);
1037 This->offset.u.LowPart -= offset.u.LowPart;
1039 assert(offset.u.HighPart==0);
1040 This->offset.u.LowPart+= offset.u.LowPart;
1044 assert(offset.u.HighPart==0);
1045 This->offset.u.LowPart = This->stde.pps_size-offset.u.LowPart;
1048 if (This->offset.u.LowPart>This->stde.pps_size)
1049 This->offset.u.LowPart=This->stde.pps_size;
1050 if (newpos) *newpos = This->offset;
1054 /******************************************************************************
1055 * IStream16_Read [STORAGE.521]
1057 HRESULT WINAPI IStream16_fnRead(
1058 IStream16* iface,void *pv,ULONG cb,ULONG *pcbRead
1060 ICOM_THIS(IStream16Impl,iface);
1061 BYTE block[BIGSIZE];
1062 ULONG *bytesread=pcbRead,xxread;
1066 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbRead);
1067 if (!pcbRead) bytesread=&xxread;
1070 if (cb>This->stde.pps_size-This->offset.u.LowPart)
1071 cb=This->stde.pps_size-This->offset.u.LowPart;
1072 if (This->stde.pps_size < 0x1000) {
1073 /* use small block reader */
1074 blocknr = STORAGE_get_nth_next_small_blocknr(This->hf,This->stde.pps_sb,This->offset.u.LowPart/SMALLSIZE);
1078 if (!STORAGE_get_small_block(This->hf,blocknr,block)) {
1079 WARN("small block read failed!!!\n");
1083 if (cc>SMALLSIZE-(This->offset.u.LowPart&(SMALLSIZE-1)))
1084 cc=SMALLSIZE-(This->offset.u.LowPart&(SMALLSIZE-1));
1085 memcpy(pbv,block+(This->offset.u.LowPart&(SMALLSIZE-1)),cc);
1086 This->offset.u.LowPart+=cc;
1090 blocknr = STORAGE_get_next_small_blocknr(This->hf,blocknr);
1093 /* use big block reader */
1094 blocknr = STORAGE_get_nth_next_big_blocknr(This->hf,This->stde.pps_sb,This->offset.u.LowPart/BIGSIZE);
1098 if (!STORAGE_get_big_block(This->hf,blocknr,block)) {
1099 WARN("big block read failed!!!\n");
1103 if (cc>BIGSIZE-(This->offset.u.LowPart&(BIGSIZE-1)))
1104 cc=BIGSIZE-(This->offset.u.LowPart&(BIGSIZE-1));
1105 memcpy(pbv,block+(This->offset.u.LowPart&(BIGSIZE-1)),cc);
1106 This->offset.u.LowPart+=cc;
1110 blocknr=STORAGE_get_next_big_blocknr(This->hf,blocknr);
1116 /******************************************************************************
1117 * IStream16_Write [STORAGE.522]
1119 HRESULT WINAPI IStream16_fnWrite(
1120 IStream16* iface,const void *pv,ULONG cb,ULONG *pcbWrite
1122 ICOM_THIS(IStream16Impl,iface);
1123 BYTE block[BIGSIZE];
1124 ULONG *byteswritten=pcbWrite,xxwritten;
1125 int oldsize,newsize,i,curoffset=0,lastblocknr,blocknr,cc;
1126 HANDLE hf = This->hf;
1127 LPBYTE pbv = (LPBYTE)pv;
1129 if (!pcbWrite) byteswritten=&xxwritten;
1132 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbWrite);
1133 /* do we need to junk some blocks? */
1134 newsize = This->offset.u.LowPart+cb;
1135 oldsize = This->stde.pps_size;
1136 if (newsize < oldsize) {
1137 if (oldsize < 0x1000) {
1138 /* only small blocks */
1139 blocknr=STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,newsize/SMALLSIZE);
1143 /* will set the rest of the chain to 'free' */
1144 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1147 if (newsize >= 0x1000) {
1148 blocknr=STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,newsize/BIGSIZE);
1151 /* will set the rest of the chain to 'free' */
1152 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1155 /* Migrate large blocks to small blocks
1156 * (we just migrate newsize bytes)
1158 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,newsize+BIGSIZE);
1162 blocknr = This->stde.pps_sb;
1165 if (!STORAGE_get_big_block(hf,blocknr,curdata)) {
1166 HeapFree(GetProcessHeap(),0,data);
1171 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1173 /* frees complete chain for this stream */
1174 if (!STORAGE_set_big_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
1177 blocknr = This->stde.pps_sb = STORAGE_get_free_small_blocknr(hf);
1182 if (!STORAGE_put_small_block(hf,blocknr,curdata))
1186 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1190 int newblocknr = STORAGE_get_free_small_blocknr(hf);
1193 if (!STORAGE_set_small_chain(hf,blocknr,newblocknr))
1195 blocknr = newblocknr;
1197 curdata += SMALLSIZE;
1201 HeapFree(GetProcessHeap(),0,data);
1206 This->stde.pps_size = newsize;
1209 if (newsize > oldsize) {
1210 if (oldsize >= 0x1000) {
1211 /* should return the block right before the 'endofchain' */
1212 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/BIGSIZE);
1214 lastblocknr = blocknr;
1215 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1216 blocknr = STORAGE_get_free_big_blocknr(hf);
1219 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1221 lastblocknr = blocknr;
1223 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1226 if (newsize < 0x1000) {
1227 /* find startblock */
1229 This->stde.pps_sb = blocknr = STORAGE_get_free_small_blocknr(hf);
1231 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/SMALLSIZE);
1235 /* allocate required new small blocks */
1236 lastblocknr = blocknr;
1237 for (i=oldsize/SMALLSIZE;i<newsize/SMALLSIZE;i++) {
1238 blocknr = STORAGE_get_free_small_blocknr(hf);
1241 if (!STORAGE_set_small_chain(hf,lastblocknr,blocknr))
1243 lastblocknr = blocknr;
1245 /* and terminate the chain */
1246 if (!STORAGE_set_small_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1250 /* no single block allocated yet */
1251 blocknr=STORAGE_get_free_big_blocknr(hf);
1254 This->stde.pps_sb = blocknr;
1256 /* Migrate small blocks to big blocks */
1257 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,oldsize+BIGSIZE);
1261 blocknr = This->stde.pps_sb;
1265 if (!STORAGE_get_small_block(hf,blocknr,curdata))
1267 curdata += SMALLSIZE;
1269 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1271 /* free small block chain */
1272 if (!STORAGE_set_small_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
1275 blocknr = This->stde.pps_sb = STORAGE_get_free_big_blocknr(hf);
1278 /* put the data into the big blocks */
1279 cc = This->stde.pps_size;
1281 if (!STORAGE_put_big_block(hf,blocknr,curdata))
1285 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1289 int newblocknr = STORAGE_get_free_big_blocknr(hf);
1292 if (!STORAGE_set_big_chain(hf,blocknr,newblocknr))
1294 blocknr = newblocknr;
1300 HeapFree(GetProcessHeap(),0,data);
1304 /* generate big blocks to fit the new data */
1305 lastblocknr = blocknr;
1306 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1307 blocknr = STORAGE_get_free_big_blocknr(hf);
1310 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1312 lastblocknr = blocknr;
1314 /* terminate chain */
1315 if (!STORAGE_set_big_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1319 This->stde.pps_size = newsize;
1322 /* There are just some cases where we didn't modify it, we write it out
1325 if (!STORAGE_put_pps_entry(hf,This->ppsent,&(This->stde)))
1328 /* finally the write pass */
1329 if (This->stde.pps_size < 0x1000) {
1330 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->offset.u.LowPart/SMALLSIZE);
1333 /* we ensured that it is allocated above */
1335 /* Read old block everytime, since we can have
1336 * overlapping data at START and END of the write
1338 if (!STORAGE_get_small_block(hf,blocknr,block))
1341 cc = SMALLSIZE-(This->offset.u.LowPart&(SMALLSIZE-1));
1344 memcpy( ((LPBYTE)block)+(This->offset.u.LowPart&(SMALLSIZE-1)),
1348 if (!STORAGE_put_small_block(hf,blocknr,block))
1353 This->offset.u.LowPart += cc;
1354 *byteswritten += cc;
1355 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1358 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->offset.u.LowPart/BIGSIZE);
1361 /* we ensured that it is allocated above, so it better is */
1363 /* read old block everytime, since we can have
1364 * overlapping data at START and END of the write
1366 if (!STORAGE_get_big_block(hf,blocknr,block))
1369 cc = BIGSIZE-(This->offset.u.LowPart&(BIGSIZE-1));
1372 memcpy( ((LPBYTE)block)+(This->offset.u.LowPart&(BIGSIZE-1)),
1376 if (!STORAGE_put_big_block(hf,blocknr,block))
1381 This->offset.u.LowPart += cc;
1382 *byteswritten += cc;
1383 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1389 /******************************************************************************
1390 * _create_istream16 [Internal]
1392 static void _create_istream16(LPSTREAM16 *str) {
1393 IStream16Impl* lpst;
1395 if (!strvt16.QueryInterface) {
1396 HMODULE16 wp = GetModuleHandle16("STORAGE");
1398 /* FIXME: what is This GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1399 #define VTENT(xfn) strvt16.xfn = (void*)GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.xfn)
1400 VTENT(QueryInterface);
1411 VTENT(UnlockRegion);
1415 segstrvt16 = (ICOM_VTABLE(IStream16)*)MapLS( &strvt16 );
1417 #define VTENT(xfn) strvt16.xfn = IStream16_fn##xfn;
1418 VTENT(QueryInterface);
1430 VTENT(UnlockRegion);
1435 segstrvt16 = &strvt16;
1438 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1439 lpst->lpVtbl = segstrvt16;
1441 lpst->thisptr = MapLS( lpst );
1442 *str = (void*)lpst->thisptr;
1446 /* --- IStream32 implementation */
1450 /* IUnknown fields */
1451 ICOM_VFIELD(IStream);
1453 /* IStream32 fields */
1454 struct storage_pps_entry stde;
1457 ULARGE_INTEGER offset;
1460 /*****************************************************************************
1461 * IStream32_QueryInterface [VTABLE]
1463 HRESULT WINAPI IStream_fnQueryInterface(
1464 IStream* iface,REFIID refiid,LPVOID *obj
1466 ICOM_THIS(IStream32Impl,iface);
1468 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1469 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1473 return OLE_E_ENUM_NOMORE;
1477 /******************************************************************************
1478 * IStream32_AddRef [VTABLE]
1480 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1481 ICOM_THIS(IStream32Impl,iface);
1482 return ++(This->ref);
1485 /******************************************************************************
1486 * IStream32_Release [VTABLE]
1488 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1489 ICOM_THIS(IStream32Impl,iface);
1490 FlushFileBuffers(This->hf);
1493 CloseHandle(This->hf);
1494 HeapFree( GetProcessHeap(), 0, This );
1500 /* --- IStorage16 implementation */
1504 /* IUnknown fields */
1505 ICOM_VFIELD(IStorage16);
1507 /* IStorage16 fields */
1508 SEGPTR thisptr; /* pointer to this struct as segmented */
1509 struct storage_pps_entry stde;
1514 /******************************************************************************
1515 * IStorage16_QueryInterface [STORAGE.500]
1517 HRESULT WINAPI IStorage16_fnQueryInterface(
1518 IStorage16* iface,REFIID refiid,LPVOID *obj
1520 ICOM_THIS(IStorage16Impl,iface);
1522 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1524 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1528 return OLE_E_ENUM_NOMORE;
1531 /******************************************************************************
1532 * IStorage16_AddRef [STORAGE.501]
1534 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1535 ICOM_THIS(IStorage16Impl,iface);
1536 return ++(This->ref);
1539 /******************************************************************************
1540 * IStorage16_Release [STORAGE.502]
1542 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1543 ICOM_THIS(IStorage16Impl,iface);
1547 UnMapLS( This->thisptr );
1548 HeapFree( GetProcessHeap(), 0, This );
1552 /******************************************************************************
1553 * IStorage16_Stat [STORAGE.517]
1555 HRESULT WINAPI IStorage16_fnStat(
1556 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1558 ICOM_THIS(IStorage16Impl,iface);
1559 DWORD len = WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, NULL, 0, NULL, NULL );
1560 LPSTR nameA = HeapAlloc( GetProcessHeap(), 0, len );
1562 TRACE("(%p)->(%p,0x%08lx)\n",
1563 This,pstatstg,grfStatFlag
1565 WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, nameA, len, NULL, NULL );
1566 pstatstg->pwcsName=(LPOLESTR16)MapLS( nameA );
1567 pstatstg->type = This->stde.pps_type;
1568 pstatstg->cbSize.u.LowPart = This->stde.pps_size;
1569 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1570 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1571 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1572 pstatstg->grfMode = 0; /* FIXME */
1573 pstatstg->grfLocksSupported = 0; /* FIXME */
1574 pstatstg->clsid = This->stde.pps_guid;
1575 pstatstg->grfStateBits = 0; /* FIXME */
1576 pstatstg->reserved = 0;
1580 /******************************************************************************
1581 * IStorage16_Commit [STORAGE.509]
1583 HRESULT WINAPI IStorage16_fnCommit(
1584 LPSTORAGE16 iface,DWORD commitflags
1586 ICOM_THIS(IStorage16Impl,iface);
1587 FIXME("(%p)->(0x%08lx),STUB!\n",
1593 /******************************************************************************
1594 * IStorage16_CopyTo [STORAGE.507]
1596 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1597 ICOM_THIS(IStorage16Impl,iface);
1598 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1599 This,ciidExclude,debugstr_guid(rgiidExclude),SNB16Exclude,pstgDest
1605 /******************************************************************************
1606 * IStorage16_CreateStorage [STORAGE.505]
1608 HRESULT WINAPI IStorage16_fnCreateStorage(
1609 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1611 ICOM_THIS(IStorage16Impl,iface);
1612 IStorage16Impl* lpstg;
1614 struct storage_pps_entry stde;
1615 struct storage_header sth;
1622 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1623 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1625 if (grfMode & STGM_TRANSACTED)
1626 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1627 _create_istorage16(ppstg);
1628 lpstg = MapSL((SEGPTR)*ppstg);
1629 lpstg->hf = This->hf;
1631 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1635 if (stde.pps_dir==-1) {
1636 stde.pps_dir = ppsent;
1639 FIXME(" use prev chain too ?\n");
1641 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1643 while (stde.pps_next!=-1) {
1645 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1648 stde.pps_next = ppsent;
1650 ret = STORAGE_put_pps_entry(lpstg->hf,x,&stde);
1652 nPPSEntries = STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde));
1653 assert(nPPSEntries == 1);
1654 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstg->stde.pps_rawname,
1655 sizeof(lpstg->stde.pps_rawname)/sizeof(WCHAR));
1656 lpstg->stde.pps_sizeofname = (strlenW(lpstg->stde.pps_rawname)+1)*sizeof(WCHAR);
1657 lpstg->stde.pps_next = -1;
1658 lpstg->stde.pps_prev = -1;
1659 lpstg->stde.pps_dir = -1;
1660 lpstg->stde.pps_sb = -1;
1661 lpstg->stde.pps_size = 0;
1662 lpstg->stde.pps_type = 1;
1663 lpstg->ppsent = ppsent;
1664 /* FIXME: timestamps? */
1665 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1670 /******************************************************************************
1671 * IStorage16_CreateStream [STORAGE.503]
1673 HRESULT WINAPI IStorage16_fnCreateStream(
1674 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1676 ICOM_THIS(IStorage16Impl,iface);
1677 IStream16Impl* lpstr;
1679 struct storage_pps_entry stde;
1683 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1684 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1686 if (grfMode & STGM_TRANSACTED)
1687 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1688 _create_istream16(ppstm);
1689 lpstr = MapSL((SEGPTR)*ppstm);
1690 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1691 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1692 lpstr->offset.u.LowPart = 0;
1693 lpstr->offset.u.HighPart = 0;
1695 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1699 if (stde.pps_next==-1)
1702 while (stde.pps_next!=-1) {
1704 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1707 stde.pps_next = ppsent;
1708 ret = STORAGE_put_pps_entry(lpstr->hf,x,&stde);
1710 nPPSEntries = STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde));
1711 assert(nPPSEntries == 1);
1712 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstr->stde.pps_rawname,
1713 sizeof(lpstr->stde.pps_rawname)/sizeof(WCHAR));
1714 lpstr->stde.pps_sizeofname = (strlenW(lpstr->stde.pps_rawname)+1) * sizeof(WCHAR);
1715 lpstr->stde.pps_next = -1;
1716 lpstr->stde.pps_prev = -1;
1717 lpstr->stde.pps_dir = -1;
1718 lpstr->stde.pps_sb = -1;
1719 lpstr->stde.pps_size = 0;
1720 lpstr->stde.pps_type = 2;
1721 lpstr->ppsent = ppsent;
1722 /* FIXME: timestamps? */
1723 if (!STORAGE_put_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)))
1728 /******************************************************************************
1729 * IStorage16_OpenStorage [STORAGE.506]
1731 HRESULT WINAPI IStorage16_fnOpenStorage(
1732 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, IStorage16 *pstgPrio, DWORD grfMode, SNB16 snbExclude, DWORD reserved, IStorage16 **ppstg
1734 ICOM_THIS(IStorage16Impl,iface);
1735 IStream16Impl* lpstg;
1739 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1740 This,pwcsName,pstgPrio,grfMode,snbExclude,reserved,ppstg
1742 if (grfMode & STGM_TRANSACTED)
1743 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1744 _create_istorage16(ppstg);
1745 lpstg = MapSL((SEGPTR)*ppstg);
1746 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1747 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1748 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1749 newpps = STORAGE_look_for_named_pps(lpstg->hf,This->stde.pps_dir,name);
1751 IStream16_fnRelease((IStream16*)lpstg);
1755 if (1!=STORAGE_get_pps_entry(lpstg->hf,newpps,&(lpstg->stde))) {
1756 IStream16_fnRelease((IStream16*)lpstg);
1759 lpstg->ppsent = newpps;
1763 /******************************************************************************
1764 * IStorage16_OpenStream [STORAGE.504]
1766 HRESULT WINAPI IStorage16_fnOpenStream(
1767 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream16 **ppstm
1769 ICOM_THIS(IStorage16Impl,iface);
1770 IStream16Impl* lpstr;
1774 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1775 This,pwcsName,reserved1,grfMode,reserved2,ppstm
1777 if (grfMode & STGM_TRANSACTED)
1778 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1779 _create_istream16(ppstm);
1780 lpstr = MapSL((SEGPTR)*ppstm);
1781 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1782 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1783 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1784 newpps = STORAGE_look_for_named_pps(lpstr->hf,This->stde.pps_dir,name);
1786 IStream16_fnRelease((IStream16*)lpstr);
1790 if (1!=STORAGE_get_pps_entry(lpstr->hf,newpps,&(lpstr->stde))) {
1791 IStream16_fnRelease((IStream16*)lpstr);
1794 lpstr->offset.u.LowPart = 0;
1795 lpstr->offset.u.HighPart = 0;
1796 lpstr->ppsent = newpps;
1800 /******************************************************************************
1801 * _create_istorage16 [INTERNAL]
1803 static void _create_istorage16(LPSTORAGE16 *stg) {
1804 IStorage16Impl* lpst;
1806 if (!stvt16.QueryInterface) {
1807 HMODULE16 wp = GetModuleHandle16("STORAGE");
1809 #define VTENT(xfn) stvt16.xfn = (void*)GetProcAddress16(wp,"IStorage16_"#xfn);
1810 VTENT(QueryInterface)
1815 VTENT(CreateStorage)
1818 VTENT(MoveElementTo)
1822 VTENT(DestroyElement)
1823 VTENT(RenameElement)
1824 VTENT(SetElementTimes)
1829 segstvt16 = (ICOM_VTABLE(IStorage16)*)MapLS( &stvt16 );
1831 #define VTENT(xfn) stvt16.xfn = IStorage16_fn##xfn;
1832 VTENT(QueryInterface)
1837 VTENT(CreateStorage)
1841 /* not (yet) implemented ...
1842 VTENT(MoveElementTo)
1845 VTENT(DestroyElement)
1846 VTENT(RenameElement)
1847 VTENT(SetElementTimes)
1853 segstvt16 = &stvt16;
1856 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1857 lpst->lpVtbl = segstvt16;
1859 lpst->thisptr = MapLS(lpst);
1860 *stg = (void*)lpst->thisptr;
1863 /******************************************************************************
1864 * Storage API functions
1867 /******************************************************************************
1868 * StgCreateDocFileA [STORAGE.1]
1870 HRESULT WINAPI StgCreateDocFile16(
1871 LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved,IStorage16 **ppstgOpen
1875 IStorage16Impl* lpstg;
1876 struct storage_pps_entry stde;
1878 TRACE("(%s,0x%08lx,0x%08lx,%p)\n",
1879 pwcsName,grfMode,reserved,ppstgOpen
1881 _create_istorage16(ppstgOpen);
1882 hf = CreateFileA(pwcsName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,0,0);
1883 if (hf==INVALID_HANDLE_VALUE) {
1884 WARN("couldn't open file for storage:%ld\n",GetLastError());
1887 lpstg = MapSL((SEGPTR)*ppstgOpen);
1889 /* FIXME: check for existence before overwriting? */
1890 if (!STORAGE_init_storage(hf)) {
1895 while (!ret) { /* neither 1 nor <0 */
1896 ret=STORAGE_get_pps_entry(hf,i,&stde);
1897 if ((ret==1) && (stde.pps_type==5)) {
1905 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1912 /******************************************************************************
1913 * StgIsStorageFile [STORAGE.5]
1915 HRESULT WINAPI StgIsStorageFile16(LPCOLESTR16 fn) {
1916 UNICODE_STRING strW;
1919 RtlCreateUnicodeStringFromAsciiz(&strW, fn);
1920 ret = StgIsStorageFile( strW.Buffer );
1921 RtlFreeUnicodeString( &strW );
1926 /******************************************************************************
1927 * StgOpenStorage [STORAGE.3]
1929 HRESULT WINAPI StgOpenStorage16(
1930 LPCOLESTR16 pwcsName,IStorage16 *pstgPriority,DWORD grfMode,
1931 SNB16 snbExclude,DWORD reserved, IStorage16 **ppstgOpen
1935 IStorage16Impl* lpstg;
1936 struct storage_pps_entry stde;
1938 TRACE("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1939 pwcsName,pstgPriority,grfMode,snbExclude,reserved,ppstgOpen
1941 _create_istorage16(ppstgOpen);
1942 hf = CreateFileA(pwcsName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
1943 if (hf==INVALID_HANDLE_VALUE) {
1944 WARN("Couldn't open file for storage\n");
1947 lpstg = MapSL((SEGPTR)*ppstgOpen);
1951 while (!ret) { /* neither 1 nor <0 */
1952 ret=STORAGE_get_pps_entry(hf,i,&stde);
1953 if ((ret==1) && (stde.pps_type==5)) {
1960 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1967 /******************************************************************************
1968 * StgIsStorageILockBytes [STORAGE.6]
1970 * Determines if the ILockBytes contains a storage object.
1972 HRESULT WINAPI StgIsStorageILockBytes16(SEGPTR plkbyt)
1978 args[0] = (DWORD)plkbyt; /* iface */
1979 args[1] = args[2] = 0; /* ULARGE_INTEGER offset */
1980 args[3] = (DWORD)K32WOWGlobalAllocLock16( 0, 8, &hsig ); /* sig */
1984 if (!K32WOWCallback16Ex(
1985 (DWORD)((ICOM_VTABLE(ILockBytes16)*)MapSL(
1986 (SEGPTR)((LPLOCKBYTES16)MapSL(plkbyt))->lpVtbl)
1993 ERR("CallTo16 ILockBytes16::ReadAt() failed, hres %lx\n",hres);
1996 if (memcmp(MapSL(args[3]), STORAGE_magic, sizeof(STORAGE_magic)) == 0) {
1997 K32WOWGlobalUnlockFree16(args[3]);
2000 K32WOWGlobalUnlockFree16(args[3]);
2004 /******************************************************************************
2005 * StgOpenStorageOnILockBytes [STORAGE.4]
2007 HRESULT WINAPI StgOpenStorageOnILockBytes16(
2008 ILockBytes16 *plkbyt,
2009 IStorage16 *pstgPriority,
2013 IStorage16 **ppstgOpen)
2015 IStorage16Impl* lpstg;
2017 if ((plkbyt == 0) || (ppstgOpen == 0))
2018 return STG_E_INVALIDPOINTER;
2022 _create_istorage16(ppstgOpen);
2023 lpstg = MapSL((SEGPTR)*ppstgOpen);
2025 /* just teach it to use HANDLE instead of ilockbytes :/ */