I'm trying to map a file to memory, and calc its hash:
// Declaration in header file which I don't control
void SymCryptSha256(PCBYTE pbData, SIZE_T cbData, PBYTE pbResult);
// MY code
HANDLE hFile = ::CreateFile(...);
HANDLE hMap = ::CreateFileMapping(hFile, nullptr, PAGE_READONLY, 0, 0, nullptr));
byte* pMap = ::MapViewOfFile(hMap, FILE_MAP_READ, 0, 0, 0);
BYTE[64] hash;
ULARGE_INTEGER li;
li.LowPart = ::GetFileSize(file , &li.HighPart);
// This compiles in 64-bit, but errors in 32-bit:
// error C4244: 'argument': conversion from 'ULONGLONG' to 'SIZE_T', possible loss of data
::SymCryptSha256(pMap, li.QuadPart, &hash);
This is because SymCryptSha256
's second argument is SIZE_T
, which in 32-bit compilation is 32-bit. The desired behavior is:
- 64-bit: Use the entire size, which is
li.QuadPart
- 32-bit: In case size>4GB, MapViewOfFile would fail anyways. So, just use
li.LowPart
.
Looks to me like I'll need to do this with #ifdef
s - is there a more elegant way for it?