Page 1 of 1

CRC32

Posted: Mon Dec 19, 2005 1:52 am
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 ;)

Posted: Mon Dec 19, 2005 9:58 am
by Droopy
Test with a 15Mb file :

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

Winner : Purebasic

Posted: Mon Dec 19, 2005 10:45 am
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

Posted: Mon Dec 19, 2005 10:46 am
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...

Posted: Mon Dec 19, 2005 10:54 am
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 :?