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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#[cfg(test)]
extern crate rand;
use std::io;
const BASE: u32 = 65521;
const NMAX: usize = 5552;
#[inline(always)]
fn do1(adler: &mut u32, sum2: &mut u32, buf: &[u8]) {
*adler += buf[0] as u32;
*sum2 += *adler;
}
#[inline(always)]
fn do2(adler: &mut u32, sum2: &mut u32, buf: &[u8]) {
do1(adler, sum2, &buf[0..1]);
do1(adler, sum2, &buf[1..2]);
}
#[inline(always)]
fn do4(adler: &mut u32, sum2: &mut u32, buf: &[u8]) {
do2(adler, sum2, &buf[0..2]);
do2(adler, sum2, &buf[2..4]);
}
#[inline(always)]
fn do8(adler: &mut u32, sum2: &mut u32, buf: &[u8]) {
do4(adler, sum2, &buf[0..4]);
do4(adler, sum2, &buf[4..8]);
}
#[inline(always)]
fn do16(adler: &mut u32, sum2: &mut u32, buf: &[u8]) {
do8(adler, sum2, &buf[0..8]);
do8(adler, sum2, &buf[8..16]);
}
pub struct RollingAdler32 {
a: u32,
b: u32,
}
impl RollingAdler32 {
pub fn new() -> RollingAdler32 {
Self::from_value(1)
}
pub fn from_value(adler32: u32) -> RollingAdler32 {
let a = adler32 & 0xFFFF;
let b = adler32 >> 16;
RollingAdler32 { a: a, b: b }
}
pub fn from_buffer(buffer: &[u8]) -> RollingAdler32 {
let mut hash = RollingAdler32::new();
hash.update_buffer(buffer);
hash
}
pub fn hash(&self) -> u32 {
(self.b << 16) | self.a
}
pub fn remove(&mut self, size: usize, byte: u8) {
let byte = byte as u32;
self.a = (self.a + BASE - byte) % BASE;
self.b = (self.b + BASE - 1 + (BASE - size as u32) * byte) % BASE;
}
pub fn update(&mut self, byte: u8) {
let byte = byte as u32;
self.a = (self.a + byte) % BASE;
self.b = (self.b + self.a) % BASE;
}
pub fn update_buffer(&mut self, buffer: &[u8]) {
let len = buffer.len();
if len == 1 {
self.update(buffer[0]);
return;
}
if len < 16 {
for pos in 0..len {
self.a += buffer[pos] as u32;
self.b += self.a;
}
if self.a >= BASE {
self.a -= BASE;
}
self.b %= BASE;
return;
}
let mut pos = 0;
while pos + NMAX <= len {
let end = pos + NMAX;
while pos < end {
do16(&mut self.a, &mut self.b, &buffer[pos..pos + 16]);
pos += 16;
}
self.a %= BASE;
self.b %= BASE;
}
if pos < len {
while len - pos >= 16 {
do16(&mut self.a, &mut self.b, &buffer[pos..pos + 16]);
pos += 16;
}
while len - pos > 0 {
self.a += buffer[pos] as u32;
self.b += self.a;
pos += 1;
}
self.a %= BASE;
self.b %= BASE;
}
}
}
pub fn adler32<R: io::Read>(mut reader: R) -> io::Result<u32> {
let mut hash = RollingAdler32::new();
let mut buffer = [0u8; NMAX];
let mut read = try!(reader.read(&mut buffer));
while read > 0 {
hash.update_buffer(&buffer[..read]);
read = try!(reader.read(&mut buffer));
}
Ok(hash.hash())
}
#[cfg(test)]
mod test {
use rand;
use rand::Rng;
use std::io;
use super::{BASE, adler32, RollingAdler32};
fn adler32_slow<R: io::Read>(reader: R) -> io::Result<u32> {
let mut a: u32 = 1;
let mut b: u32 = 0;
for byte in reader.bytes() {
let byte = try!(byte) as u32;
a = (a + byte) % BASE;
b = (b + a) % BASE;
}
Ok((b << 16) | a)
}
#[test]
fn testvectors() {
fn do_test(v: u32, bytes: &[u8]) {
let mut hash = RollingAdler32::new();
hash.update_buffer(&bytes);
assert_eq!(hash.hash(), v);
let r = io::Cursor::new(bytes);
assert_eq!(adler32(r).unwrap(), v);
}
do_test(0x00000001, b"");
do_test(0x00620062, b"a");
do_test(0x024d0127, b"abc");
do_test(0x29750586, b"message digest");
do_test(0x90860b20, b"abcdefghijklmnopqrstuvwxyz");
do_test(0x8adb150c, b"ABCDEFGHIJKLMNOPQRSTUVWXYZ\
abcdefghijklmnopqrstuvwxyz\
0123456789");
do_test(0x97b61069, b"1234567890123456789012345678901234567890\
1234567890123456789012345678901234567890");
do_test(0xD6251498, &[255; 64000]);
}
#[test]
fn compare() {
let mut rng = rand::thread_rng();
let mut data = vec![0u8; 5589];
for size in [0, 1, 3, 4, 5, 31, 32, 33, 67,
5550, 5552, 5553, 5568, 5584, 5589].iter().cloned() {
rng.fill_bytes(&mut data[..size]);
let r1 = io::Cursor::new(&data[..size]);
let r2 = r1.clone();
if adler32_slow(r1).unwrap() != adler32(r2).unwrap() {
panic!("Comparison failed, size={}", size);
}
}
}
#[test]
fn rolling() {
assert_eq!(RollingAdler32::from_value(0x01020304).hash(), 0x01020304);
fn do_test(a: &[u8], b: &[u8]) {
let mut total = Vec::with_capacity(a.len() + b.len());
total.extend(a);
total.extend(b);
let mut h = RollingAdler32::from_buffer(&total[..(b.len())]);
for i in 0..(a.len()) {
h.remove(b.len(), a[i]);
h.update(total[b.len() + i]);
}
assert_eq!(h.hash(), adler32(b).unwrap());
}
do_test(b"a", b"b");
do_test(b"", b"this a test");
do_test(b"th", b"is a test");
do_test(b"this a ", b"test");
}
}