📄 dirmon.bak.c
字号:
// DIRMON.c - main module for VxD DIRMON
#define DEVICE_MAIN
#include <vtoolsc.h>
#include "dirmon.h"
#include "ioctlcmd.h"
#undef DEVICE_MAIN
#define PARSE_FILENAME_FROM_FILENUMBER 0
#define DEBUG_FUNCTION_NAME(func) dprintf(func "\n")
#define HASH_BITS 7
#define HASH_NUMBER (1 << HASH_BITS)
#define HASH_BITS_MASK (HASH_NUMBER - 1)
// Structure to save file information
struct _tag_DirectoryItem;
typedef struct _tag_FileItem
{
sfn_t nFileNumber;
struct _tag_DirectoryItem* pDirectoryItem;
CHAR sFilePathName[MAX_PATH];
PCHAR pRelativePath;
DWORD dwUpdateDosTime;
struct _tag_FileItem* pNext;
} FILEITEM, *PFILEITEM;
// Structure to save directory information which is being monitored.
typedef struct _tag_DirectoryItem
{
int nRefCount;
char sDirectory[MAX_PATH];
// int nLen;
char sShortDirectory[MAX_PATH];
PFILEITEM pUpdatedFileItems;
struct _tag_DirectoryItem* pNext;
} DIRECTORYITEM, *PDIRECTORYITEM;
// Registered directory item list
SEMHANDLE g_semDirectoryItems;
PDIRECTORYITEM g_pDirectoryItems;
// Files hash table
SEMHANDLE g_semFileItemHashTable;
PFILEITEM g_pFileItemHashTable[HASH_NUMBER];
// Free file item list
SEMHANDLE g_semFreeFileItems;
FILEITEM g_arrayFreeFileItems[HASH_NUMBER];
PFILEITEM g_pFreeFileItems;
#define FREEFILEITEMSSTART ((PVOID)g_arrayFreeFileItems)
#define FREEFILEITEMSEND ((PVOID)((PCHAR)g_arrayFreeFileItems + sizeof(g_arrayFreeFileItems)))
// Real service pointers with the hook thunks
ppIFSFileHookFunc g_pPrevIFSHookProc;
Declare_Virtual_Device(DIRMON)
DefineControlHandler(SYS_DYNAMIC_DEVICE_INIT, OnSysDynamicDeviceInit);
DefineControlHandler(SYS_DYNAMIC_DEVICE_EXIT, OnSysDynamicDeviceExit);
DefineControlHandler(W32_DEVICEIOCONTROL, OnW32Deviceiocontrol);
BOOL __cdecl ControlDispatcher(
DWORD dwControlMessage,
DWORD EBX,
DWORD EDX,
DWORD ESI,
DWORD EDI,
DWORD ECX)
{
START_CONTROL_DISPATCH
ON_SYS_DYNAMIC_DEVICE_INIT(OnSysDynamicDeviceInit);
ON_SYS_DYNAMIC_DEVICE_EXIT(OnSysDynamicDeviceExit);
ON_W32_DEVICEIOCONTROL(OnW32Deviceiocontrol);
END_CONTROL_DISPATCH
return TRUE;
}
void InitDirectoryItemList()
{
g_semDirectoryItems = Create_Semaphore(0);
g_pDirectoryItems = NULL;
Signal_Semaphore(g_semDirectoryItems);
}
void DestroyDirectoryItemList()
{
PDIRECTORYITEM pItem, pNextItem;
PFILEITEM pFileItem, pNextFileItem;
Wait_Semaphore(g_semDirectoryItems, BLOCK_SVC_INTS);
pItem = g_pDirectoryItems;
while ( pItem )
{
pNextItem = pItem->pNext;
pFileItem = pItem->pUpdatedFileItems;
while ( pFileItem )
{
pNextFileItem = pFileItem->pNext;
HeapFree(pFileItem, 0);
pFileItem = pNextFileItem;
}
HeapFree(pItem, 0);
pItem = pNextItem;
}
Destroy_Semaphore(g_semDirectoryItems);
}
BOOL AllocateDirectoryItem(PCHAR pPath, PDIRECTORYITEM* ppDirectoryItem)
{
PDIRECTORYITEM pDirectoryItem;
if ( !pPath || !*pPath || (pDirectoryItem = HeapAllocate(sizeof(**ppDirectoryItem), 0)) == NULL )
return FALSE;
else
{
PCHAR pSrc = pPath, pDest, pBackSlash;
PDIRECTORYITEM pItem;
pBackSlash = pDirectoryItem->sDirectory;
pDest = pBackSlash + 1;
while ( *pSrc )
{
if ( *pSrc != '\\' )
*pDest++ = *pSrc++;
else
{
*pBackSlash = pDest - pBackSlash - 1;
pBackSlash = pDest++;
pSrc++;
}
}
if ( (*pBackSlash = pDest - pBackSlash - 1) )
*pDest = 0;
pSrc++;
pBackSlash = pDirectoryItem->sShortDirectory;
pDest = pBackSlash + 1;
while ( *pSrc )
{
if ( *pSrc != '\\' )
*pDest++ = *pSrc++;
else
{
*pBackSlash = pDest - pBackSlash - 1;
pBackSlash = pDest++;
pSrc++;
}
}
if ( (*pBackSlash = pDest - pBackSlash - 1) )
*pDest = 0;
Wait_Semaphore(g_semDirectoryItems, BLOCK_SVC_INTS);
pItem = g_pDirectoryItems;
while ( pItem )
{
pSrc = pDirectoryItem->sShortDirectory;
pDest = pItem->sShortDirectory;
while ( *pSrc == *pDest && *pSrc && strnicmp(pSrc + 1, pDest + 1, *pSrc) == 0 )
{
pSrc += *pDest + 1;
pDest += *pDest + 1;
}
if ( *pSrc == *pDest && *pSrc == 0 )
{
pItem->nRefCount++;
Signal_Semaphore(g_semDirectoryItems);
HeapFree(pDirectoryItem, 0);
*ppDirectoryItem = pItem;
return TRUE;
}
pItem = pItem->pNext;
}
pDirectoryItem->nRefCount = 1;
pDirectoryItem->pUpdatedFileItems = NULL;
pDirectoryItem->pNext = g_pDirectoryItems;
g_pDirectoryItems = pDirectoryItem;
Signal_Semaphore(g_semDirectoryItems);
*ppDirectoryItem = pDirectoryItem;
{
char buf1[MAX_PATH], buf2[MAX_PATH], *p1, *p2;
p1 = buf1;
p2 = pDirectoryItem->sDirectory;
while ( *p2 )
{
sprintf(p1, "(%d)", (int)*p2);
p1 += strlen(p1);
memcpy(p1, p2 + 1, *p2);
p1 += *p2;
*p1 = 0;
p2 += *p2 + 1;
}
p1 = buf2;
p2 = pDirectoryItem->sShortDirectory;
while ( *p2 )
{
sprintf(p1, "(%d)", (int)*p2);
p1 += strlen(p1);
memcpy(p1, p2 + 1, *p2);
p1 += *p2;
*p1 = 0;
p2 += *p2 + 1;
}
dprintf("Insert monitor path: %s (%s)\n", buf1, buf2);
}
return TRUE;
}
/*
else
{
PCHAR pcDest = pDirectoryItem->sDirectory, pcSrc = pPath;
PDIRECTORYITEM pPrevItem, pItem;
int nCompare;
while ( *pcSrc )
*pcDest++ = *pcSrc++;
if ( *(pcDest - 1) == '\\' )
pcDest--;
if ( (pDirectoryItem->nLen = (int)(pcDest - pDirectoryItem->sDirectory)) == 0 )
{
HeapFree(pDirectoryItem, 0);
return FALSE;
}
memset(pDirectoryItem->sDirectory + pDirectoryItem->nLen, 0, sizeof(pDirectoryItem->sDirectory) - pDirectoryItem->nLen);
strlwr(pDirectoryItem->sDirectory);
Wait_Semaphore(g_semDirectoryItems, BLOCK_SVC_INTS);
pPrevItem = NULL;
pItem = g_pDirectoryItems;
while ( pItem )
{
nCompare = strcmp(pDirectoryItem->sDirectory, pItem->sDirectory);
if ( nCompare == 0 )
{
pItem->nRefCount++;
Signal_Semaphore(g_semDirectoryItems);
HeapFree(pDirectoryItem, 0);
*ppDirectoryItem = pItem;
dprintf("Found monitor path: %s\n", pItem->sDirectory);
return TRUE;
}
else if ( nCompare < 0 )
break;
pPrevItem = pItem;
pItem = pItem->pNext;
}
if ( pPrevItem )
{
pPrevItem->pNext = pDirectoryItem;
pDirectoryItem->pNext = pItem;
}
else
{
pDirectoryItem->pNext = g_pDirectoryItems;
g_pDirectoryItems = pDirectoryItem;
}
pDirectoryItem->nRefCount = 1;
pDirectoryItem->pUpdatedFileItems = NULL;
Signal_Semaphore(g_semDirectoryItems);
*ppDirectoryItem = pDirectoryItem;
dprintf("Insert monitor path: %s\n", pDirectoryItem->sDirectory);
return TRUE;
}
*/
}
void FreeDirectoryItem(PDIRECTORYITEM pDirectoryItem)
{
PDIRECTORYITEM pPrevItem = NULL, pItem;
Wait_Semaphore(g_semDirectoryItems, BLOCK_SVC_INTS);
pItem = g_pDirectoryItems;
while ( pItem && pItem != pDirectoryItem )
{
pPrevItem = pItem;
pItem = pItem->pNext;
}
if ( pItem != NULL )
{
if ( --pItem->nRefCount == 0 )
{
if ( pPrevItem == NULL )
g_pDirectoryItems = pItem->pNext;
else
pPrevItem->pNext = pItem->pNext;
Signal_Semaphore(g_semDirectoryItems);
HeapFree(pItem, 0);
return;
}
}
Signal_Semaphore(g_semDirectoryItems);
}
BOOL AppendFileItemToDirectory(PFILEITEM pFileItem)
{
PDIRECTORYITEM pDirectoryItem;
Wait_Semaphore(g_semDirectoryItems, BLOCK_SVC_INTS);
pDirectoryItem = g_pDirectoryItems;
while ( pDirectoryItem )
{
if ( pDirectoryItem == pFileItem->pDirectoryItem )
{
pFileItem->pNext = pDirectoryItem->pUpdatedFileItems;
pDirectoryItem->pUpdatedFileItems = pFileItem;
Signal_Semaphore(g_semDirectoryItems);
return TRUE;
}
pDirectoryItem = pDirectoryItem->pNext;
}
Signal_Semaphore(g_semDirectoryItems);
return FALSE;
}
void InitFileItemHashTable()
{
g_semFileItemHashTable = Create_Semaphore(0);
memset(g_pFileItemHashTable, 0, sizeof(g_pFileItemHashTable));
Signal_Semaphore(g_semFileItemHashTable);
}
void DestroyFileItemHashTable()
{
PFILEITEM pItem, pNextItem;
int i;
Wait_Semaphore(g_semFileItemHashTable, BLOCK_SVC_INTS);
for ( i = 0; i < HASH_NUMBER; i++ )
{
pItem = g_pFileItemHashTable[i];
while ( pItem )
{
pNextItem = pItem->pNext;
HeapFree(pItem, 0);
pItem = pNextItem;
}
}
Destroy_Semaphore(g_semFileItemHashTable);
}
void InsertFileItemIntoHashTable(PFILEITEM pItem)
{
Wait_Semaphore(g_semFileItemHashTable, BLOCK_SVC_INTS);
{
register int nHashNo = pItem->nFileNumber & HASH_BITS_MASK;
pItem->pNext = g_pFileItemHashTable[nHashNo];
g_pFileItemHashTable[nHashNo] = pItem;
}
Signal_Semaphore(g_semFileItemHashTable);
}
/*
void RemoveFileItemFromHashTable(PFILEITEM pItem)
{
PFILEITEM pPrevHashItem = NULL, pHashItem;
int nHashNo = pItem->nFileNumber & HASH_BITS_MASK;
Wait_Semaphore(g_semFileItemHashTable, BLOCK_SVC_INTS);
pHashItem = g_pFileItemHashTable[nHashNo];
while ( pHashItem )
{
if ( pHashItem == pItem )
{
if ( pPrevHashItem == NULL )
g_pFileItemHashTable[nHashNo] = pHashItem->pNext;
else
pPrevHashItem->pNext = pHashItem->pNext;
break;
}
pPrevHashItem = pHashItem;
pHashItem = pHashItem->pNext;
}
Signal_Semaphore(g_semFileItemHashTable);
}
*/
PFILEITEM RemoveFileItemFromHashTable(sfn_t nFileNumber)
{
PFILEITEM pPrevHashItem = NULL, pHashItem;
Wait_Semaphore(g_semFileItemHashTable, BLOCK_SVC_INTS);
pHashItem = g_pFileItemHashTable[nFileNumber & HASH_BITS_MASK];
while ( pHashItem )
{
if ( pHashItem->nFileNumber == nFileNumber )
{
if ( pPrevHashItem == NULL )
g_pFileItemHashTable[nFileNumber & HASH_BITS_MASK] = pHashItem->pNext;
else
pPrevHashItem->pNext = pHashItem->pNext;
Signal_Semaphore(g_semFileItemHashTable);
if ( pHashItem->pDirectoryItem )
dprintf("Remove file item from hash table: %s\n", pHashItem->sFilePathName);
else if ( pHashItem->sFilePathName[0] )
dprintf("Remove (not monitored) file item from hash table: %s\n", pHashItem->sFilePathName);
else
dprintf("Remove (not monitored) file item from hash table: %d\n", pHashItem->nFileNumber);
return pHashItem;
}
pPrevHashItem = pHashItem;
pHashItem = pHashItem->pNext;
}
Signal_Semaphore(g_semFileItemHashTable);
return NULL;
}
PFILEITEM GetFileItemFromHashTable(sfn_t nFileNumber)
{
PFILEITEM pHashItem;
Wait_Semaphore(g_semFileItemHashTable, BLOCK_SVC_INTS);
pHashItem = g_pFileItemHashTable[nFileNumber & HASH_BITS_MASK];
while ( pHashItem && pHashItem->nFileNumber != nFileNumber )
pHashItem = pHashItem->pNext;
Signal_Semaphore(g_semFileItemHashTable);
return pHashItem;
}
void InitFreeFileItemList()
{
PFILEITEM pPrevItem = g_pFreeFileItems = g_arrayFreeFileItems, pItem = g_arrayFreeFileItems + 1;
g_semFreeFileItems = Create_Semaphore(0);
while ( pItem < FREEFILEITEMSEND )
{
pPrevItem->pNext = pItem;
pPrevItem = pItem++;
}
pPrevItem->pNext = NULL;
Signal_Semaphore(g_semFreeFileItems);
}
void DestroyFreeFileItemList()
{
PFILEITEM pItem, pItemNext;
Wait_Semaphore(g_semFreeFileItems, BLOCK_SVC_INTS);
pItem = g_pFreeFileItems;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -