CRC32 check with bmx?

BlitzMax Forums/BlitzMax Beginners Area/CRC32 check with bmx?

Hi!

Has someone written funcs in bmx that check if the checksum of a file is correct?

They need not be über fast, so plain stuff would do fine.
I looked up the archives but couldn't port the code there over to bmx.

The code from the archive seems to need little modification??
Global crc_table[256]
crc_init()

Print Hex$(crc_string("ABC"))

End


Function crc_init()
  Local i
  Local j
  Local value

  For i=0 To 255
    value=i
    For j=0 To 7
      If (value & $1) Then 
        value=(value Shr 1) ~ $EDB88320
      Else
        value=(value Shr 1)
      EndIf
    Next
    crc_table[i]=value
  Next
End Function

Function crc_string(txt$)
  Local bbyte
  Local crc
  Local i
  Local size

  crc=$FFFFFFFF
  size=Len(txt$)
  For i=1 To size
    bbyte=Asc(Mid$(txt$,i,1))
    crc=(crc Shr 8) ~ crc_table[bbyte ~ (crc & $FF)]
  Next
  Return ~crc
End Function

Function crc_bank(bank)
  Local bbyte
  Local crc
  Local i
  Local size

  crc=$FFFFFFFF
  size=BankSize(bank)-1
  For i=0 To size
    bbyte=PeekByte(bank,i)
    crc=(crc Shr 8) ~ crc_table[bbyte ~ (crc & $FF)]
  Next
  Return ~crc
End Function

Function crc_file(name$)
  Local bbyte
  Local crc
  Local file:TStream

  crc=$FFFFFFFF
  file=ReadFile(name$)
  If file=Null Then Return
  While Not Eof(file)
    bbyte=ReadByte(file)
    crc=(crc Shr 8) ~ crc_table[bbyte ~ (crc & $FF)]
  Wend
  CloseFile file
  Return ~crc
End Function

NOTE: I've only changed things enough to get it running, I haven't fully maxerised the code.

At least it works now. Thanks!

Its just aweful slow when you "test" some bigger files.
Might be the readbyte command.

Function crc_file(name$)
  Local crc,size,i
  size=FileSize(name$)
  Local bbyte:Byte[size]

  crc=$FFFFFFFF
  bbyte=LoadByteArray(name$)
  For i=0 To size  
    crc=(crc Shr 8) ~ crc_table[bbyte[i] ~ (crc & $FF)]
  Next
  bbyte=Null
  FlushMem()
  Return ~crc
End Function


Done a speed up for file checking!