📄 autodecrypt.cpp
字号:
#include "crtdbg.h"
#include "pgpdebug.h"
#include "autodecrypt.h"
#include "util.h"
#include "globals.h"
#include "globdefs.h"
#include "pgpclientlib.h"
#include "pgpUtilities.h"
#include "decryptverify.h"
#include "pgpsc.h"
#include "pgpclientprefs.h"
CAutoDecrypt::CAutoDecrypt()
{
m_hWatchThread=NULL;
}
CAutoDecrypt::~CAutoDecrypt()
{
}
void CAutoDecrypt::Start()
{
if(NULL == m_hWatchThread)
{
DWORD dwThreadId=0;//we dont need thread id, but the param cannot be null in 9x
m_hWatchThread=CreateThread(NULL, 0, &AutoDecryptThreadProc, 0, 0, &dwThreadId);
pgpAssert(NULL != m_hWatchThread);
}
else
{
//PostThreadMessage(dwThreadId, RESUME_IF_STOPPED, 0, 0);
}
}
void CAutoDecrypt::Stop()
{
}
void DumpChangeDetails(void *lpBuf, DWORD dwBufLength);
DWORD WINAPI AutoDecryptThreadProc(LPVOID lpParameter)
{
DWORD dwRet=0;
HANDLE hWatchedDir=NULL;
void *lpBuf=NULL;
DWORD dwBufLen=sizeof(FILE_NOTIFY_INFORMATION)*10;
DWORD dwBytesReturned=0;
BOOL bRet=FALSE;
//open the directory we want to watch for file creation and changes
hWatchedDir=CreateFile("D:\\Documents and Settings\\Administrator\\Local Settings\\Temp\\MXLibDir",
FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_DELETE,
NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
if(INVALID_HANDLE_VALUE == hWatchedDir)
{
pgpAssert(FALSE);//failed to open the directory
goto autodecryptproc_cleanup;
}
//allocate buffer for 10 change notifications
lpBuf=calloc(dwBufLen, 1);
if(NULL == lpBuf)
{
pgpAssert(FALSE);//failed to allocate memory
goto autodecryptproc_cleanup;
}
while(TRUE)
{
//reset all memory contents
ZeroMemory(lpBuf, dwBufLen);
//read directory changes
bRet=ReadDirectoryChangesW(hWatchedDir, lpBuf, dwBufLen, FALSE,
FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_ACCESS|FILE_NOTIFY_CHANGE_SECURITY,
&dwBytesReturned, NULL, NULL);
if(FALSE == bRet)
{
pgpAssert(FALSE);//some error during reading changes
goto autodecryptproc_cleanup;
}
DumpChangeDetails(lpBuf, dwBytesReturned);
//Sleep(2000);
//dwRet=WaitForSingleObject(hNotify, INFINITE);
//if(WAIT_OBJECT_0 == dwRet)
//{
// FindNextChangeNotification(hNotify);
//}
}
/*HANDLE hNotify=FindFirstChangeNotification("D:\\Documents and Settings\\Administrator\\Local Settings\\Temp\\MXLibDir",
TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE|FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_ACCESS|FILE_NOTIFY_CHANGE_SECURITY);
pgpAssert(INVALID_HANDLE_VALUE != hNotify);
if(INVALID_HANDLE_VALUE == hNotify)
{
PgpGwTrace("FindFirstChangeNotification returned error %d\n", GetLastError());
return 0;
}*/
autodecryptproc_cleanup:
if(NULL != hWatchedDir)
{
CloseHandle(hWatchedDir);
hWatchedDir=NULL;
}
if(NULL != lpBuf)
{
free(lpBuf);
lpBuf=NULL;
}
return 0;
}
void DumpChangeDetails(void *lpBuf, DWORD dwBufLength)
{
pgpAssert(NULL != lpBuf);
PFILE_NOTIFY_INFORMATION pfniNotifyInfo=NULL;
if(0 == dwBufLength)
return;
pfniNotifyInfo=(PFILE_NOTIFY_INFORMATION)lpBuf;
while(NULL != pfniNotifyInfo)
{
PgpGwTrace("CHANGE INFO...\n");
PgpGwTrace("\tFileName=");
for(unsigned int ui=0; ui < pfniNotifyInfo->FileNameLength; ui++)
PgpGwTrace("%C", pfniNotifyInfo->FileName[ui]);
PgpGwTrace("\n");
PgpGwTrace("\tAction=(%#x)", pfniNotifyInfo->Action);
if(FILE_ACTION_ADDED == pfniNotifyInfo->Action)
{
PgpGwTrace(" FILE_ACTION_ADDED");
#if 0
PGPError pgpErrRet=kPGPError_NoErr;
PGPtlsContextRef tlsContext=NULL;
PGPContextRef pgpContext=NULL;
WCHAR wszInputFile[255]={0};
char *pszOutFile=NULL;
void *pOutput=NULL;
PGPSize pgpSize=0;
BOOL bFYEO=0;
wcscpy(wszInputFile, L"D:\\Documents and Settings\\Administrator\\Local Settings\\Temp\\MXLibDir\\");
memcpy(wszInputFile+wcslen(wszInputFile), pfniNotifyInfo->FileName, pfniNotifyInfo->FileNameLength);
pgpErrRet=PGPNewContext(kPGPsdkAPIVersion, &pgpContext);
if(IsPGPError(pgpErrRet) || (NULL == pgpContext))
pgpAssert(FALSE);
pgpErrRet=PGPNewTLSContext(pgpContext, &tlsContext);
if(IsPGPError(pgpErrRet) || (NULL == tlsContext))
pgpAssert(FALSE);
//decrypt the file
pgpErrRet=DecryptVerifyFile(g_hInstance, NULL, pgpContext,
tlsContext, CLIENTNAME, MODULEFILENAME,
ConvertToAnsi(wszInputFile)/*free this*/, FALSE, FALSE,/*TRUE,*/
&pszOutFile, &pOutput, &pgpSize, &bFYEO);
if(NULL != tlsContext)
{
PGPFreeTLSContext(tlsContext);
tlsContext=NULL;
}
#endif
}
else if(FILE_ACTION_REMOVED == pfniNotifyInfo->Action)
PgpGwTrace(" FILE_ACTION_REMOVED");
else if(FILE_ACTION_MODIFIED== pfniNotifyInfo->Action)
PgpGwTrace(" FILE_ACTION_MODIFIED");
else if(FILE_ACTION_RENAMED_OLD_NAME == pfniNotifyInfo->Action)
PgpGwTrace(" FILE_ACTION_RENAMED_OLD_NAME");
else if(FILE_ACTION_RENAMED_NEW_NAME == pfniNotifyInfo->Action)
PgpGwTrace(" FILE_ACTION_RENAMED_NEW_NAME");
PgpGwTrace("\n");
if(0 == pfniNotifyInfo->NextEntryOffset)
break;
pfniNotifyInfo = (PFILE_NOTIFY_INFORMATION)
(((BYTE *)pfniNotifyInfo)+pfniNotifyInfo->NextEntryOffset);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -