-
Notifications
You must be signed in to change notification settings - Fork 0
/
adler32.ml
141 lines (125 loc) · 4.01 KB
/
adler32.ml
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
module Op =
struct
let ( >> ) = Int32.shift_right
let ( << ) = Int32.shift_left
let ( && ) = Int32.logand
let ( || ) = Int32.logor
let ( ! ) x = Int32.of_int !x
end
let _base = 65521
let _nmax = 5552
let digest buf adler32 off len =
let a = ref (Int32.to_int Op.((adler32 >> 16) && 0xFFFFl)) in
let b = ref (Int32.to_int Op.(adler32 && 0xFFFFl)) in
let l = ref len in
let o = ref off in
if len = 0
then adler32
else if len = 1
then begin
b := !b + (Char.code @@ String.get buf !o);
if !b >= _base then b := !b - _base;
a := !a + !b;
if !a >= _base then a := !a - _base;
Op.(!b || (!a << 16))
end else if len < 16
then begin
while !l <> 0
do b := !b + (Char.code @@ String.get buf !o);
a := !a + !b;
incr o;
decr l;
done;
if !b >= _base then b := !b - _base;
a := !a mod _base;
Op.(!b || (!a << 16))
end else begin
while !l >= _nmax
do l := !l - _nmax;
for _ = _nmax / 16 downto 1
do b := !b + (Char.code @@ String.get buf !o);
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 1));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 2));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 3));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 4));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 5));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 6));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 7));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 8));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 9));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 10));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 11));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 12));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 13));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 14));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 15));
a := !a + !b;
o := !o + 16
done;
b := !b mod _base;
a := !a mod _base;
done;
if !l > 0
then begin
while !l >= 16
do l := !l - 16;
b := !b + (Char.code @@ String.get buf !o);
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 1));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 2));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 3));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 4));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 5));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 6));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 7));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 8));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 9));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 10));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 11));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 12));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 13));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 14));
a := !a + !b;
b := !b + (Char.code @@ String.get buf (!o + 15));
a := !a + !b;
o := !o + 16;
done;
while !l > 0
do b := !b + (Char.code @@ String.get buf !o);
a := !a + !b;
decr l;
incr o;
done;
b := !b mod _base;
a := !a mod _base;
end;
Op.(!b || (!a << 16))
end