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 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 lstrcpyWtoA(name,stde->pps_rawname);
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 t = DOSFS_FileTimeToUnixTime(&(stde->pps_ft1),NULL);
374 DPRINTF("ts1: %s\n",ctime(&t));
375 t = DOSFS_FileTimeToUnixTime(&(stde->pps_ft2),NULL);
376 DPRINTF("ts2: %s\n",ctime(&t));
378 DPRINTF("startblock: %ld\n",stde->pps_sb);
379 DPRINTF("size: %ld\n",stde->pps_size);
382 /******************************************************************************
383 * STORAGE_init_storage [INTERNAL]
386 STORAGE_init_storage(HFILE hf) {
389 struct storage_header *sth;
390 struct storage_pps_entry *stde;
392 assert(-1!=_llseek(hf,0,SEEK_SET));
393 /* block -1 is the storage header */
394 sth = (struct storage_header*)block;
395 memcpy(sth->magic,STORAGE_magic,8);
396 memset(sth->unknown1,0,sizeof(sth->unknown1));
397 memset(sth->unknown2,0,sizeof(sth->unknown2));
398 memset(sth->unknown3,0,sizeof(sth->unknown3));
399 sth->num_of_bbd_blocks = 1;
400 sth->root_startblock = 1;
401 sth->sbd_startblock = 0xffffffff;
402 memset(sth->bbd_list,0xff,sizeof(sth->bbd_list));
403 sth->bbd_list[0] = 0;
404 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
405 /* block 0 is the big block directory */
407 memset(block,0xff,sizeof(block)); /* mark all blocks as free */
408 bbs[0]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for this block */
409 bbs[1]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for directory entry */
410 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
411 /* block 1 is the root directory entry */
412 memset(block,0x00,sizeof(block));
413 stde = (struct storage_pps_entry*)block;
414 lstrcpyAtoW(stde->pps_rawname,"RootEntry");
415 stde->pps_sizeofname = lstrlenW(stde->pps_rawname)*2+2;
420 stde->pps_sb = 0xffffffff;
422 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
426 /******************************************************************************
427 * STORAGE_set_big_chain [Internal]
430 STORAGE_set_big_chain(HFILE hf,int blocknr,INT type) {
432 LPINT bbd = (LPINT)block;
433 int nextblocknr,bigblocknr;
434 struct storage_header sth;
437 assert(blocknr!=type);
439 bigblocknr = sth.bbd_list[blocknr/128];
440 assert(bigblocknr>=0);
441 assert(STORAGE_get_big_block(hf,bigblocknr,block));
443 nextblocknr = bbd[blocknr&(128-1)];
444 bbd[blocknr&(128-1)] = type;
447 assert(STORAGE_put_big_block(hf,bigblocknr,block));
448 type = STORAGE_CHAINENTRY_FREE;
449 blocknr = nextblocknr;
454 /******************************************************************************
455 * STORAGE_set_small_chain [Internal]
458 STORAGE_set_small_chain(HFILE hf,int blocknr,INT type) {
460 LPINT sbd = (LPINT)block;
461 int lastblocknr,nextsmallblocknr,bigblocknr;
462 struct storage_header sth;
466 assert(blocknr!=type);
467 lastblocknr=-129;bigblocknr=-2;
469 /* cache block ... */
470 if (lastblocknr/128!=blocknr/128) {
471 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
472 assert(bigblocknr>=0);
473 assert(STORAGE_get_big_block(hf,bigblocknr,block));
475 lastblocknr = blocknr;
476 nextsmallblocknr = sbd[blocknr&(128-1)];
477 sbd[blocknr&(128-1)] = type;
478 assert(STORAGE_put_big_block(hf,bigblocknr,block));
481 type = STORAGE_CHAINENTRY_FREE;
482 blocknr = nextsmallblocknr;
487 /******************************************************************************
488 * STORAGE_get_free_big_blocknr [Internal]
491 STORAGE_get_free_big_blocknr(HFILE hf) {
493 LPINT sbd = (LPINT)block;
494 int lastbigblocknr,i,curblock,bigblocknr;
495 struct storage_header sth;
500 bigblocknr = sth.bbd_list[curblock];
501 while (curblock<sth.num_of_bbd_blocks) {
502 assert(bigblocknr>=0);
503 assert(STORAGE_get_big_block(hf,bigblocknr,block));
505 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
506 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
507 assert(STORAGE_put_big_block(hf,bigblocknr,block));
508 memset(block,0x42,sizeof(block));
509 assert(STORAGE_put_big_block(hf,i+curblock*128,block));
510 return i+curblock*128;
512 lastbigblocknr = bigblocknr;
513 bigblocknr = sth.bbd_list[++curblock];
515 bigblocknr = curblock*128;
516 /* since we have marked all blocks from 0 up to curblock*128-1
517 * the next free one is curblock*128, where we happily put our
518 * next large block depot.
520 memset(block,0xff,sizeof(block));
521 /* mark the block allocated and returned by this function */
522 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
523 assert(STORAGE_put_big_block(hf,bigblocknr,block));
525 /* if we had a bbd block already (mostlikely) we need
526 * to link the new one into the chain
528 if (lastbigblocknr!=-1)
529 assert(STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr));
530 sth.bbd_list[curblock]=bigblocknr;
531 sth.num_of_bbd_blocks++;
532 assert(sth.num_of_bbd_blocks==curblock+1);
533 assert(STORAGE_put_big_block(hf,-1,(LPBYTE)&sth));
535 /* Set the end of the chain for the bigblockdepots */
536 assert(STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN));
537 /* add 1, for the first entry is used for the additional big block
538 * depot. (means we already used bigblocknr) */
539 memset(block,0x42,sizeof(block));
540 /* allocate this block (filled with 0x42) */
541 assert(STORAGE_put_big_block(hf,bigblocknr+1,block));
546 /******************************************************************************
547 * STORAGE_get_free_small_blocknr [Internal]
550 STORAGE_get_free_small_blocknr(HFILE hf) {
552 LPINT sbd = (LPINT)block;
553 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
554 struct storage_pps_entry root;
555 struct storage_header sth;
558 bigblocknr = sth.sbd_startblock;
562 while (bigblocknr>=0) {
563 if (!STORAGE_get_big_block(hf,bigblocknr,block))
566 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
567 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
568 newblocknr = i+curblock*128;
573 lastbigblocknr = bigblocknr;
574 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
577 if (newblocknr==-1) {
578 bigblocknr = STORAGE_get_free_big_blocknr(hf);
582 memset(block,0xff,sizeof(block));
583 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
584 if (!STORAGE_put_big_block(hf,bigblocknr,block))
586 if (lastbigblocknr==-1) {
587 sth.sbd_startblock = bigblocknr;
588 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
591 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
594 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
596 newblocknr = curblock*128;
598 /* allocate enough big blocks for storing the allocated small block */
599 if (!STORAGE_get_root_pps_entry(hf,&root))
604 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
605 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
606 /* we need to allocate more stuff */
607 bigblocknr = STORAGE_get_free_big_blocknr(hf);
611 if (root.pps_sb==-1) {
612 root.pps_sb = bigblocknr;
613 root.pps_size += BIGSIZE;
615 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
617 root.pps_size += BIGSIZE;
619 lastbigblocknr = bigblocknr;
621 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
623 if (!STORAGE_put_pps_entry(hf,0,&root))
628 /******************************************************************************
629 * STORAGE_get_free_pps_entry [Internal]
632 STORAGE_get_free_pps_entry(HFILE hf) {
633 int blocknr,i,curblock,lastblocknr;
635 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
636 struct storage_header sth;
639 blocknr = sth.root_startblock;
643 if (!STORAGE_get_big_block(hf,blocknr,block))
646 if (stde[i].pps_sizeofname==0) /* free */
648 lastblocknr = blocknr;
649 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
652 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
653 blocknr = STORAGE_get_free_big_blocknr(hf);
654 /* sth invalidated */
658 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
660 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
662 memset(block,0,sizeof(block));
663 STORAGE_put_big_block(hf,blocknr,block);
667 /* --- IStream16 implementation */
671 /* IUnknown fields */
672 ICOM_VFIELD(IStream16);
674 /* IStream16 fields */
675 SEGPTR thisptr; /* pointer to this struct as segmented */
676 struct storage_pps_entry stde;
679 ULARGE_INTEGER offset;
682 /******************************************************************************
683 * IStream16_QueryInterface [STORAGE.518]
685 HRESULT WINAPI IStream16_fnQueryInterface(
686 IStream16* iface,REFIID refiid,LPVOID *obj
688 ICOM_THIS(IStream16Impl,iface);
689 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
690 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
694 return OLE_E_ENUM_NOMORE;
698 /******************************************************************************
699 * IStream16_AddRef [STORAGE.519]
701 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
702 ICOM_THIS(IStream16Impl,iface);
703 return ++(This->ref);
706 /******************************************************************************
707 * IStream16_Release [STORAGE.520]
709 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
710 ICOM_THIS(IStream16Impl,iface);
711 FlushFileBuffers(This->hf);
714 CloseHandle(This->hf);
721 /******************************************************************************
722 * IStream16_Seek [STORAGE.523]
725 * Does not handle 64 bits
727 HRESULT WINAPI IStream16_fnSeek(
728 IStream16* iface,LARGE_INTEGER offset,DWORD whence,ULARGE_INTEGER *newpos
730 ICOM_THIS(IStream16Impl,iface);
731 TRACE_(relay)("(%p)->([%ld.%ld],%ld,%p)\n",This,offset.s.HighPart,offset.s.LowPart,whence,newpos);
734 /* unix SEEK_xx should be the same as win95 ones */
736 /* offset must be ==0 (<0 is invalid, and >0 cannot be handled
739 assert(offset.s.HighPart==0);
740 This->offset.s.HighPart = offset.s.HighPart;
741 This->offset.s.LowPart = offset.s.LowPart;
744 if (offset.s.HighPart < 0) {
745 /* FIXME: is this negation correct ? */
746 offset.s.HighPart = -offset.s.HighPart;
747 offset.s.LowPart = (0xffffffff ^ offset.s.LowPart)+1;
749 assert(offset.s.HighPart==0);
750 assert(This->offset.s.LowPart >= offset.s.LowPart);
751 This->offset.s.LowPart -= offset.s.LowPart;
753 assert(offset.s.HighPart==0);
754 This->offset.s.LowPart+= offset.s.LowPart;
758 assert(offset.s.HighPart==0);
759 This->offset.s.LowPart = This->stde.pps_size-offset.s.LowPart;
762 if (This->offset.s.LowPart>This->stde.pps_size)
763 This->offset.s.LowPart=This->stde.pps_size;
764 if (newpos) *newpos = This->offset;
768 /******************************************************************************
769 * IStream16_Read [STORAGE.521]
771 HRESULT WINAPI IStream16_fnRead(
772 IStream16* iface,void *pv,ULONG cb,ULONG *pcbRead
774 ICOM_THIS(IStream16Impl,iface);
776 ULONG *bytesread=pcbRead,xxread;
779 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbRead);
780 if (!pcbRead) bytesread=&xxread;
783 if (cb>This->stde.pps_size-This->offset.s.LowPart)
784 cb=This->stde.pps_size-This->offset.s.LowPart;
785 if (This->stde.pps_size < 0x1000) {
786 /* use small block reader */
787 blocknr = STORAGE_get_nth_next_small_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
791 if (!STORAGE_get_small_block(This->hf,blocknr,block)) {
792 WARN("small block read failed!!!\n");
796 if (cc>SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1)))
797 cc=SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
798 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(SMALLSIZE-1)),cc);
799 This->offset.s.LowPart+=cc;
803 blocknr = STORAGE_get_next_small_blocknr(This->hf,blocknr);
806 /* use big block reader */
807 blocknr = STORAGE_get_nth_next_big_blocknr(This->hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
811 if (!STORAGE_get_big_block(This->hf,blocknr,block)) {
812 WARN("big block read failed!!!\n");
816 if (cc>BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1)))
817 cc=BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
818 memcpy((LPBYTE)pv,block+(This->offset.s.LowPart&(BIGSIZE-1)),cc);
819 This->offset.s.LowPart+=cc;
823 blocknr=STORAGE_get_next_big_blocknr(This->hf,blocknr);
829 /******************************************************************************
830 * IStream16_Write [STORAGE.522]
832 HRESULT WINAPI IStream16_fnWrite(
833 IStream16* iface,const void *pv,ULONG cb,ULONG *pcbWrite
835 ICOM_THIS(IStream16Impl,iface);
837 ULONG *byteswritten=pcbWrite,xxwritten;
838 int oldsize,newsize,i,curoffset=0,lastblocknr,blocknr,cc;
841 if (!pcbWrite) byteswritten=&xxwritten;
844 TRACE_(relay)("(%p)->(%p,%ld,%p)\n",This,pv,cb,pcbWrite);
845 /* do we need to junk some blocks? */
846 newsize = This->offset.s.LowPart+cb;
847 oldsize = This->stde.pps_size;
848 if (newsize < oldsize) {
849 if (oldsize < 0x1000) {
850 /* only small blocks */
851 blocknr=STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,newsize/SMALLSIZE);
855 /* will set the rest of the chain to 'free' */
856 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
859 if (newsize >= 0x1000) {
860 blocknr=STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,newsize/BIGSIZE);
863 /* will set the rest of the chain to 'free' */
864 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
867 /* Migrate large blocks to small blocks
868 * (we just migrate newsize bytes)
870 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,newsize+BIGSIZE);
872 blocknr = This->stde.pps_sb;
875 if (!STORAGE_get_big_block(hf,blocknr,curdata)) {
876 HeapFree(GetProcessHeap(),0,data);
881 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
883 /* frees complete chain for this stream */
884 if (!STORAGE_set_big_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
887 blocknr = This->stde.pps_sb = STORAGE_get_free_small_blocknr(hf);
892 if (!STORAGE_put_small_block(hf,blocknr,curdata))
896 if (!STORAGE_set_small_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
900 int newblocknr = STORAGE_get_free_small_blocknr(hf);
903 if (!STORAGE_set_small_chain(hf,blocknr,newblocknr))
905 blocknr = newblocknr;
907 curdata += SMALLSIZE;
909 HeapFree(GetProcessHeap(),0,data);
912 This->stde.pps_size = newsize;
915 if (newsize > oldsize) {
916 if (oldsize >= 0x1000) {
917 /* should return the block right before the 'endofchain' */
918 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/BIGSIZE);
920 lastblocknr = blocknr;
921 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
922 blocknr = STORAGE_get_free_big_blocknr(hf);
925 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
927 lastblocknr = blocknr;
929 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
932 if (newsize < 0x1000) {
933 /* find startblock */
935 This->stde.pps_sb = blocknr = STORAGE_get_free_small_blocknr(hf);
937 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->stde.pps_size/SMALLSIZE);
941 /* allocate required new small blocks */
942 lastblocknr = blocknr;
943 for (i=oldsize/SMALLSIZE;i<newsize/SMALLSIZE;i++) {
944 blocknr = STORAGE_get_free_small_blocknr(hf);
947 if (!STORAGE_set_small_chain(hf,lastblocknr,blocknr))
949 lastblocknr = blocknr;
951 /* and terminate the chain */
952 if (!STORAGE_set_small_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
956 /* no single block allocated yet */
957 blocknr=STORAGE_get_free_big_blocknr(hf);
960 This->stde.pps_sb = blocknr;
962 /* Migrate small blocks to big blocks */
963 LPBYTE curdata,data = HeapAlloc(GetProcessHeap(),0,oldsize+BIGSIZE);
965 blocknr = This->stde.pps_sb;
969 if (!STORAGE_get_small_block(hf,blocknr,curdata)) {
970 HeapFree(GetProcessHeap(),0,data);
973 curdata += SMALLSIZE;
975 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
977 /* free small block chain */
978 if (!STORAGE_set_small_chain(hf,This->stde.pps_sb,STORAGE_CHAINENTRY_FREE))
981 blocknr = This->stde.pps_sb = STORAGE_get_free_big_blocknr(hf);
984 /* put the data into the big blocks */
985 cc = This->stde.pps_size;
987 if (!STORAGE_put_big_block(hf,blocknr,curdata))
991 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
995 int newblocknr = STORAGE_get_free_big_blocknr(hf);
998 if (!STORAGE_set_big_chain(hf,blocknr,newblocknr))
1000 blocknr = newblocknr;
1004 HeapFree(GetProcessHeap(),0,data);
1006 /* generate big blocks to fit the new data */
1007 lastblocknr = blocknr;
1008 for (i=oldsize/BIGSIZE;i<newsize/BIGSIZE;i++) {
1009 blocknr = STORAGE_get_free_big_blocknr(hf);
1012 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
1014 lastblocknr = blocknr;
1016 /* terminate chain */
1017 if (!STORAGE_set_big_chain(hf,lastblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
1021 This->stde.pps_size = newsize;
1024 /* There are just some cases where we didn't modify it, we write it out
1027 if (!STORAGE_put_pps_entry(hf,This->ppsent,&(This->stde)))
1030 /* finally the write pass */
1031 if (This->stde.pps_size < 0x1000) {
1032 blocknr = STORAGE_get_nth_next_small_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/SMALLSIZE);
1035 /* we ensured that it is allocated above */
1037 /* Read old block everytime, since we can have
1038 * overlapping data at START and END of the write
1040 if (!STORAGE_get_small_block(hf,blocknr,block))
1043 cc = SMALLSIZE-(This->offset.s.LowPart&(SMALLSIZE-1));
1046 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(SMALLSIZE-1)),
1047 (LPBYTE)((char *) pv+curoffset),
1050 if (!STORAGE_put_small_block(hf,blocknr,block))
1055 This->offset.s.LowPart += cc;
1056 *byteswritten += cc;
1057 blocknr = STORAGE_get_next_small_blocknr(hf,blocknr);
1060 blocknr = STORAGE_get_nth_next_big_blocknr(hf,This->stde.pps_sb,This->offset.s.LowPart/BIGSIZE);
1063 /* we ensured that it is allocated above, so it better is */
1065 /* read old block everytime, since we can have
1066 * overlapping data at START and END of the write
1068 if (!STORAGE_get_big_block(hf,blocknr,block))
1071 cc = BIGSIZE-(This->offset.s.LowPart&(BIGSIZE-1));
1074 memcpy( ((LPBYTE)block)+(This->offset.s.LowPart&(BIGSIZE-1)),
1075 (LPBYTE)((char *) pv+curoffset),
1078 if (!STORAGE_put_big_block(hf,blocknr,block))
1083 This->offset.s.LowPart += cc;
1084 *byteswritten += cc;
1085 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
1091 /******************************************************************************
1092 * _create_istream16 [Internal]
1094 static void _create_istream16(LPSTREAM16 *str) {
1095 IStream16Impl* lpst;
1097 if (!strvt16.fnQueryInterface) {
1098 HMODULE16 wp = GetModuleHandle16("STORAGE");
1100 /* FIXME: what is This WIN32_GetProcAddress16. Should the name be IStream16_QueryInterface of IStream16_fnQueryInterface */
1101 #define VTENT(xfn) strvt16.fn##xfn = (void*)WIN32_GetProcAddress16(wp,"IStream16_"#xfn);assert(strvt16.fn##xfn)
1102 VTENT(QueryInterface);
1113 VTENT(UnlockRegion);
1117 segstrvt16 = SEGPTR_NEW(ICOM_VTABLE(IStream16));
1118 memcpy(segstrvt16,&strvt16,sizeof(strvt16));
1119 segstrvt16 = (ICOM_VTABLE(IStream16)*)SEGPTR_GET(segstrvt16);
1121 #define VTENT(xfn) strvt16.fn##xfn = IStream16_fn##xfn;
1122 VTENT(QueryInterface);
1134 VTENT(UnlockRegion);
1139 segstrvt16 = &strvt16;
1142 lpst = SEGPTR_NEW(IStream16Impl);
1143 ICOM_VTBL(lpst) = segstrvt16;
1145 lpst->thisptr = SEGPTR_GET(lpst);
1146 *str = (void*)lpst->thisptr;
1150 /* --- IStream32 implementation */
1154 /* IUnknown fields */
1155 ICOM_VFIELD(IStream);
1157 /* IStream32 fields */
1158 struct storage_pps_entry stde;
1161 ULARGE_INTEGER offset;
1164 /*****************************************************************************
1165 * IStream32_QueryInterface [VTABLE]
1167 HRESULT WINAPI IStream_fnQueryInterface(
1168 IStream* iface,REFIID refiid,LPVOID *obj
1170 ICOM_THIS(IStream32Impl,iface);
1172 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1173 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1177 return OLE_E_ENUM_NOMORE;
1181 /******************************************************************************
1182 * IStream32_AddRef [VTABLE]
1184 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1185 ICOM_THIS(IStream32Impl,iface);
1186 return ++(This->ref);
1189 /******************************************************************************
1190 * IStream32_Release [VTABLE]
1192 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1193 ICOM_THIS(IStream32Impl,iface);
1194 FlushFileBuffers(This->hf);
1197 CloseHandle(This->hf);
1204 /* --- IStorage16 implementation */
1208 /* IUnknown fields */
1209 ICOM_VFIELD(IStorage16);
1211 /* IStorage16 fields */
1212 SEGPTR thisptr; /* pointer to this struct as segmented */
1213 struct storage_pps_entry stde;
1218 /******************************************************************************
1219 * IStorage16_QueryInterface [STORAGE.500]
1221 HRESULT WINAPI IStorage16_fnQueryInterface(
1222 IStorage16* iface,REFIID refiid,LPVOID *obj
1224 ICOM_THIS(IStorage16Impl,iface);
1226 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1228 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1232 return OLE_E_ENUM_NOMORE;
1235 /******************************************************************************
1236 * IStorage16_AddRef [STORAGE.501]
1238 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1239 ICOM_THIS(IStorage16Impl,iface);
1240 return ++(This->ref);
1243 /******************************************************************************
1244 * IStorage16_Release [STORAGE.502]
1246 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1247 ICOM_THIS(IStorage16Impl,iface);
1255 /******************************************************************************
1256 * IStorage16_Stat [STORAGE.517]
1258 HRESULT WINAPI IStorage16_fnStat(
1259 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1261 ICOM_THIS(IStorage16Impl,iface);
1262 TRACE("(%p)->(%p,0x%08lx)\n",
1263 This,pstatstg,grfStatFlag
1265 pstatstg->pwcsName=(LPOLESTR16)SEGPTR_GET(SEGPTR_STRDUP_WtoA(This->stde.pps_rawname));
1266 pstatstg->type = This->stde.pps_type;
1267 pstatstg->cbSize.s.LowPart = This->stde.pps_size;
1268 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1269 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1270 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1271 pstatstg->grfMode = 0; /* FIXME */
1272 pstatstg->grfLocksSupported = 0; /* FIXME */
1273 pstatstg->clsid = This->stde.pps_guid;
1274 pstatstg->grfStateBits = 0; /* FIXME */
1275 pstatstg->reserved = 0;
1279 /******************************************************************************
1280 * IStorage16_Commit [STORAGE.509]
1282 HRESULT WINAPI IStorage16_fnCommit(
1283 LPSTORAGE16 iface,DWORD commitflags
1285 ICOM_THIS(IStorage16Impl,iface);
1286 FIXME("(%p)->(0x%08lx),STUB!\n",
1292 /******************************************************************************
1293 * IStorage16_CopyTo [STORAGE.507]
1295 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1296 ICOM_THIS(IStorage16Impl,iface);
1297 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1298 This,ciidExclude,debugstr_guid(rgiidExclude),SNB16Exclude,pstgDest
1304 /******************************************************************************
1305 * IStorage16_CreateStorage [STORAGE.505]
1307 HRESULT WINAPI IStorage16_fnCreateStorage(
1308 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1310 ICOM_THIS(IStorage16Impl,iface);
1311 IStorage16Impl* lpstg;
1313 struct storage_pps_entry stde;
1314 struct storage_header sth;
1319 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1320 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1322 if (grfMode & STGM_TRANSACTED)
1323 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1324 _create_istorage16(ppstg);
1325 lpstg = (IStorage16Impl*)PTR_SEG_TO_LIN(*ppstg);
1326 lpstg->hf = This->hf;
1328 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1332 if (stde.pps_dir==-1) {
1333 stde.pps_dir = ppsent;
1336 FIXME(" use prev chain too ?\n");
1338 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1340 while (stde.pps_next!=-1) {
1342 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1345 stde.pps_next = ppsent;
1347 assert(STORAGE_put_pps_entry(lpstg->hf,x,&stde));
1348 assert(1==STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)));
1349 lstrcpyAtoW(lpstg->stde.pps_rawname,pwcsName);
1350 lpstg->stde.pps_sizeofname = lstrlenA(pwcsName)*2+2;
1351 lpstg->stde.pps_next = -1;
1352 lpstg->stde.pps_prev = -1;
1353 lpstg->stde.pps_dir = -1;
1354 lpstg->stde.pps_sb = -1;
1355 lpstg->stde.pps_size = 0;
1356 lpstg->stde.pps_type = 1;
1357 lpstg->ppsent = ppsent;
1358 /* FIXME: timestamps? */
1359 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1364 /******************************************************************************
1365 * IStorage16_CreateStream [STORAGE.503]
1367 HRESULT WINAPI IStorage16_fnCreateStream(
1368 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1370 ICOM_THIS(IStorage16Impl,iface);
1371 IStream16Impl* lpstr;
1373 struct storage_pps_entry stde;
1375 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1376 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1378 if (grfMode & STGM_TRANSACTED)
1379 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1380 _create_istream16(ppstm);
1381 lpstr = (IStream16Impl*)PTR_SEG_TO_LIN(*ppstm);
1382 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1383 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1384 lpstr->offset.s.LowPart = 0;
1385 lpstr->offset.s.HighPart = 0;
1387 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1391 if (stde.pps_next==-1)
1394 while (stde.pps_next!=-1) {
1396 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1399 stde.pps_next = ppsent;
1400 assert(STORAGE_put_pps_entry(lpstr->hf,x,&stde));
1401 assert(1==STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)));
1402 lstrcpyAtoW(lpstr->stde.pps_rawname,pwcsName);
1403 lpstr->stde.pps_sizeofname = lstrlenA(pwcsName)*2+2;
1404 lpstr->stde.pps_next = -1;
1405 lpstr->stde.pps_prev = -1;
1406 lpstr->stde.pps_dir = -1;
1407 lpstr->stde.pps_sb = -1;
1408 lpstr->stde.pps_size = 0;
1409 lpstr->stde.pps_type = 2;
1410 lpstr->ppsent = ppsent;
1411 /* FIXME: timestamps? */
1412 if (!STORAGE_put_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)))
1417 /******************************************************************************
1418 * IStorage16_OpenStorage [STORAGE.506]
1420 HRESULT WINAPI IStorage16_fnOpenStorage(
1421 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, IStorage16 *pstgPrio, DWORD grfMode, SNB16 snbExclude, DWORD reserved, IStorage16 **ppstg
1423 ICOM_THIS(IStorage16Impl,iface);
1424 IStream16Impl* lpstg;
1428 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1429 This,pwcsName,pstgPrio,grfMode,snbExclude,reserved,ppstg
1431 if (grfMode & STGM_TRANSACTED)
1432 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1433 _create_istorage16(ppstg);
1434 lpstg = (IStream16Impl*)PTR_SEG_TO_LIN(*ppstg);
1435 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1436 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1437 lstrcpyAtoW(name,pwcsName);
1438 newpps = STORAGE_look_for_named_pps(lpstg->hf,This->stde.pps_dir,name);
1440 IStream16_fnRelease((IStream16*)lpstg);
1444 if (1!=STORAGE_get_pps_entry(lpstg->hf,newpps,&(lpstg->stde))) {
1445 IStream16_fnRelease((IStream16*)lpstg);
1448 lpstg->ppsent = newpps;
1452 /******************************************************************************
1453 * IStorage16_OpenStream [STORAGE.504]
1455 HRESULT WINAPI IStorage16_fnOpenStream(
1456 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream16 **ppstm
1458 ICOM_THIS(IStorage16Impl,iface);
1459 IStream16Impl* lpstr;
1463 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1464 This,pwcsName,reserved1,grfMode,reserved2,ppstm
1466 if (grfMode & STGM_TRANSACTED)
1467 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1468 _create_istream16(ppstm);
1469 lpstr = (IStream16Impl*)PTR_SEG_TO_LIN(*ppstm);
1470 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1471 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1472 lstrcpyAtoW(name,pwcsName);
1473 newpps = STORAGE_look_for_named_pps(lpstr->hf,This->stde.pps_dir,name);
1475 IStream16_fnRelease((IStream16*)lpstr);
1479 if (1!=STORAGE_get_pps_entry(lpstr->hf,newpps,&(lpstr->stde))) {
1480 IStream16_fnRelease((IStream16*)lpstr);
1483 lpstr->offset.s.LowPart = 0;
1484 lpstr->offset.s.HighPart = 0;
1485 lpstr->ppsent = newpps;
1489 /******************************************************************************
1490 * _create_istorage16 [INTERNAL]
1492 static void _create_istorage16(LPSTORAGE16 *stg) {
1493 IStorage16Impl* lpst;
1495 if (!stvt16.fnQueryInterface) {
1496 HMODULE16 wp = GetModuleHandle16("STORAGE");
1498 #define VTENT(xfn) stvt16.fn##xfn = (void*)WIN32_GetProcAddress16(wp,"IStorage16_"#xfn);
1499 VTENT(QueryInterface)
1504 VTENT(CreateStorage)
1507 VTENT(MoveElementTo)
1511 VTENT(DestroyElement)
1512 VTENT(RenameElement)
1513 VTENT(SetElementTimes)
1518 segstvt16 = SEGPTR_NEW(ICOM_VTABLE(IStorage16));
1519 memcpy(segstvt16,&stvt16,sizeof(stvt16));
1520 segstvt16 = (ICOM_VTABLE(IStorage16)*)SEGPTR_GET(segstvt16);
1522 #define VTENT(xfn) stvt16.fn##xfn = IStorage16_fn##xfn;
1523 VTENT(QueryInterface)
1528 VTENT(CreateStorage)
1532 /* not (yet) implemented ...
1533 VTENT(MoveElementTo)
1536 VTENT(DestroyElement)
1537 VTENT(RenameElement)
1538 VTENT(SetElementTimes)
1544 segstvt16 = &stvt16;
1547 lpst = SEGPTR_NEW(IStorage16Impl);
1548 ICOM_VTBL(lpst) = segstvt16;
1550 lpst->thisptr = SEGPTR_GET(lpst);
1551 *stg = (void*)lpst->thisptr;
1554 /******************************************************************************
1555 * Storage API functions
1558 /******************************************************************************
1559 * StgCreateDocFile16 [STORAGE.1]
1561 HRESULT WINAPI StgCreateDocFile16(
1562 LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved,IStorage16 **ppstgOpen
1566 IStorage16Impl* lpstg;
1567 struct storage_pps_entry stde;
1569 TRACE("(%s,0x%08lx,0x%08lx,%p)\n",
1570 pwcsName,grfMode,reserved,ppstgOpen
1572 _create_istorage16(ppstgOpen);
1573 hf = CreateFileA(pwcsName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,0,0);
1574 if (hf==INVALID_HANDLE_VALUE) {
1575 WARN("couldn't open file for storage:%ld\n",GetLastError());
1578 lpstg = (IStorage16Impl*)PTR_SEG_TO_LIN(*ppstgOpen);
1580 /* FIXME: check for existance before overwriting? */
1581 if (!STORAGE_init_storage(hf)) {
1586 while (!ret) { /* neither 1 nor <0 */
1587 ret=STORAGE_get_pps_entry(hf,i,&stde);
1588 if ((ret==1) && (stde.pps_type==5)) {
1596 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1603 /******************************************************************************
1604 * StgIsStorageFile16 [STORAGE.5]
1606 HRESULT WINAPI StgIsStorageFile16(LPCOLESTR16 fn) {
1611 TRACE("(\'%s\')\n",fn);
1612 hf = OpenFile(fn,&ofs,OF_SHARE_DENY_NONE);
1613 if (hf==HFILE_ERROR)
1614 return STG_E_FILENOTFOUND;
1615 if (24!=_lread(hf,magic,24)) {
1616 WARN(" too short\n");
1620 if (!memcmp(magic,STORAGE_magic,8)) {
1625 if (!memcmp(magic,STORAGE_notmagic,8)) {
1630 if (!memcmp(magic,STORAGE_oldmagic,8)) {
1631 WARN(" -> old format\n");
1633 return STG_E_OLDFORMAT;
1635 WARN(" -> Invalid header.\n");
1637 return STG_E_INVALIDHEADER;
1640 /******************************************************************************
1641 * StgIsStorageFile32 [OLE32.146]
1644 StgIsStorageFile(LPCOLESTR fn)
1646 LPOLESTR16 xfn = HEAP_strdupWtoA(GetProcessHeap(),0,fn);
1647 OLESTATUS ret = StgIsStorageFile16(xfn);
1649 HeapFree(GetProcessHeap(),0,xfn);
1654 /******************************************************************************
1655 * StgOpenStorage16 [STORAGE.3]
1657 HRESULT WINAPI StgOpenStorage16(
1658 LPCOLESTR16 pwcsName,IStorage16 *pstgPriority,DWORD grfMode,
1659 SNB16 snbExclude,DWORD reserved, IStorage16 **ppstgOpen
1663 IStorage16Impl* lpstg;
1664 struct storage_pps_entry stde;
1666 TRACE("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1667 pwcsName,pstgPriority,grfMode,snbExclude,reserved,ppstgOpen
1669 _create_istorage16(ppstgOpen);
1670 hf = CreateFileA(pwcsName,GENERIC_READ,0,NULL,0,0,0);
1671 if (hf==INVALID_HANDLE_VALUE) {
1672 WARN("Couldn't open file for storage\n");
1675 lpstg = (IStorage16Impl*)PTR_SEG_TO_LIN(*ppstgOpen);
1679 while (!ret) { /* neither 1 nor <0 */
1680 ret=STORAGE_get_pps_entry(hf,i,&stde);
1681 if ((ret==1) && (stde.pps_type==5)) {
1688 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */