📄 efg's mathematics projects -- crc calculator.htm
字号:
<P align=left>For a CRC-16, the lookup table consists of 256 2-byte WORDs (see
the <EM>CRC16.PAS</EM> unit for the actual table, or the <A
href="http://plum.ia.polsl.gliwice.pl/~DIP/efg/Mathematics/CRCTable16.pas.txt"
tppabs="http://www.efg2.com/Lab/Mathematics/CRCTable16.pas.txt">CRCTable
program</A> for computation of the lookup table for the x<SUP>16</SUP> +
x<SUP>15</SUP> + x<SUP>2</SUP> + 1 generator polynomial). For a CRC-32,
the lookup table consists of 256 4-byte DWORDs (see the CRC32.PAS unit for the
actual table). Given the lookup <EM>Table</EM>, the code for computing a
CRC-32 is as follows:</P>
<DIV align=center>
<CENTER>
<TABLE border=0>
<TBODY>
<TR>
<TD><FONT face="Courier New">// Use CalcCRC32 as a procedure so CRCValue
can be passed in but<BR>// also returned. This allows multiple calls to
CalcCRC32 for<BR>// the "same" CRC-32 calculation.<BR>PROCEDURE CalcCRC32
(p: pointer; ByteCount: DWORD; VAR CRCValue: DWORD);<BR> // The
following is a little cryptic (but executes very quickly).<BR> //
The algorithm is as follows:<BR> // 1. exclusive-or the input byte
with the low-order byte of<BR> // the CRC register to get an
INDEX<BR> // 2. shift the CRC register eight bits to the
right<BR> // 3. exclusive-or the CRC register with the contents of
Table[INDEX]<BR> // 4. repeat steps 1 through 3 for all
bytes<BR><BR> VAR<BR> i:
DWORD;<BR> q: ^BYTE;<BR>BEGIN<BR> q :=
p;<BR> FOR i := 0 TO ByteCount-1 DO<BR>
BEGIN<BR> CRCvalue := (CRCvalue SHR 8)
XOR<BR> Table[ q^ XOR (CRCvalue AND $000000FF)
];<BR> INC(q)<BR> END<BR>END
{CalcCRC32};</FONT></TD></TR></TBODY></TABLE></CENTER></DIV>
<P align=left>You can pass nearly any argument to this routine since the first
parameter is a pointer. For a string, pass the address of the first
character, for example:</P>
<P align=left> <FONT face="Courier New">CalcCRC32
(Addr(s[1]), LENGTH(s), CRC32);</FONT></P>
<P align=left>To avoid an access violation in Delphi 4 (or later) make sure
Length(s) > 0. (I'm not sure why Delphi 3 didn't complain.)</P>
<P align=left>To compute the same CRC-32 as used in the PKZIP utility, start
with a <EM>CRCvalue</EM> of $FFFFFFFF. After calling <EM>CalcCRC32
</EM>above (any number of times), the finalization consists of a 1's complement
of the <EM>CRCvalue</EM>. This can be computed with the expression <EM>NOT
CRCvalue</EM> in Delphi.</P>
<P align=left>The initialization and finalization of the CRC computation are
arbitrary. As mentioned above, 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. Here's what is done in the CRC
Calculator for CRC-32s:</P>
<DIV align=center>
<CENTER>
<TABLE border=0>
<TBODY>
<TR>
<TD width="100%"><FONT face="Courier New">CRC32 := $FFFFFFFF; // To match
PKZIP<BR>IF LENGTH(s) > 0 // Avoid access violation in
D4<BR>THEN CalcCRC32 (Addr(s[1]), LENGTH(s), CRC32);<BR>CRC32 := NOT
CRC32; // TO match PKZIP<BR><BR>CRC32Decimal.Caption :=
IntToStr(CRC32);<BR>CRC32Hex.Caption :=
IntToHex(CRC32,8)</FONT></TD></TR></TBODY></TABLE></CENTER></DIV>
<P align=left>[Thanks to Rolf Gebhardt and Glen Harman for pointing out an
inconsistency about how finalization was handled in an earlier version of this
article.]</P>
<P align=left>The CRC-16 computation starts with an initial value of 0 and
performs no finalization. Here's what is done in in the CRC Calculator for
CRC-16s:</P>
<DIV align=center>
<CENTER>
<TABLE border=0>
<TBODY>
<TR>
<TD width="100%"><FONT face="Courier New">CRC16 := 0;
// Could use $FFFF or other initial value<BR>IF LENGTH(s)
> 0 // Avoid access violation in D4<BR>THEN CalcCRC16 (Addr(s[1]),
LENGTH(s), CRC16);<BR><BR>CRC16Decimal.Caption :=
IntToStr(CRC16);<BR>CRC16Hex.Caption :=
IntToHex(CRC16,4);</FONT></TD></TR></TBODY></TABLE></CENTER></DIV>
<P align=left>See the <A
href="http://plum.ia.polsl.gliwice.pl/~DIP/efg/Mathematics/FileCheck.htm"
tppabs="http://www.efg2.com/Lab/Mathematics/FileCheck.htm">FileCheck</A> 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.)</P>
<P align=left>The command line examples, <EM>CRC16Dem</EM> and <EM>CRC32Dem</EM>
can be compiled from a DOS Window (assuming your path contains the Delphi
<EM>bin </EM>directory) by entering:</P>
<P align=left> <FONT face="Courier New">DCC32
CRC16Dem.PAS </FONT> or<BR> <FONT
face="Courier New">DCC32 CRC32Dem.PAS</FONT></P>
<P align=left>Study the <EM>CRC16Dem</EM> and <EM>CRC32Dem</EM> command line
programs for a way to calculate CRCs without a Windows interface.</P>
<P align=left>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.</P>
<P align=left><A name=Links></A><STRONG><BIG>CRC Links</BIG></STRONG></P>
<P align=left>CRC definition from <BR>- <A
href="javascript:if(confirm('http://www.oreilly.com/reference/dictionary/terms/C/Cyclic_Redundancy_Code.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.oreilly.com/reference/dictionary/terms/C/Cyclic_Redundancy_Code.htm'"
tppabs="http://www.oreilly.com/reference/dictionary/terms/C/Cyclic_Redundancy_Code.htm">Dictionary
of PC Hardware and Data Communications Terms<BR></A>- <A
href="javascript:if(confirm('http://burks.bton.ac.uk/burks/foldoc/35/27.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://burks.bton.ac.uk/burks/foldoc/35/27.htm'"
tppabs="http://burks.bton.ac.uk/burks/foldoc/35/27.htm">Online Dictionary of
Computing</A></P>
<P align=left>A Painless Guide to CRC Error Detection Algorithms<BR><A
href="javascript:if(confirm('http://www.ross.net/crc/crcpaper.html%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.ross.net/crc/crcpaper.html'"
tppabs="http://www.ross.net/crc/crcpaper.html">www.ross.net/crc/crcpaper.html</A> <BR><A
href="javascript:if(confirm('http://www.microconsultants.com/tips/crc/crc.txt%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.microconsultants.com/tips/crc/crc.txt'"
tppabs="http://www.microconsultants.com/tips/crc/crc.txt">www.microconsultants.com/tips/crc/crc.txt</A> <BR><A
href="javascript:if(confirm('http://www.geocities.com/SiliconValley/Pines/8659/crc.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.geocities.com/SiliconValley/Pines/8659/crc.htm'"
tppabs="http://www.geocities.com/SiliconValley/Pines/8659/crc.htm">www.geocities.com/SiliconValley/Pines/8659/crc.htm</A> </P>
<P align=left>Peter Haas' Delphi unit for demonstration calculation of CRC,
based on the document: "A Painless Guide to CRC Error Detection
Algorithms"<BR><A
href="javascript:if(confirm('ftp://ftp.rocksoft.com/papers/crc_v3.txt%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='ftp://ftp.rocksoft.com/papers/crc_v3.txt'"
tppabs="ftp://ftp.rocksoft.com/papers/crc_v3.txt">ftp://ftp.rocksoft.com/papers/crc_v3.txt</A></P>
<P align=left>Peter Haas' unit contains functions to calculate a arbitrary CRC
(up to 32 bit) by given parameters (Polynom, Init, XorOut, ReflectIn,
ReflectOut). A<BR>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. <A
href="javascript:if(confirm('http://home.t-online.de/home/PeterJHaas/Download/CRCs.zip%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://home.t-online.de/home/PeterJHaas/Download/CRCs.zip'"
tppabs="http://home.t-online.de/home/PeterJHaas/Download/CRCs.zip">http://home.t-online.de/home/PeterJHaas/Download/CRCs.zip</A></P>
<P align=left>Peter Haas' older unit with a very fast CRC-32 implementation
(Assembler + Lookup Table)<BR><A
href="javascript:if(confirm('http://home.t-online.de/home/PeterJHaas/Download/CRC32.zip%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://home.t-online.de/home/PeterJHaas/Download/CRC32.zip'"
tppabs="http://home.t-online.de/home/PeterJHaas/Download/CRC32.zip">http://home.t-online.de/home/PeterJHaas/Download/CRC32.zip</A><BR><BR>Cyclic
Redundancy Check Computation (Texas Instruments Application Report)<BR>- Coding
theory behind CRC<BR>- Algorithms for CRC computation<BR><A
href="javascript:if(confirm('http://www.ti.com/sc/docs/psheets/abstract/apps/spra530.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.ti.com/sc/docs/psheets/abstract/apps/spra530.htm'"
tppabs="http://www.ti.com/sc/docs/psheets/abstract/apps/spra530.htm">www.ti.com/sc/docs/psheets/abstract/apps/spra530.htm</A> <BR><A
href="javascript:if(confirm('http://www-s.ti.com/sc/psheets/spra530/spra530.pdf%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www-s.ti.com/sc/psheets/spra530/spra530.pdf'"
tppabs="http://www-s.ti.com/sc/psheets/spra530/spra530.pdf">http://www-s.ti.com/sc/psheets/spra530/spra530.pdf</A> </P>
<P align=left>"For the Love of the Game" by Michael Barr, <EM>Embedded Systems
Programming</EM>, Dec. 1999, pp. 47-54.<BR><A
href="javascript:if(confirm('http://www.embedded.com/internet/9912/9912connect.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.embedded.com/internet/9912/9912connect.htm'"
tppabs="http://www.embedded.com/internet/9912/9912connect.htm">www.embedded.com/internet/9912/9912connect.htm</A> </P>
<P align=left>"Slow and Steady Never Lost the Race" by Michael Barr,
<EM>Embedded Systems Programming</EM>, Jan. 2000, pp. 37-46. Shows how to
compute CRC lookup table. <A
href="javascript:if(confirm('http://www.embedded.com/internet/0001/0001connect.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.embedded.com/internet/0001/0001connect.htm'"
tppabs="http://www.embedded.com/internet/0001/0001connect.htm">www.embedded.com/internet/0001/0001connect.htm</A> </P>
<P align=left><EM>The CRC Pitstop</EM> is a repository for information on CRC
and other checking algorithms<BR><A
href="javascript:if(confirm('http://www.ross.net/crc%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.ross.net/crc'"
tppabs="http://www.ross.net/crc">http://www.ross.net/crc</A> </P>
<P align=left>CRC and How to Reverse it<BR><A
href="javascript:if(confirm('http://members.dencity.com/jas/fravia/crctut1.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://members.dencity.com/jas/fravia/crctut1.htm'"
tppabs="http://members.dencity.com/jas/fravia/crctut1.htm">http://members.dencity.com/jas/fravia/crctut1.htm</A> </P>
<P align=left>CRC - Der Cyclic Redundancy Code (in German)<BR><A
href="javascript:if(confirm('http://www.informatik.uni-frankfurt.de/~haase/crc.html%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.informatik.uni-frankfurt.de/~haase/crc.html'"
tppabs="http://www.informatik.uni-frankfurt.de/~haase/crc.html">www.informatik.uni-frankfurt.de/~haase/crc.html</A></P>
<P align=left>Fehlererkennung Cyclic Redundancy Check Code (in German)<BR><A
href="javascript:if(confirm('http://www.mountpoint.ch/~unique/project/crc%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.mountpoint.ch/~unique/project/crc'"
tppabs="http://www.mountpoint.ch/~unique/project/crc">www.mountpoint.ch/~unique/project/crc</A> </P>
<P align=left>For information about CRC theory:<BR><A
href="javascript:if(confirm('http://www.seanet.com/~ksbrown/kmath458.htm%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.seanet.com/~ksbrown/kmath458.htm'"
tppabs="http://www.seanet.com/~ksbrown/kmath458.htm">www.seanet.com/~ksbrown/kmath458.htm</A>
</P>
<P align=left>Understanding Cyclic Redundancy Check<BR><A
href="javascript:if(confirm('http://www.acius.com/ACIDOC/CMU/CMU79909.HTM%20%20\n\nThis%20file%20was%20not%20retrieved%20by%20Teleport%20Pro,%20because%20it%20is%20addressed%20on%20a%20domain%20or%20path%20outside%20the%20boundaries%20set%20for%20its%20Starting%20Address.%20%20\n\nDo%20you%20want%20to%20open%20it%20from%20the%20server?'))window.location='http://www.acius.com/ACIDOC/CMU/CMU79909.HTM'"
tppabs="http://www.acius.com/ACIDOC/CMU/CMU79909.HTM">www.acius.com/ACIDOC/CMU/CMU79909.HTM</A>
</P>
<P align=left>Various CRC and Checksum Functions (C Code)<BR><A
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -