-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadler32.monkey
79 lines (55 loc) · 1.99 KB
/
adler32.monkey
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
Strict
Public
' Imports (Public):
Import config
' Imports (Private):
Private
Import monkey.math
Import util
Public
' Functions (Public):
' Based loosely on the ZLib and UZLib source:
Function Adler32:Int(Data:DataBuffer, Length:Int, Offset:Int=0, Prev_Sum:Int=1) ' UInt
' Constant variable(s):
Const ADLER32_BASE:= 65521
Const ADLER32_NMAX:= 5552
' Local variable(s):
Local S1:= (Prev_Sum & $FFFF) ' UInt
Local S2:= Lsr(Prev_Sum, 16) ' UInt
While (Length > 0)
Local K:Int = Min(Length, ADLER32_NMAX)
For Local I:= (K / 16) Until 0 Step -1
#Rem
' Unrolled version:
S1 += __Adler32_PeekByte(Data, Offset); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+1); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+2); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+3); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+4); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+5); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+6); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+7); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+8); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+9); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+10); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+11); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+12); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+13); S2 += S1
S1 += __Adler32_PeekByte(Data, Offset+14); S2 += S1; S1 += __Adler32_PeekByte(Data, Offset+15); S2 += S1
#End
Local EndOffset:= (Offset + 16)
For Offset = Offset Until EndOffset
S1 += __Adler32_PeekByte(Data, Offset)
S2 += S1
Next
Next
For Local I:= (K Mod 16) Until 0 Step -1
S1 += __Adler32_PeekByte(Data, Offset)
Offset += 1
S2 += S1
Next
S1 Mod= ADLER32_BASE
S2 Mod= ADLER32_BASE
Length -= K
Wend
Return (Lsl(S2, 16) | S1) ' & $FFFFFF
End
' Functions (Private):
Private
Function __Adler32_PeekByte:Int(Buffer:DataBuffer, Address:Int)
Return (Buffer.PeekByte(Address) & $FF)
End
Public