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
13 #include <sys/types.h>
18 #include "wine/winestring.h"
19 #include "wine/winbase16.h"
22 #include "wine/obj_base.h"
23 #include "wine/obj_storage.h"
27 #include "debugtools.h"
29 DEFAULT_DEBUG_CHANNEL(ole
);
30 DECLARE_DEBUG_CHANNEL(relay
);
32 struct storage_header
{
33 BYTE magic
[8]; /* 00: magic */
34 BYTE unknown1
[36]; /* 08: unknown */
35 DWORD num_of_bbd_blocks
;/* 2C: length of big datablocks */
36 DWORD root_startblock
;/* 30: root storage first big block */
37 DWORD unknown2
[2]; /* 34: unknown */
38 DWORD sbd_startblock
; /* 3C: small block depot first big block */
39 DWORD unknown3
[3]; /* 40: unknown */
40 DWORD bbd_list
[109]; /* 4C: big data block list (up to end of sector)*/
42 struct storage_pps_entry
{
43 WCHAR pps_rawname
[32];/* 00: \0 terminated widechar name */
44 WORD pps_sizeofname
; /* 40: namelength in bytes */
45 BYTE pps_type
; /* 42: flags, 1 storage/dir, 2 stream, 5 root */
46 BYTE pps_unknown0
; /* 43: unknown */
47 DWORD pps_prev
; /* 44: previous pps */
48 DWORD pps_next
; /* 48: next pps */
49 DWORD pps_dir
; /* 4C: directory pps */
50 GUID pps_guid
; /* 50: class ID */
51 DWORD pps_unknown1
; /* 60: unknown */
52 FILETIME pps_ft1
; /* 64: filetime1 */
53 FILETIME pps_ft2
; /* 70: filetime2 */
54 DWORD pps_sb
; /* 74: data startblock */
55 DWORD pps_size
; /* 78: datalength. (<0x1000)?small:big blocks*/
56 DWORD pps_unknown2
; /* 7C: unknown */
59 #define STORAGE_CHAINENTRY_FAT 0xfffffffd
60 #define STORAGE_CHAINENTRY_ENDOFCHAIN 0xfffffffe
61 #define STORAGE_CHAINENTRY_FREE 0xffffffff
64 static const BYTE STORAGE_magic
[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
65 static const BYTE STORAGE_notmagic
[8]={0x0e,0x11,0xfc,0x0d,0xd0,0xcf,0x11,0xe0};
66 static const BYTE STORAGE_oldmagic
[8]={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
71 #define SMALLBLOCKS_PER_BIGBLOCK (BIGSIZE/SMALLSIZE)
73 #define READ_HEADER assert(STORAGE_get_big_block(hf,-1,(LPBYTE)&sth));assert(!memcmp(STORAGE_magic,sth.magic,sizeof(STORAGE_magic)));
74 static ICOM_VTABLE(IStorage16
) stvt16
;
75 static ICOM_VTABLE(IStorage16
) *segstvt16
= NULL
;
76 static ICOM_VTABLE(IStream16
) strvt16
;
77 static ICOM_VTABLE(IStream16
) *segstrvt16
= NULL
;
79 /*ULONG WINAPI IStorage16_AddRef(LPSTORAGE16 this);*/
80 static void _create_istorage16(LPSTORAGE16
*stg
);
81 static void _create_istream16(LPSTREAM16
*str
);
86 /******************************************************************************
87 * STORAGE_get_big_block [Internal]
89 * Reading OLE compound storage
92 STORAGE_get_big_block(HFILE hf
,int n
,BYTE
*block
) {
94 if (-1==_llseek(hf
,(n
+1)*BIGSIZE
,SEEK_SET
)) {
95 WARN(" seek failed (%ld)\n",GetLastError());
98 assert((n
+1)*BIGSIZE
==_llseek(hf
,0,SEEK_CUR
));
99 if (BIGSIZE
!=_lread(hf
,block
,BIGSIZE
)) {
100 WARN("(block size %d): read didn't read (%ld)\n",n
,GetLastError());
107 /******************************************************************************
108 * STORAGE_put_big_block [INTERNAL]
111 STORAGE_put_big_block(HFILE hf
,int n
,BYTE
*block
) {
113 if (-1==_llseek(hf
,(n
+1)*BIGSIZE
,SEEK_SET
)) {
114 WARN(" seek failed (%ld)\n",GetLastError());
117 assert((n
+1)*BIGSIZE
==_llseek(hf
,0,SEEK_CUR
));
118 if (BIGSIZE
!=_lwrite(hf
,block
,BIGSIZE
)) {
119 WARN(" write failed (%ld)\n",GetLastError());
125 /******************************************************************************
126 * STORAGE_get_next_big_blocknr [INTERNAL]
129 STORAGE_get_next_big_blocknr(HFILE hf
,int blocknr
) {
130 INT bbs
[BIGSIZE
/sizeof(INT
)];
131 struct storage_header sth
;
135 assert(blocknr
>>7<sth
.num_of_bbd_blocks
);
136 if (sth
.bbd_list
[blocknr
>>7]==0xffffffff)
138 if (!STORAGE_get_big_block(hf
,sth
.bbd_list
[blocknr
>>7],(LPBYTE
)bbs
))
140 assert(bbs
[blocknr
&0x7f]!=STORAGE_CHAINENTRY_FREE
);
141 return bbs
[blocknr
&0x7f];
144 /******************************************************************************
145 * STORAGE_get_nth_next_big_blocknr [INTERNAL]
148 STORAGE_get_nth_next_big_blocknr(HFILE hf
,int blocknr
,int nr
) {
149 INT bbs
[BIGSIZE
/sizeof(INT
)];
151 struct storage_header sth
;
157 assert((blocknr
>>7)<sth
.num_of_bbd_blocks
);
158 assert(sth
.bbd_list
[blocknr
>>7]!=0xffffffff);
160 /* simple caching... */
161 if (lastblock
!=sth
.bbd_list
[blocknr
>>7]) {
162 assert(STORAGE_get_big_block(hf
,sth
.bbd_list
[blocknr
>>7],(LPBYTE
)bbs
));
163 lastblock
= sth
.bbd_list
[blocknr
>>7];
165 blocknr
= bbs
[blocknr
&0x7f];
170 /******************************************************************************
171 * STORAGE_get_root_pps_entry [Internal]
174 STORAGE_get_root_pps_entry(HFILE hf
,struct storage_pps_entry
*pstde
) {
177 struct storage_pps_entry
*stde
=(struct storage_pps_entry
*)block
;
178 struct storage_header sth
;
181 blocknr
= sth
.root_startblock
;
183 assert(STORAGE_get_big_block(hf
,blocknr
,block
));
185 if (!stde
[i
].pps_sizeofname
)
187 if (stde
[i
].pps_type
==5) {
192 blocknr
=STORAGE_get_next_big_blocknr(hf
,blocknr
);
197 /******************************************************************************
198 * STORAGE_get_small_block [INTERNAL]
201 STORAGE_get_small_block(HFILE hf
,int blocknr
,BYTE
*sblock
) {
204 struct storage_pps_entry root
;
207 assert(STORAGE_get_root_pps_entry(hf
,&root
));
208 bigblocknr
= STORAGE_get_nth_next_big_blocknr(hf
,root
.pps_sb
,blocknr
/SMALLBLOCKS_PER_BIGBLOCK
);
209 assert(bigblocknr
>=0);
210 assert(STORAGE_get_big_block(hf
,bigblocknr
,block
));
212 memcpy(sblock
,((LPBYTE
)block
)+SMALLSIZE
*(blocknr
&(SMALLBLOCKS_PER_BIGBLOCK
-1)),SMALLSIZE
);
216 /******************************************************************************
217 * STORAGE_put_small_block [INTERNAL]
220 STORAGE_put_small_block(HFILE hf
,int blocknr
,BYTE
*sblock
) {
223 struct storage_pps_entry root
;
227 assert(STORAGE_get_root_pps_entry(hf
,&root
));
228 bigblocknr
= STORAGE_get_nth_next_big_blocknr(hf
,root
.pps_sb
,blocknr
/SMALLBLOCKS_PER_BIGBLOCK
);
229 assert(bigblocknr
>=0);
230 assert(STORAGE_get_big_block(hf
,bigblocknr
,block
));
232 memcpy(((LPBYTE
)block
)+SMALLSIZE
*(blocknr
&(SMALLBLOCKS_PER_BIGBLOCK
-1)),sblock
,SMALLSIZE
);
233 assert(STORAGE_put_big_block(hf
,bigblocknr
,block
));
237 /******************************************************************************
238 * STORAGE_get_next_small_blocknr [INTERNAL]
241 STORAGE_get_next_small_blocknr(HFILE hf
,int blocknr
) {
243 LPINT sbd
= (LPINT
)block
;
245 struct storage_header sth
;
249 bigblocknr
= STORAGE_get_nth_next_big_blocknr(hf
,sth
.sbd_startblock
,blocknr
/128);
250 assert(bigblocknr
>=0);
251 assert(STORAGE_get_big_block(hf
,bigblocknr
,block
));
252 assert(sbd
[blocknr
& 127]!=STORAGE_CHAINENTRY_FREE
);
253 return sbd
[blocknr
& (128-1)];
256 /******************************************************************************
257 * STORAGE_get_nth_next_small_blocknr [INTERNAL]
260 STORAGE_get_nth_next_small_blocknr(HFILE hf
,int blocknr
,int nr
) {
263 LPINT sbd
= (LPINT
)block
;
264 struct storage_header sth
;
269 while ((nr
--) && (blocknr
>=0)) {
270 if (lastblocknr
/128!=blocknr
/128) {
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 lastblocknr
= blocknr
;
277 assert(lastblocknr
>=0);
279 blocknr
=sbd
[blocknr
& (128-1)];
280 assert(blocknr
!=STORAGE_CHAINENTRY_FREE
);
285 /******************************************************************************
286 * STORAGE_get_pps_entry [INTERNAL]
289 STORAGE_get_pps_entry(HFILE hf
,int n
,struct storage_pps_entry
*pstde
) {
292 struct storage_pps_entry
*stde
= (struct storage_pps_entry
*)(((LPBYTE
)block
)+128*(n
&3));
293 struct storage_header sth
;
296 /* we have 4 pps entries per big block */
297 blocknr
= STORAGE_get_nth_next_big_blocknr(hf
,sth
.root_startblock
,n
/4);
299 assert(STORAGE_get_big_block(hf
,blocknr
,block
));
305 /******************************************************************************
306 * STORAGE_put_pps_entry [Internal]
309 STORAGE_put_pps_entry(HFILE hf
,int n
,struct storage_pps_entry
*pstde
) {
312 struct storage_pps_entry
*stde
= (struct storage_pps_entry
*)(((LPBYTE
)block
)+128*(n
&3));
313 struct storage_header sth
;
317 /* we have 4 pps entries per big block */
318 blocknr
= STORAGE_get_nth_next_big_blocknr(hf
,sth
.root_startblock
,n
/4);
320 assert(STORAGE_get_big_block(hf
,blocknr
,block
));
322 assert(STORAGE_put_big_block(hf
,blocknr
,block
));
326 /******************************************************************************
327 * STORAGE_look_for_named_pps [Internal]
330 STORAGE_look_for_named_pps(HFILE hf
,int n
,LPOLESTR name
) {
331 struct storage_pps_entry stde
;
336 if (1!=STORAGE_get_pps_entry(hf
,n
,&stde
))
339 if (!lstrcmpW(name
,stde
.pps_rawname
))
341 if (stde
.pps_prev
!= -1) {
342 ret
=STORAGE_look_for_named_pps(hf
,stde
.pps_prev
,name
);
346 if (stde
.pps_next
!= -1) {
347 ret
=STORAGE_look_for_named_pps(hf
,stde
.pps_next
,name
);
354 /******************************************************************************
355 * STORAGE_dump_pps_entry [Internal]
361 STORAGE_dump_pps_entry(struct storage_pps_entry
*stde
) {
364 lstrcpyWtoA(name
,stde
->pps_rawname
);
365 if (!stde
->pps_sizeofname
)
367 DPRINTF("name: %s\n",name
);
368 DPRINTF("type: %d\n",stde
->pps_type
);
369 DPRINTF("prev pps: %ld\n",stde
->pps_prev
);
370 DPRINTF("next pps: %ld\n",stde
->pps_next
);
371 DPRINTF("dir pps: %ld\n",stde
->pps_dir
);
372 DPRINTF("guid: %s\n",debugstr_guid(&(stde
->pps_guid
)));
373 if (stde
->pps_type
!=2) {
376 RtlTimeToSecondsSince1970(&(stde
->pps_ft1
),&dw
);
378 DPRINTF("ts1: %s\n",ctime(&t
));
379 RtlTimeToSecondsSince1970(&(stde
->pps_ft2
),&dw
);
381 DPRINTF("ts2: %s\n",ctime(&t
));
383 DPRINTF("startblock: %ld\n",stde
->pps_sb
);
384 DPRINTF("size: %ld\n",stde
->pps_size
);
387 /******************************************************************************
388 * STORAGE_init_storage [INTERNAL]
391 STORAGE_init_storage(HFILE hf
) {
394 struct storage_header
*sth
;
395 struct storage_pps_entry
*stde
;
397 assert(-1!=_llseek(hf
,0,SEEK_SET
));
398 /* block -1 is the storage header */
399 sth
= (struct storage_header
*)block
;
400 memcpy(sth
->magic
,STORAGE_magic
,8);
401 memset(sth
->unknown1
,0,sizeof(sth
->unknown1
));
402 memset(sth
->unknown2
,0,sizeof(sth
->unknown2
));
403 memset(sth
->unknown3
,0,sizeof(sth
->unknown3
));
404 sth
->num_of_bbd_blocks
= 1;
405 sth
->root_startblock
= 1;
406 sth
->sbd_startblock
= 0xffffffff;
407 memset(sth
->bbd_list
,0xff,sizeof(sth
->bbd_list
));
408 sth
->bbd_list
[0] = 0;
409 assert(BIGSIZE
==_lwrite(hf
,block
,BIGSIZE
));
410 /* block 0 is the big block directory */
412 memset(block
,0xff,sizeof(block
)); /* mark all blocks as free */
413 bbs
[0]=STORAGE_CHAINENTRY_ENDOFCHAIN
; /* for this block */
414 bbs
[1]=STORAGE_CHAINENTRY_ENDOFCHAIN
; /* for directory entry */
415 assert(BIGSIZE
==_lwrite(hf
,block
,BIGSIZE
));
416 /* block 1 is the root directory entry */
417 memset(block
,0x00,sizeof(block
));
418 stde
= (struct storage_pps_entry
*)block
;
419 lstrcpyAtoW(stde
->pps_rawname
,"RootEntry");
420 stde
->pps_sizeofname
= lstrlenW(stde
->pps_rawname
)*2+2;
425 stde
->pps_sb
= 0xffffffff;
427 assert(BIGSIZE
==_lwrite(hf
,block
,BIGSIZE
));
431 /******************************************************************************
432 * STORAGE_set_big_chain [Internal]
435 STORAGE_set_big_chain(HFILE hf
,int blocknr
,INT type
) {
437 LPINT bbd
= (LPINT
)block
;
438 int nextblocknr
,bigblocknr
;
439 struct storage_header sth
;
442 assert(blocknr
!=type
);
444 bigblocknr
= sth
.bbd_list
[blocknr
/128];
445 assert(bigblocknr
>=0);
446 assert(STORAGE_get_big_block(hf
,bigblocknr
,block
));
448 nextblocknr
= bbd
[blocknr
&(128-1)];
449 bbd
[blocknr
&(128-1)] = type
;
452 assert(STORAGE_put_big_block(hf
,bigblocknr
,block
));
453 type
= STORAGE_CHAINENTRY_FREE
;
454 blocknr
= nextblocknr
;
459 /******************************************************************************
460 * STORAGE_set_small_chain [Internal]
463 STORAGE_set_small_chain(HFILE hf
,int blocknr
,INT type
) {
465 LPINT sbd
= (LPINT
)block
;
466 int lastblocknr
,nextsmallblocknr
,bigblocknr
;
467 struct storage_header sth
;
471 assert(blocknr
!=type
);
472 lastblocknr
=-129;bigblocknr
=-2;
474 /* cache block ... */
475 if (lastblocknr
/128!=blocknr
/128) {
476 bigblocknr
= STORAGE_get_nth_next_big_blocknr(hf
,sth
.sbd_startblock
,blocknr
/128);
477 assert(bigblocknr
>=0);
478 assert(STORAGE_get_big_block(hf
,bigblocknr
,block
));
480 lastblocknr
= blocknr
;
481 nextsmallblocknr
= sbd
[blocknr
&(128-1)];
482 sbd
[blocknr
&(128-1)] = type
;
483 assert(STORAGE_put_big_block(hf
,bigblocknr
,block
));
486 type
= STORAGE_CHAINENTRY_FREE
;
487 blocknr
= nextsmallblocknr
;
492 /******************************************************************************
493 * STORAGE_get_free_big_blocknr [Internal]
496 STORAGE_get_free_big_blocknr(HFILE hf
) {
498 LPINT sbd
= (LPINT
)block
;
499 int lastbigblocknr
,i
,curblock
,bigblocknr
;
500 struct storage_header sth
;
505 bigblocknr
= sth
.bbd_list
[curblock
];
506 while (curblock
<sth
.num_of_bbd_blocks
) {
507 assert(bigblocknr
>=0);
508 assert(STORAGE_get_big_block(hf
,bigblocknr
,block
));
510 if (sbd
[i
]==STORAGE_CHAINENTRY_FREE
) {
511 sbd
[i
] = STORAGE_CHAINENTRY_ENDOFCHAIN
;
512 assert(STORAGE_put_big_block(hf
,bigblocknr
,block
));
513 memset(block
,0x42,sizeof(block
));
514 assert(STORAGE_put_big_block(hf
,i
+curblock
*128,block
));
515 return i
+curblock
*128;
517 lastbigblocknr
= bigblocknr
;
518 bigblocknr
= sth
.bbd_list
[++curblock
];
520 bigblocknr
= curblock
*128;
521 /* since we have marked all blocks from 0 up to curblock*128-1
522 * the next free one is curblock*128, where we happily put our
523 * next large block depot.
525 memset(block
,0xff,sizeof(block
));
526 /* mark the block allocated and returned by this function */
527 sbd
[1] = STORAGE_CHAINENTRY_ENDOFCHAIN
;
528 assert(STORAGE_put_big_block(hf
,bigblocknr
,block
));
530 /* if we had a bbd block already (mostlikely) we need
531 * to link the new one into the chain
533 if (lastbigblocknr
!=-1)
534 assert(STORAGE_set_big_chain(hf
,lastbigblocknr
,bigblocknr
));
535 sth
.bbd_list
[curblock
]=bigblocknr
;
536 sth
.num_of_bbd_blocks
++;
537 assert(sth
.num_of_bbd_blocks
==curblock
+1);
538 assert(STORAGE_put_big_block(hf
,-1,(LPBYTE
)&sth
));
540 /* Set the end of the chain for the bigblockdepots */
541 assert(STORAGE_set_big_chain(hf
,bigblocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
));
542 /* add 1, for the first entry is used for the additional big block
543 * depot. (means we already used bigblocknr) */
544 memset(block
,0x42,sizeof(block
));
545 /* allocate this block (filled with 0x42) */
546 assert(STORAGE_put_big_block(hf
,bigblocknr
+1,block
));
551 /******************************************************************************
552 * STORAGE_get_free_small_blocknr [Internal]
555 STORAGE_get_free_small_blocknr(HFILE hf
) {
557 LPINT sbd
= (LPINT
)block
;
558 int lastbigblocknr
,newblocknr
,i
,curblock
,bigblocknr
;
559 struct storage_pps_entry root
;
560 struct storage_header sth
;
563 bigblocknr
= sth
.sbd_startblock
;
567 while (bigblocknr
>=0) {
568 if (!STORAGE_get_big_block(hf
,bigblocknr
,block
))
571 if (sbd
[i
]==STORAGE_CHAINENTRY_FREE
) {
572 sbd
[i
]=STORAGE_CHAINENTRY_ENDOFCHAIN
;
573 newblocknr
= i
+curblock
*128;
578 lastbigblocknr
= bigblocknr
;
579 bigblocknr
= STORAGE_get_next_big_blocknr(hf
,bigblocknr
);
582 if (newblocknr
==-1) {
583 bigblocknr
= STORAGE_get_free_big_blocknr(hf
);
587 memset(block
,0xff,sizeof(block
));
588 sbd
[0]=STORAGE_CHAINENTRY_ENDOFCHAIN
;
589 if (!STORAGE_put_big_block(hf
,bigblocknr
,block
))
591 if (lastbigblocknr
==-1) {
592 sth
.sbd_startblock
= bigblocknr
;
593 if (!STORAGE_put_big_block(hf
,-1,(LPBYTE
)&sth
)) /* need to write it */
596 if (!STORAGE_set_big_chain(hf
,lastbigblocknr
,bigblocknr
))
599 if (!STORAGE_set_big_chain(hf
,bigblocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
601 newblocknr
= curblock
*128;
603 /* allocate enough big blocks for storing the allocated small block */
604 if (!STORAGE_get_root_pps_entry(hf
,&root
))
609 lastbigblocknr
= STORAGE_get_nth_next_big_blocknr(hf
,root
.pps_sb
,(root
.pps_size
-1)/BIGSIZE
);
610 while (root
.pps_size
< (newblocknr
*SMALLSIZE
+SMALLSIZE
-1)) {
611 /* we need to allocate more stuff */
612 bigblocknr
= STORAGE_get_free_big_blocknr(hf
);
616 if (root
.pps_sb
==-1) {
617 root
.pps_sb
= bigblocknr
;
618 root
.pps_size
+= BIGSIZE
;
620 if (!STORAGE_set_big_chain(hf
,lastbigblocknr
,bigblocknr
))
622 root
.pps_size
+= BIGSIZE
;
624 lastbigblocknr
= bigblocknr
;
626 if (!STORAGE_set_big_chain(hf
,lastbigblocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
628 if (!STORAGE_put_pps_entry(hf
,0,&root
))
633 /******************************************************************************
634 * STORAGE_get_free_pps_entry [Internal]
637 STORAGE_get_free_pps_entry(HFILE hf
) {
638 int blocknr
,i
,curblock
,lastblocknr
;
640 struct storage_pps_entry
*stde
= (struct storage_pps_entry
*)block
;
641 struct storage_header sth
;
644 blocknr
= sth
.root_startblock
;
648 if (!STORAGE_get_big_block(hf
,blocknr
,block
))
651 if (stde
[i
].pps_sizeofname
==0) /* free */
653 lastblocknr
= blocknr
;
654 blocknr
= STORAGE_get_next_big_blocknr(hf
,blocknr
);
657 assert(blocknr
==STORAGE_CHAINENTRY_ENDOFCHAIN
);
658 blocknr
= STORAGE_get_free_big_blocknr(hf
);
659 /* sth invalidated */
663 if (!STORAGE_set_big_chain(hf
,lastblocknr
,blocknr
))
665 if (!STORAGE_set_big_chain(hf
,blocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
667 memset(block
,0,sizeof(block
));
668 STORAGE_put_big_block(hf
,blocknr
,block
);
672 /* --- IStream16 implementation */
676 /* IUnknown fields */
677 ICOM_VFIELD(IStream16
);
679 /* IStream16 fields */
680 SEGPTR thisptr
; /* pointer to this struct as segmented */
681 struct storage_pps_entry stde
;
684 ULARGE_INTEGER offset
;
687 /******************************************************************************
688 * IStream16_QueryInterface [STORAGE.518]
690 HRESULT WINAPI
IStream16_fnQueryInterface(
691 IStream16
* iface
,REFIID refiid
,LPVOID
*obj
693 ICOM_THIS(IStream16Impl
,iface
);
694 TRACE_(relay
)("(%p)->(%s,%p)\n",This
,debugstr_guid(refiid
),obj
);
695 if (!memcmp(&IID_IUnknown
,refiid
,sizeof(IID_IUnknown
))) {
699 return OLE_E_ENUM_NOMORE
;
703 /******************************************************************************
704 * IStream16_AddRef [STORAGE.519]
706 ULONG WINAPI
IStream16_fnAddRef(IStream16
* iface
) {
707 ICOM_THIS(IStream16Impl
,iface
);
708 return ++(This
->ref
);
711 /******************************************************************************
712 * IStream16_Release [STORAGE.520]
714 ULONG WINAPI
IStream16_fnRelease(IStream16
* iface
) {
715 ICOM_THIS(IStream16Impl
,iface
);
716 FlushFileBuffers(This
->hf
);
719 CloseHandle(This
->hf
);
726 /******************************************************************************
727 * IStream16_Seek [STORAGE.523]
730 * Does not handle 64 bits
732 HRESULT WINAPI
IStream16_fnSeek(
733 IStream16
* iface
,LARGE_INTEGER offset
,DWORD whence
,ULARGE_INTEGER
*newpos
735 ICOM_THIS(IStream16Impl
,iface
);
736 TRACE_(relay
)("(%p)->([%ld.%ld],%ld,%p)\n",This
,offset
.s
.HighPart
,offset
.s
.LowPart
,whence
,newpos
);
739 /* unix SEEK_xx should be the same as win95 ones */
741 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
744 assert(offset
.s
.HighPart
==0);
745 This
->offset
.s
.HighPart
= offset
.s
.HighPart
;
746 This
->offset
.s
.LowPart
= offset
.s
.LowPart
;
749 if (offset
.s
.HighPart
< 0) {
750 /* FIXME: is this negation correct ? */
751 offset
.s
.HighPart
= -offset
.s
.HighPart
;
752 offset
.s
.LowPart
= (0xffffffff ^ offset
.s
.LowPart
)+1;
754 assert(offset
.s
.HighPart
==0);
755 assert(This
->offset
.s
.LowPart
>= offset
.s
.LowPart
);
756 This
->offset
.s
.LowPart
-= offset
.s
.LowPart
;
758 assert(offset
.s
.HighPart
==0);
759 This
->offset
.s
.LowPart
+= offset
.s
.LowPart
;
763 assert(offset
.s
.HighPart
==0);
764 This
->offset
.s
.LowPart
= This
->stde
.pps_size
-offset
.s
.LowPart
;
767 if (This
->offset
.s
.LowPart
>This
->stde
.pps_size
)
768 This
->offset
.s
.LowPart
=This
->stde
.pps_size
;
769 if (newpos
) *newpos
= This
->offset
;
773 /******************************************************************************
774 * IStream16_Read [STORAGE.521]
776 HRESULT WINAPI
IStream16_fnRead(
777 IStream16
* iface
,void *pv
,ULONG cb
,ULONG
*pcbRead
779 ICOM_THIS(IStream16Impl
,iface
);
781 ULONG
*bytesread
=pcbRead
,xxread
;
784 TRACE_(relay
)("(%p)->(%p,%ld,%p)\n",This
,pv
,cb
,pcbRead
);
785 if (!pcbRead
) bytesread
=&xxread
;
788 if (cb
>This
->stde
.pps_size
-This
->offset
.s
.LowPart
)
789 cb
=This
->stde
.pps_size
-This
->offset
.s
.LowPart
;
790 if (This
->stde
.pps_size
< 0x1000) {
791 /* use small block reader */
792 blocknr
= STORAGE_get_nth_next_small_blocknr(This
->hf
,This
->stde
.pps_sb
,This
->offset
.s
.LowPart
/SMALLSIZE
);
796 if (!STORAGE_get_small_block(This
->hf
,blocknr
,block
)) {
797 WARN("small block read failed!!!\n");
801 if (cc
>SMALLSIZE
-(This
->offset
.s
.LowPart
&(SMALLSIZE
-1)))
802 cc
=SMALLSIZE
-(This
->offset
.s
.LowPart
&(SMALLSIZE
-1));
803 memcpy((LPBYTE
)pv
,block
+(This
->offset
.s
.LowPart
&(SMALLSIZE
-1)),cc
);
804 This
->offset
.s
.LowPart
+=cc
;
808 blocknr
= STORAGE_get_next_small_blocknr(This
->hf
,blocknr
);
811 /* use big block reader */
812 blocknr
= STORAGE_get_nth_next_big_blocknr(This
->hf
,This
->stde
.pps_sb
,This
->offset
.s
.LowPart
/BIGSIZE
);
816 if (!STORAGE_get_big_block(This
->hf
,blocknr
,block
)) {
817 WARN("big block read failed!!!\n");
821 if (cc
>BIGSIZE
-(This
->offset
.s
.LowPart
&(BIGSIZE
-1)))
822 cc
=BIGSIZE
-(This
->offset
.s
.LowPart
&(BIGSIZE
-1));
823 memcpy((LPBYTE
)pv
,block
+(This
->offset
.s
.LowPart
&(BIGSIZE
-1)),cc
);
824 This
->offset
.s
.LowPart
+=cc
;
828 blocknr
=STORAGE_get_next_big_blocknr(This
->hf
,blocknr
);
834 /******************************************************************************
835 * IStream16_Write [STORAGE.522]
837 HRESULT WINAPI
IStream16_fnWrite(
838 IStream16
* iface
,const void *pv
,ULONG cb
,ULONG
*pcbWrite
840 ICOM_THIS(IStream16Impl
,iface
);
842 ULONG
*byteswritten
=pcbWrite
,xxwritten
;
843 int oldsize
,newsize
,i
,curoffset
=0,lastblocknr
,blocknr
,cc
;
846 if (!pcbWrite
) byteswritten
=&xxwritten
;
849 TRACE_(relay
)("(%p)->(%p,%ld,%p)\n",This
,pv
,cb
,pcbWrite
);
850 /* do we need to junk some blocks? */
851 newsize
= This
->offset
.s
.LowPart
+cb
;
852 oldsize
= This
->stde
.pps_size
;
853 if (newsize
< oldsize
) {
854 if (oldsize
< 0x1000) {
855 /* only small blocks */
856 blocknr
=STORAGE_get_nth_next_small_blocknr(hf
,This
->stde
.pps_sb
,newsize
/SMALLSIZE
);
860 /* will set the rest of the chain to 'free' */
861 if (!STORAGE_set_small_chain(hf
,blocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
864 if (newsize
>= 0x1000) {
865 blocknr
=STORAGE_get_nth_next_big_blocknr(hf
,This
->stde
.pps_sb
,newsize
/BIGSIZE
);
868 /* will set the rest of the chain to 'free' */
869 if (!STORAGE_set_big_chain(hf
,blocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
872 /* Migrate large blocks to small blocks
873 * (we just migrate newsize bytes)
875 LPBYTE curdata
,data
= HeapAlloc(GetProcessHeap(),0,newsize
+BIGSIZE
);
877 blocknr
= This
->stde
.pps_sb
;
880 if (!STORAGE_get_big_block(hf
,blocknr
,curdata
)) {
881 HeapFree(GetProcessHeap(),0,data
);
886 blocknr
= STORAGE_get_next_big_blocknr(hf
,blocknr
);
888 /* frees complete chain for this stream */
889 if (!STORAGE_set_big_chain(hf
,This
->stde
.pps_sb
,STORAGE_CHAINENTRY_FREE
))
892 blocknr
= This
->stde
.pps_sb
= STORAGE_get_free_small_blocknr(hf
);
897 if (!STORAGE_put_small_block(hf
,blocknr
,curdata
))
901 if (!STORAGE_set_small_chain(hf
,blocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
905 int newblocknr
= STORAGE_get_free_small_blocknr(hf
);
908 if (!STORAGE_set_small_chain(hf
,blocknr
,newblocknr
))
910 blocknr
= newblocknr
;
912 curdata
+= SMALLSIZE
;
914 HeapFree(GetProcessHeap(),0,data
);
917 This
->stde
.pps_size
= newsize
;
920 if (newsize
> oldsize
) {
921 if (oldsize
>= 0x1000) {
922 /* should return the block right before the 'endofchain' */
923 blocknr
= STORAGE_get_nth_next_big_blocknr(hf
,This
->stde
.pps_sb
,This
->stde
.pps_size
/BIGSIZE
);
925 lastblocknr
= blocknr
;
926 for (i
=oldsize
/BIGSIZE
;i
<newsize
/BIGSIZE
;i
++) {
927 blocknr
= STORAGE_get_free_big_blocknr(hf
);
930 if (!STORAGE_set_big_chain(hf
,lastblocknr
,blocknr
))
932 lastblocknr
= blocknr
;
934 if (!STORAGE_set_big_chain(hf
,blocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
937 if (newsize
< 0x1000) {
938 /* find startblock */
940 This
->stde
.pps_sb
= blocknr
= STORAGE_get_free_small_blocknr(hf
);
942 blocknr
= STORAGE_get_nth_next_small_blocknr(hf
,This
->stde
.pps_sb
,This
->stde
.pps_size
/SMALLSIZE
);
946 /* allocate required new small blocks */
947 lastblocknr
= blocknr
;
948 for (i
=oldsize
/SMALLSIZE
;i
<newsize
/SMALLSIZE
;i
++) {
949 blocknr
= STORAGE_get_free_small_blocknr(hf
);
952 if (!STORAGE_set_small_chain(hf
,lastblocknr
,blocknr
))
954 lastblocknr
= blocknr
;
956 /* and terminate the chain */
957 if (!STORAGE_set_small_chain(hf
,lastblocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
961 /* no single block allocated yet */
962 blocknr
=STORAGE_get_free_big_blocknr(hf
);
965 This
->stde
.pps_sb
= blocknr
;
967 /* Migrate small blocks to big blocks */
968 LPBYTE curdata
,data
= HeapAlloc(GetProcessHeap(),0,oldsize
+BIGSIZE
);
970 blocknr
= This
->stde
.pps_sb
;
974 if (!STORAGE_get_small_block(hf
,blocknr
,curdata
)) {
975 HeapFree(GetProcessHeap(),0,data
);
978 curdata
+= SMALLSIZE
;
980 blocknr
= STORAGE_get_next_small_blocknr(hf
,blocknr
);
982 /* free small block chain */
983 if (!STORAGE_set_small_chain(hf
,This
->stde
.pps_sb
,STORAGE_CHAINENTRY_FREE
))
986 blocknr
= This
->stde
.pps_sb
= STORAGE_get_free_big_blocknr(hf
);
989 /* put the data into the big blocks */
990 cc
= This
->stde
.pps_size
;
992 if (!STORAGE_put_big_block(hf
,blocknr
,curdata
))
996 if (!STORAGE_set_big_chain(hf
,blocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
1000 int newblocknr
= STORAGE_get_free_big_blocknr(hf
);
1003 if (!STORAGE_set_big_chain(hf
,blocknr
,newblocknr
))
1005 blocknr
= newblocknr
;
1009 HeapFree(GetProcessHeap(),0,data
);
1011 /* generate big blocks to fit the new data */
1012 lastblocknr
= blocknr
;
1013 for (i
=oldsize
/BIGSIZE
;i
<newsize
/BIGSIZE
;i
++) {
1014 blocknr
= STORAGE_get_free_big_blocknr(hf
);
1017 if (!STORAGE_set_big_chain(hf
,lastblocknr
,blocknr
))
1019 lastblocknr
= blocknr
;
1021 /* terminate chain */
1022 if (!STORAGE_set_big_chain(hf
,lastblocknr
,STORAGE_CHAINENTRY_ENDOFCHAIN
))
1026 This
->stde
.pps_size
= newsize
;
1029 /* There are just some cases where we didn't modify it, we write it out
1032 if (!STORAGE_put_pps_entry(hf
,This
->ppsent
,&(This
->stde
)))
1035 /* finally the write pass */
1036 if (This
->stde
.pps_size
< 0x1000) {
1037 blocknr
= STORAGE_get_nth_next_small_blocknr(hf
,This
->stde
.pps_sb
,This
->offset
.s
.LowPart
/SMALLSIZE
);
1040 /* we ensured that it is allocated above */
1042 /* Read old block everytime, since we can have
1043 * overlapping data at START and END of the write
1045 if (!STORAGE_get_small_block(hf
,blocknr
,block
))
1048 cc
= SMALLSIZE
-(This
->offset
.s
.LowPart
&(SMALLSIZE
-1));
1051 memcpy( ((LPBYTE
)block
)+(This
->offset
.s
.LowPart
&(SMALLSIZE
-1)),
1052 (LPBYTE
)((char *) pv
+curoffset
),
1055 if (!STORAGE_put_small_block(hf
,blocknr
,block
))
1060 This
->offset
.s
.LowPart
+= cc
;
1061 *byteswritten
+= cc
;
1062 blocknr
= STORAGE_get_next_small_blocknr(hf
,blocknr
);
1065 blocknr
= STORAGE_get_nth_next_big_blocknr(hf
,This
->stde
.pps_sb
,This
->offset
.s
.LowPart
/BIGSIZE
);
1068 /* we ensured that it is allocated above, so it better is */
1070 /* read old block everytime, since we can have
1071 * overlapping data at START and END of the write
1073 if (!STORAGE_get_big_block(hf
,blocknr
,block
))
1076 cc
= BIGSIZE
-(This
->offset
.s
.LowPart
&(BIGSIZE
-1));
1079 memcpy( ((LPBYTE
)block
)+(This
->offset
.s
.LowPart
&(BIGSIZE
-1)),
1080 (LPBYTE
)((char *) pv
+curoffset
),
1083 if (!STORAGE_put_big_block(hf
,blocknr
,block
))
1088 This
->offset
.s
.LowPart
+= cc
;
1089 *byteswritten
+= cc
;
1090 blocknr
= STORAGE_get_next_big_blocknr(hf
,blocknr
);
1096 /******************************************************************************
1097 * _create_istream16 [Internal]
1099 static void _create_istream16(LPSTREAM16
*str
) {
1100 IStream16Impl
* lpst
;
1102 if (!strvt16
.fnQueryInterface
) {
1103 HMODULE16 wp
= GetModuleHandle16("STORAGE");
1105 /* FIXME: what is This WIN32_GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1106 #define VTENT(xfn) strvt16.fn##xfn = (void*)WIN32_GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.fn##xfn)
1107 VTENT(QueryInterface
);
1118 VTENT(UnlockRegion
);
1122 segstrvt16
= SEGPTR_NEW(ICOM_VTABLE(IStream16
));
1123 memcpy(segstrvt16
,&strvt16
,sizeof(strvt16
));
1124 segstrvt16
= (ICOM_VTABLE(IStream16
)*)SEGPTR_GET(segstrvt16
);
1126 #define VTENT(xfn) strvt16.fn##xfn = IStream16_fn##xfn;
1127 VTENT(QueryInterface
);
1139 VTENT(UnlockRegion);
1144 segstrvt16
= &strvt16
;
1147 lpst
= SEGPTR_NEW(IStream16Impl
);
1148 ICOM_VTBL(lpst
) = segstrvt16
;
1150 lpst
->thisptr
= SEGPTR_GET(lpst
);
1151 *str
= (void*)lpst
->thisptr
;
1155 /* --- IStream32 implementation */
1159 /* IUnknown fields */
1160 ICOM_VFIELD(IStream
);
1162 /* IStream32 fields */
1163 struct storage_pps_entry stde
;
1166 ULARGE_INTEGER offset
;
1169 /*****************************************************************************
1170 * IStream32_QueryInterface [VTABLE]
1172 HRESULT WINAPI
IStream_fnQueryInterface(
1173 IStream
* iface
,REFIID refiid
,LPVOID
*obj
1175 ICOM_THIS(IStream32Impl
,iface
);
1177 TRACE_(relay
)("(%p)->(%s,%p)\n",This
,debugstr_guid(refiid
),obj
);
1178 if (!memcmp(&IID_IUnknown
,refiid
,sizeof(IID_IUnknown
))) {
1182 return OLE_E_ENUM_NOMORE
;
1186 /******************************************************************************
1187 * IStream32_AddRef [VTABLE]
1189 ULONG WINAPI
IStream_fnAddRef(IStream
* iface
) {
1190 ICOM_THIS(IStream32Impl
,iface
);
1191 return ++(This
->ref
);
1194 /******************************************************************************
1195 * IStream32_Release [VTABLE]
1197 ULONG WINAPI
IStream_fnRelease(IStream
* iface
) {
1198 ICOM_THIS(IStream32Impl
,iface
);
1199 FlushFileBuffers(This
->hf
);
1202 CloseHandle(This
->hf
);
1209 /* --- IStorage16 implementation */
1213 /* IUnknown fields */
1214 ICOM_VFIELD(IStorage16
);
1216 /* IStorage16 fields */
1217 SEGPTR thisptr
; /* pointer to this struct as segmented */
1218 struct storage_pps_entry stde
;
1223 /******************************************************************************
1224 * IStorage16_QueryInterface [STORAGE.500]
1226 HRESULT WINAPI
IStorage16_fnQueryInterface(
1227 IStorage16
* iface
,REFIID refiid
,LPVOID
*obj
1229 ICOM_THIS(IStorage16Impl
,iface
);
1231 TRACE_(relay
)("(%p)->(%s,%p)\n",This
,debugstr_guid(refiid
),obj
);
1233 if (!memcmp(&IID_IUnknown
,refiid
,sizeof(IID_IUnknown
))) {
1237 return OLE_E_ENUM_NOMORE
;
1240 /******************************************************************************
1241 * IStorage16_AddRef [STORAGE.501]
1243 ULONG WINAPI
IStorage16_fnAddRef(IStorage16
* iface
) {
1244 ICOM_THIS(IStorage16Impl
,iface
);
1245 return ++(This
->ref
);
1248 /******************************************************************************
1249 * IStorage16_Release [STORAGE.502]
1251 ULONG WINAPI
IStorage16_fnRelease(IStorage16
* iface
) {
1252 ICOM_THIS(IStorage16Impl
,iface
);
1260 /******************************************************************************
1261 * IStorage16_Stat [STORAGE.517]
1263 HRESULT WINAPI
IStorage16_fnStat(
1264 LPSTORAGE16 iface
,STATSTG16
*pstatstg
, DWORD grfStatFlag
1266 ICOM_THIS(IStorage16Impl
,iface
);
1267 TRACE("(%p)->(%p,0x%08lx)\n",
1268 This
,pstatstg
,grfStatFlag
1270 pstatstg
->pwcsName
=(LPOLESTR16
)SEGPTR_GET(SEGPTR_STRDUP_WtoA(This
->stde
.pps_rawname
));
1271 pstatstg
->type
= This
->stde
.pps_type
;
1272 pstatstg
->cbSize
.s
.LowPart
= This
->stde
.pps_size
;
1273 pstatstg
->mtime
= This
->stde
.pps_ft1
; /* FIXME */ /* why? */
1274 pstatstg
->atime
= This
->stde
.pps_ft2
; /* FIXME */
1275 pstatstg
->ctime
= This
->stde
.pps_ft2
; /* FIXME */
1276 pstatstg
->grfMode
= 0; /* FIXME */
1277 pstatstg
->grfLocksSupported
= 0; /* FIXME */
1278 pstatstg
->clsid
= This
->stde
.pps_guid
;
1279 pstatstg
->grfStateBits
= 0; /* FIXME */
1280 pstatstg
->reserved
= 0;
1284 /******************************************************************************
1285 * IStorage16_Commit [STORAGE.509]
1287 HRESULT WINAPI
IStorage16_fnCommit(
1288 LPSTORAGE16 iface
,DWORD commitflags
1290 ICOM_THIS(IStorage16Impl
,iface
);
1291 FIXME("(%p)->(0x%08lx),STUB!\n",
1297 /******************************************************************************
1298 * IStorage16_CopyTo [STORAGE.507]
1300 HRESULT WINAPI
IStorage16_fnCopyTo(LPSTORAGE16 iface
,DWORD ciidExclude
,const IID
*rgiidExclude
,SNB16 SNB16Exclude
,IStorage16
*pstgDest
) {
1301 ICOM_THIS(IStorage16Impl
,iface
);
1302 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1303 This
,ciidExclude
,debugstr_guid(rgiidExclude
),SNB16Exclude
,pstgDest
1309 /******************************************************************************
1310 * IStorage16_CreateStorage [STORAGE.505]
1312 HRESULT WINAPI
IStorage16_fnCreateStorage(
1313 LPSTORAGE16 iface
,LPCOLESTR16 pwcsName
,DWORD grfMode
,DWORD dwStgFormat
,DWORD reserved2
, IStorage16
**ppstg
1315 ICOM_THIS(IStorage16Impl
,iface
);
1316 IStorage16Impl
* lpstg
;
1318 struct storage_pps_entry stde
;
1319 struct storage_header sth
;
1324 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1325 This
,pwcsName
,grfMode
,dwStgFormat
,reserved2
,ppstg
1327 if (grfMode
& STGM_TRANSACTED
)
1328 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1329 _create_istorage16(ppstg
);
1330 lpstg
= (IStorage16Impl
*)PTR_SEG_TO_LIN(*ppstg
);
1331 lpstg
->hf
= This
->hf
;
1333 ppsent
=STORAGE_get_free_pps_entry(lpstg
->hf
);
1337 if (stde
.pps_dir
==-1) {
1338 stde
.pps_dir
= ppsent
;
1341 FIXME(" use prev chain too ?\n");
1343 if (1!=STORAGE_get_pps_entry(lpstg
->hf
,x
,&stde
))
1345 while (stde
.pps_next
!=-1) {
1347 if (1!=STORAGE_get_pps_entry(lpstg
->hf
,x
,&stde
))
1350 stde
.pps_next
= ppsent
;
1352 assert(STORAGE_put_pps_entry(lpstg
->hf
,x
,&stde
));
1353 assert(1==STORAGE_get_pps_entry(lpstg
->hf
,ppsent
,&(lpstg
->stde
)));
1354 lstrcpyAtoW(lpstg
->stde
.pps_rawname
,pwcsName
);
1355 lpstg
->stde
.pps_sizeofname
= strlen(pwcsName
)*2+2;
1356 lpstg
->stde
.pps_next
= -1;
1357 lpstg
->stde
.pps_prev
= -1;
1358 lpstg
->stde
.pps_dir
= -1;
1359 lpstg
->stde
.pps_sb
= -1;
1360 lpstg
->stde
.pps_size
= 0;
1361 lpstg
->stde
.pps_type
= 1;
1362 lpstg
->ppsent
= ppsent
;
1363 /* FIXME: timestamps? */
1364 if (!STORAGE_put_pps_entry(lpstg
->hf
,ppsent
,&(lpstg
->stde
)))
1369 /******************************************************************************
1370 * IStorage16_CreateStream [STORAGE.503]
1372 HRESULT WINAPI
IStorage16_fnCreateStream(
1373 LPSTORAGE16 iface
,LPCOLESTR16 pwcsName
,DWORD grfMode
,DWORD reserved1
,DWORD reserved2
, IStream16
**ppstm
1375 ICOM_THIS(IStorage16Impl
,iface
);
1376 IStream16Impl
* lpstr
;
1378 struct storage_pps_entry stde
;
1380 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1381 This
,pwcsName
,grfMode
,reserved1
,reserved2
,ppstm
1383 if (grfMode
& STGM_TRANSACTED
)
1384 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1385 _create_istream16(ppstm
);
1386 lpstr
= (IStream16Impl
*)PTR_SEG_TO_LIN(*ppstm
);
1387 DuplicateHandle( GetCurrentProcess(), This
->hf
, GetCurrentProcess(),
1388 &lpstr
->hf
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
1389 lpstr
->offset
.s
.LowPart
= 0;
1390 lpstr
->offset
.s
.HighPart
= 0;
1392 ppsent
=STORAGE_get_free_pps_entry(lpstr
->hf
);
1396 if (stde
.pps_next
==-1)
1399 while (stde
.pps_next
!=-1) {
1401 if (1!=STORAGE_get_pps_entry(lpstr
->hf
,x
,&stde
))
1404 stde
.pps_next
= ppsent
;
1405 assert(STORAGE_put_pps_entry(lpstr
->hf
,x
,&stde
));
1406 assert(1==STORAGE_get_pps_entry(lpstr
->hf
,ppsent
,&(lpstr
->stde
)));
1407 lstrcpyAtoW(lpstr
->stde
.pps_rawname
,pwcsName
);
1408 lpstr
->stde
.pps_sizeofname
= strlen(pwcsName
)*2+2;
1409 lpstr
->stde
.pps_next
= -1;
1410 lpstr
->stde
.pps_prev
= -1;
1411 lpstr
->stde
.pps_dir
= -1;
1412 lpstr
->stde
.pps_sb
= -1;
1413 lpstr
->stde
.pps_size
= 0;
1414 lpstr
->stde
.pps_type
= 2;
1415 lpstr
->ppsent
= ppsent
;
1416 /* FIXME: timestamps? */
1417 if (!STORAGE_put_pps_entry(lpstr
->hf
,ppsent
,&(lpstr
->stde
)))
1422 /******************************************************************************
1423 * IStorage16_OpenStorage [STORAGE.506]
1425 HRESULT WINAPI
IStorage16_fnOpenStorage(
1426 LPSTORAGE16 iface
,LPCOLESTR16 pwcsName
, IStorage16
*pstgPrio
, DWORD grfMode
, SNB16 snbExclude
, DWORD reserved
, IStorage16
**ppstg
1428 ICOM_THIS(IStorage16Impl
,iface
);
1429 IStream16Impl
* lpstg
;
1433 TRACE_(relay
)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1434 This
,pwcsName
,pstgPrio
,grfMode
,snbExclude
,reserved
,ppstg
1436 if (grfMode
& STGM_TRANSACTED
)
1437 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1438 _create_istorage16(ppstg
);
1439 lpstg
= (IStream16Impl
*)PTR_SEG_TO_LIN(*ppstg
);
1440 DuplicateHandle( GetCurrentProcess(), This
->hf
, GetCurrentProcess(),
1441 &lpstg
->hf
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
1442 lstrcpyAtoW(name
,pwcsName
);
1443 newpps
= STORAGE_look_for_named_pps(lpstg
->hf
,This
->stde
.pps_dir
,name
);
1445 IStream16_fnRelease((IStream16
*)lpstg
);
1449 if (1!=STORAGE_get_pps_entry(lpstg
->hf
,newpps
,&(lpstg
->stde
))) {
1450 IStream16_fnRelease((IStream16
*)lpstg
);
1453 lpstg
->ppsent
= newpps
;
1457 /******************************************************************************
1458 * IStorage16_OpenStream [STORAGE.504]
1460 HRESULT WINAPI
IStorage16_fnOpenStream(
1461 LPSTORAGE16 iface
,LPCOLESTR16 pwcsName
, void *reserved1
, DWORD grfMode
, DWORD reserved2
, IStream16
**ppstm
1463 ICOM_THIS(IStorage16Impl
,iface
);
1464 IStream16Impl
* lpstr
;
1468 TRACE_(relay
)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1469 This
,pwcsName
,reserved1
,grfMode
,reserved2
,ppstm
1471 if (grfMode
& STGM_TRANSACTED
)
1472 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1473 _create_istream16(ppstm
);
1474 lpstr
= (IStream16Impl
*)PTR_SEG_TO_LIN(*ppstm
);
1475 DuplicateHandle( GetCurrentProcess(), This
->hf
, GetCurrentProcess(),
1476 &lpstr
->hf
, 0, TRUE
, DUPLICATE_SAME_ACCESS
);
1477 lstrcpyAtoW(name
,pwcsName
);
1478 newpps
= STORAGE_look_for_named_pps(lpstr
->hf
,This
->stde
.pps_dir
,name
);
1480 IStream16_fnRelease((IStream16
*)lpstr
);
1484 if (1!=STORAGE_get_pps_entry(lpstr
->hf
,newpps
,&(lpstr
->stde
))) {
1485 IStream16_fnRelease((IStream16
*)lpstr
);
1488 lpstr
->offset
.s
.LowPart
= 0;
1489 lpstr
->offset
.s
.HighPart
= 0;
1490 lpstr
->ppsent
= newpps
;
1494 /******************************************************************************
1495 * _create_istorage16 [INTERNAL]
1497 static void _create_istorage16(LPSTORAGE16
*stg
) {
1498 IStorage16Impl
* lpst
;
1500 if (!stvt16
.fnQueryInterface
) {
1501 HMODULE16 wp
= GetModuleHandle16("STORAGE");
1503 #define VTENT(xfn) stvt16.fn##xfn = (void*)WIN32_GetProcAddress16(wp,"IStorage16_"#xfn);
1504 VTENT(QueryInterface
)
1509 VTENT(CreateStorage
)
1512 VTENT(MoveElementTo
)
1516 VTENT(DestroyElement
)
1517 VTENT(RenameElement
)
1518 VTENT(SetElementTimes
)
1523 segstvt16
= SEGPTR_NEW(ICOM_VTABLE(IStorage16
));
1524 memcpy(segstvt16
,&stvt16
,sizeof(stvt16
));
1525 segstvt16
= (ICOM_VTABLE(IStorage16
)*)SEGPTR_GET(segstvt16
);
1527 #define VTENT(xfn) stvt16.fn##xfn = IStorage16_fn##xfn;
1528 VTENT(QueryInterface
)
1533 VTENT(CreateStorage
)
1537 /* not (yet) implemented ...
1538 VTENT(MoveElementTo)
1541 VTENT(DestroyElement)
1542 VTENT(RenameElement)
1543 VTENT(SetElementTimes)
1549 segstvt16
= &stvt16
;
1552 lpst
= SEGPTR_NEW(IStorage16Impl
);
1553 ICOM_VTBL(lpst
) = segstvt16
;
1555 lpst
->thisptr
= SEGPTR_GET(lpst
);
1556 *stg
= (void*)lpst
->thisptr
;
1559 /******************************************************************************
1560 * Storage API functions
1563 /******************************************************************************
1564 * StgCreateDocFile16 [STORAGE.1]
1566 HRESULT WINAPI
StgCreateDocFile16(
1567 LPCOLESTR16 pwcsName
,DWORD grfMode
,DWORD reserved
,IStorage16
**ppstgOpen
1571 IStorage16Impl
* lpstg
;
1572 struct storage_pps_entry stde
;
1574 TRACE("(%s,0x%08lx,0x%08lx,%p)\n",
1575 pwcsName
,grfMode
,reserved
,ppstgOpen
1577 _create_istorage16(ppstgOpen
);
1578 hf
= CreateFileA(pwcsName
,GENERIC_READ
|GENERIC_WRITE
,0,NULL
,CREATE_NEW
,0,0);
1579 if (hf
==INVALID_HANDLE_VALUE
) {
1580 WARN("couldn't open file for storage:%ld\n",GetLastError());
1583 lpstg
= (IStorage16Impl
*)PTR_SEG_TO_LIN(*ppstgOpen
);
1585 /* FIXME: check for existance before overwriting? */
1586 if (!STORAGE_init_storage(hf
)) {
1591 while (!ret
) { /* neither 1 nor <0 */
1592 ret
=STORAGE_get_pps_entry(hf
,i
,&stde
);
1593 if ((ret
==1) && (stde
.pps_type
==5)) {
1601 IStorage16_fnRelease((IStorage16
*)lpstg
); /* will remove it */
1608 /******************************************************************************
1609 * StgIsStorageFile16 [STORAGE.5]
1611 HRESULT WINAPI
StgIsStorageFile16(LPCOLESTR16 fn
) {
1616 TRACE("(\'%s\')\n",fn
);
1617 hf
= OpenFile(fn
,&ofs
,OF_SHARE_DENY_NONE
);
1618 if (hf
==HFILE_ERROR
)
1619 return STG_E_FILENOTFOUND
;
1620 if (24!=_lread(hf
,magic
,24)) {
1621 WARN(" too short\n");
1625 if (!memcmp(magic
,STORAGE_magic
,8)) {
1630 if (!memcmp(magic
,STORAGE_notmagic
,8)) {
1635 if (!memcmp(magic
,STORAGE_oldmagic
,8)) {
1636 WARN(" -> old format\n");
1638 return STG_E_OLDFORMAT
;
1640 WARN(" -> Invalid header.\n");
1642 return STG_E_INVALIDHEADER
;
1645 /******************************************************************************
1646 * StgIsStorageFile [OLE32.146]
1649 StgIsStorageFile(LPCOLESTR fn
)
1651 LPOLESTR16 xfn
= HEAP_strdupWtoA(GetProcessHeap(),0,fn
);
1652 HRESULT ret
= StgIsStorageFile16(xfn
);
1654 HeapFree(GetProcessHeap(),0,xfn
);
1659 /******************************************************************************
1660 * StgOpenStorage16 [STORAGE.3]
1662 HRESULT WINAPI
StgOpenStorage16(
1663 LPCOLESTR16 pwcsName
,IStorage16
*pstgPriority
,DWORD grfMode
,
1664 SNB16 snbExclude
,DWORD reserved
, IStorage16
**ppstgOpen
1668 IStorage16Impl
* lpstg
;
1669 struct storage_pps_entry stde
;
1671 TRACE("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1672 pwcsName
,pstgPriority
,grfMode
,snbExclude
,reserved
,ppstgOpen
1674 _create_istorage16(ppstgOpen
);
1675 hf
= CreateFileA(pwcsName
,GENERIC_READ
,FILE_SHARE_READ
,NULL
,OPEN_EXISTING
,FILE_ATTRIBUTE_NORMAL
,0);
1676 if (hf
==INVALID_HANDLE_VALUE
) {
1677 WARN("Couldn't open file for storage\n");
1680 lpstg
= (IStorage16Impl
*)PTR_SEG_TO_LIN(*ppstgOpen
);
1684 while (!ret
) { /* neither 1 nor <0 */
1685 ret
=STORAGE_get_pps_entry(hf
,i
,&stde
);
1686 if ((ret
==1) && (stde
.pps_type
==5)) {
1693 IStorage16_fnRelease((IStorage16
*)lpstg
); /* will remove it */