Double click on the CRCCalculator.EXE icon to start the program
(Windows), or run the program under Linux using ./CrcCalculator..

Enter any text in the edit box.

Observe the CRC-16 and CRC-32 values in decimal or hexadecimal.

Select File tabsheet.

Click on the Read button and select a file

Observe the CRC-16 and CRC-32 values in decimal or hexadecimal.

Expected Values
Hex values do not change by version of Delphi, but the decimal values are intended to be
unsigned. CRC-16 values have always been unsigned, but since there was no 4-byte
unsigned integer in D1-D3, the decimal values are signed for the CRC-32 until the Delphi 4
version.

DiscussionCRC values, especially the CRC-32, are an extremely good way to verify the
integrity of a file. If the CRC-32 for a file stays the same, there is only an
extremely small probability that the file has been changed -- about 1 in 4 billion.
CRCs could be used as a preliminary verification tool to find identical files. If
the CRCs of two files do not match, the file are not the same. This could even be
used to compare image files.

Lookup Tables. The "hardware" method of computing CRCs involves bit
manipulations, which is very inefficient for a software computation. Instead of
computing the CRC bit-by-bit, a 256-element lookup table can be used to perform the
equivalent of 8 bit operations at a time. (This is described in "Byte-wise CRC
Calculations" in IEEE Micro, June 1983, pp. 40-50.)

For a CRC-16, the lookup table consists of 256 2-byte WORDs (see
below, or the CRC16.PAS
unit for the actual table, or the CRCTable program
for computation of the lookup table for the x16 + x15 + x2 + 1
generator polynomial):

{The following is a little cryptic (but executes very quickly).
The algorithm is as follows:
1. exclusive-or the input byte with the low-order byte of
the CRC register to get an INDEX
2. shift the CRC register eight bits to the right
3. exclusive-or the CRC register with the contents of
Table[INDEX]
4. repeat steps 1 through 3 for all bytes}
BEGIN
q := p;
FOR i := 1 TO nBYTE DO
BEGIN
CRCvalue := Hi(CRCvalue) XOR Table[ q^ XOR Lo(CRCvalue) ];
INC(q)
END
END {CalcCRC16};

For a CRC-32, the lookup table consists of 256 4-byte
DWORDs (also see the CRC32.PAS unit).

// The constants here are for the CRC-32 generator
// polynomial, as defined in the Microsoft
// Systems Journal, March 1995, pp. 107-108

// Use CalcCRC32 as a procedure so CRCValue can be passed in
but
// also returned. This allows multiple calls to CalcCRC32 for
// the "same" CRC-32 calculation.
PROCEDURE CalcCRC32 (p: pointer; ByteCount: DWORD; VAR CRCValue: DWORD);
// The following is a little cryptic (but executes very quickly).
// The algorithm is as follows:
// 1. exclusive-or the input byte with the low-order byte of
// the CRC register to get an INDEX
// 2. shift the CRC register eight bits to the right
// 3. exclusive-or the CRC register with the contents of Table[INDEX]
// 4. repeat steps 1 through 3 for all bytes

To compute the same CRC-32 as used in the PKZIP utility, start with a CRCvalue
of $FFFFFFFF. After calling CalcCRC32 above (any number of times), the
finalization consists of a 1's complement of the CRCvalue. This can be
computed with the expression NOT CRCvalue in Delphi. See
additional details in the next section.

Initialization and
Finalization. The initialization and finalization of the CRC computation are arbitrary.
Many years ago when I first started computing CRCs, I set the initial
value to 0 and did no finalization -- this is "Method 1" described
above under Expected Values.

The "standard" CRC-32 (the one used by PKZIP) starts
with $FFFFFFFF as the initial value and then performs a 1's complement to yield the final
value -- this is "Method 2" described above under Expected
Values. Here's what is done in the CRC Calculator for CRC-32s:

[Thanks to Rolf Gebhardt and Glen Harman for pointing out an inconsistency
about how finalization was handled in an earlier version of this article.]

CRC of a File. All the bytes of a file must be
passed to the CalcCRC routines, i.e., CalcCRC16 and CalcCRC32, to compute the
CRC of a file. The older BlockRead I/O primitive is used in the
CalcFileCRC16 routine in the CRC16 unit since BlockRead at one point was the
only way to read a binary stream of bytes. CalcFileCRC32 uses the more
contemporary memory stream to read the bytes of a file (when the StreamIO
conditional compilation is defined).

The above procedure assumes that the file will easily fit into
memory in a TMemoryStream. Unfortunately, this can be a bad assumption
especially when some files are bigger than physical memory. For example,
processing a 1 GB file in a memory stream with only 512 MB of physical memory
might at minimum will tax the virtual memory processing of the operating
system. For now, this isn't much of a problem.

FileCheck Program. See the FileCheck Lab Report for information
about creating CRCs of files, directories, or even whole volumes.
("Meta" CRCs -- that is, CRCs of CRCs of a well-ordered list of files
-- used to detect changes in directories or whole disk volumes.)

Command Line Programs. The command line examples, CRC16Dem and CRC32Dem can be
compiled from a DOS Window (assuming your path contains the Delphi bin directory)
by entering:

DCC32 CRC16Dem.PAS
orDCC32 CRC32Dem.PAS

Study the CRC16Dem and CRC32Dem command line programs
for a way to calculate CRCs without a Windows interface.

The Delphi installation CD has a file CRC32.C, which shows how
to compute CRC-32s, as well as the lookup table, in the directory \Info\Extras\Zlib\Src.

Peter Haas' unit contains functions to calculate a arbitrary CRC
(up to 32 bit) by given parameters (Polynom, Init, XorOut, ReflectIn, ReflectOut).
A
another part is the generation of a Lookup Table and the calculation with this
table. The unit can also used to find the parameters for a unknown CRC
calculation with trial and error. Last but not least, it contain the unit a
function, that creates Delphi source to calculate a CRC with the given
parameters in a separate application. http://delphi.pjh2.de/units/download/CRCs.zip

"Byte-wise CRC Calculations" by Aram Perez in IEEE Micro,
June 1983, pp. 40-50. Shows how to create a lookup table which is the best way to
implement in software (versus the shifts that are done when implemented in
hardware).

Borland C++ 5.02 "C" CRC-32 Source and EXE (45 KB):
CRCc.ZIP
Use the "make file" to compile: make -f
crc32.mak
Modify .mak file to point to correct location of wildargs.obj.
The .mak file automatically performs a test that the results will match those of
PKZIP.) This command-line utility can be used with wildcards to find the CRC-32 of
files in a directory, for example: crc32 *.*