

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


#COMPILE EXE
#DIM ALL
#COMPILER PBCC 6
 
' ***********
 
FUNCTION CRC32(BYVAL p AS BYTE PTR, BYVAL NumBytes AS DWORD) AS DWORD
  STATIC LUT() AS DWORD
  LOCAL i, j, k, crc AS DWORD
 
  IF ARRAYATTR(LUT(), 0) = 0 THEN
    REDIM LUT(0 TO 255)
    FOR i = 0 TO 255
      k = i
      FOR j = 0 TO 7
        IF (k AND 1) THEN
          SHIFT RIGHT k, 1
          k XOR= &HEDB88320
        ELSE
          SHIFT RIGHT k, 1
        END IF
      NEXT j
      LUT(i) = k
    NEXT i
  END IF
 
  crc = &HFFFFFFFF
 
  FOR i = 0 TO NumBytes - 1
    k = (crc AND &HFF& XOR @p[i])
    SHIFT RIGHT crc, 8
    crc XOR= LUT(k)
  NEXT i
 
  FUNCTION = NOT crc
END FUNCTION
 
' ***********
 
FUNCTION PBMAIN () AS LONG
  LOCAL s AS STRING
  LOCAL crc AS DWORD
 
  s = "The quick brown fox jumps over the lazy dog"
  CON.PRINT "Text:  " & s
  crc = CRC32(STRPTR(s), LEN(s))
  CON.PRINT "CRC32: " & HEX$(crc)
END FUNCTION