1999-01-30 14:24:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* BigBlockFile
|
|
|
|
*
|
2002-06-01 01:06:46 +02:00
|
|
|
* This is the implementation of a file that consists of blocks of
|
1999-01-30 14:24:23 +01:00
|
|
|
* a predetermined size.
|
2002-06-01 01:06:46 +02:00
|
|
|
* This class is used in the Compound File implementation of the
|
|
|
|
* IStorage and IStream interfaces. It provides the functionality
|
|
|
|
* to read and write any blocks in the file as well as setting and
|
1999-01-30 14:24:23 +01:00
|
|
|
* obtaining the size of the file.
|
|
|
|
* The blocks are indexed sequentially from the start of the file
|
|
|
|
* starting with -1.
|
2002-06-01 01:06:46 +02:00
|
|
|
*
|
1999-01-30 14:24:23 +01:00
|
|
|
* TODO:
|
|
|
|
* - Support for a transacted mode
|
|
|
|
*
|
|
|
|
* Copyright 1999 Thuy Nguyen
|
|
|
|
*
|
2002-03-10 00:29:33 +01:00
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
1999-01-30 14:24:23 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2000-03-17 16:08:25 +01:00
|
|
|
#include <limits.h>
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2004-10-07 05:06:48 +02:00
|
|
|
#define COBJMACROS
|
2003-01-07 21:36:20 +01:00
|
|
|
#define NONAMELESSUNION
|
|
|
|
#define NONAMELESSSTRUCT
|
2004-10-07 05:06:48 +02:00
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include "windef.h"
|
1999-02-28 20:14:33 +01:00
|
|
|
#include "winbase.h"
|
2003-09-09 21:39:31 +02:00
|
|
|
#include "winuser.h"
|
1999-01-30 14:24:23 +01:00
|
|
|
#include "winerror.h"
|
2002-12-05 21:33:07 +01:00
|
|
|
#include "objbase.h"
|
1999-05-08 12:05:08 +02:00
|
|
|
#include "ole2.h"
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
#include "storage32.h"
|
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
#include "wine/debug.h"
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2002-03-10 00:29:33 +01:00
|
|
|
WINE_DEFAULT_DEBUG_CHANNEL(storage);
|
1999-05-08 12:05:08 +02:00
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/***********************************************************
|
|
|
|
* Data structures used internally by the BigBlockFile
|
|
|
|
* class.
|
|
|
|
*/
|
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
/* We map in PAGE_SIZE-sized chunks. Must be a multiple of 4096. */
|
|
|
|
#define PAGE_SIZE 131072
|
|
|
|
|
|
|
|
/* We keep a list of recently-discarded pages. This controls the
|
|
|
|
* size of that list. */
|
|
|
|
#define MAX_VICTIM_PAGES 16
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/***
|
|
|
|
* This structure identifies the paged that are mapped
|
2002-06-01 01:06:46 +02:00
|
|
|
* from the file and their position in memory. It is
|
1999-01-30 14:24:23 +01:00
|
|
|
* also used to hold a reference count to those pages.
|
2000-03-17 16:08:25 +01:00
|
|
|
*
|
|
|
|
* page_index identifies which PAGE_SIZE chunk from the
|
|
|
|
* file this mapping represents. (The mappings are always
|
|
|
|
* PAGE_SIZE-aligned.)
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
2009-01-27 16:27:08 +01:00
|
|
|
|
|
|
|
typedef struct MappedPage MappedPage;
|
1999-01-30 14:24:23 +01:00
|
|
|
struct MappedPage
|
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
MappedPage *next;
|
|
|
|
MappedPage *prev;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
DWORD page_index;
|
2007-03-16 19:34:28 +01:00
|
|
|
DWORD mapped_bytes;
|
2000-03-17 16:08:25 +01:00
|
|
|
LPVOID lpBytes;
|
|
|
|
LONG refcnt;
|
|
|
|
};
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2009-01-27 16:27:08 +01:00
|
|
|
struct BigBlockFile
|
|
|
|
{
|
|
|
|
BOOL fileBased;
|
|
|
|
ULARGE_INTEGER filesize;
|
|
|
|
ULONG blocksize;
|
|
|
|
HANDLE hfile;
|
|
|
|
HANDLE hfilemap;
|
|
|
|
DWORD flProtect;
|
|
|
|
MappedPage *maplist;
|
|
|
|
MappedPage *victimhead, *victimtail;
|
|
|
|
ULONG num_victim_pages;
|
|
|
|
ILockBytes *pLkbyt;
|
|
|
|
};
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/***********************************************************
|
|
|
|
* Prototypes for private methods
|
|
|
|
*/
|
2000-03-17 16:08:25 +01:00
|
|
|
|
|
|
|
/* Note that this evaluates a and b multiple times, so don't
|
|
|
|
* pass expressions with side effects. */
|
|
|
|
#define ROUND_UP(a, b) ((((a) + (b) - 1)/(b))*(b))
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_FileInit
|
|
|
|
*
|
|
|
|
* Initialize a big block object supported by a file.
|
|
|
|
*/
|
|
|
|
static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
|
|
|
|
{
|
2009-01-27 18:35:18 +01:00
|
|
|
This->pLkbyt = NULL;
|
1999-05-08 12:05:08 +02:00
|
|
|
This->hfile = hFile;
|
|
|
|
|
|
|
|
if (This->hfile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
2004-01-23 02:51:33 +01:00
|
|
|
This->filesize.u.LowPart = GetFileSize(This->hfile,
|
|
|
|
&This->filesize.u.HighPart);
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2004-01-23 02:51:33 +01:00
|
|
|
if( This->filesize.u.LowPart || This->filesize.u.HighPart )
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2004-01-19 22:44:02 +01:00
|
|
|
/* create the file mapping object
|
|
|
|
*/
|
|
|
|
This->hfilemap = CreateFileMappingA(This->hfile,
|
|
|
|
NULL,
|
|
|
|
This->flProtect,
|
|
|
|
0, 0,
|
|
|
|
NULL);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2004-01-19 22:44:02 +01:00
|
|
|
if (!This->hfilemap)
|
|
|
|
{
|
|
|
|
CloseHandle(This->hfile);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
This->hfilemap = NULL;
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
This->maplist = NULL;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("file len %u\n", This->filesize.u.LowPart);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
2009-01-27 18:35:18 +01:00
|
|
|
* BIGBLOCKFILE_LockBytesInit
|
1999-05-08 12:05:08 +02:00
|
|
|
*
|
2009-01-27 18:35:18 +01:00
|
|
|
* Initialize a big block object supported by an ILockBytes.
|
1999-05-08 12:05:08 +02:00
|
|
|
*/
|
2009-01-27 18:35:18 +01:00
|
|
|
static BOOL BIGBLOCKFILE_LockBytesInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
|
1999-05-08 12:05:08 +02:00
|
|
|
{
|
2009-01-27 18:35:18 +01:00
|
|
|
This->hfile = 0;
|
|
|
|
This->hfilemap = 0;
|
|
|
|
This->pLkbyt = plkbyt;
|
|
|
|
ILockBytes_AddRef(This->pLkbyt);
|
1999-05-08 12:05:08 +02:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
/* We'll get the size directly with ILockBytes_Stat */
|
|
|
|
This->filesize.QuadPart = 0;
|
2000-03-17 16:08:25 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
TRACE("ILockBytes %p\n", This->pLkbyt);
|
|
|
|
return TRUE;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/******************************************************************************
|
2000-03-17 16:08:25 +01:00
|
|
|
* BIGBLOCKFILE_FindPageInList [PRIVATE]
|
1999-05-08 12:05:08 +02:00
|
|
|
*
|
|
|
|
*/
|
2000-03-17 16:08:25 +01:00
|
|
|
static MappedPage *BIGBLOCKFILE_FindPageInList(MappedPage *head,
|
|
|
|
ULONG page_index)
|
1999-05-08 12:05:08 +02:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
for (; head != NULL; head = head->next)
|
|
|
|
{
|
|
|
|
if (head->page_index == page_index)
|
|
|
|
{
|
|
|
|
InterlockedIncrement(&head->refcnt);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return head;
|
1999-05-08 12:05:08 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
static void BIGBLOCKFILE_UnlinkPage(MappedPage *page)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
if (page->next) page->next->prev = page->prev;
|
|
|
|
if (page->prev) page->prev->next = page->next;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
static void BIGBLOCKFILE_LinkHeadPage(MappedPage **head, MappedPage *page)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
if (*head) (*head)->prev = page;
|
|
|
|
page->next = *head;
|
|
|
|
page->prev = NULL;
|
|
|
|
*head = page;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
static BOOL BIGBLOCKFILE_MapPage(BigBlockFile *This, MappedPage *page)
|
|
|
|
{
|
|
|
|
DWORD lowoffset = PAGE_SIZE * page->page_index;
|
2009-01-27 18:35:18 +01:00
|
|
|
DWORD numBytesToMap;
|
|
|
|
DWORD desired_access;
|
2009-01-27 18:03:55 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
assert(This->fileBased);
|
2009-01-27 18:03:55 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
if( !This->hfilemap )
|
|
|
|
return FALSE;
|
2009-01-27 18:03:55 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
if (lowoffset + PAGE_SIZE > This->filesize.u.LowPart)
|
|
|
|
numBytesToMap = This->filesize.u.LowPart - lowoffset;
|
|
|
|
else
|
|
|
|
numBytesToMap = PAGE_SIZE;
|
2009-01-27 18:03:55 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
if (This->flProtect == PAGE_READONLY)
|
|
|
|
desired_access = FILE_MAP_READ;
|
2009-01-27 18:03:55 +01:00
|
|
|
else
|
2009-01-27 18:35:18 +01:00
|
|
|
desired_access = FILE_MAP_WRITE;
|
|
|
|
|
|
|
|
page->lpBytes = MapViewOfFile(This->hfilemap, desired_access, 0,
|
|
|
|
lowoffset, numBytesToMap);
|
|
|
|
page->mapped_bytes = numBytesToMap;
|
2009-01-27 18:03:55 +01:00
|
|
|
|
|
|
|
TRACE("mapped page %u to %p\n", page->page_index, page->lpBytes);
|
|
|
|
|
|
|
|
return page->lpBytes != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static MappedPage *BIGBLOCKFILE_CreatePage(BigBlockFile *This, ULONG page_index)
|
|
|
|
{
|
|
|
|
MappedPage *page;
|
|
|
|
|
|
|
|
page = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
|
|
|
|
if (page == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
page->page_index = page_index;
|
|
|
|
page->refcnt = 1;
|
|
|
|
|
|
|
|
page->next = NULL;
|
|
|
|
page->prev = NULL;
|
|
|
|
|
|
|
|
if (!BIGBLOCKFILE_MapPage(This, page))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(),0,page);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetMappedView [PRIVATE]
|
|
|
|
*
|
|
|
|
* Gets the page requested if it is already mapped.
|
|
|
|
* If it's not already mapped, this method will map it
|
|
|
|
*/
|
|
|
|
static void * BIGBLOCKFILE_GetMappedView(
|
|
|
|
LPBIGBLOCKFILE This,
|
2000-03-17 16:08:25 +01:00
|
|
|
DWORD page_index)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
MappedPage *page;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
page = BIGBLOCKFILE_FindPageInList(This->maplist, page_index);
|
|
|
|
if (!page)
|
|
|
|
{
|
|
|
|
page = BIGBLOCKFILE_FindPageInList(This->victimhead, page_index);
|
|
|
|
if (page)
|
|
|
|
{
|
|
|
|
This->num_victim_pages--;
|
|
|
|
}
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
if (page)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
/* If the page is not already at the head of the list, move
|
|
|
|
* it there. (Also moves pages from victim to main list.) */
|
|
|
|
if (This->maplist != page)
|
|
|
|
{
|
|
|
|
if (This->victimhead == page) This->victimhead = page->next;
|
|
|
|
if (This->victimtail == page) This->victimtail = page->prev;
|
1999-03-12 18:06:46 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
BIGBLOCKFILE_UnlinkPage(page);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
return page;
|
1999-03-12 18:06:46 +01:00
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
page = BIGBLOCKFILE_CreatePage(This, page_index);
|
|
|
|
if (!page) return NULL;
|
|
|
|
|
|
|
|
BIGBLOCKFILE_LinkHeadPage(&This->maplist, page);
|
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BIGBLOCKFILE_UnmapPage(LPBIGBLOCKFILE This, MappedPage *page)
|
|
|
|
{
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("%d at %p\n", page->page_index, page->lpBytes);
|
2009-01-27 18:35:18 +01:00
|
|
|
|
|
|
|
assert(This->fileBased);
|
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
if (page->refcnt > 0)
|
|
|
|
ERR("unmapping inuse page %p\n", page->lpBytes);
|
1999-03-12 18:06:46 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
if (page->lpBytes)
|
2000-03-17 16:08:25 +01:00
|
|
|
UnmapViewOfFile(page->lpBytes);
|
|
|
|
|
|
|
|
page->lpBytes = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BIGBLOCKFILE_DeletePage(LPBIGBLOCKFILE This, MappedPage *page)
|
|
|
|
{
|
|
|
|
BIGBLOCKFILE_UnmapPage(This, page);
|
1999-03-12 18:06:46 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, page);
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_ReleaseMappedPage [PRIVATE]
|
|
|
|
*
|
|
|
|
* Decrements the reference count of the mapped page.
|
|
|
|
*/
|
|
|
|
static void BIGBLOCKFILE_ReleaseMappedPage(
|
|
|
|
LPBIGBLOCKFILE This,
|
2000-03-17 16:08:25 +01:00
|
|
|
MappedPage *page)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
assert(This != NULL);
|
|
|
|
assert(page != NULL);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
/* If the page is no longer refenced, move it to the victim list.
|
|
|
|
* If the victim list is too long, kick somebody off. */
|
|
|
|
if (!InterlockedDecrement(&page->refcnt))
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
if (This->maplist == page) This->maplist = page->next;
|
|
|
|
|
|
|
|
BIGBLOCKFILE_UnlinkPage(page);
|
|
|
|
|
|
|
|
if (MAX_VICTIM_PAGES > 0)
|
|
|
|
{
|
|
|
|
if (This->num_victim_pages >= MAX_VICTIM_PAGES)
|
|
|
|
{
|
|
|
|
MappedPage *victim = This->victimtail;
|
|
|
|
if (victim)
|
|
|
|
{
|
|
|
|
This->victimtail = victim->prev;
|
|
|
|
if (This->victimhead == victim)
|
|
|
|
This->victimhead = victim->next;
|
|
|
|
|
|
|
|
BIGBLOCKFILE_UnlinkPage(victim);
|
|
|
|
BIGBLOCKFILE_DeletePage(This, victim);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else This->num_victim_pages++;
|
|
|
|
|
|
|
|
BIGBLOCKFILE_LinkHeadPage(&This->victimhead, page);
|
|
|
|
if (This->victimtail == NULL) This->victimtail = page;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
BIGBLOCKFILE_DeletePage(This, page);
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
2000-03-17 16:08:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void BIGBLOCKFILE_DeleteList(LPBIGBLOCKFILE This, MappedPage *list)
|
|
|
|
{
|
|
|
|
while (list != NULL)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
MappedPage *next = list->next;
|
|
|
|
|
|
|
|
BIGBLOCKFILE_DeletePage(This, list);
|
|
|
|
|
|
|
|
list = next;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_FreeAllMappedPages [PRIVATE]
|
|
|
|
*
|
|
|
|
* Unmap all currently mapped pages.
|
1999-03-12 18:06:46 +01:00
|
|
|
* Empty mapped pages list.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
|
|
|
static void BIGBLOCKFILE_FreeAllMappedPages(
|
|
|
|
LPBIGBLOCKFILE This)
|
|
|
|
{
|
2000-03-17 16:08:25 +01:00
|
|
|
BIGBLOCKFILE_DeleteList(This, This->maplist);
|
|
|
|
BIGBLOCKFILE_DeleteList(This, This->victimhead);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
This->maplist = NULL;
|
|
|
|
This->victimhead = NULL;
|
|
|
|
This->victimtail = NULL;
|
|
|
|
This->num_victim_pages = 0;
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
static void BIGBLOCKFILE_UnmapList(LPBIGBLOCKFILE This, MappedPage *list)
|
|
|
|
{
|
|
|
|
for (; list != NULL; list = list->next)
|
|
|
|
{
|
|
|
|
BIGBLOCKFILE_UnmapPage(This, list);
|
|
|
|
}
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
2000-03-17 16:08:25 +01:00
|
|
|
static void BIGBLOCKFILE_UnmapAllMappedPages(LPBIGBLOCKFILE This)
|
|
|
|
{
|
|
|
|
BIGBLOCKFILE_UnmapList(This, This->maplist);
|
|
|
|
BIGBLOCKFILE_UnmapList(This, This->victimhead);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BIGBLOCKFILE_RemapList(LPBIGBLOCKFILE This, MappedPage *list)
|
|
|
|
{
|
|
|
|
while (list != NULL)
|
|
|
|
{
|
|
|
|
MappedPage *next = list->next;
|
|
|
|
|
2004-01-23 02:51:33 +01:00
|
|
|
if (list->page_index * PAGE_SIZE > This->filesize.u.LowPart)
|
2000-03-17 16:08:25 +01:00
|
|
|
{
|
2006-10-15 12:28:08 +02:00
|
|
|
TRACE("discarding %u\n", list->page_index);
|
2000-03-17 16:08:25 +01:00
|
|
|
|
|
|
|
/* page is entirely outside of the file, delete it */
|
|
|
|
BIGBLOCKFILE_UnlinkPage(list);
|
|
|
|
BIGBLOCKFILE_DeletePage(This, list);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* otherwise, remap it */
|
|
|
|
BIGBLOCKFILE_MapPage(This, list);
|
|
|
|
}
|
|
|
|
|
|
|
|
list = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BIGBLOCKFILE_RemapAllMappedPages(LPBIGBLOCKFILE This)
|
|
|
|
{
|
|
|
|
BIGBLOCKFILE_RemapList(This, This->maplist);
|
|
|
|
BIGBLOCKFILE_RemapList(This, This->victimhead);
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-02-18 16:55:53 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetProtectMode
|
|
|
|
*
|
|
|
|
* This function will return a protection mode flag for a file-mapping object
|
|
|
|
* from the open flags of a file.
|
|
|
|
*/
|
|
|
|
static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags)
|
|
|
|
{
|
2005-03-07 13:24:42 +01:00
|
|
|
switch(STGM_ACCESS_MODE(openFlags))
|
|
|
|
{
|
|
|
|
case STGM_WRITE:
|
|
|
|
case STGM_READWRITE:
|
|
|
|
return PAGE_READWRITE;
|
|
|
|
}
|
|
|
|
return PAGE_READONLY;
|
1999-02-18 16:55:53 +01:00
|
|
|
}
|
2007-03-16 19:34:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
/* ILockByte Interfaces */
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* This method is part of the ILockBytes interface.
|
|
|
|
*
|
|
|
|
* It reads a block of information from the byte array at the specified
|
|
|
|
* offset.
|
|
|
|
*
|
|
|
|
* See the documentation of ILockBytes for more info.
|
|
|
|
*/
|
2008-11-25 16:41:41 +01:00
|
|
|
static HRESULT ImplBIGBLOCKFILE_ReadAt(
|
2007-03-16 19:34:28 +01:00
|
|
|
BigBlockFile* const This,
|
|
|
|
ULARGE_INTEGER ulOffset, /* [in] */
|
|
|
|
void* pv, /* [length_is][size_is][out] */
|
|
|
|
ULONG cb, /* [in] */
|
|
|
|
ULONG* pcbRead) /* [out] */
|
|
|
|
{
|
|
|
|
ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
|
|
|
|
ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
|
|
|
|
ULONG bytes_left = cb;
|
|
|
|
ULONG page_index = first_page;
|
|
|
|
ULONG bytes_from_page;
|
|
|
|
LPVOID writePtr = pv;
|
|
|
|
|
|
|
|
HRESULT rc = S_OK;
|
|
|
|
|
|
|
|
TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbRead);
|
|
|
|
|
2007-04-02 19:17:56 +02:00
|
|
|
/* verify a sane environment */
|
2007-03-16 19:34:28 +01:00
|
|
|
if (!This) return E_FAIL;
|
|
|
|
|
|
|
|
if (offset_in_page + bytes_left > PAGE_SIZE)
|
|
|
|
bytes_from_page = PAGE_SIZE - offset_in_page;
|
|
|
|
else
|
|
|
|
bytes_from_page = bytes_left;
|
|
|
|
|
|
|
|
if (pcbRead)
|
|
|
|
*pcbRead = 0;
|
|
|
|
|
|
|
|
while (bytes_left)
|
|
|
|
{
|
|
|
|
LPBYTE readPtr;
|
|
|
|
BOOL eof = FALSE;
|
|
|
|
MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
|
|
|
|
|
|
|
|
if (!page || !page->lpBytes)
|
|
|
|
{
|
|
|
|
rc = STG_E_READFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("page %i, offset %u, bytes_from_page %u, bytes_left %u\n",
|
|
|
|
page->page_index, offset_in_page, bytes_from_page, bytes_left);
|
|
|
|
|
|
|
|
if (page->mapped_bytes < bytes_from_page)
|
|
|
|
{
|
|
|
|
eof = TRUE;
|
|
|
|
bytes_from_page = page->mapped_bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
readPtr = (BYTE*)page->lpBytes + offset_in_page;
|
|
|
|
memcpy(writePtr,readPtr,bytes_from_page);
|
|
|
|
BIGBLOCKFILE_ReleaseMappedPage(This, page);
|
|
|
|
|
|
|
|
if (pcbRead)
|
|
|
|
*pcbRead += bytes_from_page;
|
|
|
|
bytes_left -= bytes_from_page;
|
|
|
|
|
|
|
|
if (bytes_left && !eof)
|
|
|
|
{
|
|
|
|
writePtr = (LPBYTE)writePtr + bytes_from_page;
|
|
|
|
page_index ++;
|
|
|
|
offset_in_page = 0;
|
|
|
|
if (bytes_left > PAGE_SIZE)
|
|
|
|
bytes_from_page = PAGE_SIZE;
|
|
|
|
else
|
|
|
|
bytes_from_page = bytes_left;
|
|
|
|
}
|
|
|
|
if (eof)
|
|
|
|
{
|
|
|
|
rc = STG_E_READFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TRACE("finished\n");
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* This method is part of the ILockBytes interface.
|
|
|
|
*
|
|
|
|
* It writes the specified bytes at the specified offset.
|
|
|
|
* position. If the file is too small, it will be resized.
|
|
|
|
*
|
|
|
|
* See the documentation of ILockBytes for more info.
|
|
|
|
*/
|
2008-11-25 16:41:41 +01:00
|
|
|
static HRESULT ImplBIGBLOCKFILE_WriteAt(
|
2007-03-16 19:34:28 +01:00
|
|
|
BigBlockFile* const This,
|
|
|
|
ULARGE_INTEGER ulOffset, /* [in] */
|
|
|
|
const void* pv, /* [size_is][in] */
|
|
|
|
ULONG cb, /* [in] */
|
|
|
|
ULONG* pcbWritten) /* [out] */
|
|
|
|
{
|
|
|
|
ULONG size_needed = ulOffset.u.LowPart + cb;
|
|
|
|
ULONG first_page = ulOffset.u.LowPart / PAGE_SIZE;
|
|
|
|
ULONG offset_in_page = ulOffset.u.LowPart % PAGE_SIZE;
|
|
|
|
ULONG bytes_left = cb;
|
|
|
|
ULONG page_index = first_page;
|
|
|
|
ULONG bytes_to_page;
|
|
|
|
LPCVOID readPtr = pv;
|
|
|
|
|
|
|
|
HRESULT rc = S_OK;
|
|
|
|
|
|
|
|
TRACE("(%p)-> %i %p %i %p\n",This, ulOffset.u.LowPart, pv, cb, pcbWritten);
|
|
|
|
|
2007-04-02 19:17:56 +02:00
|
|
|
/* verify a sane environment */
|
2007-03-16 19:34:28 +01:00
|
|
|
if (!This) return E_FAIL;
|
|
|
|
|
|
|
|
if (This->flProtect != PAGE_READWRITE)
|
|
|
|
return STG_E_ACCESSDENIED;
|
|
|
|
|
|
|
|
if (size_needed > This->filesize.u.LowPart)
|
|
|
|
{
|
|
|
|
ULARGE_INTEGER newSize;
|
|
|
|
newSize.u.HighPart = 0;
|
|
|
|
newSize.u.LowPart = size_needed;
|
|
|
|
BIGBLOCKFILE_SetSize(This, newSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (offset_in_page + bytes_left > PAGE_SIZE)
|
|
|
|
bytes_to_page = PAGE_SIZE - offset_in_page;
|
|
|
|
else
|
|
|
|
bytes_to_page = bytes_left;
|
|
|
|
|
|
|
|
if (pcbWritten)
|
|
|
|
*pcbWritten = 0;
|
|
|
|
|
|
|
|
while (bytes_left)
|
|
|
|
{
|
|
|
|
LPBYTE writePtr;
|
|
|
|
MappedPage *page = BIGBLOCKFILE_GetMappedView(This, page_index);
|
|
|
|
|
|
|
|
TRACE("page %i, offset %u, bytes_to_page %u, bytes_left %u\n",
|
2007-04-03 20:41:42 +02:00
|
|
|
page ? page->page_index : 0, offset_in_page, bytes_to_page, bytes_left);
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2007-04-03 20:41:42 +02:00
|
|
|
if (!page)
|
2007-03-16 19:34:28 +01:00
|
|
|
{
|
2007-04-03 20:41:42 +02:00
|
|
|
ERR("Unable to get a page to write. This should never happen\n");
|
2007-03-16 19:34:28 +01:00
|
|
|
rc = E_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-04-03 20:41:42 +02:00
|
|
|
if (page->mapped_bytes < bytes_to_page)
|
2007-03-16 19:34:28 +01:00
|
|
|
{
|
2007-04-03 20:41:42 +02:00
|
|
|
ERR("Not enough bytes mapped to the page. This should never happen\n");
|
2007-03-16 19:34:28 +01:00
|
|
|
rc = E_FAIL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
writePtr = (BYTE*)page->lpBytes + offset_in_page;
|
|
|
|
memcpy(writePtr,readPtr,bytes_to_page);
|
|
|
|
BIGBLOCKFILE_ReleaseMappedPage(This, page);
|
|
|
|
|
|
|
|
if (pcbWritten)
|
|
|
|
*pcbWritten += bytes_to_page;
|
|
|
|
bytes_left -= bytes_to_page;
|
|
|
|
|
|
|
|
if (bytes_left)
|
|
|
|
{
|
2007-07-06 00:27:24 +02:00
|
|
|
readPtr = (const BYTE *)readPtr + bytes_to_page;
|
2007-03-16 19:34:28 +01:00
|
|
|
page_index ++;
|
|
|
|
offset_in_page = 0;
|
|
|
|
if (bytes_left > PAGE_SIZE)
|
|
|
|
bytes_to_page = PAGE_SIZE;
|
|
|
|
else
|
|
|
|
bytes_to_page = bytes_left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_Construct
|
|
|
|
*
|
|
|
|
* Construct a big block file. Create the file mapping object.
|
|
|
|
* Create the read only mapped pages list, the writable mapped page list
|
|
|
|
* and the blocks in use list.
|
|
|
|
*/
|
|
|
|
BigBlockFile *BIGBLOCKFILE_Construct(HANDLE hFile, ILockBytes* pLkByt, DWORD openFlags,
|
|
|
|
ULONG blocksize, BOOL fileBased)
|
|
|
|
{
|
|
|
|
BigBlockFile *This;
|
|
|
|
|
|
|
|
This = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
|
|
|
|
|
|
|
|
if (This == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
This->fileBased = fileBased;
|
|
|
|
This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
|
|
|
|
This->blocksize = blocksize;
|
|
|
|
|
|
|
|
This->maplist = NULL;
|
|
|
|
This->victimhead = NULL;
|
|
|
|
This->victimtail = NULL;
|
|
|
|
This->num_victim_pages = 0;
|
|
|
|
|
|
|
|
if (This->fileBased)
|
|
|
|
{
|
|
|
|
if (!BIGBLOCKFILE_FileInit(This, hFile))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-01-27 18:35:18 +01:00
|
|
|
if (!BIGBLOCKFILE_LockBytesInit(This, pLkByt))
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return This;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_Destructor
|
|
|
|
*
|
|
|
|
* Destructor. Clean up, free memory.
|
|
|
|
*/
|
|
|
|
void BIGBLOCKFILE_Destructor(BigBlockFile *This)
|
|
|
|
{
|
|
|
|
BIGBLOCKFILE_FreeAllMappedPages(This);
|
2007-03-16 19:34:28 +01:00
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
if (This->fileBased)
|
|
|
|
{
|
|
|
|
CloseHandle(This->hfilemap);
|
|
|
|
CloseHandle(This->hfile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ILockBytes_Release(This->pLkbyt);
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_ReadAt
|
|
|
|
*/
|
|
|
|
HRESULT BIGBLOCKFILE_ReadAt(BigBlockFile *This, ULARGE_INTEGER offset,
|
|
|
|
void* buffer, ULONG size, ULONG* bytesRead)
|
2007-03-16 19:34:28 +01:00
|
|
|
{
|
|
|
|
if (This->fileBased)
|
|
|
|
return ImplBIGBLOCKFILE_ReadAt(This,offset,buffer,size,bytesRead);
|
|
|
|
else
|
|
|
|
return ILockBytes_ReadAt(This->pLkbyt,offset,buffer,size,bytesRead);
|
|
|
|
}
|
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_WriteAt
|
|
|
|
*/
|
|
|
|
HRESULT BIGBLOCKFILE_WriteAt(BigBlockFile *This, ULARGE_INTEGER offset,
|
|
|
|
const void* buffer, ULONG size, ULONG* bytesRead)
|
2007-03-16 19:34:28 +01:00
|
|
|
{
|
|
|
|
if (This->fileBased)
|
|
|
|
return ImplBIGBLOCKFILE_WriteAt(This,offset,buffer,size,bytesRead);
|
|
|
|
else
|
|
|
|
return ILockBytes_WriteAt(This->pLkbyt,offset,buffer,size,bytesRead);
|
|
|
|
}
|
2009-01-27 18:03:55 +01:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_SetSize
|
|
|
|
*
|
|
|
|
* Sets the size of the file.
|
|
|
|
*
|
|
|
|
*/
|
2009-01-27 18:13:28 +01:00
|
|
|
HRESULT BIGBLOCKFILE_SetSize(BigBlockFile *This, ULARGE_INTEGER newSize)
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
2009-01-27 18:13:28 +01:00
|
|
|
HRESULT hr = S_OK;
|
2009-01-27 18:35:18 +01:00
|
|
|
LARGE_INTEGER newpos;
|
|
|
|
|
|
|
|
if (!This->fileBased)
|
|
|
|
return ILockBytes_SetSize(This->pLkbyt, newSize);
|
2009-01-27 18:13:28 +01:00
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
if (This->filesize.u.LowPart == newSize.u.LowPart)
|
2009-01-27 18:13:28 +01:00
|
|
|
return hr;
|
2009-01-27 18:03:55 +01:00
|
|
|
|
|
|
|
TRACE("from %u to %u\n", This->filesize.u.LowPart, newSize.u.LowPart);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unmap all views, must be done before call to SetEndFile.
|
|
|
|
*
|
|
|
|
* Just ditch the victim list because there is no guarantee we will need them
|
|
|
|
* and it is not worth the performance hit to unmap and remap them all.
|
|
|
|
*/
|
|
|
|
BIGBLOCKFILE_DeleteList(This, This->victimhead);
|
|
|
|
This->victimhead = NULL;
|
|
|
|
This->victimtail = NULL;
|
|
|
|
This->num_victim_pages = 0;
|
|
|
|
|
|
|
|
BIGBLOCKFILE_UnmapAllMappedPages(This);
|
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
newpos.QuadPart = newSize.QuadPart;
|
|
|
|
if (SetFilePointerEx(This->hfile, newpos, NULL, FILE_BEGIN))
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
2009-01-27 18:35:18 +01:00
|
|
|
if( This->hfilemap ) CloseHandle(This->hfilemap);
|
2009-01-27 18:03:55 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
SetEndOfFile(This->hfile);
|
2009-01-27 18:03:55 +01:00
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
/* re-create the file mapping object */
|
|
|
|
This->hfilemap = CreateFileMappingA(This->hfile, NULL, This->flProtect,
|
|
|
|
0, 0, NULL);
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|
|
|
|
|
2009-01-27 18:35:18 +01:00
|
|
|
This->filesize = newSize;
|
2009-01-27 18:03:55 +01:00
|
|
|
BIGBLOCKFILE_RemapAllMappedPages(This);
|
2009-01-27 18:13:28 +01:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetSize
|
|
|
|
*
|
|
|
|
* Gets the size of the file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static HRESULT BIGBLOCKFILE_GetSize(BigBlockFile *This, ULARGE_INTEGER *size)
|
|
|
|
{
|
|
|
|
HRESULT hr = S_OK;
|
2009-01-27 18:35:18 +01:00
|
|
|
if(This->fileBased)
|
|
|
|
*size = This->filesize;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
STATSTG stat;
|
|
|
|
hr = ILockBytes_Stat(This->pLkbyt, &stat, STATFLAG_NONAME);
|
|
|
|
if(SUCCEEDED(hr)) *size = stat.cbSize;
|
|
|
|
}
|
2009-01-27 18:13:28 +01:00
|
|
|
return hr;
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_EnsureExists
|
|
|
|
*
|
|
|
|
* Grows the file if necessary to make sure the block is valid.
|
|
|
|
*/
|
2009-01-27 18:13:28 +01:00
|
|
|
HRESULT BIGBLOCKFILE_EnsureExists(BigBlockFile *This, ULONG index)
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
2009-01-27 18:13:28 +01:00
|
|
|
ULARGE_INTEGER size;
|
|
|
|
HRESULT hr;
|
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
/* Block index starts at -1 translate to zero based index */
|
|
|
|
if (index == 0xffffffff)
|
|
|
|
index = 0;
|
|
|
|
else
|
|
|
|
index++;
|
|
|
|
|
2009-01-27 18:13:28 +01:00
|
|
|
hr = BIGBLOCKFILE_GetSize(This, &size);
|
|
|
|
if(FAILED(hr)) return hr;
|
|
|
|
|
2009-01-27 18:03:55 +01:00
|
|
|
/* make sure that the block physically exists */
|
2009-01-27 18:13:28 +01:00
|
|
|
if ((This->blocksize * (index + 1)) > size.QuadPart)
|
2009-01-27 18:03:55 +01:00
|
|
|
{
|
|
|
|
ULARGE_INTEGER newSize;
|
|
|
|
|
2009-01-27 18:13:28 +01:00
|
|
|
newSize.QuadPart = This->blocksize * (index + 1);
|
|
|
|
hr = BIGBLOCKFILE_SetSize(This, newSize);
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|
2009-01-27 18:13:28 +01:00
|
|
|
return hr;
|
2009-01-27 18:03:55 +01:00
|
|
|
}
|