

Task

Demonstrate a method of deriving the Cyclic Redundancy Check from within the language.



The result should be in accordance with ISO 3309, ITU-T V.42, Gzip and PNG.

Algorithms are described on Computation of CRC in Wikipedia.
This variant of CRC-32 uses LSB-first order, sets the initial CRC to FFFFFFFF16, and complements the final CRC.

For the purpose of this task, generate a CRC-32 checksum for the ASCII encoded string:

 The quick brown fox jumps over the lazy dog


 
: crc/ ( n -- n ) 8 0 do dup 1 rshift swap 1 and if $edb88320 xor then loop ;
 
: crcfill   256 0 do i crc/ , loop ;
 
create crctbl crcfill
 
: crc+ ( crc n -- crc' ) over xor $ff and  cells crctbl + @  swap 8 rshift xor ;
 
: crcbuf ( crc str len -- crc ) bounds ?do i c@ crc+ loop ;
 
$ffffffff s" The quick brown fox jumps over the lazy dog" crcbuf $ffffffff xor hex.  bye   \ $414FA339
 