

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


link hexcvt,printf
 
procedure main()
   s := "The quick brown fox jumps over the lazy dog"
   a := "414FA339"
   printf("crc(%i)=%s - implementation is %s\n",
          s,r := crc32(s),if r == a then "correct" else "in error")
end
 
procedure crc32(s)      #: return crc-32 (ISO 3309, ITU-T V.42, Gzip, PNG) of s
static crcL,mask
initial {
   crcL := list(256)                            # crc table
   p := [0,1,2,4,5,7,8,10,11,12,16,22,23,26]    # polynomial terms 
   mask := 2^32-1                               # word size mask   
   every (poly := 0) := ior(poly,ishift(1,31-p[1 to *p]))
   every c := n := 0 to *crcL-1 do {            # table 
      every 1 to 8 do 
         c := iand(mask, 
                   if iand(c,1) = 1 then
                      ixor(poly,ishift(c,-1)) 
                   else 
                      ishift(c,-1)
                  )
      crcL[n+1] := c
      }
   }
 
   crc := ixor(0,mask)                          # invert bits
   every crc := iand(mask,
                     ixor(crcL[iand(255,ixor(crc,ord(!s)))+1],ishift(crc,-8)))               
   return hexstring(ixor(crc,mask))             # return hexstring
end