-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSimpleHash.pas
More file actions
1769 lines (1445 loc) · 59.9 KB
/
SimpleHash.pas
File metadata and controls
1769 lines (1445 loc) · 59.9 KB
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-------------------------------------------------------------------------------
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
-------------------------------------------------------------------------------}
{===============================================================================
Simple hash
This library provides means of calculating very simple hash of almost any
provided data. Two versions of this hash are provided - 32bit and 64bit.
The hash was designed to be maximally simple and also fast to calculate,
which means it is not particularly good in terms of security, collisions,
diffusion and other desired hash properties - and it for sure is not a
cryptographic hash.
It is meant for situations where one simply needs SOME hash, and where
security is of no great concern (eg. hash lists or hash maps).
The implementation is pretty much random, but it was inspired by other
code bases I have seen over the years - unfortunatelly I cannot name any,
simply because I do not remember them. So if anyone thinks it is a stealed
code, it is not, but that does not mean someone cannot recognize it, sorry!
Version 1.0.2 (2026-03-03)
Last change 2026-03-03
©2026 František Milt
Contacts:
František Milt: frantisek.milt@gmail.com
Support:
If you find this code useful, please consider supporting its author(s) by
making a small donation using the following link(s):
https://www.paypal.me/FMilt
Changelog:
For detailed changelog and history please refer to this git repository:
github.com/TheLazyTomcat/Lib.SimpleHash
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
BasicUIM - github.com/TheLazyTomcat/Lib.BasicUIM
HashBase - github.com/TheLazyTomcat/Lib.HashBase
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
ListUtils - github.com/TheLazyTomcat/Lib.ListUtils
SimpleCPUID - github.com/TheLazyTomcat/Lib.SimpleCPUID
StaticMemoryStream - github.com/TheLazyTomcat/Lib.StaticMemoryStream
StrRect - github.com/TheLazyTomcat/Lib.StrRect
UInt64Utils - github.com/TheLazyTomcat/Lib.UInt64Utils
WinFileInfo - github.com/TheLazyTomcat/Lib.WinFileInfo
===============================================================================}
unit SimpleHash;
{
SimpleHash_PurePascal
If you want to compile this unit without ASM, don't want to or cannot define
PurePascal for the entire project and at the same time you don't want to or
cannot make changes to this unit, define this symbol for the entire project
and this unit will be compiled in PurePascal mode.
}
{$IFDEF SimpleHash_PurePascal}
{$DEFINE PurePascal}
{$ENDIF}
//------------------------------------------------------------------------------
{$IF Defined(CPUX86_64) or Defined(CPUX64)}
{$DEFINE x64}
{$ELSEIF Defined(CPU386)}
{$DEFINE x86}
{$ELSE}
{$DEFINE PurePascal}
{$IFEND}
{$IF Defined(WINDOWS) or Defined(MSWINDOWS)}
{$DEFINE Windows}
{$IFEND}
{$IFDEF FPC}
{$MODE ObjFPC}{$MODESWITCH CLASSICPROCVARS+}
{$IFNDEF PurePascal}
{$ASMMODE Intel}
{$ENDIF}
{$ENDIF}
{$H+}
interface
uses
Classes,
AuxTypes, BasicUIM, HashBase;
{===============================================================================
Library-specific exceptions
===============================================================================}
type
ESHException = class(EHashException);
ESHIncompatibleClass = class(ESHException);
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in TSimpleHash32 and TSimpleHash64 are in memory always ordered from
least significant byte to most significant byte (little endian).
Types TSimpleHash32Sys and TSimpleHash64Sys have no such guarantee and their
endianness is system-dependent.
To convert the checksum in default ordering to a required specific ordering,
use methods SimpleHash32ToLE or SimpleHash64ToLe for little endian and
SimpleHash32ToBE or SimpleHash64ToBE for big endian. Note that these methods
are expecting the input value to be in default ordering, if it is not, the
result will be wrong. Be careful when using them.
}
type
TSimpleHash32 = packed array[0..3] of UInt8;
PSimpleHash32 = ^TSimpleHash32;
TSimpleHash64 = packed array[0..7] of UInt8;
PSimpleHash64 = ^TSimpleHash64;
TSimpleHash32Sys = UInt32;
PSimpleHash32Sys = ^TSimpleHash32Sys;
TSimpleHash64Sys = UInt64;
PSimpleHash64Sys = ^TSimpleHash64Sys;
const
{
Initial 32bit value (0x87CD219F) is a simple hash obtained when hashing
ASCII-encoded string "ROL and XOR, nothing more!" (without quotes) with
initial value of zero.
Similarly, 64bit initial value (0xC404A918F6E67CBD) is a hash of string
"ROL and XOR, nothing more, in 64!" (without quotes).
}
InitialSimpleHash32: TSimpleHash32 = ($9F,$21,$CD,$87);
InitialSimpleHash64: TSimpleHash64 = ($BD,$7C,$E6,$F6,$18,$A9,$04,$C4);
ZeroSimpleHash32: TSimpleHash32 = (0,0,0,0);
ZeroSimpleHash64: TSimpleHash64 = (0,0,0,0,0,0,0,0);
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash32Base
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSimpleHash32Base - class declaration
===============================================================================}
type
TSimpleHash32Base = class(TStreamHash)
protected
fInitialValue: TSimpleHash32Sys;
fSimpleHash32: TSimpleHash32Sys;
fImplManager: TImplementationManager;
fProcessBuffer: Function(Init: TSimpleHash32Sys; const Buffer; Size: TMemSize): TSimpleHash32Sys register;
Function GetInitialValue: TSimpleHash32; virtual;
Function GetSimpleHash32: TSimpleHash32; virtual;
Function GetHashImplementation: THashImplementation; override;
procedure SetHashImplementation(Value: THashImplementation); override;
procedure ProcessBuffer(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
procedure Finalize; override;
public
class Function SimpleHash32ToSys(Hash: TSimpleHash32): TSimpleHash32Sys; virtual;
class Function SimpleHash32FromSys(Hash: TSimpleHash32Sys): TSimpleHash32; virtual;
class Function SimpleHash32ToLE(Hash: TSimpleHash32): TSimpleHash32; virtual;
class Function SimpleHash32ToBE(Hash: TSimpleHash32): TSimpleHash32; virtual;
class Function SimpleHash32FromLE(Hash: TSimpleHash32): TSimpleHash32; virtual;
class Function SimpleHash32FromBE(Hash: TSimpleHash32): TSimpleHash32; virtual;
class Function HashImplementationsAvailable: THashImplementations; override;
class Function HashImplementationsSupported: THashImplementations; override;
class Function HashSize: TMemSize; override;
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSimpleHash32); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TSimpleHash32); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property InitialValue: TSimpleHash32 read GetInitialValue;
property InitialValueSys: TSimpleHash32Sys read fInitialValue;
property SimpleHash32: TSimpleHash32 read GetSimpleHash32;
property SimpleHash32Sys: TSimpleHash32Sys read fSimpleHash32;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash32Init
--------------------------------------------------------------------------------
===============================================================================}
{
This class provides the same processing (works with the same algorithm) as
TSimpleHash32Base, the only diffrence is that initial value of the calculated
hash is not set to zero, as is the case in base class, but to a predefined
non-zero value (see source code for more details).
It also allows you to change this initial value if you want to create your
own variant.
}
{===============================================================================
TSimpleHash32Init - class declaration
===============================================================================}
type
TSimpleHash32Init = class(TSimpleHash32Base)
protected
procedure Initialize; override;
procedure SetInitialValue(NewValue: TSimpleHash32); virtual;
public
class Function HashName: String; override;
property InitialValue: TSimpleHash32 read GetInitialValue write SetInitialValue;
property InitialValueSys: TSimpleHash32Sys read fInitialValue write fInitialValue;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash64Base
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSimpleHash64Base - class declaration
===============================================================================}
type
TSimpleHash64Base = class(TStreamHash)
protected
fInitialValue: TSimpleHash64Sys;
fSimpleHash64: TSimpleHash64Sys;
fImplManager: TImplementationManager;
fProcessBuffer: Function(Init: TSimpleHash64Sys; const Buffer; Size: TMemSize): TSimpleHash64Sys register;
Function GetInitialValue: TSimpleHash64; virtual;
Function GetSimpleHash64: TSimpleHash64; virtual;
Function GetHashImplementation: THashImplementation; override;
procedure SetHashImplementation(Value: THashImplementation); override;
procedure ProcessBuffer(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
procedure Finalize; override;
public
class Function SimpleHash64ToSys(Hash: TSimpleHash64): TSimpleHash64Sys; virtual;
class Function SimpleHash64FromSys(Hash: TSimpleHash64Sys): TSimpleHash64; virtual;
class Function SimpleHash64ToLE(Hash: TSimpleHash64): TSimpleHash64; virtual;
class Function SimpleHash64ToBE(Hash: TSimpleHash64): TSimpleHash64; virtual;
class Function SimpleHash64FromLE(Hash: TSimpleHash64): TSimpleHash64; virtual;
class Function SimpleHash64FromBE(Hash: TSimpleHash64): TSimpleHash64; virtual;
class Function HashImplementationsAvailable: THashImplementations; override;
class Function HashImplementationsSupported: THashImplementations; override;
class Function HashSize: TMemSize; override;
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
class Function HashName: String; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TSimpleHash64); overload; virtual;
procedure Init; override;
Function Compare(Hash: THashBase): Integer; override;
Function AsString: String; override;
procedure FromString(const Str: String); override;
procedure FromStringDef(const Str: String; const Default: TSimpleHash64); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property InitialValue: TSimpleHash64 read GetInitialValue;
property InitialValueSys: TSimpleHash64Sys read fInitialValue;
property SimpleHash64: TSimpleHash64 read GetSimpleHash64;
property SimpleHash64Sys: TSimpleHash64Sys read fSimpleHash64;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash64Init
--------------------------------------------------------------------------------
===============================================================================}
{
Similarly to 32bit variant, this class differs from TSimpleHash64Base only
in initial value of the hash.
}
{===============================================================================
TSimpleHash64Init - class declaration
===============================================================================}
type
TSimpleHash64Init = class(TSimpleHash64Base)
protected
procedure Initialize; override;
procedure SetInitialValue(NewValue: TSimpleHash64); virtual;
public
class Function HashName: String; override;
property InitialValue: TSimpleHash64 read GetInitialValue write SetInitialValue;
property InitialValueSys: TSimpleHash64Sys read fInitialValue write fInitialValue;
end;
{===============================================================================
--------------------------------------------------------------------------------
Procedural interface
--------------------------------------------------------------------------------
===============================================================================}
{
Some functions provided in procedural interface are only wrappers around
TSimpleHash32Init and TSimpleHash64Init classes and their methods - meaning,
among other facts, that they calculate the hash using non-zero initial value.
Function that are not wrappers around mentioned classes are (more-or-less)
dirrectly calling core implementation for the sake of better performance
(also using non-zero initial value, where applicable). These non-wrappers
are:
BufferSimpleHash[32/64] (both overloads)
AnsiStringSimpleHash[32/64]
WideStringSimpleHash[32/64]
StringSimpleHash[32/64]
SimpleHash[32/64]_Init
SimpleHash[32/64]_Update
SimpleHash[32/64]_Final (both overloads)
SimpleHash[32/64]_Hash
Note that these non-wrappers are always calling assembly code whenever it
is possible - there is no UIM switching awailable for them (unlike object
implementation).
}
{===============================================================================
Procedural interface - 32bit hash declaration
===============================================================================}
Function SimpleHash32ToStr(Hash: TSimpleHash32): String;
Function StrToSimpleHash32(const Str: String): TSimpleHash32;
Function TryStrToSimpleHash32(const Str: String; out Hash: TSimpleHash32): Boolean;
Function StrToSimpleHash32Def(const Str: String; Default: TSimpleHash32): TSimpleHash32;
Function CompareSimpleHash32(A,B: TSimpleHash32): Integer;
Function SameSimpleHash32(A,B: TSimpleHash32): Boolean;
//------------------------------------------------------------------------------
Function BufferSimpleHash32(Hash: TSimpleHash32; const Buffer; Size: TMemSize): TSimpleHash32; overload;
Function BufferSimpleHash32(const Buffer; Size: TMemSize): TSimpleHash32; overload;
Function AnsiStringSimpleHash32(const Str: AnsiString): TSimpleHash32;
Function WideStringSimpleHash32(const Str: WideString): TSimpleHash32;
Function StringSimpleHash32(const Str: String): TSimpleHash32;
Function StreamSimpleHash32(Stream: TStream; Count: Int64 = -1): TSimpleHash32;
Function FileSimpleHash32(const FileName: String): TSimpleHash32;
//------------------------------------------------------------------------------
type
TSimpleHash32Context = type Pointer;
Function SimpleHash32_Init: TSimpleHash32Context;
procedure SimpleHash32_Update(var Context: TSimpleHash32Context; const Buffer; Size: TMemSize);
Function SimpleHash32_Final(var Context: TSimpleHash32Context; const Buffer; Size: TMemSize): TSimpleHash32; overload;
Function SimpleHash32_Final(var Context: TSimpleHash32Context): TSimpleHash32; overload;
Function SimpleHash32_Hash(const Buffer; Size: TMemSize): TSimpleHash32;
{===============================================================================
Procedural interface - 64bit hash declaration
===============================================================================}
Function SimpleHash64ToStr(Hash: TSimpleHash64): String;
Function StrToSimpleHash64(const Str: String): TSimpleHash64;
Function TryStrToSimpleHash64(const Str: String; out Hash: TSimpleHash64): Boolean;
Function StrToSimpleHash64Def(const Str: String; Default: TSimpleHash64): TSimpleHash64;
Function CompareSimpleHash64(A,B: TSimpleHash64): Integer;
Function SameSimpleHash64(A,B: TSimpleHash64): Boolean;
//------------------------------------------------------------------------------
Function BufferSimpleHash64(Hash: TSimpleHash64; const Buffer; Size: TMemSize): TSimpleHash64; overload;
Function BufferSimpleHash64(const Buffer; Size: TMemSize): TSimpleHash64; overload;
Function AnsiStringSimpleHash64(const Str: AnsiString): TSimpleHash64;
Function WideStringSimpleHash64(const Str: WideString): TSimpleHash64;
Function StringSimpleHash64(const Str: String): TSimpleHash64;
Function StreamSimpleHash64(Stream: TStream; Count: Int64 = -1): TSimpleHash64;
Function FileSimpleHash64(const FileName: String): TSimpleHash64;
//------------------------------------------------------------------------------
type
TSimpleHash64Context = type Pointer;
Function SimpleHash64_Init: TSimpleHash64Context;
procedure SimpleHash64_Update(var Context: TSimpleHash64Context; const Buffer; Size: TMemSize);
Function SimpleHash64_Final(var Context: TSimpleHash64Context; const Buffer; Size: TMemSize): TSimpleHash64; overload;
Function SimpleHash64_Final(var Context: TSimpleHash64Context): TSimpleHash64; overload;
Function SimpleHash64_Hash(const Buffer; Size: TMemSize): TSimpleHash64;
implementation
uses
SysUtils;
{===============================================================================
Main implementation
===============================================================================}
Function SimpleHash32_PAS(Init: TSimpleHash32Sys; const Buffer; Size: TMemSize): TSimpleHash32Sys; register;
var
WorkPtr: PByte;
i: TMemSize;
begin
Result := Init;
If Size > 0 then
begin
WorkPtr := @Buffer;
For i := 0 to Pred(Size) do
begin
Result := TSimpleHash32Sys(TSimpleHash32Sys(Result shl 3) or
(Result shr 29)){ROL(3)} xor TSimpleHash32Sys(WorkPtr^);
Inc(WorkPtr);
end;
end;
end;
//------------------------------------------------------------------------------
Function SimpleHash64_PAS(Init: TSimpleHash64Sys; const Buffer; Size: TMemSize): TSimpleHash64Sys; register;
var
WorkPtr: PByte;
i: TMemSize;
begin
Result := Init;
If Size > 0 then
begin
WorkPtr := @Buffer;
For i := 0 to Pred(Size) do
begin
Result := TSimpleHash64Sys(TSimpleHash64Sys(Result shl 3) or
(Result shr 61){ROL(3)}) xor TSimpleHash64Sys(WorkPtr^);
Inc(WorkPtr);
end;
end;
end;
//------------------------------------------------------------------------------
{$IFNDEF PurePascal}
Function SimpleHash32_ASM(Init: TSimpleHash32Sys; const Buffer; Size: TMemSize): TSimpleHash32Sys; register; assembler;
asm
{-------------------------------------------------------------------------------
win32 & lin32 win64 lin64
Init EAX ECX EDI
@Buffer EDX RDX RSI
Size ECX R8 RDX
Result EAX EAX EAX
-------------------------------------------------------------------------------}
{$IFDEF x64}
{$IFDEF Windows}
TEST R8, R8
JZ @RoutineEnd
@ProcessingLoop:
ROL ECX, 3
MOVZX EAX, byte ptr [RDX]
XOR ECX, EAX
INC RDX
DEC R8
JNZ @ProcessingLoop
@RoutineEnd:
MOV EAX, ECX
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - -
TEST RDX, RDX
JZ @RoutineEnd
@ProcessingLoop:
ROL EDI, 3
MOVZX EAX, byte ptr [RSI]
XOR EDI, EAX
INC RSI
DEC RDX
JNZ @ProcessingLoop
@RoutineEnd:
MOV EAX, EDI
{$ENDIF}
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
JECXZ @RoutineEnd
@ProcessingLoop:
ROL EAX, 3
MOVZX EBX, byte ptr [EDX]
XOR EAX, EBX
INC EDX
DEC ECX
JNZ @ProcessingLoop
@RoutineEnd:
POP EBX
{$ENDIF}
end;
//------------------------------------------------------------------------------
Function SimpleHash64_ASM(Init: TSimpleHash64Sys; const Buffer; Size: TMemSize): TSimpleHash64Sys; register; assembler;
asm
{-------------------------------------------------------------------------------
win32 & lin32 win64 lin64
Init (EBP + 8) RCX RDI
@Buffer EAX RDX RSI
Size EDX R8 RDX
Result EDX:EAX RAX RAX
-------------------------------------------------------------------------------}
{$IFDEF x64}
{$IFDEF Windows}
TEST R8, R8
JZ @RoutineEnd
@ProcessingLoop:
ROL RCX, 3
MOVZX RAX, byte ptr [RDX]
XOR RCX, RAX
INC RDX
DEC R8
JNZ @ProcessingLoop
@RoutineEnd:
MOV RAX, RCX
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - -
TEST RDX, RDX
JZ @RoutineEnd
@ProcessingLoop:
ROL RDI, 3
MOVZX RAX, byte ptr [RSI]
XOR RDI, RAX
INC RSI
DEC RDX
JNZ @ProcessingLoop
@RoutineEnd:
MOV RAX, RDI
{$ENDIF}
{$ELSE}// - - - - - - - - - - - - - - - - - - - - - - - -
PUSH EBX
PUSH ESI
MOV ESI, EAX
MOV ECX, EDX
MOV EAX, dword ptr [Init]
MOV EDX, dword ptr [Init + 4]
JECXZ @RoutineEnd
@ProcessingLoop:
MOV EBX, EDX
SHLD EDX, EAX, 3
SHLD EAX, EBX, 3
MOVZX EBX, byte ptr [ESI]
XOR EAX, EBX
INC ESI
DEC ECX
JNZ @ProcessingLoop
@RoutineEnd:
POP ESI
POP EBX
{$ENDIF}
end;
{$ENDIF}
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash32Base
--------------------------------------------------------------------------------
===============================================================================}
Function SwapEndian(Value: TSimpleHash32Sys): TSimpleHash32Sys; overload;
begin
Result := TSimpleHash32Sys(
((Value and $000000FF) shl 24) or
((Value and $0000FF00) shl 8) or
((Value and $00FF0000) shr 8) or
((Value and $FF000000) shr 24));
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function SwapEndian(Value: TSimpleHash32): TSimpleHash32; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := TSimpleHash32(SwapEndian(TSimpleHash32Sys(Value)));
end;
{===============================================================================
TSimpleHash32Base - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSimpleHash32Base - protected methods
-------------------------------------------------------------------------------}
Function TSimpleHash32Base.GetInitialValue: TSimpleHash32;
begin
Result := SimpleHash32FromSys(fInitialValue);
end;
//------------------------------------------------------------------------------
Function TSimpleHash32Base.GetSimpleHash32: TSimpleHash32;
begin
Result := SimpleHash32FromSys(fSimpleHash32);
end;
//------------------------------------------------------------------------------
Function TSimpleHash32Base.GetHashImplementation: THashImplementation;
begin
Result := THashImplementation(fImplManager.RoutingFindObj(0).Selected);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.SetHashImplementation(Value: THashImplementation);
begin
fImplManager.RoutingFindObj(0).Select(TUIMIdentifier(Value));
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.ProcessBuffer(const Buffer; Size: TMemSize);
begin
fSimpleHash32 := fProcessBuffer(fSimpleHash32,Buffer,Size);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.Initialize;
begin
inherited;
fInitialValue := SimpleHash32ToSys(ZeroSimpleHash32);
fSimpleHash32 := fInitialValue;
fImplManager := TImplementationManager.Create;
// fill routings (also assigns fProcessBuffer)
AddRoutingSelect(fImplManager,0,@fProcessBuffer,[
ImplInfo(TUImIdentifier(hiPascal),@SimpleHash32_PAS)
{$IFNDEF PurePascal},
ImplInfo(TUImIdentifier(hiAssembly),@SimpleHash32_ASM)
],TUImIdentifier(hiAssembly));
{$ELSE}
],TUImIdentifier(hiPascal));
{$ENDIF}
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.Finalize;
begin
FreeAndNil(fImplManager);
inherited;
end;
{-------------------------------------------------------------------------------
TSimpleHash32Base - public methods
-------------------------------------------------------------------------------}
class Function TSimpleHash32Base.SimpleHash32ToSys(Hash: TSimpleHash32): TSimpleHash32Sys;
begin
Result := TSimpleHash32Sys({$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Hash));
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.SimpleHash32FromSys(Hash: TSimpleHash32Sys): TSimpleHash32;
begin
Result := TSimpleHash32({$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Hash));
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.SimpleHash32ToLE(Hash: TSimpleHash32): TSimpleHash32;
begin
Result := Hash;
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.SimpleHash32ToBE(Hash: TSimpleHash32): TSimpleHash32;
begin
Result := SwapEndian(Hash);
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.SimpleHash32FromLE(Hash: TSimpleHash32): TSimpleHash32;
begin
Result := Hash;
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.SimpleHash32FromBE(Hash: TSimpleHash32): TSimpleHash32;
begin
Result := SwapEndian(Hash);
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.HashImplementationsAvailable: THashImplementations;
begin
Result := [hiPascal{$IFNDEF PurePascal}, hiAssembly{$ENDIF}];
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.HashImplementationsSupported: THashImplementations;
begin
Result := [hiPascal{$IFNDEF PurePascal}, hiAssembly{$ENDIF}];
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.HashSize: TMemSize;
begin
Result := SizeOf(TSimpleHash32);
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.HashEndianness: THashEndianness;
begin
Result := heLittle;
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.HashFinalization: Boolean;
begin
Result := False;
end;
//------------------------------------------------------------------------------
class Function TSimpleHash32Base.HashName: String;
begin
Result := 'SimpleHash32Base';
end;
//------------------------------------------------------------------------------
constructor TSimpleHash32Base.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TSimpleHash32Base then
begin
fInitialValue := TSimpleHash32Base(Hash).InitialValueSys;
fSimpleHash32 := TSimpleHash32Base(Hash).SimpleHash32Sys;
end
else raise ESHIncompatibleClass.CreateFmt('TSimpleHash32Base.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TSimpleHash32Base.CreateAndInitFrom(Hash: TSimpleHash32);
begin
CreateAndInit;
fSimpleHash32 := SimpleHash32ToSys(Hash);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.Init;
begin
inherited;
fSimpleHash32 := fInitialValue;
end;
//------------------------------------------------------------------------------
Function TSimpleHash32Base.Compare(Hash: THashBase): Integer;
begin
If Hash is TSimpleHash32Base then
begin
If fSimpleHash32 > TSimpleHash32Base(Hash).SimpleHash32Sys then
Result := +1
else If fSimpleHash32 < TSimpleHash32Base(Hash).SimpleHash32Sys then
Result := -1
else
Result := 0;
end
else raise ESHIncompatibleClass.CreateFmt('TSimpleHash32Base.Compare: Incompatible class (%s).',[Hash.ClassName]);
end;
//------------------------------------------------------------------------------
Function TSimpleHash32Base.AsString: String;
begin
Result := IntToHex(fSimpleHash32,8);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.FromString(const Str: String);
begin
If Length(Str) > 0 then
begin
If Str[1] = '$' then
fSimpleHash32 := TSimpleHash32Sys(StrToInt(Str))
else
fSimpleHash32 := TSimpleHash32Sys(StrToInt('$' + Str));
end
else fSimpleHash32 := SimpleHash32ToSys(ZeroSimpleHash32);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.FromStringDef(const Str: String; const Default: TSimpleHash32);
begin
inherited FromStringDef(Str,Default);
If not TryFromString(Str) then
fSimpleHash32 := SimpleHash32ToSys(Default);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault);
var
Temp: TSimpleHash32;
begin
case Endianness of
heSystem: Temp := {$IFDEF ENDIAN_BIG}SimpleHash32ToBE{$ELSE}SimpleHash32ToLE{$ENDIF}(SimpleHash32FromSys(fSimpleHash32));
heLittle: Temp := SimpleHash32ToLE(SimpleHash32FromSys(fSimpleHash32));
heBig: Temp := SimpleHash32ToBE(SimpleHash32FromSys(fSimpleHash32));
else
{heDefault}
Temp := SimpleHash32FromSys(fSimpleHash32);
end;
Stream.WriteBuffer(Temp,SizeOf(TSimpleHash32));
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Base.LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault);
var
Temp: TSimpleHash32;
begin
Temp := ZeroSimpleHash32;
Stream.ReadBuffer(Temp,SizeOf(TSimpleHash32));
case Endianness of
heSystem: fSimpleHash32 := SimpleHash32ToSys({$IFDEF ENDIAN_BIG}SimpleHash32FromBE{$ELSE}SimpleHash32FromLE{$ENDIF}(Temp));
heLittle: fSimpleHash32 := SimpleHash32ToSys(SimpleHash32FromLE(Temp));
heBig: fSimpleHash32 := SimpleHash32ToSys(SimpleHash32FromBE(Temp));
else
{heDefault}
fSimpleHash32 := SimpleHash32ToSys(Temp);
end;
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash32Init
--------------------------------------------------------------------------------
===============================================================================}
{===============================================================================
TSimpleHash32Init - class declaration
===============================================================================}
{-------------------------------------------------------------------------------
TSimpleHash32Init - protected methods
-------------------------------------------------------------------------------}
procedure TSimpleHash32Init.Initialize;
begin
inherited;
fInitialValue := SimpleHash32ToSys(InitialSimpleHash32);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash32Init.SetInitialValue(NewValue: TSimpleHash32);
begin
fInitialValue := SimpleHash32ToSys(NewValue);
end;
{-------------------------------------------------------------------------------
TSimpleHash32Init - public methods
-------------------------------------------------------------------------------}
class Function TSimpleHash32Init.HashName: String;
begin
Result := 'SimpleHash32Init';
end;
{===============================================================================
--------------------------------------------------------------------------------
TSimpleHash64Base
--------------------------------------------------------------------------------
===============================================================================}
Function SwapEndian(Value: TSimpleHash64Sys): TSimpleHash64Sys; overload;
begin
Int64Rec(Result).Hi := UInt32(SwapEndian(TSimpleHash32Sys(Int64Rec(Value).Lo)));
Int64Rec(Result).Lo := UInt32(SwapEndian(TSimpleHash32Sys(Int64Rec(Value).Hi)));
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function SwapEndian(Value: TSimpleHash64): TSimpleHash64; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := TSimpleHash64(SwapEndian(TSimpleHash64Sys(Value)));
end;
{===============================================================================
TSimpleHash64Base - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TSimpleHash64Base - protected methods
-------------------------------------------------------------------------------}
Function TSimpleHash64Base.GetInitialValue: TSimpleHash64;
begin
Result := SimpleHash64FromSys(fInitialValue);
end;
//------------------------------------------------------------------------------
Function TSimpleHash64Base.GetSimpleHash64: TSimpleHash64;
begin
Result := SimpleHash64FromSys(fSimpleHash64);
end;
//------------------------------------------------------------------------------
Function TSimpleHash64Base.GetHashImplementation: THashImplementation;
begin
Result := THashImplementation(fImplManager.RoutingFindObj(0).Selected);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash64Base.SetHashImplementation(Value: THashImplementation);
begin
fImplManager.RoutingFindObj(0).Select(TUIMIdentifier(Value));
end;
//------------------------------------------------------------------------------
procedure TSimpleHash64Base.ProcessBuffer(const Buffer; Size: TMemSize);
begin
fSimpleHash64 := fProcessBuffer(fSimpleHash64,Buffer,Size);
end;
//------------------------------------------------------------------------------
procedure TSimpleHash64Base.Initialize;
begin
inherited;
fInitialValue := SimpleHash64ToSys(ZeroSimpleHash64);
fSimpleHash64 := fInitialValue;
fImplManager := TImplementationManager.Create;
AddRoutingSelect(fImplManager,0,@fProcessBuffer,[
ImplInfo(TUImIdentifier(hiPascal),@SimpleHash64_PAS)
{$IFNDEF PurePascal},
ImplInfo(TUImIdentifier(hiAssembly),@SimpleHash64_ASM)
],TUImIdentifier(hiAssembly));