Page 1 of 1

Get the CRC32 of a file

Posted: Sat Nov 05, 2005 4:23 am
by Armoured
Code updated For 5.20+ (Same รก CRC32FileFingerprint())

this procedure get the CRC32 of a file.
Yes there are alternatives more fast but this solution doesn't require external libraries or asm code. :)

Code: Select all

Procedure.s GetFileCRC32(file$)
  Protected crc32$,length,*MemoryBuffer
  If (ReadFile(0,file$))
    length = Lof(0)
    If (length)
      *MemoryBuffer = AllocateMemory(length)
      If (*MemoryBuffer)
        If (ReadData(0,*MemoryBuffer,length))
          crc32$ = Hex(CRC32Fingerprint(*MemoryBuffer,length))
        EndIf
        FreeMemory(*MemoryBuffer)
      EndIf
    Else
      crc32$ = "0"
    EndIf
    CloseFile(0)
  EndIf
  ProcedureReturn crc32$
EndProcedure

path$ = OpenFileRequester("Select a file", "","Text (*.txt)|*.txt;*.bat|PureBasic (*.pb)|*.pb|All files (*.*)|*.*", 0)

Debug "File path: " + path$
Debug "CRC32: " + GetFileCRC32(path$)


Posted: Sat Nov 05, 2005 7:13 am
by Armoured
Hi
I have fixed a little bug with zero length files! :D

Posted: Sat Nov 05, 2005 10:14 am
by Droopy
Cool for small files only

Posted: Sat Nov 05, 2005 10:26 am
by blueznl
droopy, how do you mean? what do you call 'small' :-)

Posted: Sat Nov 05, 2005 10:42 am
by Blade
The file is loaded in memory at once.
Try with your latest 1.2Gb divX movie ;)

Posted: Sat Nov 05, 2005 12:43 pm
by utopiomania
Great! I searched the forums a few days back for checksum code, but this is exactly what I wanted.
Thanks for the contribution! :)

Posted: Sat Nov 05, 2005 1:46 pm
by Droopy
blueznl wrote:droopy, how do you mean? what do you call 'small' :-)

The file is loaded in memory, try using a 700Mb file with 256Mo Ram, you have allocatememory(700Mb) !

Posted: Sat Nov 05, 2005 4:26 pm
by Armoured
I can't make miracles Droopy! :D
There are procedures made in asm that have the same limits of my :)

Posted: Sat Nov 05, 2005 7:19 pm
by Dr. Dri
Droopy wrote:The file is loaded in memory, try using a 700Mb file with 256Mo Ram, you have allocatememory(700Mb) !
Windows may use a hard disk as extended RAM... very slow, i know

Dri

Posted: Sat Nov 05, 2005 7:27 pm
by Rings
i use filemapping in PBOSL
(comes with full PB-Source) to do it
(File-CRC32 and FileMD5 ).
When using Filemapping, Windows does
all the stuff for the Memory itself.
You don't have to do, Windows does it,
and in a proper and fast way .
So perhaps you don't fall in the
Idle-while-Swapping-Memory - trap .

But okay, try the Fifth wheel for your
car again ;)

Posted: Sat Nov 05, 2005 7:56 pm
by Droopy
Armoured wrote:I can't make miracles Droopy! :D
There are procedures made in asm that have the same limits of my :)
I know, it's just a remark :D

Posted: Mon Nov 07, 2005 12:49 am
by dagcrack
Just wondering, whats wrong with ASM anyway?, because of the "readability" for people who isnt "inside" ASM?, I'd go with ASM if you ask me.

Posted: Mon Nov 07, 2005 3:14 pm
by Armoured
Just wondering, whats wrong with ASM anyway?, because of the "readability" for people who isnt "inside" ASM?, I'd go with ASM if you ask me.
Ok don't use it! :D

If the limits are the same i prefer an high level language.
I use this code for checking the CRC32 of a file with a length of 142Kb for this reason I don't have problems with virtual memory and I can leave Assembler for other important sections. This is the perfect solution of my problem.

P.S.
If you have one better solution post it! The "Tricks 'n' Tips" section of the forum is here for this reason. :wink: