MemFileObject.cpp
资源名称:BAV.v2.rar [点击查看]
上传用户:chaoyu
上传日期:2013-04-28
资源大小:18k
文件大小:2k
源码类别:
杀毒
开发平台:
Visual C++
- #include "StdAfx.h"
- #include "MemFileObject.h"
- CMemFileObject::CMemFileObject(void)
- {
- m_eObjType = BO_MEM_FILE;
- m_hMapFile = NULL;
- m_pFile = NULL;
- }
- CMemFileObject::CMemFileObject(CMemFileObject& cMemFileObj)
- {
- m_eObjType = BO_MEM_FILE;
- m_hFile = INVALID_HANDLE_VALUE;
- m_dwObjSize = cMemFileObj.m_dwObjSize;
- m_strObjName= cMemFileObj.m_strObjName;
- m_hMapFile = NULL;
- m_pFile = NULL;
- }
- CMemFileObject::~CMemFileObject(void)
- {
- if(m_pFile)
- Close();
- }
- bool CMemFileObject::Open(bool bScan)
- {
- if(!CFileObject::Open(bScan))
- return false;
- DWORD dwProtect, dwDesiredAccess;
- if(bScan)
- {
- dwProtect = PAGE_READONLY;
- dwDesiredAccess = FILE_MAP_READ;
- }
- else
- {
- dwProtect = PAGE_READWRITE;
- dwDesiredAccess = FILE_MAP_WRITE;
- }
- // TODO: If clean virus enlarge the file size, we need calculate and change the dwMaximumSizeLow value.
- m_hMapFile = ::CreateFileMapping(m_hFile, NULL, dwProtect, 0, 0, NULL);
- if(m_hMapFile==NULL)
- {
- Close();
- return false;
- }
- m_pFile = static_cast<LPBYTE>( ::MapViewOfFile(m_hMapFile, dwDesiredAccess, 0, 0, NULL) );
- if(m_pFile==NULL)
- {
- Close();
- return false;
- }
- return true;
- }
- bool CMemFileObject::Close()
- {
- if(m_pFile)
- {
- ::UnmapViewOfFile(m_pFile);
- m_pFile = NULL;
- }
- if(m_hMapFile)
- {
- ::CloseHandle(m_hMapFile);
- m_hMapFile = NULL;
- }
- return CFileObject::Close();
- }
- bool CMemFileObject::Compare(IN INT nOffset, IN INT nSize, IN LPBYTE pSignature, IN BAV_SIGN_LOGIC_OPERATION eLogicOp)
- {
- return Compare(m_pFile+nOffset, nSize, pSignature, eLogicOp);
- }
- bool CMemFileObject::Compare(IN LPBYTE pData, IN INT nSize, IN LPBYTE pSignature, IN BAV_SIGN_LOGIC_OPERATION eLogicOp)
- {
- ASSERT(m_eObjType==BO_MEM_FILE);
- ASSERT(nSize<=MAX_SIGNATURE_LEN);
- if(!IsOpened())
- {
- ASSERT(false);
- return false;
- }
- // Signature position exceed the file size
- if(pData<m_pFile || pData>(m_pFile+m_dwObjSize))
- {
- // not match
- return false;
- }
- int nRet = memcmp(pData, pSignature, nSize);
- switch(eLogicOp)
- {
- case BL_EQUAL:
- return nRet==0;
- case BL_NOT_EQUAL:
- return nRet!=0;
- default:
- ASSERT(false);
- }
- return false;
- }
- bool CMemFileObject::IsOpened()
- {
- return m_pFile!=NULL;
- }
- LPBYTE CMemFileObject::GetBuffer()
- {
- ASSERT(IsOpened());
- return m_pFile;
- }