Results 1 to 1 of 1

Thread: Calculate File's CRC 32

Threaded View

  1. #1

    Thread Starter
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Calculate File's CRC 32

    The class:
    Code:
    Option Explicit On
    Option Strict On
    
    <System.Diagnostics.DebuggerStepThrough()> _
    Friend Class CRC32
    
        ' This is v2 of the VB CRC32 algorithm provided by Paul
        ' ([email protected]) - much quicker than the nasty
        ' original version I posted.  Excellent work!
    
        'Last edited by Jeff Ballard on 2008-10-16
        'Added GetCrc32String which returns the properly formatted string
    
        Private crc32Table() As Integer
        Private Const BUFFER_SIZE As Integer = 1024I
    
        Friend Function GetCrc32(ByRef stream As System.IO.FileStream) As Integer
            Dim crc32Result As Integer = &HFFFFFFFF
    
            Dim buffer(BUFFER_SIZE) As Byte
            Dim readSize As Integer = BUFFER_SIZE
            Dim count As Integer = stream.Read(buffer, 0I, readSize)
            Dim i As Integer
            Dim iLookup As Integer
    
            Do While (count > 0I)
                For i = 0I To count - 1I
                    iLookup = (crc32Result And &HFF) Xor buffer(i)
                    crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF   ' nasty shr 8 with vb :/
                    crc32Result = crc32Result Xor crc32Table(iLookup)
                Next i
                count = stream.Read(buffer, 0I, readSize)
            Loop
            Return Not (crc32Result)
        End Function
    
        Friend Function GetCrc32String(ByRef stream As System.IO.FileStream) As String
            Return String.Format("{0:X8}", GetCrc32(stream))
        End Function
    
        Friend Sub New()
            ' This is the official polynomial used by CRC32 in PKZip.
            ' Often the polynomial is shown reversed (04C11DB7).
            Dim dwPolynomial As Integer = &HEDB88320
            Dim i, j As Integer
    
            ReDim crc32Table(256I)
            Dim dwCrc As Integer
    
            For i = 0I To 255I
                dwCrc = i
                For j = 8I To 1I Step -1I
                    If (dwCrc And 1I) > 0I Then
                        dwCrc = ((dwCrc And &HFFFFFFFE) \ 2I) And &H7FFFFFFF
                        dwCrc = dwCrc Xor dwPolynomial
                    Else
                        dwCrc = ((dwCrc And &HFFFFFFFE) \ 2I) And &H7FFFFFFF
                    End If
                Next j
                crc32Table(i) = dwCrc
            Next i
        End Sub
    End Class
    Usage:
    Code:
    Dim Crc_Class As New CRC32
    Dim Raw_Crc_Integer As Integer
    Dim Formated_CRC_String As String
    Dim FileStream As New FileStream("File Here", FileMode.Open, FileAccess.Read, FileShare.Read, 8192I)
    
    Raw_Crc_Integer = Crc_Class.GetCrc32(m_FileStream)
    'Or:
    Formated_CRC_String = Crc_Class.GetCrc32String(m_FileStream)
    
    FileStream.Close()
    Last edited by JuggaloBrotha; Oct 16th, 2008 at 08:17 AM.
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width