

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


' version 18-03-2017
' compile with: fbc -s console
 
Function crc32(buf As String) As UInteger<32>
 
    Static As UInteger<32> table(256)
    Static As UInteger<32> have_table
    Dim As UInteger<32> crc, k
    Dim As ULong i, j
 
    If have_table = 0 Then
        For i = 0 To 255
            k = i
            For j = 0 To 7
                If (k And 1) Then
                    k Shr= 1
                    k Xor= &Hedb88320
                Else
                    k Shr= 1
                End If
                table(i) = k
            Next
        Next
        have_table = 1
    End If
 
    crc = Not crc ' crc = &Hffffffff
 
    For i = 0 To Len(buf) -1
        crc = (crc Shr 8) Xor table((crc And &hff) Xor buf[i])
    Next
 
    Return Not crc
 
End Function
 
' ------=< MAIN >=------
 
Dim As String l = "The quick brown fox jumps over the lazy dog"
Dim As UInteger<32> crc
 
Print "input = "; l
print
Print "The CRC-32 checksum = "; Hex(crc32(l), 8)
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End