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
29 #include <sys/types.h>
34 #define NONAMELESSUNION
35 #define NONAMELESSSTRUCT
39 #include "wine/winbase16.h"
41 #include "wine/unicode.h"
43 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(ole);
48 WINE_DECLARE_DEBUG_CHANNEL(relay);
50 struct storage_header {
51 BYTE magic[8]; /* 00: magic */
52 BYTE unknown1[36]; /* 08: unknown */
53 DWORD num_of_bbd_blocks;/* 2C: length of big datablocks */
54 DWORD root_startblock;/* 30: root storage first big block */
55 DWORD unknown2[2]; /* 34: unknown */
56 DWORD sbd_startblock; /* 3C: small block depot first big block */
57 DWORD unknown3[3]; /* 40: unknown */
58 DWORD bbd_list[109]; /* 4C: big data block list (up to end of sector)*/
60 struct storage_pps_entry {
61 WCHAR pps_rawname[32];/* 00: \0 terminated widechar name */
62 WORD pps_sizeofname; /* 40: namelength in bytes */
63 BYTE pps_type; /* 42: flags, 1 storage/dir, 2 stream, 5 root */
64 BYTE pps_unknown0; /* 43: unknown */
65 DWORD pps_prev; /* 44: previous pps */
66 DWORD pps_next; /* 48: next pps */
67 DWORD pps_dir; /* 4C: directory pps */
68 GUID pps_guid; /* 50: class ID */
69 DWORD pps_unknown1; /* 60: unknown */
70 FILETIME pps_ft1; /* 64: filetime1 */
71 FILETIME pps_ft2; /* 70: filetime2 */
72 DWORD pps_sb; /* 74: data startblock */
73 DWORD pps_size; /* 78: datalength. (<0x1000)?small:big blocks*/
74 DWORD pps_unknown2; /* 7C: unknown */
77 #define STORAGE_CHAINENTRY_FAT 0xfffffffd
78 #define STORAGE_CHAINENTRY_ENDOFCHAIN 0xfffffffe
79 #define STORAGE_CHAINENTRY_FREE 0xffffffff
82 static const BYTE STORAGE_magic[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
87 #define SMALLBLOCKS_PER_BIGBLOCK (BIGSIZE/SMALLSIZE)
89 #define READ_HEADER assert(STORAGE_get_big_block(hf,-1,(LPBYTE)&sth));assert(!memcmp(STORAGE_magic,sth.magic,sizeof(STORAGE_magic)));
90 static ICOM_VTABLE(IStorage16) stvt16;
91 static ICOM_VTABLE(IStorage16) *segstvt16 = NULL;
92 static ICOM_VTABLE(IStream16) strvt16;
93 static ICOM_VTABLE(IStream16) *segstrvt16 = NULL;
95 /*ULONG WINAPI IStorage16_AddRef(LPSTORAGE16 this);*/
96 static void _create_istorage16(LPSTORAGE16 *stg);
97 static void _create_istream16(LPSTREAM16 *str);
102 /******************************************************************************
103 * STORAGE_get_big_block [Internal]
105 * Reading OLE compound storage
108 STORAGE_get_big_block(HANDLE hf,int n,BYTE *block)
113 if (!SetFilePointer( hf, (n+1)*BIGSIZE, NULL, SEEK_SET ))
115 WARN(" seek failed (%ld)\n",GetLastError());
118 if (!ReadFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE)
120 WARN("(block size %d): read didn't read (%ld)\n",n,GetLastError());
126 /******************************************************************************
127 * STORAGE_put_big_block [INTERNAL]
130 STORAGE_put_big_block(HANDLE hf,int n,BYTE *block)
135 if (!SetFilePointer( hf, (n+1)*BIGSIZE, NULL, SEEK_SET ))
137 WARN(" seek failed (%ld)\n",GetLastError());
140 if (!WriteFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE)
142 WARN(" write failed (%ld)\n",GetLastError());
148 /******************************************************************************
149 * STORAGE_get_next_big_blocknr [INTERNAL]
152 STORAGE_get_next_big_blocknr(HANDLE hf,int blocknr) {
153 INT bbs[BIGSIZE/sizeof(INT)];
154 struct storage_header sth;
158 assert(blocknr>>7<sth.num_of_bbd_blocks);
159 if (sth.bbd_list[blocknr>>7]==0xffffffff)
161 if (!STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs))
163 assert(bbs[blocknr&0x7f]!=STORAGE_CHAINENTRY_FREE);
164 return bbs[blocknr&0x7f];
167 /******************************************************************************
168 * STORAGE_get_nth_next_big_blocknr [INTERNAL]
171 STORAGE_get_nth_next_big_blocknr(HANDLE hf,int blocknr,int nr) {
172 INT bbs[BIGSIZE/sizeof(INT)];
174 struct storage_header sth;
180 assert((blocknr>>7)<sth.num_of_bbd_blocks);
181 assert(sth.bbd_list[blocknr>>7]!=0xffffffff);
183 /* simple caching... */
184 if (lastblock!=sth.bbd_list[blocknr>>7]) {
185 assert(STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs));
186 lastblock = sth.bbd_list[blocknr>>7];
188 blocknr = bbs[blocknr&0x7f];
193 /******************************************************************************
194 * STORAGE_get_root_pps_entry [Internal]
197 STORAGE_get_root_pps_entry(HANDLE hf,struct storage_pps_entry *pstde) {
200 struct storage_pps_entry *stde=(struct storage_pps_entry*)block;
201 struct storage_header sth;
204 blocknr = sth.root_startblock;
206 assert(STORAGE_get_big_block(hf,blocknr,block));
208 if (!stde[i].pps_sizeofname)
210 if (stde[i].pps_type==5) {
215 blocknr=STORAGE_get_next_big_blocknr(hf,blocknr);
220 /******************************************************************************
221 * STORAGE_get_small_block [INTERNAL]
224 STORAGE_get_small_block(HANDLE hf,int blocknr,BYTE *sblock) {
227 struct storage_pps_entry root;
230 assert(STORAGE_get_root_pps_entry(hf,&root));
231 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
232 assert(bigblocknr>=0);
233 assert(STORAGE_get_big_block(hf,bigblocknr,block));
235 memcpy(sblock,((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),SMALLSIZE);
239 /******************************************************************************
240 * STORAGE_put_small_block [INTERNAL]
243 STORAGE_put_small_block(HANDLE hf,int blocknr,BYTE *sblock) {
246 struct storage_pps_entry root;
250 assert(STORAGE_get_root_pps_entry(hf,&root));
251 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
252 assert(bigblocknr>=0);
253 assert(STORAGE_get_big_block(hf,bigblocknr,block));
255 memcpy(((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),sblock,SMALLSIZE);
256 assert(STORAGE_put_big_block(hf,bigblocknr,block));
260 /******************************************************************************
261 * STORAGE_get_next_small_blocknr [INTERNAL]
264 STORAGE_get_next_small_blocknr(HANDLE hf,int blocknr) {
266 LPINT sbd = (LPINT)block;
268 struct storage_header sth;
272 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
273 assert(bigblocknr>=0);
274 assert(STORAGE_get_big_block(hf,bigblocknr,block));
275 assert(sbd[blocknr & 127]!=STORAGE_CHAINENTRY_FREE);
276 return sbd[blocknr & (128-1)];
279 /******************************************************************************
280 * STORAGE_get_nth_next_small_blocknr [INTERNAL]
283 STORAGE_get_nth_next_small_blocknr(HANDLE hf,int blocknr,int nr) {
286 LPINT sbd = (LPINT)block;
287 struct storage_header sth;
292 while ((nr--) && (blocknr>=0)) {
293 if (lastblocknr/128!=blocknr/128) {
295 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
296 assert(bigblocknr>=0);
297 assert(STORAGE_get_big_block(hf,bigblocknr,block));
298 lastblocknr = blocknr;
300 assert(lastblocknr>=0);
302 blocknr=sbd[blocknr & (128-1)];
303 assert(blocknr!=STORAGE_CHAINENTRY_FREE);
308 /******************************************************************************
309 * STORAGE_get_pps_entry [INTERNAL]
312 STORAGE_get_pps_entry(HANDLE hf,int n,struct storage_pps_entry *pstde) {
315 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
316 struct storage_header sth;
319 /* we have 4 pps entries per big block */
320 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
322 assert(STORAGE_get_big_block(hf,blocknr,block));
328 /******************************************************************************
329 * STORAGE_put_pps_entry [Internal]
332 STORAGE_put_pps_entry(HANDLE hf,int n,struct storage_pps_entry *pstde) {
335 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
336 struct storage_header sth;
340 /* we have 4 pps entries per big block */
341 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
343 assert(STORAGE_get_big_block(hf,blocknr,block));
345 assert(STORAGE_put_big_block(hf,blocknr,block));
349 /******************************************************************************
350 * STORAGE_look_for_named_pps [Internal]
353 STORAGE_look_for_named_pps(HANDLE hf,int n,LPOLESTR name) {
354 struct storage_pps_entry stde;
359 if (1!=STORAGE_get_pps_entry(hf,n,&stde))
362 if (!lstrcmpW(name,stde.pps_rawname))
364 if (stde.pps_prev != -1) {
365 ret=STORAGE_look_for_named_pps(hf,stde.pps_prev,name);
369 if (stde.pps_next != -1) {
370 ret=STORAGE_look_for_named_pps(hf,stde.pps_next,name);
377 /******************************************************************************
378 * STORAGE_dump_pps_entry [Internal]
384 STORAGE_dump_pps_entry(struct storage_pps_entry *stde) {
387 WideCharToMultiByte( CP_ACP, 0, stde->pps_rawname, -1, name, sizeof(name), NULL, NULL);
388 if (!stde->pps_sizeofname)
390 DPRINTF("name: %s\n",name);
391 DPRINTF("type: %d\n",stde->pps_type);
392 DPRINTF("prev pps: %ld\n",stde->pps_prev);
393 DPRINTF("next pps: %ld\n",stde->pps_next);
394 DPRINTF("dir pps: %ld\n",stde->pps_dir);
395 DPRINTF("guid: %s\n",debugstr_guid(&(stde->pps_guid)));
396 if (stde->pps_type !=2) {
399 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&(stde->pps_ft1),&dw);
401 DPRINTF("ts1: %s\n",ctime(&t));
402 RtlTimeToSecondsSince1970((LARGE_INTEGER *)&(stde->pps_ft2),&dw);
404 DPRINTF("ts2: %s\n",ctime(&t));
406 DPRINTF("startblock: %ld\n",stde->pps_sb);
407 DPRINTF("size: %ld\n",stde->pps_size);
410 /******************************************************************************
411 * STORAGE_init_storage [INTERNAL]
414 STORAGE_init_storage(HANDLE hf) {
417 struct storage_header *sth;
418 struct storage_pps_entry *stde;
421 SetFilePointer( hf, 0, NULL, SEEK_SET );
422 /* block -1 is the storage header */
423 sth = (struct storage_header*)block;
424 memcpy(sth->magic,STORAGE_magic,8);
425 memset(sth->unknown1,0,sizeof(sth->unknown1));
426 memset(sth->unknown2,0,sizeof(sth->unknown2));
427 memset(sth->unknown3,0,sizeof(sth->unknown3));
428 sth->num_of_bbd_blocks = 1;
429 sth->root_startblock = 1;
430 sth->sbd_startblock = 0xffffffff;
431 memset(sth->bbd_list,0xff,sizeof(sth->bbd_list));
432 sth->bbd_list[0] = 0;
433 if (!WriteFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE) return FALSE;
434 /* block 0 is the big block directory */
436 memset(block,0xff,sizeof(block)); /* mark all blocks as free */
437 bbs[0]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for this block */
438 bbs[1]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for directory entry */
439 if (!WriteFile( hf, block, BIGSIZE, &result, NULL ) || result != BIGSIZE) return FALSE;
440 /* block 1 is the root directory entry */
441 memset(block,0x00,sizeof(block));
442 stde = (struct storage_pps_entry*)block;
443 MultiByteToWideChar( CP_ACP, 0, "RootEntry", -1, stde->pps_rawname,
444 sizeof(stde->pps_rawname)/sizeof(WCHAR));
445 stde->pps_sizeofname = (strlenW(stde->pps_rawname)+1) * sizeof(WCHAR);
450 stde->pps_sb = 0xffffffff;
452 return (WriteFile( hf, block, BIGSIZE, &result, NULL ) && result == BIGSIZE);
455 /******************************************************************************
456 * STORAGE_set_big_chain [Internal]
459 STORAGE_set_big_chain(HANDLE hf,int blocknr,INT type) {
461 LPINT bbd = (LPINT)block;
462 int nextblocknr,bigblocknr;
463 struct storage_header sth;
466 assert(blocknr!=type);
468 bigblocknr = sth.bbd_list[blocknr/128];
469 assert(bigblocknr>=0);
470 assert(STORAGE_get_big_block(hf,bigblocknr,block));
472 nextblocknr = bbd[blocknr&(128-1)];
473 bbd[blocknr&(128-1)] = type;
476 assert(STORAGE_put_big_block(hf,bigblocknr,block));
477 type = STORAGE_CHAINENTRY_FREE;
478 blocknr = nextblocknr;
483 /******************************************************************************
484 * STORAGE_set_small_chain [Internal]
487 STORAGE_set_small_chain(HANDLE hf,int blocknr,INT type) {
489 LPINT sbd = (LPINT)block;
490 int lastblocknr,nextsmallblocknr,bigblocknr;
491 struct storage_header sth;
495 assert(blocknr!=type);
496 lastblocknr=-129;bigblocknr=-2;
498 /* cache block ... */
499 if (lastblocknr/128!=blocknr/128) {
500 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
501 assert(bigblocknr>=0);
502 assert(STORAGE_get_big_block(hf,bigblocknr,block));
504 lastblocknr = blocknr;
505 nextsmallblocknr = sbd[blocknr&(128-1)];
506 sbd[blocknr&(128-1)] = type;
507 assert(STORAGE_put_big_block(hf,bigblocknr,block));
510 type = STORAGE_CHAINENTRY_FREE;
511 blocknr = nextsmallblocknr;
516 /******************************************************************************
517 * STORAGE_get_free_big_blocknr [Internal]
520 STORAGE_get_free_big_blocknr(HANDLE hf) {
522 LPINT sbd = (LPINT)block;
523 int lastbigblocknr,i,curblock,bigblocknr;
524 struct storage_header sth;
529 bigblocknr = sth.bbd_list[curblock];
530 while (curblock<sth.num_of_bbd_blocks) {
531 assert(bigblocknr>=0);
532 assert(STORAGE_get_big_block(hf,bigblocknr,block));
534 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
535 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
536 assert(STORAGE_put_big_block(hf,bigblocknr,block));
537 memset(block,0x42,sizeof(block));
538 assert(STORAGE_put_big_block(hf,i+curblock*128,block));
539 return i+curblock*128;
541 lastbigblocknr = bigblocknr;
542 bigblocknr = sth.bbd_list[++curblock];
544 bigblocknr = curblock*128;
545 /* since we have marked all blocks from 0 up to curblock*128-1
546 * the next free one is curblock*128, where we happily put our
547 * next large block depot.
549 memset(block,0xff,sizeof(block));
550 /* mark the block allocated and returned by this function */
551 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
552 assert(STORAGE_put_big_block(hf,bigblocknr,block));
554 /* if we had a bbd block already (mostlikely) we need
555 * to link the new one into the chain
557 if (lastbigblocknr!=-1)
558 assert(STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr));
559 sth.bbd_list[curblock]=bigblocknr;
560 sth.num_of_bbd_blocks++;
561 assert(sth.num_of_bbd_blocks==curblock+1);
562 assert(STORAGE_put_big_block(hf,-1,(LPBYTE)&sth));
564 /* Set the end of the chain for the bigblockdepots */
565 assert(STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN));
566 /* add 1, for the first entry is used for the additional big block
567 * depot. (means we already used bigblocknr) */
568 memset(block,0x42,sizeof(block));
569 /* allocate this block (filled with 0x42) */
570 assert(STORAGE_put_big_block(hf,bigblocknr+1,block));
575 /******************************************************************************
576 * STORAGE_get_free_small_blocknr [Internal]
579 STORAGE_get_free_small_blocknr(HANDLE hf) {
581 LPINT sbd = (LPINT)block;
582 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
583 struct storage_pps_entry root;
584 struct storage_header sth;
587 bigblocknr = sth.sbd_startblock;
591 while (bigblocknr>=0) {
592 if (!STORAGE_get_big_block(hf,bigblocknr,block))
595 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
596 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
597 newblocknr = i+curblock*128;
602 lastbigblocknr = bigblocknr;
603 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
606 if (newblocknr==-1) {
607 bigblocknr = STORAGE_get_free_big_blocknr(hf);
611 memset(block,0xff,sizeof(block));
612 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
613 if (!STORAGE_put_big_block(hf,bigblocknr,block))
615 if (lastbigblocknr==-1) {
616 sth.sbd_startblock = bigblocknr;
617 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
620 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
623 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
625 newblocknr = curblock*128;
627 /* allocate enough big blocks for storing the allocated small block */
628 if (!STORAGE_get_root_pps_entry(hf,&root))
633 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
634 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
635 /* we need to allocate more stuff */
636 bigblocknr = STORAGE_get_free_big_blocknr(hf);
640 if (root.pps_sb==-1) {
641 root.pps_sb = bigblocknr;
642 root.pps_size += BIGSIZE;
644 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
646 root.pps_size += BIGSIZE;
648 lastbigblocknr = bigblocknr;
650 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
652 if (!STORAGE_put_pps_entry(hf,0,&root))
657 /******************************************************************************
658 * STORAGE_get_free_pps_entry [Internal]
661 STORAGE_get_free_pps_entry(HANDLE hf) {
662 int blocknr,i,curblock,lastblocknr;
664 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
665 struct storage_header sth;
668 blocknr = sth.root_startblock;
672 if (!STORAGE_get_big_block(hf,blocknr,block))
675 if (stde[i].pps_sizeofname==0) /* free */
677 lastblocknr = blocknr;
678 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
681 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
682 blocknr = STORAGE_get_free_big_blocknr(hf);
683 /* sth invalidated */
687 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
689 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
691 memset(block,0,sizeof(block));
692 STORAGE_put_big_block(hf,blocknr,block);
696 /* --- IStream16 implementation */
700 /* IUnknown fields */
701 ICOM_VFIELD(IStream16);
703 /* IStream16 fields */
704 SEGPTR thisptr; /* pointer to this struct as segmented */
705 struct storage_pps_entry stde;
708 ULARGE_INTEGER offset;
711 /******************************************************************************
712 * IStream16_QueryInterface [STORAGE.518]
714 HRESULT WINAPI IStream16_fnQueryInterface(
715 IStream16* iface,REFIID refiid,LPVOID *obj
717 ICOM_THIS(IStream16Impl,iface);
718 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
719 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
723 return OLE_E_ENUM_NOMORE;
727 /******************************************************************************
728 * IStream16_AddRef [STORAGE.519]
730 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
731 ICOM_THIS(IStream16Impl,iface);
732 return ++(This->ref);
735 /******************************************************************************
736 * IStream16_Release [STORAGE.520]
738 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
739 ICOM_THIS(IStream16Impl,iface);
740 FlushFileBuffers(This->hf);
743 CloseHandle(This->hf);
744 UnMapLS( This->thisptr );
745 HeapFree( GetProcessHeap(), 0, This );
751 /******************************************************************************
752 * IStream16_Seek [STORAGE.523]
755 * Does not handle 64 bits
757 HRESULT WINAPI IStream16_fnSeek(
758 IStream16* iface,LARGE_INTEGER offset,DWORD whence,ULARGE_INTEGER *newpos
760 ICOM_THIS(IStream16Impl,iface);
761 TRACE_(relay)("(%p)->([%ld.%ld],%ld,%p)\n",This,offset.s.HighPart,offset.s.LowPart,whence,newpos);
764 /* unix SEEK_xx should be the same as win95 ones */
766 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
769 assert(offset.s.HighPart==0);
770 This->offset.s.HighPart = offset.s.HighPart;
771 This->offset.s.LowPart = offset.s.LowPart;
774 if (offset.s.HighPart < 0) {
775 /* FIXME: is this negation correct ? */
776 offset.s.HighPart = -offset.s.HighPart;
777 offset.s.LowPart = (0xffffffff ^ offset.s.LowPart)+1;
779 assert(offset.s.HighPart==0);
780 assert(This->offset.s.LowPart >= offset.s.LowPart);
781 This->offset.s.LowPart -= offset.s.LowPart;
783 assert(offset.s.HighPart==0);
784 This->offset.s.LowPart+= offset.s.LowPart;
788 assert(offset.s.HighPart==0);
789 This->offset.s.LowPart = This->stde.pps_size-offset.s.LowPart;
792 if (This->offset.s.LowPart>This->stde.pps_size)
793 This->offset.s.LowPart=This->stde.pps_size;
794 if (newpos) *newpos = This->offset;
798 /******************************************************************************
799 * IStream16_Read [STORAGE.521]
801 HRESULT WINAPI IStream16_fnRead(
802 IStream16* iface,void *pv,ULONG cb,ULONG *pcbRead
804 ICOM_THIS(IStream16Impl,iface);
806 ULONG *bytesread=pcbRead,xxread;
809 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbRead);
810 if (!pcbRead) bytesread=&xxread;
813 if (cb>This->stde.pps_size-This->offset.s.LowPart)
814 cb=This->stde.pps_size-This->offset.s.LowPart;
815 if (This->stde.pps_size < 0x1000) {
816 /* use small block reader */
817 blocknr = STORAGE_get_nth_next_small_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
821 if (!STORAGE_get_small_block(This->hf,blocknr,block)) {
822 WARN("small block read failed!!!\n");
826 if (cc>SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1)))
827 cc=SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
828 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(SMALLSIZE-1)),cc);
829 This->offset.s.LowPart+=cc;
833 blocknr = STORAGE_get_next_small_blocknr(This->hf,blocknr);
836 /* use big block reader */
837 blocknr = STORAGE_get_nth_next_big_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
841 if (!STORAGE_get_big_block(This->hf,blocknr,block)) {
842 WARN("big block read failed!!!\n");
846 if (cc>BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1)))
847 cc=BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
848 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(BIGSIZE-1)),cc);
849 This->offset.s.LowPart+=cc;
853 blocknr=STORAGE_get_next_big_blocknr(This->hf,blocknr);
859 /******************************************************************************
860 * IStream16_Write [STORAGE.522]
862 HRESULT WINAPI IStream16_fnWrite(
863 IStream16* iface,const void *pv,ULONG cb,ULONG *pcbWrite
865 ICOM_THIS(IStream16Impl,iface);
867 ULONG *byteswritten=pcbWrite,xxwritten;
868 int oldsize,newsize,i,curoffset=0,lastblocknr,blocknr,cc;
869 HANDLE hf = This->hf;
871 if (!pcbWrite) byteswritten=&xxwritten;
874 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbWrite);
875 /* do we need to junk some blocks? */
876 newsize = This->offset.s.LowPart+cb;
877 oldsize = This->stde.pps_size;
878 if (newsize < oldsize) {
879 if (oldsize < 0x1000) {
880 /* only small blocks */
881 blocknr=STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,newsize/SMALLSIZE);
885 /* will set the rest of the chain to 'free' */
886 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
889 if (newsize >= 0x1000) {
890 blocknr=STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,newsize/BIGSIZE);
893 /* will set the rest of the chain to 'free' */
894 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
897 /* Migrate large blocks to small blocks
898 * (we just migrate newsize bytes)
900 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,newsize+BIGSIZE);
904 blocknr = This->stde.pps_sb;
907 if (!STORAGE_get_big_block(hf,blocknr,curdata)) {
908 HeapFree(GetProcessHeap(),0,data);
913 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
915 /* frees complete chain for this stream */
916 if (!STORAGE_set_big_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
919 blocknr = This->stde.pps_sb = STORAGE_get_free_small_blocknr(hf);
924 if (!STORAGE_put_small_block(hf,blocknr,curdata))
928 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
932 int newblocknr = STORAGE_get_free_small_blocknr(hf);
935 if (!STORAGE_set_small_chain(hf,blocknr,newblocknr))
937 blocknr = newblocknr;
939 curdata += SMALLSIZE;
943 HeapFree(GetProcessHeap(),0,data);
948 This->stde.pps_size = newsize;
951 if (newsize > oldsize) {
952 if (oldsize >= 0x1000) {
953 /* should return the block right before the 'endofchain' */
954 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/BIGSIZE);
956 lastblocknr = blocknr;
957 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
958 blocknr = STORAGE_get_free_big_blocknr(hf);
961 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
963 lastblocknr = blocknr;
965 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
968 if (newsize < 0x1000) {
969 /* find startblock */
971 This->stde.pps_sb = blocknr = STORAGE_get_free_small_blocknr(hf);
973 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/SMALLSIZE);
977 /* allocate required new small blocks */
978 lastblocknr = blocknr;
979 for (i=oldsize/SMALLSIZE;i<newsize/SMALLSIZE;i++) {
980 blocknr = STORAGE_get_free_small_blocknr(hf);
983 if (!STORAGE_set_small_chain(hf,lastblocknr,blocknr))
985 lastblocknr = blocknr;
987 /* and terminate the chain */
988 if (!STORAGE_set_small_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
992 /* no single block allocated yet */
993 blocknr=STORAGE_get_free_big_blocknr(hf);
996 This->stde.pps_sb = blocknr;
998 /* Migrate small blocks to big blocks */
999 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,oldsize+BIGSIZE);
1003 blocknr = This->stde.pps_sb;
1007 if (!STORAGE_get_small_block(hf,blocknr,curdata))
1009 curdata += SMALLSIZE;
1011 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1013 /* free small block chain */
1014 if (!STORAGE_set_small_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
1017 blocknr = This->stde.pps_sb = STORAGE_get_free_big_blocknr(hf);
1020 /* put the data into the big blocks */
1021 cc = This->stde.pps_size;
1023 if (!STORAGE_put_big_block(hf,blocknr,curdata))
1027 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1031 int newblocknr = STORAGE_get_free_big_blocknr(hf);
1034 if (!STORAGE_set_big_chain(hf,blocknr,newblocknr))
1036 blocknr = newblocknr;
1042 HeapFree(GetProcessHeap(),0,data);
1046 /* generate big blocks to fit the new data */
1047 lastblocknr = blocknr;
1048 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1049 blocknr = STORAGE_get_free_big_blocknr(hf);
1052 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1054 lastblocknr = blocknr;
1056 /* terminate chain */
1057 if (!STORAGE_set_big_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1061 This->stde.pps_size = newsize;
1064 /* There are just some cases where we didn't modify it, we write it out
1067 if (!STORAGE_put_pps_entry(hf,This->ppsent,&(This->stde)))
1070 /* finally the write pass */
1071 if (This->stde.pps_size < 0x1000) {
1072 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
1075 /* we ensured that it is allocated above */
1077 /* Read old block everytime, since we can have
1078 * overlapping data at START and END of the write
1080 if (!STORAGE_get_small_block(hf,blocknr,block))
1083 cc = SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
1086 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(SMALLSIZE-1)),
1087 (LPBYTE)((char *) pv+curoffset),
1090 if (!STORAGE_put_small_block(hf,blocknr,block))
1095 This->offset.s.LowPart += cc;
1096 *byteswritten += cc;
1097 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1100 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
1103 /* we ensured that it is allocated above, so it better is */
1105 /* read old block everytime, since we can have
1106 * overlapping data at START and END of the write
1108 if (!STORAGE_get_big_block(hf,blocknr,block))
1111 cc = BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
1114 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(BIGSIZE-1)),
1115 (LPBYTE)((char *) pv+curoffset),
1118 if (!STORAGE_put_big_block(hf,blocknr,block))
1123 This->offset.s.LowPart += cc;
1124 *byteswritten += cc;
1125 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1131 /******************************************************************************
1132 * _create_istream16 [Internal]
1134 static void _create_istream16(LPSTREAM16 *str) {
1135 IStream16Impl* lpst;
1137 if (!strvt16.QueryInterface) {
1138 HMODULE16 wp = GetModuleHandle16("STORAGE");
1140 /* FIXME: what is This GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1141 #define VTENT(xfn) strvt16.xfn = (void*)GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.xfn)
1142 VTENT(QueryInterface);
1153 VTENT(UnlockRegion);
1157 segstrvt16 = (ICOM_VTABLE(IStream16)*)MapLS( &strvt16 );
1159 #define VTENT(xfn) strvt16.xfn = IStream16_fn##xfn;
1160 VTENT(QueryInterface);
1172 VTENT(UnlockRegion);
1177 segstrvt16 = &strvt16;
1180 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1181 ICOM_VTBL(lpst) = segstrvt16;
1183 lpst->thisptr = MapLS( lpst );
1184 *str = (void*)lpst->thisptr;
1188 /* --- IStream32 implementation */
1192 /* IUnknown fields */
1193 ICOM_VFIELD(IStream);
1195 /* IStream32 fields */
1196 struct storage_pps_entry stde;
1199 ULARGE_INTEGER offset;
1202 /*****************************************************************************
1203 * IStream32_QueryInterface [VTABLE]
1205 HRESULT WINAPI IStream_fnQueryInterface(
1206 IStream* iface,REFIID refiid,LPVOID *obj
1208 ICOM_THIS(IStream32Impl,iface);
1210 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1211 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1215 return OLE_E_ENUM_NOMORE;
1219 /******************************************************************************
1220 * IStream32_AddRef [VTABLE]
1222 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1223 ICOM_THIS(IStream32Impl,iface);
1224 return ++(This->ref);
1227 /******************************************************************************
1228 * IStream32_Release [VTABLE]
1230 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1231 ICOM_THIS(IStream32Impl,iface);
1232 FlushFileBuffers(This->hf);
1235 CloseHandle(This->hf);
1236 HeapFree( GetProcessHeap(), 0, This );
1242 /* --- IStorage16 implementation */
1246 /* IUnknown fields */
1247 ICOM_VFIELD(IStorage16);
1249 /* IStorage16 fields */
1250 SEGPTR thisptr; /* pointer to this struct as segmented */
1251 struct storage_pps_entry stde;
1256 /******************************************************************************
1257 * IStorage16_QueryInterface [STORAGE.500]
1259 HRESULT WINAPI IStorage16_fnQueryInterface(
1260 IStorage16* iface,REFIID refiid,LPVOID *obj
1262 ICOM_THIS(IStorage16Impl,iface);
1264 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1266 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1270 return OLE_E_ENUM_NOMORE;
1273 /******************************************************************************
1274 * IStorage16_AddRef [STORAGE.501]
1276 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1277 ICOM_THIS(IStorage16Impl,iface);
1278 return ++(This->ref);
1281 /******************************************************************************
1282 * IStorage16_Release [STORAGE.502]
1284 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1285 ICOM_THIS(IStorage16Impl,iface);
1289 UnMapLS( This->thisptr );
1290 HeapFree( GetProcessHeap(), 0, This );
1294 /******************************************************************************
1295 * IStorage16_Stat [STORAGE.517]
1297 HRESULT WINAPI IStorage16_fnStat(
1298 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1300 ICOM_THIS(IStorage16Impl,iface);
1301 DWORD len = WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, NULL, 0, NULL, NULL );
1302 LPSTR nameA = HeapAlloc( GetProcessHeap(), 0, len );
1304 TRACE("(%p)->(%p,0x%08lx)\n",
1305 This,pstatstg,grfStatFlag
1307 WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, nameA, len, NULL, NULL );
1308 pstatstg->pwcsName=(LPOLESTR16)MapLS( nameA );
1309 pstatstg->type = This->stde.pps_type;
1310 pstatstg->cbSize.s.LowPart = This->stde.pps_size;
1311 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1312 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1313 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1314 pstatstg->grfMode = 0; /* FIXME */
1315 pstatstg->grfLocksSupported = 0; /* FIXME */
1316 pstatstg->clsid = This->stde.pps_guid;
1317 pstatstg->grfStateBits = 0; /* FIXME */
1318 pstatstg->reserved = 0;
1322 /******************************************************************************
1323 * IStorage16_Commit [STORAGE.509]
1325 HRESULT WINAPI IStorage16_fnCommit(
1326 LPSTORAGE16 iface,DWORD commitflags
1328 ICOM_THIS(IStorage16Impl,iface);
1329 FIXME("(%p)->(0x%08lx),STUB!\n",
1335 /******************************************************************************
1336 * IStorage16_CopyTo [STORAGE.507]
1338 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1339 ICOM_THIS(IStorage16Impl,iface);
1340 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1341 This,ciidExclude,debugstr_guid(rgiidExclude),SNB16Exclude,pstgDest
1347 /******************************************************************************
1348 * IStorage16_CreateStorage [STORAGE.505]
1350 HRESULT WINAPI IStorage16_fnCreateStorage(
1351 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1353 ICOM_THIS(IStorage16Impl,iface);
1354 IStorage16Impl* lpstg;
1356 struct storage_pps_entry stde;
1357 struct storage_header sth;
1362 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1363 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1365 if (grfMode & STGM_TRANSACTED)
1366 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1367 _create_istorage16(ppstg);
1368 lpstg = MapSL((SEGPTR)*ppstg);
1369 lpstg->hf = This->hf;
1371 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1375 if (stde.pps_dir==-1) {
1376 stde.pps_dir = ppsent;
1379 FIXME(" use prev chain too ?\n");
1381 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1383 while (stde.pps_next!=-1) {
1385 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1388 stde.pps_next = ppsent;
1390 assert(STORAGE_put_pps_entry(lpstg->hf,x,&stde));
1391 assert(1==STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)));
1392 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstg->stde.pps_rawname,
1393 sizeof(lpstg->stde.pps_rawname)/sizeof(WCHAR));
1394 lpstg->stde.pps_sizeofname = (strlenW(lpstg->stde.pps_rawname)+1)*sizeof(WCHAR);
1395 lpstg->stde.pps_next = -1;
1396 lpstg->stde.pps_prev = -1;
1397 lpstg->stde.pps_dir = -1;
1398 lpstg->stde.pps_sb = -1;
1399 lpstg->stde.pps_size = 0;
1400 lpstg->stde.pps_type = 1;
1401 lpstg->ppsent = ppsent;
1402 /* FIXME: timestamps? */
1403 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1408 /******************************************************************************
1409 * IStorage16_CreateStream [STORAGE.503]
1411 HRESULT WINAPI IStorage16_fnCreateStream(
1412 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1414 ICOM_THIS(IStorage16Impl,iface);
1415 IStream16Impl* lpstr;
1417 struct storage_pps_entry stde;
1419 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1420 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1422 if (grfMode & STGM_TRANSACTED)
1423 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1424 _create_istream16(ppstm);
1425 lpstr = MapSL((SEGPTR)*ppstm);
1426 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1427 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1428 lpstr->offset.s.LowPart = 0;
1429 lpstr->offset.s.HighPart = 0;
1431 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1435 if (stde.pps_next==-1)
1438 while (stde.pps_next!=-1) {
1440 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1443 stde.pps_next = ppsent;
1444 assert(STORAGE_put_pps_entry(lpstr->hf,x,&stde));
1445 assert(1==STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)));
1446 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstr->stde.pps_rawname,
1447 sizeof(lpstr->stde.pps_rawname)/sizeof(WCHAR));
1448 lpstr->stde.pps_sizeofname = (strlenW(lpstr->stde.pps_rawname)+1) * sizeof(WCHAR);
1449 lpstr->stde.pps_next = -1;
1450 lpstr->stde.pps_prev = -1;
1451 lpstr->stde.pps_dir = -1;
1452 lpstr->stde.pps_sb = -1;
1453 lpstr->stde.pps_size = 0;
1454 lpstr->stde.pps_type = 2;
1455 lpstr->ppsent = ppsent;
1456 /* FIXME: timestamps? */
1457 if (!STORAGE_put_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)))
1462 /******************************************************************************
1463 * IStorage16_OpenStorage [STORAGE.506]
1465 HRESULT WINAPI IStorage16_fnOpenStorage(
1466 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, IStorage16 *pstgPrio, DWORD grfMode, SNB16 snbExclude, DWORD reserved, IStorage16 **ppstg
1468 ICOM_THIS(IStorage16Impl,iface);
1469 IStream16Impl* lpstg;
1473 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1474 This,pwcsName,pstgPrio,grfMode,snbExclude,reserved,ppstg
1476 if (grfMode & STGM_TRANSACTED)
1477 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1478 _create_istorage16(ppstg);
1479 lpstg = MapSL((SEGPTR)*ppstg);
1480 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1481 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1482 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1483 newpps = STORAGE_look_for_named_pps(lpstg->hf,This->stde.pps_dir,name);
1485 IStream16_fnRelease((IStream16*)lpstg);
1489 if (1!=STORAGE_get_pps_entry(lpstg->hf,newpps,&(lpstg->stde))) {
1490 IStream16_fnRelease((IStream16*)lpstg);
1493 lpstg->ppsent = newpps;
1497 /******************************************************************************
1498 * IStorage16_OpenStream [STORAGE.504]
1500 HRESULT WINAPI IStorage16_fnOpenStream(
1501 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream16 **ppstm
1503 ICOM_THIS(IStorage16Impl,iface);
1504 IStream16Impl* lpstr;
1508 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1509 This,pwcsName,reserved1,grfMode,reserved2,ppstm
1511 if (grfMode & STGM_TRANSACTED)
1512 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1513 _create_istream16(ppstm);
1514 lpstr = MapSL((SEGPTR)*ppstm);
1515 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1516 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1517 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1518 newpps = STORAGE_look_for_named_pps(lpstr->hf,This->stde.pps_dir,name);
1520 IStream16_fnRelease((IStream16*)lpstr);
1524 if (1!=STORAGE_get_pps_entry(lpstr->hf,newpps,&(lpstr->stde))) {
1525 IStream16_fnRelease((IStream16*)lpstr);
1528 lpstr->offset.s.LowPart = 0;
1529 lpstr->offset.s.HighPart = 0;
1530 lpstr->ppsent = newpps;
1534 /******************************************************************************
1535 * _create_istorage16 [INTERNAL]
1537 static void _create_istorage16(LPSTORAGE16 *stg) {
1538 IStorage16Impl* lpst;
1540 if (!stvt16.QueryInterface) {
1541 HMODULE16 wp = GetModuleHandle16("STORAGE");
1543 #define VTENT(xfn) stvt16.xfn = (void*)GetProcAddress16(wp,"IStorage16_"#xfn);
1544 VTENT(QueryInterface)
1549 VTENT(CreateStorage)
1552 VTENT(MoveElementTo)
1556 VTENT(DestroyElement)
1557 VTENT(RenameElement)
1558 VTENT(SetElementTimes)
1563 segstvt16 = (ICOM_VTABLE(IStorage16)*)MapLS( &stvt16 );
1565 #define VTENT(xfn) stvt16.xfn = IStorage16_fn##xfn;
1566 VTENT(QueryInterface)
1571 VTENT(CreateStorage)
1575 /* not (yet) implemented ...
1576 VTENT(MoveElementTo)
1579 VTENT(DestroyElement)
1580 VTENT(RenameElement)
1581 VTENT(SetElementTimes)
1587 segstvt16 = &stvt16;
1590 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1591 ICOM_VTBL(lpst) = segstvt16;
1593 lpst->thisptr = MapLS(lpst);
1594 *stg = (void*)lpst->thisptr;
1597 /******************************************************************************
1598 * Storage API functions
1601 /******************************************************************************
1602 * StgCreateDocFileA [STORAGE.1]
1604 HRESULT WINAPI StgCreateDocFile16(
1605 LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved,IStorage16 **ppstgOpen
1609 IStorage16Impl* lpstg;
1610 struct storage_pps_entry stde;
1612 TRACE("(%s,0x%08lx,0x%08lx,%p)\n",
1613 pwcsName,grfMode,reserved,ppstgOpen
1615 _create_istorage16(ppstgOpen);
1616 hf = CreateFileA(pwcsName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,0,0);
1617 if (hf==INVALID_HANDLE_VALUE) {
1618 WARN("couldn't open file for storage:%ld\n",GetLastError());
1621 lpstg = MapSL((SEGPTR)*ppstgOpen);
1623 /* FIXME: check for existence before overwriting? */
1624 if (!STORAGE_init_storage(hf)) {
1629 while (!ret) { /* neither 1 nor <0 */
1630 ret=STORAGE_get_pps_entry(hf,i,&stde);
1631 if ((ret==1) && (stde.pps_type==5)) {
1639 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1646 /******************************************************************************
1647 * StgIsStorageFile [STORAGE.5]
1649 HRESULT WINAPI StgIsStorageFile16(LPCOLESTR16 fn) {
1650 UNICODE_STRING strW;
1653 RtlCreateUnicodeStringFromAsciiz(&strW, fn);
1654 ret = StgIsStorageFile( strW.Buffer );
1655 RtlFreeUnicodeString( &strW );
1660 /******************************************************************************
1661 * StgOpenStorage [STORAGE.3]
1663 HRESULT WINAPI StgOpenStorage16(
1664 LPCOLESTR16 pwcsName,IStorage16 *pstgPriority,DWORD grfMode,
1665 SNB16 snbExclude,DWORD reserved, IStorage16 **ppstgOpen
1669 IStorage16Impl* lpstg;
1670 struct storage_pps_entry stde;
1672 TRACE("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1673 pwcsName,pstgPriority,grfMode,snbExclude,reserved,ppstgOpen
1675 _create_istorage16(ppstgOpen);
1676 hf = CreateFileA(pwcsName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
1677 if (hf==INVALID_HANDLE_VALUE) {
1678 WARN("Couldn't open file for storage\n");
1681 lpstg = MapSL((SEGPTR)*ppstgOpen);
1685 while (!ret) { /* neither 1 nor <0 */
1686 ret=STORAGE_get_pps_entry(hf,i,&stde);
1687 if ((ret==1) && (stde.pps_type==5)) {
1694 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1701 /******************************************************************************
1702 * StgIsStorageILockBytes [STORAGE.6]
1704 * Determines if the ILockBytes contains a storage object.
1706 HRESULT WINAPI StgIsStorageILockBytes16(SEGPTR plkbyt)
1712 args[0] = (DWORD)plkbyt; /* iface */
1713 args[1] = args[2] = 0; /* ULARGE_INTEGER offset */
1714 args[3] = (DWORD)K32WOWGlobalAllocLock16( 0, 8, &hsig ); /* sig */
1718 if (!K32WOWCallback16Ex(
1719 (DWORD)((ICOM_VTABLE(ILockBytes16)*)MapSL(
1720 (SEGPTR)ICOM_VTBL(((LPLOCKBYTES16)MapSL(plkbyt))))
1727 ERR("CallTo16 ILockBytes16::ReadAt() failed, hres %lx\n",hres);
1730 if (memcmp(MapSL(args[3]), STORAGE_magic, sizeof(STORAGE_magic)) == 0) {
1731 K32WOWGlobalUnlockFree16(args[3]);
1734 K32WOWGlobalUnlockFree16(args[3]);
1738 /******************************************************************************
1739 * StgOpenStorageOnILockBytes [STORAGE.4]
1741 HRESULT WINAPI StgOpenStorageOnILockBytes16(
1742 ILockBytes16 *plkbyt,
1743 IStorage16 *pstgPriority,
1747 IStorage16 **ppstgOpen)
1749 IStorage16Impl* lpstg;
1751 if ((plkbyt == 0) || (ppstgOpen == 0))
1752 return STG_E_INVALIDPOINTER;
1756 _create_istorage16(ppstgOpen);
1757 lpstg = MapSL((SEGPTR)*ppstgOpen);
1759 /* just teach it to use HANDLE instead of ilockbytes :/ */