Re: Checking CRC?? [URJENT!!]



On 6 Oct 2005 22:26:55 -0700, smart.bug@xxxxxxxxx wrote:

> I want to write my own routine to calculate the CRC of any given file.
> Does anybody knows the algorithm or where to find it ?

I was looking for the same thing once. While Google gave me a lot of hits,
none of them would do what I wanted to, which is basicly what you want to
do. I ended up hacking apart a VB6 class for generating CRCs from strings
so it would make a CRC from a file and turned it into a VB5/6 compatible
module, no need to mess around with classes (which to tell you the truth I
still don't understand any of, even after 5 years of VB programming).

Anyway here it is, if anybody recognises some if the code please say who
wrote the original as I would credit it to him now but I long since deleted
the original source after I got this working but I left his comments in
place. Also the code generates a 32 bit CRC number.

To use just dump the following code into a empty module and then call
InitCrc32 at the start of your app. To get a CRC from a file just do a
'dim myCRC as long : myCRC = GetCRC32("some file")' or whatever you think
suits best. The GetChunk constant controls how big a piece of a file to
read at a time. Probably best if its a multiple of 1024 (1KB).

When used in a loop this code can process a CDR's worth of JPEGS (various
sizes) in about 4:30 running in the VB5 IDE on my Athlon XP3000+ powered
WinME comp.


Private Crc32Table(255) As Long
Const GetChunk As Long = 8192


'// Then all we have to do is writing public functions like these...
Sub InitCrc32()

'// Declare counter variable iBytes, counter variable iBits, value
variables lCrc32 and lTempCrc32
Dim iBytes As Integer
Dim iBits As Integer
Dim lCrc32 As Long
Dim lTempCrc32 As Long
Dim Seed As Long
Seed = &HEDB88320
'// Turn on error trapping
'On Error Resume Next

'// Iterate 256 times
For iBytes = 0 To 255

'// Initiate lCrc32 to counter variable
lCrc32 = iBytes

'// Now iterate through each bit in counter byte
For iBits = 0 To 7
'// Right shift unsigned long 1 bit
lTempCrc32 = lCrc32 And &HFFFFFFFE
lTempCrc32 = lTempCrc32 \ &H2
lTempCrc32 = lTempCrc32 And &H7FFFFFFF

'// Now check if temporary is less than zero and then mix Crc32
checksum with Seed value
If (lCrc32 And &H1) <> 0 Then
lCrc32 = lTempCrc32 Xor Seed
Else
lCrc32 = lTempCrc32
End If
Next

'// Put Crc32 checksum value in the holding array
Crc32Table(iBytes) = lCrc32
Next


End Sub

'// The function above is the initializing function, now we have to write
the computation function
Public Function GetCrc32(ByVal FileCRC As String) As Long

'// Declare following variables
Dim iCounter As Integer, lIndex As Long
Dim lAccValue As Long, lTableValue As Long
Dim Crc32 As Long
Crc32 = &HFFFFFFFF
'// Turn on error trapping
'On Error Resume Next
Dim wfsiu As Integer
wfsiu = FreeFile
Open FileCRC For Binary Access Read As wfsiu
Seek wfsiu, 1
ReDim CheckArr(GetChunk - 1) As Byte
CheckRem = LOF(wfsiu) Mod GetChunk
For q = 1 To LOF(wfsiu) \ GetChunk
Get wfsiu, , CheckArr()
For w = 0 To GetChunk - 1
lAccValue = Crc32 And &HFFFFFF00
lAccValue = lAccValue \ &H100
lAccValue = lAccValue And &HFFFFFF
lIndex = Crc32 And &HFF
lIndex = lIndex Xor CheckArr(w)
lTableValue = Crc32Table(lIndex)
Crc32 = lAccValue Xor lTableValue
Next w
Next q
If CheckRem > 0 Then
ReDim CheckArr(CheckRem - 1) As Byte
Get wfsiu, , CheckArr()
For w = 1 To CheckRem - 1
lAccValue = Crc32 And &HFFFFFF00
lAccValue = lAccValue \ &H100
lAccValue = lAccValue And &HFFFFFF
lIndex = Crc32 And &HFF
lIndex = lIndex Xor CheckArr(w)
lTableValue = Crc32Table(lIndex)
Crc32 = lAccValue Xor lTableValue
Next w
End If

Close wfsiu

'// Set function value the the new Crc32 checksum
GetCrc32 = Crc32 Xor &HFFFFFFFF
End Function


PS. Anybody here know of a better way? I bet it could be done faster.

--
"Star War - The third gathers - The backstroke of the west"
Please change 'no.spam' to 'jcomcp.plus' to reply.
----- http://jcom.shorturl.com ----
.