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
27 #include <sys/types.h>
32 #include "wine/winbase16.h"
33 #include "wine/unicode.h"
35 #include "wine/obj_base.h"
36 #include "wine/obj_storage.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
40 WINE_DECLARE_DEBUG_CHANNEL(relay);
42 struct storage_header {
43 BYTE magic[8]; /* 00: magic */
44 BYTE unknown1[36]; /* 08: unknown */
45 DWORD num_of_bbd_blocks;/* 2C: length of big datablocks */
46 DWORD root_startblock;/* 30: root storage first big block */
47 DWORD unknown2[2]; /* 34: unknown */
48 DWORD sbd_startblock; /* 3C: small block depot first big block */
49 DWORD unknown3[3]; /* 40: unknown */
50 DWORD bbd_list[109]; /* 4C: big data block list (up to end of sector)*/
52 struct storage_pps_entry {
53 WCHAR pps_rawname[32];/* 00: \0 terminated widechar name */
54 WORD pps_sizeofname; /* 40: namelength in bytes */
55 BYTE pps_type; /* 42: flags, 1 storage/dir, 2 stream, 5 root */
56 BYTE pps_unknown0; /* 43: unknown */
57 DWORD pps_prev; /* 44: previous pps */
58 DWORD pps_next; /* 48: next pps */
59 DWORD pps_dir; /* 4C: directory pps */
60 GUID pps_guid; /* 50: class ID */
61 DWORD pps_unknown1; /* 60: unknown */
62 FILETIME pps_ft1; /* 64: filetime1 */
63 FILETIME pps_ft2; /* 70: filetime2 */
64 DWORD pps_sb; /* 74: data startblock */
65 DWORD pps_size; /* 78: datalength. (<0x1000)?small:big blocks*/
66 DWORD pps_unknown2; /* 7C: unknown */
69 #define STORAGE_CHAINENTRY_FAT 0xfffffffd
70 #define STORAGE_CHAINENTRY_ENDOFCHAIN 0xfffffffe
71 #define STORAGE_CHAINENTRY_FREE 0xffffffff
74 static const BYTE STORAGE_magic[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
75 static const BYTE STORAGE_notmagic[8]={0x0e,0x11,0xfc,0x0d,0xd0,0xcf,0x11,0xe0};
76 static const BYTE STORAGE_oldmagic[8]={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
81 #define SMALLBLOCKS_PER_BIGBLOCK (BIGSIZE/SMALLSIZE)
83 #define READ_HEADER assert(STORAGE_get_big_block(hf,-1,(LPBYTE)&sth));assert(!memcmp(STORAGE_magic,sth.magic,sizeof(STORAGE_magic)));
84 static ICOM_VTABLE(IStorage16) stvt16;
85 static ICOM_VTABLE(IStorage16) *segstvt16 = NULL;
86 static ICOM_VTABLE(IStream16) strvt16;
87 static ICOM_VTABLE(IStream16) *segstrvt16 = NULL;
89 /*ULONG WINAPI IStorage16_AddRef(LPSTORAGE16 this);*/
90 static void _create_istorage16(LPSTORAGE16 *stg);
91 static void _create_istream16(LPSTREAM16 *str);
96 /******************************************************************************
97 * STORAGE_get_big_block [Internal]
99 * Reading OLE compound storage
102 STORAGE_get_big_block(HFILE hf,int n,BYTE *block) {
104 if (-1==_llseek(hf,(n+1)*BIGSIZE,SEEK_SET)) {
105 WARN(" seek failed (%ld)\n",GetLastError());
108 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
109 if (BIGSIZE!=_lread(hf,block,BIGSIZE)) {
110 WARN("(block size %d): read didn't read (%ld)\n",n,GetLastError());
117 /******************************************************************************
118 * STORAGE_put_big_block [INTERNAL]
121 STORAGE_put_big_block(HFILE hf,int n,BYTE *block) {
123 if (-1==_llseek(hf,(n+1)*BIGSIZE,SEEK_SET)) {
124 WARN(" seek failed (%ld)\n",GetLastError());
127 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
128 if (BIGSIZE!=_lwrite(hf,block,BIGSIZE)) {
129 WARN(" write failed (%ld)\n",GetLastError());
135 /******************************************************************************
136 * STORAGE_get_next_big_blocknr [INTERNAL]
139 STORAGE_get_next_big_blocknr(HFILE hf,int blocknr) {
140 INT bbs[BIGSIZE/sizeof(INT)];
141 struct storage_header sth;
145 assert(blocknr>>7<sth.num_of_bbd_blocks);
146 if (sth.bbd_list[blocknr>>7]==0xffffffff)
148 if (!STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs))
150 assert(bbs[blocknr&0x7f]!=STORAGE_CHAINENTRY_FREE);
151 return bbs[blocknr&0x7f];
154 /******************************************************************************
155 * STORAGE_get_nth_next_big_blocknr [INTERNAL]
158 STORAGE_get_nth_next_big_blocknr(HFILE hf,int blocknr,int nr) {
159 INT bbs[BIGSIZE/sizeof(INT)];
161 struct storage_header sth;
167 assert((blocknr>>7)<sth.num_of_bbd_blocks);
168 assert(sth.bbd_list[blocknr>>7]!=0xffffffff);
170 /* simple caching... */
171 if (lastblock!=sth.bbd_list[blocknr>>7]) {
172 assert(STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs));
173 lastblock = sth.bbd_list[blocknr>>7];
175 blocknr = bbs[blocknr&0x7f];
180 /******************************************************************************
181 * STORAGE_get_root_pps_entry [Internal]
184 STORAGE_get_root_pps_entry(HFILE hf,struct storage_pps_entry *pstde) {
187 struct storage_pps_entry *stde=(struct storage_pps_entry*)block;
188 struct storage_header sth;
191 blocknr = sth.root_startblock;
193 assert(STORAGE_get_big_block(hf,blocknr,block));
195 if (!stde[i].pps_sizeofname)
197 if (stde[i].pps_type==5) {
202 blocknr=STORAGE_get_next_big_blocknr(hf,blocknr);
207 /******************************************************************************
208 * STORAGE_get_small_block [INTERNAL]
211 STORAGE_get_small_block(HFILE hf,int blocknr,BYTE *sblock) {
214 struct storage_pps_entry root;
217 assert(STORAGE_get_root_pps_entry(hf,&root));
218 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
219 assert(bigblocknr>=0);
220 assert(STORAGE_get_big_block(hf,bigblocknr,block));
222 memcpy(sblock,((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),SMALLSIZE);
226 /******************************************************************************
227 * STORAGE_put_small_block [INTERNAL]
230 STORAGE_put_small_block(HFILE hf,int blocknr,BYTE *sblock) {
233 struct storage_pps_entry root;
237 assert(STORAGE_get_root_pps_entry(hf,&root));
238 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
239 assert(bigblocknr>=0);
240 assert(STORAGE_get_big_block(hf,bigblocknr,block));
242 memcpy(((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),sblock,SMALLSIZE);
243 assert(STORAGE_put_big_block(hf,bigblocknr,block));
247 /******************************************************************************
248 * STORAGE_get_next_small_blocknr [INTERNAL]
251 STORAGE_get_next_small_blocknr(HFILE hf,int blocknr) {
253 LPINT sbd = (LPINT)block;
255 struct storage_header sth;
259 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
260 assert(bigblocknr>=0);
261 assert(STORAGE_get_big_block(hf,bigblocknr,block));
262 assert(sbd[blocknr & 127]!=STORAGE_CHAINENTRY_FREE);
263 return sbd[blocknr & (128-1)];
266 /******************************************************************************
267 * STORAGE_get_nth_next_small_blocknr [INTERNAL]
270 STORAGE_get_nth_next_small_blocknr(HFILE hf,int blocknr,int nr) {
273 LPINT sbd = (LPINT)block;
274 struct storage_header sth;
279 while ((nr--) && (blocknr>=0)) {
280 if (lastblocknr/128!=blocknr/128) {
282 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
283 assert(bigblocknr>=0);
284 assert(STORAGE_get_big_block(hf,bigblocknr,block));
285 lastblocknr = blocknr;
287 assert(lastblocknr>=0);
289 blocknr=sbd[blocknr & (128-1)];
290 assert(blocknr!=STORAGE_CHAINENTRY_FREE);
295 /******************************************************************************
296 * STORAGE_get_pps_entry [INTERNAL]
299 STORAGE_get_pps_entry(HFILE hf,int n,struct storage_pps_entry *pstde) {
302 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
303 struct storage_header sth;
306 /* we have 4 pps entries per big block */
307 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
309 assert(STORAGE_get_big_block(hf,blocknr,block));
315 /******************************************************************************
316 * STORAGE_put_pps_entry [Internal]
319 STORAGE_put_pps_entry(HFILE hf,int n,struct storage_pps_entry *pstde) {
322 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
323 struct storage_header sth;
327 /* we have 4 pps entries per big block */
328 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
330 assert(STORAGE_get_big_block(hf,blocknr,block));
332 assert(STORAGE_put_big_block(hf,blocknr,block));
336 /******************************************************************************
337 * STORAGE_look_for_named_pps [Internal]
340 STORAGE_look_for_named_pps(HFILE hf,int n,LPOLESTR name) {
341 struct storage_pps_entry stde;
346 if (1!=STORAGE_get_pps_entry(hf,n,&stde))
349 if (!lstrcmpW(name,stde.pps_rawname))
351 if (stde.pps_prev != -1) {
352 ret=STORAGE_look_for_named_pps(hf,stde.pps_prev,name);
356 if (stde.pps_next != -1) {
357 ret=STORAGE_look_for_named_pps(hf,stde.pps_next,name);
364 /******************************************************************************
365 * STORAGE_dump_pps_entry [Internal]
371 STORAGE_dump_pps_entry(struct storage_pps_entry *stde) {
374 WideCharToMultiByte( CP_ACP, 0, stde->pps_rawname, -1, name, sizeof(name), NULL, NULL);
375 if (!stde->pps_sizeofname)
377 DPRINTF("name: %s\n",name);
378 DPRINTF("type: %d\n",stde->pps_type);
379 DPRINTF("prev pps: %ld\n",stde->pps_prev);
380 DPRINTF("next pps: %ld\n",stde->pps_next);
381 DPRINTF("dir pps: %ld\n",stde->pps_dir);
382 DPRINTF("guid: %s\n",debugstr_guid(&(stde->pps_guid)));
383 if (stde->pps_type !=2) {
386 RtlTimeToSecondsSince1970(&(stde->pps_ft1),&dw);
388 DPRINTF("ts1: %s\n",ctime(&t));
389 RtlTimeToSecondsSince1970(&(stde->pps_ft2),&dw);
391 DPRINTF("ts2: %s\n",ctime(&t));
393 DPRINTF("startblock: %ld\n",stde->pps_sb);
394 DPRINTF("size: %ld\n",stde->pps_size);
397 /******************************************************************************
398 * STORAGE_init_storage [INTERNAL]
401 STORAGE_init_storage(HFILE hf) {
404 struct storage_header *sth;
405 struct storage_pps_entry *stde;
407 assert(-1!=_llseek(hf,0,SEEK_SET));
408 /* block -1 is the storage header */
409 sth = (struct storage_header*)block;
410 memcpy(sth->magic,STORAGE_magic,8);
411 memset(sth->unknown1,0,sizeof(sth->unknown1));
412 memset(sth->unknown2,0,sizeof(sth->unknown2));
413 memset(sth->unknown3,0,sizeof(sth->unknown3));
414 sth->num_of_bbd_blocks = 1;
415 sth->root_startblock = 1;
416 sth->sbd_startblock = 0xffffffff;
417 memset(sth->bbd_list,0xff,sizeof(sth->bbd_list));
418 sth->bbd_list[0] = 0;
419 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
420 /* block 0 is the big block directory */
422 memset(block,0xff,sizeof(block)); /* mark all blocks as free */
423 bbs[0]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for this block */
424 bbs[1]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for directory entry */
425 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
426 /* block 1 is the root directory entry */
427 memset(block,0x00,sizeof(block));
428 stde = (struct storage_pps_entry*)block;
429 MultiByteToWideChar( CP_ACP, 0, "RootEntry", -1, stde->pps_rawname,
430 sizeof(stde->pps_rawname)/sizeof(WCHAR));
431 stde->pps_sizeofname = (strlenW(stde->pps_rawname)+1) * sizeof(WCHAR);
436 stde->pps_sb = 0xffffffff;
438 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
442 /******************************************************************************
443 * STORAGE_set_big_chain [Internal]
446 STORAGE_set_big_chain(HFILE hf,int blocknr,INT type) {
448 LPINT bbd = (LPINT)block;
449 int nextblocknr,bigblocknr;
450 struct storage_header sth;
453 assert(blocknr!=type);
455 bigblocknr = sth.bbd_list[blocknr/128];
456 assert(bigblocknr>=0);
457 assert(STORAGE_get_big_block(hf,bigblocknr,block));
459 nextblocknr = bbd[blocknr&(128-1)];
460 bbd[blocknr&(128-1)] = type;
463 assert(STORAGE_put_big_block(hf,bigblocknr,block));
464 type = STORAGE_CHAINENTRY_FREE;
465 blocknr = nextblocknr;
470 /******************************************************************************
471 * STORAGE_set_small_chain [Internal]
474 STORAGE_set_small_chain(HFILE hf,int blocknr,INT type) {
476 LPINT sbd = (LPINT)block;
477 int lastblocknr,nextsmallblocknr,bigblocknr;
478 struct storage_header sth;
482 assert(blocknr!=type);
483 lastblocknr=-129;bigblocknr=-2;
485 /* cache block ... */
486 if (lastblocknr/128!=blocknr/128) {
487 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
488 assert(bigblocknr>=0);
489 assert(STORAGE_get_big_block(hf,bigblocknr,block));
491 lastblocknr = blocknr;
492 nextsmallblocknr = sbd[blocknr&(128-1)];
493 sbd[blocknr&(128-1)] = type;
494 assert(STORAGE_put_big_block(hf,bigblocknr,block));
497 type = STORAGE_CHAINENTRY_FREE;
498 blocknr = nextsmallblocknr;
503 /******************************************************************************
504 * STORAGE_get_free_big_blocknr [Internal]
507 STORAGE_get_free_big_blocknr(HFILE hf) {
509 LPINT sbd = (LPINT)block;
510 int lastbigblocknr,i,curblock,bigblocknr;
511 struct storage_header sth;
516 bigblocknr = sth.bbd_list[curblock];
517 while (curblock<sth.num_of_bbd_blocks) {
518 assert(bigblocknr>=0);
519 assert(STORAGE_get_big_block(hf,bigblocknr,block));
521 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
522 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
523 assert(STORAGE_put_big_block(hf,bigblocknr,block));
524 memset(block,0x42,sizeof(block));
525 assert(STORAGE_put_big_block(hf,i+curblock*128,block));
526 return i+curblock*128;
528 lastbigblocknr = bigblocknr;
529 bigblocknr = sth.bbd_list[++curblock];
531 bigblocknr = curblock*128;
532 /* since we have marked all blocks from 0 up to curblock*128-1
533 * the next free one is curblock*128, where we happily put our
534 * next large block depot.
536 memset(block,0xff,sizeof(block));
537 /* mark the block allocated and returned by this function */
538 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
539 assert(STORAGE_put_big_block(hf,bigblocknr,block));
541 /* if we had a bbd block already (mostlikely) we need
542 * to link the new one into the chain
544 if (lastbigblocknr!=-1)
545 assert(STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr));
546 sth.bbd_list[curblock]=bigblocknr;
547 sth.num_of_bbd_blocks++;
548 assert(sth.num_of_bbd_blocks==curblock+1);
549 assert(STORAGE_put_big_block(hf,-1,(LPBYTE)&sth));
551 /* Set the end of the chain for the bigblockdepots */
552 assert(STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN));
553 /* add 1, for the first entry is used for the additional big block
554 * depot. (means we already used bigblocknr) */
555 memset(block,0x42,sizeof(block));
556 /* allocate this block (filled with 0x42) */
557 assert(STORAGE_put_big_block(hf,bigblocknr+1,block));
562 /******************************************************************************
563 * STORAGE_get_free_small_blocknr [Internal]
566 STORAGE_get_free_small_blocknr(HFILE hf) {
568 LPINT sbd = (LPINT)block;
569 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
570 struct storage_pps_entry root;
571 struct storage_header sth;
574 bigblocknr = sth.sbd_startblock;
578 while (bigblocknr>=0) {
579 if (!STORAGE_get_big_block(hf,bigblocknr,block))
582 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
583 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
584 newblocknr = i+curblock*128;
589 lastbigblocknr = bigblocknr;
590 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
593 if (newblocknr==-1) {
594 bigblocknr = STORAGE_get_free_big_blocknr(hf);
598 memset(block,0xff,sizeof(block));
599 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
600 if (!STORAGE_put_big_block(hf,bigblocknr,block))
602 if (lastbigblocknr==-1) {
603 sth.sbd_startblock = bigblocknr;
604 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
607 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
610 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
612 newblocknr = curblock*128;
614 /* allocate enough big blocks for storing the allocated small block */
615 if (!STORAGE_get_root_pps_entry(hf,&root))
620 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
621 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
622 /* we need to allocate more stuff */
623 bigblocknr = STORAGE_get_free_big_blocknr(hf);
627 if (root.pps_sb==-1) {
628 root.pps_sb = bigblocknr;
629 root.pps_size += BIGSIZE;
631 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
633 root.pps_size += BIGSIZE;
635 lastbigblocknr = bigblocknr;
637 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
639 if (!STORAGE_put_pps_entry(hf,0,&root))
644 /******************************************************************************
645 * STORAGE_get_free_pps_entry [Internal]
648 STORAGE_get_free_pps_entry(HFILE hf) {
649 int blocknr,i,curblock,lastblocknr;
651 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
652 struct storage_header sth;
655 blocknr = sth.root_startblock;
659 if (!STORAGE_get_big_block(hf,blocknr,block))
662 if (stde[i].pps_sizeofname==0) /* free */
664 lastblocknr = blocknr;
665 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
668 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
669 blocknr = STORAGE_get_free_big_blocknr(hf);
670 /* sth invalidated */
674 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
676 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
678 memset(block,0,sizeof(block));
679 STORAGE_put_big_block(hf,blocknr,block);
683 /* --- IStream16 implementation */
687 /* IUnknown fields */
688 ICOM_VFIELD(IStream16);
690 /* IStream16 fields */
691 SEGPTR thisptr; /* pointer to this struct as segmented */
692 struct storage_pps_entry stde;
695 ULARGE_INTEGER offset;
698 /******************************************************************************
699 * IStream16_QueryInterface [STORAGE.518]
701 HRESULT WINAPI IStream16_fnQueryInterface(
702 IStream16* iface,REFIID refiid,LPVOID *obj
704 ICOM_THIS(IStream16Impl,iface);
705 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
706 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
710 return OLE_E_ENUM_NOMORE;
714 /******************************************************************************
715 * IStream16_AddRef [STORAGE.519]
717 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
718 ICOM_THIS(IStream16Impl,iface);
719 return ++(This->ref);
722 /******************************************************************************
723 * IStream16_Release [STORAGE.520]
725 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
726 ICOM_THIS(IStream16Impl,iface);
727 FlushFileBuffers(This->hf);
730 CloseHandle(This->hf);
731 UnMapLS( This->thisptr );
732 HeapFree( GetProcessHeap(), 0, This );
738 /******************************************************************************
739 * IStream16_Seek [STORAGE.523]
742 * Does not handle 64 bits
744 HRESULT WINAPI IStream16_fnSeek(
745 IStream16* iface,LARGE_INTEGER offset,DWORD whence,ULARGE_INTEGER *newpos
747 ICOM_THIS(IStream16Impl,iface);
748 TRACE_(relay)("(%p)->([%ld.%ld],%ld,%p)\n",This,offset.s.HighPart,offset.s.LowPart,whence,newpos);
751 /* unix SEEK_xx should be the same as win95 ones */
753 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
756 assert(offset.s.HighPart==0);
757 This->offset.s.HighPart = offset.s.HighPart;
758 This->offset.s.LowPart = offset.s.LowPart;
761 if (offset.s.HighPart < 0) {
762 /* FIXME: is this negation correct ? */
763 offset.s.HighPart = -offset.s.HighPart;
764 offset.s.LowPart = (0xffffffff ^ offset.s.LowPart)+1;
766 assert(offset.s.HighPart==0);
767 assert(This->offset.s.LowPart >= offset.s.LowPart);
768 This->offset.s.LowPart -= offset.s.LowPart;
770 assert(offset.s.HighPart==0);
771 This->offset.s.LowPart+= offset.s.LowPart;
775 assert(offset.s.HighPart==0);
776 This->offset.s.LowPart = This->stde.pps_size-offset.s.LowPart;
779 if (This->offset.s.LowPart>This->stde.pps_size)
780 This->offset.s.LowPart=This->stde.pps_size;
781 if (newpos) *newpos = This->offset;
785 /******************************************************************************
786 * IStream16_Read [STORAGE.521]
788 HRESULT WINAPI IStream16_fnRead(
789 IStream16* iface,void *pv,ULONG cb,ULONG *pcbRead
791 ICOM_THIS(IStream16Impl,iface);
793 ULONG *bytesread=pcbRead,xxread;
796 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbRead);
797 if (!pcbRead) bytesread=&xxread;
800 if (cb>This->stde.pps_size-This->offset.s.LowPart)
801 cb=This->stde.pps_size-This->offset.s.LowPart;
802 if (This->stde.pps_size < 0x1000) {
803 /* use small block reader */
804 blocknr = STORAGE_get_nth_next_small_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
808 if (!STORAGE_get_small_block(This->hf,blocknr,block)) {
809 WARN("small block read failed!!!\n");
813 if (cc>SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1)))
814 cc=SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
815 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(SMALLSIZE-1)),cc);
816 This->offset.s.LowPart+=cc;
820 blocknr = STORAGE_get_next_small_blocknr(This->hf,blocknr);
823 /* use big block reader */
824 blocknr = STORAGE_get_nth_next_big_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
828 if (!STORAGE_get_big_block(This->hf,blocknr,block)) {
829 WARN("big block read failed!!!\n");
833 if (cc>BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1)))
834 cc=BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
835 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(BIGSIZE-1)),cc);
836 This->offset.s.LowPart+=cc;
840 blocknr=STORAGE_get_next_big_blocknr(This->hf,blocknr);
846 /******************************************************************************
847 * IStream16_Write [STORAGE.522]
849 HRESULT WINAPI IStream16_fnWrite(
850 IStream16* iface,const void *pv,ULONG cb,ULONG *pcbWrite
852 ICOM_THIS(IStream16Impl,iface);
854 ULONG *byteswritten=pcbWrite,xxwritten;
855 int oldsize,newsize,i,curoffset=0,lastblocknr,blocknr,cc;
858 if (!pcbWrite) byteswritten=&xxwritten;
861 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbWrite);
862 /* do we need to junk some blocks? */
863 newsize = This->offset.s.LowPart+cb;
864 oldsize = This->stde.pps_size;
865 if (newsize < oldsize) {
866 if (oldsize < 0x1000) {
867 /* only small blocks */
868 blocknr=STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,newsize/SMALLSIZE);
872 /* will set the rest of the chain to 'free' */
873 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
876 if (newsize >= 0x1000) {
877 blocknr=STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,newsize/BIGSIZE);
880 /* will set the rest of the chain to 'free' */
881 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
884 /* Migrate large blocks to small blocks
885 * (we just migrate newsize bytes)
887 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,newsize+BIGSIZE);
891 blocknr = This->stde.pps_sb;
894 if (!STORAGE_get_big_block(hf,blocknr,curdata)) {
895 HeapFree(GetProcessHeap(),0,data);
900 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
902 /* frees complete chain for this stream */
903 if (!STORAGE_set_big_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
906 blocknr = This->stde.pps_sb = STORAGE_get_free_small_blocknr(hf);
911 if (!STORAGE_put_small_block(hf,blocknr,curdata))
915 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
919 int newblocknr = STORAGE_get_free_small_blocknr(hf);
922 if (!STORAGE_set_small_chain(hf,blocknr,newblocknr))
924 blocknr = newblocknr;
926 curdata += SMALLSIZE;
930 HeapFree(GetProcessHeap(),0,data);
935 This->stde.pps_size = newsize;
938 if (newsize > oldsize) {
939 if (oldsize >= 0x1000) {
940 /* should return the block right before the 'endofchain' */
941 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/BIGSIZE);
943 lastblocknr = blocknr;
944 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
945 blocknr = STORAGE_get_free_big_blocknr(hf);
948 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
950 lastblocknr = blocknr;
952 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
955 if (newsize < 0x1000) {
956 /* find startblock */
958 This->stde.pps_sb = blocknr = STORAGE_get_free_small_blocknr(hf);
960 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/SMALLSIZE);
964 /* allocate required new small blocks */
965 lastblocknr = blocknr;
966 for (i=oldsize/SMALLSIZE;i<newsize/SMALLSIZE;i++) {
967 blocknr = STORAGE_get_free_small_blocknr(hf);
970 if (!STORAGE_set_small_chain(hf,lastblocknr,blocknr))
972 lastblocknr = blocknr;
974 /* and terminate the chain */
975 if (!STORAGE_set_small_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
979 /* no single block allocated yet */
980 blocknr=STORAGE_get_free_big_blocknr(hf);
983 This->stde.pps_sb = blocknr;
985 /* Migrate small blocks to big blocks */
986 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,oldsize+BIGSIZE);
990 blocknr = This->stde.pps_sb;
994 if (!STORAGE_get_small_block(hf,blocknr,curdata))
996 curdata += SMALLSIZE;
998 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1000 /* free small block chain */
1001 if (!STORAGE_set_small_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
1004 blocknr = This->stde.pps_sb = STORAGE_get_free_big_blocknr(hf);
1007 /* put the data into the big blocks */
1008 cc = This->stde.pps_size;
1010 if (!STORAGE_put_big_block(hf,blocknr,curdata))
1014 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1018 int newblocknr = STORAGE_get_free_big_blocknr(hf);
1021 if (!STORAGE_set_big_chain(hf,blocknr,newblocknr))
1023 blocknr = newblocknr;
1029 HeapFree(GetProcessHeap(),0,data);
1033 /* generate big blocks to fit the new data */
1034 lastblocknr = blocknr;
1035 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1036 blocknr = STORAGE_get_free_big_blocknr(hf);
1039 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1041 lastblocknr = blocknr;
1043 /* terminate chain */
1044 if (!STORAGE_set_big_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1048 This->stde.pps_size = newsize;
1051 /* There are just some cases where we didn't modify it, we write it out
1054 if (!STORAGE_put_pps_entry(hf,This->ppsent,&(This->stde)))
1057 /* finally the write pass */
1058 if (This->stde.pps_size < 0x1000) {
1059 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
1062 /* we ensured that it is allocated above */
1064 /* Read old block everytime, since we can have
1065 * overlapping data at START and END of the write
1067 if (!STORAGE_get_small_block(hf,blocknr,block))
1070 cc = SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
1073 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(SMALLSIZE-1)),
1074 (LPBYTE)((char *) pv+curoffset),
1077 if (!STORAGE_put_small_block(hf,blocknr,block))
1082 This->offset.s.LowPart += cc;
1083 *byteswritten += cc;
1084 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1087 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
1090 /* we ensured that it is allocated above, so it better is */
1092 /* read old block everytime, since we can have
1093 * overlapping data at START and END of the write
1095 if (!STORAGE_get_big_block(hf,blocknr,block))
1098 cc = BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
1101 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(BIGSIZE-1)),
1102 (LPBYTE)((char *) pv+curoffset),
1105 if (!STORAGE_put_big_block(hf,blocknr,block))
1110 This->offset.s.LowPart += cc;
1111 *byteswritten += cc;
1112 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1118 /******************************************************************************
1119 * _create_istream16 [Internal]
1121 static void _create_istream16(LPSTREAM16 *str) {
1122 IStream16Impl* lpst;
1124 if (!strvt16.QueryInterface) {
1125 HMODULE16 wp = GetModuleHandle16("STORAGE");
1127 /* FIXME: what is This GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1128 #define VTENT(xfn) strvt16.xfn = (void*)GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.xfn)
1129 VTENT(QueryInterface);
1140 VTENT(UnlockRegion);
1144 segstrvt16 = (ICOM_VTABLE(IStream16)*)MapLS( &strvt16 );
1146 #define VTENT(xfn) strvt16.xfn = IStream16_fn##xfn;
1147 VTENT(QueryInterface);
1159 VTENT(UnlockRegion);
1164 segstrvt16 = &strvt16;
1167 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1168 ICOM_VTBL(lpst) = segstrvt16;
1170 lpst->thisptr = MapLS( lpst );
1171 *str = (void*)lpst->thisptr;
1175 /* --- IStream32 implementation */
1179 /* IUnknown fields */
1180 ICOM_VFIELD(IStream);
1182 /* IStream32 fields */
1183 struct storage_pps_entry stde;
1186 ULARGE_INTEGER offset;
1189 /*****************************************************************************
1190 * IStream32_QueryInterface [VTABLE]
1192 HRESULT WINAPI IStream_fnQueryInterface(
1193 IStream* iface,REFIID refiid,LPVOID *obj
1195 ICOM_THIS(IStream32Impl,iface);
1197 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1198 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1202 return OLE_E_ENUM_NOMORE;
1206 /******************************************************************************
1207 * IStream32_AddRef [VTABLE]
1209 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1210 ICOM_THIS(IStream32Impl,iface);
1211 return ++(This->ref);
1214 /******************************************************************************
1215 * IStream32_Release [VTABLE]
1217 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1218 ICOM_THIS(IStream32Impl,iface);
1219 FlushFileBuffers(This->hf);
1222 CloseHandle(This->hf);
1223 HeapFree( GetProcessHeap(), 0, This );
1229 /* --- IStorage16 implementation */
1233 /* IUnknown fields */
1234 ICOM_VFIELD(IStorage16);
1236 /* IStorage16 fields */
1237 SEGPTR thisptr; /* pointer to this struct as segmented */
1238 struct storage_pps_entry stde;
1243 /******************************************************************************
1244 * IStorage16_QueryInterface [STORAGE.500]
1246 HRESULT WINAPI IStorage16_fnQueryInterface(
1247 IStorage16* iface,REFIID refiid,LPVOID *obj
1249 ICOM_THIS(IStorage16Impl,iface);
1251 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1253 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1257 return OLE_E_ENUM_NOMORE;
1260 /******************************************************************************
1261 * IStorage16_AddRef [STORAGE.501]
1263 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1264 ICOM_THIS(IStorage16Impl,iface);
1265 return ++(This->ref);
1268 /******************************************************************************
1269 * IStorage16_Release [STORAGE.502]
1271 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1272 ICOM_THIS(IStorage16Impl,iface);
1276 UnMapLS( This->thisptr );
1277 HeapFree( GetProcessHeap(), 0, This );
1281 /******************************************************************************
1282 * IStorage16_Stat [STORAGE.517]
1284 HRESULT WINAPI IStorage16_fnStat(
1285 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1287 ICOM_THIS(IStorage16Impl,iface);
1288 DWORD len = WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, NULL, 0, NULL, NULL );
1289 LPSTR nameA = HeapAlloc( GetProcessHeap(), 0, len );
1291 TRACE("(%p)->(%p,0x%08lx)\n",
1292 This,pstatstg,grfStatFlag
1294 WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, nameA, len, NULL, NULL );
1295 pstatstg->pwcsName=(LPOLESTR16)MapLS( nameA );
1296 pstatstg->type = This->stde.pps_type;
1297 pstatstg->cbSize.s.LowPart = This->stde.pps_size;
1298 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1299 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1300 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1301 pstatstg->grfMode = 0; /* FIXME */
1302 pstatstg->grfLocksSupported = 0; /* FIXME */
1303 pstatstg->clsid = This->stde.pps_guid;
1304 pstatstg->grfStateBits = 0; /* FIXME */
1305 pstatstg->reserved = 0;
1309 /******************************************************************************
1310 * IStorage16_Commit [STORAGE.509]
1312 HRESULT WINAPI IStorage16_fnCommit(
1313 LPSTORAGE16 iface,DWORD commitflags
1315 ICOM_THIS(IStorage16Impl,iface);
1316 FIXME("(%p)->(0x%08lx),STUB!\n",
1322 /******************************************************************************
1323 * IStorage16_CopyTo [STORAGE.507]
1325 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1326 ICOM_THIS(IStorage16Impl,iface);
1327 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1328 This,ciidExclude,debugstr_guid(rgiidExclude),SNB16Exclude,pstgDest
1334 /******************************************************************************
1335 * IStorage16_CreateStorage [STORAGE.505]
1337 HRESULT WINAPI IStorage16_fnCreateStorage(
1338 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1340 ICOM_THIS(IStorage16Impl,iface);
1341 IStorage16Impl* lpstg;
1343 struct storage_pps_entry stde;
1344 struct storage_header sth;
1349 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1350 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1352 if (grfMode & STGM_TRANSACTED)
1353 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1354 _create_istorage16(ppstg);
1355 lpstg = MapSL((SEGPTR)*ppstg);
1356 lpstg->hf = This->hf;
1358 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1362 if (stde.pps_dir==-1) {
1363 stde.pps_dir = ppsent;
1366 FIXME(" use prev chain too ?\n");
1368 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1370 while (stde.pps_next!=-1) {
1372 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1375 stde.pps_next = ppsent;
1377 assert(STORAGE_put_pps_entry(lpstg->hf,x,&stde));
1378 assert(1==STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)));
1379 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstg->stde.pps_rawname,
1380 sizeof(lpstg->stde.pps_rawname)/sizeof(WCHAR));
1381 lpstg->stde.pps_sizeofname = (strlenW(lpstg->stde.pps_rawname)+1)*sizeof(WCHAR);
1382 lpstg->stde.pps_next = -1;
1383 lpstg->stde.pps_prev = -1;
1384 lpstg->stde.pps_dir = -1;
1385 lpstg->stde.pps_sb = -1;
1386 lpstg->stde.pps_size = 0;
1387 lpstg->stde.pps_type = 1;
1388 lpstg->ppsent = ppsent;
1389 /* FIXME: timestamps? */
1390 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1395 /******************************************************************************
1396 * IStorage16_CreateStream [STORAGE.503]
1398 HRESULT WINAPI IStorage16_fnCreateStream(
1399 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1401 ICOM_THIS(IStorage16Impl,iface);
1402 IStream16Impl* lpstr;
1404 struct storage_pps_entry stde;
1406 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1407 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1409 if (grfMode & STGM_TRANSACTED)
1410 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1411 _create_istream16(ppstm);
1412 lpstr = MapSL((SEGPTR)*ppstm);
1413 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1414 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1415 lpstr->offset.s.LowPart = 0;
1416 lpstr->offset.s.HighPart = 0;
1418 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1422 if (stde.pps_next==-1)
1425 while (stde.pps_next!=-1) {
1427 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1430 stde.pps_next = ppsent;
1431 assert(STORAGE_put_pps_entry(lpstr->hf,x,&stde));
1432 assert(1==STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)));
1433 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstr->stde.pps_rawname,
1434 sizeof(lpstr->stde.pps_rawname)/sizeof(WCHAR));
1435 lpstr->stde.pps_sizeofname = (strlenW(lpstr->stde.pps_rawname)+1) * sizeof(WCHAR);
1436 lpstr->stde.pps_next = -1;
1437 lpstr->stde.pps_prev = -1;
1438 lpstr->stde.pps_dir = -1;
1439 lpstr->stde.pps_sb = -1;
1440 lpstr->stde.pps_size = 0;
1441 lpstr->stde.pps_type = 2;
1442 lpstr->ppsent = ppsent;
1443 /* FIXME: timestamps? */
1444 if (!STORAGE_put_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)))
1449 /******************************************************************************
1450 * IStorage16_OpenStorage [STORAGE.506]
1452 HRESULT WINAPI IStorage16_fnOpenStorage(
1453 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, IStorage16 *pstgPrio, DWORD grfMode, SNB16 snbExclude, DWORD reserved, IStorage16 **ppstg
1455 ICOM_THIS(IStorage16Impl,iface);
1456 IStream16Impl* lpstg;
1460 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1461 This,pwcsName,pstgPrio,grfMode,snbExclude,reserved,ppstg
1463 if (grfMode & STGM_TRANSACTED)
1464 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1465 _create_istorage16(ppstg);
1466 lpstg = MapSL((SEGPTR)*ppstg);
1467 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1468 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1469 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1470 newpps = STORAGE_look_for_named_pps(lpstg->hf,This->stde.pps_dir,name);
1472 IStream16_fnRelease((IStream16*)lpstg);
1476 if (1!=STORAGE_get_pps_entry(lpstg->hf,newpps,&(lpstg->stde))) {
1477 IStream16_fnRelease((IStream16*)lpstg);
1480 lpstg->ppsent = newpps;
1484 /******************************************************************************
1485 * IStorage16_OpenStream [STORAGE.504]
1487 HRESULT WINAPI IStorage16_fnOpenStream(
1488 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream16 **ppstm
1490 ICOM_THIS(IStorage16Impl,iface);
1491 IStream16Impl* lpstr;
1495 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1496 This,pwcsName,reserved1,grfMode,reserved2,ppstm
1498 if (grfMode & STGM_TRANSACTED)
1499 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1500 _create_istream16(ppstm);
1501 lpstr = MapSL((SEGPTR)*ppstm);
1502 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1503 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1504 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1505 newpps = STORAGE_look_for_named_pps(lpstr->hf,This->stde.pps_dir,name);
1507 IStream16_fnRelease((IStream16*)lpstr);
1511 if (1!=STORAGE_get_pps_entry(lpstr->hf,newpps,&(lpstr->stde))) {
1512 IStream16_fnRelease((IStream16*)lpstr);
1515 lpstr->offset.s.LowPart = 0;
1516 lpstr->offset.s.HighPart = 0;
1517 lpstr->ppsent = newpps;
1521 /******************************************************************************
1522 * _create_istorage16 [INTERNAL]
1524 static void _create_istorage16(LPSTORAGE16 *stg) {
1525 IStorage16Impl* lpst;
1527 if (!stvt16.QueryInterface) {
1528 HMODULE16 wp = GetModuleHandle16("STORAGE");
1530 #define VTENT(xfn) stvt16.xfn = (void*)GetProcAddress16(wp,"IStorage16_"#xfn);
1531 VTENT(QueryInterface)
1536 VTENT(CreateStorage)
1539 VTENT(MoveElementTo)
1543 VTENT(DestroyElement)
1544 VTENT(RenameElement)
1545 VTENT(SetElementTimes)
1550 segstvt16 = (ICOM_VTABLE(IStorage16)*)MapLS( &stvt16 );
1552 #define VTENT(xfn) stvt16.xfn = IStorage16_fn##xfn;
1553 VTENT(QueryInterface)
1558 VTENT(CreateStorage)
1562 /* not (yet) implemented ...
1563 VTENT(MoveElementTo)
1566 VTENT(DestroyElement)
1567 VTENT(RenameElement)
1568 VTENT(SetElementTimes)
1574 segstvt16 = &stvt16;
1577 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1578 ICOM_VTBL(lpst) = segstvt16;
1580 lpst->thisptr = MapLS(lpst);
1581 *stg = (void*)lpst->thisptr;
1584 /******************************************************************************
1585 * Storage API functions
1588 /******************************************************************************
1589 * StgCreateDocFileA [STORAGE.1]
1591 HRESULT WINAPI StgCreateDocFile16(
1592 LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved,IStorage16 **ppstgOpen
1596 IStorage16Impl* lpstg;
1597 struct storage_pps_entry stde;
1599 TRACE("(%s,0x%08lx,0x%08lx,%p)\n",
1600 pwcsName,grfMode,reserved,ppstgOpen
1602 _create_istorage16(ppstgOpen);
1603 hf = CreateFileA(pwcsName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,0,0);
1604 if (hf==INVALID_HANDLE_VALUE) {
1605 WARN("couldn't open file for storage:%ld\n",GetLastError());
1608 lpstg = MapSL((SEGPTR)*ppstgOpen);
1610 /* FIXME: check for existence before overwriting? */
1611 if (!STORAGE_init_storage(hf)) {
1616 while (!ret) { /* neither 1 nor <0 */
1617 ret=STORAGE_get_pps_entry(hf,i,&stde);
1618 if ((ret==1) && (stde.pps_type==5)) {
1626 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1633 /******************************************************************************
1634 * StgIsStorageFile [STORAGE.5]
1636 HRESULT WINAPI StgIsStorageFile16(LPCOLESTR16 fn) {
1641 TRACE("(\'%s\')\n",fn);
1642 hf = OpenFile(fn,&ofs,OF_SHARE_DENY_NONE);
1643 if (hf==HFILE_ERROR)
1644 return STG_E_FILENOTFOUND;
1645 if (24!=_lread(hf,magic,24)) {
1646 WARN(" too short\n");
1650 if (!memcmp(magic,STORAGE_magic,8)) {
1655 if (!memcmp(magic,STORAGE_notmagic,8)) {
1660 if (!memcmp(magic,STORAGE_oldmagic,8)) {
1661 WARN(" -> old format\n");
1663 return STG_E_OLDFORMAT;
1665 WARN(" -> Invalid header.\n");
1667 return STG_E_INVALIDHEADER;
1670 /******************************************************************************
1671 * StgIsStorageFile [OLE32.146]
1674 StgIsStorageFile(LPCOLESTR fn)
1677 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
1678 LPSTR strA = HeapAlloc( GetProcessHeap(), 0, len );
1680 WideCharToMultiByte( CP_ACP, 0, fn, -1, strA, len, NULL, NULL );
1681 ret = StgIsStorageFile16(strA);
1682 HeapFree( GetProcessHeap(), 0, strA );
1687 /******************************************************************************
1688 * StgOpenStorage [STORAGE.3]
1690 HRESULT WINAPI StgOpenStorage16(
1691 LPCOLESTR16 pwcsName,IStorage16 *pstgPriority,DWORD grfMode,
1692 SNB16 snbExclude,DWORD reserved, IStorage16 **ppstgOpen
1696 IStorage16Impl* lpstg;
1697 struct storage_pps_entry stde;
1699 TRACE("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1700 pwcsName,pstgPriority,grfMode,snbExclude,reserved,ppstgOpen
1702 _create_istorage16(ppstgOpen);
1703 hf = CreateFileA(pwcsName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
1704 if (hf==INVALID_HANDLE_VALUE) {
1705 WARN("Couldn't open file for storage\n");
1708 lpstg = MapSL((SEGPTR)*ppstgOpen);
1712 while (!ret) { /* neither 1 nor <0 */
1713 ret=STORAGE_get_pps_entry(hf,i,&stde);
1714 if ((ret==1) && (stde.pps_type==5)) {
1721 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */