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
15 #include "wine/winestring.h"
16 #include "wine/winbase16.h"
19 #include "wine/obj_base.h"
20 #include "wine/obj_storage.h"
24 #include "debugtools.h"
26 DECLARE_DEBUG_CHANNEL(ole)
27 DECLARE_DEBUG_CHANNEL(relay)
29 struct storage_header {
30 BYTE magic[8]; /* 00: magic */
31 BYTE unknown1[36]; /* 08: unknown */
32 DWORD num_of_bbd_blocks;/* 2C: length of big datablocks */
33 DWORD root_startblock;/* 30: root storage first big block */
34 DWORD unknown2[2]; /* 34: unknown */
35 DWORD sbd_startblock; /* 3C: small block depot first big block */
36 DWORD unknown3[3]; /* 40: unknown */
37 DWORD bbd_list[109]; /* 4C: big data block list (up to end of sector)*/
39 struct storage_pps_entry {
40 WCHAR pps_rawname[32];/* 00: \0 terminated widechar name */
41 WORD pps_sizeofname; /* 40: namelength in bytes */
42 BYTE pps_type; /* 42: flags, 1 storage/dir, 2 stream, 5 root */
43 BYTE pps_unknown0; /* 43: unknown */
44 DWORD pps_prev; /* 44: previous pps */
45 DWORD pps_next; /* 48: next pps */
46 DWORD pps_dir; /* 4C: directory pps */
47 GUID pps_guid; /* 50: class ID */
48 DWORD pps_unknown1; /* 60: unknown */
49 FILETIME pps_ft1; /* 64: filetime1 */
50 FILETIME pps_ft2; /* 70: filetime2 */
51 DWORD pps_sb; /* 74: data startblock */
52 DWORD pps_size; /* 78: datalength. (<0x1000)?small:big blocks*/
53 DWORD pps_unknown2; /* 7C: unknown */
56 #define STORAGE_CHAINENTRY_FAT 0xfffffffd
57 #define STORAGE_CHAINENTRY_ENDOFCHAIN 0xfffffffe
58 #define STORAGE_CHAINENTRY_FREE 0xffffffff
61 static const BYTE STORAGE_magic[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
62 static const BYTE STORAGE_notmagic[8]={0x0e,0x11,0xfc,0x0d,0xd0,0xcf,0x11,0xe0};
63 static const BYTE STORAGE_oldmagic[8]={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
68 #define SMALLBLOCKS_PER_BIGBLOCK (BIGSIZE/SMALLSIZE)
70 #define READ_HEADER assert(STORAGE_get_big_block(hf,-1,(LPBYTE)&sth));assert(!memcmp(STORAGE_magic,sth.magic,sizeof(STORAGE_magic)));
71 static ICOM_VTABLE(IStorage16) stvt16;
72 static ICOM_VTABLE(IStorage16) *segstvt16 = NULL;
73 static ICOM_VTABLE(IStream16) strvt16;
74 static ICOM_VTABLE(IStream16) *segstrvt16 = NULL;
76 /*ULONG WINAPI IStorage16_AddRef(LPSTORAGE16 this);*/
77 static void _create_istorage16(LPSTORAGE16 *stg);
78 static void _create_istream16(LPSTREAM16 *str);
83 /******************************************************************************
84 * STORAGE_get_big_block [Internal]
86 * Reading OLE compound storage
89 STORAGE_get_big_block(HFILE hf,int n,BYTE *block) {
91 if (-1==_llseek(hf,(n+1)*BIGSIZE,SEEK_SET)) {
92 WARN_(ole)(" seek failed (%ld)\n",GetLastError());
95 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
96 if (BIGSIZE!=_lread(hf,block,BIGSIZE)) {
97 WARN_(ole)("(block size %d): read didn't read (%ld)\n",n,GetLastError());
104 /******************************************************************************
105 * STORAGE_put_big_block [INTERNAL]
108 STORAGE_put_big_block(HFILE hf,int n,BYTE *block) {
110 if (-1==_llseek(hf,(n+1)*BIGSIZE,SEEK_SET)) {
111 WARN_(ole)(" seek failed (%ld)\n",GetLastError());
114 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
115 if (BIGSIZE!=_lwrite(hf,block,BIGSIZE)) {
116 WARN_(ole)(" write failed (%ld)\n",GetLastError());
122 /******************************************************************************
123 * STORAGE_get_next_big_blocknr [INTERNAL]
126 STORAGE_get_next_big_blocknr(HFILE hf,int blocknr) {
127 INT bbs[BIGSIZE/sizeof(INT)];
128 struct storage_header sth;
132 assert(blocknr>>7<sth.num_of_bbd_blocks);
133 if (sth.bbd_list[blocknr>>7]==0xffffffff)
135 if (!STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs))
137 assert(bbs[blocknr&0x7f]!=STORAGE_CHAINENTRY_FREE);
138 return bbs[blocknr&0x7f];
141 /******************************************************************************
142 * STORAGE_get_nth_next_big_blocknr [INTERNAL]
145 STORAGE_get_nth_next_big_blocknr(HFILE hf,int blocknr,int nr) {
146 INT bbs[BIGSIZE/sizeof(INT)];
148 struct storage_header sth;
154 assert((blocknr>>7)<sth.num_of_bbd_blocks);
155 assert(sth.bbd_list[blocknr>>7]!=0xffffffff);
157 /* simple caching... */
158 if (lastblock!=sth.bbd_list[blocknr>>7]) {
159 assert(STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs));
160 lastblock = sth.bbd_list[blocknr>>7];
162 blocknr = bbs[blocknr&0x7f];
167 /******************************************************************************
168 * STORAGE_get_root_pps_entry [Internal]
171 STORAGE_get_root_pps_entry(HFILE hf,struct storage_pps_entry *pstde) {
174 struct storage_pps_entry *stde=(struct storage_pps_entry*)block;
175 struct storage_header sth;
178 blocknr = sth.root_startblock;
180 assert(STORAGE_get_big_block(hf,blocknr,block));
182 if (!stde[i].pps_sizeofname)
184 if (stde[i].pps_type==5) {
189 blocknr=STORAGE_get_next_big_blocknr(hf,blocknr);
194 /******************************************************************************
195 * STORAGE_get_small_block [INTERNAL]
198 STORAGE_get_small_block(HFILE hf,int blocknr,BYTE *sblock) {
201 struct storage_pps_entry root;
204 assert(STORAGE_get_root_pps_entry(hf,&root));
205 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
206 assert(bigblocknr>=0);
207 assert(STORAGE_get_big_block(hf,bigblocknr,block));
209 memcpy(sblock,((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),SMALLSIZE);
213 /******************************************************************************
214 * STORAGE_put_small_block [INTERNAL]
217 STORAGE_put_small_block(HFILE hf,int blocknr,BYTE *sblock) {
220 struct storage_pps_entry root;
224 assert(STORAGE_get_root_pps_entry(hf,&root));
225 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
226 assert(bigblocknr>=0);
227 assert(STORAGE_get_big_block(hf,bigblocknr,block));
229 memcpy(((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),sblock,SMALLSIZE);
230 assert(STORAGE_put_big_block(hf,bigblocknr,block));
234 /******************************************************************************
235 * STORAGE_get_next_small_blocknr [INTERNAL]
238 STORAGE_get_next_small_blocknr(HFILE hf,int blocknr) {
240 LPINT sbd = (LPINT)block;
242 struct storage_header sth;
246 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
247 assert(bigblocknr>=0);
248 assert(STORAGE_get_big_block(hf,bigblocknr,block));
249 assert(sbd[blocknr & 127]!=STORAGE_CHAINENTRY_FREE);
250 return sbd[blocknr & (128-1)];
253 /******************************************************************************
254 * STORAGE_get_nth_next_small_blocknr [INTERNAL]
257 STORAGE_get_nth_next_small_blocknr(HFILE hf,int blocknr,int nr) {
260 LPINT sbd = (LPINT)block;
261 struct storage_header sth;
266 while ((nr--) && (blocknr>=0)) {
267 if (lastblocknr/128!=blocknr/128) {
269 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
270 assert(bigblocknr>=0);
271 assert(STORAGE_get_big_block(hf,bigblocknr,block));
272 lastblocknr = blocknr;
274 assert(lastblocknr>=0);
276 blocknr=sbd[blocknr & (128-1)];
277 assert(blocknr!=STORAGE_CHAINENTRY_FREE);
282 /******************************************************************************
283 * STORAGE_get_pps_entry [INTERNAL]
286 STORAGE_get_pps_entry(HFILE hf,int n,struct storage_pps_entry *pstde) {
289 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
290 struct storage_header sth;
293 /* we have 4 pps entries per big block */
294 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
296 assert(STORAGE_get_big_block(hf,blocknr,block));
302 /******************************************************************************
303 * STORAGE_put_pps_entry [Internal]
306 STORAGE_put_pps_entry(HFILE hf,int n,struct storage_pps_entry *pstde) {
309 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
310 struct storage_header sth;
314 /* we have 4 pps entries per big block */
315 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
317 assert(STORAGE_get_big_block(hf,blocknr,block));
319 assert(STORAGE_put_big_block(hf,blocknr,block));
323 /******************************************************************************
324 * STORAGE_look_for_named_pps [Internal]
327 STORAGE_look_for_named_pps(HFILE hf,int n,LPOLESTR name) {
328 struct storage_pps_entry stde;
333 if (1!=STORAGE_get_pps_entry(hf,n,&stde))
336 if (!lstrcmpW(name,stde.pps_rawname))
338 if (stde.pps_prev != -1) {
339 ret=STORAGE_look_for_named_pps(hf,stde.pps_prev,name);
343 if (stde.pps_next != -1) {
344 ret=STORAGE_look_for_named_pps(hf,stde.pps_next,name);
351 /******************************************************************************
352 * STORAGE_dump_pps_entry [Internal]
358 STORAGE_dump_pps_entry(struct storage_pps_entry *stde) {
359 char name[33],xguid[50];
361 WINE_StringFromCLSID(&(stde->pps_guid),xguid);
363 lstrcpyWtoA(name,stde->pps_rawname);
364 if (!stde->pps_sizeofname)
366 DPRINTF("name: %s\n",name);
367 DPRINTF("type: %d\n",stde->pps_type);
368 DPRINTF("prev pps: %ld\n",stde->pps_prev);
369 DPRINTF("next pps: %ld\n",stde->pps_next);
370 DPRINTF("dir pps: %ld\n",stde->pps_dir);
371 DPRINTF("guid: %s\n",xguid);
372 if (stde->pps_type !=2) {
375 t = DOSFS_FileTimeToUnixTime(&(stde->pps_ft1),NULL);
376 DPRINTF("ts1: %s\n",ctime(&t));
377 t = DOSFS_FileTimeToUnixTime(&(stde->pps_ft2),NULL);
378 DPRINTF("ts2: %s\n",ctime(&t));
380 DPRINTF("startblock: %ld\n",stde->pps_sb);
381 DPRINTF("size: %ld\n",stde->pps_size);
384 /******************************************************************************
385 * STORAGE_init_storage [INTERNAL]
388 STORAGE_init_storage(HFILE hf) {
391 struct storage_header *sth;
392 struct storage_pps_entry *stde;
394 assert(-1!=_llseek(hf,0,SEEK_SET));
395 /* block -1 is the storage header */
396 sth = (struct storage_header*)block;
397 memcpy(sth->magic,STORAGE_magic,8);
398 memset(sth->unknown1,0,sizeof(sth->unknown1));
399 memset(sth->unknown2,0,sizeof(sth->unknown2));
400 memset(sth->unknown3,0,sizeof(sth->unknown3));
401 sth->num_of_bbd_blocks = 1;
402 sth->root_startblock = 1;
403 sth->sbd_startblock = 0xffffffff;
404 memset(sth->bbd_list,0xff,sizeof(sth->bbd_list));
405 sth->bbd_list[0] = 0;
406 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
407 /* block 0 is the big block directory */
409 memset(block,0xff,sizeof(block)); /* mark all blocks as free */
410 bbs[0]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for this block */
411 bbs[1]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for directory entry */
412 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
413 /* block 1 is the root directory entry */
414 memset(block,0x00,sizeof(block));
415 stde = (struct storage_pps_entry*)block;
416 lstrcpyAtoW(stde->pps_rawname,"RootEntry");
417 stde->pps_sizeofname = lstrlenW(stde->pps_rawname)*2+2;
422 stde->pps_sb = 0xffffffff;
424 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
428 /******************************************************************************
429 * STORAGE_set_big_chain [Internal]
432 STORAGE_set_big_chain(HFILE hf,int blocknr,INT type) {
434 LPINT bbd = (LPINT)block;
435 int nextblocknr,bigblocknr;
436 struct storage_header sth;
439 assert(blocknr!=type);
441 bigblocknr = sth.bbd_list[blocknr/128];
442 assert(bigblocknr>=0);
443 assert(STORAGE_get_big_block(hf,bigblocknr,block));
445 nextblocknr = bbd[blocknr&(128-1)];
446 bbd[blocknr&(128-1)] = type;
449 assert(STORAGE_put_big_block(hf,bigblocknr,block));
450 type = STORAGE_CHAINENTRY_FREE;
451 blocknr = nextblocknr;
456 /******************************************************************************
457 * STORAGE_set_small_chain [Internal]
460 STORAGE_set_small_chain(HFILE hf,int blocknr,INT type) {
462 LPINT sbd = (LPINT)block;
463 int lastblocknr,nextsmallblocknr,bigblocknr;
464 struct storage_header sth;
468 assert(blocknr!=type);
469 lastblocknr=-129;bigblocknr=-2;
471 /* cache block ... */
472 if (lastblocknr/128!=blocknr/128) {
473 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
474 assert(bigblocknr>=0);
475 assert(STORAGE_get_big_block(hf,bigblocknr,block));
477 lastblocknr = blocknr;
478 nextsmallblocknr = sbd[blocknr&(128-1)];
479 sbd[blocknr&(128-1)] = type;
480 assert(STORAGE_put_big_block(hf,bigblocknr,block));
483 type = STORAGE_CHAINENTRY_FREE;
484 blocknr = nextsmallblocknr;
489 /******************************************************************************
490 * STORAGE_get_free_big_blocknr [Internal]
493 STORAGE_get_free_big_blocknr(HFILE hf) {
495 LPINT sbd = (LPINT)block;
496 int lastbigblocknr,i,curblock,bigblocknr;
497 struct storage_header sth;
502 bigblocknr = sth.bbd_list[curblock];
503 while (curblock<sth.num_of_bbd_blocks) {
504 assert(bigblocknr>=0);
505 assert(STORAGE_get_big_block(hf,bigblocknr,block));
507 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
508 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
509 assert(STORAGE_put_big_block(hf,bigblocknr,block));
510 memset(block,0x42,sizeof(block));
511 assert(STORAGE_put_big_block(hf,i+curblock*128,block));
512 return i+curblock*128;
514 lastbigblocknr = bigblocknr;
515 bigblocknr = sth.bbd_list[++curblock];
517 bigblocknr = curblock*128;
518 /* since we have marked all blocks from 0 up to curblock*128-1
519 * the next free one is curblock*128, where we happily put our
520 * next large block depot.
522 memset(block,0xff,sizeof(block));
523 /* mark the block allocated and returned by this function */
524 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
525 assert(STORAGE_put_big_block(hf,bigblocknr,block));
527 /* if we had a bbd block already (mostlikely) we need
528 * to link the new one into the chain
530 if (lastbigblocknr!=-1)
531 assert(STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr));
532 sth.bbd_list[curblock]=bigblocknr;
533 sth.num_of_bbd_blocks++;
534 assert(sth.num_of_bbd_blocks==curblock+1);
535 assert(STORAGE_put_big_block(hf,-1,(LPBYTE)&sth));
537 /* Set the end of the chain for the bigblockdepots */
538 assert(STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN));
539 /* add 1, for the first entry is used for the additional big block
540 * depot. (means we already used bigblocknr) */
541 memset(block,0x42,sizeof(block));
542 /* allocate this block (filled with 0x42) */
543 assert(STORAGE_put_big_block(hf,bigblocknr+1,block));
548 /******************************************************************************
549 * STORAGE_get_free_small_blocknr [Internal]
552 STORAGE_get_free_small_blocknr(HFILE hf) {
554 LPINT sbd = (LPINT)block;
555 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
556 struct storage_pps_entry root;
557 struct storage_header sth;
560 bigblocknr = sth.sbd_startblock;
564 while (bigblocknr>=0) {
565 if (!STORAGE_get_big_block(hf,bigblocknr,block))
568 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
569 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
570 newblocknr = i+curblock*128;
575 lastbigblocknr = bigblocknr;
576 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
579 if (newblocknr==-1) {
580 bigblocknr = STORAGE_get_free_big_blocknr(hf);
584 memset(block,0xff,sizeof(block));
585 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
586 if (!STORAGE_put_big_block(hf,bigblocknr,block))
588 if (lastbigblocknr==-1) {
589 sth.sbd_startblock = bigblocknr;
590 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
593 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
596 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
598 newblocknr = curblock*128;
600 /* allocate enough big blocks for storing the allocated small block */
601 if (!STORAGE_get_root_pps_entry(hf,&root))
606 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
607 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
608 /* we need to allocate more stuff */
609 bigblocknr = STORAGE_get_free_big_blocknr(hf);
613 if (root.pps_sb==-1) {
614 root.pps_sb = bigblocknr;
615 root.pps_size += BIGSIZE;
617 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
619 root.pps_size += BIGSIZE;
621 lastbigblocknr = bigblocknr;
623 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
625 if (!STORAGE_put_pps_entry(hf,0,&root))
630 /******************************************************************************
631 * STORAGE_get_free_pps_entry [Internal]
634 STORAGE_get_free_pps_entry(HFILE hf) {
635 int blocknr,i,curblock,lastblocknr;
637 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
638 struct storage_header sth;
641 blocknr = sth.root_startblock;
645 if (!STORAGE_get_big_block(hf,blocknr,block))
648 if (stde[i].pps_sizeofname==0) /* free */
650 lastblocknr = blocknr;
651 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
654 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
655 blocknr = STORAGE_get_free_big_blocknr(hf);
656 /* sth invalidated */
660 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
662 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
664 memset(block,0,sizeof(block));
665 STORAGE_put_big_block(hf,blocknr,block);
669 /* --- IStream16 implementation */
673 /* IUnknown fields */
674 ICOM_VTABLE(IStream16)* lpvtbl;
676 /* IStream16 fields */
677 SEGPTR thisptr; /* pointer to this struct as segmented */
678 struct storage_pps_entry stde;
681 ULARGE_INTEGER offset;
684 /******************************************************************************
685 * IStream16_QueryInterface [STORAGE.518]
687 HRESULT WINAPI IStream16_fnQueryInterface(
688 IStream16* iface,REFIID refiid,LPVOID *obj
690 ICOM_THIS(IStream16Impl,iface);
692 WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
693 TRACE_(relay)("(%p)->(%s,%p)\n",This,xrefiid,obj);
694 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
698 return OLE_E_ENUM_NOMORE;
702 /******************************************************************************
703 * IStream16_AddRef [STORAGE.519]
705 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
706 ICOM_THIS(IStream16Impl,iface);
707 return ++(This->ref);
710 /******************************************************************************
711 * IStream16_Release [STORAGE.520]
713 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
714 ICOM_THIS(IStream16Impl,iface);
715 FlushFileBuffers(This->hf);
718 CloseHandle(This->hf);
725 /******************************************************************************
726 * IStream16_Seek [STORAGE.523]
729 * Does not handle 64 bits
731 HRESULT WINAPI IStream16_fnSeek(
732 IStream16* iface,LARGE_INTEGER offset,DWORD whence,ULARGE_INTEGER *newpos
734 ICOM_THIS(IStream16Impl,iface);
735 TRACE_(relay)("(%p)->([%ld.%ld],%ld,%p)\n",This,offset.HighPart,offset.LowPart,whence,newpos);
738 /* unix SEEK_xx should be the same as win95 ones */
740 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
743 assert(offset.HighPart==0);
744 This->offset.HighPart = offset.HighPart;
745 This->offset.LowPart = offset.LowPart;
748 if (offset.HighPart < 0) {
749 /* FIXME: is this negation correct ? */
750 offset.HighPart = -offset.HighPart;
751 offset.LowPart = (0xffffffff ^ offset.LowPart)+1;
753 assert(offset.HighPart==0);
754 assert(This->offset.LowPart >= offset.LowPart);
755 This->offset.LowPart -= offset.LowPart;
757 assert(offset.HighPart==0);
758 This->offset.LowPart+= offset.LowPart;
762 assert(offset.HighPart==0);
763 This->offset.LowPart = This->stde.pps_size-offset.LowPart;
766 if (This->offset.LowPart>This->stde.pps_size)
767 This->offset.LowPart=This->stde.pps_size;
768 if (newpos) *newpos = This->offset;
772 /******************************************************************************
773 * IStream16_Read [STORAGE.521]
775 HRESULT WINAPI IStream16_fnRead(
776 IStream16* iface,void *pv,ULONG cb,ULONG *pcbRead
778 ICOM_THIS(IStream16Impl,iface);
780 ULONG *bytesread=pcbRead,xxread;
783 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbRead);
784 if (!pcbRead) bytesread=&xxread;
787 if (cb>This->stde.pps_size-This->offset.LowPart)
788 cb=This->stde.pps_size-This->offset.LowPart;
789 if (This->stde.pps_size < 0x1000) {
790 /* use small block reader */
791 blocknr = STORAGE_get_nth_next_small_blocknr(This->hf,This->stde.pps_sb,This->offset.LowPart/SMALLSIZE);
795 if (!STORAGE_get_small_block(This->hf,blocknr,block)) {
796 WARN_(ole)("small block read failed!!!\n");
800 if (cc>SMALLSIZE-(This->offset.LowPart&(SMALLSIZE-1)))
801 cc=SMALLSIZE-(This->offset.LowPart&(SMALLSIZE-1));
802 memcpy((LPBYTE)pv,block+(This->offset.LowPart&(SMALLSIZE-1)),cc);
803 This->offset.LowPart+=cc;
807 blocknr = STORAGE_get_next_small_blocknr(This->hf,blocknr);
810 /* use big block reader */
811 blocknr = STORAGE_get_nth_next_big_blocknr(This->hf,This->stde.pps_sb,This->offset.LowPart/BIGSIZE);
815 if (!STORAGE_get_big_block(This->hf,blocknr,block)) {
816 WARN_(ole)("big block read failed!!!\n");
820 if (cc>BIGSIZE-(This->offset.LowPart&(BIGSIZE-1)))
821 cc=BIGSIZE-(This->offset.LowPart&(BIGSIZE-1));
822 memcpy((LPBYTE)pv,block+(This->offset.LowPart&(BIGSIZE-1)),cc);
823 This->offset.LowPart+=cc;
827 blocknr=STORAGE_get_next_big_blocknr(This->hf,blocknr);
833 /******************************************************************************
834 * IStream16_Write [STORAGE.522]
836 HRESULT WINAPI IStream16_fnWrite(
837 IStream16* iface,const void *pv,ULONG cb,ULONG *pcbWrite
839 ICOM_THIS(IStream16Impl,iface);
841 ULONG *byteswritten=pcbWrite,xxwritten;
842 int oldsize,newsize,i,curoffset=0,lastblocknr,blocknr,cc;
845 if (!pcbWrite) byteswritten=&xxwritten;
848 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbWrite);
849 /* do we need to junk some blocks? */
850 newsize = This->offset.LowPart+cb;
851 oldsize = This->stde.pps_size;
852 if (newsize < oldsize) {
853 if (oldsize < 0x1000) {
854 /* only small blocks */
855 blocknr=STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,newsize/SMALLSIZE);
859 /* will set the rest of the chain to 'free' */
860 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
863 if (newsize >= 0x1000) {
864 blocknr=STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,newsize/BIGSIZE);
867 /* will set the rest of the chain to 'free' */
868 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
871 /* Migrate large blocks to small blocks
872 * (we just migrate newsize bytes)
874 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,newsize+BIGSIZE);
876 blocknr = This->stde.pps_sb;
879 if (!STORAGE_get_big_block(hf,blocknr,curdata)) {
880 HeapFree(GetProcessHeap(),0,data);
885 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
887 /* frees complete chain for this stream */
888 if (!STORAGE_set_big_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
891 blocknr = This->stde.pps_sb = STORAGE_get_free_small_blocknr(hf);
896 if (!STORAGE_put_small_block(hf,blocknr,curdata))
900 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
904 int newblocknr = STORAGE_get_free_small_blocknr(hf);
907 if (!STORAGE_set_small_chain(hf,blocknr,newblocknr))
909 blocknr = newblocknr;
911 curdata += SMALLSIZE;
913 HeapFree(GetProcessHeap(),0,data);
916 This->stde.pps_size = newsize;
919 if (newsize > oldsize) {
920 if (oldsize >= 0x1000) {
921 /* should return the block right before the 'endofchain' */
922 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/BIGSIZE);
924 lastblocknr = blocknr;
925 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
926 blocknr = STORAGE_get_free_big_blocknr(hf);
929 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
931 lastblocknr = blocknr;
933 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
936 if (newsize < 0x1000) {
937 /* find startblock */
939 This->stde.pps_sb = blocknr = STORAGE_get_free_small_blocknr(hf);
941 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/SMALLSIZE);
945 /* allocate required new small blocks */
946 lastblocknr = blocknr;
947 for (i=oldsize/SMALLSIZE;i<newsize/SMALLSIZE;i++) {
948 blocknr = STORAGE_get_free_small_blocknr(hf);
951 if (!STORAGE_set_small_chain(hf,lastblocknr,blocknr))
953 lastblocknr = blocknr;
955 /* and terminate the chain */
956 if (!STORAGE_set_small_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
960 /* no single block allocated yet */
961 blocknr=STORAGE_get_free_big_blocknr(hf);
964 This->stde.pps_sb = blocknr;
966 /* Migrate small blocks to big blocks */
967 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,oldsize+BIGSIZE);
969 blocknr = This->stde.pps_sb;
973 if (!STORAGE_get_small_block(hf,blocknr,curdata)) {
974 HeapFree(GetProcessHeap(),0,data);
977 curdata += SMALLSIZE;
979 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
981 /* free small block chain */
982 if (!STORAGE_set_small_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
985 blocknr = This->stde.pps_sb = STORAGE_get_free_big_blocknr(hf);
988 /* put the data into the big blocks */
989 cc = This->stde.pps_size;
991 if (!STORAGE_put_big_block(hf,blocknr,curdata))
995 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
999 int newblocknr = STORAGE_get_free_big_blocknr(hf);
1002 if (!STORAGE_set_big_chain(hf,blocknr,newblocknr))
1004 blocknr = newblocknr;
1008 HeapFree(GetProcessHeap(),0,data);
1010 /* generate big blocks to fit the new data */
1011 lastblocknr = blocknr;
1012 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1013 blocknr = STORAGE_get_free_big_blocknr(hf);
1016 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1018 lastblocknr = blocknr;
1020 /* terminate chain */
1021 if (!STORAGE_set_big_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1025 This->stde.pps_size = newsize;
1028 /* There are just some cases where we didn't modify it, we write it out
1031 if (!STORAGE_put_pps_entry(hf,This->ppsent,&(This->stde)))
1034 /* finally the write pass */
1035 if (This->stde.pps_size < 0x1000) {
1036 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->offset.LowPart/SMALLSIZE);
1039 /* we ensured that it is allocated above */
1041 /* Read old block everytime, since we can have
1042 * overlapping data at START and END of the write
1044 if (!STORAGE_get_small_block(hf,blocknr,block))
1047 cc = SMALLSIZE-(This->offset.LowPart&(SMALLSIZE-1));
1050 memcpy( ((LPBYTE)block)+(This->offset.LowPart&(SMALLSIZE-1)),
1051 (LPBYTE)((char *) pv+curoffset),
1054 if (!STORAGE_put_small_block(hf,blocknr,block))
1059 This->offset.LowPart += cc;
1060 *byteswritten += cc;
1061 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1064 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->offset.LowPart/BIGSIZE);
1067 /* we ensured that it is allocated above, so it better is */
1069 /* read old block everytime, since we can have
1070 * overlapping data at START and END of the write
1072 if (!STORAGE_get_big_block(hf,blocknr,block))
1075 cc = BIGSIZE-(This->offset.LowPart&(BIGSIZE-1));
1078 memcpy( ((LPBYTE)block)+(This->offset.LowPart&(BIGSIZE-1)),
1079 (LPBYTE)((char *) pv+curoffset),
1082 if (!STORAGE_put_big_block(hf,blocknr,block))
1087 This->offset.LowPart += cc;
1088 *byteswritten += cc;
1089 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1095 /******************************************************************************
1096 * _create_istream16 [Internal]
1098 static void _create_istream16(LPSTREAM16 *str) {
1099 IStream16Impl* lpst;
1101 if (!strvt16.fnQueryInterface) {
1102 HMODULE16 wp = GetModuleHandle16("STORAGE");
1104 /* FIXME: what is This WIN32_GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1105 #define VTENT(xfn) strvt16.fn##xfn = (void*)WIN32_GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.fn##xfn)
1106 VTENT(QueryInterface);
1117 VTENT(UnlockRegion);
1121 segstrvt16 = SEGPTR_NEW(ICOM_VTABLE(IStream16));
1122 memcpy(segstrvt16,&strvt16,sizeof(strvt16));
1123 segstrvt16 = (ICOM_VTABLE(IStream16)*)SEGPTR_GET(segstrvt16);
1125 #define VTENT(xfn) strvt16.fn##xfn = IStream16_fn##xfn;
1126 VTENT(QueryInterface);
1138 VTENT(UnlockRegion);
1143 segstrvt16 = &strvt16;
1146 lpst = SEGPTR_NEW(IStream16Impl);
1147 lpst->lpvtbl = segstrvt16;
1149 lpst->thisptr = SEGPTR_GET(lpst);
1150 *str = (void*)lpst->thisptr;
1154 /* --- IStream32 implementation */
1158 /* IUnknown fields */
1159 ICOM_VTABLE(IStream)* lpvtbl;
1161 /* IStream32 fields */
1162 struct storage_pps_entry stde;
1165 ULARGE_INTEGER offset;
1168 /*****************************************************************************
1169 * IStream32_QueryInterface [VTABLE]
1171 HRESULT WINAPI IStream_fnQueryInterface(
1172 IStream* iface,REFIID refiid,LPVOID *obj
1174 ICOM_THIS(IStream32Impl,iface);
1177 WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
1178 TRACE_(relay)("(%p)->(%s,%p)\n",This,xrefiid,obj);
1179 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1183 return OLE_E_ENUM_NOMORE;
1187 /******************************************************************************
1188 * IStream32_AddRef [VTABLE]
1190 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1191 ICOM_THIS(IStream32Impl,iface);
1192 return ++(This->ref);
1195 /******************************************************************************
1196 * IStream32_Release [VTABLE]
1198 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1199 ICOM_THIS(IStream32Impl,iface);
1200 FlushFileBuffers(This->hf);
1203 CloseHandle(This->hf);
1210 /* --- IStorage16 implementation */
1214 /* IUnknown fields */
1215 ICOM_VTABLE(IStorage16)* lpvtbl;
1217 /* IStorage16 fields */
1218 SEGPTR thisptr; /* pointer to this struct as segmented */
1219 struct storage_pps_entry stde;
1224 /******************************************************************************
1225 * IStorage16_QueryInterface [STORAGE.500]
1227 HRESULT WINAPI IStorage16_fnQueryInterface(
1228 IStorage16* iface,REFIID refiid,LPVOID *obj
1230 ICOM_THIS(IStorage16Impl,iface);
1233 WINE_StringFromCLSID((LPCLSID)refiid,xrefiid);
1234 TRACE_(relay)("(%p)->(%s,%p)\n",This,xrefiid,obj);
1236 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1240 return OLE_E_ENUM_NOMORE;
1243 /******************************************************************************
1244 * IStorage16_AddRef [STORAGE.501]
1246 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1247 ICOM_THIS(IStorage16Impl,iface);
1248 return ++(This->ref);
1251 /******************************************************************************
1252 * IStorage16_Release [STORAGE.502]
1254 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1255 ICOM_THIS(IStorage16Impl,iface);
1263 /******************************************************************************
1264 * IStorage16_Stat [STORAGE.517]
1266 HRESULT WINAPI IStorage16_fnStat(
1267 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1269 ICOM_THIS(IStorage16Impl,iface);
1270 TRACE_(ole)("(%p)->(%p,0x%08lx)\n",
1271 This,pstatstg,grfStatFlag
1273 pstatstg->pwcsName=(LPOLESTR16)SEGPTR_GET(SEGPTR_STRDUP_WtoA(This->stde.pps_rawname));
1274 pstatstg->type = This->stde.pps_type;
1275 pstatstg->cbSize.LowPart = This->stde.pps_size;
1276 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1277 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1278 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1279 pstatstg->grfMode = 0; /* FIXME */
1280 pstatstg->grfLocksSupported = 0; /* FIXME */
1281 pstatstg->clsid = This->stde.pps_guid;
1282 pstatstg->grfStateBits = 0; /* FIXME */
1283 pstatstg->reserved = 0;
1287 /******************************************************************************
1288 * IStorage16_Commit [STORAGE.509]
1290 HRESULT WINAPI IStorage16_fnCommit(
1291 LPSTORAGE16 iface,DWORD commitflags
1293 ICOM_THIS(IStorage16Impl,iface);
1294 FIXME_(ole)("(%p)->(0x%08lx),STUB!\n",
1300 /******************************************************************************
1301 * IStorage16_CopyTo [STORAGE.507]
1303 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1304 ICOM_THIS(IStorage16Impl,iface);
1308 WINE_StringFromCLSID(rgiidExclude,xguid);
1310 strcpy(xguid,"<no guid>");
1311 FIXME_(ole)("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1312 This,ciidExclude,xguid,SNB16Exclude,pstgDest
1318 /******************************************************************************
1319 * IStorage16_CreateStorage [STORAGE.505]
1321 HRESULT WINAPI IStorage16_fnCreateStorage(
1322 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1324 ICOM_THIS(IStorage16Impl,iface);
1325 IStorage16Impl* lpstg;
1327 struct storage_pps_entry stde;
1328 struct storage_header sth;
1333 TRACE_(ole)("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1334 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1336 if (grfMode & STGM_TRANSACTED)
1337 FIXME_(ole)("We do not support transacted Compound Storage. Using direct mode.\n");
1338 _create_istorage16(ppstg);
1339 lpstg = (IStorage16Impl*)PTR_SEG_TO_LIN(*ppstg);
1340 lpstg->hf = This->hf;
1342 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1346 if (stde.pps_dir==-1) {
1347 stde.pps_dir = ppsent;
1350 FIXME_(ole)(" use prev chain too ?\n");
1352 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1354 while (stde.pps_next!=-1) {
1356 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1359 stde.pps_next = ppsent;
1361 assert(STORAGE_put_pps_entry(lpstg->hf,x,&stde));
1362 assert(1==STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)));
1363 lstrcpyAtoW(lpstg->stde.pps_rawname,pwcsName);
1364 lpstg->stde.pps_sizeofname = lstrlenA(pwcsName)*2+2;
1365 lpstg->stde.pps_next = -1;
1366 lpstg->stde.pps_prev = -1;
1367 lpstg->stde.pps_dir = -1;
1368 lpstg->stde.pps_sb = -1;
1369 lpstg->stde.pps_size = 0;
1370 lpstg->stde.pps_type = 1;
1371 lpstg->ppsent = ppsent;
1372 /* FIXME: timestamps? */
1373 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1378 /******************************************************************************
1379 * IStorage16_CreateStream [STORAGE.503]
1381 HRESULT WINAPI IStorage16_fnCreateStream(
1382 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1384 ICOM_THIS(IStorage16Impl,iface);
1385 IStream16Impl* lpstr;
1387 struct storage_pps_entry stde;
1389 TRACE_(ole)("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1390 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1392 if (grfMode & STGM_TRANSACTED)
1393 FIXME_(ole)("We do not support transacted Compound Storage. Using direct mode.\n");
1394 _create_istream16(ppstm);
1395 lpstr = (IStream16Impl*)PTR_SEG_TO_LIN(*ppstm);
1396 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1397 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1398 lpstr->offset.LowPart = 0;
1399 lpstr->offset.HighPart = 0;
1401 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1405 if (stde.pps_next==-1)
1408 while (stde.pps_next!=-1) {
1410 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1413 stde.pps_next = ppsent;
1414 assert(STORAGE_put_pps_entry(lpstr->hf,x,&stde));
1415 assert(1==STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)));
1416 lstrcpyAtoW(lpstr->stde.pps_rawname,pwcsName);
1417 lpstr->stde.pps_sizeofname = lstrlenA(pwcsName)*2+2;
1418 lpstr->stde.pps_next = -1;
1419 lpstr->stde.pps_prev = -1;
1420 lpstr->stde.pps_dir = -1;
1421 lpstr->stde.pps_sb = -1;
1422 lpstr->stde.pps_size = 0;
1423 lpstr->stde.pps_type = 2;
1424 lpstr->ppsent = ppsent;
1425 /* FIXME: timestamps? */
1426 if (!STORAGE_put_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)))
1431 /******************************************************************************
1432 * IStorage16_OpenStorage [STORAGE.506]
1434 HRESULT WINAPI IStorage16_fnOpenStorage(
1435 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, IStorage16 *pstgPrio, DWORD grfMode, SNB16 snbExclude, DWORD reserved, IStorage16 **ppstg
1437 ICOM_THIS(IStorage16Impl,iface);
1438 IStream16Impl* lpstg;
1442 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1443 This,pwcsName,pstgPrio,grfMode,snbExclude,reserved,ppstg
1445 if (grfMode & STGM_TRANSACTED)
1446 FIXME_(ole)("We do not support transacted Compound Storage. Using direct mode.\n");
1447 _create_istorage16(ppstg);
1448 lpstg = (IStream16Impl*)PTR_SEG_TO_LIN(*ppstg);
1449 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1450 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1451 lstrcpyAtoW(name,pwcsName);
1452 newpps = STORAGE_look_for_named_pps(lpstg->hf,This->stde.pps_dir,name);
1454 IStream16_fnRelease((IStream16*)lpstg);
1458 if (1!=STORAGE_get_pps_entry(lpstg->hf,newpps,&(lpstg->stde))) {
1459 IStream16_fnRelease((IStream16*)lpstg);
1462 lpstg->ppsent = newpps;
1466 /******************************************************************************
1467 * IStorage16_OpenStream [STORAGE.504]
1469 HRESULT WINAPI IStorage16_fnOpenStream(
1470 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream16 **ppstm
1472 ICOM_THIS(IStorage16Impl,iface);
1473 IStream16Impl* lpstr;
1477 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1478 This,pwcsName,reserved1,grfMode,reserved2,ppstm
1480 if (grfMode & STGM_TRANSACTED)
1481 FIXME_(ole)("We do not support transacted Compound Storage. Using direct mode.\n");
1482 _create_istream16(ppstm);
1483 lpstr = (IStream16Impl*)PTR_SEG_TO_LIN(*ppstm);
1484 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1485 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1486 lstrcpyAtoW(name,pwcsName);
1487 newpps = STORAGE_look_for_named_pps(lpstr->hf,This->stde.pps_dir,name);
1489 IStream16_fnRelease((IStream16*)lpstr);
1493 if (1!=STORAGE_get_pps_entry(lpstr->hf,newpps,&(lpstr->stde))) {
1494 IStream16_fnRelease((IStream16*)lpstr);
1497 lpstr->offset.LowPart = 0;
1498 lpstr->offset.HighPart = 0;
1499 lpstr->ppsent = newpps;
1503 /******************************************************************************
1504 * _create_istorage16 [INTERNAL]
1506 static void _create_istorage16(LPSTORAGE16 *stg) {
1507 IStorage16Impl* lpst;
1509 if (!stvt16.fnQueryInterface) {
1510 HMODULE16 wp = GetModuleHandle16("STORAGE");
1512 #define VTENT(xfn) stvt16.fn##xfn = (void*)WIN32_GetProcAddress16(wp,"IStorage16_"#xfn);
1513 VTENT(QueryInterface)
1518 VTENT(CreateStorage)
1521 VTENT(MoveElementTo)
1525 VTENT(DestroyElement)
1526 VTENT(RenameElement)
1527 VTENT(SetElementTimes)
1532 segstvt16 = SEGPTR_NEW(ICOM_VTABLE(IStorage16));
1533 memcpy(segstvt16,&stvt16,sizeof(stvt16));
1534 segstvt16 = (ICOM_VTABLE(IStorage16)*)SEGPTR_GET(segstvt16);
1536 #define VTENT(xfn) stvt16.fn##xfn = IStorage16_fn##xfn;
1537 VTENT(QueryInterface)
1542 VTENT(CreateStorage)
1546 /* not (yet) implemented ...
1547 VTENT(MoveElementTo)
1550 VTENT(DestroyElement)
1551 VTENT(RenameElement)
1552 VTENT(SetElementTimes)
1558 segstvt16 = &stvt16;
1561 lpst = SEGPTR_NEW(IStorage16Impl);
1562 lpst->lpvtbl = segstvt16;
1564 lpst->thisptr = SEGPTR_GET(lpst);
1565 *stg = (void*)lpst->thisptr;
1568 /******************************************************************************
1569 * Storage API functions
1572 /******************************************************************************
1573 * StgCreateDocFile16 [STORAGE.1]
1575 HRESULT WINAPI StgCreateDocFile16(
1576 LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved,IStorage16 **ppstgOpen
1580 IStorage16Impl* lpstg;
1581 struct storage_pps_entry stde;
1583 TRACE_(ole)("(%s,0x%08lx,0x%08lx,%p)\n",
1584 pwcsName,grfMode,reserved,ppstgOpen
1586 _create_istorage16(ppstgOpen);
1587 hf = CreateFileA(pwcsName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,0,0);
1588 if (hf==INVALID_HANDLE_VALUE) {
1589 WARN_(ole)("couldn't open file for storage:%ld\n",GetLastError());
1592 lpstg = (IStorage16Impl*)PTR_SEG_TO_LIN(*ppstgOpen);
1594 /* FIXME: check for existance before overwriting? */
1595 if (!STORAGE_init_storage(hf)) {
1600 while (!ret) { /* neither 1 nor <0 */
1601 ret=STORAGE_get_pps_entry(hf,i,&stde);
1602 if ((ret==1) && (stde.pps_type==5)) {
1610 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1617 /******************************************************************************
1618 * StgIsStorageFile16 [STORAGE.5]
1620 HRESULT WINAPI StgIsStorageFile16(LPCOLESTR16 fn) {
1625 TRACE_(ole)("(\'%s\')\n",fn);
1626 hf = OpenFile(fn,&ofs,OF_SHARE_DENY_NONE);
1627 if (hf==HFILE_ERROR)
1628 return STG_E_FILENOTFOUND;
1629 if (24!=_lread(hf,magic,24)) {
1630 WARN_(ole)(" too short\n");
1634 if (!memcmp(magic,STORAGE_magic,8)) {
1635 WARN_(ole)(" -> YES\n");
1639 if (!memcmp(magic,STORAGE_notmagic,8)) {
1640 WARN_(ole)(" -> NO\n");
1644 if (!memcmp(magic,STORAGE_oldmagic,8)) {
1645 WARN_(ole)(" -> old format\n");
1647 return STG_E_OLDFORMAT;
1649 WARN_(ole)(" -> Invalid header.\n");
1651 return STG_E_INVALIDHEADER;
1654 /******************************************************************************
1655 * StgIsStorageFile32 [OLE32.146]
1658 StgIsStorageFile(LPCOLESTR fn)
1660 LPOLESTR16 xfn = HEAP_strdupWtoA(GetProcessHeap(),0,fn);
1661 OLESTATUS ret = StgIsStorageFile16(xfn);
1663 HeapFree(GetProcessHeap(),0,xfn);
1668 /******************************************************************************
1669 * StgOpenStorage16 [STORAGE.3]
1671 HRESULT WINAPI StgOpenStorage16(
1672 const OLECHAR16 * pwcsName,IStorage16 *pstgPriority,DWORD grfMode,
1673 SNB16 snbExclude,DWORD reserved, IStorage16 **ppstgOpen
1677 IStorage16Impl* lpstg;
1678 struct storage_pps_entry stde;
1680 TRACE_(ole)("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1681 pwcsName,pstgPriority,grfMode,snbExclude,reserved,ppstgOpen
1683 _create_istorage16(ppstgOpen);
1684 hf = CreateFileA(pwcsName,GENERIC_READ,0,NULL,0,0,0);
1685 if (hf==INVALID_HANDLE_VALUE) {
1686 WARN_(ole)("Couldn't open file for storage\n");
1689 lpstg = (IStorage16Impl*)PTR_SEG_TO_LIN(*ppstgOpen);
1693 while (!ret) { /* neither 1 nor <0 */
1694 ret=STORAGE_get_pps_entry(hf,i,&stde);
1695 if ((ret==1) && (stde.pps_type==5)) {
1702 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */