-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrc32.monkey
91 lines (70 loc) · 1.93 KB
/
crc32.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
80
81
82
83
84
85
86
87
88
89
90
91
Strict
Public
' Preprocessor related:
#HASH_CRC32_USE_ARRAYS = True
' Imports (Public):
Import config
' Imports (Private):
Private
Import util
Public
' Constant variable(s) (Private):
Private
Const Default_CRC:= $FFFFFFFF
Public
' Functions (Public):
' Loosely based on the ZLib and UZLib source:
Function CRC32:Int(Data:DataBuffer, Length:Int, Offset:Int=0, CRC:Int=Default_CRC, FixValue:Bool=True) ' UInt
If ((Length-Offset) = 0) Then
Return 0
Endif
For Local I:= Offset Until Length
CRC ~= (Data.PeekByte(I) & $FF)
CRC = _CRC32_Table_Get(CRC & $0F) ~ Lsr(CRC, 4)
CRC = _CRC32_Table_Get(CRC & $0F) ~ Lsr(CRC, 4)
Next
If (Not FixValue) Then
Return CRC
Endif
Return (CRC ~ $FFFFFFFF)
End
' This command simply wraps 'CRC32', and outputs the result in hexadecimal.
Function CRC32InHex:HexValue(Data:DataBuffer, Length:Int, Offset:Int=0, CRC:Int=Default_CRC, FixValue:Bool=True) ' UInt
' Return the hash-code in hexadecimal.
Return IntToHex(CRC32(Data, Length, Offset, CRC, FixValue))
End
' Functions (Private):
Private
' Constant variable(s):
#If HASH_CRC32_USE_ARRAYS
Global __CRC32_Table:Int[] = [ $00000000, $1DB71064, $3B6E20C8, $26D930AC, $76DC4190,
$6B6B51F4, $4DB26158, $5005713C, $EDB88320, $F00F9344,
$D6D6A3E8, $CB61B38C, $9B64C2B0, $86D3D2D4, $A00AE278,
$BDBDF21C] ' Const
#End
Function _CRC32_Table_Get:Int(Index:Int)
#If HASH_CRC32_USE_ARRAYS
Return __CRC32_Table[Index]
#Else
Select (Index)
Case 0; Return $00000000
Case 1; Return $1DB71064
Case 2; Return $3B6E20C8
Case 3; Return $26D930AC
Case 4; Return $76DC4190
Case 5; Return $6B6B51F4
Case 6; Return $4DB26158
Case 7; Return $5005713C
Case 8; Return $EDB88320
Case 9; Return $F00F9344
Case 10; Return $D6D6A3E8
Case 11; Return $CB61B38C
Case 12; Return $9B64C2B0
Case 13; Return $86D3D2D4
Case 14; Return $A00AE278
Case 15; Return $BDBDF21C
End Select
Return 0
#End
End
Public