-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCrcStream.cs
99 lines (83 loc) · 1.7 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
using System;
using System.IO;
namespace Weland {
public class CrcStream : Stream {
public CrcStream(Stream s) {
stream = s;
BuildTable();
}
public uint GetCRC() {
return (crc ^ 0xffffffff);
}
public override void Write(byte[] buffer, int offset, int count) {
for (int i = offset; i < offset + count; ++i) {
uint a = (crc >> 8) & 0x00ffffff;
uint b = (table[(crc ^ buffer[i]) & 0xff]);
crc = a^b;
}
stream.Write(buffer, offset, count);
}
public Stream Stream {
get {
return stream;
}
}
public override bool CanRead {
get {
return false;
}
}
public override bool CanWrite {
get {
return true;
}
}
public override bool CanSeek {
get {
return false;
}
}
public override long Seek(long position, SeekOrigin origin) {
throw new NotSupportedException();
}
public override long Length {
get {
throw new NotSupportedException();
}
}
public override long Position {
get {
throw new NotSupportedException();
}
set {
throw new NotSupportedException();
}
}
public override void Flush() {
stream.Flush();
}
public override void SetLength(long length) {
throw new NotSupportedException();
}
public override int Read(byte[] buffer, int offset, int count) {
throw new NotSupportedException();
}
void BuildTable() {
table = new uint[256];
for (int i = 0; i < table.Length; ++i) {
uint crc = (uint) i;
for (int j = 0; j < 8; ++j) {
if ((crc & 1) != 0)
crc = (crc >> 1) ^ polynomial;
else
crc >>= 1;
}
table[i] = crc;
}
}
uint[] table;
uint crc = 0xffffffff;
const uint polynomial = 0xedb88320;
Stream stream;
}
}