Skip to content

Instantly share code, notes, and snippets.

@zer0cat
Created January 21, 2020 10:57
Show Gist options
  • Select an option

  • Save zer0cat/34d018b567d85b4663b4fb637d718140 to your computer and use it in GitHub Desktop.

Select an option

Save zer0cat/34d018b567d85b4663b4fb637d718140 to your computer and use it in GitHub Desktop.
calc crc32 for files
#include <windows.h>
typedef DWORD (__stdcall *_RtlComputeCrc32)(DWORD, const BYTE*, INT);
#define CRC32_BLOCK_SIZE 1024
DWORD get_crc32_of_file(LPCSTR sFileName)
{
_RtlComputeCrc32 RtlComputeCrc32 = (_RtlComputeCrc32)GetProcAddress(LoadLibraryA("ntdll.dll"), "RtlComputeCrc32");
HANDLE hFile = CreateFileA(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
BYTE aBuf[CRC32_BLOCK_SIZE];
DWORD dwCrc32 = 0, dwBytesRead;
while (ReadFile(hFile, aBuf, CRC32_BLOCK_SIZE, &dwBytesRead, NULL) && dwBytesRead != 0)
{
dwCrc32 = RtlComputeCrc32(dwCrc32, aBuf, dwBytesRead);
}
CloseHandle(hFile);
return dwCrc32;
}
return -1;
}
void entry()
{
char *aFiles[] = {"test_1.txt", "test_2.txt", "test_3.txt"};
for (size_t i = 0; i < sizeof(aFiles) / sizeof(aFiles[0]); i++) {
char aBuf[MAX_PATH + 20];
wsprintfA(aBuf, "%s = %08X", aFiles[i], get_crc32_of_file(aFiles[i]));
OutputDebugStringA(aBuf);
}
ExitProcess(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment