Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.0 KiB

  1. // (c) 2002 Microsoft Corporation
  2. // [jorgeba] Jorge Peraza
  3. #include "StdAfx.h"
  4. #include "fastfilehash.h"
  5. CFastFileHash::CFastFileHash(void):m_hFile(0),m_iFileSize(0)
  6. {
  7. }
  8. CFastFileHash::~CFastFileHash(void)
  9. {
  10. }
  11. __int32* CFastFileHash::getHash(TCHAR *sFileName)
  12. {
  13. __int32* iHash = NULL;
  14. if(sFileName==NULL)
  15. {
  16. return NULL;
  17. }
  18. if(!openFile(sFileName))
  19. {
  20. return NULL;
  21. }
  22. iHash = calcHash();
  23. CloseHandle(m_hFile);
  24. return iHash;
  25. }
  26. int CFastFileHash::openFile(TCHAR *sFileName)
  27. {
  28. m_hFile = CreateFile(sFileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_ALWAYS,0,NULL);
  29. if(m_hFile==INVALID_HANDLE_VALUE)
  30. {
  31. return FALSE;
  32. }
  33. else
  34. {
  35. return TRUE;
  36. }
  37. }
  38. __int32* CFastFileHash::calcHash()
  39. {
  40. int iFlag = 0;
  41. int iNdx = 0;
  42. int iNumOfParts = 0;
  43. char bFilePart[PART_SIZE];
  44. __int32* piRes = NULL;
  45. //Clear common vars
  46. m_iFileSize = 0;
  47. //Create the memory for the hash
  48. piRes = new __int32[5];
  49. if(piRes==NULL)
  50. {
  51. return NULL;
  52. }
  53. memset(piRes,0,sizeof(__int32)*5);
  54. //First, get the size of the file
  55. m_iFileSize = GetFileSize(m_hFile,NULL);
  56. iNumOfParts = m_iFileSize / PART_SIZE;
  57. //Fill the parts buffer
  58. for(iNdx=0;iNdx<MAX_PARTS;iNdx++)
  59. {
  60. //Clear the buffer
  61. memset(bFilePart,0,PART_SIZE);
  62. if(iFlag!=1)
  63. {
  64. if(!getPart(bFilePart,iNdx))
  65. {
  66. iFlag = 1;
  67. }
  68. else
  69. {
  70. doHash(piRes,bFilePart);
  71. }
  72. }
  73. }
  74. //Fill the last block with m_iFileSize
  75. memset(bFilePart,0,PART_SIZE);
  76. memcpy(bFilePart,&m_iFileSize,sizeof(m_iFileSize));
  77. doHash(piRes,bFilePart);
  78. return piRes;
  79. }
  80. int CFastFileHash::getPart(char* pBuffer,int iPart)
  81. {
  82. int hRes = FALSE;
  83. DWORD iBytes;
  84. hRes = ReadFile(m_hFile,pBuffer,PART_SIZE,(LPDWORD) &iBytes,NULL);
  85. return hRes;
  86. }
  87. void CFastFileHash::doHash(__int32* piHash,char* pBuffer)
  88. {
  89. for(int iNdx=0;iNdx<5;iNdx++)
  90. {
  91. piHash[iNdx] = piHash[iNdx] ^ *((__int32*)pBuffer + iNdx);
  92. }
  93. }