Get the CRC32 of a file

Share your advanced PureBasic knowledge/code with the community.
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Get the CRC32 of a file

Post 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$)

Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

Hi
I have fixed a little bug with zero length files! :D
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post by Droopy »

Cool for small files only
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

droopy, how do you mean? what do you call 'small' :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Blade
Enthusiast
Enthusiast
Posts: 362
Joined: Wed Aug 06, 2003 2:49 pm
Location: Venice - Italy, Japan when possible.
Contact:

Post by Blade »

The file is loaded in memory at once.
Try with your latest 1.2Gb divX movie ;)
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post 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! :)
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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) !
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post by Armoured »

I can't make miracles Droopy! :D
There are procedures made in asm that have the same limits of my :)
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post 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 ;)
SPAMINATOR NR.1
User avatar
Droopy
Enthusiast
Enthusiast
Posts: 658
Joined: Thu Sep 16, 2004 9:50 pm
Location: France
Contact:

Post 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
dagcrack
Addict
Addict
Posts: 1868
Joined: Sun Mar 07, 2004 8:47 am
Location: Argentina
Contact:

Post 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.
! Black holes are where God divided by zero !
My little blog!
(Not for the faint hearted!)
Armoured
Enthusiast
Enthusiast
Posts: 365
Joined: Mon Jan 26, 2004 11:39 am
Location: ITALY
Contact:

Post 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:
Post Reply