crc32, crc32file

Calculates the CRC-32 of a string or a file. (version 4.60 or later)

crc32 <intvar> <string>
crc32file <intvar> <filename>

Remarks

This macro function calculates the CRC(Cyclic Redundancy Checking) of a string or a file. The polynomial expression(right rotation) is as follows:

100000100110000010001110110110111 (x32+x26+x23+x22+x16+x12+x11+x10+x8+x7+x5+x4+x2+x1+x0)

The calculated value stores the variable "intvar" as mathematical value.
If the <filename> file can not open by using crc32file, the system variable "result" is set to -1.

The CRC algorithm implementation by C language is as follows: This algorithm is often used as the Ethernet FCS(Frame Check Sequence).

static unsigned long crc32(int n, unsigned char c[])
{
#define CRC32POLY1 0x04C11DB7UL
#define CRC32POLY2 0xEDB88320UL  /* left-right reversal */
	int i, j;
	unsigned long r;

	r = 0xFFFFFFFFUL;
	for (i = 0; i < n; i++) {
		r ^= c[i];
		for (j = 0; j < CHAR_BIT; j++)
			if (r & 1) r = (r >> 1) ^ CRC32POLY2;
			else       r >>= 1;
	}
	return r ^ 0xFFFFFFFFUL;
}

Example

str = 'this is a test string to be CRC32ed'
crc32 crc str

; Display CRC32 result asHEX
sprintf '0x%08X' crc
messagebox inputstr 'CRC32 = '

crc32file crc 'foo.bin'
if result = -1 then
    messagebox 'file open error' 'CRC32 = '
else
    sprintf '0x%08X' crc
    messagebox inputstr 'CRC32 = '
endif