-
Notifications
You must be signed in to change notification settings - Fork 0
/
adler32.rs
106 lines (84 loc) · 2.57 KB
/
adler32.rs
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
// Copy from https://golang.org/src/hash/adler32/adler32.go
// BASE is the largest prime that is less than 65536.
const BASE: usize = 65521;
// nmax is the largest n such that
// 255 * n * (n+1) / 2 + (n+1) * (BASE-1) <= 2^32-1.
// It is mentioned in RFC 1950 (search for "5552").
const NMAX: usize = 5552;
struct Adler32 (u32);
struct Bytes {
vec: Vec<u8>
}
impl Adler32 {
fn new () -> Self {
Adler32(1)
}
fn check_sum(data: &mut Vec<u8>) -> u32 {
let adler32 = Adler32::new();
adler32.update(data)
}
fn update(&self, p: &mut Vec<u8>) -> u32{
let mut s1 = 1;
let mut s2 = 0;
let mut p_s = 0;
let mut p_e = p.len();
loop {
let p_length = p.len();
if p_length == 0 {
break;
}
let mut q: Vec<u8> = Vec::new();
if p_length >= 4 {
let mut _p = &p[0..std::cmp::min(NMAX, p_length)].to_owned();
let mut _q = &p[std::cmp::min(NMAX, p_length)..].to_owned();
p.clear();
p.append(&mut _p.to_vec());
q.clear();
q.append(&mut _q.to_vec());
}
loop {
let p_length = p.len();
if p_length < 4 {
break;
}
s1 += p[0] as u32;
s2 += s1;
s1 += p[1] as u32;
s2 += s1;
s1 += p[2] as u32;
s2 += s1;
s1 += p[3] as u32;
s2 += s1;
let mut _p = &p[std::cmp::min(4, p_length)..].to_owned();
p.clear();
p.append(&mut _p.to_vec());
}
for x in p.iter() {
s1 += *x as u32;
s2 += s1;
}
s1 %= BASE as u32;
s2 %= BASE as u32;
p.clear();
p.append(&mut q);
}
(s2 << 16 | s1) as u32
}
fn sum32(&self) -> u32 {
self.0.clone()
}
fn sum(&self, _in: &mut Vec<u8>) {
let s: u32 = self.0.clone();
_in.push( (s>>24) as u8 );
_in.push( (s>>16) as u8 );
_in.push( (s>> 8) as u8 );
_in.push( s as u8 );
}
}
fn main() {
let mut s = String::from("hello, 世界!");
let mut bytes: Vec<u8> = s.as_bytes().to_owned();
println!("{} {:?}", s, bytes);
let num = Adler32::check_sum(&mut bytes);
println!("String: {:?}\nBytes: {:?}\nNum: {:?}\tHex: {:#x}", s, bytes, num, num);
}