📄 freeotfe4pdacontextmgropen.c
字号:
// Description:
// By Sarah Dean
// Email: sdean12@sdean12.org
// WWW: http://www.FreeOTFE.org/
//
// -----------------------------------------------------------------------------
//
// Originally implemented as a dynamic array, changed to use a linked list
// in order to minimise memory usage; in particular with the open file
// handles, which could result in a large (mainly unused) array if a large
// number of file handles were opened on the device, followed by all except
// the *last* being closed again.
#include "FreeOTFE4PDA.h"
#include "FreeOTFE4PDAContextMgrOpen.h"
#include "FreeOTFEDebug.h"
#include "SDUGeneral.h"
#include "FreeOTFElib.h"
// =========================================================================
// Default globals...
BOOL G_contextMgrOpenInitialized;
DWORD G_contextMgrOpenLastHandle;
CRITICAL_SECTION G_contextMgrOpenCriticalSection;
OPEN_LIST_ITEM* G_contextMgrOpenDeviceList;
// =========================================================================
BOOL contextMgrOpen_Init()
{
BOOL retval = TRUE;
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("contextMgrOpen_Init\n")));
if (!(G_contextMgrOpenInitialized))
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("Initializing...\n")));
G_contextMgrOpenLastHandle = 1;
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("About to initialize critical section: 0x%0.8x\n"), &G_contextMgrOpenCriticalSection));
InitializeCriticalSection(&G_contextMgrOpenCriticalSection);
// Note that we defer creating the actual list until a device is added
// to it; this reduces the load/initialization time for the DLL
G_contextMgrOpenDeviceList = NULL;
G_contextMgrOpenInitialized = TRUE;
}
DEBUGOUTMAINDRV(DEBUGLEV_EXIT, (TEXT("contextMgrOpen_Init\n")));
return retval;
}
// =========================================================================
BOOL contextMgrOpen_Deinit()
{
BOOL retval = TRUE;
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("contextMgrOpen_Deinit\n")));
if (G_contextMgrOpenInitialized)
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("Deinitializing...\n")));
G_contextMgrOpenLastHandle = 1;
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("About to delete critical section: 0x%0.8x\n"), &G_contextMgrOpenCriticalSection));
DeleteCriticalSection(&G_contextMgrOpenCriticalSection);
G_contextMgrOpenDeviceList = NULL;
G_contextMgrOpenInitialized = TRUE;
}
DEBUGOUTMAINDRV(DEBUGLEV_EXIT, (TEXT("contextMgrOpen_Deinit\n")));
return retval;
}
// =========================================================================
// Returns the device context, or NULL if the volume is no longer valid
// (e.g. handle passed in relates to a dismounted volume/volume in the
// process of being dismounted)
OPEN_LIST_ITEM* contextMgrOpen_GetOpenContext(DWORD hOpen)
{
OPEN_LIST_ITEM* retval = NULL;
OPEN_LIST_ITEM* currListItem;
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("(OH: %d) contextMgrOpen_GetOpenContext\n"), hOpen));
if (!(G_contextMgrOpenInitialized))
{
DEBUGOUTMAINDRV(DEBUGLEV_ERROR, (TEXT("(OH: %d) Device mgr list not initialized\n"), hOpen));
}
else
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) +++OL About to...\n"), hOpen));
EnterCriticalSection(&G_contextMgrOpenCriticalSection);
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) +++OL ...Done.\n"), hOpen));
currListItem = G_contextMgrOpenDeviceList;
while (currListItem != NULL)
{
if (currListItem->hOpen == hOpen)
{
if (currListItem->hDevice != 0)
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) +++OI About to...\n"), hOpen));
EnterCriticalSection(&(currListItem->CriticalSection));
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) +++OI ...Done.\n"), hOpen));
if (currListItem->hDevice != 0)
{
// Still valid...
retval = currListItem;
}
else
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) ---OI About to...\n"), hOpen));
LeaveCriticalSection(&(currListItem->CriticalSection));
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) ---OI ...Done.\n"), hOpen));
// Dismount attempted during entering criticalsection
}
}
break;
}
currListItem = currListItem->NextListItem;
}
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) ---OL About to...\n"), hOpen));
LeaveCriticalSection(&G_contextMgrOpenCriticalSection);
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) ---OL ...Done.\n"), hOpen));
}
DEBUGOUTMAINDRV(DEBUGLEV_EXIT, (TEXT("(OH: %d) contextMgrOpen_GetOpenContext\n"), hOpen));
return retval;
}
// =========================================================================
// Note: Can handle being passed NULL as listItem
void contextMgrOpen_ReleaseOpenContext(OPEN_LIST_ITEM* listItem)
{
if (listItem != NULL)
{
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("(OH: %d) contextMgrOpen_ReleaseOpenContext\n"), listItem->hOpen));
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) ---OI About to...\n"), listItem->hOpen));
LeaveCriticalSection(&(listItem->CriticalSection));
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: %d) ---OI ...Done.\n"), listItem->hOpen));
DEBUGOUTMAINDRV(DEBUGLEV_EXIT, (TEXT("(OH: %d) contextMgrOpen_ReleaseOpenContext\n"), listItem->hOpen));
}
else
{
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("(OH: <NULL PTR PASSED IN>)\n")));
DEBUGOUTMAINDRV(DEBUGLEV_EXIT, (TEXT("(OH: <NULL PTR PASSED IN>)\n")));
}
}
// =========================================================================
// Note: Device list critical section should be taken before calling this
// function
DWORD contextMgrOpen_GetNextHandleDevice()
{
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("contextMgrOpen_GetNextHandleDevice\n")));
G_contextMgrOpenLastHandle++;
DEBUGOUTMAINDRV(DEBUGLEV_EXIT, (TEXT("contextMgrOpen_GetNextHandleDevice\n")));
return G_contextMgrOpenLastHandle;
}
// =========================================================================
DWORD contextMgrOpen_AddOpenContext(DWORD hDevice)
{
DWORD retval = 0;
OPEN_LIST_ITEM* newListItem;
DEBUGOUTMAINDRV(DEBUGLEV_ENTER, (TEXT("(OH: +) contextMgrOpen_AddOpenContext\n"), hDevice));
if (!(G_contextMgrOpenInitialized))
{
DEBUGOUTMAINDRV(DEBUGLEV_ERROR, (TEXT("(OH: +) Device mgr list not initialized\n"), hDevice));
}
else
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) AQ-1\n"), hDevice));
newListItem = malloc(sizeof(*newListItem));
if (newListItem == NULL)
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) Unable to malloc new open list item\n"), hDevice));
}
else
{
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) Setting up for new open list item\n"), hDevice));
InitializeCriticalSection(&(newListItem->CriticalSection));
newListItem->hDevice = hDevice;
newListItem->Position.QuadPart = 0;
// Setup device handle and insert new device item into list
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) +++OL About to...\n")));
EnterCriticalSection(&G_contextMgrOpenCriticalSection);
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) +++OL ...Done.\n")));
newListItem->hOpen = contextMgrOpen_GetNextHandleDevice();
// Insert at head of list...
newListItem->NextListItem = G_contextMgrOpenDeviceList;
G_contextMgrOpenDeviceList = newListItem;
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) Open handle : %0.8x\n"), hDevice, newListItem->hOpen));
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) Open device handle: %0.8x\n"), hDevice, newListItem->hDevice));
retval = newListItem->hOpen;
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) AQ-3\n"), hDevice));
DEBUGOUTMAINDRV(DEBUGLEV_INFO, (TEXT("(OH: +) ---OL About to...\n")));
LeaveCriticalSection(&G_contextMgrOpenCriticalSection);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -