oleaut32: Implement ITypeLibComp::BindType.
[wine] / dlls / ole32 / stg_bigblockfile.c
1 /******************************************************************************
2  *
3  * BigBlockFile
4  *
5  * This is the implementation of a file that consists of blocks of
6  * a predetermined size.
7  * This class is used in the Compound File implementation of the
8  * IStorage and IStream interfaces. It provides the functionality
9  * to read and write any blocks in the file as well as setting and
10  * obtaining the size of the file.
11  * The blocks are indexed sequentially from the start of the file
12  * starting with -1.
13  *
14  * TODO:
15  * - Support for a transacted mode
16  *
17  * Copyright 1999 Thuy Nguyen
18  *
19  * This library is free software; you can redistribute it and/or
20  * modify it under the terms of the GNU Lesser General Public
21  * License as published by the Free Software Foundation; either
22  * version 2.1 of the License, or (at your option) any later version.
23  *
24  * This library is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
27  * Lesser General Public License for more details.
28  *
29  * You should have received a copy of the GNU Lesser General Public
30  * License along with this library; if not, write to the Free Software
31  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
32  */
33
34 #include <assert.h>
35 #include <stdlib.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <limits.h>
40
41 #define COBJMACROS
42 #define NONAMELESSUNION
43 #define NONAMELESSSTRUCT
44
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "winerror.h"
49 #include "objbase.h"
50 #include "ole2.h"
51
52 #include "storage32.h"
53
54 #include "wine/debug.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(storage);
57
58 /***********************************************************
59  * Data structures used internally by the BigBlockFile
60  * class.
61  */
62
63 /* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
64 #define PAGE_SIZE       131072
65
66 #define BLOCKS_PER_PAGE (PAGE_SIZE / BIG_BLOCK_SIZE)
67
68 /* We keep a list of recently-discarded pages. This controls the
69  * size of that list. */
70 #define MAX_VICTIM_PAGES 16
71
72 /* This structure provides one bit for each block in a page.
73  * Use BIGBLOCKFILE_{Test,Set,Clear}Bit to manipulate it. */
74 typedef struct
75 {
76     unsigned int bits[BLOCKS_PER_PAGE / (CHAR_BIT * sizeof(unsigned int))];
77 } BlockBits;
78
79 /***
80  * This structure identifies the paged that are mapped
81  * from the file and their position in memory. It is
82  * also used to hold a reference count to those pages.
83  *
84  * page_index identifies which PAGE_SIZE chunk from the
85  * file this mapping represents. (The mappings are always
86  * PAGE_SIZE-aligned.)
87  */
88
89 typedef struct MappedPage MappedPage;
90 struct MappedPage
91 {
92     MappedPage *next;
93     MappedPage *prev;
94
95     DWORD  page_index;
96     DWORD  mapped_bytes;
97     LPVOID lpBytes;
98     LONG   refcnt;
99
100     BlockBits readable_blocks;
101     BlockBits writable_blocks;
102 };
103
104 struct BigBlockFile
105 {
106     BOOL fileBased;
107     ULARGE_INTEGER filesize;
108     ULONG blocksize;
109     HANDLE hfile;
110     HANDLE hfilemap;
111     DWORD flProtect;
112     MappedPage *maplist;
113     MappedPage *victimhead, *victimtail;
114     ULONG num_victim_pages;
115     ILockBytes *pLkbyt;
116 };
117
118 /***********************************************************
119  * Prototypes for private methods
120  */
121
122 /* Note that this evaluates a and b multiple times, so don't
123  * pass expressions with side effects. */
124 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
125
126 /***********************************************************
127  * Blockbits functions.
128  */
129 static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb,
130                                         unsigned int index)
131 {
132     unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
133     unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
134
135     return bb->bits[array_index] & (1 << bit_index);
136 }
137
138 static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index)
139 {
140     unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
141     unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
142
143     bb->bits[array_index] |= (1 << bit_index);
144 }
145
146 static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index)
147 {
148     unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
149     unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
150
151     bb->bits[array_index] &= ~(1 << bit_index);
152 }
153
154 static inline void BIGBLOCKFILE_Zero(BlockBits *bb)
155 {
156     memset(bb->bits, 0, sizeof(bb->bits));
157 }
158
159 /******************************************************************************
160  *      BIGBLOCKFILE_FileInit
161  *
162  * Initialize a big block object supported by a file.
163  */
164 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
165 {
166   This->pLkbyt = NULL;
167   This->hfile = hFile;
168
169   if (This->hfile == INVALID_HANDLE_VALUE)
170     return FALSE;
171
172   This->filesize.u.LowPart = GetFileSize(This->hfile,
173                                          &This->filesize.u.HighPart);
174
175   if( This->filesize.u.LowPart || This->filesize.u.HighPart )
176   {
177     /* create the file mapping object
178      */
179     This->hfilemap = CreateFileMappingA(This->hfile,
180                                         NULL,
181                                         This->flProtect,
182                                         0, 0,
183                                         NULL);
184
185     if (!This->hfilemap)
186     {
187       CloseHandle(This->hfile);
188       return FALSE;
189     }
190   }
191   else
192     This->hfilemap = NULL;
193
194   This->maplist = NULL;
195
196   TRACE("file len %u\n", This->filesize.u.LowPart);
197
198   return TRUE;
199 }
200
201 /******************************************************************************
202  *      BIGBLOCKFILE_LockBytesInit
203  *
204  * Initialize a big block object supported by an ILockBytes.
205  */
206 static BOOL BIGBLOCKFILE_LockBytesInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
207 {
208     This->hfile    = 0;
209     This->hfilemap = 0;
210     This->pLkbyt   = plkbyt;
211     ILockBytes_AddRef(This->pLkbyt);
212
213     /* We'll get the size directly with ILockBytes_Stat */
214     This->filesize.QuadPart = 0;
215
216     TRACE("ILockBytes %p\n", This->pLkbyt);
217     return TRUE;
218 }
219
220 /******************************************************************************
221  *      BIGBLOCKFILE_FindPageInList      [PRIVATE]
222  *
223  */
224 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
225                                                ULONG page_index)
226 {
227     for (; head != NULL; head = head->next)
228     {
229         if (head->page_index == page_index)
230         {
231             InterlockedIncrement(&head->refcnt);
232             break;
233         }
234     }
235
236     return head;
237
238 }
239
240 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
241 {
242     if (page->next) page->next->prev = page->prev;
243     if (page->prev) page->prev->next = page->next;
244 }
245
246 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
247 {
248     if (*head) (*head)->prev = page;
249     page->next = *head;
250     page->prev = NULL;
251     *head = page;
252 }
253
254 static BOOL BIGBLOCKFILE_MapPage(BigBlockFile *This, MappedPage *page)
255 {
256     DWORD lowoffset = PAGE_SIZE * page->page_index;
257     DWORD numBytesToMap;
258     DWORD desired_access;
259
260     assert(This->fileBased);
261
262     if( !This->hfilemap )
263         return FALSE;
264
265     if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
266         numBytesToMap = This->filesize.u.LowPart - lowoffset;
267     else
268         numBytesToMap = PAGE_SIZE;
269
270     if (This->flProtect == PAGE_READONLY)
271         desired_access = FILE_MAP_READ;
272     else
273         desired_access = FILE_MAP_WRITE;
274
275     page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
276                                   lowoffset, numBytesToMap);
277     page->mapped_bytes = numBytesToMap;
278
279     TRACE("mapped page %u to %p\n", page->page_index, page->lpBytes);
280
281     return page->lpBytes != NULL;
282 }
283
284
285 static MappedPage *BIGBLOCKFILE_CreatePage(BigBlockFile *This, ULONG page_index)
286 {
287     MappedPage *page;
288
289     page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
290     if (page == NULL)
291         return NULL;
292
293     page->page_index = page_index;
294     page->refcnt = 1;
295
296     page->next = NULL;
297     page->prev = NULL;
298
299     if (!BIGBLOCKFILE_MapPage(This, page))
300     {
301         HeapFree(GetProcessHeap(),0,page);
302         return NULL;
303     }
304
305     BIGBLOCKFILE_Zero(&page->readable_blocks);
306     BIGBLOCKFILE_Zero(&page->writable_blocks);
307
308     return page;
309 }
310
311
312 /******************************************************************************
313  *      BIGBLOCKFILE_GetMappedView      [PRIVATE]
314  *
315  * Gets the page requested if it is already mapped.
316  * If it's not already mapped, this method will map it
317  */
318 static void * BIGBLOCKFILE_GetMappedView(
319   LPBIGBLOCKFILE This,
320   DWORD          page_index)
321 {
322     MappedPage *page;
323
324     page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
325     if (!page)
326     {
327         page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
328         if (page)
329         {
330             This->num_victim_pages--;
331
332             BIGBLOCKFILE_Zero(&page->readable_blocks);
333             BIGBLOCKFILE_Zero(&page->writable_blocks);
334         }
335     }
336
337     if (page)
338     {
339         /* If the page is not already at the head of the list, move
340          * it there. (Also moves pages from victim to main list.) */
341         if (This->maplist != page)
342         {
343             if (This->victimhead == page) This->victimhead = page->next;
344             if (This->victimtail == page) This->victimtail = page->prev;
345
346             BIGBLOCKFILE_UnlinkPage(page);
347
348             BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
349         }
350
351         return page;
352     }
353
354     page = BIGBLOCKFILE_CreatePage(This, page_index);
355     if (!page) return NULL;
356
357     BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
358
359     return page;
360 }
361
362 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
363 {
364     TRACE("%d at %p\n", page->page_index, page->lpBytes);
365
366     assert(This->fileBased);
367
368     if (page->refcnt > 0)
369         ERR("unmapping inuse page %p\n", page->lpBytes);
370
371     if (page->lpBytes)
372         UnmapViewOfFile(page->lpBytes);
373
374     page->lpBytes = NULL;
375 }
376
377 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
378 {
379     BIGBLOCKFILE_UnmapPage(This, page);
380
381     HeapFree(GetProcessHeap(), 0, page);
382 }
383
384 /******************************************************************************
385  *      BIGBLOCKFILE_ReleaseMappedPage      [PRIVATE]
386  *
387  * Decrements the reference count of the mapped page.
388  */
389 static void BIGBLOCKFILE_ReleaseMappedPage(
390   LPBIGBLOCKFILE This,
391   MappedPage    *page)
392 {
393     assert(This != NULL);
394     assert(page != NULL);
395
396     /* If the page is no longer refenced, move it to the victim list.
397      * If the victim list is too long, kick somebody off. */
398     if (!InterlockedDecrement(&page->refcnt))
399     {
400         if (This->maplist == page) This->maplist = page->next;
401
402         BIGBLOCKFILE_UnlinkPage(page);
403
404         if (MAX_VICTIM_PAGES > 0)
405         {
406             if (This->num_victim_pages >= MAX_VICTIM_PAGES)
407             {
408                 MappedPage *victim = This->victimtail;
409                 if (victim)
410                 {
411                     This->victimtail = victim->prev;
412                     if (This->victimhead == victim)
413                         This->victimhead = victim->next;
414
415                     BIGBLOCKFILE_UnlinkPage(victim);
416                     BIGBLOCKFILE_DeletePage(This, victim);
417                 }
418             }
419             else This->num_victim_pages++;
420
421             BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
422             if (This->victimtail == NULL) This->victimtail = page;
423         }
424         else
425             BIGBLOCKFILE_DeletePage(This, page);
426     }
427 }
428
429 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
430 {
431     while (list != NULL)
432     {
433         MappedPage *next = list->next;
434
435         BIGBLOCKFILE_DeletePage(This, list);
436
437         list = next;
438     }
439 }
440
441 /******************************************************************************
442  *      BIGBLOCKFILE_FreeAllMappedPages     [PRIVATE]
443  *
444  * Unmap all currently mapped pages.
445  * Empty mapped pages list.
446  */
447 static void BIGBLOCKFILE_FreeAllMappedPages(
448   LPBIGBLOCKFILE This)
449 {
450     BIGBLOCKFILE_DeleteList(This, This->maplist);
451     BIGBLOCKFILE_DeleteList(This, This->victimhead);
452
453     This->maplist = NULL;
454     This->victimhead = NULL;
455     This->victimtail = NULL;
456     This->num_victim_pages = 0;
457 }
458
459 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
460 {
461     for (; list != NULL; list = list->next)
462     {
463         BIGBLOCKFILE_UnmapPage(This, list);
464     }
465 }
466
467 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
468 {
469     BIGBLOCKFILE_UnmapList(This, This->maplist);
470     BIGBLOCKFILE_UnmapList(This, This->victimhead);
471 }
472
473 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
474 {
475     while (list != NULL)
476     {
477         MappedPage *next = list->next;
478
479         if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
480         {
481             TRACE("discarding %u\n", list->page_index);
482
483             /* page is entirely outside of the file, delete it */
484             BIGBLOCKFILE_UnlinkPage(list);
485             BIGBLOCKFILE_DeletePage(This, list);
486         }
487         else
488         {
489             /* otherwise, remap it */
490             BIGBLOCKFILE_MapPage(This, list);
491         }
492
493         list = next;
494     }
495 }
496
497 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
498 {
499     BIGBLOCKFILE_RemapList(This, This->maplist);
500     BIGBLOCKFILE_RemapList(This, This->victimhead);
501 }
502
503 /****************************************************************************
504  *      BIGBLOCKFILE_GetProtectMode
505  *
506  * This function will return a protection mode flag for a file-mapping object
507  * from the open flags of a file.
508  */
509 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
510 {
511     switch(STGM_ACCESS_MODE(openFlags))
512     {
513     case STGM_WRITE:
514     case STGM_READWRITE:
515         return PAGE_READWRITE;
516     }
517     return PAGE_READONLY;
518 }
519
520
521 /* ILockByte Interfaces */
522
523 /******************************************************************************
524  * This method is part of the ILockBytes interface.
525  *
526  * It reads a block of information from the byte array at the specified
527  * offset.
528  *
529  * See the documentation of ILockBytes for more info.
530  */
531 static HRESULT ImplBIGBLOCKFILE_ReadAt(
532       BigBlockFile* const This,
533       ULARGE_INTEGER ulOffset,  /* [in] */
534       void*          pv,        /* [length_is][size_is][out] */
535       ULONG          cb,        /* [in] */
536       ULONG*         pcbRead)   /* [out] */
537 {
538     ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
539     ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
540     ULONG bytes_left = cb;
541     ULONG page_index = first_page;
542     ULONG bytes_from_page;
543     LPVOID writePtr = pv;
544
545     HRESULT rc = S_OK;
546
547     TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbRead);
548
549     /* verify a sane environment */
550     if (!This) return E_FAIL;
551
552     if (offset_in_page + bytes_left > PAGE_SIZE)
553         bytes_from_page = PAGE_SIZE - offset_in_page;
554     else
555         bytes_from_page = bytes_left;
556
557     if (pcbRead)
558         *pcbRead = 0;
559
560     while (bytes_left)
561     {
562         LPBYTE readPtr;
563         BOOL eof = FALSE;
564         MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
565
566         if (!page || !page->lpBytes)
567         {
568             rc = STG_E_READFAULT;
569             break;
570         }
571
572         TRACE("page %i,  offset %u, bytes_from_page %u, bytes_left %u\n",
573             page->page_index, offset_in_page, bytes_from_page, bytes_left);
574
575         if (page->mapped_bytes < bytes_from_page)
576         {
577             eof = TRUE;
578             bytes_from_page = page->mapped_bytes;
579         }
580
581         readPtr = (BYTE*)page->lpBytes + offset_in_page;
582         memcpy(writePtr,readPtr,bytes_from_page);
583         BIGBLOCKFILE_ReleaseMappedPage(This, page);
584
585         if (pcbRead)
586             *pcbRead += bytes_from_page;
587         bytes_left -= bytes_from_page;
588
589         if (bytes_left && !eof)
590         {
591             writePtr = (LPBYTE)writePtr + bytes_from_page;
592             page_index ++;
593             offset_in_page = 0;
594             if (bytes_left > PAGE_SIZE)
595                 bytes_from_page = PAGE_SIZE;
596             else
597                 bytes_from_page = bytes_left;
598         }
599         if (eof)
600         {
601             rc = STG_E_READFAULT;
602             break;
603         }
604     }
605
606     TRACE("finished\n");
607     return rc;
608 }
609
610 /******************************************************************************
611  * This method is part of the ILockBytes interface.
612  *
613  * It writes the specified bytes at the specified offset.
614  * position. If the file is too small, it will be resized.
615  *
616  * See the documentation of ILockBytes for more info.
617  */
618 static HRESULT ImplBIGBLOCKFILE_WriteAt(
619       BigBlockFile* const This,
620       ULARGE_INTEGER ulOffset,    /* [in] */
621       const void*    pv,          /* [size_is][in] */
622       ULONG          cb,          /* [in] */
623       ULONG*         pcbWritten)  /* [out] */
624 {
625     ULONG size_needed = ulOffset.u.LowPart + cb;
626     ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
627     ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
628     ULONG bytes_left = cb;
629     ULONG page_index = first_page;
630     ULONG bytes_to_page;
631     LPCVOID readPtr = pv;
632
633     HRESULT rc = S_OK;
634
635     TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbWritten);
636
637     /* verify a sane environment */
638     if (!This) return E_FAIL;
639
640     if (This->flProtect != PAGE_READWRITE)
641         return STG_E_ACCESSDENIED;
642
643     if (size_needed > This->filesize.u.LowPart)
644     {
645         ULARGE_INTEGER newSize;
646         newSize.u.HighPart = 0;
647         newSize.u.LowPart = size_needed;
648         BIGBLOCKFILE_SetSize(This, newSize);
649     }
650
651     if (offset_in_page + bytes_left > PAGE_SIZE)
652         bytes_to_page = PAGE_SIZE - offset_in_page;
653     else
654         bytes_to_page = bytes_left;
655
656     if (pcbWritten)
657         *pcbWritten = 0;
658
659     while (bytes_left)
660     {
661         LPBYTE writePtr;
662         MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
663
664         TRACE("page %i,  offset %u, bytes_to_page %u, bytes_left %u\n",
665             page ? page->page_index : 0, offset_in_page, bytes_to_page, bytes_left);
666
667         if (!page)
668         {
669             ERR("Unable to get a page to write. This should never happen\n");
670             rc = E_FAIL;
671             break;
672         }
673
674         if (page->mapped_bytes < bytes_to_page)
675         {
676             ERR("Not enough bytes mapped to the page. This should never happen\n");
677             rc = E_FAIL;
678             break;
679         }
680
681         writePtr = (BYTE*)page->lpBytes + offset_in_page;
682         memcpy(writePtr,readPtr,bytes_to_page);
683         BIGBLOCKFILE_ReleaseMappedPage(This, page);
684
685         if (pcbWritten)
686             *pcbWritten += bytes_to_page;
687         bytes_left -= bytes_to_page;
688
689         if (bytes_left)
690         {
691             readPtr = (const BYTE *)readPtr + bytes_to_page;
692             page_index ++;
693             offset_in_page = 0;
694             if (bytes_left > PAGE_SIZE)
695                 bytes_to_page = PAGE_SIZE;
696             else
697                 bytes_to_page = bytes_left;
698         }
699     }
700
701     return rc;
702 }
703
704 /******************************************************************************
705  *      BIGBLOCKFILE_Construct
706  *
707  * Construct a big block file. Create the file mapping object.
708  * Create the read only mapped pages list, the writable mapped page list
709  * and the blocks in use list.
710  */
711 BigBlockFile *BIGBLOCKFILE_Construct(HANDLE hFile, ILockBytes* pLkByt, DWORD openFlags,
712                                      ULONG blocksize, BOOL fileBased)
713 {
714     BigBlockFile *This;
715
716     This = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
717
718     if (This == NULL)
719         return NULL;
720
721     This->fileBased = fileBased;
722     This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
723     This->blocksize = blocksize;
724
725     This->maplist = NULL;
726     This->victimhead = NULL;
727     This->victimtail = NULL;
728     This->num_victim_pages = 0;
729
730     if (This->fileBased)
731     {
732         if (!BIGBLOCKFILE_FileInit(This, hFile))
733         {
734             HeapFree(GetProcessHeap(), 0, This);
735             return NULL;
736         }
737     }
738     else
739     {
740         if (!BIGBLOCKFILE_LockBytesInit(This, pLkByt))
741         {
742             HeapFree(GetProcessHeap(), 0, This);
743             return NULL;
744         }
745     }
746
747     return This;
748 }
749
750 /******************************************************************************
751  *      BIGBLOCKFILE_Destructor
752  *
753  * Destructor. Clean up, free memory.
754  */
755 void BIGBLOCKFILE_Destructor(BigBlockFile *This)
756 {
757     BIGBLOCKFILE_FreeAllMappedPages(This);
758
759     if (This->fileBased)
760     {
761         CloseHandle(This->hfilemap);
762         CloseHandle(This->hfile);
763     }
764     else
765     {
766         ILockBytes_Release(This->pLkbyt);
767     }
768
769     HeapFree(GetProcessHeap(), 0, This);
770 }
771
772 /******************************************************************************
773  *      BIGBLOCKFILE_ReadAt
774  */
775 HRESULT BIGBLOCKFILE_ReadAt(BigBlockFile *This, ULARGE_INTEGER offset,
776                             void* buffer, ULONG size, ULONG* bytesRead)
777 {
778     if (This->fileBased)
779         return ImplBIGBLOCKFILE_ReadAt(This,offset,buffer,size,bytesRead);
780     else
781         return ILockBytes_ReadAt(This->pLkbyt,offset,buffer,size,bytesRead);
782 }
783
784 /******************************************************************************
785  *      BIGBLOCKFILE_WriteAt
786  */
787 HRESULT BIGBLOCKFILE_WriteAt(BigBlockFile *This, ULARGE_INTEGER offset,
788                              const void* buffer, ULONG size, ULONG* bytesRead)
789 {
790     if (This->fileBased)
791         return ImplBIGBLOCKFILE_WriteAt(This,offset,buffer,size,bytesRead);
792     else
793         return ILockBytes_WriteAt(This->pLkbyt,offset,buffer,size,bytesRead);
794 }
795
796 /******************************************************************************
797  *      BIGBLOCKFILE_SetSize
798  *
799  * Sets the size of the file.
800  *
801  */
802 HRESULT BIGBLOCKFILE_SetSize(BigBlockFile *This, ULARGE_INTEGER newSize)
803 {
804     HRESULT hr = S_OK;
805     LARGE_INTEGER newpos;
806
807     if (!This->fileBased)
808         return ILockBytes_SetSize(This->pLkbyt, newSize);
809
810     if (This->filesize.u.LowPart == newSize.u.LowPart)
811         return hr;
812
813     TRACE("from %u to %u\n", This->filesize.u.LowPart, newSize.u.LowPart);
814
815     /*
816      * Unmap all views, must be done before call to SetEndFile.
817      *
818      * Just ditch the victim list because there is no guarantee we will need them
819      * and it is not worth the performance hit to unmap and remap them all.
820      */
821     BIGBLOCKFILE_DeleteList(This, This->victimhead);
822     This->victimhead = NULL;
823     This->victimtail = NULL;
824     This->num_victim_pages = 0;
825
826     BIGBLOCKFILE_UnmapAllMappedPages(This);
827
828     newpos.QuadPart = newSize.QuadPart;
829     if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
830     {
831         if( This->hfilemap ) CloseHandle(This->hfilemap);
832
833         SetEndOfFile(This->hfile);
834
835         /* re-create the file mapping object */
836         This->hfilemap = CreateFileMappingA(This->hfile, NULL, This->flProtect,
837                                             0, 0, NULL);
838     }
839
840     This->filesize = newSize;
841     BIGBLOCKFILE_RemapAllMappedPages(This);
842     return hr;
843 }
844
845 /******************************************************************************
846  *      BIGBLOCKFILE_GetSize
847  *
848  * Gets the size of the file.
849  *
850  */
851 static HRESULT BIGBLOCKFILE_GetSize(BigBlockFile *This, ULARGE_INTEGER *size)
852 {
853     HRESULT hr = S_OK;
854     if(This->fileBased)
855         *size = This->filesize;
856     else
857     {
858         STATSTG stat;
859         hr = ILockBytes_Stat(This->pLkbyt, &stat, STATFLAG_NONAME);
860         if(SUCCEEDED(hr)) *size = stat.cbSize;
861     }
862     return hr;
863 }
864
865 /******************************************************************************
866  *      BIGBLOCKFILE_EnsureExists
867  *
868  * Grows the file if necessary to make sure the block is valid.
869  */
870 HRESULT BIGBLOCKFILE_EnsureExists(BigBlockFile *This, ULONG index)
871 {
872     ULARGE_INTEGER size;
873     HRESULT hr;
874
875     /* Block index starts at -1 translate to zero based index */
876     if (index == 0xffffffff)
877         index = 0;
878     else
879         index++;
880
881     hr = BIGBLOCKFILE_GetSize(This, &size);
882     if(FAILED(hr)) return hr;
883
884     /* make sure that the block physically exists */
885     if ((This->blocksize * (index + 1)) > size.QuadPart)
886     {
887         ULARGE_INTEGER newSize;
888
889         newSize.QuadPart = This->blocksize * (index + 1);
890         hr = BIGBLOCKFILE_SetSize(This, newSize);
891     }
892     return hr;
893 }