CRC32

Share your advanced PureBasic knowledge/code with the community.
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

CRC32

Post by Dr. Dri »

Code updated For 5.20+

no CRC32FileFingerprint in PB so i made one

Code: Select all

    Structure Bytes
      b.b[0]
    EndStructure

    #CRC32_BufferLength = 2048

    Global Dim    CRC32_Table($FF)
    Global CRC32_TableComputed

    Procedure CRC32_MakeTable()
      Protected i, j, crc
     
      For i = 0 To $FF
        crc = i
       
        For j = 1 To 8
       
          If crc & 1
            crc = $EDB88320 ! ((crc >> 1) & $7FFFFFFF)
          Else
            crc = (crc >> 1) & $7FFFFFFF
          EndIf
         
        Next j
       
        CRC32_Table(i) = crc
      Next i
     
      CRC_TableComputed = #True
    EndProcedure

    Procedure CRC32_Update(crc, *buffer.Bytes, Length)
      Protected i
     
      If CRC32_TableComputed = #False
        CRC32_MakeTable()
      EndIf
     
      While i < Length
        crc = CRC32_Table((crc ! *buffer\b[i]) & $FF) ! ((crc >> 8) & $00FFFFFF)
        i + 1
      Wend
     
      ProcedureReturn crc
    EndProcedure

    Procedure CRC32_Fingerprint(*buffer, Length)
      ProcedureReturn ~CRC32_Update($FFFFFFFF, *buffer, Length)
    EndProcedure

    Procedure CRC32_FileFingerprint(Filename.s)
      Protected crc, File, Buffer, Offset, Length
     
      File = ReadFile(#PB_Any, Filename)
      If File
        Buffer = AllocateMemory(#CRC32_BufferLength)
       
        If Buffer
          Length = Lof(File)
          crc = $FFFFFFFF
         
          While (Length - Offset) > #CRC32_BufferLength
            ReadData(File,Buffer, #CRC32_BufferLength)
            crc = CRC32_Update(crc, Buffer, #CRC32_BufferLength)
            Offset + #CRC32_BufferLength
          Wend
         
          If Length > Offset
            Length - Offset
            ReadData(File,Buffer, Length)
            crc = CRC32_Update(crc, Buffer, Length)
          EndIf
         
          FreeMemory(Buffer)
        EndIf
       
        CloseFile(File)
      EndIf
     
      ProcedureReturn ~crc
    EndProcedure
Dri ;)
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Test with a 15Mb file :

Your CRC32 function : 5428ms
Native PureBasic MD5 function : 390ms

Winner : Purebasic
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Droopy wrote:Test with a 15Mb file :

Your CRC32 function : 5428ms
Native PureBasic MD5 function : 390ms

Winner : Purebasic
PureBasic is always faster than me... It's one of the reasons why i like it :P
Whatever, i'm not talking about md5 but CRC32FileFingerprint which doesn't exists.

Dri
okasvi
Enthusiast
Enthusiast
Posts: 150
Joined: Wed Apr 27, 2005 9:41 pm
Location: Finland

Post by okasvi »

Droopy is right but the thing is that there is no CRC32FileFingerPrint in PB... of course it is preferred for ppl to use MD5 on their own progs if it is that much faster but sometimes you cant choose what to use as checksum of the file if the file isnt made by you etc...
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post by Dr. Dri »

Did you turn the debugger off ?
It takes 7 seconds for a 80 Mb file for me
(and 966 ms for a 15Mb file)

Code: Select all

time  = ElapsedMilliseconds()
crc.s = Hex(CRC32_FileFingerprint("your file"))
time  = ElapsedMilliseconds() - time
MessageRequester(crc, Str(time), #MB_ICONINFORMATION)
Dri :?
Post Reply