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"
19 #include "wine/unicode.h"
21 #include "wine/obj_base.h"
22 #include "wine/obj_storage.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(ole);
26 DECLARE_DEBUG_CHANNEL(relay);
28 struct storage_header {
29 BYTE magic[8]; /* 00: magic */
30 BYTE unknown1[36]; /* 08: unknown */
31 DWORD num_of_bbd_blocks;/* 2C: length of big datablocks */
32 DWORD root_startblock;/* 30: root storage first big block */
33 DWORD unknown2[2]; /* 34: unknown */
34 DWORD sbd_startblock; /* 3C: small block depot first big block */
35 DWORD unknown3[3]; /* 40: unknown */
36 DWORD bbd_list[109]; /* 4C: big data block list (up to end of sector)*/
38 struct storage_pps_entry {
39 WCHAR pps_rawname[32];/* 00: \0 terminated widechar name */
40 WORD pps_sizeofname; /* 40: namelength in bytes */
41 BYTE pps_type; /* 42: flags, 1 storage/dir, 2 stream, 5 root */
42 BYTE pps_unknown0; /* 43: unknown */
43 DWORD pps_prev; /* 44: previous pps */
44 DWORD pps_next; /* 48: next pps */
45 DWORD pps_dir; /* 4C: directory pps */
46 GUID pps_guid; /* 50: class ID */
47 DWORD pps_unknown1; /* 60: unknown */
48 FILETIME pps_ft1; /* 64: filetime1 */
49 FILETIME pps_ft2; /* 70: filetime2 */
50 DWORD pps_sb; /* 74: data startblock */
51 DWORD pps_size; /* 78: datalength. (<0x1000)?small:big blocks*/
52 DWORD pps_unknown2; /* 7C: unknown */
55 #define STORAGE_CHAINENTRY_FAT 0xfffffffd
56 #define STORAGE_CHAINENTRY_ENDOFCHAIN 0xfffffffe
57 #define STORAGE_CHAINENTRY_FREE 0xffffffff
60 static const BYTE STORAGE_magic[8] ={0xd0,0xcf,0x11,0xe0,0xa1,0xb1,0x1a,0xe1};
61 static const BYTE STORAGE_notmagic[8]={0x0e,0x11,0xfc,0x0d,0xd0,0xcf,0x11,0xe0};
62 static const BYTE STORAGE_oldmagic[8]={0xd0,0xcf,0x11,0xe0,0x0e,0x11,0xfc,0x0d};
67 #define SMALLBLOCKS_PER_BIGBLOCK (BIGSIZE/SMALLSIZE)
69 #define READ_HEADER assert(STORAGE_get_big_block(hf,-1,(LPBYTE)&sth));assert(!memcmp(STORAGE_magic,sth.magic,sizeof(STORAGE_magic)));
70 static ICOM_VTABLE(IStorage16) stvt16;
71 static ICOM_VTABLE(IStorage16) *segstvt16 = NULL;
72 static ICOM_VTABLE(IStream16) strvt16;
73 static ICOM_VTABLE(IStream16) *segstrvt16 = NULL;
75 /*ULONG WINAPI IStorage16_AddRef(LPSTORAGE16 this);*/
76 static void _create_istorage16(LPSTORAGE16 *stg);
77 static void _create_istream16(LPSTREAM16 *str);
82 /******************************************************************************
83 * STORAGE_get_big_block [Internal]
85 * Reading OLE compound storage
88 STORAGE_get_big_block(HFILE hf,int n,BYTE *block) {
90 if (-1==_llseek(hf,(n+1)*BIGSIZE,SEEK_SET)) {
91 WARN(" seek failed (%ld)\n",GetLastError());
94 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
95 if (BIGSIZE!=_lread(hf,block,BIGSIZE)) {
96 WARN("(block size %d): read didn't read (%ld)\n",n,GetLastError());
103 /******************************************************************************
104 * STORAGE_put_big_block [INTERNAL]
107 STORAGE_put_big_block(HFILE hf,int n,BYTE *block) {
109 if (-1==_llseek(hf,(n+1)*BIGSIZE,SEEK_SET)) {
110 WARN(" seek failed (%ld)\n",GetLastError());
113 assert((n+1)*BIGSIZE==_llseek(hf,0,SEEK_CUR));
114 if (BIGSIZE!=_lwrite(hf,block,BIGSIZE)) {
115 WARN(" write failed (%ld)\n",GetLastError());
121 /******************************************************************************
122 * STORAGE_get_next_big_blocknr [INTERNAL]
125 STORAGE_get_next_big_blocknr(HFILE hf,int blocknr) {
126 INT bbs[BIGSIZE/sizeof(INT)];
127 struct storage_header sth;
131 assert(blocknr>>7<sth.num_of_bbd_blocks);
132 if (sth.bbd_list[blocknr>>7]==0xffffffff)
134 if (!STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs))
136 assert(bbs[blocknr&0x7f]!=STORAGE_CHAINENTRY_FREE);
137 return bbs[blocknr&0x7f];
140 /******************************************************************************
141 * STORAGE_get_nth_next_big_blocknr [INTERNAL]
144 STORAGE_get_nth_next_big_blocknr(HFILE hf,int blocknr,int nr) {
145 INT bbs[BIGSIZE/sizeof(INT)];
147 struct storage_header sth;
153 assert((blocknr>>7)<sth.num_of_bbd_blocks);
154 assert(sth.bbd_list[blocknr>>7]!=0xffffffff);
156 /* simple caching... */
157 if (lastblock!=sth.bbd_list[blocknr>>7]) {
158 assert(STORAGE_get_big_block(hf,sth.bbd_list[blocknr>>7],(LPBYTE)bbs));
159 lastblock = sth.bbd_list[blocknr>>7];
161 blocknr = bbs[blocknr&0x7f];
166 /******************************************************************************
167 * STORAGE_get_root_pps_entry [Internal]
170 STORAGE_get_root_pps_entry(HFILE hf,struct storage_pps_entry *pstde) {
173 struct storage_pps_entry *stde=(struct storage_pps_entry*)block;
174 struct storage_header sth;
177 blocknr = sth.root_startblock;
179 assert(STORAGE_get_big_block(hf,blocknr,block));
181 if (!stde[i].pps_sizeofname)
183 if (stde[i].pps_type==5) {
188 blocknr=STORAGE_get_next_big_blocknr(hf,blocknr);
193 /******************************************************************************
194 * STORAGE_get_small_block [INTERNAL]
197 STORAGE_get_small_block(HFILE hf,int blocknr,BYTE *sblock) {
200 struct storage_pps_entry root;
203 assert(STORAGE_get_root_pps_entry(hf,&root));
204 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
205 assert(bigblocknr>=0);
206 assert(STORAGE_get_big_block(hf,bigblocknr,block));
208 memcpy(sblock,((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),SMALLSIZE);
212 /******************************************************************************
213 * STORAGE_put_small_block [INTERNAL]
216 STORAGE_put_small_block(HFILE hf,int blocknr,BYTE *sblock) {
219 struct storage_pps_entry root;
223 assert(STORAGE_get_root_pps_entry(hf,&root));
224 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,blocknr/SMALLBLOCKS_PER_BIGBLOCK);
225 assert(bigblocknr>=0);
226 assert(STORAGE_get_big_block(hf,bigblocknr,block));
228 memcpy(((LPBYTE)block)+SMALLSIZE*(blocknr&(SMALLBLOCKS_PER_BIGBLOCK-1)),sblock,SMALLSIZE);
229 assert(STORAGE_put_big_block(hf,bigblocknr,block));
233 /******************************************************************************
234 * STORAGE_get_next_small_blocknr [INTERNAL]
237 STORAGE_get_next_small_blocknr(HFILE hf,int blocknr) {
239 LPINT sbd = (LPINT)block;
241 struct storage_header sth;
245 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
246 assert(bigblocknr>=0);
247 assert(STORAGE_get_big_block(hf,bigblocknr,block));
248 assert(sbd[blocknr & 127]!=STORAGE_CHAINENTRY_FREE);
249 return sbd[blocknr & (128-1)];
252 /******************************************************************************
253 * STORAGE_get_nth_next_small_blocknr [INTERNAL]
256 STORAGE_get_nth_next_small_blocknr(HFILE hf,int blocknr,int nr) {
259 LPINT sbd = (LPINT)block;
260 struct storage_header sth;
265 while ((nr--) && (blocknr>=0)) {
266 if (lastblocknr/128!=blocknr/128) {
268 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
269 assert(bigblocknr>=0);
270 assert(STORAGE_get_big_block(hf,bigblocknr,block));
271 lastblocknr = blocknr;
273 assert(lastblocknr>=0);
275 blocknr=sbd[blocknr & (128-1)];
276 assert(blocknr!=STORAGE_CHAINENTRY_FREE);
281 /******************************************************************************
282 * STORAGE_get_pps_entry [INTERNAL]
285 STORAGE_get_pps_entry(HFILE hf,int n,struct storage_pps_entry *pstde) {
288 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
289 struct storage_header sth;
292 /* we have 4 pps entries per big block */
293 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
295 assert(STORAGE_get_big_block(hf,blocknr,block));
301 /******************************************************************************
302 * STORAGE_put_pps_entry [Internal]
305 STORAGE_put_pps_entry(HFILE hf,int n,struct storage_pps_entry *pstde) {
308 struct storage_pps_entry *stde = (struct storage_pps_entry*)(((LPBYTE)block)+128*(n&3));
309 struct storage_header sth;
313 /* we have 4 pps entries per big block */
314 blocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.root_startblock,n/4);
316 assert(STORAGE_get_big_block(hf,blocknr,block));
318 assert(STORAGE_put_big_block(hf,blocknr,block));
322 /******************************************************************************
323 * STORAGE_look_for_named_pps [Internal]
326 STORAGE_look_for_named_pps(HFILE hf,int n,LPOLESTR name) {
327 struct storage_pps_entry stde;
332 if (1!=STORAGE_get_pps_entry(hf,n,&stde))
335 if (!lstrcmpW(name,stde.pps_rawname))
337 if (stde.pps_prev != -1) {
338 ret=STORAGE_look_for_named_pps(hf,stde.pps_prev,name);
342 if (stde.pps_next != -1) {
343 ret=STORAGE_look_for_named_pps(hf,stde.pps_next,name);
350 /******************************************************************************
351 * STORAGE_dump_pps_entry [Internal]
357 STORAGE_dump_pps_entry(struct storage_pps_entry *stde) {
360 WideCharToMultiByte( CP_ACP, 0, stde->pps_rawname, -1, name, sizeof(name), NULL, NULL);
361 if (!stde->pps_sizeofname)
363 DPRINTF("name: %s\n",name);
364 DPRINTF("type: %d\n",stde->pps_type);
365 DPRINTF("prev pps: %ld\n",stde->pps_prev);
366 DPRINTF("next pps: %ld\n",stde->pps_next);
367 DPRINTF("dir pps: %ld\n",stde->pps_dir);
368 DPRINTF("guid: %s\n",debugstr_guid(&(stde->pps_guid)));
369 if (stde->pps_type !=2) {
372 RtlTimeToSecondsSince1970(&(stde->pps_ft1),&dw);
374 DPRINTF("ts1: %s\n",ctime(&t));
375 RtlTimeToSecondsSince1970(&(stde->pps_ft2),&dw);
377 DPRINTF("ts2: %s\n",ctime(&t));
379 DPRINTF("startblock: %ld\n",stde->pps_sb);
380 DPRINTF("size: %ld\n",stde->pps_size);
383 /******************************************************************************
384 * STORAGE_init_storage [INTERNAL]
387 STORAGE_init_storage(HFILE hf) {
390 struct storage_header *sth;
391 struct storage_pps_entry *stde;
393 assert(-1!=_llseek(hf,0,SEEK_SET));
394 /* block -1 is the storage header */
395 sth = (struct storage_header*)block;
396 memcpy(sth->magic,STORAGE_magic,8);
397 memset(sth->unknown1,0,sizeof(sth->unknown1));
398 memset(sth->unknown2,0,sizeof(sth->unknown2));
399 memset(sth->unknown3,0,sizeof(sth->unknown3));
400 sth->num_of_bbd_blocks = 1;
401 sth->root_startblock = 1;
402 sth->sbd_startblock = 0xffffffff;
403 memset(sth->bbd_list,0xff,sizeof(sth->bbd_list));
404 sth->bbd_list[0] = 0;
405 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
406 /* block 0 is the big block directory */
408 memset(block,0xff,sizeof(block)); /* mark all blocks as free */
409 bbs[0]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for this block */
410 bbs[1]=STORAGE_CHAINENTRY_ENDOFCHAIN; /* for directory entry */
411 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
412 /* block 1 is the root directory entry */
413 memset(block,0x00,sizeof(block));
414 stde = (struct storage_pps_entry*)block;
415 MultiByteToWideChar( CP_ACP, 0, "RootEntry", -1, stde->pps_rawname,
416 sizeof(stde->pps_rawname)/sizeof(WCHAR));
417 stde->pps_sizeofname = (strlenW(stde->pps_rawname)+1) * sizeof(WCHAR);
422 stde->pps_sb = 0xffffffff;
424 assert(BIGSIZE==_lwrite(hf,block,BIGSIZE));
428 /******************************************************************************
429 * STORAGE_set_big_chain [Internal]
432 STORAGE_set_big_chain(HFILE hf,int blocknr,INT type) {
434 LPINT bbd = (LPINT)block;
435 int nextblocknr,bigblocknr;
436 struct storage_header sth;
439 assert(blocknr!=type);
441 bigblocknr = sth.bbd_list[blocknr/128];
442 assert(bigblocknr>=0);
443 assert(STORAGE_get_big_block(hf,bigblocknr,block));
445 nextblocknr = bbd[blocknr&(128-1)];
446 bbd[blocknr&(128-1)] = type;
449 assert(STORAGE_put_big_block(hf,bigblocknr,block));
450 type = STORAGE_CHAINENTRY_FREE;
451 blocknr = nextblocknr;
456 /******************************************************************************
457 * STORAGE_set_small_chain [Internal]
460 STORAGE_set_small_chain(HFILE hf,int blocknr,INT type) {
462 LPINT sbd = (LPINT)block;
463 int lastblocknr,nextsmallblocknr,bigblocknr;
464 struct storage_header sth;
468 assert(blocknr!=type);
469 lastblocknr=-129;bigblocknr=-2;
471 /* cache block ... */
472 if (lastblocknr/128!=blocknr/128) {
473 bigblocknr = STORAGE_get_nth_next_big_blocknr(hf,sth.sbd_startblock,blocknr/128);
474 assert(bigblocknr>=0);
475 assert(STORAGE_get_big_block(hf,bigblocknr,block));
477 lastblocknr = blocknr;
478 nextsmallblocknr = sbd[blocknr&(128-1)];
479 sbd[blocknr&(128-1)] = type;
480 assert(STORAGE_put_big_block(hf,bigblocknr,block));
483 type = STORAGE_CHAINENTRY_FREE;
484 blocknr = nextsmallblocknr;
489 /******************************************************************************
490 * STORAGE_get_free_big_blocknr [Internal]
493 STORAGE_get_free_big_blocknr(HFILE hf) {
495 LPINT sbd = (LPINT)block;
496 int lastbigblocknr,i,curblock,bigblocknr;
497 struct storage_header sth;
502 bigblocknr = sth.bbd_list[curblock];
503 while (curblock<sth.num_of_bbd_blocks) {
504 assert(bigblocknr>=0);
505 assert(STORAGE_get_big_block(hf,bigblocknr,block));
507 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
508 sbd[i] = STORAGE_CHAINENTRY_ENDOFCHAIN;
509 assert(STORAGE_put_big_block(hf,bigblocknr,block));
510 memset(block,0x42,sizeof(block));
511 assert(STORAGE_put_big_block(hf,i+curblock*128,block));
512 return i+curblock*128;
514 lastbigblocknr = bigblocknr;
515 bigblocknr = sth.bbd_list[++curblock];
517 bigblocknr = curblock*128;
518 /* since we have marked all blocks from 0 up to curblock*128-1
519 * the next free one is curblock*128, where we happily put our
520 * next large block depot.
522 memset(block,0xff,sizeof(block));
523 /* mark the block allocated and returned by this function */
524 sbd[1] = STORAGE_CHAINENTRY_ENDOFCHAIN;
525 assert(STORAGE_put_big_block(hf,bigblocknr,block));
527 /* if we had a bbd block already (mostlikely) we need
528 * to link the new one into the chain
530 if (lastbigblocknr!=-1)
531 assert(STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr));
532 sth.bbd_list[curblock]=bigblocknr;
533 sth.num_of_bbd_blocks++;
534 assert(sth.num_of_bbd_blocks==curblock+1);
535 assert(STORAGE_put_big_block(hf,-1,(LPBYTE)&sth));
537 /* Set the end of the chain for the bigblockdepots */
538 assert(STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN));
539 /* add 1, for the first entry is used for the additional big block
540 * depot. (means we already used bigblocknr) */
541 memset(block,0x42,sizeof(block));
542 /* allocate this block (filled with 0x42) */
543 assert(STORAGE_put_big_block(hf,bigblocknr+1,block));
548 /******************************************************************************
549 * STORAGE_get_free_small_blocknr [Internal]
552 STORAGE_get_free_small_blocknr(HFILE hf) {
554 LPINT sbd = (LPINT)block;
555 int lastbigblocknr,newblocknr,i,curblock,bigblocknr;
556 struct storage_pps_entry root;
557 struct storage_header sth;
560 bigblocknr = sth.sbd_startblock;
564 while (bigblocknr>=0) {
565 if (!STORAGE_get_big_block(hf,bigblocknr,block))
568 if (sbd[i]==STORAGE_CHAINENTRY_FREE) {
569 sbd[i]=STORAGE_CHAINENTRY_ENDOFCHAIN;
570 newblocknr = i+curblock*128;
575 lastbigblocknr = bigblocknr;
576 bigblocknr = STORAGE_get_next_big_blocknr(hf,bigblocknr);
579 if (newblocknr==-1) {
580 bigblocknr = STORAGE_get_free_big_blocknr(hf);
584 memset(block,0xff,sizeof(block));
585 sbd[0]=STORAGE_CHAINENTRY_ENDOFCHAIN;
586 if (!STORAGE_put_big_block(hf,bigblocknr,block))
588 if (lastbigblocknr==-1) {
589 sth.sbd_startblock = bigblocknr;
590 if (!STORAGE_put_big_block(hf,-1,(LPBYTE)&sth)) /* need to write it */
593 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
596 if (!STORAGE_set_big_chain(hf,bigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
598 newblocknr = curblock*128;
600 /* allocate enough big blocks for storing the allocated small block */
601 if (!STORAGE_get_root_pps_entry(hf,&root))
606 lastbigblocknr = STORAGE_get_nth_next_big_blocknr(hf,root.pps_sb,(root.pps_size-1)/BIGSIZE);
607 while (root.pps_size < (newblocknr*SMALLSIZE+SMALLSIZE-1)) {
608 /* we need to allocate more stuff */
609 bigblocknr = STORAGE_get_free_big_blocknr(hf);
613 if (root.pps_sb==-1) {
614 root.pps_sb = bigblocknr;
615 root.pps_size += BIGSIZE;
617 if (!STORAGE_set_big_chain(hf,lastbigblocknr,bigblocknr))
619 root.pps_size += BIGSIZE;
621 lastbigblocknr = bigblocknr;
623 if (!STORAGE_set_big_chain(hf,lastbigblocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
625 if (!STORAGE_put_pps_entry(hf,0,&root))
630 /******************************************************************************
631 * STORAGE_get_free_pps_entry [Internal]
634 STORAGE_get_free_pps_entry(HFILE hf) {
635 int blocknr,i,curblock,lastblocknr;
637 struct storage_pps_entry *stde = (struct storage_pps_entry*)block;
638 struct storage_header sth;
641 blocknr = sth.root_startblock;
645 if (!STORAGE_get_big_block(hf,blocknr,block))
648 if (stde[i].pps_sizeofname==0) /* free */
650 lastblocknr = blocknr;
651 blocknr = STORAGE_get_next_big_blocknr(hf,blocknr);
654 assert(blocknr==STORAGE_CHAINENTRY_ENDOFCHAIN);
655 blocknr = STORAGE_get_free_big_blocknr(hf);
656 /* sth invalidated */
660 if (!STORAGE_set_big_chain(hf,lastblocknr,blocknr))
662 if (!STORAGE_set_big_chain(hf,blocknr,STORAGE_CHAINENTRY_ENDOFCHAIN))
664 memset(block,0,sizeof(block));
665 STORAGE_put_big_block(hf,blocknr,block);
669 /* --- IStream16 implementation */
673 /* IUnknown fields */
674 ICOM_VFIELD(IStream16);
676 /* IStream16 fields */
677 SEGPTR thisptr; /* pointer to this struct as segmented */
678 struct storage_pps_entry stde;
681 ULARGE_INTEGER offset;
684 /******************************************************************************
685 * IStream16_QueryInterface [STORAGE.518]
687 HRESULT WINAPI IStream16_fnQueryInterface(
688 IStream16* iface,REFIID refiid,LPVOID *obj
690 ICOM_THIS(IStream16Impl,iface);
691 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
692 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
696 return OLE_E_ENUM_NOMORE;
700 /******************************************************************************
701 * IStream16_AddRef [STORAGE.519]
703 ULONG WINAPI IStream16_fnAddRef(IStream16* iface) {
704 ICOM_THIS(IStream16Impl,iface);
705 return ++(This->ref);
708 /******************************************************************************
709 * IStream16_Release [STORAGE.520]
711 ULONG WINAPI IStream16_fnRelease(IStream16* iface) {
712 ICOM_THIS(IStream16Impl,iface);
713 FlushFileBuffers(This->hf);
716 CloseHandle(This->hf);
717 UnMapLS( This->thisptr );
718 HeapFree( GetProcessHeap(), 0, This );
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 = (ICOM_VTABLE(IStream16)*)MapLS( &strvt16 );
1122 #define VTENT(xfn) strvt16.xfn = IStream16_fn##xfn;
1123 VTENT(QueryInterface);
1135 VTENT(UnlockRegion);
1140 segstrvt16 = &strvt16;
1143 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1144 ICOM_VTBL(lpst) = segstrvt16;
1146 lpst->thisptr = MapLS( lpst );
1147 *str = (void*)lpst->thisptr;
1151 /* --- IStream32 implementation */
1155 /* IUnknown fields */
1156 ICOM_VFIELD(IStream);
1158 /* IStream32 fields */
1159 struct storage_pps_entry stde;
1162 ULARGE_INTEGER offset;
1165 /*****************************************************************************
1166 * IStream32_QueryInterface [VTABLE]
1168 HRESULT WINAPI IStream_fnQueryInterface(
1169 IStream* iface,REFIID refiid,LPVOID *obj
1171 ICOM_THIS(IStream32Impl,iface);
1173 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1174 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1178 return OLE_E_ENUM_NOMORE;
1182 /******************************************************************************
1183 * IStream32_AddRef [VTABLE]
1185 ULONG WINAPI IStream_fnAddRef(IStream* iface) {
1186 ICOM_THIS(IStream32Impl,iface);
1187 return ++(This->ref);
1190 /******************************************************************************
1191 * IStream32_Release [VTABLE]
1193 ULONG WINAPI IStream_fnRelease(IStream* iface) {
1194 ICOM_THIS(IStream32Impl,iface);
1195 FlushFileBuffers(This->hf);
1198 CloseHandle(This->hf);
1199 HeapFree( GetProcessHeap(), 0, This );
1205 /* --- IStorage16 implementation */
1209 /* IUnknown fields */
1210 ICOM_VFIELD(IStorage16);
1212 /* IStorage16 fields */
1213 SEGPTR thisptr; /* pointer to this struct as segmented */
1214 struct storage_pps_entry stde;
1219 /******************************************************************************
1220 * IStorage16_QueryInterface [STORAGE.500]
1222 HRESULT WINAPI IStorage16_fnQueryInterface(
1223 IStorage16* iface,REFIID refiid,LPVOID *obj
1225 ICOM_THIS(IStorage16Impl,iface);
1227 TRACE_(relay)("(%p)->(%s,%p)\n",This,debugstr_guid(refiid),obj);
1229 if (!memcmp(&IID_IUnknown,refiid,sizeof(IID_IUnknown))) {
1233 return OLE_E_ENUM_NOMORE;
1236 /******************************************************************************
1237 * IStorage16_AddRef [STORAGE.501]
1239 ULONG WINAPI IStorage16_fnAddRef(IStorage16* iface) {
1240 ICOM_THIS(IStorage16Impl,iface);
1241 return ++(This->ref);
1244 /******************************************************************************
1245 * IStorage16_Release [STORAGE.502]
1247 ULONG WINAPI IStorage16_fnRelease(IStorage16* iface) {
1248 ICOM_THIS(IStorage16Impl,iface);
1252 UnMapLS( This->thisptr );
1253 HeapFree( GetProcessHeap(), 0, This );
1257 /******************************************************************************
1258 * IStorage16_Stat [STORAGE.517]
1260 HRESULT WINAPI IStorage16_fnStat(
1261 LPSTORAGE16 iface,STATSTG16 *pstatstg, DWORD grfStatFlag
1263 ICOM_THIS(IStorage16Impl,iface);
1264 DWORD len = WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, NULL, 0, NULL, NULL );
1265 LPSTR nameA = HeapAlloc( GetProcessHeap(), 0, len );
1267 TRACE("(%p)->(%p,0x%08lx)\n",
1268 This,pstatstg,grfStatFlag
1270 WideCharToMultiByte( CP_ACP, 0, This->stde.pps_rawname, -1, nameA, len, NULL, NULL );
1271 pstatstg->pwcsName=(LPOLESTR16)MapLS( nameA );
1272 pstatstg->type = This->stde.pps_type;
1273 pstatstg->cbSize.s.LowPart = This->stde.pps_size;
1274 pstatstg->mtime = This->stde.pps_ft1; /* FIXME */ /* why? */
1275 pstatstg->atime = This->stde.pps_ft2; /* FIXME */
1276 pstatstg->ctime = This->stde.pps_ft2; /* FIXME */
1277 pstatstg->grfMode = 0; /* FIXME */
1278 pstatstg->grfLocksSupported = 0; /* FIXME */
1279 pstatstg->clsid = This->stde.pps_guid;
1280 pstatstg->grfStateBits = 0; /* FIXME */
1281 pstatstg->reserved = 0;
1285 /******************************************************************************
1286 * IStorage16_Commit [STORAGE.509]
1288 HRESULT WINAPI IStorage16_fnCommit(
1289 LPSTORAGE16 iface,DWORD commitflags
1291 ICOM_THIS(IStorage16Impl,iface);
1292 FIXME("(%p)->(0x%08lx),STUB!\n",
1298 /******************************************************************************
1299 * IStorage16_CopyTo [STORAGE.507]
1301 HRESULT WINAPI IStorage16_fnCopyTo(LPSTORAGE16 iface,DWORD ciidExclude,const IID *rgiidExclude,SNB16 SNB16Exclude,IStorage16 *pstgDest) {
1302 ICOM_THIS(IStorage16Impl,iface);
1303 FIXME("IStorage16(%p)->(0x%08lx,%s,%p,%p),stub!\n",
1304 This,ciidExclude,debugstr_guid(rgiidExclude),SNB16Exclude,pstgDest
1310 /******************************************************************************
1311 * IStorage16_CreateStorage [STORAGE.505]
1313 HRESULT WINAPI IStorage16_fnCreateStorage(
1314 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD dwStgFormat,DWORD reserved2, IStorage16 **ppstg
1316 ICOM_THIS(IStorage16Impl,iface);
1317 IStorage16Impl* lpstg;
1319 struct storage_pps_entry stde;
1320 struct storage_header sth;
1325 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1326 This,pwcsName,grfMode,dwStgFormat,reserved2,ppstg
1328 if (grfMode & STGM_TRANSACTED)
1329 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1330 _create_istorage16(ppstg);
1331 lpstg = MapSL((SEGPTR)*ppstg);
1332 lpstg->hf = This->hf;
1334 ppsent=STORAGE_get_free_pps_entry(lpstg->hf);
1338 if (stde.pps_dir==-1) {
1339 stde.pps_dir = ppsent;
1342 FIXME(" use prev chain too ?\n");
1344 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1346 while (stde.pps_next!=-1) {
1348 if (1!=STORAGE_get_pps_entry(lpstg->hf,x,&stde))
1351 stde.pps_next = ppsent;
1353 assert(STORAGE_put_pps_entry(lpstg->hf,x,&stde));
1354 assert(1==STORAGE_get_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)));
1355 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstg->stde.pps_rawname,
1356 sizeof(lpstg->stde.pps_rawname)/sizeof(WCHAR));
1357 lpstg->stde.pps_sizeofname = (strlenW(lpstg->stde.pps_rawname)+1)*sizeof(WCHAR);
1358 lpstg->stde.pps_next = -1;
1359 lpstg->stde.pps_prev = -1;
1360 lpstg->stde.pps_dir = -1;
1361 lpstg->stde.pps_sb = -1;
1362 lpstg->stde.pps_size = 0;
1363 lpstg->stde.pps_type = 1;
1364 lpstg->ppsent = ppsent;
1365 /* FIXME: timestamps? */
1366 if (!STORAGE_put_pps_entry(lpstg->hf,ppsent,&(lpstg->stde)))
1371 /******************************************************************************
1372 * IStorage16_CreateStream [STORAGE.503]
1374 HRESULT WINAPI IStorage16_fnCreateStream(
1375 LPSTORAGE16 iface,LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved1,DWORD reserved2, IStream16 **ppstm
1377 ICOM_THIS(IStorage16Impl,iface);
1378 IStream16Impl* lpstr;
1380 struct storage_pps_entry stde;
1382 TRACE("(%p)->(%s,0x%08lx,0x%08lx,0x%08lx,%p)\n",
1383 This,pwcsName,grfMode,reserved1,reserved2,ppstm
1385 if (grfMode & STGM_TRANSACTED)
1386 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1387 _create_istream16(ppstm);
1388 lpstr = MapSL((SEGPTR)*ppstm);
1389 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1390 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1391 lpstr->offset.s.LowPart = 0;
1392 lpstr->offset.s.HighPart = 0;
1394 ppsent=STORAGE_get_free_pps_entry(lpstr->hf);
1398 if (stde.pps_next==-1)
1401 while (stde.pps_next!=-1) {
1403 if (1!=STORAGE_get_pps_entry(lpstr->hf,x,&stde))
1406 stde.pps_next = ppsent;
1407 assert(STORAGE_put_pps_entry(lpstr->hf,x,&stde));
1408 assert(1==STORAGE_get_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)));
1409 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, lpstr->stde.pps_rawname,
1410 sizeof(lpstr->stde.pps_rawname)/sizeof(WCHAR));
1411 lpstr->stde.pps_sizeofname = (strlenW(lpstr->stde.pps_rawname)+1) * sizeof(WCHAR);
1412 lpstr->stde.pps_next = -1;
1413 lpstr->stde.pps_prev = -1;
1414 lpstr->stde.pps_dir = -1;
1415 lpstr->stde.pps_sb = -1;
1416 lpstr->stde.pps_size = 0;
1417 lpstr->stde.pps_type = 2;
1418 lpstr->ppsent = ppsent;
1419 /* FIXME: timestamps? */
1420 if (!STORAGE_put_pps_entry(lpstr->hf,ppsent,&(lpstr->stde)))
1425 /******************************************************************************
1426 * IStorage16_OpenStorage [STORAGE.506]
1428 HRESULT WINAPI IStorage16_fnOpenStorage(
1429 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, IStorage16 *pstgPrio, DWORD grfMode, SNB16 snbExclude, DWORD reserved, IStorage16 **ppstg
1431 ICOM_THIS(IStorage16Impl,iface);
1432 IStream16Impl* lpstg;
1436 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,%p,0x%08lx,%p)\n",
1437 This,pwcsName,pstgPrio,grfMode,snbExclude,reserved,ppstg
1439 if (grfMode & STGM_TRANSACTED)
1440 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1441 _create_istorage16(ppstg);
1442 lpstg = MapSL((SEGPTR)*ppstg);
1443 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1444 &lpstg->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1445 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1446 newpps = STORAGE_look_for_named_pps(lpstg->hf,This->stde.pps_dir,name);
1448 IStream16_fnRelease((IStream16*)lpstg);
1452 if (1!=STORAGE_get_pps_entry(lpstg->hf,newpps,&(lpstg->stde))) {
1453 IStream16_fnRelease((IStream16*)lpstg);
1456 lpstg->ppsent = newpps;
1460 /******************************************************************************
1461 * IStorage16_OpenStream [STORAGE.504]
1463 HRESULT WINAPI IStorage16_fnOpenStream(
1464 LPSTORAGE16 iface,LPCOLESTR16 pwcsName, void *reserved1, DWORD grfMode, DWORD reserved2, IStream16 **ppstm
1466 ICOM_THIS(IStorage16Impl,iface);
1467 IStream16Impl* lpstr;
1471 TRACE_(relay)("(%p)->(%s,%p,0x%08lx,0x%08lx,%p)\n",
1472 This,pwcsName,reserved1,grfMode,reserved2,ppstm
1474 if (grfMode & STGM_TRANSACTED)
1475 FIXME("We do not support transacted Compound Storage. Using direct mode.\n");
1476 _create_istream16(ppstm);
1477 lpstr = MapSL((SEGPTR)*ppstm);
1478 DuplicateHandle( GetCurrentProcess(), This->hf, GetCurrentProcess(),
1479 &lpstr->hf, 0, TRUE, DUPLICATE_SAME_ACCESS );
1480 MultiByteToWideChar( CP_ACP, 0, pwcsName, -1, name, sizeof(name)/sizeof(WCHAR));
1481 newpps = STORAGE_look_for_named_pps(lpstr->hf,This->stde.pps_dir,name);
1483 IStream16_fnRelease((IStream16*)lpstr);
1487 if (1!=STORAGE_get_pps_entry(lpstr->hf,newpps,&(lpstr->stde))) {
1488 IStream16_fnRelease((IStream16*)lpstr);
1491 lpstr->offset.s.LowPart = 0;
1492 lpstr->offset.s.HighPart = 0;
1493 lpstr->ppsent = newpps;
1497 /******************************************************************************
1498 * _create_istorage16 [INTERNAL]
1500 static void _create_istorage16(LPSTORAGE16 *stg) {
1501 IStorage16Impl* lpst;
1503 if (!stvt16.QueryInterface) {
1504 HMODULE16 wp = GetModuleHandle16("STORAGE");
1506 #define VTENT(xfn) stvt16.xfn = (void*)GetProcAddress16(wp,"IStorage16_"#xfn);
1507 VTENT(QueryInterface)
1512 VTENT(CreateStorage)
1515 VTENT(MoveElementTo)
1519 VTENT(DestroyElement)
1520 VTENT(RenameElement)
1521 VTENT(SetElementTimes)
1526 segstvt16 = (ICOM_VTABLE(IStorage16)*)MapLS( &stvt16 );
1528 #define VTENT(xfn) stvt16.xfn = IStorage16_fn##xfn;
1529 VTENT(QueryInterface)
1534 VTENT(CreateStorage)
1538 /* not (yet) implemented ...
1539 VTENT(MoveElementTo)
1542 VTENT(DestroyElement)
1543 VTENT(RenameElement)
1544 VTENT(SetElementTimes)
1550 segstvt16 = &stvt16;
1553 lpst = HeapAlloc( GetProcessHeap(), 0, sizeof(*lpst) );
1554 ICOM_VTBL(lpst) = segstvt16;
1556 lpst->thisptr = MapLS(lpst);
1557 *stg = (void*)lpst->thisptr;
1560 /******************************************************************************
1561 * Storage API functions
1564 /******************************************************************************
1565 * StgCreateDocFileA [STORAGE.1]
1567 HRESULT WINAPI StgCreateDocFile16(
1568 LPCOLESTR16 pwcsName,DWORD grfMode,DWORD reserved,IStorage16 **ppstgOpen
1572 IStorage16Impl* lpstg;
1573 struct storage_pps_entry stde;
1575 TRACE("(%s,0x%08lx,0x%08lx,%p)\n",
1576 pwcsName,grfMode,reserved,ppstgOpen
1578 _create_istorage16(ppstgOpen);
1579 hf = CreateFileA(pwcsName,GENERIC_READ|GENERIC_WRITE,0,NULL,CREATE_NEW,0,0);
1580 if (hf==INVALID_HANDLE_VALUE) {
1581 WARN("couldn't open file for storage:%ld\n",GetLastError());
1584 lpstg = MapSL((SEGPTR)*ppstgOpen);
1586 /* FIXME: check for existence before overwriting? */
1587 if (!STORAGE_init_storage(hf)) {
1592 while (!ret) { /* neither 1 nor <0 */
1593 ret=STORAGE_get_pps_entry(hf,i,&stde);
1594 if ((ret==1) && (stde.pps_type==5)) {
1602 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */
1609 /******************************************************************************
1610 * StgIsStorageFile [STORAGE.5]
1612 HRESULT WINAPI StgIsStorageFile16(LPCOLESTR16 fn) {
1617 TRACE("(\'%s\')\n",fn);
1618 hf = OpenFile(fn,&ofs,OF_SHARE_DENY_NONE);
1619 if (hf==HFILE_ERROR)
1620 return STG_E_FILENOTFOUND;
1621 if (24!=_lread(hf,magic,24)) {
1622 WARN(" too short\n");
1626 if (!memcmp(magic,STORAGE_magic,8)) {
1631 if (!memcmp(magic,STORAGE_notmagic,8)) {
1636 if (!memcmp(magic,STORAGE_oldmagic,8)) {
1637 WARN(" -> old format\n");
1639 return STG_E_OLDFORMAT;
1641 WARN(" -> Invalid header.\n");
1643 return STG_E_INVALIDHEADER;
1646 /******************************************************************************
1647 * StgIsStorageFile [OLE32.146]
1650 StgIsStorageFile(LPCOLESTR fn)
1653 DWORD len = WideCharToMultiByte( CP_ACP, 0, fn, -1, NULL, 0, NULL, NULL );
1654 LPSTR strA = HeapAlloc( GetProcessHeap(), 0, len );
1656 WideCharToMultiByte( CP_ACP, 0, fn, -1, strA, len, NULL, NULL );
1657 ret = StgIsStorageFile16(strA);
1658 HeapFree( GetProcessHeap(), 0, strA );
1663 /******************************************************************************
1664 * StgOpenStorage [STORAGE.3]
1666 HRESULT WINAPI StgOpenStorage16(
1667 LPCOLESTR16 pwcsName,IStorage16 *pstgPriority,DWORD grfMode,
1668 SNB16 snbExclude,DWORD reserved, IStorage16 **ppstgOpen
1672 IStorage16Impl* lpstg;
1673 struct storage_pps_entry stde;
1675 TRACE("(%s,%p,0x%08lx,%p,%ld,%p)\n",
1676 pwcsName,pstgPriority,grfMode,snbExclude,reserved,ppstgOpen
1678 _create_istorage16(ppstgOpen);
1679 hf = CreateFileA(pwcsName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
1680 if (hf==INVALID_HANDLE_VALUE) {
1681 WARN("Couldn't open file for storage\n");
1684 lpstg = MapSL((SEGPTR)*ppstgOpen);
1688 while (!ret) { /* neither 1 nor <0 */
1689 ret=STORAGE_get_pps_entry(hf,i,&stde);
1690 if ((ret==1) && (stde.pps_type==5)) {
1697 IStorage16_fnRelease((IStorage16*)lpstg); /* will remove it */