This is from CEE-64 Alive! Vol 3 Issue 2, but was posted originally in Fidonet by George Hug. --------------------- Text Import Start --------------------- --------------- CRC INFORMATION --------------- Edited by Jack Vander White Rod Gasson, the author of the QWKRR off-line reader for the 128 had asked for information on the CRC routines used by MS-DOS ARC. George Hug provided them and even went a little further. We are printing their messages here because we feel that these routines may be of great interest to some CBM programmers who are working on porting programs between CBM and MS-DOS. ------------ CRC Routines ------------ From : George Hug Rod mentioned the problem of having to come back and re-write the file in order to put the CRC value in the right place after you've calculated it. There is an interesting property of all of these CRC algorithms which might be useful in solving that problem. (Well probably not, but it's still interesting.) If you calculate the CRC of a file, and then process the CRC value itself through the CRC routine, in low...high sequence, the resulting final CRC will always be zero. Let's say you have a 12-byte file which has a CRC16 of $A467. If you add the bytes $67 and $A4 to the end of that file, the new 14-byte file will now have a CRC16 of zero. If it is possible to add "garbage" bytes to the end of a file which will be ignored by the mail processor after it is unarced, then you could arrange for such files to always have a CRC16 of zero. This works as well with the CRC32 used by ZIP, except that you have to keep in mind that the value used in the ZIP header is the one's complement (the inverse) of the actual CRC32 value. So the predestined value would be $FFFFFFFF in the ZIP header instead of zero. And of course you would be adding four bytes to the end of the file instead of two. -------------------------- CRC routines Msg #1 of 3. -------------------------- There are three messages, each ending with "End of Message". The second and third messages contain the look-up tables (512 bytes for MS-DOS ARC's CRC-16, and 1024 bytes for ZIP's CRC-32). 1. To calculate the CRC of a file or block of text, first initialize the CRC value to #$0000 for CRC-16, or to #$FFFFFFFF for CRC-32. 2. Process each byte of the block or file through the appropriate CRC routine shown below - ARCCRC or ZIPCRC. 3. For CRC-32 only, convert the final CRC value to its one's complement. In other words, invert each byte of the CRC (EOR it with #$FF). 4. The resulting CRC appears in the appropriate place in the ARC or ZIP header in standard 6502 (and 80x86) low...high sequence. For MS-DOS ARC CRC-16 (Note: this is NOT the Xmodem CRC-16): Define the lookup tables ATABLE0 and ATABLE1 per msg #2. Define two memory locations as ACRCL and ACRCH, 1 byte each. For each CRC calculation, initialize ACRCL and ACRCH to zero, then process each byte through the following routine. 3000 arccrc eor acrcl ;enter with byte to be processed in Acc. 3010 tax 3020 lda atable0,x 3030 eor acrch 3040 sta acrcl 3050 lda atable1,x 3060 sta acrch 3070 rts For ZIP CRC-32: Define the lookup tables ZTABLE0 thru ZTABLE3 per msg #2 and msg #3. Define four memory locations as ZCRC0 thru ZCRC3, 1 byte each. For each CRC calculation, initialize ZCRC0 thru ZCRC3 to #$FF, then process each byte through the following routine. When all bytes have been processed, invert ZCRC0 thru ZCRC3. 3100 zipcrc eor zcrc0 ;enter with byte to be processed in Acc. 3110 tax 3120 lda zcrc1 3130 eor ztable0,x 3140 sta zcrc0 3150 lda zcrc2 3160 eor ztable1,x 3170 sta zcrc1 3180 lda zcrc3 3190 eor ztable2,x 3200 sta zcrc2 3210 lda ztable3,x 3220 sta zcrc3 3230 rts End of Message -------------------------- CRC routines Msg #2 of 3. -------------------------- 4000 atable0 ;1st of 2 lookup tables for ARC (CRC-16). 4010 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4020 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4030 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4040 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4050 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4060 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4070 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4080 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4090 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4100 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4110 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4120 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4130 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4140 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4150 .b $01,$c0,$80,$41,$00,$c1,$81,$40,$00,$c1,$81,$40,$01,$c0,$80,$41 4160 .b $00,$c1,$81,$40,$01,$c0,$80,$41,$01,$c0,$80,$41,$00,$c1,$81,$40 4200 atable1 ;2nd of 2 lookup tables for ARC (CRC-16). 4210 .b $00,$c0,$c1,$01,$c3,$03,$02,$c2,$c6,$06,$07,$c7,$05,$c5,$c4,$04 4220 .b $cc,$0c,$0d,$cd,$0f,$cf,$ce,$0e,$0a,$ca,$cb,$0b,$c9,$09,$08,$c8 4230 .b $d8,$18,$19,$d9,$1b,$db,$da,$1a,$1e,$de,$df,$1f,$dd,$1d,$1c,$dc 4240 .b $14,$d4,$d5,$15,$d7,$17,$16,$d6,$d2,$12,$13,$d3,$11,$d1,$d0,$10 4250 .b $f0,$30,$31,$f1,$33,$f3,$f2,$32,$36,$f6,$f7,$37,$f5,$35,$34,$f4 4260 .b $3c,$fc,$fd,$3d,$ff,$3f,$3e,$fe,$fa,$3a,$3b,$fb,$39,$f9,$f8,$38 4270 .b $28,$e8,$e9,$29,$eb,$2b,$2a,$ea,$ee,$2e,$2f,$ef,$2d,$ed,$ec,$2c 4280 .b $e4,$24,$25,$e5,$27,$e7,$e6,$26,$22,$e2,$e3,$23,$e1,$21,$20,$e0 4290 .b $a0,$60,$61,$a1,$63,$a3,$a2,$62,$66,$a6,$a7,$67,$a5,$65,$64,$a4 4300 .b $6c,$ac,$ad,$6d,$af,$6f,$6e,$ae,$aa,$6a,$6b,$ab,$69,$a9,$a8,$68 4310 .b $78,$b8,$b9,$79,$bb,$7b,$7a,$ba,$be,$7e,$7f,$bf,$7d,$bd,$bc,$7c 4320 .b $b4,$74,$75,$b5,$77,$b7,$b6,$76,$72,$b2,$b3,$73,$b1,$71,$70,$b0 4330 .b $50,$90,$91,$51,$93,$53,$52,$92,$96,$56,$57,$97,$55,$95,$94,$54 4340 .b $9c,$5c,$5d,$9d,$5f,$9f,$9e,$5e,$5a,$9a,$9b,$5b,$99,$59,$58,$98 4350 .b $88,$48,$49,$89,$4b,$8b,$8a,$4a,$4e,$8e,$8f,$4f,$8d,$4d,$4c,$8c 4360 .b $44,$84,$85,$45,$87,$47,$46,$86,$82,$42,$43,$83,$41,$81,$80,$40 4400 ztable0 ;1st of 4 lookup tables for ZIP (CRC-32). 4410 .b $00,$96,$2c,$ba,$19,$8f,$35,$a3,$32,$a4,$1e,$88,$2b,$bd,$07,$91 4420 .b $64,$f2,$48,$de,$7d,$eb,$51,$c7,$56,$c0,$7a,$ec,$4f,$d9,$63,$f5 4430 .b $c8,$5e,$e4,$72,$d1,$47,$fd,$6b,$fa,$6c,$d6,$40,$e3,$75,$cf,$59 4440 .b $ac,$3a,$80,$16,$b5,$23,$99,$0f,$9e,$08,$b2,$24,$87,$11,$ab,$3d 4450 .b $90,$06,$bc,$2a,$89,$1f,$a5,$33,$a2,$34,$8e,$18,$bb,$2d,$97,$01 4460 .b $f4,$62,$d8,$4e,$ed,$7b,$c1,$57,$c6,$50,$ea,$7c,$df,$49,$f3,$65 4470 .b $58,$ce,$74,$e2,$41,$d7,$6d,$fb,$6a,$fc,$46,$d0,$73,$e5,$5f,$c9 4480 .b $3c,$aa,$10,$86,$25,$b3,$09,$9f,$0e,$98,$22,$b4,$17,$81,$3b,$ad 4490 .b $20,$b6,$0c,$9a,$39,$af,$15,$83,$12,$84,$3e,$a8,$0b,$9d,$27,$b1 4500 .b $44,$d2,$68,$fe,$5d,$cb,$71,$e7,$76,$e0,$5a,$cc,$6f,$f9,$43,$d5 4510 .b $e8,$7e,$c4,$52,$f1,$67,$dd,$4b,$da,$4c,$f6,$60,$c3,$55,$ef,$79 4520 .b $8c,$1a,$a0,$36,$95,$03,$b9,$2f,$be,$28,$92,$04,$a7,$31,$8b,$1d 4530 .b $b0,$26,$9c,$0a,$a9,$3f,$85,$13,$82,$14,$ae,$38,$9b,$0d,$b7,$21 4540 .b $d4,$42,$f8,$6e,$cd,$5b,$e1,$77,$e6,$70,$ca,$5c,$ff,$69,$d3,$45 4550 .b $78,$ee,$54,$c2,$61,$f7,$4d,$db,$4a,$dc,$66,$f0,$53,$c5,$7f,$e9 4560 .b $1c,$8a,$30,$a6,$05,$93,$29,$bf,$2e,$b8,$02,$94,$37,$a1,$1b,$8d End of Message -------------------------- CRC routines. Msg #3 of 3. -------------------------- 4600 ztable1 ;2nd of 4 lookup tables for ZIP (CRC-32). 4610 .b $00,$30,$61,$51,$c4,$f4,$a5,$95,$88,$b8,$e9,$d9,$4c,$7c,$2d,$1d 4620 .b $10,$20,$71,$41,$d4,$e4,$b5,$85,$98,$a8,$f9,$c9,$5c,$6c,$3d,$0d 4630 .b $20,$10,$41,$71,$e4,$d4,$85,$b5,$a8,$98,$c9,$f9,$6c,$5c,$0d,$3d 4640 .b $30,$00,$51,$61,$f4,$c4,$95,$a5,$b8,$88,$d9,$e9,$7c,$4c,$1d,$2d 4650 .b $41,$71,$20,$10,$85,$b5,$e4,$d4,$c9,$f9,$a8,$98,$0d,$3d,$6c,$5c 4660 .b $51,$61,$30,$00,$95,$a5,$f4,$c4,$d9,$e9,$b8,$88,$1d,$2d,$7c,$4c 4670 .b $61,$51,$00,$30,$a5,$95,$c4,$f4,$e9,$d9,$88,$b8,$2d,$1d,$4c,$7c 4680 .b $71,$41,$10,$20,$b5,$85,$d4,$e4,$f9,$c9,$98,$a8,$3d,$0d,$5c,$6c 4690 .b $83,$b3,$e2,$d2,$47,$77,$26,$16,$0b,$3b,$6a,$5a,$cf,$ff,$ae,$9e 4700 .b $93,$a3,$f2,$c2,$57,$67,$36,$06,$1b,$2b,$7a,$4a,$df,$ef,$be,$8e 4710 .b $a3,$93,$c2,$f2,$67,$57,$06,$36,$2b,$1b,$4a,$7a,$ef,$df,$8e,$be 4720 .b $b3,$83,$d2,$e2,$77,$47,$16,$26,$3b,$0b,$5a,$6a,$ff,$cf,$9e,$ae 4730 .b $c2,$f2,$a3,$93,$06,$36,$67,$57,$4a,$7a,$2b,$1b,$8e,$be,$ef,$df 4740 .b $d2,$e2,$b3,$83,$16,$26,$77,$47,$5a,$6a,$3b,$0b,$9e,$ae,$ff,$cf 4750 .b $e2,$d2,$83,$b3,$26,$16,$47,$77,$6a,$5a,$0b,$3b,$ae,$9e,$cf,$ff 4760 .b $f2,$c2,$93,$a3,$36,$06,$57,$67,$7a,$4a,$1b,$2b,$be,$8e,$df,$ef 4800 ztable2 ;3rd of 4 lookup tables for ZIP (CRC-32). 4810 .b $00,$07,$0e,$09,$6d,$6a,$63,$64,$db,$dc,$d5,$d2,$b6,$b1,$b8,$bf 4820 .b $b7,$b0,$b9,$be,$da,$dd,$d4,$d3,$6c,$6b,$62,$65,$01,$06,$0f,$08 4830 .b $6e,$69,$60,$67,$03,$04,$0d,$0a,$b5,$b2,$bb,$bc,$d8,$df,$d6,$d1 4840 .b $d9,$de,$d7,$d0,$b4,$b3,$ba,$bd,$02,$05,$0c,$0b,$6f,$68,$61,$66 4850 .b $dc,$db,$d2,$d5,$b1,$b6,$bf,$b8,$07,$00,$09,$0e,$6a,$6d,$64,$63 4860 .b $6b,$6c,$65,$62,$06,$01,$08,$0f,$b0,$b7,$be,$b9,$dd,$da,$d3,$d4 4870 .b $b2,$b5,$bc,$bb,$df,$d8,$d1,$d6,$69,$6e,$67,$60,$04,$03,$0a,$0d 4880 .b $05,$02,$0b,$0c,$68,$6f,$66,$61,$de,$d9,$d0,$d7,$b3,$b4,$bd,$ba 4890 .b $b8,$bf,$b6,$b1,$d5,$d2,$db,$dc,$63,$64,$6d,$6a,$0e,$09,$00,$07 4900 .b $0f,$08,$01,$06,$62,$65,$6c,$6b,$d4,$d3,$da,$dd,$b9,$be,$b7,$b0 4910 .b $d6,$d1,$d8,$df,$bb,$bc,$b5,$b2,$0d,$0a,$03,$04,$60,$67,$6e,$69 4920 .b $61,$66,$6f,$68,$0c,$0b,$02,$05,$ba,$bd,$b4,$b3,$d7,$d0,$d9,$de 4930 .b $64,$63,$6a,$6d,$09,$0e,$07,$00,$bf,$b8,$b1,$b6,$d2,$d5,$dc,$db 4940 .b $d3,$d4,$dd,$da,$be,$b9,$b0,$b7,$08,$0f,$06,$01,$65,$62,$6b,$6c 4950 .b $0a,$0d,$04,$03,$67,$60,$69,$6e,$d1,$d6,$df,$d8,$bc,$bb,$b2,$b5 4960 .b $bd,$ba,$b3,$b4,$d0,$d7,$de,$d9,$66,$61,$68,$6f,$0b,$0c,$05,$02 5000 ztable3 ;4th of 4 lookup tables for ZIP (CRC-32). 5010 .b $00,$77,$ee,$99,$07,$70,$e9,$9e,$0e,$79,$e0,$97,$09,$7e,$e7,$90 5020 .b $1d,$6a,$f3,$84,$1a,$6d,$f4,$83,$13,$64,$fd,$8a,$14,$63,$fa,$8d 5030 .b $3b,$4c,$d5,$a2,$3c,$4b,$d2,$a5,$35,$42,$db,$ac,$32,$45,$dc,$ab 5040 .b $26,$51,$c8,$bf,$21,$56,$cf,$b8,$28,$5f,$c6,$b1,$2f,$58,$c1,$b6 5050 .b $76,$01,$98,$ef,$71,$06,$9f,$e8,$78,$0f,$96,$e1,$7f,$08,$91,$e6 5060 .b $6b,$1c,$85,$f2,$6c,$1b,$82,$f5,$65,$12,$8b,$fc,$62,$15,$8c,$fb 5070 .b $4d,$3a,$a3,$d4,$4a,$3d,$a4,$d3,$43,$34,$ad,$da,$44,$33,$aa,$dd 5080 .b $50,$27,$be,$c9,$57,$20,$b9,$ce,$5e,$29,$b0,$c7,$59,$2e,$b7,$c0 5090 .b $ed,$9a,$03,$74,$ea,$9d,$04,$73,$e3,$94,$0d,$7a,$e4,$93,$0a,$7d 5100 .b $f0,$87,$1e,$69,$f7,$80,$19,$6e,$fe,$89,$10,$67,$f9,$8e,$17,$60 5110 .b $d6,$a1,$38,$4f,$d1,$a6,$3f,$48,$d8,$af,$36,$41,$df,$a8,$31,$46 5120 .b $cb,$bc,$25,$52,$cc,$bb,$22,$55,$c5,$b2,$2b,$5c,$c2,$b5,$2c,$5b 5130 .b $9b,$ec,$75,$02,$9c,$eb,$72,$05,$95,$e2,$7b,$0c,$92,$e5,$7c,$0b 5140 .b $86,$f1,$68,$1f,$81,$f6,$6f,$18,$88,$ff,$66,$11,$8f,$f8,$61,$16 5150 .b $a0,$d7,$4e,$39,$a7,$d0,$49,$3e,$ae,$d9,$40,$37,$a9,$de,$47,$30 5160 .b $bd,$ca,$53,$24,$ba,$cd,$54,$23,$b3,$c4,$5d,$2a,$b4,$c3,$5a,$2d End of Message --------------------- Text Import End ---------------------