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/winbase16.h"
21 #include "wine/obj_base.h"
22 #include "wine/obj_storage.h"
24 #include "debugtools.h"
26 DEFAULT_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(" seek failed (%ld)\n",GetLastError());
95 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
96 if (BIGSIZE!=_lread(hf,block,BIGSIZE)) {
97 WARN("(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(" seek failed (%ld)\n",GetLastError());
114 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
115 if (BIGSIZE!=_lwrite(hf,block,BIGSIZE)) {
116 WARN(" 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) {
361 WideCharToMultiByte( CP_ACP, 0, stde->pps_rawname, -1, name, sizeof(name), NULL, NULL);
362 if (!stde->pps_sizeofname)
364 DPRINTF("name: %s\n",name);
365 DPRINTF("type: %d\n",stde->pps_type);
366 DPRINTF("prev pps: %ld\n",stde->pps_prev);
367 DPRINTF("next pps: %ld\n",stde->pps_next);
368 DPRINTF("dir pps: %ld\n",stde->pps_dir);
369 DPRINTF("guid: %s\n",debugstr_guid(&(stde->pps_guid)));
370 if (stde->pps_type !=2) {
373 RtlTimeToSecondsSince1970(&(stde->pps_ft1),&dw);
375 DPRINTF("ts1: %s\n",ctime(&t));
376 RtlTimeToSecondsSince1970(&(stde->pps_ft2),&dw);
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 MultiByteToWideChar( CP_ACP, 0, "RootEntry", -1, stde->pps_rawname,
417 sizeof(stde->pps_rawname)/sizeof(WCHAR));
418 stde->pps_sizeofname = (strlenW(stde->pps_rawname)+1) * sizeof(WCHAR);
423 stde->pps_sb = 0xffffffff;
425 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
429 /******************************************************************************
430 * STORAGE_set_big_chain [Internal]
433 STORAGE_set_big_chain(HFILE hf,int blocknr,INT type) {
435 LPINT bbd = (LPINT)block;
436 int nextblocknr,bigblocknr;
437 struct storage_header sth;
440 assert(blocknr!=type);
442 bigblocknr = sth.bbd_list[blocknr/128];
443 assert(bigblocknr>=0);
444 assert(STORAGE_get_big_block(hf,bigblocknr,block));
446 nextblocknr = bbd[blocknr&(128-1)];
447 bbd[blocknr&(128-1)] = type;
450 assert(STORAGE_put_big_block(hf,bigblocknr,block));
451 type = STORAGE_CHAINENTRY_FREE;
452 blocknr = nextblocknr;
457 /******************************************************************************
458 * STORAGE_set_small_chain [Internal]
461 STORAGE_set_small_chain(HFILE hf,int blocknr,INT type) {
463 LPINT sbd = (LPINT)block;
464 int lastblocknr,nextsmallblocknr,bigblocknr;
465 struct storage_header sth;
469 assert(blocknr!=type);
470 lastblocknr=-129;bigblocknr=-2;
472 /* cache block ... */
473 if (lastblocknr/128!=blocknr/128) {
474 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
475 assert(bigblocknr>=0);
476 assert(STORAGE_get_big_block(hf,bigblocknr,block));
478 lastblocknr = blocknr;
479 nextsmallblocknr = sbd[blocknr&(128-1)];
480 sbd[blocknr&(128-1)] = type;
481 assert(STORAGE_put_big_block(hf,bigblocknr,block));
484 type = STORAGE_CHAINENTRY_FREE;
485 blocknr = nextsmallblocknr;
490 /******************************************************************************
491 * STORAGE_get_free_big_blocknr [Internal]
494 STORAGE_get_free_big_blocknr(HFILE hf) {
496 LPINT sbd = (LPINT)block;
497 int lastbigblocknr,i,curblock,bigblocknr;
498 struct storage_header sth;
503 bigblocknr = sth.bbd_list[curblock];
504 while (curblock<sth.num_of_bbd_blocks) {
505 assert(bigblocknr>=0);
506 assert(STORAGE_get_big_block(hf,bigblocknr,block));
508 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
509 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
510 assert(STORAGE_put_big_block(hf,bigblocknr,block));
511 memset(block,0x42,sizeof(block));
512 assert(STORAGE_put_big_block(hf,i+curblock*128,block));
513 return i+curblock*128;
515 lastbigblocknr = bigblocknr;
516 bigblocknr = sth.bbd_list[++curblock];
518 bigblocknr = curblock*128;
519 /* since we have marked all blocks from 0 up to curblock*128-1
520 * the next free one is curblock*128, where we happily put our
521 * next large block depot.
523 memset(block,0xff,sizeof(block));
524 /* mark the block allocated and returned by this function */
525 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
526 assert(STORAGE_put_big_block(hf,bigblocknr,block));
528 /* if we had a bbd block already (mostlikely) we need
529 * to link the new one into the chain
531 if (lastbigblocknr!=-1)
532 assert(STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr));
533 sth.bbd_list[curblock]=bigblocknr;
534 sth.num_of_bbd_blocks++;
535 assert(sth.num_of_bbd_blocks==curblock+1);
536 assert(STORAGE_put_big_block(hf,-1,(LPBYTE)&sth));
538 /* Set the end of the chain for the bigblockdepots */
539 assert(STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN));
540 /* add 1, for the first entry is used for the additional big block
541 * depot. (means we already used bigblocknr) */
542 memset(block,0x42,sizeof(block));
543 /* allocate this block (filled with 0x42) */
544 assert(STORAGE_put_big_block(hf,bigblocknr+1,block));
549 /******************************************************************************
550 * STORAGE_get_free_small_blocknr [Internal]
553 STORAGE_get_free_small_blocknr(HFILE hf) {
555 LPINT sbd = (LPINT)block;
556 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
557 struct storage_pps_entry root;
558 struct storage_header sth;
561 bigblocknr = sth.sbd_startblock;
565 while (bigblocknr>=0) {
566 if (!STORAGE_get_big_block(hf,bigblocknr,block))
569 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
570 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
571 newblocknr = i+curblock*128;
576 lastbigblocknr = bigblocknr;
577 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
580 if (newblocknr==-1) {
581 bigblocknr = STORAGE_get_free_big_blocknr(hf);
585 memset(block,0xff,sizeof(block));
586 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
587 if (!STORAGE_put_big_block(hf,bigblocknr,block))
589 if (lastbigblocknr==-1) {
590 sth.sbd_startblock = bigblocknr;
591 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
594 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
597 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
599 newblocknr = curblock*128;
601 /* allocate enough big blocks for storing the allocated small block */
602 if (!STORAGE_get_root_pps_entry(hf,&root))
607 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
608 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
609 /* we need to allocate more stuff */
610 bigblocknr = STORAGE_get_free_big_blocknr(hf);
614 if (root.pps_sb==-1) {
615 root.pps_sb = bigblocknr;
616 root.pps_size += BIGSIZE;
618 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
620 root.pps_size += BIGSIZE;
622 lastbigblocknr = bigblocknr;
624 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
626 if (!STORAGE_put_pps_entry(hf,0,&root))
631 /******************************************************************************
632 * STORAGE_get_free_pps_entry [Internal]
635 STORAGE_get_free_pps_entry(HFILE hf) {
636 int blocknr,i,curblock,lastblocknr;
638 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
639 struct storage_header sth;
642 blocknr = sth.root_startblock;
646 if (!STORAGE_get_big_block(hf,blocknr,block))
649 if (stde[i].pps_sizeofname==0) /* free */
651 lastblocknr = blocknr;
652 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
655 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
656 blocknr = STORAGE_get_free_big_blocknr(hf);
657 /* sth invalidated */
661 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
663 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
665 memset(block,0,sizeof(block));
666 STORAGE_put_big_block(hf,blocknr,block);
670 /* --- IStream16 implementation */
674 /* IUnknown fields */
675 ICOM_VFIELD(IStream16);
677 /* IStream16 fields */
678 SEGPTR thisptr; /* pointer to this struct as segmented */
679 struct storage_pps_entry stde;
682 ULARGE_INTEGER offset;
685 /******************************************************************************
686 * IStream16_QueryInterface [STORAGE.518]
688 HRESULT WINAPI IStream16_fnQueryInterface(
689 IStream16* iface,REFIID refiid,LPVOID *obj
691 ICOM_THIS(IStream16Impl,iface);
692 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
693 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
697 return OLE_E_ENUM_NOMORE;
701 /******************************************************************************
702 * IStream16_AddRef [STORAGE.519]
704 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
705 ICOM_THIS(IStream16Impl,iface);
706 return ++(This->ref);
709 /******************************************************************************
710 * IStream16_Release [STORAGE.520]
712 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
713 ICOM_THIS(IStream16Impl,iface);
714 FlushFileBuffers(This->hf);
717 CloseHandle(This->hf);
724 /******************************************************************************
725 * IStream16_Seek [STORAGE.523]
728 * Does not handle 64 bits
730 HRESULT WINAPI IStream16_fnSeek(
731 IStream16* iface,LARGE_INTEGER offset,DWORD whence,ULARGE_INTEGER *newpos
733 ICOM_THIS(IStream16Impl,iface);
734 TRACE_(relay)("(%p)->([%ld.%ld],%ld,%p)\n",This,offset.s.HighPart,offset.s.LowPart,whence,newpos);
737 /* unix SEEK_xx should be the same as win95 ones */
739 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
742 assert(offset.s.HighPart==0);
743 This->offset.s.HighPart = offset.s.HighPart;
744 This->offset.s.LowPart = offset.s.LowPart;
747 if (offset.s.HighPart < 0) {
748 /* FIXME: is this negation correct ? */
749 offset.s.HighPart = -offset.s.HighPart;
750 offset.s.LowPart = (0xffffffff ^ offset.s.LowPart)+1;
752 assert(offset.s.HighPart==0);
753 assert(This->offset.s.LowPart >= offset.s.LowPart);
754 This->offset.s.LowPart -= offset.s.LowPart;
756 assert(offset.s.HighPart==0);
757 This->offset.s.LowPart+= offset.s.LowPart;
761 assert(offset.s.HighPart==0);
762 This->offset.s.LowPart = This->stde.pps_size-offset.s.LowPart;
765 if (This->offset.s.LowPart>This->stde.pps_size)
766 This->offset.s.LowPart=This->stde.pps_size;
767 if (newpos) *newpos = This->offset;
771 /******************************************************************************
772 * IStream16_Read [STORAGE.521]
774 HRESULT WINAPI IStream16_fnRead(
775 IStream16* iface,void *pv,ULONG cb,ULONG *pcbRead
777 ICOM_THIS(IStream16Impl,iface);
779 ULONG *bytesread=pcbRead,xxread;
782 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbRead);
783 if (!pcbRead) bytesread=&xxread;
786 if (cb>This->stde.pps_size-This->offset.s.LowPart)
787 cb=This->stde.pps_size-This->offset.s.LowPart;
788 if (This->stde.pps_size < 0x1000) {
789 /* use small block reader */
790 blocknr = STORAGE_get_nth_next_small_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
794 if (!STORAGE_get_small_block(This->hf,blocknr,block)) {
795 WARN("small block read failed!!!\n");
799 if (cc>SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1)))
800 cc=SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
801 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(SMALLSIZE-1)),cc);
802 This->offset.s.LowPart+=cc;
806 blocknr = STORAGE_get_next_small_blocknr(This->hf,blocknr);
809 /* use big block reader */
810 blocknr = STORAGE_get_nth_next_big_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
814 if (!STORAGE_get_big_block(This->hf,blocknr,block)) {
815 WARN("big block read failed!!!\n");
819 if (cc>BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1)))
820 cc=BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
821 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(BIGSIZE-1)),cc);
822 This->offset.s.LowPart+=cc;
826 blocknr=STORAGE_get_next_big_blocknr(This->hf,blocknr);
832 /******************************************************************************
833 * IStream16_Write [STORAGE.522]
835 HRESULT WINAPI IStream16_fnWrite(
836 IStream16* iface,const void *pv,ULONG cb,ULONG *pcbWrite
838 ICOM_THIS(IStream16Impl,iface);
840 ULONG *byteswritten=pcbWrite,xxwritten;
841 int oldsize,newsize,i,curoffset=0,lastblocknr,blocknr,cc;
844 if (!pcbWrite) byteswritten=&xxwritten;
847 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbWrite);
848 /* do we need to junk some blocks? */
849 newsize = This->offset.s.LowPart+cb;
850 oldsize = This->stde.pps_size;
851 if (newsize < oldsize) {
852 if (oldsize < 0x1000) {
853 /* only small blocks */
854 blocknr=STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,newsize/SMALLSIZE);
858 /* will set the rest of the chain to 'free' */
859 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
862 if (newsize >= 0x1000) {
863 blocknr=STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,newsize/BIGSIZE);
866 /* will set the rest of the chain to 'free' */
867 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
870 /* Migrate large blocks to small blocks
871 * (we just migrate newsize bytes)
873 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,newsize+BIGSIZE);
875 blocknr = This->stde.pps_sb;
878 if (!STORAGE_get_big_block(hf,blocknr,curdata)) {
879 HeapFree(GetProcessHeap(),0,data);
884 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
886 /* frees complete chain for this stream */
887 if (!STORAGE_set_big_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
890 blocknr = This->stde.pps_sb = STORAGE_get_free_small_blocknr(hf);
895 if (!STORAGE_put_small_block(hf,blocknr,curdata))
899 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
903 int newblocknr = STORAGE_get_free_small_blocknr(hf);
906 if (!STORAGE_set_small_chain(hf,blocknr,newblocknr))
908 blocknr = newblocknr;
910 curdata += SMALLSIZE;
912 HeapFree(GetProcessHeap(),0,data);
915 This->stde.pps_size = newsize;
918 if (newsize > oldsize) {
919 if (oldsize >= 0x1000) {
920 /* should return the block right before the 'endofchain' */
921 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/BIGSIZE);
923 lastblocknr = blocknr;
924 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
925 blocknr = STORAGE_get_free_big_blocknr(hf);
928 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
930 lastblocknr = blocknr;
932 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
935 if (newsize < 0x1000) {
936 /* find startblock */
938 This->stde.pps_sb = blocknr = STORAGE_get_free_small_blocknr(hf);
940 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/SMALLSIZE);
944 /* allocate required new small blocks */
945 lastblocknr = blocknr;
946 for (i=oldsize/SMALLSIZE;i<newsize/SMALLSIZE;i++) {
947 blocknr = STORAGE_get_free_small_blocknr(hf);
950 if (!STORAGE_set_small_chain(hf,lastblocknr,blocknr))
952 lastblocknr = blocknr;
954 /* and terminate the chain */
955 if (!STORAGE_set_small_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
959 /* no single block allocated yet */
960 blocknr=STORAGE_get_free_big_blocknr(hf);
963 This->stde.pps_sb = blocknr;
965 /* Migrate small blocks to big blocks */
966 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,oldsize+BIGSIZE);
968 blocknr = This->stde.pps_sb;
972 if (!STORAGE_get_small_block(hf,blocknr,curdata)) {
973 HeapFree(GetProcessHeap(),0,data);
976 curdata += SMALLSIZE;
978 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
980 /* free small block chain */
981 if (!STORAGE_set_small_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
984 blocknr = This->stde.pps_sb = STORAGE_get_free_big_blocknr(hf);
987 /* put the data into the big blocks */
988 cc = This->stde.pps_size;
990 if (!STORAGE_put_big_block(hf,blocknr,curdata))
994 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
998 int newblocknr = STORAGE_get_free_big_blocknr(hf);
1001 if (!STORAGE_set_big_chain(hf,blocknr,newblocknr))
1003 blocknr = newblocknr;
1007 HeapFree(GetProcessHeap(),0,data);
1009 /* generate big blocks to fit the new data */
1010 lastblocknr = blocknr;
1011 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1012 blocknr = STORAGE_get_free_big_blocknr(hf);
1015 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1017 lastblocknr = blocknr;
1019 /* terminate chain */
1020 if (!STORAGE_set_big_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1024 This->stde.pps_size = newsize;
1027 /* There are just some cases where we didn't modify it, we write it out
1030 if (!STORAGE_put_pps_entry(hf,This->ppsent,&(This->stde)))
1033 /* finally the write pass */
1034 if (This->stde.pps_size < 0x1000) {
1035 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
1038 /* we ensured that it is allocated above */
1040 /* Read old block everytime, since we can have
1041 * overlapping data at START and END of the write
1043 if (!STORAGE_get_small_block(hf,blocknr,block))
1046 cc = SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
1049 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(SMALLSIZE-1)),
1050 (LPBYTE)((char *) pv+curoffset),
1053 if (!STORAGE_put_small_block(hf,blocknr,block))
1058 This->offset.s.LowPart += cc;
1059 *byteswritten += cc;
1060 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1063 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
1066 /* we ensured that it is allocated above, so it better is */
1068 /* read old block everytime, since we can have
1069 * overlapping data at START and END of the write
1071 if (!STORAGE_get_big_block(hf,blocknr,block))
1074 cc = BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
1077 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(BIGSIZE-1)),
1078 (LPBYTE)((char *) pv+curoffset),
1081 if (!STORAGE_put_big_block(hf,blocknr,block))
1086 This->offset.s.LowPart += cc;
1087 *byteswritten += cc;
1088 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1094 /******************************************************************************
1095 * _create_istream16 [Internal]
1097 static void _create_istream16(LPSTREAM16 *str) {
1098 IStream16Impl* lpst;
1100 if (!strvt16.QueryInterface) {
1101 HMODULE16 wp = GetModuleHandle16("STORAGE");
1103 /* FIXME: what is This GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1104 #define VTENT(xfn) strvt16.xfn = (void*)GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.xfn)
1105 VTENT(QueryInterface);
1116 VTENT(UnlockRegion);
1120 segstrvt16 = SEGPTR_NEW(ICOM_VTABLE(IStream16));
1121 memcpy(segstrvt16,&strvt16,sizeof(strvt16));
1122 segstrvt16 = (ICOM_VTABLE(IStream16)*)SEGPTR_GET(segstrvt16);
1124 #define VTENT(xfn) strvt16.xfn = IStream16_fn##xfn;
1125 VTENT(QueryInterface);
1137 VTENT(UnlockRegion);
1142 segstrvt16 = &strvt16;
1145 lpst = SEGPTR_NEW(IStream16Impl);
1146 ICOM_VTBL(lpst) = segstrvt16;
1148 lpst->thisptr = SEGPTR_GET(lpst);
1149 *str = (void*)lpst->thisptr;
1153 /* --- IStream32 implementation */
1157 /* IUnknown fields */
1158 ICOM_VFIELD(IStream);
1160 /* IStream32 fields */
1161 struct storage_pps_entry stde;
1164 ULARGE_INTEGER offset;
1167 /*****************************************************************************
1168 * IStream32_QueryInterface [VTABLE]
1170 HRESULT WINAPI IStream_fnQueryInterface(
1171 IStream* iface,REFIID refiid,LPVOID *obj
1173 ICOM_THIS(IStream32Impl,iface);
1175 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1176 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1180 return OLE_E_ENUM_NOMORE;
1184 /******************************************************************************
1185 * IStream32_AddRef [VTABLE]
1187 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1188 ICOM_THIS(IStream32Impl,iface);
1189 return ++(This->ref);
1192 /******************************************************************************
1193 * IStream32_Release [VTABLE]
1195 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1196 ICOM_THIS(IStream32Impl,iface);
1197 FlushFileBuffers(This->hf);
1200 CloseHandle(This->hf);
1207 /* --- IStorage16 implementation */
1211 /* IUnknown fields */
1212 ICOM_VFIELD(IStorage16);
1214 /* IStorage16 fields */
1215 SEGPTR thisptr; /* pointer to this struct as segmented */
1216 struct storage_pps_entry stde;
1221 /******************************************************************************
1222 * IStorage16_QueryInterface [STORAGE.500]
1224 HRESULT WINAPI IStorage16_fnQueryInterface(
1225 IStorage16* iface,REFIID refiid,LPVOID *obj
1227 ICOM_THIS(IStorage16Impl,iface);
1229 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1231 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1235 return OLE_E_ENUM_NOMORE;
1238 /******************************************************************************
1239 * IStorage16_AddRef [STORAGE.501]
1241 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1242 ICOM_THIS(IStorage16Impl,iface);
1243 return ++(This->ref);
1246 /******************************************************************************
1247 * IStorage16_Release [STORAGE.502]
1249 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1250 ICOM_THIS(IStorage16Impl,iface);
1258 /******************************************************************************
1259 * IStorage16_Stat [STORAGE.517]
1261 HRESULT WINAPI IStorage16_fnStat(
1262 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1264 ICOM_THIS(IStorage16Impl,iface);
1265 TRACE("(%p)->(%p,0x%08lx)\n",
1266 This,pstatstg,grfStatFlag
1268 pstatstg->pwcsName=(LPOLESTR16)SEGPTR_GET(SEGPTR_STRDUP_WtoA(This->stde.pps_rawname));
1269 pstatstg->type = This->stde.pps_type;
1270 pstatstg->cbSize.s.LowPart = This->stde.pps_size;
1271 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1272 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1273 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1274 pstatstg->grfMode = 0; /* FIXME */
1275 pstatstg->grfLocksSupported = 0; /* FIXME */
1276 pstatstg->clsid = This->stde.pps_guid;
1277 pstatstg->grfStateBits = 0; /* FIXME */
1278 pstatstg->reserved = 0;
1282 /******************************************************************************
1283 * IStorage16_Commit [STORAGE.509]
1285 HRESULT WINAPI IStorage16_fnCommit(
1286 LPSTORAGE16 iface,DWORD commitflags
1288 ICOM_THIS(IStorage16Impl,iface);
1289 FIXME("(%p)->(0x%08lx),STUB!\n",
1295 /******************************************************************************
1296 * IStorage16_CopyTo [STORAGE.507]
1298 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1299 ICOM_THIS(IStorage16Impl,iface);
1300 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1301 This,ciidExclude,debugstr_guid(rgiidExclude),SNB16Exclude,pstgDest
1307 /******************************************************************************
1308 * IStorage16_CreateStorage [STORAGE.505]
1310 HRESULT WINAPI IStorage16_fnCreateStorage(
1311 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1313 ICOM_THIS(IStorage16Impl,iface);
1314 IStorage16Impl* lpstg;
1316 struct storage_pps_entry stde;
1317 struct storage_header sth;
1322 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1323 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1325 if (grfMode & STGM_TRANSACTED)
1326 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1327 _create_istorage16(ppstg);
1328 lpstg = MapSL((SEGPTR)*ppstg);
1329 lpstg->hf = This->hf;
1331 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1335 if (stde.pps_dir==-1) {
1336 stde.pps_dir = ppsent;
1339 FIXME(" use prev chain too ?\n");
1341 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1343 while (stde.pps_next!=-1) {
1345 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1348 stde.pps_next = ppsent;
1350 assert(STORAGE_put_pps_entry(lpstg->hf,x,&stde));
1351 assert(1==STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)));
1352 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstg->stde.pps_rawname,
1353 sizeof(lpstg->stde.pps_rawname)/sizeof(WCHAR));
1354 lpstg->stde.pps_sizeofname = (strlenW(lpstg->stde.pps_rawname)+1)*sizeof(WCHAR);
1355 lpstg->stde.pps_next = -1;
1356 lpstg->stde.pps_prev = -1;
1357 lpstg->stde.pps_dir = -1;
1358 lpstg->stde.pps_sb = -1;
1359 lpstg->stde.pps_size = 0;
1360 lpstg->stde.pps_type = 1;
1361 lpstg->ppsent = ppsent;
1362 /* FIXME: timestamps? */
1363 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1368 /******************************************************************************
1369 * IStorage16_CreateStream [STORAGE.503]
1371 HRESULT WINAPI IStorage16_fnCreateStream(
1372 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1374 ICOM_THIS(IStorage16Impl,iface);
1375 IStream16Impl* lpstr;
1377 struct storage_pps_entry stde;
1379 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1380 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1382 if (grfMode & STGM_TRANSACTED)
1383 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1384 _create_istream16(ppstm);
1385 lpstr = MapSL((SEGPTR)*ppstm);
1386 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1387 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1388 lpstr->offset.s.LowPart = 0;
1389 lpstr->offset.s.HighPart = 0;
1391 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1395 if (stde.pps_next==-1)
1398 while (stde.pps_next!=-1) {
1400 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1403 stde.pps_next = ppsent;
1404 assert(STORAGE_put_pps_entry(lpstr->hf,x,&stde));
1405 assert(1==STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)));
1406 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstr->stde.pps_rawname,
1407 sizeof(lpstr->stde.pps_rawname)/sizeof(WCHAR));
1408 lpstr->stde.pps_sizeofname = (strlenW(lpstr->stde.pps_rawname)+1) * sizeof(WCHAR);
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 = MapSL((SEGPTR)*ppstg);
1440 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1441 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1442 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
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 = MapSL((SEGPTR)*ppstm);
1475 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1476 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1477 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
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.QueryInterface) {
1501 HMODULE16 wp = GetModuleHandle16("STORAGE");
1503 #define VTENT(xfn) stvt16.xfn = (void*)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.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 * StgCreateDocFileA [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 = MapSL((SEGPTR)*ppstgOpen);
1585 /* FIXME: check for existence 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 * StgIsStorageFile [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 * StgOpenStorage [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 = MapSL((SEGPTR)*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 */