October 28, 2020, 11:31:32 PM

Author Topic: [bb] CRC32 - Checksum calculator by MrCredo [ 1+ years ago ]  (Read 1686 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : CRC32 - Checksum calculator
Author : MrCredo
Posted : 1+ years ago

Description : here is clean and fast code for CRC32-checksum-calculating. Use it with string, bank or file...

Code :
Code: BlitzBasic
  1. Dim crc_table(255)
  2. crc_init()
  3.  
  4. Print Hex$(crc_string("ABC"))
  5. Input
  6.  
  7.  
  8.  
  9.  
  10.  
  11. Function crc_init()
  12.   Local i
  13.   Local j
  14.   Local value
  15.  
  16.   For i=0 To 255
  17.     value=i
  18.     For j=0 To 7
  19.       If (value And $1) Then
  20.         value=(value Shr 1) Xor $EDB88320
  21.       Else
  22.         value=(value Shr 1)
  23.       EndIf
  24.     Next
  25.     crc_table(i)=value
  26.   Next
  27. End Function
  28.  
  29.  
  30.  
  31.  
  32.  
  33. Function crc_string(txt$)
  34.   Local byte
  35.   Local crc
  36.   Local i
  37.   Local size
  38.  
  39.   crc=$FFFFFFFF
  40.   size=Len(txt$)
  41.   For i=1 To size
  42.     byte=Asc(Mid$(txt$,i,1))
  43.     crc=(crc Shr 8) Xor crc_table(byte Xor (crc And $FF))
  44.   Next
  45.   Return ~crc
  46. End Function
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53. Function crc_bank(bank)
  54.   Local byte
  55.   Local crc
  56.   Local i
  57.   Local size
  58.  
  59.   crc=$FFFFFFFF
  60.   size=BankSize(bank)-1
  61.   For i=0 To size
  62.     byte=PeekByte(bank,i)
  63.     crc=(crc Shr 8) Xor crc_table(byte Xor (crc And $FF))
  64.   Next
  65.   Return ~crc
  66. End Function
  67.  
  68.  
  69.  
  70.  
  71.  
  72. Function crc_file(name$)
  73.   Local byte
  74.   Local crc
  75.   Local file
  76.  
  77.   crc=$FFFFFFFF
  78.   file=ReadFile(name$)
  79.   If file=0 Then Return
  80.   While Not Eof(file)
  81.     byte=ReadByte(file)
  82.     crc=(crc Shr 8) Xor crc_table(byte Xor (crc And $FF))
  83.   Wend
  84.   Return ~crc
  85. End Function


Comments :


Dip(Posted 1+ years ago)

 Just a note in case someone else uses this code:Add a CloseFile file before the return in function crc_file, or you will be pulling your hair out when it suddenly stops working.


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal