21-05-14, 12:55 AM
-
بالنسبة لـ CRC32
منقول
هذا كلاس CrcStream
طريقة استخدامه
المصدر (CrcStream stream checksum calculator)
بالنسبة لـ CRC32
منقول
هذا كلاس CrcStream
كود :
Imports System.IO
''' <summary>
''' Encapsulates a <see cref="System.IO.Stream" /> to calculate the CRC32 checksum on-the-fly as data passes through.
''' </summary>
Public Class CrcStream
Inherits Stream
''' <summary>
''' Encapsulate a <see cref="System.IO.Stream" />.
''' </summary>
''' <param name="stream">The stream to calculate the checksum for.</param>
Public Sub New(ByVal stream As Stream)
Me.m_stream = stream
End Sub
Private m_stream As Stream
''' <summary>
''' Gets the underlying stream.
''' </summary>
Public ReadOnly Property Stream() As Stream
Get
Return m_stream
End Get
End Property
Public Overrides ReadOnly Property CanRead() As Boolean
Get
Return m_stream.CanRead
End Get
End Property
Public Overrides ReadOnly Property CanSeek() As Boolean
Get
Return m_stream.CanSeek
End Get
End Property
Public Overrides ReadOnly Property CanWrite() As Boolean
Get
Return m_stream.CanWrite
End Get
End Property
Public Overrides Sub Flush()
m_stream.Flush()
End Sub
Public Overrides ReadOnly Property Length() As Long
Get
Return m_stream.Length
End Get
End Property
Public Overrides Property Position() As Long
Get
Return m_stream.Position
End Get
Set(ByVal value As Long)
m_stream.Position = value
End Set
End Property
Public Overrides Function Seek(ByVal offset As Long, ByVal origin As SeekOrigin) As Long
Return m_stream.Seek(offset, origin)
End Function
Public Overrides Sub SetLength(ByVal value As Long)
m_stream.SetLength(value)
End Sub
Public Overrides Function Read(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer) As Integer
count = m_stream.Read(buffer, offset, count)
m_readCrc = CalculateCrc(m_readCrc, buffer, offset, count)
Return count
End Function
Public Overrides Sub Write(ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer)
m_stream.Write(buffer, offset, count)
m_writeCrc = CalculateCrc(m_writeCrc, buffer, offset, count)
End Sub
Private Function CalculateCrc(ByVal crc As UInteger, ByVal buffer As Byte(), ByVal offset As Integer, ByVal count As Integer) As UInteger
Dim i As Integer = offset, [end] As Integer = offset + count
While i < [end]
crc = (crc >> 8) Xor table((crc Xor buffer(i)) And &HFF)
i += 1
End While
Return crc
End Function
Private Shared table As UInteger() = GenerateTable()
Private Shared Function GenerateTable() As UInteger()
Dim table As UInteger() = New UInteger(255) {}
Dim crc As UInteger
Const poly As UInteger = &HEDB88320UI
For i As UInteger = 0 To table.Length - 1
crc = i
For j As Integer = 8 To 1 Step -1
If (crc And 1) = 1 Then
crc = (crc >> 1) Xor poly
Else
crc >>= 1
End If
Next
table(i) = crc
Next
Return table
End Function
Private m_readCrc As UInteger = &HFFFFFFFFUI
''' <summary>
''' Gets the CRC checksum of the data that was read by the stream thus far.
''' </summary>
Public ReadOnly Property ReadCrc() As UInteger
Get
Return m_readCrc Xor &HFFFFFFFFUI
End Get
End Property
Private m_writeCrc As UInteger = &HFFFFFFFFUI
''' <summary>
''' Gets the CRC checksum of the data that was written to the stream thus far.
''' </summary>
Public ReadOnly Property WriteCrc() As UInteger
Get
Return m_writeCrc Xor &HFFFFFFFFUI
End Get
End Property
''' <summary>
''' Resets the read and write checksums.
''' </summary>
Public Sub ResetChecksum()
m_readCrc = &HFFFFFFFFUI
m_writeCrc = &HFFFFFFFFUI
End Sub
End Classطريقة استخدامه
كود :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Open a file stream, encapsulate it in CrcStream
Dim file As New IO.FileStream("C:\test.exe", IO.FileMode.Open)
Dim stream As New CrcStream(file)
'Use the file somehow -- in this case, read it as a string
Dim reader As New IO.StreamReader(stream)
Dim text As String = reader.ReadToEnd()
'Print the checksum
MessageBox.Show("CRC: " & stream.ReadCrc.ToString("X8"))
End Sub
End Classالمصدر (CrcStream stream checksum calculator)



