Imports System.IO
''' <summary>
''' Diese Klasse verarbeitet SFV Dateien.
''' Dabei wird eine SFV Datei geöffnet und es werden von allen aufgelisteten Dateien
''' CRC32 Summen erstellt und diese werden dann mit den Summen aus der SFV Datei vergleichen.
''' Bei Fehlenden, Kaputten und Sauberen Dateien wird jeweils das dazugehörige Event aufgerufen.
''' SFV Dateien müssen das Standardformat besitzen: "Dateiname.dat ABCD1234" wobei der CRC32 Hash
''' in der SFV Datei immer 8 Zeichen lang sein muss. In einigen Fällen hat ein CRC32 Hash nur 7 Zeichen,
''' dann sollte am Anfang des Hashes eine 0 angehängt werden also "0ABC1234" statt "ABC1234".
''' </summary>
''' <remarks>Geschrieben von Tim Hartwig (Januar 08)</remarks>
Public Class SimpleFileVerification
Private mSFVFile As String
''' <summary>
''' Tritt ein wenn eine Datei in der SFV Liste aufgeführt ist, aber nicht vorhanden ist.
''' </summary>
Public Event FileIsMissing(ByVal sender As Object, ByVal e As SFVEventArgs)
''' <summary>
''' Tritt ein wenn die Datei nicht die vorgegebene Prüfsumme aus der SFV Datei hat.
''' </summary>
Public Event FileIsCorrupted(ByVal sender As Object, ByVal e As SFVEventArgs)
''' <summary>
''' Tritt immer ein wenn eine Datei in Ordnung ist.
''' </summary>
Public Event FileIsClean(ByVal sender As Object, ByVal e As SFVEventArgs)
''' <summary>
''' Tritt ein wenn die überprüfung der aktuellen SFV Datei fertiggestellt ist.
''' </summary>
Public Event Finished(ByVal sender As Object)
''' <summary>
''' Die SFV Datei in der die Prüfdaten drin stehen.
''' </summary>
Public Property SFVFile() As String
Get
Return mSFVFile
End Get
Set(ByVal value As String)
mSFVFile = value
End Set
End Property
Sub New()
CreateCRC32Table()
End Sub
Sub New(ByVal nSFVFile As String)
mSFVFile = nSFVFile
CreateCRC32Table()
End Sub
''' <summary>
''' Beginnt die prüfung der SFV Datei.
''' </summary>
Public Sub Start()
Dim Data() As String = File.ReadAllLines(mSFVFile)
Dim CurrExpectedHash As String = ""
Dim CurrCalculatedHash As String = ""
Dim CurrFileNameToCheck As String = ""
Dim SFVFilePath As String = Path.GetDirectoryName(mSFVFile) & "\"
For i As Integer = 0 To Data.Length - 1
If Data(i) IsNot String.Empty And Not Data(i).StartsWith(";") Then
CurrFileNameToCheck = SFVFilePath & Data(i).Substring(0, Data(i).Length - 9)
If File.Exists(CurrFileNameToCheck) = False Then
RaiseEvent FileIsMissing(Me, New SFVEventArgs(CurrFileNameToCheck, "", ""))
Else
CurrExpectedHash = Data(i).Substring(Data(i).Length - 8, 8).ToUpper.Replace(" ", "")
CurrCalculatedHash = ProcessFile(CurrFileNameToCheck).ToUpper
If CurrCalculatedHash.Length <> 8 Then CurrCalculatedHash = "0" & CurrCalculatedHash
If CurrExpectedHash = CurrCalculatedHash Then
RaiseEvent FileIsClean(Me, New SFVEventArgs(CurrFileNameToCheck, CurrExpectedHash, CurrCalculatedHash))
Else
RaiseEvent FileIsCorrupted(Me, New SFVEventArgs(CurrFileNameToCheck, CurrExpectedHash, CurrCalculatedHash))
End If
End If
End If
Next
RaiseEvent Finished(Me)
End Sub
#Region "CRC32"
Private CRC32Table() As Integer
Private Const CRC32Polynomial As Integer = &HEDB88320
Public Function ProcessFile(ByVal FileName As String) As String
Dim FS As FileStream = New FileStream(FileName, FileMode.Open, _
FileAccess.Read, FileShare.Read, 1048576)
Dim CRC32Result As Integer = &HFFFFFFFF
Dim Buffer(4096) As Byte
Dim ReadSize As Integer = 4096
Dim Count As Integer = FS.Read(Buffer, 0, ReadSize)
Dim Lookup As Integer
Do While (Count > 0)
For i As Integer = 0 To Count - 1
Lookup = (CRC32Result And &HFF) Xor Buffer(i)
CRC32Result = ((CRC32Result And &HFFFFFF00) \ &H100) And &HFFFFFF
CRC32Result = CRC32Result Xor CRC32Table(Lookup)
Next i
Count = FS.Read(Buffer, 0, ReadSize)
Loop
Return Hex(Not (CRC32Result)).ToString
End Function
Private Sub CreateCRC32Table()
Dim DWCRC As Integer
ReDim CRC32Table(256)
For i As Integer = 0 To 255
DWCRC = i
For n As Integer = 8 To 1 Step -1
If (DWCRC And 1) Then
DWCRC = ((DWCRC And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
DWCRC = DWCRC Xor CRC32Polynomial
Else
DWCRC = ((DWCRC And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
End If
Next
CRC32Table(i) = DWCRC
Next i
End Sub
#End Region
End Class
Public Class SFVEventArgs
Private mFileName As String
Private mExpectedHash As String
Private mCalculatedHash As String
Public Property FileName() As String
Get
Return mFileName
End Get
Set(ByVal value As String)
mFileName = value
End Set
End Property
Public Property ExpectedHash() As String
Get
Return mExpectedHash
End Get
Set(ByVal value As String)
mExpectedHash = value
End Set
End Property
Public Property CalculatedHash() As String
Get
Return mCalculatedHash
End Get
Set(ByVal value As String)
mCalculatedHash = value
End Set
End Property
Sub New(ByVal nFileName As String, ByVal nExpectedHash As String, ByVal nCalculatedHash As String)
mFileName = nFileName
mExpectedHash = nExpectedHash
mCalculatedHash = nCalculatedHash
End Sub
End Class