ole32: Add a test for the retrieved local class object in another thread being a...
[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 struct MappedPage
89 {
90     MappedPage *next;
91     MappedPage *prev;
92
93     DWORD  page_index;
94     DWORD  mapped_bytes;
95     LPVOID lpBytes;
96     LONG   refcnt;
97
98     BlockBits readable_blocks;
99     BlockBits writable_blocks;
100 };
101
102 /***********************************************************
103  * Prototypes for private methods
104  */
105 static void*     BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
106                                             DWORD          page_index);
107 static void      BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
108                                                 MappedPage *page);
109 static void      BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This);
110 static void      BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This);
111 static void      BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This);
112 static MappedPage* BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
113                                            ULONG page_index);
114 static DWORD     BIGBLOCKFILE_GetProtectMode(DWORD openFlags);
115 static BOOL      BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile);
116 static BOOL      BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt);
117 static void      BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list);
118
119 /* Note that this evaluates a and b multiple times, so don't
120  * pass expressions with side effects. */
121 #define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
122
123 /***********************************************************
124  * Blockbits functions.
125  */
126 static inline BOOL BIGBLOCKFILE_TestBit(const BlockBits *bb,
127                                         unsigned int index)
128 {
129     unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
130     unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
131
132     return bb->bits[array_index] & (1 << bit_index);
133 }
134
135 static inline void BIGBLOCKFILE_SetBit(BlockBits *bb, unsigned int index)
136 {
137     unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
138     unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
139
140     bb->bits[array_index] |= (1 << bit_index);
141 }
142
143 static inline void BIGBLOCKFILE_ClearBit(BlockBits *bb, unsigned int index)
144 {
145     unsigned int array_index = index / (CHAR_BIT * sizeof(unsigned int));
146     unsigned int bit_index = index % (CHAR_BIT * sizeof(unsigned int));
147
148     bb->bits[array_index] &= ~(1 << bit_index);
149 }
150
151 static inline void BIGBLOCKFILE_Zero(BlockBits *bb)
152 {
153     memset(bb->bits, 0, sizeof(bb->bits));
154 }
155
156 /******************************************************************************
157  *      BIGBLOCKFILE_Construct
158  *
159  * Construct a big block file. Create the file mapping object.
160  * Create the read only mapped pages list, the writable mapped page list
161  * and the blocks in use list.
162  */
163 BigBlockFile * BIGBLOCKFILE_Construct(
164   HANDLE   hFile,
165   ILockBytes* pLkByt,
166   DWORD    openFlags,
167   ULONG    blocksize,
168   BOOL     fileBased)
169 {
170   LPBIGBLOCKFILE This;
171
172   This = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
173
174   if (This == NULL)
175     return NULL;
176
177   This->fileBased = fileBased;
178
179   This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
180
181   This->blocksize = blocksize;
182
183   This->maplist = NULL;
184   This->victimhead = NULL;
185   This->victimtail = NULL;
186   This->num_victim_pages = 0;
187
188   if (This->fileBased)
189   {
190     if (!BIGBLOCKFILE_FileInit(This, hFile))
191     {
192       HeapFree(GetProcessHeap(), 0, This);
193       return NULL;
194     }
195   }
196   else
197   {
198     if (!BIGBLOCKFILE_MemInit(This, pLkByt))
199     {
200       HeapFree(GetProcessHeap(), 0, This);
201       return NULL;
202     }
203   }
204
205   return This;
206 }
207
208 /******************************************************************************
209  *      BIGBLOCKFILE_FileInit
210  *
211  * Initialize a big block object supported by a file.
212  */
213 static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
214 {
215   This->pLkbyt     = NULL;
216   This->hbytearray = 0;
217   This->pbytearray = NULL;
218
219   This->hfile = hFile;
220
221   if (This->hfile == INVALID_HANDLE_VALUE)
222     return FALSE;
223
224   This->filesize.u.LowPart = GetFileSize(This->hfile,
225                                          &This->filesize.u.HighPart);
226
227   if( This->filesize.u.LowPart || This->filesize.u.HighPart )
228   {
229     /* create the file mapping object
230      */
231     This->hfilemap = CreateFileMappingA(This->hfile,
232                                         NULL,
233                                         This->flProtect,
234                                         0, 0,
235                                         NULL);
236
237     if (!This->hfilemap)
238     {
239       CloseHandle(This->hfile);
240       return FALSE;
241     }
242   }
243   else
244     This->hfilemap = NULL;
245
246   This->maplist = NULL;
247
248   TRACE("file len %u\n", This->filesize.u.LowPart);
249
250   return TRUE;
251 }
252
253 /******************************************************************************
254  *      BIGBLOCKFILE_MemInit
255  *
256  * Initialize a big block object supported by an ILockBytes on HGLOABL.
257  */
258 static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
259 {
260   This->hfile       = 0;
261   This->hfilemap    = 0;
262
263   /*
264    * Retrieve the handle to the byte array from the LockByte object.
265    */
266   if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
267   {
268     FIXME("May not be an ILockBytes on HGLOBAL\n");
269     return FALSE;
270   }
271
272   This->pLkbyt = plkbyt;
273
274   /*
275    * Increment the reference count of the ILockByte object since
276    * we're keeping a reference to it.
277    */
278   ILockBytes_AddRef(This->pLkbyt);
279
280   This->filesize.u.LowPart = GlobalSize(This->hbytearray);
281   This->filesize.u.HighPart = 0;
282
283   This->pbytearray = GlobalLock(This->hbytearray);
284
285   TRACE("mem on %p len %u\n", This->pbytearray, This->filesize.u.LowPart);
286
287   return TRUE;
288 }
289
290 /******************************************************************************
291  *      BIGBLOCKFILE_Destructor
292  *
293  * Destructor. Clean up, free memory.
294  */
295 void BIGBLOCKFILE_Destructor(
296   LPBIGBLOCKFILE This)
297 {
298   BIGBLOCKFILE_FreeAllMappedPages(This);
299
300   if (This->fileBased)
301   {
302     CloseHandle(This->hfilemap);
303     CloseHandle(This->hfile);
304   }
305   else
306   {
307     GlobalUnlock(This->hbytearray);
308     ILockBytes_Release(This->pLkbyt);
309   }
310
311   /* destroy this
312    */
313   HeapFree(GetProcessHeap(), 0, This);
314 }
315
316 /******************************************************************************
317  *      BIGBLOCKFILE_EnsureExists
318  *
319  * Grows the file if necessary to make sure the block is valid.
320  */
321 void BIGBLOCKFILE_EnsureExists(LPBIGBLOCKFILE This, ULONG index)
322 {
323   /*
324    * block index starts at -1
325    * translate to zero based index
326    */
327   if (index == 0xffffffff)
328     index = 0;
329   else
330     index++;
331
332   /*
333    * make sure that the block physically exists
334    */
335   if ((This->blocksize * (index + 1)) > This->filesize.u.LowPart)
336   {
337     ULARGE_INTEGER newSize;
338
339     newSize.u.HighPart = 0;
340     newSize.u.LowPart = This->blocksize * (index + 1);
341
342     BIGBLOCKFILE_SetSize(This, newSize);
343   }
344 }
345
346 /******************************************************************************
347  *      BIGBLOCKFILE_SetSize
348  *
349  * Sets the size of the file.
350  *
351  */
352 void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
353 {
354   if (This->filesize.u.LowPart == newSize.u.LowPart)
355     return;
356
357   TRACE("from %u to %u\n", This->filesize.u.LowPart, newSize.u.LowPart);
358   /*
359    * unmap all views, must be done before call to SetEndFile
360    *
361    * Just ditch the victim list because there is no guarentee we will need them
362    * and it is not worth the performance hit to unmap and remap them all.
363    */
364   BIGBLOCKFILE_DeleteList(This, This->victimhead);
365   This->victimhead = NULL;
366   This->victimtail = NULL;
367   This->num_victim_pages = 0;
368
369   BIGBLOCKFILE_UnmapAllMappedPages(This);
370
371   if (This->fileBased)
372   {
373     LARGE_INTEGER newpos;
374
375     newpos.QuadPart = newSize.QuadPart;
376     if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
377     {
378         if( This->hfilemap ) CloseHandle(This->hfilemap);
379
380         SetEndOfFile(This->hfile);
381
382         /*
383          * re-create the file mapping object
384          */
385         This->hfilemap = CreateFileMappingA(This->hfile,
386                                             NULL,
387                                             This->flProtect,
388                                             0, 0,
389                                             NULL);
390     }
391   }
392   else
393   {
394     GlobalUnlock(This->hbytearray);
395
396     /*
397      * Resize the byte array object.
398      */
399     ILockBytes_SetSize(This->pLkbyt, newSize);
400
401     /*
402      * Re-acquire the handle, it may have changed.
403      */
404     GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
405     This->pbytearray = GlobalLock(This->hbytearray);
406   }
407
408   This->filesize.u.LowPart = newSize.u.LowPart;
409   This->filesize.u.HighPart = newSize.u.HighPart;
410
411   BIGBLOCKFILE_RemapAllMappedPages(This);
412 }
413
414 /******************************************************************************
415  *      BIGBLOCKFILE_GetSize
416  *
417  * Returns the size of the file.
418  *
419  */
420 ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
421 {
422   return This->filesize;
423 }
424
425 /******************************************************************************
426  *      BIGBLOCKFILE_FindPageInList      [PRIVATE]
427  *
428  */
429 static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
430                                                ULONG page_index)
431 {
432     for (; head != NULL; head = head->next)
433     {
434         if (head->page_index == page_index)
435         {
436             InterlockedIncrement(&head->refcnt);
437             break;
438         }
439     }
440
441     return head;
442
443 }
444
445 static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
446 {
447     if (page->next) page->next->prev = page->prev;
448     if (page->prev) page->prev->next = page->next;
449 }
450
451 static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
452 {
453     if (*head) (*head)->prev = page;
454     page->next = *head;
455     page->prev = NULL;
456     *head = page;
457 }
458
459 /******************************************************************************
460  *      BIGBLOCKFILE_GetMappedView      [PRIVATE]
461  *
462  * Gets the page requested if it is already mapped.
463  * If it's not already mapped, this method will map it
464  */
465 static void * BIGBLOCKFILE_GetMappedView(
466   LPBIGBLOCKFILE This,
467   DWORD          page_index)
468 {
469     MappedPage *page;
470
471     page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
472     if (!page)
473     {
474         page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
475         if (page)
476         {
477             This->num_victim_pages--;
478
479             BIGBLOCKFILE_Zero(&page->readable_blocks);
480             BIGBLOCKFILE_Zero(&page->writable_blocks);
481         }
482     }
483
484     if (page)
485     {
486         /* If the page is not already at the head of the list, move
487          * it there. (Also moves pages from victim to main list.) */
488         if (This->maplist != page)
489         {
490             if (This->victimhead == page) This->victimhead = page->next;
491             if (This->victimtail == page) This->victimtail = page->prev;
492
493             BIGBLOCKFILE_UnlinkPage(page);
494
495             BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
496         }
497
498         return page;
499     }
500
501     page = BIGBLOCKFILE_CreatePage(This, page_index);
502     if (!page) return NULL;
503
504     BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
505
506     return page;
507 }
508
509 static BOOL BIGBLOCKFILE_MapPage(LPBIGBLOCKFILE This, MappedPage *page)
510 {
511     DWORD lowoffset = PAGE_SIZE * page->page_index;
512
513     if (This->fileBased)
514     {
515         DWORD numBytesToMap;
516         DWORD desired_access;
517
518         if( !This->hfilemap )
519             return FALSE;
520
521         if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
522             numBytesToMap = This->filesize.u.LowPart - lowoffset;
523         else
524             numBytesToMap = PAGE_SIZE;
525
526         if (This->flProtect == PAGE_READONLY)
527             desired_access = FILE_MAP_READ;
528         else
529             desired_access = FILE_MAP_WRITE;
530
531         page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
532                                       lowoffset, numBytesToMap);
533         page->mapped_bytes = numBytesToMap;
534     }
535     else
536     {
537         page->lpBytes = (LPBYTE)This->pbytearray + lowoffset;
538         page->mapped_bytes = PAGE_SIZE;
539     }
540
541     TRACE("mapped page %u to %p\n", page->page_index, page->lpBytes);
542
543     return page->lpBytes != NULL;
544 }
545
546 static MappedPage *BIGBLOCKFILE_CreatePage(LPBIGBLOCKFILE This,
547                                            ULONG page_index)
548 {
549     MappedPage *page;
550
551     page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
552     if (page == NULL)
553       return NULL;
554
555     page->page_index = page_index;
556     page->refcnt = 1;
557
558     page->next = NULL;
559     page->prev = NULL;
560
561     if (!BIGBLOCKFILE_MapPage(This, page))
562     {
563         HeapFree(GetProcessHeap(),0,page);
564         return NULL;
565     }
566
567     BIGBLOCKFILE_Zero(&page->readable_blocks);
568     BIGBLOCKFILE_Zero(&page->writable_blocks);
569
570     return page;
571 }
572
573 static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
574 {
575     TRACE("%d at %p\n", page->page_index, page->lpBytes);
576     if (page->refcnt > 0)
577         ERR("unmapping inuse page %p\n", page->lpBytes);
578
579     if (This->fileBased && page->lpBytes)
580         UnmapViewOfFile(page->lpBytes);
581
582     page->lpBytes = NULL;
583 }
584
585 static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
586 {
587     BIGBLOCKFILE_UnmapPage(This, page);
588
589     HeapFree(GetProcessHeap(), 0, page);
590 }
591
592 /******************************************************************************
593  *      BIGBLOCKFILE_ReleaseMappedPage      [PRIVATE]
594  *
595  * Decrements the reference count of the mapped page.
596  */
597 static void BIGBLOCKFILE_ReleaseMappedPage(
598   LPBIGBLOCKFILE This,
599   MappedPage    *page)
600 {
601     assert(This != NULL);
602     assert(page != NULL);
603
604     /* If the page is no longer refenced, move it to the victim list.
605      * If the victim list is too long, kick somebody off. */
606     if (!InterlockedDecrement(&page->refcnt))
607     {
608         if (This->maplist == page) This->maplist = page->next;
609
610         BIGBLOCKFILE_UnlinkPage(page);
611
612         if (MAX_VICTIM_PAGES > 0)
613         {
614             if (This->num_victim_pages >= MAX_VICTIM_PAGES)
615             {
616                 MappedPage *victim = This->victimtail;
617                 if (victim)
618                 {
619                     This->victimtail = victim->prev;
620                     if (This->victimhead == victim)
621                         This->victimhead = victim->next;
622
623                     BIGBLOCKFILE_UnlinkPage(victim);
624                     BIGBLOCKFILE_DeletePage(This, victim);
625                 }
626             }
627             else This->num_victim_pages++;
628
629             BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
630             if (This->victimtail == NULL) This->victimtail = page;
631         }
632         else
633             BIGBLOCKFILE_DeletePage(This, page);
634     }
635 }
636
637 static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
638 {
639     while (list != NULL)
640     {
641         MappedPage *next = list->next;
642
643         BIGBLOCKFILE_DeletePage(This, list);
644
645         list = next;
646     }
647 }
648
649 /******************************************************************************
650  *      BIGBLOCKFILE_FreeAllMappedPages     [PRIVATE]
651  *
652  * Unmap all currently mapped pages.
653  * Empty mapped pages list.
654  */
655 static void BIGBLOCKFILE_FreeAllMappedPages(
656   LPBIGBLOCKFILE This)
657 {
658     BIGBLOCKFILE_DeleteList(This, This->maplist);
659     BIGBLOCKFILE_DeleteList(This, This->victimhead);
660
661     This->maplist = NULL;
662     This->victimhead = NULL;
663     This->victimtail = NULL;
664     This->num_victim_pages = 0;
665 }
666
667 static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
668 {
669     for (; list != NULL; list = list->next)
670     {
671         BIGBLOCKFILE_UnmapPage(This, list);
672     }
673 }
674
675 static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
676 {
677     BIGBLOCKFILE_UnmapList(This, This->maplist);
678     BIGBLOCKFILE_UnmapList(This, This->victimhead);
679 }
680
681 static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
682 {
683     while (list != NULL)
684     {
685         MappedPage *next = list->next;
686
687         if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
688         {
689             TRACE("discarding %u\n", list->page_index);
690
691             /* page is entirely outside of the file, delete it */
692             BIGBLOCKFILE_UnlinkPage(list);
693             BIGBLOCKFILE_DeletePage(This, list);
694         }
695         else
696         {
697             /* otherwise, remap it */
698             BIGBLOCKFILE_MapPage(This, list);
699         }
700
701         list = next;
702     }
703 }
704
705 static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
706 {
707     BIGBLOCKFILE_RemapList(This, This->maplist);
708     BIGBLOCKFILE_RemapList(This, This->victimhead);
709 }
710
711 /****************************************************************************
712  *      BIGBLOCKFILE_GetProtectMode
713  *
714  * This function will return a protection mode flag for a file-mapping object
715  * from the open flags of a file.
716  */
717 static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
718 {
719     switch(STGM_ACCESS_MODE(openFlags))
720     {
721     case STGM_WRITE:
722     case STGM_READWRITE:
723         return PAGE_READWRITE;
724     }
725     return PAGE_READONLY;
726 }
727
728
729 /* ILockByte Interfaces */
730
731 /******************************************************************************
732  * This method is part of the ILockBytes interface.
733  *
734  * It reads a block of information from the byte array at the specified
735  * offset.
736  *
737  * See the documentation of ILockBytes for more info.
738  */
739 static HRESULT WINAPI ImplBIGBLOCKFILE_ReadAt(
740       BigBlockFile* const This,
741       ULARGE_INTEGER ulOffset,  /* [in] */
742       void*          pv,        /* [length_is][size_is][out] */
743       ULONG          cb,        /* [in] */
744       ULONG*         pcbRead)   /* [out] */
745 {
746     ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
747     ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
748     ULONG bytes_left = cb;
749     ULONG page_index = first_page;
750     ULONG bytes_from_page;
751     LPVOID writePtr = pv;
752
753     HRESULT rc = S_OK;
754
755     TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbRead);
756
757     /* verify a sane environment */
758     if (!This) return E_FAIL;
759
760     if (offset_in_page + bytes_left > PAGE_SIZE)
761         bytes_from_page = PAGE_SIZE - offset_in_page;
762     else
763         bytes_from_page = bytes_left;
764
765     if (pcbRead)
766         *pcbRead = 0;
767
768     while (bytes_left)
769     {
770         LPBYTE readPtr;
771         BOOL eof = FALSE;
772         MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
773
774         if (!page || !page->lpBytes)
775         {
776             rc = STG_E_READFAULT;
777             break;
778         }
779
780         TRACE("page %i,  offset %u, bytes_from_page %u, bytes_left %u\n",
781             page->page_index, offset_in_page, bytes_from_page, bytes_left);
782
783         if (page->mapped_bytes < bytes_from_page)
784         {
785             eof = TRUE;
786             bytes_from_page = page->mapped_bytes;
787         }
788
789         readPtr = (BYTE*)page->lpBytes + offset_in_page;
790         memcpy(writePtr,readPtr,bytes_from_page);
791         BIGBLOCKFILE_ReleaseMappedPage(This, page);
792
793         if (pcbRead)
794             *pcbRead += bytes_from_page;
795         bytes_left -= bytes_from_page;
796
797         if (bytes_left && !eof)
798         {
799             writePtr = (LPBYTE)writePtr + bytes_from_page;
800             page_index ++;
801             offset_in_page = 0;
802             if (bytes_left > PAGE_SIZE)
803                 bytes_from_page = PAGE_SIZE;
804             else
805                 bytes_from_page = bytes_left;
806         }
807         if (eof)
808         {
809             rc = STG_E_READFAULT;
810             break;
811         }
812     }
813
814     TRACE("finished\n");
815     return rc;
816 }
817
818 /******************************************************************************
819  * This method is part of the ILockBytes interface.
820  *
821  * It writes the specified bytes at the specified offset.
822  * position. If the file is too small, it will be resized.
823  *
824  * See the documentation of ILockBytes for more info.
825  */
826 static HRESULT WINAPI ImplBIGBLOCKFILE_WriteAt(
827       BigBlockFile* const This,
828       ULARGE_INTEGER ulOffset,    /* [in] */
829       const void*    pv,          /* [size_is][in] */
830       ULONG          cb,          /* [in] */
831       ULONG*         pcbWritten)  /* [out] */
832 {
833     ULONG size_needed = ulOffset.u.LowPart + cb;
834     ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
835     ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
836     ULONG bytes_left = cb;
837     ULONG page_index = first_page;
838     ULONG bytes_to_page;
839     LPCVOID readPtr = pv;
840
841     HRESULT rc = S_OK;
842
843     TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbWritten);
844
845     /* verify a sane environment */
846     if (!This) return E_FAIL;
847
848     if (This->flProtect != PAGE_READWRITE)
849         return STG_E_ACCESSDENIED;
850
851     if (size_needed > This->filesize.u.LowPart)
852     {
853         ULARGE_INTEGER newSize;
854         newSize.u.HighPart = 0;
855         newSize.u.LowPart = size_needed;
856         BIGBLOCKFILE_SetSize(This, newSize);
857     }
858
859     if (offset_in_page + bytes_left > PAGE_SIZE)
860         bytes_to_page = PAGE_SIZE - offset_in_page;
861     else
862         bytes_to_page = bytes_left;
863
864     if (pcbWritten)
865         *pcbWritten = 0;
866
867     while (bytes_left)
868     {
869         LPBYTE writePtr;
870         MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
871
872         TRACE("page %i,  offset %u, bytes_to_page %u, bytes_left %u\n",
873             page ? page->page_index : 0, offset_in_page, bytes_to_page, bytes_left);
874
875         if (!page)
876         {
877             ERR("Unable to get a page to write. This should never happen\n");
878             rc = E_FAIL;
879             break;
880         }
881
882         if (page->mapped_bytes < bytes_to_page)
883         {
884             ERR("Not enough bytes mapped to the page. This should never happen\n");
885             rc = E_FAIL;
886             break;
887         }
888
889         writePtr = (BYTE*)page->lpBytes + offset_in_page;
890         memcpy(writePtr,readPtr,bytes_to_page);
891         BIGBLOCKFILE_ReleaseMappedPage(This, page);
892
893         if (pcbWritten)
894             *pcbWritten += bytes_to_page;
895         bytes_left -= bytes_to_page;
896
897         if (bytes_left)
898         {
899             readPtr = (LPBYTE)readPtr + bytes_to_page;
900             page_index ++;
901             offset_in_page = 0;
902             if (bytes_left > PAGE_SIZE)
903                 bytes_to_page = PAGE_SIZE;
904             else
905                 bytes_to_page = bytes_left;
906         }
907     }
908
909     return rc;
910 }
911
912
913 HRESULT     BIGBLOCKFILE_ReadAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
914                 void* buffer, ULONG size, ULONG* bytesRead)
915 {
916     if (This->fileBased)
917         return ImplBIGBLOCKFILE_ReadAt(This,offset,buffer,size,bytesRead);
918     else
919         return ILockBytes_ReadAt(This->pLkbyt,offset,buffer,size,bytesRead);
920 }
921
922 HRESULT    BIGBLOCKFILE_WriteAt(LPBIGBLOCKFILE This, ULARGE_INTEGER offset,
923                 void* buffer, const ULONG size, ULONG* bytesRead)
924 {
925     if (This->fileBased)
926         return ImplBIGBLOCKFILE_WriteAt(This,offset,buffer,size,bytesRead);
927     else
928         return ILockBytes_WriteAt(This->pLkbyt,offset,buffer,size,bytesRead);
929 }