

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


with javascript_semantics
sequence table
bool have_table = false
 
function crc32(string s)
    if not have_table then
        have_table = true
        table = repeat(0,256)
        for i=0 to 255 do
            atom rem = i
            for j=1 to 8 do
                if and_bits(rem,1) then
                    rem = xor_bits(floor(rem/2),#EDB88320)
                else
                    rem = floor(rem/2)
                end if
                if rem<0 then
                    rem += #100000000
                end if
            end for
            table[i+1] = rem
        end for
    end if
    atom crc = #FFFFFFFF
    for i=1 to length(s) do
        crc = xor_bits(floor(crc/#100),table[xor_bits(and_bits(crc,0xff),s[i])+1])
        if crc<0 then
            crc += #100000000
        end if
    end for
    return and_bits(not_bits(crc),#FFFFFFFF)
end function

string s = "The quick brown fox jumps over the lazy dog"
printf(1,"The CRC of %s is %08x\n",{s,crc32(s)})
