You are on page 1of 32

5/15/2015

www.zlib.net/crc_v3.txt

APAINLESSGUIDETOCRCERRORDETECTIONALGORITHMS
==================================================
"EverythingyouwantedtoknowaboutCRCalgorithms,butwereafraid
toaskforfearthaterrorsinyourunderstandingmightbedetected."
Version:3.
Date:19August1993.
Author:RossN.Williams.
Net:ross@guest.adelaide.edu.au.
FTP:ftp.adelaide.edu.au/pub/rocksoft/crc_v3.txt
Company:Rocksoft^tmPtyLtd.
Snail:16LerwickAvenue,HazelwoodPark5066,Australia.
Fax:+6183734911(c/InternodeSystemsPtyLtd).
Phone:+6183799217(10amto10pmAdelaideAustraliatime).
Note:"Rocksoft"isatrademarkofRocksoftPtyLtd,Australia.
Status:Copyright(C)RossWilliams,1993.However,permissionis
grantedtomakeanddistributeverbatimcopiesofthis
documentprovidedthatthisinformationblockandcopyright
noticeisincluded.Also,theCcodemodulesincluded
inthisdocumentarefullypublicdomain.
Thanks:ThankstoJeanloupGailly(jloup@chorus.fr)andMarkAdler
(me@quest.jpl.nasa.gov)whobothproofreadthisdocument
andpickedoutlotsofnitsaswellassomebigfatbugs.
TableofContents

Abstract
1.Introduction:ErrorDetection
2.TheNeedForComplexity
3.TheBasicIdeaBehindCRCAlgorithms
4.PolynomicalArithmetic
5.BinaryArithmeticwithNoCarries
6.AFullyWorkedExample
7.ChoosingAPoly
8.AStraightforwardCRCImplementation
9.ATableDrivenImplementation
10.ASlightlyMangledTableDrivenImplementation
11."Reflected"TableDrivenImplementations
12."Reversed"Polys
13.InitialandFinalValues
14.DefiningAlgorithmsAbsolutely
15.AParameterizedModelForCRCAlgorithms
16.ACatalogofParameterSetsforStandards
17.AnImplementationoftheModelAlgorithm
18.RollYourOwnTableDrivenImplementation
19.GeneratingALookupTable
20.Summary
21.Corrections
A.Glossary
B.References
C.ReferencesIHaveDetectedButHaven'tYetSighted
Abstract

ThisdocumentexplainsCRCs(CyclicRedundancyCodes)andtheir
tabledrivenimplementationsinfull,precisedetail.Muchofthe
literatureonCRCs,andinparticularontheirtabledriven
implementations,isalittleobscure(oratleastseemssotome).
Thisdocumentisanattempttoprovideaclearandsimplenononsense
explanationofCRCsandtoabsolutelynaildowneverydetailofthe
operationoftheirhighspeedimplementations.Inadditiontothis,
thisdocumentpresentsaparameterizedmodelCRCalgorithmcalledthe
http://www.zlib.net/crc_v3.txt

1/32

5/15/2015

www.zlib.net/crc_v3.txt

"Rocksoft^tmModelCRCAlgorithm".Themodelalgorithmcanbe
parameterizedtobehavelikemostoftheCRCimplementationsaround,
andsoactsasagoodreferencefordescribingparticularalgorithms.
AlowspeedimplementationofthemodelCRCalgorithmisprovidedin
theCprogramminglanguage.Lastlythereisasectiongivingtwoforms
ofhighspeedtabledrivenimplementations,andprovidingaprogram
thatgeneratesCRClookuptables.
1.Introduction:ErrorDetection

Theaimofanerrordetectiontechniqueistoenablethereceiverofa
messagetransmittedthroughanoisy(errorintroducing)channelto
determinewhetherthemessagehasbeencorrupted.Todothis,the
transmitterconstructsavalue(calledachecksum)thatisafunction
ofthemessage,andappendsittothemessage.Thereceivercanthen
usethesamefunctiontocalculatethechecksumofthereceived
messageandcompareitwiththeappendedchecksumtoseeifthe
messagewascorrectlyreceived.Forexample,ifwechoseachecksum
functionwhichwassimplythesumofthebytesinthemessagemod256
(i.e.modulo256),thenitmightgosomethingasfollows.Allnumbers
areindecimal.
Message:6234
Messagewithchecksum:623433
Messageaftertransmission:627433
Intheabove,thesecondbyteofthemessagewascorruptedfrom23to
27bythecommunicationschannel.However,thereceivercandetect
thisbycomparingthetransmittedchecksum(33)withthecomputer
checksumof37(6+27+4).Ifthechecksumitselfiscorrupted,a
correctlytransmittedmessagemightbeincorrectlyidentifiedasa
corruptedone.However,thisisasafesidefailure.Adangerousside
failureoccurswherethemessageand/orchecksumiscorruptedina
mannerthatresultsinatransmissionthatisinternallyconsistent.
Unfortunately,thispossibilityiscompletelyunavoidableandthebest
thatcanbedoneistominimizeitsprobabilitybyincreasingthe
amountofinformationinthechecksum(e.g.wideningthechecksumfrom
onebytetotwobytes).
Othererrordetectiontechniquesexistthatinvolveperformingcomplex
transformationsonthemessagetoinjectitwithredundant
information.However,thisdocumentaddressesonlyCRCalgorithms,
whichfallintotheclassoferrordetectionalgorithmsthatleavethe
dataintactandappendachecksumontheend.i.e.:
<originalintactmessage><checksum>
2.TheNeedForComplexity

Inthechecksumexampleintheprevioussection,wesawhowa
corruptedmessagewasdetectedusingachecksumalgorithmthatsimply
sumsthebytesinthemessagemod256:
Message:6234
Messagewithchecksum:623433
Messageaftertransmission:627433
Aproblemwiththisalgorithmisthatitistoosimple.Ifanumberof
randomcorruptionsoccur,thereisa1in256chancethattheywill
notbedetected.Forexample:
Message:6234
Messagewithchecksum:623433
http://www.zlib.net/crc_v3.txt

2/32

5/15/2015

www.zlib.net/crc_v3.txt

Messageaftertransmission:820533
Tostrengthenthechecksum,wecouldchangefroman8bitregisterto
a16bitregister(i.e.sumthebytesmod65536insteadofmod256)so
astoapparentlyreducetheprobabilityoffailurefrom1/256to
1/65536.Whilebasicallyagoodidea,itfailsinthiscasebecause
theformulausedisnotsufficiently"random";withasimplesumming
formula,eachincomingbyteaffectsroughlyonlyonebyteofthe
summingregisternomatterhowwideitis.Forexample,inthesecond
exampleabove,thesummingregistercouldbeaMegabytewide,andthe
errorwouldstillgoundetected.Thisproblemcanonlybesolvedby
replacingthesimplesummingformulawithamoresophisticatedformula
thatcauseseachincomingbytetohaveaneffectontheentire
checksumregister.
Thus,weseethatatleasttwoaspectsarerequiredtoformastrong
checksumfunction:
WIDTH:Aregisterwidthwideenoughtoprovidealowapriori
probabilityoffailure(e.g.32bitsgivesa1/2^32chance
offailure).
CHAOS:Aformulathatgiveseachinputbytethepotentialtochange
anynumberofbitsintheregister.
Note:Theterm"checksum"waspresumablyusedtodescribeearly
summingformulas,buthasnowtakenonamoregeneralmeaning
encompassingmoresophisticatedalgorithmssuchastheCRCones.The
CRCalgorithmstobedescribedsatisfythesecondconditionverywell,
andcanbeconfiguredtooperatewithavarietyofchecksumwidths.
3.TheBasicIdeaBehindCRCAlgorithms

Wheremightwegoinoursearchforamorecomplexfunctionthan
summing?Allsortsofschemesspringtomind.Wecouldconstruct
tablesusingthedigitsofpi,orhasheachincomingbytewithallthe
bytesintheregister.Wecouldevenkeepalargetelephonebook
online,anduseeachincomingbytecombinedwiththeregisterbytes
toindexanewphonenumberwhichwouldbethenextregistervalue.
Thepossibilitiesarelimitless.
However,wedonotneedtogosofar;thenextarithmeticstep
suffices.Whileadditionisclearlynotstrongenoughtoforman
effectivechecksum,itturnsoutthatdivisionis,solongasthe
divisorisaboutaswideasthechecksumregister.
ThebasicideaofCRCalgorithmsissimplytotreatthemessageasan
enormousbinarynumber,todivideitbyanotherfixedbinarynumber,
andtomaketheremainderfromthisdivisionthechecksum.Upon
receiptofthemessage,thereceivercanperformthesamedivisionand
comparetheremainderwiththe"checksum"(transmittedremainder).
Example:Supposethethemessageconsistedofthetwobytes(6,23)as
inthepreviousexample.Thesecanbeconsideredtobethehexadecimal
number0617whichcanbeconsideredtobethebinarynumber
0000011000010111.Supposethatweuseachecksumregisteronebyte
wideanduseaconstantdivisorof1001,thenthechecksumisthe
remainderafter0000011000010111isdividedby1001.Whileinthis
case,thiscalculationcouldobviouslybeperformedusingcommon
gardenvariety32bitregisters,inthegeneralcasethisismessy.So
instead,we'lldothedivisionusinggood'ollongdivisionwhichyou
learntinschool(remember?).Exceptthistime,it'sinbinary:
...0000010101101=00AD=173=QUOTIENT
http://www.zlib.net/crc_v3.txt

3/32

5/15/2015

www.zlib.net/crc_v3.txt

_____________
9=1001)0000011000010111=0617=1559=DIVIDEND
DIVISOR0000.,,....,.,,,
.,,....,.,,,
0000,,....,.,,,
0000,,....,.,,,
,,....,.,,,
0001,....,.,,,
0000,....,.,,,
,....,.,,,
0011....,.,,,
0000....,.,,,
....,.,,,
0110...,.,,,
0000...,.,,,
...,.,,,
1100..,.,,,
1001..,.,,,
====..,.,,,
0110.,.,,,
0000.,.,,,
.,.,,,
1100,.,,,
1001,.,,,
====,.,,,
0111.,,,
0000.,,,
.,,,
1110,,,
1001,,,
====,,,
1011,,
1001,,
====,,
0101,
0000,

1011
1001
====
0010=02=2=REMAINDER
Indecimalthisis"1559dividedby9is173witharemainderof2".
Althoughtheeffectofeachbitoftheinputmessageonthequotient
isnotallthatsignificant,the4bitremaindergetskickedabout
quitealotduringthecalculation,andifmorebyteswereaddedto
themessage(dividend)it'svaluecouldchangeradicallyagainvery
quickly.Thisiswhydivisionworkswhereadditiondoesn't.
Incaseyou'rewondering,usingthis4bitchecksumthetransmitted
messagewouldlooklikethis(inhexadecimal):06172(wherethe0617
isthemessageandthe2isthechecksum).Thereceiverwoulddivide
0617by9andseewhethertheremainderwas2.
4.PolynomicalArithmetic

Whilethedivisionschemedescribedintheprevioussectionisvery
verysimilartothechecksummingschemescalledCRCschemes,theCRC
schemesareinfactabitweirder,andweneedtodelveintosome
strangenumbersystemstounderstandthem.
ThewordyouwillhearallthetimewhendealingwithCRCalgorithms
http://www.zlib.net/crc_v3.txt

4/32

5/15/2015

www.zlib.net/crc_v3.txt

istheword"polynomial".AgivenCRCalgorithmwillbesaidtobe
usingaparticularpolynomial,andCRCalgorithmsingeneralaresaid
tobeoperatingusingpolynomialarithmetic.Whatdoesthismean?
Insteadofthedivisor,dividend(message),quotient,andremainder
(asdescribedintheprevioussection)beingviewedaspositive
integers,theyareviewedaspolynomialswithbinarycoefficients.
Thisisdonebytreatingeachnumberasabitstringwhosebitsare
thecoefficientsofapolynomial.Forexample,theordinarynumber23
(decimal)is17(hex)and10111binaryandsoitcorrespondstothe
polynomial:
1*x^4+0*x^3+1*x^2+1*x^1+1*x^0
or,moresimply:
x^4+x^2+x^1+x^0
Usingthistechnique,themessage,andthedivisorcanberepresented
aspolynomialsandwecandoallourarithmeticjustasbefore,except
thatnowit'sallclutteredupwithXs.Forexample,supposewewanted
tomultiply1101by1011.Wecandothissimplybymultiplyingthe
polynomials:
(x^3+x^2+x^0)(x^3+x^1+x^0)
=(x^6+x^4+x^3
+x^5+x^3+x^2
+x^3+x^1+x^0)=x^6+x^5+x^4+3*x^3+x^2+x^1+x^0
Atthispoint,togettherightanswer,wehavetopretendthatxis2
andpropagatebinarycarriesfromthe3*x^3yielding
x^7+x^3+x^2+x^1+x^0
It'sjustlikeordinaryarithmeticexceptthatthebaseisabstracted
andbroughtintoallthecalculationsexplicitlyinsteadofbeing
thereimplicitly.Sowhat'sthepoint?
ThepointisthatIFwepretendthatweDON'Tknowwhatxis,weCAN'T
performthecarries.Wedon'tknowthat3*x^3isthesameasx^4+x^3
becausewedon'tknowthatxis2.Inthistruepolynomialarithmetic
therelationshipbetweenallthecoefficientsisunknownandsothe
coefficientsofeachpowereffectivelybecomestronglytyped;
coefficientsofx^2areeffectivelyofadifferenttypeto
coefficientsofx^3.
Withthecoefficientsofeachpowernicelyisolated,mathematicians
cameupwithallsortsofdifferentkindsofpolynomialarithmetics
simplybychangingtherulesabouthowcoefficientswork.Ofthese
schemes,oneinparticularisrelevanthere,andthatisapolynomial
arithmeticwherethecoefficientsarecalculatedMOD2andthereisno
carry;allcoefficientsmustbeeither0or1andnocarriesare
calculated.Thisiscalled"polynomialarithmeticmod2".Thus,
returningtotheearlierexample:
(x^3+x^2+x^0)(x^3+x^1+x^0)
=(x^6+x^4+x^3
+x^5+x^3+x^2
+x^3+x^1+x^0)
=x^6+x^5+x^4+3*x^3+x^2+x^1+x^0
Undertheotherarithmetic,the3*x^3termwaspropagatedusingthe
carrymechanismusingtheknowledgethatx=2.Under"polynomial
arithmeticmod2",wedon'tknowwhatxis,therearenocarries,and
allcoefficientshavetobecalculatedmod2.Thus,theresult
http://www.zlib.net/crc_v3.txt

5/32

5/15/2015

www.zlib.net/crc_v3.txt

becomes:
=x^6+x^5+x^4+x^3+x^2+x^1+x^0
AsKnuth[Knuth81]says(p.400):
"Thereadershouldnotethesimilaritybetweenpolynomial
arithmeticandmultipleprecisionarithmetic(Section4.3.1),where
theradixbissubstitutedforx.Thechiefdifferenceisthatthe
coefficientu_kofx^kinpolynomialarithmeticbearslittleorno
relationtoitsneighboringcoefficientsx^{k1}[andx^{k+1}],so
theideaof"carrying"fromoneplacetoanotherisabsent.Infact
polynomialarithmeticmodulobisessentiallyidenticaltomultiple
precisionarithmeticwithradixb,exceptthatallcarriesare
suppressed."
Thuspolynomicalarithmeticmod2isjustbinaryarithmeticmod2with
nocarries.Whilepolynomialsprovideusefulmathematicalmachineryin
moreanalyticalapproachestoCRCanderrorcorrectionalgorithms,for
thepurposesofexpositiontheyprovidenoextrainsightandsome
encumbranceandhavebeendiscardedintheremainderofthisdocument
infavourofdirectmanipulationofthearithmeticalsystemwithwhich
theyareisomorphic:binaryarithmeticwithnocarry.
5.BinaryArithmeticwithNoCarries

Havingdispensedwithpolynomials,wecanfocusontherealarithmetic
issue,whichisthatallthearithmeticperformedduringCRC
calculationsisperformedinbinarywithnocarries.Oftenthisis
calledpolynomialarithmetic,butasIhavedeclaredtherestofthis
documentapolynomialfreezone,we'llhavetocallitCRCarithmetic
instead.AsthisarithmeticisakeypartofCRCcalculations,we'd
bettergetusedtoit.Herewego:
AddingtwonumbersinCRCarithmeticisthesameasaddingnumbersin
ordinarybinaryarithmeticexceptthereisnocarry.Thismeansthat
eachpairofcorrespondingbitsdeterminethecorrespondingoutputbit
withoutreferencetoanyotherbitpositions.Forexample:
10011011
+11001010

01010001

Thereareonlyfourcasesforeachbitposition:
0+0=0
0+1=1
1+0=1
1+1=0(nocarry)
Subtractionisidentical:
10011011
11001010

01010001

with
00=0
01=1(wraparound)
http://www.zlib.net/crc_v3.txt

6/32

5/15/2015

www.zlib.net/crc_v3.txt

10=1
11=0
Infact,bothadditionandsubtractioninCRCarithmeticisequivalent
totheXORoperation,andtheXORoperationisitsowninverse.This
effectivelyreducestheoperationsofthefirstlevelofpower
(addition,subtraction)toasingleoperationthatisitsowninverse.
Thisisaveryconvenientpropertyofthearithmetic.
Bycollapsingofadditionandsubtraction,thearithmeticdiscardsany
notionofmagnitudebeyondthepowerofitshighestonebit.Whileit
seemsclearthat1010isgreaterthan10,itisnolongerthecase
that1010canbeconsideredtobegreaterthan1001.Toseethis,note
thatyoucangetfrom1010to1001bybothaddingandsubtractingthe
samequantity:
1010=1010+0011
1010=10100011
Thismakesnonsenseofanynotionoforder.
Havingdefinedaddition,wecanmovetomultiplicationanddivision.
Multiplicationisabsolutelystraightforward,beingthesumofthe
firstnumber,shiftedinaccordancewiththesecondnumber.
1101
x1011

1101
1101.
0000..
1101...

1111111Note:ThesumusesCRCaddition

Divisionisalittlemessierasweneedtoknowwhen"anumbergoes
intoanothernumber".Todothis,weinvoketheweakdefinitionof
magnitudedefinedearlier:thatXisgreaterthanorequaltoYiff
thepositionofthehighest1bitofXisthesameorgreaterthanthe
positionofthehighest1bitofY.Here'safullyworkeddivision
(nickedfrom[Tanenbaum81]).
1100001010
_______________
10011)11010110110000
10011,,.,,....
,,.,,....
10011,.,,....
10011,.,,....
,.,,....
00001.,,....
00000.,,....
.,,....
00010,,....
00000,,....
,,....
00101,....
00000,....
,....
01011....
00000....
....
10110...
10011...
http://www.zlib.net/crc_v3.txt

7/32

5/15/2015

www.zlib.net/crc_v3.txt

...
01010..
00000..
..
10100.
10011.
.
01110
00000

1110=Remainder
That'sreallyit.Beforeproceedingfurther,however,it'sworthour
whileplayingwiththisarithmeticabittogetusedtoit.
We'vealreadyplayedwithadditionandsubtraction,noticingthatthey
arethesamething.Here,though,weshouldnotethatinthis
arithmeticA+0=AandA0=A.Thisobviouspropertyisveryuseful
later.
IndealingwithCRCmultiplicationanddivision,it'sworthgettinga
feelfortheconceptsofMULTIPLEandDIVISIBLE.
IfanumberAisamultipleofBthenwhatthismeansinCRC
arithmeticisthatitispossibletoconstructAfromzerobyXORing
invariousshiftsofB.Forexample,ifAwas0111010110andBwas11,
wecouldconstructAfromBasfollows:
0111010110
=.......11.
+....11....
+...11.....
.11.......
However,ifAis0111010111,itisnotpossibletoconstructitoutof
variousshiftsofB(canyouseewhy?seelater)soitissaidtobe
notdivisiblebyBinCRCarithmetic.
ThusweseethatCRCarithmeticisprimarilyaboutXORingparticular
valuesatvariousshiftingoffsets.
6.AFullyWorkedExample

HavingdefinedCRCarithmetic,wecannowframeaCRCcalculationas
simplyadivision,becausethat'sallitis!Thissectionfillsinthe
detailsandgivesanexample.
ToperformaCRCcalculation,weneedtochooseadivisor.Inmaths
marketingspeakthedivisoriscalledthe"generatorpolynomial"or
simplythe"polynomial",andisakeyparameterofanyCRCalgorithm.
Itwouldprobablybemorefriendlytocallthedivisorsomethingelse,
butthepolytalkissodeeplyingrainedinthefieldthatitwould
nowbeconfusingtoavoidit.Asacompromise,wewillrefertothe
CRCpolynomialasthe"poly".Justthinkofthisnumberasasortof
parrot."Hellopoly!"
YoucanchooseanypolyandcomeupwithaCRCalgorithm.However,
somepolysarebetterthanothers,andsoitiswisetostickwiththe
triedantestedones.Alatersectionaddressesthisissue.
Thewidth(positionofthehighest1bit)ofthepolyisvery
importantasitdominatesthewholecalculation.Typically,widthsof
16or32arechosensoastosimplifyimplementationonmodern
computers.Thewidthofapolyistheactualbitpositionofthe
http://www.zlib.net/crc_v3.txt

8/32

5/15/2015

www.zlib.net/crc_v3.txt

highestbit.Forexample,thewidthof10011is4,not5.Forthe
purposesofexample,wewillchoseapolyof10011(ofwidthWof4).
Havingchosenapoly,wecanproceedwiththecalculation.Thisis
simplyadivision(inCRCarithmetic)ofthemessagebythepoly.The
onlytrickisthatWzerobitsareappendedtothemessagebeforethe
CRCiscalculated.Thuswehave:
Originalmessage:1101011011
Poly:10011
MessageafterappendingWzeros:11010110110000
NowwesimplydividetheaugmentedmessagebythepolyusingCRC
arithmetic.Thisisthesamedivisionasbefore:
1100001010=Quotient(nobodycaresaboutthequotient)
_______________
10011)11010110110000=Augmentedmessage(1101011011+0000)
=Poly10011,,.,,....
,,.,,....
10011,.,,....
10011,.,,....
,.,,....
00001.,,....
00000.,,....
.,,....
00010,,....
00000,,....
,,....
00101,....
00000,....
,....
01011....
00000....
....
10110...
10011...
...
01010..
00000..
..
10100.
10011.
.
01110
00000

1110=Remainder=THECHECKSUM!!!!
Thedivisionyieldsaquotient,whichwethrowaway,andaremainder,
whichisthecalculatedchecksum.Thisendsthecalculation.
Usually,thechecksumisthenappendedtothemessageandtheresult
transmitted.Inthiscasethetransmissionwouldbe:11010110111110.
Attheotherend,thereceivercandooneoftwothings:
a.Separatethemessageandchecksum.Calculatethechecksumfor
themessage(afterappendingWzeros)andcomparethetwo
checksums.
b.Checksumthewholelot(withoutappendingzeros)andseeifit
comesoutaszero!
Thesetwooptionsareequivalent.However,inthenextsection,we
http://www.zlib.net/crc_v3.txt

9/32

5/15/2015

www.zlib.net/crc_v3.txt

willbeassumingoptionbbecauseitismarginallymathematically
cleaner.
AsummaryoftheoperationoftheclassofCRCalgorithms:
1.ChooseawidthW,andapolyG(ofwidthW).
2.AppendWzerobitstothemessage.CallthisM'.
3.DivideM'byGusingCRCarithmetic.Theremainderisthechecksum.
That'sallthereistoit.
7.ChoosingAPoly

Choosingapolyissomewhatofablackartandthereaderisreferred
to[Tanenbaum81](p.130132)whichhasaverycleardiscussionofthis
issue.Thissectionmerelyaimstoputthefearofdeathintoanyone
whosomuchastoyswiththeideaofmakinguptheirownpoly.Ifyou
don'tcareaboutwhyonepolymightbebetterthananotherandjust
wanttofindoutabouthighspeedimplementations,chooseoneofthe
arithmeticallysoundpolyslistedattheendofthissectionandskip
tothenextsection.
FirstnotethatthetransmittedmessageTisamultipleofthepoly.
Toseethis,notethat1)thelastWbitsofTistheremainderafter
dividingtheaugmented(byzerosremember)messagebythepoly,and2)
additionisthesameassubtractionsoaddingtheremainderpushesthe
valueuptothenextmultiple.Nownotethatifthetransmitted
messageiscorruptedintransmissionthatwewillreceiveT+EwhereE
isanerrorvector(and+isCRCaddition(i.e.XOR)).Uponreceiptof
thismessage,thereceiverdividesT+EbyG.AsTmodGis0,(T+E)
modG=EmodG.Thus,thecapacityofthepolywechoosetocatch
particularkindsoferrorswillbedeterminedbythesetofmultiples
ofG,foranycorruptionEthatisamultipleofGwillbeundetected.
OurtaskthenistofindclassesofGwhosemultipleslookaslittle
likethekindoflinenoise(thatwillbecreatingthecorruptions)as
possible.Solet'sexaminethekindsoflinenoisewecanexpect.
SINGLEBITERRORS:AsinglebiterrormeansE=1000...0000.Wecan
ensurethatthisclassoferrorisalwaysdetectedbymakingsurethat
Ghasatleasttwobitssetto1.AnymultipleofGwillbe
constructedusingshiftingandaddinganditisimpossibleto
constructavaluewithasinglebitbyshiftinganaddingasingle
valuewithmorethanonebitset,asthetwoendbitswillalways
persist.
TWOBITERRORS:Todetectallerrorsoftheform100...000100...000
(i.e.Econtainstwo1bits)chooseaGthatdoesnothavemultiples
thatare11,101,1001,10001,100001,etc.Itisnotcleartomehow
onegoesaboutdoingthis(Idon'thavethepuremathsbackground),
butTanenbaumassuresusthatsuchGdoexist,andcitesGwith1bits
(15,14,1)turnedonasanexampleofoneGthatwon'tdivideanything
lessthan1...1where...is32767zeros.
ERRORSWITHANODDNUMBEROFBITS:Wecancatchallcorruptionswhere
EhasanoddnumberofbitsbychoosingaGthathasanevennumberof
bits.Toseethis,notethat1)CRCmultiplicationissimplyXORinga
constantvalueintoaregisteratvariousoffsets,2)XORingissimply
abitflipoperation,and3)ifyouXORavaluewithanevennumberof
bitsintoaregister,theoddnessofthenumberof1bitsinthe
registerremainsinvariant.Example:StartingwithE=111,attemptto
flipallthreebitstozerobytherepeatedapplicationofXORingin
11atoneofthetwooffsets(i.e."E=EXOR011"and"E=EXOR110")
Thisisnearlyisomorphictothe"glasstumblers"partypuzzlewhere
youchallengesomeonetoflipthreetumblersbytherepeated
applicationoftheoperationofflippinganytwo.Mostofthepopular
http://www.zlib.net/crc_v3.txt

10/32

5/15/2015

www.zlib.net/crc_v3.txt

CRCpolyscontainanevennumberof1bits.(Note:Tanenbaumstates
morespecificallythatallerrorswithanoddnumberofbitscanbe
caughtbymakingGamultipleof11).
BURSTERRORS:AbursterrorlookslikeE=000...000111...11110000...00.
Thatis,Econsistsofallzerosexceptforarunof1ssomewhere
inside.ThiscanberecastasE=(10000...00)(1111111...111)where
therearezzerosintheLEFTpartandnonesintheRIGHTpart.To
catcherrorsofthiskind,wesimplysetthelowestbitofGto1.
DoingthisensuresthatLEFTcannotbeafactorofG.Then,solongas
GiswiderthanRIGHT,theerrorwillbedetected.SeeTanenbaumfora
clearerexplanationofthis;I'malittlefuzzyonthisone.Note:
Tanenbaumassertsthattheprobabilityofaburstoflengthgreater
thanWgettingthroughis(0.5)^W.
Thatconcludesthesectiononthefineartofselectingpolys.
Somepopularpolysare:
16bits:(16,12,5,0)[X25standard]
(16,15,2,0)["CRC16"]
32bits:(32,26,23,22,16,12,11,10,8,7,5,4,2,1,0)[Ethernet]
8.AStraightforwardCRCImplementation

That'stheendofthetheory;nowweturntoimplementations.Tostart
with,weexamineanabsolutelystraightdownthemiddleboring
straightforwardlowspeedimplementationthatdoesn'tuseanyspeed
tricksatall.We'llthentransformthatprogramprogessivelyuntilwe
endupwiththecompacttabledrivencodeweallknowandloveand
whichsomeofuswouldliketounderstand.
ToimplementaCRCalgorithmallwehavetodoisimplementCRC
division.Therearetworeasonswhywecannotsimplyusethedivide
instructionofwhatevermachineweareon.Thefirstisthatwehave
todothedivideinCRCarithmetic.Thesecondisthatthedividend
mightbetenmegabyteslong,andtodaysprocessorsdonothave
registersthatbig.
SotoimplementCRCdivision,wehavetofeedthemessagethrougha
divisionregister.Atthispoint,wehavetobeabsolutelyprecise
aboutthemessagedata.Inallthefollowingexamplesthemessagewill
beconsideredtobeastreamofbytes(eachof8bits)withbit7of
eachbytebeingconsideredtobethemostsignificantbit(MSB).The
bitstreamformedfromthesebyteswillbethebitstreamwiththeMSB
(bit7)ofthefirstbytefirst,goingdowntobit0ofthefirst
byte,andthentheMSBofthesecondbyteandsoon.
Withthisinmind,wecansketchanimplementationoftheCRC
division.Forthepurposesofexample,considerapolywithW=4and
thepoly=10111.Then,theperformthedivision,weneedtousea4bit
register:
3210Bits
+++++
Pop!<|||||<Augmentedmessage
+++++
10111=ThePoly
(Reminder:TheaugmentedmessageisthemessagefollowedbyWzerobits.)
Toperformthedivisionperformthefollowing:
Loadtheregisterwithzerobits.
http://www.zlib.net/crc_v3.txt

11/32

5/15/2015

www.zlib.net/crc_v3.txt

AugmentthemessagebyappendingWzerobitstotheendofit.
While(moremessagebits)
Begin
Shifttheregisterleftbyonebit,readingthenextbitofthe
augmentedmessageintoregisterbitposition0.
If(a1bitpoppedoutoftheregisterduringstep3)
Register=RegisterXORPoly.
End
Theregisternowcontainstheremainder.
(Note:Inpractice,theIFconditioncanbetestedbytestingthetop
bitofRbeforeperformingtheshift.)
Wewillcallthisalgorithm"SIMPLE".
Thismightlookabitmessy,butallwearereallydoingis
"subtracting"variouspowers(i.e.shiftings)ofthepolyfromthe
messageuntilthereisnothingleftbuttheremainder.Studythe
manualexamplesoflongdivisionifyoudon'tunderstandthis.
ItshouldbeclearthattheabovealgorithmwillworkforanywidthW.
9.ATableDrivenImplementation

TheSIMPLEalgorithmaboveisagoodstartingpointbecauseit
correspondsdirectlytothetheorypresentedsofar,andbecauseitis
soSIMPLE.However,becauseitoperatesatthebitlevel,itisrather
awkwardtocode(eveninC),andinefficienttoexecute(ithasto
looponceforeachbit).Tospeeditup,weneedtofindawayto
enablethealgorithmtoprocessthemessageinunitslargerthanone
bit.Candidatequantitiesarenibbles(4bits),bytes(8bits),words
(16bits)andlongwords(32bits)andhigherifwecanachieveit.Of
these,4bitsisbestavoidedbecauseitdoesnotcorrespondtoabyte
boundary.Attheveryleast,anyspeedupshouldallowustooperateat
byteboundaries,andinfactmostofthetabledrivenalgorithms
operateabyteatatime.
Forthepurposesofdiscussion,letusswitchfroma4bitpolytoa
32bitone.Ourregisterlooksmuchthesame,excepttheboxes
representbytesinsteadofbits,andthePolyis33bits(oneimplicit
1bitatthetopand32"active"bits)(W=32).
3210Bytes
+++++
Pop!<|||||<Augmentedmessage
+++++
1<32bits>
TheSIMPLEalgorithmisstillapplicable.Letusexaminewhatitdoes.
ImaginethattheSIMPLEalgorithmisinfullswingandconsiderthe
top8bitsofthe32bitregister(byte3)tohavethevalues:
t7t6t5t4t3t2t1t0
InthenextiterationofSIMPLE,t7willdeterminewhetherthePoly
willbeXORedintotheentireregister.Ift7=1,thiswillhappen,
otherwiseitwillnot.Supposethatthetop8bitsofthepolyareg7
g6..g0,thenafterthenextiteration,thetopbytewillbe:
t6t5t4t3t2t1t0??
+t7*(g7g6g5g4g3g2g1g0)[Reminder:+isXOR]
TheNEWtopbit(thatwillcontrolwhathappensinthenextiteration)
http://www.zlib.net/crc_v3.txt

12/32

5/15/2015

www.zlib.net/crc_v3.txt

nowhasthevaluet6+t7*g7.Theimportantthingtonoticehereis
thatfromaninformationalpointofview,alltheinformationrequired
tocalculatetheNEWtopbitwaspresentinthetopTWObitsofthe
originaltopbyte.Similarly,theNEXTtopbitcanbecalculatedin
advanceSOLELYfromthetopTHREEbitst7,t6,andt5.Infact,in
general,thevalueofthetopbitintheregisterinkiterationscan
becalculatedfromthetopkbitsoftheregister.Letustakethis
forgrantedforamoment.
Considerforamomentthatweusethetop8bitsoftheregisterto
calculatethevalueofthetopbitoftheregisterduringthenext8
iterations.Supposethatwedrivethenext8iterationsusingthe
calculatedvalues(whichwecouldperhapsstoreinasinglebyte
registerandshiftouttopickoffeachbit).Thenwenotethree
things:
*Thetopbyteoftheregisternowdoesn'tmatter.Nomatterhow
manytimesandatwhatoffsetthepolyisXORedtothetop8
bits,theywillallbeshiftedouttherighthandsideduringthe
next8iterationsanyway.
*Theremainingbitswillbeshiftedleftonepositionandthe
rightmostbyteoftheregisterwillbeshiftedinthenextbyte
AND
*Whileallthisisgoingon,theregisterwillbesubjectedtoa
seriesofXOR'sinaccordancewiththebitsoftheprecalculated
controlbyte.
NowconsidertheeffectofXORinginaconstantvalueatvarious
offsetstoaregister.Forexample:
0100010Register
...0110XORthis
..0110.XORthis
0110...XORthis

0011000

ThepointofthisisthatyoucanXORconstantvaluesintoaregister
toyourheart'sdelight,andintheend,therewillexistavalue
whichwhenXORedinwiththeoriginalregisterwillhavethesame
effectasalltheotherXORs.
Perhapsyoucanseethesolutionnow.Puttingallthepiecestogether
wehaveanalgorithmthatgoeslikethis:
While(augmentedmessageisnotexhausted)
Begin
Examinethetopbyteoftheregister
Calculatethecontrolbytefromthetopbyteoftheregister
SumallthePolysatvariousoffsetsthataretobeXORedinto
theregisterinaccordancewiththecontrolbyte
Shifttheregisterleftbyonebyte,readinganewmessagebyte
intotherightmostbyteoftheregister
XORthesummedpolystotheregister
End
AsitstandsthisisnotmuchbetterthantheSIMPLEalgorithm.
However,itturnsoutthatmostofthecalculationcanbeprecomputed
andassembledintoatable.Asaresult,theabovealgorithmcanbe
reducedto:
http://www.zlib.net/crc_v3.txt

13/32

5/15/2015

www.zlib.net/crc_v3.txt

While(augmentedmessageisnotexhaused)
Begin
Top=top_byte(Register);
Register=(Register<<24)|next_augmessage_byte;
Register=RegisterXORprecomputed_table[Top];
End
There!Ifyouunderstandthis,you'vegraspedthemainideaof
tabledrivenCRCalgorithms.Theaboveisaveryefficientalgorithm
requiringjustashift,andOR,anXOR,andatablelookupperbyte.
Graphically,itlookslikethis:
3210Bytes
+++++
+<|||||<Augmentedmessage
|+++++
|^
||
|XOR
||
|0+++++Algorithm
v+++++
|+++++1.Shifttheregisterleftby
|+++++onebyte,readinginanew
|+++++messagebyte.
|+++++2.Usethetopbytejustrotated
|+++++outoftheregistertoindex
+>+++++thetableof25632bitvalues.
+++++3.XORthetablevalueintothe
+++++register.
+++++4.Goto1iffmoreaugmented
+++++messagebytes.
255+++++
InC,thealgorithmmainlooplookslikethis:
r=0;
while(len)
{
bytet=(r>>24)&0xFF;
r=(r<<8)|*p++;
r^=table[t];
}
wherelenisthelengthoftheaugmentedmessageinbytes,ppointsto
theaugmentedmessage,ristheregister,tisatemporary,andtable
isthecomputedtable.Thiscodecanbemadeevenmoreunreadableas
follows:
r=0;while(len)r=((r<<8)|*p++)^t[(r>>24)&0xFF];
Thisisaveryclean,efficientloop,althoughnotaveryobviousone
tothecasualobservernotversedinCRCtheory.Wewillcallthisthe
TABLEalgorithm.
10.ASlightlyMangledTableDrivenImplementation

Despitethetersebeautyoftheline
r=0;while(len)r=((r<<8)|*p++)^t[(r>>24)&0xFF];
thoseoptimizinghackerscouldn'tleaveitalone.Thetrouble,you
http://www.zlib.net/crc_v3.txt

14/32

5/15/2015

www.zlib.net/crc_v3.txt

see,isthatthisloopoperatesupontheAUGMENTEDmessageandin
ordertousethiscode,youhavetoappendW/8zerobytestotheend
ofthemessagebeforepointingpatit.Dependingontheruntime
environment,thismayormaynotbeaproblem;iftheblockofdata
washandedtousbysomeothercode,itcouldbeaBIGproblem.One
alternativeissimplytoappendthefollowinglineaftertheabove
loop,onceforeachzerobyte:
for(i=0;i<W/4;i++)r=(r<<8)^t[(r>>24)&0xFF];
Thislookslikeasaneenoughsolutiontome.However,atthefurther
expenseofclarity(which,youmustadmit,isalreadyaprettyscare
commodityinthiscode)wecanreorganizethissmallloopfurtherso
astoavoidtheneedtoeitheraugmentthemessagewithzerobytes,or
toexplicitlyprocesszerobytesattheendasabove.Toexplainthe
optimization,wereturntotheprocessingdiagramgivenearlier.
3210Bytes
+++++
+<|||||<Augmentedmessage
|+++++
|^
||
|XOR
||
|0+++++Algorithm
v+++++
|+++++1.Shifttheregisterleftby
|+++++onebyte,readinginanew
|+++++messagebyte.
|+++++2.Usethetopbytejustrotated
|+++++outoftheregistertoindex
+>+++++thetableof25632bitvalues.
+++++3.XORthetablevalueintothe
+++++register.
+++++4.Goto1iffmoreaugmented
+++++messagebytes.
255+++++
Now,notethefollowingfacts:
TAIL:TheW/4augmentedzerobytesthatappearattheendofthe
messagewillbepushedintotheregisterfromtherightasall
theotherbytesare,buttheirvalues(0)willhavenoeffect
whatsoeverontheregisterbecause1)XORingwithzerodoesnot
changethetargetbyte,and2)thefourbytesarenever
propagatedouttheleftsideoftheregisterwheretheir
zeronessmighthavesomesortofinfluence.Thus,thesole
functionoftheW/4augmentedzerobytesistodrivethe
calculationforanotherW/4bytecyclessothattheendofthe
REALdatapassesallthewaythroughtheregister.
HEAD:Iftheinitialvalueoftheregisteriszero,thefirstfour
iterationsoftheloopwillhavethesoleeffectofshiftingin
thefirstfourbytesofthemessagefromtheright.Thisis
becausethefirst32controlbitsareallzeroandsonothingis
XORedintotheregister.Eveniftheinitialvalueisnotzero,
thefirst4byteiterationsofthealgorithmwillhavethesole
effectofshiftingthefirst4bytesofthemessageintothe
registerandthenXORingthemwithsomeconstantvalue(thatis
afunctionoftheinitialvalueoftheregister).
Thesefacts,combinedwiththeXORproperty
(AxorB)xorC=Axor(BxorC)
http://www.zlib.net/crc_v3.txt

15/32

5/15/2015

www.zlib.net/crc_v3.txt

meanthatmessagebytesneednotactuallytravelthroughtheW/4bytes
oftheregister.Instead,theycanbeXORedintothetopbytejust
beforeitisusedtoindexthelookuptable.Thisleadstothe
followingmodifiedversionofthealgorithm.
+<Message(nonaugmented)
|
v3210Bytes
|+++++
XOR<|||||
|+++++
|^
||
|XOR
||
|0+++++Algorithm
v+++++
|+++++1.Shifttheregisterleftby
|+++++onebyte,readinginanew
|+++++messagebyte.
|+++++2.XORthetopbytejustrotated
|+++++outoftheregisterwiththe
+>+++++nextmessagebytetoyieldan
+++++indexintothetable([0,255]).
+++++3.XORthetablevalueintothe
+++++register.
+++++4.Goto1iffmoreaugmented
255+++++messagebytes.
Note:Theinitialregistervalueforthisalgorithmmustbethe
initialvalueoftheregisterforthepreviousalgorithmfedthrough
thetablefourtimes.Note:Thetableissuchthatiftheprevious
algorithmused0,thenewalgorithmwilltoo.
ThisisanIDENTICALalgorithmandwillyieldIDENTICALresults.TheC
codelookssomethinglikethis:
r=0;while(len)r=(r<<8)^t[(r>>24)^*p++];
andTHISisthecodethatyouarelikelytofindinsidecurrent
tabledrivenCRCimplementations.SomeFFmasksmighthavetobeANDed
inhereandthereforportability'ssake,butbasically,theabove
loopisIT.WewillcallthistheDIRECTTABLEALGORITHM.
Duringtheprocessoftryingtounderstandallthisstuff,Imanaged
toderivetheSIMPLEalgorithmandthetabledrivenversionderived
fromthat.However,whenIcomparedmycodewiththecodefoundin
realimplementations,Iwastotallybamboozledastowhythebytes
werebeingXORedinatthewrongendoftheregister!Ittookquitea
whilebeforeIfiguredoutthattheirsandmyalgorithmswereactually
thesame.PartofwhyIamwritingthisdocumentisthat,whilethe
linkbetweendivisionandmyearliertabledrivencodeisvaguely
apparent,anysuchlinkisfairlywellerasedwhenyoustartpumping
bytesinatthe"wrongend"oftheregister.Itlooksallwrong!
Ifyou'vegotthisfar,younotonlyunderstandthetheory,the
practice,theoptimizedpractice,butyoualsounderstandthereal
codeyouarelikelytoruninto.Couldgetanymorecomplicated?Yes
itcan.
11."Reflected"TableDrivenImplementations
http://www.zlib.net/crc_v3.txt

16/32

5/15/2015

www.zlib.net/crc_v3.txt

Despitethefactthattheabovecodeisprobablyoptimizedaboutas
muchasitcouldbe,thisdidnotstopsomeenterprisingindividuals
frommakingthingsevenmorecomplicated.Tounderstandhowthis
happened,wehavetoentertheworldofhardware.
DEFINITION:Avalue/registerisreflectedifit'sbitsareswapped
arounditscentre.Forexample:0101isthe4bitreflectionof1010.
0011isthereflectionof1100.
01110101101011110010010110111100isthereflectionof
00111101101001001111010110101110.
TurnsoutthatUARTs(thosehandylittlechipsthatperformserialIO)
areinthehabitoftransmittingeachbytewiththeleastsignificant
bit(bit0)firstandthemostsignificantbit(bit7)last(i.e.
reflected).Aneffectofthisconventionisthathardwareengineers
constructinghardwareCRCcalculatorsthatoperateatthebitlevel
tooktocalculatingCRCsofbytesstreamswitheachofthebytes
reflectedwithinitself.Thebytesareprocessedinthesameorder,
butthebitsineachbyteareswapped;bit0isnowbit7,bit1is
nowbit6,andsoon.Nowthiswouldn'tmattermuchifthisconvention
wasrestrictedtohardwareland.Howeveritseemsthatatsomestage
someoftheseCRCvalueswerepresentedatthesoftwareleveland
someonehadtowritesomecodethatwouldinteroperatewiththe
hardwareCRCcalculation.
Inthissituation,anormalsanesoftwareengineerwouldsimply
reflecteachbytebeforeprocessingit.However,itwouldseemthat
normalsanesoftwareengineerswerethinonthegroundwhenthisearly
groundwasbeingbroken,becauseinsteadofreflectingthebytes,
whoeverwasresponsiblehelddownthebyteandreflectedtheworld,
leadingtothefollowing"reflected"algorithmwhichisidenticalto
thepreviousoneexceptthateverythingisreflectedexcepttheinput
bytes.
Message(nonaugmented)>+
|
Bytes0123v
+++++|
|||||>XOR
+++++|
^|
||
XOR|
||
+++++0|
+++++v
+++++|
+++++|
+++++|
+++++|
+++++|
+++++<+
+++++
+++++
+++++
+++++
+++++255
Notes:
*Thetableisidenticaltotheoneinthepreviousalgorithm
exceptthateachentryhasbeenreflected.
http://www.zlib.net/crc_v3.txt

17/32

5/15/2015

www.zlib.net/crc_v3.txt

*Theinitialvalueoftheregisteristhesameasintheprevious
algorithmexceptthatithasbeenreflected.
*Thebytesofthemessageareprocessedinthesameorderas
before(i.e.themessageitselfisnotreflected).
*Themessagebytesthemselvesdon'tneedtobeexplicitly
reflected,becauseeverythingelsehasbeen!
Attheendofexecution,theregistercontainsthereflectionofthe
finalCRCvalue(remainder).Actually,I'mbeingratherhardon
whoevercookedthisupbecauseitseemsthathardwareimplementations
oftheCRCalgorithmusedthereflectedchecksumvalueandso
producingareflectedCRCwasjustright.Infactreflectingtheworld
wasprobablyagoodengineeringsolutionifaconfusingone.
WewillcallthistheREFLECTEDalgorithm.
Whetherornotitmadesenseatthetime,theeffectofhaving
reflectedalgorithmskickingaroundtheworld'sFTPsitesisthat
abouthalftheCRCimplementationsonerunsintoarereflectedandthe
otherhalfnot.It'sreallyterriblyconfusing.Inparticular,it
wouldseemtomethatthecasualreaderwhorunsintoareflected,
tabledrivenimplementationwiththebytes"fedinthewrongend"
wouldhaveBuckley'schanceofeverconnectingthecodetotheconcept
ofbinarymod2division.
Itcouldn'tgetanymoreconfusingcouldit?Yesitcould.
12."Reversed"Polys

Asifreflectedimplementationsweren'tenough,thereisanother
conceptkickingaroundwhichmakesthesituationbizaarlyconfusing.
TheconceptisreversedPolys.
Itturnsoutthatthereflectionofgoodpolystendtobegoodpolys
too!Thatis,ifG=11101isagoodpolyvalue,then10111willbeas
well.Asaconsequence,itseemsthateverytimeanorganization(such
asCCITT)standardizesonaparticularlygoodpoly("polynomial"),
thoseintherealworldcan'tleavethepoly'sreflectionalone
either.TheyjustHAVEtouseit.Asaresult,thesetof"standard"
poly'shasacorrespondingsetofreflections,whicharealsoinuse.
Toavoidconfusion,wewillcallthesethe"reversed"polys.
X25standard:10001000000100001
X25reversed:10000100000010001
CRC16standard:11000000000000101
CRC16reversed:10100000000000011
Notethathereitistheentirepolythatisbeingreflected/reversed,
notjustthebottomWbits.Thisisanimportantdistinction.Inthe
reflectedalgorithmdescribedintheprevioussection,thepolyused
inthereflectedalgorithmwasactuallyidenticaltothatusedinthe
nonreflectedalgorithm;allthathadhappenedisthatthebyteshad
effectivelybeenreflected.Assuch,allthe16bit/32bitnumbersin
thealgorithmhadtobereflected.Incontrast,theENTIREpoly
includestheimplicitonebitatthetop,andsoreversingapolyis
notthesameasreflectingitsbottom16or32bits.
Theupshotofallthisisthatareflectedalgorithmisnotequivalent
totheoriginalalgorithmwiththepolyreflected.Actually,thisis
probablylessconfusingthaniftheywereduals.
http://www.zlib.net/crc_v3.txt

18/32

5/15/2015

www.zlib.net/crc_v3.txt

Ifallthisseemsabitunclear,don'tworry,becausewe'regoingto
sortitallout"realsoonnow".Justonemoresectiontogobefore
that.
13.InitialandFinalValues

Inadditiontothecomplexityalreadyseen,CRCalgorithmsdifferfrom
eachotherintwootherregards:
*Theinitialvalueoftheregister.
*ThevaluetobeXORedwiththefinalregistervalue.
Forexample,the"CRC32"algorithminitializesitsregisterto
FFFFFFFFandXORsthefinalregistervaluewithFFFFFFFF.
MostCRCalgorithmsinitializetheirregistertozero.However,some
initializeittoanonzerovalue.Intheory(i.e.withnoassumptions
aboutthemessage),theinitialvaluehasnoaffectonthestrengthof
theCRCalgorithm,theinitialvaluemerelyprovidingafixedstarting
pointfromwhichtheregistervaluecanprogress.However,in
practice,somemessagesaremorelikelythanothers,anditiswiseto
initializetheCRCalgorithmregistertoavaluethatdoesnothave
"blindspots"thatarelikelytooccurinpractice.By"blindspot"is
meantasequenceofmessagebytesthatdonotresultintheregister
changingitsvalue.Inparticular,anyCRCalgorithmthatinitializes
itsregistertozerowillhaveablindspotofzerowhenitstartsup
andwillbeunableto"count"aleadingrunofzerobytes.Asa
leadingrunofzerobytesisquitecommoninrealmessages,itiswise
toinitializethealgorithmregistertoanonzerovalue.
14.DefiningAlgorithmsAbsolutely

Atthispointwehavecoveredallthedifferentaspectsof
tabledrivenCRCalgorithms.Astherearesomanyvariationsonthese
algorithms,itisworthtryingtoestablishanomenclatureforthem.
Thissectionattemptstodothat.
WehaveseenthatCRCalgorithmsvaryin:
*Widthofthepoly(polynomial).
*Valueofthepoly.
*Initialvaluefortheregister.
*Whetherthebitsofeachbytearereflectedbeforebeingprocessed.
*Whetherthealgorithmfeedsinputbytesthroughtheregisteror
xorsthemwithabytefromoneendandthenstraightintothetable.
*Whetherthefinalregistervalueshouldbereversed(asinreflected
versions).
*ValuetoXORwiththefinalregistervalue.
InordertobeabletotalkaboutparticularCRCalgorithms,weneed
toabletodefinethemmorepreciselythanthis.Forthisreason,the
nextsectionattemptstoprovideawelldefinedparameterizedmodel
forCRCalgorithms.Torefertoaparticularalgorithm,weneedthen
simplyspecifythealgorithmintermsofparameterstothemodel.
15.AParameterizedModelForCRCAlgorithms

InthissectionwedefineapreciseparameterizedmodelCRCalgorithm
which,forwantofabettername,wewillcallthe"Rocksoft^tmModel
CRCAlgorithm"(andwhynot?Rocksoft^tmcoulddowithsomefree
advertising:).
http://www.zlib.net/crc_v3.txt

19/32

5/15/2015

www.zlib.net/crc_v3.txt

Themostimportantaspectofthemodelalgorithmisthatitfocusses
exclusivelyonfunctionality,ignoringallimplementationdetails.The
aimoftheexerciseistoconstructawayofreferringpreciselyto
particularCRCalgorithms,regardlessofhowconfusinglytheyare
implemented.Tothisend,themodelmustbeassimpleandpreciseas
possible,withaslittleconfusionaspossible.
TheRocksoft^tmModelCRCAlgorithmisbasedessentiallyontheDIRECT
TABLEALGORITHMspecifiedearlier.However,thealgorithmhastobe
furtherparameterizedtoenableittobehaveinthesamewayassome
ofthemessieralgorithmsoutintherealworld.
Toenablethealgorithmtobehavelikereflectedalgorithms,we
provideabooleanoptiontoreflecttheinputbytes,andaboolean
optiontospecifywhethertoreflecttheoutputchecksumvalue.By
framingreflectionasaninput/outputtransformation,weavoidthe
confusionofhavingtomentallymaptheparametersofreflectedand
nonreflectedalgorithms.
Anextraparameterallowsthealgorithm'sregistertobeinitialized
toaparticularvalue.AfurtherparameterisXORedwiththefinal
valuebeforeitisreturned.
Byputtingallthesepiecestogetherweendupwiththeparametersof
thealgorithm:
NAME:Thisisanamegiventothealgorithm.Astringvalue.
WIDTH:Thisisthewidthofthealgorithmexpressedinbits.This
isonelessthanthewidthofthePoly.
POLY:Thisparameteristhepoly.Thisisabinaryvaluethat
shouldbespecifiedasahexadecimalnumber.Thetopbitofthe
polyshouldbeomitted.Forexample,ifthepolyis10110,you
shouldspecify06.Animportantaspectofthisparameteristhatit
representstheunreflectedpoly;thebottombitofthisparameter
isalwaystheLSBofthedivisorduringthedivisionregardlessof
whetherthealgorithmbeingmodelledisreflected.
INIT:Thisparameterspecifiestheinitialvalueoftheregister
whenthealgorithmstarts.Thisisthevaluethatistobeassigned
totheregisterinthedirecttablealgorithm.Inthetable
algorithm,wemaythinkoftheregisteralwayscommencingwiththe
valuezero,andthisvaluebeingXORedintotheregisterafterthe
N'thbititeration.Thisparametershouldbespecifiedasa
hexadecimalnumber.
REFIN:Thisisabooleanparameter.IfitisFALSE,inputbytesare
processedwithbit7beingtreatedasthemostsignificantbit
(MSB)andbit0beingtreatedastheleastsignificantbit.Ifthis
parameterisFALSE,eachbyteisreflectedbeforebeingprocessed.
REFOUT:Thisisabooleanparameter.IfitissettoFALSE,the
finalvalueintheregisterisfedintotheXOROUTstagedirectly,
otherwise,ifthisparameterisTRUE,thefinalregistervalueis
reflectedfirst.
XOROUT:ThisisanWbitvaluethatshouldbespecifiedasa
hexadecimalnumber.ItisXORedtothefinalregistervalue(after
theREFOUT)stagebeforethevalueisreturnedastheofficial
checksum.
CHECK:Thisfieldisnotstrictlypartofthedefinition,and,in
theeventofaninconsistencybetweenthisfieldandtheother
http://www.zlib.net/crc_v3.txt

20/32

5/15/2015

www.zlib.net/crc_v3.txt

field,theotherfieldstakeprecedence.Thisfieldisacheck
valuethatcanbeusedasaweakvalidatorofimplementationsof
thealgorithm.Thefieldcontainsthechecksumobtainedwhenthe
ASCIIstring"123456789"isfedthroughthespecifiedalgorithm
(i.e.313233...(hexadecimal)).
Withtheseparametersdefined,themodelcannowbeusedtospecifya
particularCRCalgorithmexactly.Hereisanexamplespecificationfor
apopularformoftheCRC16algorithm.
Name:"CRC16"
Width:16
Poly:8005
Init:0000
RefIn:True
RefOut:True
XorOut:0000
Check:BB3D
16.ACatalogofParameterSetsforStandards

Atthispoint,Iwouldliketogivealistofthespecificationsfor
commonlyusedCRCalgorithms.However,mostofthealgorithmsthatI
havecomeintocontactwithsofararespecifiedinsuchavagueway
thatthishasnotbeenpossible.WhatIcanprovideisalistofpolys
forvariousCRCstandardsIhaveheardof:
X25standard:1021[CRCCCITT,ADCCP,SDLC/HDLC]
X25reversed:0811
CRC16standard:8005
CRC16reversed:4003[LHA]
CRC32:04C11DB7[PKZIP,AUTODINII,Ethernet,FDDI]
Iwouldbeinterestedinhearingfromanyoneouttherewhocantie
downthecompletesetofmodelparametersforanyofthesestandards.
However,aprogramthatwaskickingaroundseemedtoimplythe
followingspecifications.Cananyoneconfirmordenythem(orprovide
thecheckvalues(whichIcouldn'tbebotheredcodingupand
calculating)).
Name:"CRC16/CITT"
Width:16
Poly:1021
Init:FFFF
RefIn:False
RefOut:False
XorOut:0000
Check:?
Name:"XMODEM"
Width:16
Poly:8408
Init:0000
RefIn:True
RefOut:True
XorOut:0000
Check:?
Name:"ARC"
http://www.zlib.net/crc_v3.txt

21/32

5/15/2015

www.zlib.net/crc_v3.txt

Width:16
Poly:8005
Init:0000
RefIn:True
RefOut:True
XorOut:0000
Check:?
HereisthespecificationfortheCRC32algorithmwhichisreportedly
usedinPKZip,AUTODINII,Ethernet,andFDDI.
Name:"CRC32"
Width:32
Poly:04C11DB7
Init:FFFFFFFF
RefIn:True
RefOut:True
XorOut:FFFFFFFF
Check:CBF43926
17.AnImplementationoftheModelAlgorithm

HereisanimplementationofthemodelalgorithmintheCprogramming
language.Theimplementationconsistsofaheaderfile(.h)andan
implementationfile(.c).Ifyou'rereadingthisdocumentina
sequentialscroller,youcanskipthiscodebysearchingforthe
string"RollYourOwn".
Toensurethatthefollowingcodeisworking,configureitforthe
CRC16andCRC32algorithmsgivenaboveandensurethattheyproduce
thespecified"check"checksumwhenfedtheteststring"123456789"
(seeearlier).
/******************************************************************************/
/*Startofcrcmodel.h*/
/******************************************************************************/
/**/
/*Author:RossWilliams(ross@guest.adelaide.edu.au.).*/
/*Date:3June1993.*/
/*Status:Publicdomain.*/
/**/
/*Description:Thisistheheader(.h)fileforthereference*/
/*implementationoftheRocksoft^tmModelCRCAlgorithm.Formore*/
/*informationontheRocksoft^tmModelCRCAlgorithm,seethedocument*/
/*titled"APainlessGuidetoCRCErrorDetectionAlgorithms"byRoss*/
/*Williams(ross@guest.adelaide.edu.au.).Thisdocumentislikelytobein*/
/*"ftp.adelaide.edu.au/pub/rocksoft".*/
/**/
/*Note:RocksoftisatrademarkofRocksoftPtyLtd,Adelaide,Australia.*/
/**/
/******************************************************************************/
/**/
/*HowtoUseThisPackage*/
/**/
/*Step1:Declareavariableoftypecm_t.Declareanothervariable*/
/*(p_cmsay)oftypep_cm_tandinitializeittopointtothefirst*/
/*variable(e.g.p_cm_tp_cm=&cm_t).*/
/**/
/*Step2:Assignvaluestotheparameterfieldsofthestructure.*/
/*Ifyoudon'tknowwhattoassign,seethedocumentcitedearlier.*/
/*Forexample:*/
/*p_cm>cm_width=16;*/
/*p_cm>cm_poly=0x8005L;*/
/*p_cm>cm_init=0L;*/
http://www.zlib.net/crc_v3.txt

22/32

5/15/2015

www.zlib.net/crc_v3.txt

/*p_cm>cm_refin=TRUE;*/
/*p_cm>cm_refot=TRUE;*/
/*p_cm>cm_xorot=0L;*/
/*Note:Polyisspecifiedwithoutitstopbit(18005becomes8005).*/
/*Note:Widthisonebitlessthantherawpolywidth.*/
/**/
/*Step3:Initializetheinstancewithacallcm_ini(p_cm);*/
/**/
/*Step4:Processzeroormoremessagebytesbyplacingzeroormore*/
/*successivecallstocm_nxt.Example:cm_nxt(p_cm,ch);*/
/**/
/*Step5:ExtracttheCRCvalueatanytimebycallingcrc=cm_crc(p_cm);*/
/*IftheCRCisa16bitvalue,itwillbeinthebottom16bits.*/
/**/
/******************************************************************************/
/**/
/*DesignNotes*/
/**/
/*PORTABILITY:Thispackagehasbeencodedveryconservativelysothat*/
/*itwillrunonasmanymachinesaspossible.Forexample,allexternal*/
/*identifiershavebeenrestrictedto6charactersandallinternalonesto*/
/*8characters.Theprefixcm(forCrcModel)isusedasanattempttoavoid*/
/*namespacecollisions.Thispackageisendianindependent.*/
/**/
/*EFFICIENCY:Thispackage(anditsinterface)isnotdesignedfor*/
/*speed.Thepurposeofthispackageistoactasawelldefinedreference*/
/*modelforthespecificationofCRCalgorithms.Ifyouwantspeed,cookup*/
/*aspecifictabledrivenimplementationasdescribedinthedocumentcited*/
/*above.Thispackageisdesignedforvalidationonly;ifyouhavefoundor*/
/*implementedaCRCalgorithmandwishtodescribeitasasetofparameters*/
/*totheRocksoft^tmModelCRCAlgorithm,yourCRCalgorithmimplementation*/
/*shouldbehaveidenticallytothispackageunderthoseparameters.*/
/**/
/******************************************************************************/
/*Thefollowing#ifndefenclosesthisentire*/
/*headerfile,renderingitindempotent.*/
#ifndefCM_DONE
#defineCM_DONE
/******************************************************************************/
/*Thefollowingdefinitionsareextractedfrommystyleheaderfilewhich*/
/*wouldbecumbersometodistributewiththispackage.TheDONE_STYLEisthe*/
/*idempotencesymbolusedinmystyleheaderfile.*/
#ifndefDONE_STYLE
typedefunsignedlongulong;
typedefunsignedbool;
typedefunsignedchar*p_ubyte_;
#ifndefTRUE
#defineFALSE0
#defineTRUE1
#endif
/*Changetotheseconddefinitionifyoudon'thaveprototypes.*/
#defineP_(A)A
/*#defineP_(A)()*/
/*Uncommentthisdefinitionifyoudon'thavevoid.*/
/*typedefintvoid;*/
#endif
http://www.zlib.net/crc_v3.txt

23/32

5/15/2015

www.zlib.net/crc_v3.txt

/******************************************************************************/
/*CRCModelAbstractType*/
/**/
/*Thefollowingtypestoresthecontextofanexecutinginstanceofthe*/
/*modelalgorithm.Mostofthefieldsaremodelparameterswhichmustbe*/
/*setbeforethefirstinitializingcalltocm_ini.*/
typedefstruct
{
intcm_width;/*Parameter:Widthinbits[8,32].*/
ulongcm_poly;/*Parameter:Thealgorithm'spolynomial.*/
ulongcm_init;/*Parameter:Initialregistervalue.*/
boolcm_refin;/*Parameter:Reflectinputbytes?*/
boolcm_refot;/*Parameter:ReflectoutputCRC?*/
ulongcm_xorot;/*Parameter:XORthistooutputCRC.*/
ulongcm_reg;/*Context:Contextduringexecution.*/
}cm_t;
typedefcm_t*p_cm_t;
/******************************************************************************/
/*FunctionsThatImplementTheModel*/
/**/
/*Thefollowingfunctionsanimatethecm_tabstraction.*/
voidcm_iniP_((p_cm_tp_cm));
/*InitializestheargumentCRCmodelinstance.*/
/*Allparameterfieldsmustbesetbeforecallingthis.*/
voidcm_nxtP_((p_cm_tp_cm,intch));
/*Processesasinglemessagebyte[0,255].*/
voidcm_blkP_((p_cm_tp_cm,p_ubyte_blk_adr,ulongblk_len));
/*Processesablockofmessagebytes.*/
ulongcm_crcP_((p_cm_tp_cm));
/*ReturnstheCRCvalueforthemessagebytesprocessedsofar.*/
/******************************************************************************/
/*FunctionsForTableCalculation*/
/**/
/*ThefollowingfunctioncanbeusedtocalculateaCRClookuptable.*/
/*Itcanalsobeusedatruntimetocreateorcheckstatictables.*/
ulongcm_tabP_((p_cm_tp_cm,intindex));
/*Returnsthei'thentryforthelookuptableforthespecifiedalgorithm.*/
/*Thefunctionexaminesthefieldscm_width,cm_poly,cm_refin,andthe*/
/*argumenttableindexintherange[0,255]andreturnsthetableentryin*/
/*thebottomcm_widthbytesofthereturnvalue.*/
/******************************************************************************/
/*Endoftheheaderfileidempotence#ifndef*/
#endif
/******************************************************************************/
/*Endofcrcmodel.h*/
/******************************************************************************/
/******************************************************************************/
/*Startofcrcmodel.c*/
http://www.zlib.net/crc_v3.txt

24/32

5/15/2015

www.zlib.net/crc_v3.txt

/******************************************************************************/
/**/
/*Author:RossWilliams(ross@guest.adelaide.edu.au.).*/
/*Date:3June1993.*/
/*Status:Publicdomain.*/
/**/
/*Description:Thisistheimplementation(.c)fileforthereference*/
/*implementationoftheRocksoft^tmModelCRCAlgorithm.Formore*/
/*informationontheRocksoft^tmModelCRCAlgorithm,seethedocument*/
/*titled"APainlessGuidetoCRCErrorDetectionAlgorithms"byRoss*/
/*Williams(ross@guest.adelaide.edu.au.).Thisdocumentislikelytobein*/
/*"ftp.adelaide.edu.au/pub/rocksoft".*/
/**/
/*Note:RocksoftisatrademarkofRocksoftPtyLtd,Adelaide,Australia.*/
/**/
/******************************************************************************/
/**/
/*ImplementationNotes*/
/**/
/*Toavoidinconsistencies,thespecificationofeachfunctionisnotechoed*/
/*here.Seetheheaderfileforadescriptionofthesefunctions.*/
/*ThispackageislightoncheckingbecauseIwanttokeepitshortand*/
/*simpleandportable(i.e.itwouldbetoomessytodistributemyentire*/
/*Cculture(e.g.assertionspackage)withthispackage.*/
/**/
/******************************************************************************/
#include"crcmodel.h"
/******************************************************************************/
/*Thefollowingdefinitionsmakethecodemorereadable.*/
#defineBITMASK(X)(1L<<(X))
#defineMASK320xFFFFFFFFL
#defineLOCALstatic
/******************************************************************************/
LOCALulongreflectP_((ulongv,intb));
LOCALulongreflect(v,b)
/*Returnsthevaluevwiththebottomb[0,32]bitsreflected.*/
/*Example:reflect(0x3e23L,3)==0x3e26*/
ulongv;
intb;
{
inti;
ulongt=v;
for(i=0;i<b;i++)
{
if(t&1L)
v|=BITMASK((b1)i);
else
v&=~BITMASK((b1)i);
t>>=1;
}
returnv;
}
/******************************************************************************/
LOCALulongwidmaskP_((p_cm_t));
LOCALulongwidmask(p_cm)
/*Returnsalongwordwhosevalueis(2^p_cm>cm_width)1.*/
/*Thetrickistodothisportably(e.g.withoutdoing<<32).*/
http://www.zlib.net/crc_v3.txt

25/32

5/15/2015

www.zlib.net/crc_v3.txt

p_cm_tp_cm;
{
return(((1L<<(p_cm>cm_width1))1L)<<1)|1L;
}
/******************************************************************************/
voidcm_ini(p_cm)
p_cm_tp_cm;
{
p_cm>cm_reg=p_cm>cm_init;
}
/******************************************************************************/
voidcm_nxt(p_cm,ch)
p_cm_tp_cm;
intch;
{
inti;
ulonguch=(ulong)ch;
ulongtopbit=BITMASK(p_cm>cm_width1);
if(p_cm>cm_refin)uch=reflect(uch,8);
p_cm>cm_reg^=(uch<<(p_cm>cm_width8));
for(i=0;i<8;i++)
{
if(p_cm>cm_reg&topbit)
p_cm>cm_reg=(p_cm>cm_reg<<1)^p_cm>cm_poly;
else
p_cm>cm_reg<<=1;
p_cm>cm_reg&=widmask(p_cm);
}
}
/******************************************************************************/
voidcm_blk(p_cm,blk_adr,blk_len)
p_cm_tp_cm;
p_ubyte_blk_adr;
ulongblk_len;
{
while(blk_len)cm_nxt(p_cm,*blk_adr++);
}
/******************************************************************************/
ulongcm_crc(p_cm)
p_cm_tp_cm;
{
if(p_cm>cm_refot)
returnp_cm>cm_xorot^reflect(p_cm>cm_reg,p_cm>cm_width);
else
returnp_cm>cm_xorot^p_cm>cm_reg;
}
/******************************************************************************/
ulongcm_tab(p_cm,index)
p_cm_tp_cm;
intindex;
{
inti;
ulongr;
ulongtopbit=BITMASK(p_cm>cm_width1);
http://www.zlib.net/crc_v3.txt

26/32

5/15/2015

www.zlib.net/crc_v3.txt

ulonginbyte=(ulong)index;
if(p_cm>cm_refin)inbyte=reflect(inbyte,8);
r=inbyte<<(p_cm>cm_width8);
for(i=0;i<8;i++)
if(r&topbit)
r=(r<<1)^p_cm>cm_poly;
else
r<<=1;
if(p_cm>cm_refin)r=reflect(r,p_cm>cm_width);
returnr&widmask(p_cm);
}
/******************************************************************************/
/*Endofcrcmodel.c*/
/******************************************************************************/
18.RollYourOwnTableDrivenImplementation

DespiteallthefussI'vemadeaboutunderstandinganddefiningCRC
algorithms,themechanicsoftheirhighspeedimplementationremains
trivial.Therearereallyonlytwoforms:normalandreflected.Normal
shiftstotheleftandcoversthecaseofalgorithmswithRefin=FALSE
andRefot=FALSE.Reflectedshiftstotherightandcoversalgorithms
withboththoseparameterstrue.(Ifyouwantoneparametertrueand
theotherfalse,you'llhavetofigureitoutforyourself!)The
polynomialisembeddedinthelookuptable(tobediscussed).The
otherparameters,InitandXorOtcanbecodedasmacros.Hereisthe
32bitnormalform(the16bitformissimilar).
unsignedlongcrc_normal();
unsignedlongcrc_normal(blk_adr,blk_len)
unsignedchar*blk_adr;
unsignedlongblk_len;
{
unsignedlongcrc=INIT;
while(blk_len)
crc=crctable[((crc>>24)^*blk_adr++)&0xFFL]^(crc<<8);
returncrc^XOROT;
}
Hereisthereflectedform:
unsignedlongcrc_reflected();
unsignedlongcrc_reflected(blk_adr,blk_len)
unsignedchar*blk_adr;
unsignedlongblk_len;
{
unsignedlongcrc=INIT_REFLECTED;
while(blk_len)
crc=crctable[(crc^*blk_adr++)&0xFFL]^(crc>>8));
returncrc^XOROT;
}
Note:Ihavecarefullycheckedtheabovetwocodefragments,butI
haven'tactuallycompiledortestedthem.Thisshouldn'tmatterto
you,as,nomatterWHATyoucode,youwillalwaysbeabletotellif
youhavegotitrightbyrunningwhateveryouhavecreatedagainstthe
referencemodelgivenearlier.Thecodefragmentsabovearereally
justaroughguide.Thereferencemodelisthedefinitiveguide.
Note:Ifyoudon'tcaremuchaboutspeed,justusethereferencemodel
code!
http://www.zlib.net/crc_v3.txt

27/32

5/15/2015

www.zlib.net/crc_v3.txt

19.GeneratingALookupTable

Theonlycomponentmissingfromthenormalandreversedcodefragments
intheprevioussectionisthelookuptable.Thelookuptablecanbe
computedatruntimeusingthecm_tabfunctionofthemodelpackage
givenearlier,orcanbeprecomputedandinsertedintotheCprogram.
Ineithercase,itshouldbenotedthatthelookuptabledependsonly
onthePOLYandRefIn(andRefOt)parameters.Basically,the
polynomialdeterminesthetable,butyoucangenerateareflected
tabletooifyouwanttousethereflectedformabove.
Thefollowingprogramgeneratesanydesired16bitor32bitlookup
table.Skiptotheword"Summary"ifyouwanttoskipoverthiscode.

/******************************************************************************/
/*Startofcrctable.c*/
/******************************************************************************/
/**/
/*Author:RossWilliams(ross@guest.adelaide.edu.au.).*/
/*Date:3June1993.*/
/*Version:1.0.*/
/*Status:Publicdomain.*/
/**/
/*Description:ThisprogramwritesaCRClookuptable(suitablefor*/
/*inclusioninaCprogram)toadesignatedoutputfile.Theprogramcanbe*/
/*staticallyconfiguredtoproduceanytablecoveredbytheRocksoft^tm*/
/*ModelCRCAlgorithm.FormoreinformationontheRocksoft^tmModelCRC*/
/*Algorithm,seethedocumenttitled"APainlessGuidetoCRCError*/
/*DetectionAlgorithms"byRossWilliams(ross@guest.adelaide.edu.au.).This*/
/*documentislikelytobein"ftp.adelaide.edu.au/pub/rocksoft".*/
/**/
/*Note:RocksoftisatrademarkofRocksoftPtyLtd,Adelaide,Australia.*/
/**/
/******************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include"crcmodel.h"
/******************************************************************************/
/*TABLEPARAMETERS*/
/*================*/
/*Thefollowingparametersentirelydeterminethetabletobegenerated.You*/
/*shouldneedtomodifyonlythedefinitionsinthissectionbeforerunning*/
/*thisprogram.*/
/**/
/*TB_FILEisthenameoftheoutputfile.*/
/*TB_WIDTHisthetablewidthinbytes(either2or4).*/
/*TB_POLYisthe"polynomial",whichmustbeTB_WIDTHbyteswide.*/
/*TB_REVERindicateswhetherthetableistobereversed(reflected).*/
/**/
/*Example:*/
/**/
/*#defineTB_FILE"crctable.out"*/
/*#defineTB_WIDTH2*/
/*#defineTB_POLY0x8005L*/
/*#defineTB_REVERTRUE*/
#defineTB_FILE"crctable.out"
#defineTB_WIDTH4
#defineTB_POLY0x04C11DB7L
http://www.zlib.net/crc_v3.txt

28/32

5/15/2015

www.zlib.net/crc_v3.txt

#defineTB_REVERTRUE
/******************************************************************************/
/*Miscellaneousdefinitions.*/
#defineLOCALstatic
FILE*outfile;
#defineWR(X)fprintf(outfile,(X))
#defineWP(X,Y)fprintf(outfile,(X),(Y))
/******************************************************************************/
LOCALvoidchk_errP_((char*));
LOCALvoidchk_err(mess)
/*Ifmessisnonempty,writeitoutandabort.Otherwise,checktheerror*/
/*statusofoutfileandabortifanerrorhasoccurred.*/
char*mess;
{
if(mess[0]!=0){printf("%s\n",mess);exit(EXIT_FAILURE);}
if(ferror(outfile)){perror("chk_err");exit(EXIT_FAILURE);}
}
/******************************************************************************/
LOCALvoidchkparamP_((void));
LOCALvoidchkparam()
{
if((TB_WIDTH!=2)&&(TB_WIDTH!=4))
chk_err("chkparam:Widthparameterisillegal.");
if((TB_WIDTH==2)&&(TB_POLY&0xFFFF0000L))
chk_err("chkparam:Polyparameteristoowide.");
if((TB_REVER!=FALSE)&&(TB_REVER!=TRUE))
chk_err("chkparam:Reverseparameterisnotboolean.");
}
/******************************************************************************/
LOCALvoidgentableP_((void));
LOCALvoidgentable()
{
WR("/*****************************************************************/\n");
WR("/**/\n");
WR("/*CRCLOOKUPTABLE*/\n");
WR("/*================*/\n");
WR("/*ThefollowingCRClookuptablewasgeneratedautomagically*/\n");
WR("/*bytheRocksoft^tmModelCRCAlgorithmTableGeneration*/\n");
WR("/*ProgramV1.0usingthefollowingmodelparameters:*/\n");
WR("/**/\n");
WP("/*Width:%1lubytes.*/\n",
(ulong)TB_WIDTH);
if(TB_WIDTH==2)
WP("/*Poly:0x%04lX*/\n",
(ulong)TB_POLY);
else
WP("/*Poly:0x%08lXL*/\n",
(ulong)TB_POLY);
if(TB_REVER)
WR("/*Reverse:TRUE.*/\n");
else
WR("/*Reverse:FALSE.*/\n");
WR("/**/\n");
WR("/*FormoreinformationontheRocksoft^tmModelCRCAlgorithm,*/\n");
WR("/*seethedocumenttitled\"APainlessGuidetoCRCError*/\n");
WR("/*DetectionAlgorithms\"byRossWilliams*/\n");
http://www.zlib.net/crc_v3.txt

29/32

5/15/2015

www.zlib.net/crc_v3.txt

WR("/*(ross@guest.adelaide.edu.au.).Thisdocumentislikelytobe*/\n");
WR("/*intheFTParchive\"ftp.adelaide.edu.au/pub/rocksoft\".*/\n");
WR("/**/\n");
WR("/*****************************************************************/\n");
WR("\n");
switch(TB_WIDTH)
{
case2:WR("unsignedshortcrctable[256]=\n{\n");break;
case4:WR("unsignedlongcrctable[256]=\n{\n");break;
default:chk_err("gentable:TB_WIDTHisinvalid.");
}
chk_err("");
{
inti;
cm_tcm;
char*form=(TB_WIDTH==2)?"0x%04lX":"0x%08lXL";
intperline=(TB_WIDTH==2)?8:4;
cm.cm_width=TB_WIDTH*8;
cm.cm_poly=TB_POLY;
cm.cm_refin=TB_REVER;
for(i=0;i<256;i++)
{
WR("");
WP(form,(ulong)cm_tab(&cm,i));
if(i!=255)WR(",");
if(((i+1)%perline)==0)WR("\n");
chk_err("");
}
WR("};\n");
WR("\n");
WR("/*****************************************************************/\n");
WR("/*EndofCRCLookupTable*/\n");
WR("/*****************************************************************/\n");
WR("");
chk_err("");
}
}
/******************************************************************************/
main()
{
printf("\n");
printf("Rocksoft^tmModelCRCAlgorithmTableGenerationProgramV1.0\n");
printf("\n");
printf("Outputfileis\"%s\".\n",TB_FILE);
chkparam();
outfile=fopen(TB_FILE,"w");chk_err("");
gentable();
if(fclose(outfile)!=0)
chk_err("main:Couldn'tcloseoutputfile.");
printf("\nSUCCESS:Thetablehasbeensuccessfullywritten.\n");
}
/******************************************************************************/
/*Endofcrctable.c*/
/******************************************************************************/
20.Summary

ThisdocumenthasprovidedadetailedexplanationofCRCalgorithms
http://www.zlib.net/crc_v3.txt

30/32

5/15/2015

www.zlib.net/crc_v3.txt

explainingtheirtheoryandsteppingthroughincreasingly
sophisticatedimplementationsrangingfromsimplebitshiftingthrough
tobyteatatimetabledrivenimplementations.Thevarious
implementationsofdifferentCRCalgorithmsthatmakethemconfusing
todealwithhavebeenexplained.Aparameterizedmodelalgorithmhas
beendescribedthatcanbeusedtopreciselydefineaparticularCRC
algorithm,andareferenceimplementationprovided.Finally,aprogram
togenerateCRCtableshasbeenprovided.
21.Corrections

Ifyouthinkthatanypartofthisdocumentisunclearorincorrect,
orhaveanyotherinformation,orsuggestionsonhowthisdocument
couldbeimproved,pleasecontexttheauthor.Inparticular,Iwould
liketohearfromanyonewhocanprovideRocksoft^tmModelCRC
Algorithmparametersforstandardalgorithmsoutthere.
A.Glossary

CHECKSUMAnumberthathasbeencalculatedasafunctionofsome
message.Theliteralinterpretationofthisword"CheckSum"indicates
thatthefunctionshouldinvolvesimplyaddingupthebytesinthe
message.Perhapsthiswaswhatearlychecksumswere.Today,however,
althoughmoresophisticatedformulaeareused,theterm"checksum"is
stillused.
CRCThisstandsfor"CyclicRedundancyCode".Whereastheterm
"checksum"seemstobeusedtorefertoanynoncryptographicchecking
informationunit,theterm"CRC"seemstobereservedonlyfor
algorithmsthatarebasedonthe"polynomial"divisionidea.
GThissymbolisusedinthisdocumenttorepresentthePoly.
MESSAGETheinputdatabeingchecksummed.Thisisusuallystructured
asasequenceofbytes.Whetherthetopbitorthebottombitofeach
byteistreatedasthemostsignificantorleastsignificantisa
parameterofCRCalgorithms.
POLYThisismyfriendlytermforthepolynomialofaCRC.
POLYNOMIALThe"polynomial"ofaCRCalgorithmissimplythedivisor
inthedivisionimplementingtheCRCalgorithm.
REFLECTAbinarynumberisreflectedbyswappingallofitsbits
aroundthecentralpoint.Forexample,1101isthereflectionof1011.
ROCKSOFT^TMMODELCRCALGORITHMAparameterizedalgorithmwhose
purposeistoactasasolidreferencefordescribingCRCalgorithms.
TypicallyCRCalgorithmsarespecifiedbyquotingapolynomial.
However,inordertoconstructapreciseimplementation,onealso
needstoknowinitializationvaluesandsoon.
WIDTHThewidthofaCRCalgorithmisthewidthofitspolynomical
minusone.Forexample,ifthepolynomialis11010,thewidthwouldbe
4bits.Thewidthisusuallysettobeamultipleof8bits.
B.References

[Griffiths87]Griffiths,G.,CarlyleStones,G.,"TheTeaLeafReader
Algorithm:AnEfficientImplementationofCRC16andCRC32",
CommunicationsoftheACM,30(7),pp.617620.Comment:Thispaper
describesahighspeedtabledrivenimplementationofCRCalgorithms.
Thetechniqueseemstobeatouchmessy,andissupercededbythe
Sarwetealgorithm.
http://www.zlib.net/crc_v3.txt

31/32

5/15/2015

www.zlib.net/crc_v3.txt

[Knuth81]Knuth,D.E.,"TheArtofComputerProgramming",Volume2:
SeminumericalAlgorithms,Section4.6.
[Nelson91]Nelson,M.,"TheDataCompressionBook",M&TBooks,(501
GalvestonDrive,RedwoodCity,CA94063),1991,ISBN:1558512144.
Comment:Ifyouwanttoseearealimplementationofareal32bit
checksumalgorithm,lookonpages440,and446448.
[Sarwate88]Sarwate,D.V.,"ComputationofCyclicRedundancyChecks
viaTableLookUp",CommunicationsoftheACM,31(8),pp.10081013.
Comment:Thispaperdescribesahighspeedtabledrivenimplementation
forCRCalgorithmsthatissuperiortothetealeafalgorithm.
AlthoughthispaperdescribesthetechniqueusedbymostmodernCRC
implementations,Ifoundtheappendixofthispaper(whereallthe
goodstuffis)difficulttounderstand.
[Tanenbaum81]Tanenbaum,A.S.,"ComputerNetworks",PrenticeHall,
1981,ISBN:0131646990.Comment:Section3.5.3onpages128to132
providesaverycleardescriptionofCRCcodes.However,itdoesnot
describetabledrivenimplementationtechniques.
C.ReferencesIHaveDetectedButHaven'tYetSighted

Boudreau,Steen,"CyclicRedundancyCheckingbyProgram,"AFIPS
Proceedings,Vol.39,1971.
Davies,Barber,"ComputerNetworksandTheirProtocols,"J.Wiley&
Sons,1979.
Higginson,Kirstein,"OntheComputationofCyclicRedundancyChecks
byProgram,"TheComputerJournal(British),Vol.16,No.1,Feb1973.
McNamara,J.E.,"TechnicalAspectsofDataCommunication,"2nd
Edition,DigitalPress,Bedford,Massachusetts,1982.
MartonandFrambs,"ACyclicRedundancyChecking(CRC)Algorithm,"
HoneywellComputerJournal,Vol.5,No.3,1971.
NelsonM.,"FileverificationusingCRC",DrDobbsJournal,May1992,
pp.6467.
RamabadranT.V.,GaitondeS.S.,"AtutorialonCRCcomputations",IEEE
Micro,Aug1988.
SchwadererW.D.,"CRCCalculation",April85PCTechJournal,
pp.118133.
WardR.K,TabandehM.,"ErrorCorrectionandDetection,AGeometric
Approach"TheComputerJournal,Vol.27,No.3,1984,pp.246253.
Wecker,S.,"ATableLookupAlgorithmforSoftwareComputationof
CyclicRedundancyCheck(CRC),"DigitalEquipmentCorporation
memorandum,1974.
<EndofDocument>

http://www.zlib.net/crc_v3.txt

32/32

You might also like