-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdlerHelper.cs
101 lines (76 loc) · 1.81 KB
/
AdlerHelper.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace GodLesZ.Library {
public class AdlerHelper {
public readonly static uint BASE = 65521;
public static uint Checksum = 0;
public static event UpdateStatusHandler OnUpdate;
public static event FinishedHandler OnFinish;
public static uint Build(byte[] buf) {
int off = 0;
int state = 0, newstate = 0;
uint s1 = 1;
uint s2 = 0;
Checksum = 0;
do {
s1 += buf[off++];
s2 += s1;
newstate = (int)((off / buf.Length) * 100);
if (newstate > state && OnUpdate != null) {
state = newstate;
OnUpdate(state);
}
} while (off < buf.Length);
buf = null;
s1 %= BASE;
s2 %= BASE;
Checksum = (s2 << 16) | s1;
if (OnFinish != null) {
OnFinish(Checksum);
}
return Checksum;
}
public static uint Build(string Filename) {
int i = 0, off = 0;
int state = 0, newstate = 0;
uint s1 = 1;
uint s2 = 0;
long len = 0;
Checksum = 0;
using (FileStream fs = File.OpenRead(Filename)) {
byte[] buf;
long streamLength = fs.Length;
len = streamLength;
while (len > 0) {
int tlen = (int)(len > 5550 ? 5550 : len);
len -= tlen;
off = 0;
buf = new byte[tlen];
fs.Read(buf, 0, buf.Length);
do {
s1 += buf[off++];
s2 += s1;
i++;
newstate = (int)((i / streamLength) * 100);
if (newstate > state && OnUpdate != null) {
state = newstate;
OnUpdate(state);
}
} while (--tlen != 0);
buf = null;
s1 %= BASE;
s2 %= BASE;
}
}
Checksum = (s2 << 16) | s1;
if (OnFinish != null) {
OnFinish(Checksum);
}
return Checksum;
}
}
public delegate void UpdateStatusHandler(int State);
public delegate void FinishedHandler(uint Checksum);
}