Source code for CRC32 bit check code

zhaozj2021-02-16  71

'Copyright (c) Wrox Press Ltd 2002. All rights reserved.' Buyers of the Visual Basic .NET Solutions Toolkit book (ISBN 1861007396) can use this code without restriction. 'This code can not be copied, compiled, or altered except by buyers Of the aforementioned book. 'Wrox Press Ltd Retains Copyright of this code in All Cases and unauthorized Transmission, Duplication, and' Publishing IS Forbidden.

Imports SystemImports System.io

Namespace Wrox.Toolkit.Util Public Class Crc32 Private Const TABLESIZE As Integer = 256 Private Const DEFAULTPOLYNOMIAL As Integer = & HEDB88320 Private Const DEFAULTINITIALVALUE As Integer = & HFFFFFFFF

Private Lookup (TableSize - 1) AS Integer Private CRCPOLYNOMIAL AS INTEGER = 0

Public Sub new () me.new (defaultpolynomial) End Sub

Public Sub New (Byval CrcPolyNomial AS Integer Me.crcpolyNomial = CRCPOLYNOMIAL InitLookuptable () End Sub

Public property polynomial () AS integer get return crCPolyNomial End Get

Set (Byval value as integer) me.crcpolynomial = value initlookuptable () end set dement

Public overloads function CalculateBlock (Byval Bytes () AS BYTE) _ as integer return calculateblock (bytes, 0, bytes.length) End Function

Public Overloads Function CalculateBlock (ByVal bytes () As Byte, _ ByVal index As Integer, _ ByVal length As Integer _) As Integer Return CalculateBlock (bytes, index, length, DEFAULTINITIALVALUE) End FunctionPublic Overloads Function CalculateBlock (_ ByVal bytes () As byte, _ ByVal index As Integer, _ ByVal length As Integer, _ ByVal initialValue As Integer) _ As Integer If bytes Is Nothing Then Throw New ArgumentNullException ( "CalculateBlock (): bytes") ElseIf index <0 Or length <= 0 _ Or Index Length> Bytes.length THROW NEW ARGUMENTOFRANGEXCEPTION () END IF

Return Not InternalcalcalculateBlock (Bytes, Index, _ Length, InitialValue)

Private Function InternalCalculateBlock (_ ByVal bytes () As Byte, _ ByVal index As Integer, _ ByVal length As Integer, _ ByVal initialValue As Integer) _ As Integer Dim crc As Integer = initialValue Dim shiftedCrc As Integer

Dim position as integer for position = index to length - 1

SHIFTEDCRC = CRC AND HFFFFFF00 ShiftedCrc = shiftedCRC / & H100 ShiftedCrc = shiftedCrc and & HFFFFFFF

CRC = shiftedCrc xor look (bytes (position) xor _ (CRC and & HFF) Next

Return CRC END FUNCTION

Public overloads function calculatefile (byval path as string) _ as integer returnitialval (Path, DefaultInitialValue) End Function

Public Overloads Function CalculateFile (_ ByVal path As String, _ ByVal initialValue As Integer) _ As Integer If path Is Nothing Then Throw New ArgumentNullException ( "path") ElseIf path.Length = 0 Then Throw New ArgumentException ( "Invalid path") End IF

Return NOT INTERCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALCALULE (PATH, INITIALVALUE) End Function

Private Function InternalCalculateFile (_ ByVal path As String, _ ByVal initialValue As Integer) _ As Integer Const blockSize As Integer = 4096 Dim count As Integer Dim inStream As FileStream Dim bytes (blockSize - 1) As Byte Dim crc As Integer = initialValue

Try Instream = file.open (path, filemode.open, fileaccess.read)

While inStream.Position

Return CRC End FunctionPrivate Sub InitLookuptable () DIM Bytecount, Bitcount AS Integer Dim CRC, ShiftedCrc As Integer

For Bytecount = 0 to TableSize - 1 CRC = bytecount

For bitcount = 0 to 7

SHIFTEDCRC = CRC AND & HFFFFFFE SHIFTEDCRC = ShiftedCRC / & H2 ShiftedCrc = ShiftedCrc and & H7FFFFFFFFFFFFFFFF

IF (CRC and & H1) THEN CRC = ShiftedCrc XOR CRCPOLYNOMIAL ELSE CRC = SHIFTEDCRC END IF NEXT

Lookup (Bytecount) = CRC Next End Sub End Classend Namespace

'Copyright (c) Wrox Press Ltd 2002. All rights reserved.' Buyers of the Visual Basic .NET Solutions Toolkit book (ISBN 1861007396) can use this code without restriction. 'This code can not be copied, compiled, or altered except by buyers Of the aforementioned book. 'Wrox Press Ltd Retains Copyright of this code in All Cases and unauthorized Transmission, Duplication, and' Publishing IS Forbidden.

Imports systemimports wrox.toolkit.util

Module CRC32TEST SUB Main (Byval Cmdargs () AS String) If Cmdargs.Length <> 1 THEN Console.writeline ("Usage: CRC32TEST ") EXIT SUB END IF

TRY DIM CRC32VAL AS INTEGER DIM CRC AS New CRC32 ()

CRC32VAL = crc.calculatefile (cmdargs (0))

Console.writeline ("CRC32 IS {0: X}", CRC32VAL) Catch E AS Exception Console.Writeline ("An Exception Occurred: {0}", End Subend Module

转载请注明原文地址:https://www.9cbs.com/read-20361.html

New Post(0)