OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [zlib/] [contrib/] [dotzlib/] [DotZLib/] [ChecksumImpl.cs] - Blame information for rev 745

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 745 jeremybenn
//
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
16
    /// 
17
    /// Implements the common functionality needed for all s
18
    /// 
19
    /// 
20
    public abstract class ChecksumGeneratorBase : ChecksumGenerator
21
    {
22
        /// 
23
        /// The value of the current checksum
24
        /// 
25
        protected uint _current;
26
 
27
        /// 
28
        /// Initializes a new instance of the checksum generator base - the current checksum is
29
        /// set to zero
30
        /// 
31
        public ChecksumGeneratorBase()
32
        {
33
            _current = 0;
34
        }
35
 
36
        /// 
37
        /// Initializes a new instance of the checksum generator basewith a specified value
38
        /// 
39
        /// The value to set the current checksum to
40
        public ChecksumGeneratorBase(uint initialValue)
41
        {
42
            _current = initialValue;
43
        }
44
 
45
        /// 
46
        /// Resets the current checksum to zero
47
        /// 
48
        public void Reset() { _current = 0; }
49
 
50
        /// 
51
        /// Gets the current checksum value
52
        /// 
53
        public uint Value { get { return _current; } }
54
 
55
        /// 
56
        /// Updates the current checksum with part of an array of bytes
57
        /// 
58
        /// The data to update the checksum with
59
        /// Where in data to start updating
60
        /// The number of bytes from data to use
61
        /// The sum of offset and count is larger than the length of data
62
        /// data is a null reference
63
        /// Offset or count is negative.
64
        /// All the other Update methods are implmeneted in terms of this one.
65
        /// This is therefore the only method a derived class has to implement
66
        public abstract void Update(byte[] data, int offset, int count);
67
 
68
        /// 
69
        /// Updates the current checksum with an array of bytes.
70
        /// 
71
        /// The data to update the checksum with
72
        public void Update(byte[] data)
73
        {
74
            Update(data, 0, data.Length);
75
        }
76
 
77
        /// 
78
        /// Updates the current checksum with the data from a string
79
        /// 
80
        /// The string to update the checksum with
81
        /// The characters in the string are converted by the UTF-8 encoding
82
        public void Update(string data)
83
        {
84
                        Update(Encoding.UTF8.GetBytes(data));
85
        }
86
 
87
        /// 
88
        /// Updates the current checksum with the data from a string, using a specific encoding
89
        /// 
90
        /// The string to update the checksum with
91
        /// The encoding to use
92
        public void Update(string data, Encoding encoding)
93
        {
94
            Update(encoding.GetBytes(data));
95
        }
96
 
97
    }
98
    #endregion
99
 
100
    #region CRC32
101
    /// 
102
    /// Implements a CRC32 checksum generator
103
    /// 
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
 
113
        /// 
114
        /// Initializes a new instance of the CRC32 checksum generator
115
        /// 
116
        public CRC32Checksum() : base() {}
117
 
118
        /// 
119
        /// Initializes a new instance of the CRC32 checksum generator with a specified value
120
        /// 
121
        /// The value to set the current checksum to
122
        public CRC32Checksum(uint initialValue) : base(initialValue) {}
123
 
124
        /// 
125
        /// Updates the current checksum with part of an array of bytes
126
        /// 
127
        /// The data to update the checksum with
128
        /// Where in data to start updating
129
        /// The number of bytes from data to use
130
        /// The sum of offset and count is larger than the length of data
131
        /// data is a null reference
132
        /// Offset or count is negative.
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
152
    /// 
153
    /// Implements a checksum generator that computes the Adler checksum on data
154
    /// 
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
 
164
        /// 
165
        /// Initializes a new instance of the Adler checksum generator
166
        /// 
167
        public AdlerChecksum() : base() {}
168
 
169
        /// 
170
        /// Initializes a new instance of the Adler checksum generator with a specified value
171
        /// 
172
        /// The value to set the current checksum to
173
        public AdlerChecksum(uint initialValue) : base(initialValue) {}
174
 
175
        /// 
176
        /// Updates the current checksum with part of an array of bytes
177
        /// 
178
        /// The data to update the checksum with
179
        /// Where in data to start updating
180
        /// The number of bytes from data to use
181
        /// The sum of offset and count is larger than the length of data
182
        /// data is a null reference
183
        /// Offset or count is negative.
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
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.