January 15, 2021, 05:17:23 PM

Author Topic: [bmx] CRC32 checksum calculation by Jake L. [ 1+ years ago ]  (Read 721 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : CRC32 checksum calculation
Author : Jake L.
Posted : 1+ years ago

Description : Version 1.1: Added buffered streams from Jeremy's code.

Thanks to skidracer and Noel for docmods-support!


Code :
Code: BlitzMax
  1. superstrict
  2.  
  3. rem
  4. bbdoc: CRC32 Checksum
  5. endrem
  6. module bt.crc32
  7. moduleinfo "CRC32-Checksum calculation"
  8. moduleinfo "ported to BMax by Jake@bittrap-software.de"
  9. moduleinfo "Original code (BB) written by MrCredo"
  10. moduleinfo "VERSION: 1.1"
  11. moduleinfo "OS: Win32 / MacOS / Linux"
  12. moduleinfo "HISTORY: 1.1 Changed Stream2CRC to buffered version (Credits: JeremytheJJ)"
  13.  
  14. import brl.stream
  15.  
  16. private
  17. global table%[256]
  18.  
  19. local val%
  20.         for local i%=0 to 255
  21.                 val=i
  22.                 for local j%=0 to 7
  23.                         if (val & $1)
  24.                                 val=(val shr 1) ~ $EDB88320
  25.                         else
  26.                                 val=(val shr 1)
  27.                         End If
  28.                 Next
  29.                 table[i]=val
  30.         Next
  31.  
  32.        
  33. public
  34.  
  35. rem
  36. bbdoc:create checksum from String
  37. returns: (Int) CRC32
  38. endrem 
  39. Function String2CRC32%(s$)
  40.         local crc%=$FFFFFFFF
  41.         for local i%=0 to s.length-1
  42.                 crc=(crc shr 8) ~ table[s[i] ~ (crc and $FF)]
  43.         next
  44.         return (crc)
  45. End Function
  46.  
  47. rem
  48. bbdoc:create checksum from an open stream
  49. returns: (Int) CRC32
  50. endrem 
  51. Function Stream2CRC32%(stream:TStream,bufferSize:Int=$400000)
  52.         local crc%=$FFFFFFFF
  53.         Local buffPtr:Byte Ptr = MemAlloc(bufferSize)
  54.        
  55.         Repeat
  56.         Local bytesRead:Int = stream.Read(buffPtr, bufferSize)
  57.         For Local b%=0 Until bytesRead
  58.                 crc = (crc Shr 8) ~ table[buffPtr[b] ~ (crc & $FF)]
  59.         Next
  60.         Until stream.Eof()
  61.  
  62.     MemFree(buffPtr)
  63.         return (crc)
  64. End Function
  65.  
  66. rem
  67. bbdoc:create checksum from url/file
  68. returns: (Int) CRC32
  69. endrem 
  70. Function URL2CRC32% (url$)
  71.         local st:TStream=OpenStream (url,true,false)
  72.         local crc%=Stream2CRC32(st)
  73.         st.Close()
  74.         return (crc)
  75. End Function


Comments :


N(Posted 1+ years ago)

 Put this above the module bt.crc32 line:
Code: [Select]
Rem
bbdoc: CRC32 Checksum
EndRem



Yan(Posted 1+ years ago)

 I did a buffered stream version a while back...
Code: [Select]
'Adaptation of MrCredo's CRC code
Function CRCFile(FileName:String, bufferSize:Int=$400000) 'default bufferSize = 4MB
Local crc = $FFFFFFFF
Global CRCTable[]

If CRCTable = Null
CRCTable = New Int[256]

 For Local i=0 To 255
   Local value = i
   For Local j=0 To 7
     If value & $1
       value = (value Shr 1) ~ $EDB88320
     Else
       value = value Shr 1
     EndIf
   Next
   CRCTable[i] = value
 Next
EndIf

  Local fileStream:TStream = ReadStream(fileName$)
  If fileStream = Null Then Return
 
Local buffPtr:Byte Ptr = MemAlloc(bufferSize)

Repeat
Local bytesRead = fileStream.Read(buffPtr, bufferSize)

For Local b=0 Until bytesRead
crc = (crc Shr 8) ~ CRCTable[buffPtr[b] ~ (crc & $FF)]
Next
Until Eof(FileStream)

CloseStream FileStream
MemFree(buffPtr)

Return ~crc
End Function
Feel free to add it, if you want.


WildStorm(Posted 1+ years ago)

 you forgot to add the type identifiersi removed also the ~ @ return ~crc ... it returned a negative integer??hope i didn't add any bugs
Code: [Select]
'Adaptation of MrCredo's CRC code
'Edited by Wild-Storm
Function CRCFile:Int(FileName:String, bufferSize:Int=$1500000) 'default bufferSize = 4MB
  Local crc:Int = $FFFFFFFF
  Global CRCTable:Int[]
 
  If CRCTable = Null
    CRCTable = New Int[256]
 
    For Local i%=0 To 255
      Local value% = i
      For Local j%=0 To 7
        If value% & $1
          value = (value Shr 1) ~ $EDB88320
        Else
          value = value Shr 1
        EndIf
      Next
      CRCTable[i] = value
    Next
  EndIf
 
  Local fileStream:TStream = ReadStream(fileName$)
  If fileStream = Null Then Return -1
 
  Local buffPtr:Byte Ptr = MemAlloc(bufferSize)
 
  Repeat
    Local bytesRead:Int = fileStream.Read(buffPtr, bufferSize)
   
    For Local b%=0 Until bytesRead
      crc = (crc Shr 8) ~ CRCTable[buffPtr[b] ~ (crc & $FF)]
    Next
  Until FileStream.Eof()
 
  FileStream.Close()
  MemFree(buffPtr)
 
  Return crc
End Function



Yan(Posted 1+ years ago)

 <div class="quote"> you forgot to add the type identifiers </div>Erm...No I didn't...I use Strict mode (SuperStrict didn't even exist when this code was written).<div class="quote"> i removed also the ~ @ return ~crc ... it returned a negative integer?? </div>I think you'll find that my routine (well, MrCredo's) returns the correct CRC-32 value...
Code: [Select]
Print Hex$(CRCFile(RequestFile("File to check...", "All Files:*")))Try comparing the results from other CRC-32 generators.


Jake L.(Posted 1+ years ago)

 are there any issues yet? My/MrCredo's code should return correct crc32, as I checked the output against some "official" samples.


RifRaf(Posted 1+ years ago)

 How much faster is Bmax at this than B3D ?  I am using the b3d version, and on large files it can chug a bit.If its ALOT faster, is there any chance someone here can produce a dll from it? im primarily interested in the crcfile portion.


N(Posted 1+ years ago)

 Could probably do it yourself.  It's not terribly complex code.


RifRaf(Posted 1+ years ago)

 its not the above code, but creating the dll i need help with. I dont own blide ,i do own blitzmax for a few years now but havent so much as wrote hello world with it yet, and i dont have any other compilers that can produce a dll I was hoping it would be very fast and easy task for someone prepared for it.


Nate the Great(Posted 1+ years ago)

 you have bmax and all youve written is hello world?!?!??!?! wow.. anyway this is pretty useful stuff


TomToad(Posted 1+ years ago)

 I know this is pretty old, but there are a couple of bugs in this mod that will prevent it from returning correct crc32 values.  you need to change the Return (crc) to Return(~crc) in String2CRC32() and Stream2CRC32() functions.  No need to change it in URL2CRC32() as that function already calls Stream2CRC32().Also change the crc and $FF in the StringtoCRC32() function to crc & $FF. [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal