Engine.cpp
资源名称:BAV.v2.rar [点击查看]
上传用户:chaoyu
上传日期:2013-04-28
资源大小:18k
文件大小:3k
源码类别:
杀毒
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "VirusDB.h"
- #include "Engine.h"
- #include "MemFileObject.h"
- CEngine::CEngine(void)
- {
- m_pcVDB = NULL;
- m_dwObjCount = 0;
- m_pScanParam = NULL;
- m_cScanResults.nSize = sizeof(SCAN_RESULTS);
- m_cScanResults.dwObjCount = 0;
- m_cScanResults.dwTime = 0;
- m_cScanResults.dwRecCount = 0;
- m_cScanResults.pScanRecords = NULL;
- }
- CEngine::~CEngine(void)
- {
- Release();
- }
- bool CEngine::Load(IN CVirusDB* pVDB)
- {
- ASSERT(pVDB);
- if(pVDB==NULL)
- return false;
- m_pcVDB = pVDB;
- return true;
- }
- bool CEngine::Release()
- {
- ReleaseResults();
- return true;
- }
- PSCAN_RESULTS CEngine::Scan(IN const PSCAN_PARAM pScanParam)
- {
- m_pScanParam = pScanParam;
- // release previous results.
- ReleaseResults();
- DWORD dwBeginTime = GetTickCount();
- // deep first search and call VirusDB.Search() to detect virus.
- DFS(pScanParam->strPathName);
- DWORD dwEndTime = GetTickCount();
- m_cScanResults.dwTime = dwEndTime-dwBeginTime;
- return &m_cScanResults;
- }
- void CEngine::ReleaseResults()
- {
- PSCAN_RECORD pCurRecord = m_cScanResults.pScanRecords;
- m_cScanResults.pScanRecords = NULL;
- m_cScanResults.dwRecCount = 0;
- m_cScanResults.dwTime = 0;
- m_cScanResults.dwObjCount = 0;
- while( pCurRecord )
- {
- PSCAN_RECORD pTmpRecord = pCurRecord;
- pCurRecord = pCurRecord->pNext;
- if(pTmpRecord->pScanObject)
- delete pTmpRecord->pScanObject;
- delete pTmpRecord;
- }
- }
- // deep first search
- void CEngine::DFS(PCTSTR pszPathName)
- {
- if(FILE_ATTRIBUTE_DIRECTORY&GetFileAttributes(pszPathName))
- {
- CString strPath = pszPathName;
- WIN32_FIND_DATA FindFileData;
- bool bFinished = false;
- HANDLE hFinder = FindFirstFile(strPath+"\*.*", &FindFileData);
- if(hFinder!=INVALID_HANDLE_VALUE)
- {
- while(!bFinished)
- {
- if( strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, ".."))
- {
- DFS(strPath+"\"+FindFileData.cFileName);
- }
- if (!FindNextFile(hFinder, &FindFileData))
- bFinished = true;
- }
- FindClose(hFinder);
- }
- }
- else
- {
- ScanOneObject(pszPathName);
- }
- }
- bool CEngine::ScanOneObject(PCTSTR pszObjectName)
- {
- // debug use only
- // 所有对外显示应该集中在主程序
- printf("Scan %sn", pszObjectName);
- m_cScanResults.dwObjCount++;
- CMemFileObject cScanObj;
- cScanObj.m_strObjName = pszObjectName;
- if( !cScanObj.Open() )
- {
- // TODO: show error here.
- // debug use only
- printf("Error: "%s" open failed.n", pszObjectName);
- return false;
- }
- DWORD dwVID = m_pcVDB->Search(&cScanObj);
- if( dwVID )
- {
- PSCAN_RECORD pScanRecord = new SCAN_RECORD;
- if(pScanRecord)
- {
- CFileObject* pScanObj = new CFileObject(cScanObj);
- pScanRecord->dwVirusID = dwVID;
- pScanRecord->eResult = BR_WITH_VIRUS;
- pScanRecord->pScanObject= pScanObj;
- pScanRecord->pNext = m_cScanResults.pScanRecords;
- m_cScanResults.pScanRecords = pScanRecord;
- m_cScanResults.dwRecCount++;
- }
- }
- cScanObj.Close();
- return true;
- }