Rizin
unix-like reverse engineering framework and cli tools
ChecksumImpl.cs
Go to the documentation of this file.
1 //
2 // © Copyright Henrik Ravn 2004
3 //
4 // Use, modification and distribution are subject to the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 using System;
9 using System.Runtime.InteropServices;
10 using System.Text;
11 
12 
13 namespace DotZLib
14 {
15  #region ChecksumGeneratorBase
20  public abstract class ChecksumGeneratorBase : ChecksumGenerator
21  {
25  protected uint _current;
26 
32  {
33  _current = 0;
34  }
35 
40  public ChecksumGeneratorBase(uint initialValue)
41  {
42  _current = initialValue;
43  }
44 
48  public void Reset() { _current = 0; }
49 
53  public uint Value { get { return _current; } }
54 
66  public abstract void Update(byte[] data, int offset, int count);
67 
72  public void Update(byte[] data)
73  {
74  Update(data, 0, data.Length);
75  }
76 
82  public void Update(string data)
83  {
84  Update(Encoding.UTF8.GetBytes(data));
85  }
86 
92  public void Update(string data, Encoding encoding)
93  {
94  Update(encoding.GetBytes(data));
95  }
96 
97  }
98  #endregion
99 
100  #region CRC32
104  public sealed class CRC32Checksum : ChecksumGeneratorBase
105  {
106  #region DLL imports
107 
108  [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
109  private static extern uint crc32(uint crc, int data, uint length);
110 
111  #endregion
112 
116  public CRC32Checksum() : base() {}
117 
122  public CRC32Checksum(uint initialValue) : base(initialValue) {}
123 
133  public override void Update(byte[] data, int offset, int count)
134  {
135  if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
136  if ((offset+count) > data.Length) throw new ArgumentException();
137  GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);
138  try
139  {
140  _current = crc32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);
141  }
142  finally
143  {
144  hData.Free();
145  }
146  }
147 
148  }
149  #endregion
150 
151  #region Adler
155  public sealed class AdlerChecksum : ChecksumGeneratorBase
156  {
157  #region DLL imports
158 
159  [DllImport("ZLIB1.dll", CallingConvention=CallingConvention.Cdecl)]
160  private static extern uint adler32(uint adler, int data, uint length);
161 
162  #endregion
163 
167  public AdlerChecksum() : base() {}
168 
173  public AdlerChecksum(uint initialValue) : base(initialValue) {}
174 
184  public override void Update(byte[] data, int offset, int count)
185  {
186  if (offset < 0 || count < 0) throw new ArgumentOutOfRangeException();
187  if ((offset+count) > data.Length) throw new ArgumentException();
188  GCHandle hData = GCHandle.Alloc(data, GCHandleType.Pinned);
189  try
190  {
191  _current = adler32(_current, hData.AddrOfPinnedObject().ToInt32()+offset, (uint)count);
192  }
193  finally
194  {
195  hData.Free();
196  }
197  }
198 
199  }
200  #endregion
201 
202 }
Implements a checksum generator that computes the Adler checksum on data
static uint adler32(uint adler, int data, uint length)
override void Update(byte[] data, int offset, int count)
Updates the current checksum with part of an array of bytes
AdlerChecksum()
Initializes a new instance of the Adler checksum generator
AdlerChecksum(uint initialValue)
Initializes a new instance of the Adler checksum generator with a specified value
Implements a CRC32 checksum generator
CRC32Checksum(uint initialValue)
Initializes a new instance of the CRC32 checksum generator with a specified value
static uint crc32(uint crc, int data, uint length)
CRC32Checksum()
Initializes a new instance of the CRC32 checksum generator
override void Update(byte[] data, int offset, int count)
Updates the current checksum with part of an array of bytes
Implements the common functionality needed for all ChecksumGenerators
Definition: ChecksumImpl.cs:21
uint Value
Gets the current checksum value
Definition: ChecksumImpl.cs:53
void Update(byte[] data)
Updates the current checksum with an array of bytes.
Definition: ChecksumImpl.cs:72
ChecksumGeneratorBase()
Initializes a new instance of the checksum generator base - the current checksum is set to zero
Definition: ChecksumImpl.cs:31
void Update(string data)
Updates the current checksum with the data from a string
Definition: ChecksumImpl.cs:82
abstract void Update(byte[] data, int offset, int count)
Updates the current checksum with part of an array of bytes
void Reset()
Resets the current checksum to zero
Definition: ChecksumImpl.cs:48
ChecksumGeneratorBase(uint initialValue)
Initializes a new instance of the checksum generator basewith a specified value
Definition: ChecksumImpl.cs:40
void Update(string data, Encoding encoding)
Updates the current checksum with the data from a string, using a specific encoding
Definition: ChecksumImpl.cs:92
uint _current
The value of the current checksum
Definition: ChecksumImpl.cs:25
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
unsigned int uint
Definition: gzlog.c:242
Declares methods and properties that enables a running checksum to be calculated
Definition: DotZLib.cs:115
voidpf uLong offset
Definition: ioapi.h:144