Whiz Kid Technomagic Fast CRC-32 Routines

This is a Unix port of fast assembly language routines for CRC-32 calculation I originally wrote in 1997 as a Windows 95 DLL.

Calculating CRC-32

To calculate CRC-32, we must first initialize CRC to 0xFFFFFFFF. The initcrc32 function does that. Then we need to calculate an intermediate CRC-32 for each byte in the stream of bytes we are working with. After that, we need to convert the result to the final CRC-32. The finishcrc32 function performs this operation.

The fast CRC-32 routines

These routines are written in Intel assembly language and work on anything from i386 on. Here is a quick reference:

unsigned int initcrc32(void); Implemented as a macro, unless you define NOCRC32MACROS before including crc32.h. It simply returns 0xFFFFFFFF.

unsigned int finishcrc32(unsigned int const crc); Also a macro (under same conditions). It converts the result of all intermediate calculations into final CRC-32.

unsigned int crc32(unsigned int const oldcrc, unsigned char const abyte); This is the work horse that calculates intermediate CRC-32 on individual bytes of a stream. Before it processes the first byte, initcrc32 must be called. At the end, finishcrc32 must be called.

Example:

#include "crc32.h"

unsigned int checkstream(unsigned char *stream, unsigned int streamsize) {
	unsigned int i;
	unsigned int crc;

	crc = initcrc32();

	for (i = 0; i < streamsize; i++)
		crc = crc32(crc, stream[i]);

	return finishcrc32(crc);
}

unsigned int partialcrc32(unsigned int const oldcrc, unsigned char const *stream, unsigned int const streamsize); Calculates intermediate CRC-32 on a partial stream of bytes.

Example:

#include "crc32.h"

unsigned int checkstreams(unsigned char *stream1, unsigned int streamsize1,
	unsigned char *stream2, unsigned int streamsize2) {

	unsigned int crc = initcrc32();

	crc = partialcrc32(crc, stream1, streamsize1);
	crc = partialcrc32(crc, stream2, streamsize2);

	return finishcrc32(crc);
}

If you like terse but hard-to-read code, you could change that example to:

#include "crc32.h"

unsigned int checkstreams2(unsigned char *stream1, unsigned int streamsize1,
	unsigned char *stream2, unsigned int streamsize2) {

	return finishcrc32(partialcrc32(partialcrc32(initcrc32(),
		stream1, streamsize1), stream2, streamsize2));
}

unsigned int arraycrc32(unsigned char const *stream, unsigned int const streamsize); This function is used on a complete stream of bytes. It does the initialization and final conversion internally. It is functionally identical to the checkstream() example above.

Example:

#include "crc32.h"

unsigned int checkstream2(unsigned char *stream, unsigned int streamsize) {

	return arraycrc32(stream, streamsize);
}

Sample Program

A sample program, fc32.c, is included with the routines. This program calculates CRC-32 of a file passed to it through the command line. If no file is specified, it reads stdin and calculates the CRC-32 of whatever is entered.

Legalities

You may use these routines freely for whatever purposes you want as long as you give me credit for creating them.

That means you need to include something like this in your program documentation:

“This program includes Whiz Kid Technomagic Fast CRC-32 Routines, which are Copyright 1997 G. Adam Stanislav. All rights reserved. These routines are available on the World-wide Web at http://www.whizkidtech.redprince.net/crc32/.”

If you distribute your program on the Web, you also need to place that notice on your web site, and include a hyperlink to the above-mentioned site.

[ Download ] [ Home ] [ Contact ]