UNTESTED: Here is a very short CRC32 function (could be made slightly faster, but longer). It does not use tables. The C source code is:
unsigned int CRC32(unsigned char *data, int len, unsigned int result) {
int i,j; unsigned char octet; // INIT: result = -1, CONT: ~last_result
for (i=0; i<len; i++) {
octet = *(data++);
for (j=0; j<8; j++) {
if ((octet >> 7) ^ (result >> 31))
result = (result << 1) ^ 0x04c11db7;
else
result = (result << 1);
octet <<= 1;
}
}
return ~result;
}
A possible AHK wrapper:
CRC32(ByRef Buffer, Bytes=0, Start=-1) {
Static f
If f =
MCode(f,"558bec8b450c85c07e3a8b5508535689450c8b4510578a0a6a08425e8bf80fb6d9c1e"
. "f1833fb03c0f7c780ffffff740535b71dc10402c94e75e2ff4d0c75d75f5e5beb038b4510f7d05dc3")
If Bytes <= 0
Bytes := StrLen(Buffer)
Return DllCall(&f, "uint",&Buffer, "uint",Bytes, "int",Start, "cdecl uint")
}
It does not have the length field auto-attached to the input (no padding), but should be OK for home use. You can continue the CRC calculation for other parts of a large data set, if you set the Start parameter the bitwise complement of the result of the previous part. Test it with:
SetFormat Integer, HEX
a = ABCDEFGHIJKLMNOPQRSTUVWXYZ
MsgBox % CRC32(a)
CRC32(ByRef Buffer, Bytes=0, Start=-1) {
Static f
If f =
MCode(f,"558bec8b450c85c07e3a8b5508535689450c8b4510578a0a6a08425e8bf80fb6d9c1e"
. "f1833fb03c0f7c780ffffff740535b71dc10402c94e75e2ff4d0c75d75f5e5beb038b4510f7d05dc3")
If Bytes <= 0
Bytes := StrLen(Buffer)
Return DllCall(&f, "uint",&Buffer, "uint",Bytes, "int",Start, "cdecl uint")
}
MCode(ByRef code, hex) { ; allocate memory and write Machine Code there
VarSetCapacity(code,StrLen(hex)//2)
Loop % StrLen(hex)//2
NumPut("0x" . SubStr(hex,2*A_Index-1,2), code, A_Index-1, "Char")
}