forked from canneverbe/Ketarin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CrcStream.cs
171 lines (143 loc) · 3.51 KB
/
CrcStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using System.IO;
namespace CodeProject.ReiMiyasaka
{
/// <summary>
/// Encapsulates a <see cref="System.IO.Stream" /> to calculate the CRC32 checksum on-the-fly as data passes through.
/// </summary>
public class CrcStream : Stream
{
/// <summary>
/// Encapsulate a <see cref="System.IO.Stream" />.
/// </summary>
/// <param name="stream">The stream to calculate the checksum for.</param>
public CrcStream(Stream stream)
{
this.stream = stream;
}
Stream stream;
/// <summary>
/// Gets the underlying stream.
/// </summary>
public Stream Stream
{
get { return stream; }
}
public override bool CanRead
{
get { return stream.CanRead; }
}
public override bool CanSeek
{
get { return stream.CanSeek; }
}
public override bool CanWrite
{
get { return stream.CanWrite; }
}
public override void Flush()
{
stream.Flush();
}
public override long Length
{
get { return stream.Length; }
}
public override long Position
{
get
{
return stream.Position;
}
set
{
stream.Position = value;
}
}
public override long Seek(long offset, SeekOrigin origin)
{
return stream.Seek(offset, origin);
}
public override void SetLength(long value)
{
stream.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count)
{
count = stream.Read(buffer, offset, count);
readCrc = CalculateCrc(readCrc, buffer, offset, count);
return count;
}
public override void Write(byte[] buffer, int offset, int count)
{
stream.Write(buffer, offset, count);
writeCrc = CalculateCrc(writeCrc, buffer, offset, count);
}
uint CalculateCrc(uint crc, byte[] buffer, int offset, int count)
{
unchecked
{
for (int i = offset, end = offset + count; i < end; i++)
crc = (crc >> 8) ^ table[(crc ^ buffer[i]) & 0xFF];
}
return crc;
}
static private uint[] table = GenerateTable();
static private uint[] GenerateTable()
{
unchecked
{
uint[] table = new uint[256];
const uint poly = 0xEDB88320;
for (uint i = 0; i < table.Length; i++)
{
uint crc = i;
for (int j = 8; j > 0; j--)
{
if ((crc & 1) == 1)
crc = (crc >> 1) ^ poly;
else
crc >>= 1;
}
table[i] = crc;
}
return table;
}
}
uint readCrc = unchecked(0xFFFFFFFF);
/// <summary>
/// Gets the CRC checksum of the data that was read by the stream thus far.
/// </summary>
public uint ReadCrc
{
get { return unchecked(readCrc ^ 0xFFFFFFFF); }
}
uint writeCrc = unchecked(0xFFFFFFFF);
/// <summary>
/// Gets the CRC checksum of the data that was written to the stream thus far.
/// </summary>
public uint WriteCrc
{
get { return unchecked(writeCrc ^ 0xFFFFFFFF); }
}
/// <summary>
/// Resets the read and write checksums.
/// </summary>
public void ResetChecksum()
{
readCrc = unchecked(0xFFFFFFFF);
writeCrc = unchecked(0xFFFFFFFF);
}
public static uint GetCrcFromFile(string filename)
{
using (FileStream stream = File.OpenRead(filename))
{
CrcStream crcStream = new CrcStream(stream);
byte[] buffer = new byte[1024];
while (crcStream.Read(buffer, 0, buffer.Length) > 0)
{
}
return crcStream.ReadCrc;
}
}
}
}