1999-01-30 14:24:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* BigBlockFile
|
|
|
|
*
|
|
|
|
* This is the implementation of a file that consists of blocks of
|
|
|
|
* a predetermined size.
|
|
|
|
* 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
|
|
|
|
* obtaining the size of the file.
|
|
|
|
* The blocks are indexed sequentially from the start of the file
|
|
|
|
* starting with -1.
|
|
|
|
*
|
|
|
|
* TODO:
|
|
|
|
* - Support for a transacted mode
|
|
|
|
*
|
|
|
|
* Copyright 1999 Thuy Nguyen
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1999-02-28 20:14:33 +01:00
|
|
|
#include "winbase.h"
|
1999-01-30 14:24:23 +01:00
|
|
|
#include "winerror.h"
|
1999-02-02 17:14:23 +01:00
|
|
|
#include "wine/obj_storage.h"
|
1999-05-08 12:05:08 +02:00
|
|
|
#include "ole2.h"
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
#include "storage32.h"
|
|
|
|
|
1999-07-04 18:02:24 +02:00
|
|
|
#include "debugtools.h"
|
1999-05-08 12:05:08 +02:00
|
|
|
|
|
|
|
DEFAULT_DEBUG_CHANNEL(storage)
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/***********************************************************
|
|
|
|
* Data structures used internally by the BigBlockFile
|
|
|
|
* class.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/***
|
|
|
|
* Itdentifies a single big block and the related
|
|
|
|
* information
|
|
|
|
*/
|
|
|
|
struct BigBlock
|
|
|
|
{
|
|
|
|
BigBlock * next;
|
|
|
|
DWORD index;
|
|
|
|
DWORD access_mode;
|
|
|
|
LPVOID lpBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
|
|
|
* This structure identifies the paged that are mapped
|
|
|
|
* from the file and their position in memory. It is
|
|
|
|
* also used to hold a reference count to those pages.
|
|
|
|
*/
|
|
|
|
struct MappedPage
|
|
|
|
{
|
|
|
|
MappedPage * next;
|
|
|
|
DWORD number;
|
|
|
|
int ref;
|
|
|
|
LPVOID lpBytes;
|
|
|
|
};
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
#define PAGE_SIZE 131072
|
|
|
|
#define BLOCKS_PER_PAGE 256
|
|
|
|
|
|
|
|
#define NUMBER_OF_MAPPED_PAGES 100
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
/***********************************************************
|
|
|
|
* Prototypes for private methods
|
|
|
|
*/
|
|
|
|
static void* BIGBLOCKFILE_GetMappedView(LPBIGBLOCKFILE This,
|
|
|
|
DWORD pagenum,
|
1999-03-12 18:06:46 +01:00
|
|
|
DWORD desired_access);
|
|
|
|
static void BIGBLOCKFILE_ReleaseMappedPage(LPBIGBLOCKFILE This,
|
|
|
|
DWORD pagenum,
|
|
|
|
DWORD access);
|
1999-01-30 14:24:23 +01:00
|
|
|
static void BIGBLOCKFILE_FreeAllMappedPages(LPBIGBLOCKFILE This);
|
|
|
|
static void* BIGBLOCKFILE_GetBigBlockPointer(LPBIGBLOCKFILE This,
|
1999-03-12 18:06:46 +01:00
|
|
|
ULONG index,
|
|
|
|
DWORD desired_access);
|
1999-01-30 14:24:23 +01:00
|
|
|
static BigBlock* BIGBLOCKFILE_GetBigBlockFromPointer(LPBIGBLOCKFILE This,
|
1999-03-12 18:06:46 +01:00
|
|
|
void* pBlock);
|
1999-01-30 14:24:23 +01:00
|
|
|
static void BIGBLOCKFILE_RemoveBlock(LPBIGBLOCKFILE This,
|
1999-03-12 18:06:46 +01:00
|
|
|
ULONG index);
|
1999-01-30 14:24:23 +01:00
|
|
|
static BigBlock* BIGBLOCKFILE_AddBigBlock(LPBIGBLOCKFILE This,
|
1999-03-12 18:06:46 +01:00
|
|
|
ULONG index);
|
1999-01-30 14:24:23 +01:00
|
|
|
static BigBlock* BIGBLOCKFILE_CreateBlock(ULONG index);
|
1999-03-12 18:06:46 +01:00
|
|
|
static DWORD BIGBLOCKFILE_GetProtectMode(DWORD openFlags);
|
1999-05-08 12:05:08 +02:00
|
|
|
static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile);
|
|
|
|
static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt);
|
|
|
|
static void BIGBLOCKFILE_RemoveAllBlocks(LPBIGBLOCKFILE This);
|
1999-02-18 16:55:53 +01:00
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_Construct
|
|
|
|
*
|
|
|
|
* Construct a big block file. Create the file mapping object.
|
|
|
|
* Create the read only mapped pages list, the writeable mapped page list
|
|
|
|
* and the blocks in use list.
|
|
|
|
*/
|
|
|
|
BigBlockFile * BIGBLOCKFILE_Construct(
|
1999-05-08 12:05:08 +02:00
|
|
|
HANDLE hFile,
|
|
|
|
ILockBytes* pLkByt,
|
1999-03-12 18:06:46 +01:00
|
|
|
DWORD openFlags,
|
1999-05-08 12:05:08 +02:00
|
|
|
ULONG blocksize,
|
|
|
|
BOOL fileBased)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
|
|
|
LPBIGBLOCKFILE This;
|
|
|
|
|
|
|
|
This = (LPBIGBLOCKFILE)HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlockFile));
|
|
|
|
|
|
|
|
if (This == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
This->fileBased = fileBased;
|
|
|
|
|
1999-05-12 12:36:17 +02:00
|
|
|
This->flProtect = BIGBLOCKFILE_GetProtectMode(openFlags);
|
|
|
|
|
|
|
|
This->blocksize = blocksize;
|
|
|
|
|
|
|
|
/* initialize the block list
|
|
|
|
*/
|
|
|
|
This->headblock = NULL;
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
if (This->fileBased)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
1999-05-08 12:05:08 +02:00
|
|
|
if (!BIGBLOCKFILE_FileInit(This, hFile))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!BIGBLOCKFILE_MemInit(This, pLkByt))
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
return This;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_FileInit
|
|
|
|
*
|
|
|
|
* Initialize a big block object supported by a file.
|
|
|
|
*/
|
|
|
|
static BOOL BIGBLOCKFILE_FileInit(LPBIGBLOCKFILE This, HANDLE hFile)
|
|
|
|
{
|
|
|
|
This->pLkbyt = NULL;
|
|
|
|
This->hbytearray = 0;
|
|
|
|
This->pbytearray = NULL;
|
|
|
|
|
|
|
|
This->hfile = hFile;
|
|
|
|
|
|
|
|
if (This->hfile == INVALID_HANDLE_VALUE)
|
|
|
|
return FALSE;
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/* create the file mapping object
|
|
|
|
*/
|
1999-02-26 12:11:13 +01:00
|
|
|
This->hfilemap = CreateFileMappingA(This->hfile,
|
1999-05-08 12:05:08 +02:00
|
|
|
NULL,
|
|
|
|
This->flProtect,
|
|
|
|
0, 0,
|
|
|
|
NULL);
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
if (This->hfilemap == NULL)
|
|
|
|
{
|
|
|
|
CloseHandle(This->hfile);
|
1999-05-08 12:05:08 +02:00
|
|
|
return FALSE;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-09-19 16:20:33 +02:00
|
|
|
This->filesize.s.LowPart = GetFileSize(This->hfile, NULL);
|
1999-05-08 12:05:08 +02:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
/* create the mapped pages list
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-03-12 18:06:46 +01:00
|
|
|
This->maplisthead = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
if (This->maplisthead == NULL)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
|
|
|
CloseHandle(This->hfilemap);
|
|
|
|
CloseHandle(This->hfile);
|
1999-05-08 12:05:08 +02:00
|
|
|
return FALSE;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
This->maplisthead->next = NULL;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_MemInit
|
|
|
|
*
|
|
|
|
* Initialize a big block object supported by an ILockBytes on HGLOABL.
|
|
|
|
*/
|
|
|
|
static BOOL BIGBLOCKFILE_MemInit(LPBIGBLOCKFILE This, ILockBytes* plkbyt)
|
|
|
|
{
|
|
|
|
This->hfile = 0;
|
|
|
|
This->hfilemap = NULL;
|
|
|
|
This->maplisthead = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Retrieve the handle to the byte array from the LockByte object.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-05-08 12:05:08 +02:00
|
|
|
if (GetHGlobalFromILockBytes(plkbyt, &(This->hbytearray)) != S_OK)
|
|
|
|
{
|
1999-07-04 18:02:24 +02:00
|
|
|
FIXME("May not be an ILockBytes on HGLOBAL\n");
|
1999-05-08 12:05:08 +02:00
|
|
|
return FALSE;
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
This->pLkbyt = plkbyt;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment the reference count of the ILockByte object since
|
|
|
|
* we're keeping a reference to it.
|
|
|
|
*/
|
|
|
|
ILockBytes_AddRef(This->pLkbyt);
|
|
|
|
|
1999-09-19 16:20:33 +02:00
|
|
|
This->filesize.s.LowPart = GlobalSize(This->hbytearray);
|
|
|
|
This->filesize.s.HighPart = 0;
|
1999-05-08 12:05:08 +02:00
|
|
|
|
|
|
|
This->pbytearray = GlobalLock(This->hbytearray);
|
|
|
|
|
|
|
|
return TRUE;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_Destructor
|
|
|
|
*
|
|
|
|
* Destructor. Clean up, free memory.
|
|
|
|
*/
|
|
|
|
void BIGBLOCKFILE_Destructor(
|
|
|
|
LPBIGBLOCKFILE This)
|
|
|
|
{
|
1999-05-08 12:05:08 +02:00
|
|
|
if (This->fileBased)
|
|
|
|
{
|
|
|
|
/* unmap all views and destroy the mapped page list
|
|
|
|
*/
|
|
|
|
BIGBLOCKFILE_FreeAllMappedPages(This);
|
|
|
|
HeapFree(GetProcessHeap(), 0, This->maplisthead);
|
|
|
|
|
|
|
|
/* close all open handles
|
|
|
|
*/
|
|
|
|
CloseHandle(This->hfilemap);
|
|
|
|
CloseHandle(This->hfile);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GlobalUnlock(This->hbytearray);
|
|
|
|
ILockBytes_Release(This->pLkbyt);
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/*
|
|
|
|
* Destroy the blocks list.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-05-08 12:05:08 +02:00
|
|
|
BIGBLOCKFILE_RemoveAllBlocks(This);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
/* destroy this
|
|
|
|
*/
|
|
|
|
HeapFree(GetProcessHeap(), 0, This);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetROBigBlock
|
|
|
|
*
|
|
|
|
* Returns the specified block in read only mode.
|
|
|
|
* Will return NULL if the block doesn't exists.
|
|
|
|
*/
|
|
|
|
void* BIGBLOCKFILE_GetROBigBlock(
|
|
|
|
LPBIGBLOCKFILE This,
|
|
|
|
ULONG index)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* block index starts at -1
|
|
|
|
* translate to zero based index
|
|
|
|
*/
|
|
|
|
if (index == 0xffffffff)
|
|
|
|
index = 0;
|
|
|
|
else
|
|
|
|
index++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* validate the block index
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
if ((This->blocksize * (index + 1)) >
|
1999-09-19 16:20:33 +02:00
|
|
|
(This->filesize.s.LowPart +
|
|
|
|
(This->blocksize - (This->filesize.s.LowPart % This->blocksize))))
|
1999-01-30 14:24:23 +01:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_READ);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetBigBlock
|
|
|
|
*
|
|
|
|
* Returns the specified block.
|
|
|
|
* Will grow the file if necessary.
|
|
|
|
*/
|
|
|
|
void* BIGBLOCKFILE_GetBigBlock(LPBIGBLOCKFILE This, ULONG index)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* block index starts at -1
|
|
|
|
* translate to zero based index
|
|
|
|
*/
|
|
|
|
if (index == 0xffffffff)
|
|
|
|
index = 0;
|
|
|
|
else
|
|
|
|
index++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* make sure that the block physically exists
|
|
|
|
*/
|
1999-09-19 16:20:33 +02:00
|
|
|
if ((This->blocksize * (index + 1)) > This->filesize.s.LowPart)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
|
|
|
ULARGE_INTEGER newSize;
|
|
|
|
|
1999-09-19 16:20:33 +02:00
|
|
|
newSize.s.HighPart = 0;
|
|
|
|
newSize.s.LowPart = This->blocksize * (index + 1);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
BIGBLOCKFILE_SetSize(This, newSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return BIGBLOCKFILE_GetBigBlockPointer(This, index, FILE_MAP_WRITE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_ReleaseBigBlock
|
|
|
|
*
|
|
|
|
* Releases the specified block.
|
|
|
|
*/
|
|
|
|
void BIGBLOCKFILE_ReleaseBigBlock(LPBIGBLOCKFILE This, void *pBlock)
|
|
|
|
{
|
|
|
|
DWORD page_num;
|
|
|
|
BigBlock* theBigBlock;
|
|
|
|
|
|
|
|
if (pBlock == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get the block from the block list
|
|
|
|
*/
|
|
|
|
theBigBlock = BIGBLOCKFILE_GetBigBlockFromPointer(This, pBlock);
|
|
|
|
|
|
|
|
if (theBigBlock == NULL)
|
|
|
|
return;
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
if (This->fileBased)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* find out which page this block is in
|
|
|
|
*/
|
|
|
|
page_num = theBigBlock->index / BLOCKS_PER_PAGE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* release this page
|
|
|
|
*/
|
|
|
|
BIGBLOCKFILE_ReleaseMappedPage(This, page_num, theBigBlock->access_mode);
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* remove block from list
|
|
|
|
*/
|
|
|
|
BIGBLOCKFILE_RemoveBlock(This, theBigBlock->index);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_SetSize
|
|
|
|
*
|
|
|
|
* Sets the size of the file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void BIGBLOCKFILE_SetSize(LPBIGBLOCKFILE This, ULARGE_INTEGER newSize)
|
|
|
|
{
|
1999-09-19 16:20:33 +02:00
|
|
|
if (This->filesize.s.LowPart == newSize.s.LowPart)
|
1999-01-30 14:24:23 +01:00
|
|
|
return;
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
if (This->fileBased)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* unmap all views, must be done before call to SetEndFile
|
|
|
|
*/
|
|
|
|
BIGBLOCKFILE_FreeAllMappedPages(This);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* close file-mapping object, must be done before call to SetEndFile
|
|
|
|
*/
|
|
|
|
CloseHandle(This->hfilemap);
|
|
|
|
This->hfilemap = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* set the new end of file
|
|
|
|
*/
|
1999-09-19 16:20:33 +02:00
|
|
|
SetFilePointer(This->hfile, newSize.s.LowPart, NULL, FILE_BEGIN);
|
1999-05-08 12:05:08 +02:00
|
|
|
SetEndOfFile(This->hfile);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* re-create the file mapping object
|
|
|
|
*/
|
|
|
|
This->hfilemap = CreateFileMappingA(This->hfile,
|
|
|
|
NULL,
|
|
|
|
This->flProtect,
|
|
|
|
0, 0,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
GlobalUnlock(This->hbytearray);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/*
|
|
|
|
* Resize the byte array object.
|
|
|
|
*/
|
|
|
|
ILockBytes_SetSize(This->pLkbyt, newSize);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/*
|
|
|
|
* Re-acquire the handle, it may have changed.
|
|
|
|
*/
|
|
|
|
GetHGlobalFromILockBytes(This->pLkbyt, &This->hbytearray);
|
|
|
|
This->pbytearray = GlobalLock(This->hbytearray);
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
/*
|
1999-05-08 12:05:08 +02:00
|
|
|
* empty the blocks list.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-05-08 12:05:08 +02:00
|
|
|
BIGBLOCKFILE_RemoveAllBlocks(This);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-09-19 16:20:33 +02:00
|
|
|
This->filesize.s.LowPart = newSize.s.LowPart;
|
|
|
|
This->filesize.s.HighPart = newSize.s.HighPart;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetSize
|
|
|
|
*
|
|
|
|
* Returns the size of the file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
ULARGE_INTEGER BIGBLOCKFILE_GetSize(LPBIGBLOCKFILE This)
|
|
|
|
{
|
|
|
|
return This->filesize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetBigBlockPointer [PRIVATE]
|
|
|
|
*
|
|
|
|
* Returns a pointer to the specified block.
|
|
|
|
*/
|
|
|
|
static void* BIGBLOCKFILE_GetBigBlockPointer(
|
|
|
|
LPBIGBLOCKFILE This,
|
|
|
|
ULONG index,
|
|
|
|
DWORD desired_access)
|
|
|
|
{
|
1999-05-08 12:05:08 +02:00
|
|
|
DWORD block_num;
|
1999-01-30 14:24:23 +01:00
|
|
|
void * pBytes;
|
|
|
|
BigBlock *aBigBlock;
|
|
|
|
|
|
|
|
/* get the big block from the list or add it to the list
|
|
|
|
*/
|
|
|
|
aBigBlock = BIGBLOCKFILE_AddBigBlock(This, index);
|
|
|
|
|
|
|
|
if (aBigBlock == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* we already have an address for this block
|
|
|
|
*/
|
|
|
|
if (aBigBlock->lpBlock != NULL)
|
|
|
|
{
|
|
|
|
/* make sure the desired access matches what we already have
|
|
|
|
*/
|
|
|
|
if (aBigBlock->access_mode == desired_access)
|
|
|
|
return aBigBlock->lpBlock;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* else aBigBlock->lpBigBlock == NULL, it's a new block
|
|
|
|
*/
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
if (This->fileBased)
|
|
|
|
{
|
|
|
|
DWORD page_num;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/* find out which page this block is in
|
|
|
|
*/
|
|
|
|
page_num = index / BLOCKS_PER_PAGE;
|
|
|
|
|
|
|
|
/* offset of the block in the page
|
|
|
|
*/
|
|
|
|
block_num = index % BLOCKS_PER_PAGE;
|
|
|
|
|
|
|
|
/* get a pointer to the first byte in the page
|
|
|
|
*/
|
|
|
|
pBytes = BIGBLOCKFILE_GetMappedView(This, page_num, desired_access);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pBytes = This->pbytearray;
|
|
|
|
block_num = index;
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
if (pBytes == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* initialize block
|
|
|
|
*/
|
|
|
|
aBigBlock->lpBlock = ((BYTE*)pBytes + (block_num*This->blocksize));
|
|
|
|
aBigBlock->access_mode = desired_access;
|
|
|
|
|
|
|
|
return aBigBlock->lpBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_CreateBlock [PRIVATE]
|
|
|
|
*
|
|
|
|
* Creates a node of the blocks list.
|
|
|
|
*/
|
|
|
|
static BigBlock* BIGBLOCKFILE_CreateBlock(
|
|
|
|
ULONG index)
|
|
|
|
{
|
|
|
|
BigBlock *newBigBlock;
|
|
|
|
|
|
|
|
/* create new list node
|
|
|
|
*/
|
|
|
|
newBigBlock = HeapAlloc(GetProcessHeap(), 0, sizeof(BigBlock));
|
|
|
|
|
|
|
|
if (newBigBlock == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* initialize node
|
|
|
|
*/
|
|
|
|
newBigBlock->index = index;
|
|
|
|
newBigBlock->lpBlock = NULL;
|
|
|
|
|
|
|
|
return newBigBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_AddBigBlock [PRIVATE]
|
|
|
|
*
|
|
|
|
* Returns the specified block from the blocks list.
|
|
|
|
* If the block is not found in the list, we will create it and add it to the
|
|
|
|
* list.
|
|
|
|
*/
|
|
|
|
static BigBlock* BIGBLOCKFILE_AddBigBlock(
|
|
|
|
LPBIGBLOCKFILE This,
|
|
|
|
ULONG index)
|
|
|
|
{
|
|
|
|
BigBlock *current = This->headblock;
|
|
|
|
BigBlock *newBigBlock;
|
|
|
|
|
|
|
|
if (current == NULL) /* empty list */
|
|
|
|
{
|
|
|
|
newBigBlock = BIGBLOCKFILE_CreateBlock(index);
|
|
|
|
|
|
|
|
if (newBigBlock != NULL)
|
|
|
|
{
|
|
|
|
newBigBlock->next = NULL;
|
|
|
|
This->headblock = newBigBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBigBlock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* special handling for head of the list
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (current->index == index) /* it's already here */
|
|
|
|
return current;
|
|
|
|
else if (current->index > index) /* insertion at head of the list */
|
|
|
|
{
|
|
|
|
newBigBlock = BIGBLOCKFILE_CreateBlock(index);
|
|
|
|
|
|
|
|
if (newBigBlock != NULL)
|
|
|
|
{
|
|
|
|
newBigBlock->next = current;
|
|
|
|
This->headblock = newBigBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBigBlock;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* iterate through rest the list
|
|
|
|
*/
|
|
|
|
while (current->next != NULL)
|
|
|
|
{
|
|
|
|
if (current->next->index == index) /* found it */
|
|
|
|
{
|
|
|
|
return current->next;
|
|
|
|
}
|
|
|
|
else if (current->next->index > index) /* it's not in the list */
|
|
|
|
{
|
|
|
|
newBigBlock = BIGBLOCKFILE_CreateBlock(index);
|
|
|
|
|
|
|
|
if (newBigBlock != NULL)
|
|
|
|
{
|
|
|
|
newBigBlock->next = current->next;
|
|
|
|
current->next = newBigBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBigBlock;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* insertion at end of the list
|
|
|
|
*/
|
|
|
|
if (current->next == NULL)
|
|
|
|
{
|
|
|
|
newBigBlock = BIGBLOCKFILE_CreateBlock(index);
|
|
|
|
|
|
|
|
if (newBigBlock != NULL)
|
|
|
|
{
|
|
|
|
newBigBlock->next = NULL;
|
|
|
|
current->next = newBigBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return newBigBlock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1999-05-08 12:05:08 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_RemoveAllBlocks [PRIVATE]
|
|
|
|
*
|
|
|
|
* Removes all blocks from the blocks list.
|
|
|
|
*/
|
|
|
|
static void BIGBLOCKFILE_RemoveAllBlocks(
|
|
|
|
LPBIGBLOCKFILE This)
|
|
|
|
{
|
|
|
|
BigBlock *current;
|
|
|
|
|
|
|
|
while (This->headblock != NULL)
|
|
|
|
{
|
|
|
|
current = This->headblock;
|
|
|
|
This->headblock = current->next;
|
|
|
|
HeapFree(GetProcessHeap(), 0, current);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_RemoveBlock [PRIVATE]
|
|
|
|
*
|
|
|
|
* Removes the specified block from the blocks list.
|
|
|
|
*/
|
|
|
|
static void BIGBLOCKFILE_RemoveBlock(
|
|
|
|
LPBIGBLOCKFILE This,
|
|
|
|
ULONG index)
|
|
|
|
{
|
|
|
|
BigBlock *current = This->headblock;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* empty list
|
|
|
|
*/
|
|
|
|
if (current == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
*special case: removing head of list
|
|
|
|
*/
|
|
|
|
if (current->index == index)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* set new head free the old one
|
|
|
|
*/
|
|
|
|
This->headblock = current->next;
|
|
|
|
HeapFree(GetProcessHeap(), 0, current);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* iterate through rest of the list
|
|
|
|
*/
|
|
|
|
while (current->next != NULL)
|
|
|
|
{
|
|
|
|
if (current->next->index == index) /* found it */
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* unlink the block and free the block
|
|
|
|
*/
|
|
|
|
current->next = current->next->next;
|
|
|
|
HeapFree(GetProcessHeap(), 0, current->next);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* next node
|
|
|
|
*/
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* BIGBLOCKFILE_GetBigBlockFromPointer [PRIVATE]
|
|
|
|
*
|
|
|
|
* Given a block pointer, this will return the corresponding block
|
|
|
|
* from the blocks list.
|
|
|
|
*/
|
|
|
|
static BigBlock* BIGBLOCKFILE_GetBigBlockFromPointer(
|
|
|
|
LPBIGBLOCKFILE This,
|
|
|
|
void* pBlock)
|
|
|
|
{
|
|
|
|
BigBlock *current = This->headblock;
|
|
|
|
|
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
if (current->lpBlock == pBlock)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* 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,
|
1999-03-12 18:06:46 +01:00
|
|
|
DWORD pagenum,
|
1999-01-30 14:24:23 +01:00
|
|
|
DWORD desired_access)
|
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
MappedPage* current = This->maplisthead;
|
|
|
|
ULONG count = 1;
|
|
|
|
BOOL found = FALSE;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
assert(This->maplisthead != NULL);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
/*
|
|
|
|
* Search for the page in the list.
|
|
|
|
*/
|
|
|
|
while ((found == FALSE) && (current->next != NULL))
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
if (current->next->number == pagenum)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
found = TRUE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If it's not already at the head of the list
|
|
|
|
* move it there.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-03-12 18:06:46 +01:00
|
|
|
if (current != This->maplisthead)
|
|
|
|
{
|
|
|
|
MappedPage* temp = current->next;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
current->next = current->next->next;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
temp->next = This->maplisthead->next;
|
|
|
|
This->maplisthead->next = temp;
|
|
|
|
}
|
|
|
|
}
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
/*
|
|
|
|
* The list is full and we haven't found it.
|
|
|
|
* Free the last element of the list because we'll add a new
|
|
|
|
* one at the head.
|
|
|
|
*/
|
|
|
|
if ((found == FALSE) &&
|
|
|
|
(count >= NUMBER_OF_MAPPED_PAGES) &&
|
|
|
|
(current->next != NULL))
|
|
|
|
{
|
|
|
|
UnmapViewOfFile(current->next->lpBytes);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, current->next);
|
|
|
|
current->next = NULL;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
1999-03-12 18:06:46 +01:00
|
|
|
|
|
|
|
if (current->next != NULL)
|
1999-01-30 14:24:23 +01:00
|
|
|
current = current->next;
|
1999-03-12 18:06:46 +01:00
|
|
|
|
|
|
|
count++;
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
/*
|
|
|
|
* Add the page at the head of the list.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-03-12 18:06:46 +01:00
|
|
|
if (found == FALSE)
|
1999-01-30 14:24:23 +01:00
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
MappedPage* newMappedPage;
|
|
|
|
DWORD numBytesToMap;
|
|
|
|
DWORD hioffset = 0;
|
|
|
|
DWORD lowoffset = PAGE_SIZE * pagenum;
|
|
|
|
|
1999-01-30 14:24:23 +01:00
|
|
|
newMappedPage = HeapAlloc(GetProcessHeap(), 0, sizeof(MappedPage));
|
|
|
|
|
|
|
|
if (newMappedPage == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
newMappedPage->number = pagenum;
|
1999-03-12 18:06:46 +01:00
|
|
|
newMappedPage->ref = 0;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
newMappedPage->next = This->maplisthead->next;
|
|
|
|
This->maplisthead->next = newMappedPage;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-09-19 16:20:33 +02:00
|
|
|
if (((pagenum + 1) * PAGE_SIZE) > This->filesize.s.LowPart)
|
|
|
|
numBytesToMap = This->filesize.s.LowPart - (pagenum * PAGE_SIZE);
|
1999-01-30 14:24:23 +01:00
|
|
|
else
|
1999-02-24 11:52:26 +01:00
|
|
|
numBytesToMap = PAGE_SIZE;
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
if (This->flProtect == PAGE_READONLY)
|
|
|
|
desired_access = FILE_MAP_READ;
|
|
|
|
else
|
|
|
|
desired_access = FILE_MAP_WRITE;
|
|
|
|
|
1999-02-24 11:52:26 +01:00
|
|
|
newMappedPage->lpBytes = MapViewOfFile(This->hfilemap,
|
|
|
|
desired_access,
|
|
|
|
hioffset,
|
|
|
|
lowoffset,
|
|
|
|
numBytesToMap);
|
1999-01-30 14:24:23 +01:00
|
|
|
}
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
/*
|
|
|
|
* The page we want should now be at the head of the list.
|
|
|
|
*/
|
|
|
|
assert(This->maplisthead->next != NULL);
|
|
|
|
|
|
|
|
current = This->maplisthead->next;
|
|
|
|
current->ref++;
|
|
|
|
|
|
|
|
return current->lpBytes;
|
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,
|
|
|
|
DWORD pagenum,
|
|
|
|
DWORD access)
|
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
MappedPage* previous = This->maplisthead;
|
|
|
|
MappedPage* current;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
assert(This->maplisthead->next != NULL);
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
current = previous->next;
|
|
|
|
|
|
|
|
/* search for the page in the list
|
|
|
|
*/
|
|
|
|
while (current != NULL)
|
|
|
|
{
|
|
|
|
if (current->number == pagenum)
|
|
|
|
{
|
|
|
|
/* decrement the reference count
|
|
|
|
*/
|
|
|
|
current->ref--;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
previous = current;
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* 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)
|
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
MappedPage * current = This->maplisthead->next;
|
1999-01-30 14:24:23 +01:00
|
|
|
|
|
|
|
while (current != NULL)
|
|
|
|
{
|
1999-03-12 18:06:46 +01:00
|
|
|
/* Unmap views.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
|
|
|
UnmapViewOfFile(current->lpBytes);
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
/* Free the nodes.
|
1999-01-30 14:24:23 +01:00
|
|
|
*/
|
1999-03-12 18:06:46 +01:00
|
|
|
This->maplisthead->next = current->next;
|
1999-01-30 14:24:23 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, current);
|
|
|
|
|
1999-03-12 18:06:46 +01:00
|
|
|
current = This->maplisthead->next;
|
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)
|
|
|
|
{
|
|
|
|
DWORD flProtect = PAGE_READONLY;
|
1999-02-26 12:11:13 +01:00
|
|
|
BOOL bSTGM_WRITE = ((openFlags & STGM_WRITE) == STGM_WRITE);
|
|
|
|
BOOL bSTGM_READWRITE = ((openFlags & STGM_READWRITE) == STGM_READWRITE);
|
|
|
|
BOOL bSTGM_READ = ! (bSTGM_WRITE || bSTGM_READWRITE);
|
1999-02-18 16:55:53 +01:00
|
|
|
|
|
|
|
if (bSTGM_READ)
|
|
|
|
flProtect = PAGE_READONLY;
|
|
|
|
|
|
|
|
if ((bSTGM_WRITE) || (bSTGM_READWRITE))
|
|
|
|
flProtect = PAGE_READWRITE;
|
|
|
|
|
|
|
|
return flProtect;
|
|
|
|
}
|
|
|
|
|
|
|
|
|