Ooops
March 05, 2021, 06:10:35 AM

Author Topic: [bb] Recursive CRC-32 file builder by ozak [ 1+ years ago ]  (Read 592 times)

Offline BlitzBot

Title : Recursive CRC-32 file builder
Author : ozak
Posted : 1+ years ago

Description : Takes a directory and recursively calculates crc-32 for all files and stores it in a file.
Great for online game patchers. Simply use the tool to build a list, and your patcher can download patchlist.txt and see if the CRC of any game files have changed and if so, download the new one.

NOTE: It's hardcoded to the path the exe file is in, and it skips patchlist.txt and buildcrc.exe due to obvious reasons :)


Code :
Code: BlitzBasic
  1. ; Recursive directory list CRC-32 builder by Odin Jensen (www.furi.dk)
  2.  
  3. ; Setup CRC stuff
  4. Dim CRC32_table(255)
  5. CRC32_init()
  6.  
  7. Global outfile = WriteFile("patchinfo.dat")
  8. Print("CRC-32 list builder by Odin Jensen")
  9. Print("----------------------------------")
  10. Print("")
  11.  
  12. BuildList("./")
  13.  
  14. CloseFile(outfile)
  15.  
  16. Print("")
  17. Print("Done.")
  18.  
  19. ; Build list function
  20. Function BuildList(From_$)
  21.   Local MyDir, MyFile$
  22.  
  23.   MyDir = ReadDir(From_$)
  24.   MyFile$ = NextFile$(MyDir)
  25.   MyFile$ = NextFile$(MyDir)
  26.   Repeat
  27.     MyFile$ = NextFile$(MyDir)
  28.     If MyFile$ = "" Then Exit
  29.  
  30.     If FileType(From_$ + "" + MyFile$) = 2 Then
  31.       BuildList(From_$ + "" + MyFile$)
  32.     Else
  33.           temp$ = Replace(From_$ + "" + MyFile$, "./", "")
  34.  
  35.           If ((Not Right(temp, 13) = "patchinfo.dat") And (Not Right(temp, 12) = "buildcrc.exe"))
  36.          
  37.                   crc32% = CRC32_FromFile(temp)
  38.               WriteLine(outfile, temp + "," + Str(Hex(crc32)))
  39.                   Print("> CRC32 for file '" + temp + "' is " + Str(Hex(crc32)))
  40.           End If
  41.  
  42.     End If
  43.   Forever
  44.   CloseDir(MyDir)  
  45. End Function
  46.  
  47.  
  48. ; CRC init function
  49. Function CRC32_Init()
  50.   Local i
  51.   Local j
  52.   Local value
  53.  
  54.   For i=0 To 255
  55.     value=i
  56.     For j=0 To 7
  57.       If (value And $1) Then
  58.         value=(value Shr 1) Xor $EDB88320
  59.       Else
  60.         value=(value Shr 1)
  61.       EndIf
  62.     Next
  63.     CRC32_table(i)=value
  64.   Next
  65. End Function
  66.  
  67. ; Function to get CRC-32 value from a file
  68. Function CRC32_FromFile(name$)
  69.   Local byte
  70.   Local crc
  71.   Local file
  72.  
  73.   crc=$FFFFFFFF
  74.   file=ReadFile(name$)
  75.   If file=0 Then Return
  76.   While Not Eof(file)
  77.     byte=ReadByte(file)
  78.     crc=(crc Shr 8) Xor CRC32_table(byte Xor (crc And $FF))
  79.   Wend
  80.   Return ~crc
  81. End Function


Comments :


ckob(Posted 1+ years ago)

 it seems that after so many files the CRC starts spitting out all 0's


ckob(Posted 1+ years ago)

 Also here's this same thing working in Blitzmax
Code: [Select]


Global CRC32_table[256]
CRC32_init()

Global outfile
outfile = WriteFile("patchinfo.dat")

BuildList(dir$)
CloseFile outfile


' Build list Function
Function BuildList(From_$)
  Local MyDir, MyFile$

  MyDir = ReadDir(From_$)
  MyFile$ = NextFile$(MyDir)
  MyFile$ = NextFile$(MyDir)
Repeat

    MyFile$ = NextFile$(MyDir)

    If MyFile$ = "" Then Exit

    If FileType(From_$ + "" + MyFile$) = 2 Then
      BuildList(From_$ + "" + MyFile$)
    Else
 temp$ = Replace(From_$ + "" + MyFile$, "./", "")

 crc32% = CRC32_FromFile(temp)

 Print "> CRC32 for file '" + temp + "' is " + String(Hex(crc32))
     WriteLine outfile, temp + "," + String(Hex(crc32))

End If

If KeyHit(KEY_ESCAPE) Then
End
EndIf



Forever

  CloseDir(MyDir)
 

End Function









' CRC init Function
Function CRC32_Init()
  Local i
  Local j
  Local value

  For i=0 To 255
    value=i
    For j=0 To 7
      If (value And $1) Then
        value=(value Shr 1) ~ $EDB88320
      Else
        value=(value Shr 1)
      EndIf
    Next
    CRC32_table[i]=value
  Next
End Function

' Function To get CRC-32 value from a file
Function CRC32_FromFile(name$)
  Local Bytes
  Local crc
  Local file

  crc=$FFFFFFFF
  file=ReadFile(name$)
  If file=0 Then Return
  While Not Eof(file) Or KeyHit(1)
    Bytes=ReadByte(file)
    crc=(crc Shr 8) ~ CRC32_table(Bytes ~ (crc And $FF))
  Wend
  Return ~crc
End Function



 

SimplePortal 2.3.6 © 2008-2014, SimplePortal