spiegel-keyman/windows/src/engine/mcompile/mc_crc32.cpp
Marc Durdin fe2c1cd187 fix(windows): incxstr could run over buffer with malformed data
Fixes #4591.

I fixed incxstr in 4 places:

1. Common/Core: kmx_xstring.cpp
2. Engine: xstring.cpp
3. Test project importkeyboard importkeyboard.cpp
4. Test project m-to-p m-to-p.cpp

I updated mcompile to remove its own copy of incxstr (identical to that
in xstring.cpp) to reduce WETness but opted not to do so for the test
apps, which are pretty much throwaway anyway.

I note that there is more work we could do here; we need to check every
character as we increment so we don't miss a `U+0000` end of string with
malformed data. But I would like to tackle that as a separate job at
some point in the future after Core integration.
2021-03-05 12:50:29 +11:00

64 lines
1.1 KiB
C++

#include "pch.h"
#define CRC32_POLYNOMIAL 0xEDB88320
unsigned long CRCTable[256];
int TableBuilt = 0;
void BuildCRCTable()
{
int i, j;
unsigned long crc;
if(!TableBuilt)
for(i = 0; i < 256; i++)
{
crc = i;
for(j = 8; j >= 1; j--)
if((crc & 1)) crc = (crc >> 1) ^ CRC32_POLYNOMIAL; else crc >>= 1;
CRCTable[i] = crc;
}
TableBuilt = 1;
}
/*
* This routine calculates the CRC for a block of data using the
* table lookup method. It accepts an original value for the crc,
* and returns the updated value.
*/
unsigned long CalculateBufferCRC(unsigned char *p, unsigned long count)
{
unsigned long temp1, temp2, crc;
crc = 0xFFFFFFFF;
if(!TableBuilt) BuildCRCTable();
while(count > 0)
{
temp1 = (crc >> 8) & 0x00FFFFFF;
temp2 = CRCTable[((int)crc ^ (int)*p) & 0xFF];
crc = temp1 ^ temp2;
p++; count--;
}
return crc;
}
unsigned long CalculateBufferCRC(unsigned long count, unsigned char *p)
{
return CalculateBufferCRC(p, count);
}
void Dehash(unsigned char *buf, unsigned long len)
{
while(len > 0)
{
*buf = *buf ^ 0x6D;
buf++; len--;
}
}