Skip to content

Commit bd473a9

Browse files
committed
Update hashing
1 parent 2005245 commit bd473a9

File tree

7 files changed

+216
-914
lines changed

7 files changed

+216
-914
lines changed

Source/script/imports/simba.import_encoding.pas

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ implementation
2929

3030
(*
3131
EHashAlgo
32-
--------
32+
---------
3333
```
3434
type EHashAlgo = enum(SHA1, SHA256, SHA384, SHA512, MD5);
3535
```
@@ -41,7 +41,7 @@ implementation
4141

4242
(*
4343
EBaseEncoding
44-
------------
44+
-------------
4545
```
4646
type EBaseEncoding = enum(b64URL, b64, b32, b32Hex, b16);
4747
```
@@ -93,6 +93,8 @@ procedure _LapeHashFile(const Params: PParamArray; const Result: Pointer); LAPE_
9393
```
9494
function Hash32(Data: Pointer; Len: Int32; Seed: UInt32 = 0): UInt32;
9595
```
96+
Computes a UInt32 hash of data using xxhash32 algorithm.
97+
https://xxhash.com/
9698
*)
9799
procedure _LapeHash32(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
98100
begin
@@ -105,58 +107,121 @@ procedure _LapeHash32(const Params: PParamArray; const Result: Pointer); LAPE_WR
105107
```
106108
function Hash32(S: String; Seed: UInt32 = 0): UInt32;
107109
```
110+
Computes a UInt32 hash of string using xxhash32 algorithm.
111+
https://xxhash.com/
108112
*)
109113
procedure _LapeHash32String(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
110114
begin
111115
PUInt32(Result)^ := Hash32(PString(Params^[0])^, PUInt32(Params^[1])^);
112116
end;
113117

114118
(*
115-
Hash64
116-
------
119+
BaseEncode
120+
----------
117121
```
118-
function Hash64(Data: PByte; Len: Int32; Seed: UInt64 = 0): UInt64;
122+
function BaseEncode(Encoding: EBaseEncoding; const Data: String): String;
119123
```
120124
*)
121-
procedure _LapeHash64(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
125+
procedure _LapeBaseEncode(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
122126
begin
123-
PUInt64(Result)^ := Hash64(PPointer(Params^[0])^, PInteger(Params^[1])^, PUInt64(Params^[2])^);
127+
PString(Result)^ := BaseEncode(EBaseEncoding(Params^[0]^), PString(Params^[1])^);
124128
end;
125129

126130
(*
127-
Hash64
128-
------
131+
BaseDecode
132+
----------
129133
```
130-
function Hash64(S: String; Seed: UInt64 = 0): UInt64;
134+
function BaseDecode(Encoding: EBaseEncoding; const Data: String): String;
131135
```
132136
*)
133-
procedure _LapeHash64String(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
137+
procedure _LapeBaseDecode(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
134138
begin
135-
PUInt64(Result)^ := Hash64(PString(Params^[0])^, PUInt64(Params^[1])^);
139+
PString(Result)^ := BaseDecode(EBaseEncoding(Params^[0]^), PString(Params^[1])^);
136140
end;
137141

142+
138143
(*
139-
BaseEncode
140-
----------
144+
ECompressAlgo
145+
-------------
141146
```
142-
function BaseEncode(Encoding: EBaseEncoding; const Data: String): String;
147+
type ECompressAlgo = enum(ZLIB, SYNLZ);
148+
```
149+
150+
```{note}
151+
This enum is scoped, so must be used like `ECompressAlgo.ZLIB`
143152
```
144153
*)
145-
procedure _LapeBaseEncode(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
154+
155+
(*
156+
CompressData
157+
------------
158+
```
159+
procedure CompressData(Algo: ECompressAlgo; InData: Pointer; InSize: Int64; out OutData: Pointer; out OutSize: Int64);
160+
```
161+
*)
162+
procedure _LapeCompressData(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
146163
begin
147-
PString(Result)^ := BaseEncode(EBaseEncoding(Params^[0]^), PString(Params^[1])^);
164+
CompressData(ESimbaCompressAlgo(Params^[0]^), PPByte(Params^[1])^, PInt64(Params^[2])^, PPByte(Params^[3])^, PInt64(Params^[4])^);
148165
end;
149166

150167
(*
151-
BaseDecode
152-
----------
168+
DecompressData
169+
--------------
153170
```
154-
function BaseDecode(Encoding: EBaseEncoding; const Data: String): String;
171+
procedure DecompressData(Algo: ECompressAlgo; InData: Pointer; InSize: Int64; out OutData: Pointer; out OutSize: Int64);
155172
```
156173
*)
157-
procedure _LapeBaseDecode(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
174+
procedure _LapeDecompressData(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
158175
begin
159-
PString(Result)^ := BaseDecode(EBaseEncoding(Params^[0]^), PString(Params^[1])^);
176+
DecompressData(ESimbaCompressAlgo(Params^[0]^), PPByte(Params^[1])^, PInt64(Params^[2])^, PPByte(Params^[3])^, PInt64(Params^[4])^);
177+
end;
178+
179+
(*
180+
CompressBytes
181+
-------------
182+
```
183+
function CompressBytes(Algo: ECompressAlgo; Bytes: TByteArray): TByteArray;
184+
```
185+
*)
186+
procedure _LapeCompressBytes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
187+
begin
188+
PByteArray(Result)^ := CompressBytes(ESimbaCompressAlgo(Params^[0]^), PByteArray(Params^[1])^);
189+
end;
190+
191+
(*
192+
DecompressBytes
193+
---------------
194+
```
195+
function DecompressBytes(Algo: ECompressAlgo; Bytes: TByteArray): TByteArray;
196+
```
197+
*)
198+
procedure _LapeDecompressBytes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
199+
begin
200+
PByteArray(Result)^ := DecompressBytes(ESimbaCompressAlgo(Params^[0]^), PByteArray(Params^[1])^);
201+
end;
202+
203+
(*
204+
CompressString
205+
--------------
206+
```
207+
function CompressString(Algo: ECompressAlgo; Encoding: EBaseEncoding; Str: String): String;
208+
```
209+
*)
210+
procedure _LapeCompressString(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
211+
begin
212+
PString(Result)^ := CompressString(ESimbaCompressAlgo(Params^[0]^), EBaseEncoding(Params^[1]^), PString(Params^[2])^);
213+
end;
214+
215+
(*
216+
DecompressString
217+
----------------
218+
```
219+
function DecompressString(Algo: ESimbaCompressAlgo; Encoding: EBaseEncoding; Str: String): String;
220+
```
221+
*)
222+
procedure _LapeDecompressString(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
223+
begin
224+
PString(Result)^ := DecompressString(ESimbaCompressAlgo(Params^[0]^), EBaseEncoding(Params^[1]^), PString(Params^[2])^);
160225
end;
161226

162227
(*
@@ -313,36 +378,6 @@ procedure _LapeResourceReader_Save2(const Params: PParamArray; const Result: Poi
313378
PBoolean(Result)^ := PLapeObjectResourceReader(Params^[0])^^.Save(PString(Params^[1])^, PString(Params^[2])^);
314379
end;
315380

316-
procedure _LapeCompressData(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
317-
begin
318-
CompressData(ESimbaCompressAlgo(Params^[0]^), PPByte(Params^[1])^, PInt64(Params^[2])^, PPByte(Params^[3])^, PInt64(Params^[4])^);
319-
end;
320-
321-
procedure _LapeDecompressData(const Params: PParamArray); LAPE_WRAPPER_CALLING_CONV
322-
begin
323-
DecompressData(ESimbaCompressAlgo(Params^[0]^), PPByte(Params^[1])^, PInt64(Params^[2])^, PPByte(Params^[3])^, PInt64(Params^[4])^);
324-
end;
325-
326-
procedure _LapeCompressBytes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
327-
begin
328-
PByteArray(Result)^ := CompressBytes(ESimbaCompressAlgo(Params^[0]^), PByteArray(Params^[1])^);
329-
end;
330-
331-
procedure _LapeDecompressBytes(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
332-
begin
333-
PByteArray(Result)^ := DecompressBytes(ESimbaCompressAlgo(Params^[0]^), PByteArray(Params^[1])^);
334-
end;
335-
336-
procedure _LapeCompressString(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
337-
begin
338-
PString(Result)^ := CompressString(ESimbaCompressAlgo(Params^[0]^), EBaseEncoding(Params^[1]^), PString(Params^[2])^);
339-
end;
340-
341-
procedure _LapeDecompressString(const Params: PParamArray; const Result: Pointer); LAPE_WRAPPER_CALLING_CONV
342-
begin
343-
PString(Result)^ := DecompressString(ESimbaCompressAlgo(Params^[0]^), EBaseEncoding(Params^[1]^), PString(Params^[2])^);
344-
end;
345-
346381
procedure ImportEncoding(Script: TSimbaScript);
347382
begin
348383
with Script.Compiler do
@@ -364,8 +399,6 @@ procedure ImportEncoding(Script: TSimbaScript);
364399

365400
addGlobalFunc('function Hash32(Data: Pointer; Len: Int32; Seed: UInt32 = 0): UInt32; overload', @_LapeHash32);
366401
addGlobalFunc('function Hash32(S: String; Seed: UInt32 = 0): UInt32; overload', @_LapeHash32String);
367-
addGlobalFunc('function Hash64(Data: Pointer; Len: Int32; Seed: UInt64 = 0): UInt64; overload', @_LapeHash64);
368-
addGlobalFunc('function Hash64(S: String; Seed: UInt64 = 0): UInt64; overload', @_LapeHash64String);
369402

370403
addGlobalType('enum(ZLIB, SYNLZ)', 'ECompressAlgo');
371404

Source/simba.compress.pas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
{
2+
Author: Raymond van Venetië and Merlijn Wajer
3+
Project: Simba (https://github.com/MerlijnWajer/Simba)
4+
License: GNU General Public License (https://www.gnu.org/licenses/gpl-3.0)
5+
}
16
unit simba.compress;
27

38
{$i simba.inc}

Source/simba.container_dict.pas

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ implementation
148148

149149
uses
150150
TypInfo,
151-
simba.math, simba.hash_murmur;
151+
simba.math, simba.hash;
152152

153153
{$overflowchecks off}
154154
{$rangechecks off}
@@ -316,7 +316,7 @@ procedure TDictionary<K,V>.Clear;
316316
function TDictionary<K,V>.Hash(constref key: K): UInt32;
317317
begin
318318
if FHashData then
319-
Result := UInt32(TMurmur2aLE.HashBuf(@key, SizeOf(K)) and FSize)
319+
Result := UInt32(Hash32(@key, SizeOf(K)) and FSize)
320320
else
321321
Result := UInt32(FHashFunc(key) and FSize);
322322
end;

Source/simba.hash.pas

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
}
66
unit simba.hash;
77

8-
{$i simba.inc}
8+
{$i simba.Inc}
99

1010
interface
1111

@@ -29,17 +29,18 @@ interface
2929
function Hash32(Data: PByte; Len: Int32; Seed: UInt32 = 0): UInt32; overload;
3030
function Hash32(S: String; Seed: UInt32 = 0): UInt32; overload;
3131

32-
function Hash64(Data: PByte; Len: Int32; Seed: UInt64 = 0): UInt64; overload;
33-
function Hash64(S: String; Seed: UInt64 = 0): UInt64; overload;
34-
3532
function CRC32(Data: PByte; Len: Int32): UInt32;
3633
function CRC64(Data: PByte; Len: Int32): UInt64;
3734

3835
implementation
3936

4037
uses
41-
crc, md5, fpsha256, fpsha512, sha1,
42-
simba.hash_murmur;
38+
mormor2_xxhash32,
39+
crc, md5, fpsha256, fpsha512, sha1;
40+
41+
var
42+
InitialCrc32: UInt32;
43+
InitialCrc64: UInt64;
4344

4445
function HashBuffer(Algo: EHashAlgo; Buf: PByte; Len: Integer): String;
4546
var
@@ -101,39 +102,30 @@ function HashFile(Algo: EHashAlgo; const FileName: String): String;
101102

102103
function Hash32(Data: PByte; Len: Int32; Seed: UInt32): UInt32;
103104
begin
104-
Result := TMurmur2aLE.HashBuf(Data, Len, Seed);
105+
Result := xxHash32(Seed, Data, Len);
105106
end;
106107

107108
function Hash32(S: String; Seed: UInt32): UInt32;
108109
begin
109110
if (Length(S) > 0) then
110-
Result := Hash32(@S[1], Length(S), Seed)
111-
else
112-
Result := Seed;
113-
end;
114-
115-
function Hash64(Data: PByte; Len: Int32; Seed: UInt64): UInt64;
116-
begin
117-
Result := TMurmur64aLE.HashBuf(Data, Len, Seed);
118-
end;
119-
120-
function Hash64(S: String; Seed: UInt64): UInt64;
121-
begin
122-
if (Length(S) > 0) then
123-
Result := Hash64(@S[1], Length(S), Seed)
111+
Result := xxHash32(Seed, @S[1], Length(S))
124112
else
125113
Result := Seed;
126114
end;
127115

128116
function CRC32(Data: PByte; Len: Int32): UInt32;
129117
begin
130-
Result := crc.crc32(crc.crc32(0, nil, 0), Data, Len);
118+
Result := crc.crc32(InitialCrc32, Data, Len);
131119
end;
132120

133121
function CRC64(Data: PByte; Len: Int32): UInt64;
134122
begin
135-
Result := crc.crc64(crc.crc64(0, nil, 0), Data, Len);
123+
Result := crc.crc64(InitialCrc64, Data, Len);
136124
end;
137125

126+
initialization
127+
InitialCrc32 := crc.crc32(0, nil, 0);
128+
InitialCrc64 := crc.crc64(0, nil, 0);
129+
138130
end.
139131

0 commit comments

Comments
 (0)