Adler32 Checksumme berechnen

    • VB.NET

      Adler32 Checksumme berechnen

      Hier ist eine Klasse dich ich von C# nach Vb.Net übersetzt und modifiziert habe. Sie berechnet die Adler32-Checksumme eines Buffers oder Streams.

      VB.NET-Quellcode

      1. Public Class Adler32
      2. Private Const Base As Integer = 65521
      3. Private _Checksum As Long
      4. Public ReadOnly Property Checksum() As Long
      5. Get
      6. Return _Checksum
      7. End Get
      8. End Property
      9. Public Sub New()
      10. _Checksum = 0
      11. End Sub
      12. Public Sub Clear()
      13. _Checksum = 0
      14. End Sub
      15. Public Sub Update(ByVal value As Integer)
      16. Dim s1 As UInt32 = _Checksum And &HFFFF
      17. Dim s2 As UInt32 = _Checksum >> 16
      18. s1 = (s1 + (CUInt(value) And &HFF)) Mod Base
      19. s2 = (s1 + s2) Mod Base
      20. _Checksum = (s2 << 16) + s1
      21. End Sub
      22. Public Sub Update(ByVal buffer As Byte(), ByVal offset As UInt32, ByVal count As Integer)
      23. If buffer Is Nothing Then
      24. Throw New ArgumentNullException("Buffer")
      25. End If
      26. If count < 0 Then
      27. Throw New ArgumentOutOfRangeException("Count", "cannot be negative")
      28. End If
      29. If offset + count > buffer.Length Then
      30. Throw New ArgumentOutOfRangeException("Offset AndOr Count")
      31. End If
      32. Dim s1 As UInt32 = _Checksum And &HFFFF
      33. Dim s2 As UInt32 = _Checksum >> 16
      34. While count > 0
      35. Dim n As Integer = 3800
      36. If n > count Then
      37. n = count
      38. End If
      39. count -= n
      40. n -= 1
      41. While n >= 0
      42. s1 = s1 + CUInt(buffer(offset) And &HFF)
      43. s2 = s2 + s1
      44. offset += 1
      45. n -= 1
      46. End While
      47. s1 = s1 Mod Base
      48. s2 = s2 Mod Base
      49. End While
      50. _Checksum = (s2 << 16) Or s1
      51. End Sub
      52. Public Sub Update(ByVal Stream As FileStream, ByVal count As Integer)
      53. If Stream Is Nothing Then
      54. Throw New ArgumentNullException("Buffer")
      55. End If
      56. If count < 0 Then
      57. Throw New ArgumentOutOfRangeException("Count", "cannot be negative")
      58. End If
      59. If Stream.Position + count > Stream.Length Then
      60. Throw New ArgumentOutOfRangeException("Offset AndOr Count")
      61. End If
      62. Dim s1 As UInt32 = _Checksum And &HFFFF
      63. Dim s2 As UInt32 = _Checksum >> 16
      64. While count > 0
      65. Dim n As Integer = 3800
      66. If n > count Then
      67. n = count
      68. End If
      69. count -= n
      70. n -= 1
      71. While n >= 0
      72. s1 = s1 + CUInt(Stream.ReadByte() And &HFF)
      73. s2 = s2 + s1
      74. n -= 1
      75. End While
      76. s1 = s1 Mod Base
      77. s2 = s2 Mod Base
      78. End While
      79. _Checksum = (s2 << 16) Or s1
      80. End Sub
      81. End Class


      Hier ein kleines beispiel wie man die Manifest-Checksumme einer GCF Datei berechnet:

      VB.NET-Quellcode

      1. Dim adler32 As New Checksums.Adler32
      2. adler32.Update(Stream, 48)'Die ersten 12 UInts berechnen
      3. For i = 1 To 8
      4. adler32.Update(0)'Die folgenden 2 Uints auslassen
      5. Next
      6. Stream.Position += 8
      7. adler32.Update(Stream, Manifest.Header.BinarySize - 56)'den rest berechnen


      Copyright: © 1999, 2000, 2001 Free Software Foundation, Inc. © 2001 Mike Krueger
      Translation and modification: Me (rAzoR8)
      Source: koders.com/csharp/fid630960EA3…CC79915FB359B62788D3.aspx

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „razor8“ ()