September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,29 @@
Public Class Crc32
' Table for pre-calculated values.
Shared table(255) As UInteger
' Initialize table
Shared Sub New()
For i As UInteger = 0 To table.Length - 1
Dim te As UInteger = i ' table entry
For j As Integer = 0 To 7
If (te And 1) = 1 Then te = (te >> 1) Xor &HEDB88320UI Else te >>= 1
Next
table(i) = te
Next
End Sub
' Return checksum calculation for Byte Array,
' optionally resuming (used when breaking a large file into read-buffer-sized blocks).
' Call with Init = False to continue calculation.
Public Shared Function cs(BA As Byte(), Optional Init As Boolean = True) As UInteger
Static crc As UInteger
If Init Then crc = UInteger.MaxValue
For Each b In BA
crc = (crc >> 8) Xor table((crc And &HFF) Xor b)
Next
Return Not crc
End Function
End Class

View file

@ -0,0 +1,23 @@
' Returns a Byte Array from a string of ASCII characters.
Function Str2BA(Str As String) As Byte()
Return System.Text.Encoding.ASCII.GetBytes(Str)
End Function
' Returns a Hex string from an UInteger, formatted to a number of digits,
' adding leading zeros If necessary.
Function HexF(Value As UInteger, Digits As Integer) As String
HexF = Hex(Value)
If Len(HexF) < Digits Then HexF = StrDup(Digits - Len(HexF), "0") & HexF
End Function
' Tests Crc32 class
Sub Test()
Dim Str As String = "The quick brown fox jumps over the lazy dog"
Debug.Print("Input = """ & Str & """")
' Convert string to Byte Array, compute crc32, and display formatted result
Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Str)), 8))
' This next code demonstrates continuing a crc32 calculation when breaking the input
' into pieces, such as processing a large file by a series of buffer reads.
Crc32.cs(Str2BA(Mid(Str, 1, 20)))
Debug.Print("Crc32 = " & HexF(Crc32.cs(Str2BA(Mid(Str, 21)), False), 8))
End Sub

View file

@ -0,0 +1,3 @@
Input = "The quick brown fox jumps over the lazy dog"
Crc32 = 414FA339
Crc32 = 414FA339