Engine.cpp
上传用户:chaoyu
上传日期:2013-04-28
资源大小:18k
文件大小:3k
源码类别:

杀毒

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include "VirusDB.h"
  3. #include "Engine.h"
  4. #include "MemFileObject.h"
  5. CEngine::CEngine(void)
  6. {
  7. m_pcVDB = NULL;
  8. m_dwObjCount = 0;
  9. m_pScanParam = NULL;
  10. m_cScanResults.nSize = sizeof(SCAN_RESULTS);
  11. m_cScanResults.dwObjCount = 0;
  12. m_cScanResults.dwTime = 0;
  13. m_cScanResults.dwRecCount = 0;
  14. m_cScanResults.pScanRecords = NULL;
  15. }
  16. CEngine::~CEngine(void)
  17. {
  18. Release();
  19. }
  20. bool CEngine::Load(IN CVirusDB* pVDB)
  21. {
  22. ASSERT(pVDB);
  23. if(pVDB==NULL)
  24. return false;
  25. m_pcVDB = pVDB;
  26. return true;
  27. }
  28. bool CEngine::Release()
  29. {
  30. ReleaseResults();
  31. return true;
  32. }
  33. PSCAN_RESULTS CEngine::Scan(IN const PSCAN_PARAM pScanParam)
  34. {
  35. m_pScanParam = pScanParam;
  36. // release previous results.
  37. ReleaseResults();
  38. DWORD dwBeginTime = GetTickCount();
  39. // deep first search and call VirusDB.Search() to detect virus.
  40. DFS(pScanParam->strPathName);
  41. DWORD dwEndTime = GetTickCount();
  42. m_cScanResults.dwTime = dwEndTime-dwBeginTime;
  43. return &m_cScanResults;
  44. }
  45. void CEngine::ReleaseResults()
  46. {
  47. PSCAN_RECORD pCurRecord = m_cScanResults.pScanRecords;
  48. m_cScanResults.pScanRecords = NULL;
  49. m_cScanResults.dwRecCount = 0;
  50. m_cScanResults.dwTime = 0;
  51. m_cScanResults.dwObjCount = 0;
  52. while( pCurRecord )
  53. {
  54. PSCAN_RECORD pTmpRecord = pCurRecord;
  55. pCurRecord = pCurRecord->pNext;
  56. if(pTmpRecord->pScanObject)
  57. delete pTmpRecord->pScanObject;
  58. delete pTmpRecord;
  59. }
  60. }
  61. // deep first search
  62. void CEngine::DFS(PCTSTR pszPathName)
  63. {
  64. if(FILE_ATTRIBUTE_DIRECTORY&GetFileAttributes(pszPathName))
  65. {
  66. CString strPath = pszPathName;
  67. WIN32_FIND_DATA FindFileData;
  68. bool bFinished = false; 
  69. HANDLE hFinder = FindFirstFile(strPath+"\*.*", &FindFileData);
  70. if(hFinder!=INVALID_HANDLE_VALUE)
  71. {
  72. while(!bFinished)
  73. {
  74. if( strcmp(FindFileData.cFileName, ".") && strcmp(FindFileData.cFileName, ".."))
  75. {
  76. DFS(strPath+"\"+FindFileData.cFileName);
  77. }
  78. if (!FindNextFile(hFinder, &FindFileData)) 
  79. bFinished = true;
  80. }
  81. FindClose(hFinder);
  82. }
  83. }
  84. else
  85. {
  86. ScanOneObject(pszPathName);
  87. }
  88. }
  89. bool CEngine::ScanOneObject(PCTSTR pszObjectName)
  90. {
  91. // debug use only
  92. // 所有对外显示应该集中在主程序
  93. printf("Scan %sn", pszObjectName);
  94. m_cScanResults.dwObjCount++;
  95. CMemFileObject cScanObj;
  96. cScanObj.m_strObjName = pszObjectName;
  97. if( !cScanObj.Open() )
  98. {
  99. // TODO: show error here.
  100. // debug use only
  101. printf("Error: "%s" open failed.n", pszObjectName);
  102. return false;
  103. }
  104. DWORD dwVID = m_pcVDB->Search(&cScanObj);
  105. if( dwVID )
  106. {
  107. PSCAN_RECORD pScanRecord = new SCAN_RECORD;
  108. if(pScanRecord)
  109. {
  110. CFileObject* pScanObj = new CFileObject(cScanObj);
  111. pScanRecord->dwVirusID = dwVID;
  112. pScanRecord->eResult = BR_WITH_VIRUS;
  113. pScanRecord->pScanObject= pScanObj;
  114. pScanRecord->pNext = m_cScanResults.pScanRecords;
  115. m_cScanResults.pScanRecords = pScanRecord;
  116. m_cScanResults.dwRecCount++;
  117. }
  118. }
  119. cScanObj.Close();
  120. return true;
  121. }