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

杀毒

开发平台:

Visual C++

  1. #include "StdAfx.h"
  2. #include ".fileobject.h"
  3. CFileObject::CFileObject(void)
  4. {
  5. m_eObjType = BO_PHY_FILE;
  6. m_hFile = INVALID_HANDLE_VALUE;
  7. m_dwObjSize = 0;
  8. }
  9. CFileObject::CFileObject(CFileObject& cFileObj)
  10. {
  11. m_eObjType = BO_PHY_FILE;
  12. m_hFile = INVALID_HANDLE_VALUE;
  13. m_dwObjSize = cFileObj.m_dwObjSize;
  14. m_strObjName= cFileObj.m_strObjName;
  15. }
  16. CFileObject::~CFileObject(void)
  17. {
  18. if(m_hFile!=INVALID_HANDLE_VALUE)
  19. Close();
  20. }
  21. bool CFileObject::SetObjectName(CString* pstrName)
  22. {
  23. ASSERT(pstrName);
  24. if(pstrName==NULL)
  25. return false;
  26. m_strObjName = *pstrName;
  27. return true;
  28. }
  29. PCSTR CFileObject::GetObjectName()
  30. {
  31. return (PCSTR)m_strObjName;
  32. }
  33. bool CFileObject::Open(bool bScan)
  34. {
  35. DWORD dwDesiredAccess, dwShareMode;
  36. if(bScan)
  37. {
  38. dwDesiredAccess = GENERIC_READ;
  39. dwShareMode = FILE_SHARE_WRITE;
  40. }
  41. else
  42. {
  43. dwDesiredAccess = GENERIC_WRITE;
  44. dwShareMode = FILE_SHARE_DELETE;
  45. }
  46. m_hFile = ::CreateFile(m_strObjName, dwDesiredAccess, dwShareMode, NULL, OPEN_EXISTING, 0, NULL);
  47. m_dwObjSize = GetFileSize(m_hFile, NULL);
  48. return m_hFile != INVALID_HANDLE_VALUE;
  49. }
  50. bool CFileObject::Close()
  51. {
  52. bool bRet = ::CloseHandle(m_hFile)==TRUE;
  53. m_hFile = INVALID_HANDLE_VALUE;
  54. return bRet;
  55. }
  56. bool CFileObject::Compare(IN INT nOffset, IN INT dwSize, IN LPBYTE pSignature, IN BAV_SIGN_LOGIC_OPERATION eLogicOp)
  57. {
  58. ASSERT(m_eObjType==BO_PHY_FILE);
  59. ASSERT(dwSize<=MAX_SIGNATURE_LEN);
  60. if(!IsOpened())
  61. {
  62. ASSERT(false);
  63. return false;
  64. }
  65. // Signature position exceed the file size
  66. if( (nOffset+dwSize)>(INT)m_dwObjSize )
  67. return false;
  68. if(INVALID_SET_FILE_POINTER==::SetFilePointer(m_hFile, nOffset, NULL, FILE_BEGIN))
  69. return false;
  70. // read and compare
  71. BYTE fbuf[MAX_SIGNATURE_LEN];
  72. DWORD dwReadBytes;
  73. if( ::ReadFile(m_hFile, fbuf, dwSize, &dwReadBytes, NULL) && dwReadBytes==dwSize)
  74. {
  75. int nRet = memcmp(fbuf, pSignature, dwSize);
  76. switch(eLogicOp)
  77. {
  78. case BL_EQUAL:
  79. return nRet==0;
  80. case BL_NOT_EQUAL:
  81. return nRet!=0;
  82. default:
  83. ASSERT(false);
  84. }
  85. }
  86. return false;
  87. }
  88. bool CFileObject::Compare(IN LPBYTE pData, IN INT nSize, IN LPBYTE pSignature, IN BAV_SIGN_LOGIC_OPERATION eLogicOp)
  89. {
  90. // CFileObject not support this
  91. ASSERT(false);
  92. return false;
  93. }
  94. bool CFileObject::IsOpened()
  95. {
  96. return m_hFile!=INVALID_HANDLE_VALUE;
  97. }
  98. BAV_OBJ_TYPE CFileObject::GetObjectType()
  99. {
  100. return m_eObjType;
  101. }
  102. DWORD CFileObject::GetObjectSize()
  103. {
  104. return m_dwObjSize;
  105. }