

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


 0 DIM D$(1111):D$(0)="0":D$(1)="1":D$(10)="2":D$(11)="3":D$(100)="4":D$(101)="5":D$(110)="6":D$(111)="7":D$(1000)="8":D$(1001)="9":D$(1010)="A":D$(1011)="B":D$(1100)="C":D$(1101)="D":D$(1110)="E":D$(1111)="F"
 1 Z$ =  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8) +  CHR$ (8)
 100 C$ = "00000000000000000000000000000000"
 110 S$ = "The quick brown fox jumps over the lazy dog"
 120  GOSUB 200"CRC32
 130  PRINT D$( VAL ( MID$ (C$,1,4)))D$( VAL ( MID$ (C$,5,4)))D$( VAL ( MID$ (C$,9,4)))D$( VAL ( MID$ (C$,13,4)))D$( VAL ( MID$ (C$,17,4)))D$( VAL ( MID$ (C$,21,4)))D$( VAL ( MID$ (C$,25,4)))D$( VAL ( MID$ (C$,29,4)))"  ";
 140  END 
 200  IF  LEN (S$) = 0 THEN  RETURN 
 210  GOSUB 280"XOR #$FFFFFFFF
 220  FOR I = 1 TO  LEN (S$)
 230 R$ = "00000000" +  MID$ (C$,1,24)
 235  PRINT D$( VAL ( MID$ (C$,1,4)))D$( VAL ( MID$ (C$,5,4)))D$( VAL ( MID$ (C$,9,4)))D$( VAL ( MID$ (C$,13,4)));
 236  PRINT D$( VAL ( MID$ (C$,17,4)))D$( VAL ( MID$ (C$,21,4)))D$( VAL ( MID$ (C$,25,4)))D$( VAL ( MID$ (C$,29,4)))" " MID$ (S$,I,1)Z$;
 240 C =  ASC ( MID$ (S$,I,1)):O$ = "": FOR B = 1 TO 8:K =  INT (C / 2):O$ =  STR$ (C - K * 2) + O$:C = K: NEXT B
 250 A = ( MID$ (C$,25,1) <  >  MID$ (O$,1,1)) * 128 + ( MID$ (C$,26,1) <  >  MID$ (O$,2,1)) * 64 + ( MID$ (C$,27,1) <  >  MID$ (O$,3,1)) * 32 + ( MID$ (C$,28,1) <  >  MID$ (O$,4,1)) * 16
 251 A = ( MID$ (C$,29,1) <  >  MID$ (O$,5,1)) * 8 + ( MID$ (C$,30,1) <  >  MID$ (O$,6,1)) * 4 + ( MID$ (C$,31,1) <  >  MID$ (O$,7,1)) * 2 + ( MID$ (C$,32,1) <  >  MID$ (O$,8,1)) + A: GOSUB 300
 260 C$ =  STR$ (( MID$ (R$,1,1) <  >  MID$ (T$,1,1))) +  STR$ (( MID$ (R$,2,1) <  >  MID$ (T$,2,1))) +  STR$ (( MID$ (R$,3,1) <  >  MID$ (T$,3,1))) +  STR$ (( MID$ (R$,4,1) <  >  MID$ (T$,4,1)))
 261 C$ = C$ +  STR$ (( MID$ (R$,5,1) <  >  MID$ (T$,5,1))) +  STR$ (( MID$ (R$,6,1) <  >  MID$ (T$,6,1))) +  STR$ (( MID$ (R$,7,1) <  >  MID$ (T$,7,1))) +  STR$ (( MID$ (R$,8,1) <  >  MID$ (T$,8,1)))
 262 C$ = C$ +  STR$ (( MID$ (R$,9,1) <  >  MID$ (T$,9,1))) +  STR$ (( MID$ (R$,10,1) <  >  MID$ (T$,10,1))) +  STR$ (( MID$ (R$,11,1) <  >  MID$ (T$,11,1))) +  STR$ (( MID$ (R$,12,1) <  >  MID$ (T$,12,1)))
 263 C$ = C$ +  STR$ (( MID$ (R$,13,1) <  >  MID$ (T$,13,1))) +  STR$ (( MID$ (R$,14,1) <  >  MID$ (T$,14,1))) +  STR$ (( MID$ (R$,15,1) <  >  MID$ (T$,15,1))) +  STR$ (( MID$ (R$,16,1) <  >  MID$ (T$,16,1)))
 264 C$ = C$ +  STR$ (( MID$ (R$,17,1) <  >  MID$ (T$,17,1))) +  STR$ (( MID$ (R$,18,1) <  >  MID$ (T$,18,1))) +  STR$ (( MID$ (R$,19,1) <  >  MID$ (T$,19,1))) +  STR$ (( MID$ (R$,20,1) <  >  MID$ (T$,20,1)))
 265 C$ = C$ +  STR$ (( MID$ (R$,21,1) <  >  MID$ (T$,21,1))) +  STR$ (( MID$ (R$,22,1) <  >  MID$ (T$,22,1))) +  STR$ (( MID$ (R$,23,1) <  >  MID$ (T$,23,1))) +  STR$ (( MID$ (R$,24,1) <  >  MID$ (T$,24,1)))
 266 C$ = C$ +  STR$ (( MID$ (R$,25,1) <  >  MID$ (T$,25,1))) +  STR$ (( MID$ (R$,26,1) <  >  MID$ (T$,26,1))) +  STR$ (( MID$ (R$,27,1) <  >  MID$ (T$,27,1))) +  STR$ (( MID$ (R$,28,1) <  >  MID$ (T$,28,1)))
 267 C$ = C$ +  STR$ (( MID$ (R$,29,1) <  >  MID$ (T$,29,1))) +  STR$ (( MID$ (R$,30,1) <  >  MID$ (T$,30,1))) +  STR$ (( MID$ (R$,31,1) <  >  MID$ (T$,31,1))) +  STR$ (( MID$ (R$,32,1) <  >  MID$ (T$,32,1)))
 270  NEXT I
 280 B$ = "": FOR B = 1 TO 32:B$ = B$ +  STR$ (( MID$ (C$,B,1) <  > "1")): NEXT B:C$ = B$
 290  RETURN 
 300  IF  NOT T THEN  DIM T$(255): FOR T = 0 TO 38: READ J: READ T$(J): NEXT T
 310  IF  LEN (T$(A)) THEN T$ = T$(A): RETURN 
 320 R = A:T$ = "": FOR B = 1 TO 8:N =  INT (R / 2):T$ =  MID$ ("01",R - N * 2 + 1,1) + T$:R = N: NEXT B:T$ = "000000000000000000000000" + T$
 330  FOR J = 0 TO 7
 340 X =  VAL ( MID$ (T$,32,1))
 350 T$ = "0" +  MID$ (T$,1,31)
 360  IF X THEN B$ = "": FOR B = 1 TO 32:B$ = B$ +  MID$ ("01",( MID$ (T$,B,1) <  >  MID$ ("11101101101110001000001100100000",B,1)) + 1,1): NEXT B:T$ = B$
 370  NEXT J
 380 T$(A) = T$:T = T + 1
 390  RETURN 
 600  DATA171,01000001000001000111101001100000
 610  DATA247,00100011110110010110011110111111
 620  DATA95,11111011110101000100110001100101
 630  DATA217,11111111000011110110101001110000
 640  DATA213,11110110101110010010011001011011
 650  DATA179,01010010011010001110001000110110
 660  DATA141,10010011000010011111111110011101
 670  DATA90,10001011101111101011100011101010
 680  DATA224,10100000000010101110001001111000
 690  DATA187,01011100101100110110101000000100
 700  DATA169,10101111000010100001101101001100
 710  DATA60,00101111011011110111110010000111
 720  DATA128,11101101101110001000001100100000
 730  DATA36,00111100000000111110010011010001
 740  DATA235,00110111110110000011101111110000
 750  DATA229,11010000011000000001011011110111
 760  DATA77,00001000011011010011110100101101
 770  DATA167,01001000101100100011011001001011
 780  DATA1,01110111000001110011000010010110
 790  DATA119,11001110011000011110010010011111
 800  DATA96,01001101101100100110000101011000
 810  DATA158,00010111101101111011111001000011
 820  DATA68,01110001101100011000010110001001
 830  DATA56,00101000000000101011100010011110
 840  DATA193,11101100011000111111001000100110
 850  DATA87,11110101000011111100010001010111
 860  DATA160,11010110110101101010001111101000
 870  DATA2,11101110000011100110000100101100
 880  DATA30,11111010000011110011110101100011
 890  DATA7,10011110011001001001010110100011
 900  DATA26,11111101011000101111100101111010
 910  DATA85,00011011000000011010010101111011
 920  DATA15,10010000101111110001110110010001
 930  DATA201,11100010101110000111101000010100
 940  DATA188,11000010110101111111111110100111
 950  DATA0,00000000000000000000000000000000
 960  DATA238,01000111101100101100111101111111
 970  DATA181,10111011000010110100011100000011
 980  DATA114,10111110000010110001000000010000