-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Adler32.pas
731 lines (604 loc) · 21.8 KB
/
Adler32.pas
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
{-------------------------------------------------------------------------------
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/.
-------------------------------------------------------------------------------}
{===============================================================================
Adler-32 calculation
Version 1.2.3 (2020-07-13)
Last change 2024-04-14
©2018-2024 František Milt
Contacts:
František Milt: [email protected]
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.Adler32
Dependencies:
AuxTypes - github.com/TheLazyTomcat/Lib.AuxTypes
HashBase - github.com/TheLazyTomcat/Lib.HashBase
Indirect dependencies:
AuxClasses - github.com/TheLazyTomcat/Lib.AuxClasses
AuxExceptions - github.com/TheLazyTomcat/Lib.AuxExceptions
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 Adler32;
{$IFDEF FPC}
{$MODE ObjFPC}
{$INLINE ON}
{$DEFINE CanInline}
{$DEFINE FPC_DisableWarns}
{$MACRO ON}
{$ELSE}
{$IF CompilerVersion >= 17} // Delphi 2005+
{$DEFINE CanInline}
{$ELSE}
{$UNDEF CanInline}
{$IFEND}
{$ENDIF}
{$H+}
interface
uses
Classes,
AuxTypes, HashBase;
{===============================================================================
Common types and constants
===============================================================================}
{
Bytes in TAdler32 are always ordered from least significant byte to most
significant byte (little endian).
Type TAdler32Sys has no such guarantee and its endianness is system-dependent.
To convert the checksum in default ordering to a required specific ordering,
use methods Adler32ToLE for little endian and Adler32ToBE 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 carefull when using them.
}
type
TAdler32 = packed array[0..3] of UInt8;
PAdler32 = ^TAdler32;
TAdler32Sys = UInt32;
PAdler32Sys = ^TAdler32Sys;
const
InitialAdler32: TAdler32 = ($01,$00,$00,$00);
ZeroAdler32: TAdler32 = (0,0,0,0);
type
EADLER32Exception = class(EHASHException);
EADLER32IncompatibleClass = class(EADLER32Exception);
{-------------------------------------------------------------------------------
================================================================================
TAdler32Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TAdler32Hash - class declaration
===============================================================================}
type
TAdler32Hash = class(TStreamHash)
protected
fAdler32: TAdler32Sys;
Function GetAdler32: TAdler32; virtual;
procedure ProcessBuffer(const Buffer; Size: TMemSize); override;
procedure Initialize; override;
public
class Function Adler32ToSys(Adler32: TAdler32): TAdler32Sys; virtual;
class Function Adler32FromSys(Adler32: TAdler32Sys): TAdler32; virtual;
class Function Adler32ToLE(Adler32: TAdler32): TAdler32; virtual;
class Function Adler32ToBE(Adler32: TAdler32): TAdler32; virtual;
class Function Adler32FromLE(Adler32: TAdler32): TAdler32; virtual;
class Function Adler32FromBE(Adler32: TAdler32): TAdler32; virtual;
class Function HashSize: TMemSize; override;
class Function HashName: String; override;
class Function HashEndianness: THashEndianness; override;
class Function HashFinalization: Boolean; override;
constructor CreateAndInitFrom(Hash: THashBase); overload; override;
constructor CreateAndInitFrom(Hash: TAdler32); 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: TAdler32); reintroduce;
procedure SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
procedure LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault); override;
property Adler32: TAdler32 read GetAdler32;
property Adler32Sys: TAdler32Sys read fAdler32;
end;
{===============================================================================
Backward compatibility functions
===============================================================================}
Function Adler32ToStr(Adler32: TAdler32): String;
Function StrToAdler32(const Str: String): TAdler32;
Function TryStrToAdler32(const Str: String; out Adler32: TAdler32): Boolean;
Function StrToAdler32Def(const Str: String; Default: TAdler32): TAdler32;
Function CompareAdler32(A,B: TAdler32): Integer;
Function SameAdler32(A,B: TAdler32): Boolean;
//------------------------------------------------------------------------------
Function BufferAdler32(Adler32: TAdler32; const Buffer; Size: TMemSize): TAdler32; overload;
Function BufferAdler32(const Buffer; Size: TMemSize): TAdler32; overload;
Function AnsiStringAdler32(const Str: AnsiString): TAdler32;
Function WideStringAdler32(const Str: WideString): TAdler32;
Function StringAdler32(const Str: String): TAdler32;
Function StreamAdler32(Stream: TStream; Count: Int64 = -1): TAdler32;
Function FileAdler32(const FileName: String): TAdler32;
//------------------------------------------------------------------------------
type
TAdler32Context = type Pointer;
Function Adler32_Init: TAdler32Context;
procedure Adler32_Update(var Context: TAdler32Context; const Buffer; Size: TMemSize);
Function Adler32_Final(var Context: TAdler32Context; const Buffer; Size: TMemSize): TAdler32; overload;
Function Adler32_Final(var Context: TAdler32Context): TAdler32; overload;
Function Adler32_Hash(const Buffer; Size: TMemSize): TAdler32;
implementation
uses
SysUtils;
{$IFDEF FPC_DisableWarns}
{$DEFINE FPCDWM}
{$DEFINE W4055:={$WARN 4055 OFF}} // Conversion between ordinals and pointers is not portable
{$DEFINE W5057:={$WARN 5057 OFF}} // Local variable "$1" does not seem to be initialized
{$ENDIF}
{-------------------------------------------------------------------------------
================================================================================
TAdler32Hash
================================================================================
-------------------------------------------------------------------------------}
{===============================================================================
TAdler32Hash - utility functions
===============================================================================}
Function SwapEndian(Value: TAdler32Sys): TAdler32Sys; overload;
begin
Result := TAdler32Sys(
((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: TAdler32): TAdler32; overload;{$IFDEF CanInline} inline; {$ENDIF}
begin
Result := TAdler32(SwapEndian(TAdler32Sys(Value)));
end;
{===============================================================================
TAdler32Hash - calculation constants
===============================================================================}
const
Adler32Modulo = 65521;
Adler32NMRounds = 5552; // number of rounds that can be done without calculating modulo
{===============================================================================
TAdler32Hash - class implementation
===============================================================================}
{-------------------------------------------------------------------------------
TAdler32Hash - protected methods
-------------------------------------------------------------------------------}
Function TAdler32Hash.GetAdler32: TAdler32;
begin
Result := Adler32FromSys(fAdler32);
end;
//------------------------------------------------------------------------------
procedure TAdler32Hash.ProcessBuffer(const Buffer; Size: TMemSize);
var
SumA: UInt32;
SumB: UInt32;
Buff: PByte;
i: TMemSize;
begin
If Size > 0 then
begin
SumA := fAdler32 and $FFFF;
SumB := (fAdler32 shr 16) and $FFFF;
Buff := PByte(@Buffer);
// rounds with deferred modulo
while Size >= Adler32NMRounds do
begin
For i := 0 to Pred(Adler32NMRounds) do
begin
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
SumA := SumA + PByte(PtrUInt(Buff) + PtrUInt(i))^;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
SumB := SumB + SumA;
end;
SumA := SumA mod Adler32Modulo;
SumB := SumB mod Adler32Modulo;
Inc(Buff,Adler32NMRounds);
Dec(Size,Adler32NMRounds);
end;
// remaining bytes
If Size > 0 then
begin
For i := 0 to Pred(Size) do
begin
{$IFDEF FPCDWM}{$PUSH}W4055{$ENDIF}
SumA := (SumA + PByte(PtrUInt(Buff) + PtrUInt(i))^);
{$IFDEF FPCDWM}{$POP}{$ENDIF}
SumB := (SumB + SumA);
end;
SumA := SumA mod Adler32Modulo;
SumB := SumB mod Adler32Modulo;
end;
// construct result
fAdler32 := (SumB shl 16) or (SumA and $FFFF);
end;
end;
//------------------------------------------------------------------------------
procedure TAdler32Hash.Initialize;
begin
inherited;
fAdler32 := 0;
end;
{-------------------------------------------------------------------------------
TAdler32Hash - public methods
-------------------------------------------------------------------------------}
class Function TAdler32Hash.Adler32ToSys(Adler32: TAdler32): TAdler32Sys;
begin
Result := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(TAdler32Sys(Adler32));
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.Adler32FromSys(Adler32: TAdler32Sys): TAdler32;
begin
Result := TAdler32({$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Adler32));
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.Adler32ToLE(Adler32: TAdler32): TAdler32;
begin
Result := Adler32;
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.Adler32ToBE(Adler32: TAdler32): TAdler32;
begin
Result := SwapEndian(Adler32);
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.Adler32FromLE(Adler32: TAdler32): TAdler32;
begin
Result := Adler32;
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.Adler32FromBE(Adler32: TAdler32): TAdler32;
begin
Result := SwapEndian(Adler32);
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.HashSize: TMemSize;
begin
Result := SizeOf(TAdler32);
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.HashName: String;
begin
Result := 'Adler-32';
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.HashEndianness: THashEndianness;
begin
Result := heLittle;
end;
//------------------------------------------------------------------------------
class Function TAdler32Hash.HashFinalization: Boolean;
begin
Result := False;
end;
//------------------------------------------------------------------------------
constructor TAdler32Hash.CreateAndInitFrom(Hash: THashBase);
begin
inherited CreateAndInitFrom(Hash);
If Hash is TAdler32Hash then
fAdler32 := TAdler32Hash(Hash).Adler32Sys
else
raise EADLER32IncompatibleClass.CreateFmt('TAdler32Hash.CreateAndInitFrom: Incompatible class (%s).',[Hash.ClassName]);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
constructor TAdler32Hash.CreateAndInitFrom(Hash: TAdler32);
begin
CreateAndInit;
fAdler32 := Adler32ToSys(Hash);
end;
//------------------------------------------------------------------------------
procedure TAdler32Hash.Init;
begin
inherited;
fAdler32 := Adler32ToSys(InitialAdler32);
end;
//------------------------------------------------------------------------------
Function TAdler32Hash.Compare(Hash: THashBase): Integer;
begin
If Hash is TAdler32Hash then
begin
If fAdler32 > TAdler32Hash(Hash).Adler32Sys then
Result := +1
else If fAdler32 < TAdler32Hash(Hash).Adler32Sys then
Result := -1
else
Result := 0;
end
else raise EADLER32IncompatibleClass.CreateFmt('TAdler32Hash.Compare: Incompatible class (%s).',[Hash.ClassName]);
end;
//------------------------------------------------------------------------------
Function TAdler32Hash.AsString: String;
begin
Result := IntToHex(fAdler32,8);
end;
//------------------------------------------------------------------------------
procedure TAdler32Hash.FromString(const Str: String);
begin
If Length(Str) > 0 then
begin
If Str[1] = '$' then
fAdler32 := TAdler32Sys(StrToInt(Str))
else
fAdler32 := TAdler32Sys(StrToInt('$' + Str));
end
else fAdler32 := Adler32ToSys(InitialAdler32);
end;
//------------------------------------------------------------------------------
procedure TAdler32Hash.FromStringDef(const Str: String; const Default: TAdler32);
begin
inherited FromStringDef(Str,Default);
If not TryFromString(Str) then
fAdler32 := Adler32ToSys(Default);
end;
//------------------------------------------------------------------------------
procedure TAdler32Hash.SaveToStream(Stream: TStream; Endianness: THashEndianness = heDefault);
var
Temp: TAdler32;
begin
case Endianness of
heSystem: Temp := {$IFDEF ENDIAN_BIG}Adler32ToBE{$ELSE}Adler32ToLE{$ENDIF}(Adler32FromSys(fAdler32));
heLittle: Temp := Adler32ToLE(Adler32FromSys(fAdler32));
heBig: Temp := Adler32ToBE(Adler32FromSys(fAdler32));
else
{heDefault}
Temp := Adler32FromSys(fAdler32);
end;
Stream.WriteBuffer(Temp,SizeOf(TAdler32));
end;
//------------------------------------------------------------------------------
{$IFDEF FPCDWM}{$PUSH}W5057{$ENDIF}
procedure TAdler32Hash.LoadFromStream(Stream: TStream; Endianness: THashEndianness = heDefault);
var
Temp: TAdler32;
begin
Stream.ReadBuffer(Temp,SizeOf(TAdler32));
case Endianness of
heSystem: fAdler32 := Adler32ToSys({$IFDEF ENDIAN_BIG}Adler32FromBE{$ELSE}Adler32FromLE{$ENDIF}(Temp));
heLittle: fAdler32 := Adler32ToSys(Adler32FromLE(Temp));
heBig: fAdler32 := Adler32ToSys(Adler32FromBE(Temp));
else
{heDefault}
fAdler32 := Adler32ToSys(Temp);
end;
end;
{$IFDEF FPCDWM}{$POP}{$ENDIF}
{===============================================================================
Backward compatibility functions
===============================================================================}
{-------------------------------------------------------------------------------
Backward compatibility functions - utility functions
-------------------------------------------------------------------------------}
Function Adler32ToStr(Adler32: TAdler32): String;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.CreateAndInitFrom(Adler32);
try
Result := Hash.AsString;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function StrToAdler32(const Str: String): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.FromString(Str);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function TryStrToAdler32(const Str: String; out Adler32: TAdler32): Boolean;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Result := Hash.TryFromString(Str);
If Result then
Adler32 := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function StrToAdler32Def(const Str: String; Default: TAdler32): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.FromStringDef(Str,Default);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function CompareAdler32(A,B: TAdler32): Integer;
var
HashA: TAdler32Hash;
HashB: TAdler32Hash;
begin
HashA := TAdler32Hash.CreateAndInitFrom(A);
try
HashB := TAdler32Hash.CreateAndInitFrom(B);
try
Result := HashA.Compare(HashB);
finally
HashB.Free;
end;
finally
HashA.Free;
end;
end;
//------------------------------------------------------------------------------
Function SameAdler32(A,B: TAdler32): Boolean;
var
HashA: TAdler32Hash;
HashB: TAdler32Hash;
begin
HashA := TAdler32Hash.CreateAndInitFrom(A);
try
HashB := TAdler32Hash.CreateAndInitFrom(B);
try
Result := HashA.Same(HashB);
finally
HashB.Free;
end;
finally
HashA.Free;
end;
end;
{-------------------------------------------------------------------------------
Backward compatibility functions - processing functions
-------------------------------------------------------------------------------}
Function BufferAdler32(Adler32: TAdler32; const Buffer; Size: TMemSize): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.CreateAndInitFrom(Adler32);
try
Hash.Final(Buffer,Size);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function BufferAdler32(const Buffer; Size: TMemSize): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashBuffer(Buffer,Size);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function AnsiStringAdler32(const Str: AnsiString): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashAnsiString(Str);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function WideStringAdler32(const Str: WideString): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashWideString(Str);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function StringAdler32(const Str: String): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashString(Str);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function StreamAdler32(Stream: TStream; Count: Int64 = -1): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashStream(Stream,Count);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
//------------------------------------------------------------------------------
Function FileAdler32(const FileName: String): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashFile(FileName);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
{-------------------------------------------------------------------------------
Backward compatibility functions - context functions
-------------------------------------------------------------------------------}
Function Adler32_Init: TAdler32Context;
var
Temp: TAdler32Hash;
begin
Temp := TAdler32Hash.Create;
Temp.Init;
Result := TAdler32Context(Temp);
end;
//------------------------------------------------------------------------------
procedure Adler32_Update(var Context: TAdler32Context; const Buffer; Size: TMemSize);
begin
TAdler32Hash(Context).Update(Buffer,Size);
end;
//------------------------------------------------------------------------------
Function Adler32_Final(var Context: TAdler32Context; const Buffer; Size: TMemSize): TAdler32;
begin
Adler32_Update(Context,Buffer,Size);
Result := Adler32_Final(Context);
end;
//------------------------------------------------------------------------------
Function Adler32_Final(var Context: TAdler32Context): TAdler32;
begin
TAdler32Hash(Context).Final;
Result := TAdler32Hash(Context).Adler32;
FreeAndNil(TAdler32Hash(Context));
end;
//------------------------------------------------------------------------------
Function Adler32_Hash(const Buffer; Size: TMemSize): TAdler32;
var
Hash: TAdler32Hash;
begin
Hash := TAdler32Hash.Create;
try
Hash.HashBuffer(Buffer,Size);
Result := Hash.Adler32;
finally
Hash.Free;
end;
end;
end.