FileObject.cpp
资源名称:BAV.v2.rar [点击查看]
上传用户:chaoyu
上传日期:2013-04-28
资源大小:18k
文件大小:2k
源码类别:
杀毒
开发平台:
Visual C++
- #include "StdAfx.h"
- #include ".fileobject.h"
- CFileObject::CFileObject(void)
- {
- m_eObjType = BO_PHY_FILE;
- m_hFile = INVALID_HANDLE_VALUE;
- m_dwObjSize = 0;
- }
- CFileObject::CFileObject(CFileObject& cFileObj)
- {
- m_eObjType = BO_PHY_FILE;
- m_hFile = INVALID_HANDLE_VALUE;
- m_dwObjSize = cFileObj.m_dwObjSize;
- m_strObjName= cFileObj.m_strObjName;
- }
- CFileObject::~CFileObject(void)
- {
- if(m_hFile!=INVALID_HANDLE_VALUE)
- Close();
- }
- bool CFileObject::SetObjectName(CString* pstrName)
- {
- ASSERT(pstrName);
- if(pstrName==NULL)
- return false;
- m_strObjName = *pstrName;
- return true;
- }
- PCSTR CFileObject::GetObjectName()
- {
- return (PCSTR)m_strObjName;
- }
- bool CFileObject::Open(bool bScan)
- {
- DWORD dwDesiredAccess, dwShareMode;
- if(bScan)
- {
- dwDesiredAccess = GENERIC_READ;
- dwShareMode = FILE_SHARE_WRITE;
- }
- else
- {
- dwDesiredAccess = GENERIC_WRITE;
- dwShareMode = FILE_SHARE_DELETE;
- }
- m_hFile = ::CreateFile(m_strObjName, dwDesiredAccess, dwShareMode, NULL, OPEN_EXISTING, 0, NULL);
- m_dwObjSize = GetFileSize(m_hFile, NULL);
- return m_hFile != INVALID_HANDLE_VALUE;
- }
- bool CFileObject::Close()
- {
- bool bRet = ::CloseHandle(m_hFile)==TRUE;
- m_hFile = INVALID_HANDLE_VALUE;
- return bRet;
- }
- bool CFileObject::Compare(IN INT nOffset, IN INT dwSize, IN LPBYTE pSignature, IN BAV_SIGN_LOGIC_OPERATION eLogicOp)
- {
- ASSERT(m_eObjType==BO_PHY_FILE);
- ASSERT(dwSize<=MAX_SIGNATURE_LEN);
- if(!IsOpened())
- {
- ASSERT(false);
- return false;
- }
- // Signature position exceed the file size
- if( (nOffset+dwSize)>(INT)m_dwObjSize )
- return false;
- if(INVALID_SET_FILE_POINTER==::SetFilePointer(m_hFile, nOffset, NULL, FILE_BEGIN))
- return false;
- // read and compare
- BYTE fbuf[MAX_SIGNATURE_LEN];
- DWORD dwReadBytes;
- if( ::ReadFile(m_hFile, fbuf, dwSize, &dwReadBytes, NULL) && dwReadBytes==dwSize)
- {
- int nRet = memcmp(fbuf, pSignature, dwSize);
- switch(eLogicOp)
- {
- case BL_EQUAL:
- return nRet==0;
- case BL_NOT_EQUAL:
- return nRet!=0;
- default:
- ASSERT(false);
- }
- }
- return false;
- }
- bool CFileObject::Compare(IN LPBYTE pData, IN INT nSize, IN LPBYTE pSignature, IN BAV_SIGN_LOGIC_OPERATION eLogicOp)
- {
- // CFileObject not support this
- ASSERT(false);
- return false;
- }
- bool CFileObject::IsOpened()
- {
- return m_hFile!=INVALID_HANDLE_VALUE;
- }
- BAV_OBJ_TYPE CFileObject::GetObjectType()
- {
- return m_eObjType;
- }
- DWORD CFileObject::GetObjectSize()
- {
- return m_dwObjSize;
- }