-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathJPL.ChecksumFile.pas
526 lines (421 loc) · 14.1 KB
/
JPL.ChecksumFile.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
unit JPL.ChecksumFile;
{
Jacek Pazera
http://www.pazera-software.com
https://github.com/jackdp
}
{$I .\..\jp.inc}
{$IFDEF FPC}{$MODE DELPHI}{$ENDIF}
{
TChecksumFile
Usage
A. Write checksum file
1. Create instance
2. Add some files with AddFile
3. Call SaveToFile and specify desired cheksum format.
4. Free instance
----------------------------
B. Read existsing checksum file
1. Create instance
2. Call LoadFromFile
3. If ValidFile = True, do something with Items
4. Free instance
}
interface
uses
SysUtils, Classes,
Generics.Collections,
JPL.Strings, JPL.Conversion, JPL.TStr, JPL.Hash.Common, JPL.Win.Dialogs;
type
TChecksumFormat = (cfDefault, cfBSD, cfSFV);
TChecksumItem = record
HashType: TJPHashType;
HashID: string;
FileName: string;
ExpandedFileName: string;
FileExists: Boolean;
Hash: string;
function AsDebugStr: string;
procedure ExpandFileName(const RootDir: string);
procedure CheckFileExists;
end;
TChecksumItems = TList<TChecksumItem>;
TChecksumFile = class
private
FValidFile: Boolean;
FItems: TChecksumItems;
FModeChar: Char;
FComment: TStringList;
FAddComment: Boolean;
FFileChecksumFormat: TChecksumFormat;
FRootDirectory: string;
procedure SetModeChar(const Value: Char);
procedure SetAddComment(const Value: Boolean);
function GetCommentStr(ChecksumFormat: TChecksumFormat = cfDefault): string;
function GetCount: integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
procedure AddFile(const FileName, Hash: string; HashType: TJPHashType);
function AsDebugStr: string;
function AsString(ChecksumFormat: TChecksumFormat = cfDefault): string;
procedure SaveToFile(const FileName: string; ChecksumFormat: TChecksumFormat = cfDefault);
function LoadFromFile(const FileName: string): Boolean;
property ValidFile: Boolean read FValidFile;
property ModeChar: Char read FModeChar write SetModeChar;
property Comment: TStringList read FComment;
property AddComment: Boolean read FAddComment write SetAddComment;
property Count: integer read GetCount;
property FileChecksumFormat: TChecksumFormat read FFileChecksumFormat;
property RootDirectory: string read FRootDirectory;
property Items: TChecksumItems read FItems;
end;
function HashTypeToHashID(const HashType: TJPHashType): string;
function HashIDToHashType(HashID: string): TJPHashType;
function GetHashTypeByFileExt(Ext: string): TJPHashType;
implementation
{$region ' helpers '}
function HashTypeToHashID(const HashType: TJPHashType): string;
var
s: string;
begin
Result := '';
s := HashTypeToStr(HashType, '');
if s = '' then Exit;
s := ReplaceFirst(s, 'SHA-2', 'SHA', True);
s := ReplaceFirst(s, 'SHA-3 ', 'SHA3--', True);
s := ReplaceFirst(s, '-', '');
s := RemoveSpaces(s);
Result := s;
end;
function HashIDToHashType(HashID: string): TJPHashType;
begin
Result := htNone;
HashID := TStr.TrimAndUp(HashID);
if HashID = '' then Exit;
if HashID = 'CRC16' then Result := htCrc16
else if HashID = 'CRC24' then Result := htCrc24
else if HashID = 'CRC32' then Result := htCrc32
else if HashID = 'CRC64' then Result := htCrc64
else if HashID = 'ADLER32' then Result := htAdler32
else if HashID = 'MD2' then Result := htMd2
else if HashID = 'MD5' then Result := htMd5
else if HashID = 'RIPEMD' then Result := htRipeMD
else if HashID = 'RIPEMD128' then Result := htRipeMD128
else if HashID = 'RIPEMD160' then Result := htRipeMD160
else if HashID = 'RIPEMD256' then Result := htRipeMD256
else if HashID = 'RIPEMD320' then Result := htRipeMD320
else if HashID = 'SHA0' then Result := htSha0
else if HashID = 'SHA1' then Result := htSha1
else if HashID = 'SHA224' then Result := htSha2_224
else if HashID = 'SHA256' then Result := htSha2_256
else if HashID = 'SHA384' then Result := htSha2_384
else if HashID = 'SHA512' then Result := htSha2_512
else if HashID = 'SHA512/224' then Result := htSha2_512_224
else if HashID = 'SHA512/256' then Result := htSha2_512_256
else if HashID = 'SHA3-224' then Result := htSha3_224
else if HashID = 'SHA3-256' then Result := htSha3_256
else if HashID = 'SHA3-384' then Result := htSha3_384
else if HashID = 'SHA3-512' then Result := htSha3_512
else if HashID = 'SHAKE128' then Result := htShake128
else if HashID = 'SHAKE256' then Result := htShake256
else if HashID = 'SNEFRU128' then Result := htSnefru128
else if HashID = 'SNEFRU256' then Result := htSnefru256
else if HashID = 'WHIRLPOOL' then Result := htWhirlpool;
end;
function GetHashTypeByFileExt(Ext: string): TJPHashType;
begin
Result := htNone;
Ext := TStr.TrimFromStart(Ext, '.');
Ext := TStr.TrimAndLow(Ext);
Ext := RemoveAll(Ext, '-');
Ext := RemoveAll(Ext, '_');
if Ext = '' then Exit;
if Ext = 'sfv' then Result := htCrc32
else if Ext = 'crc16' then Result := htCrc16
else if Ext = 'crc24' then Result := htCrc24
else if Ext = 'crc32' then Result := htCrc32
else if Ext = 'crc64' then Result := htCrc64
else if Ext = 'adler32' then Result := htAdler32
else if Ext = 'md2' then Result := htMd2
else if Ext = 'md4' then Result := htMd4
else if Ext = 'md5' then Result := htMd5
else if (Ext = 'ripemd') or (Ext = 'rmd') then Result := htRipeMD
else if (Ext = 'ripemd128') or (Ext = 'rmd128') then Result := htRipeMD128
else if (Ext = 'ripemd160') or (Ext = 'rmd160') then Result := htRipeMD160
else if (Ext = 'ripemd256') or (Ext = 'rmd256') then Result := htRipeMD256
else if (Ext = 'ripemd320') or (Ext = 'rmd320') then Result := htRipeMD320
else if Ext = 'sha0' then Result := htSha0
else if Ext = 'sha1' then Result := htSha1
else if Ext = 'sha224' then Result := htSha2_224
else if Ext = 'sha256' then Result := htSha2_256
else if Ext = 'sha384' then Result := htSha2_384
else if Ext = 'sha512' then Result := htSha2_512
else if Ext = 'sha512224' then Result := htSha2_512_224
else if Ext = 'sha512256' then Result := htSha2_512_256
else if Ext = 'sha3224' then Result := htSha3_224
else if Ext = 'sha3256' then Result := htSha3_256
else if Ext = 'sha3384' then Result := htSha3_384
else if Ext = 'sha3512' then Result := htSha3_512
else if Ext = 'shake128' then Result := htShake128
else if Ext = 'shake256' then Result := htShake256
else if Ext = 'snefru128' then Result := htSnefru128
else if Ext = 'snefru256' then Result := htSnefru256
else if Ext = 'whirlpool' then Result := htWhirlpool;
end;
{$endregion helpers}
constructor TChecksumFile.Create;
begin
inherited Create;
FModeChar := '*';
FItems := TChecksumItems.Create;
FComment := TStringList.Create;
FAddComment := True;
Clear;
end;
destructor TChecksumFile.Destroy;
begin
FItems.Clear;
FItems.Free;
FComment.Free;
inherited;
end;
procedure TChecksumFile.Clear;
begin
FValidFile := True;
FItems.Clear;
FComment.Clear;
FFileChecksumFormat := cfDefault;
FRootDirectory := ExtractFileDir(ParamStr(0));
end;
procedure TChecksumFile.SetAddComment(const Value: Boolean);
begin
FAddComment := Value;
end;
procedure TChecksumFile.SetModeChar(const Value: Char);
begin
FModeChar := Value;
end;
procedure TChecksumFile.AddFile(const FileName, Hash: string; HashType: TJPHashType);
var
Item: TChecksumItem;
begin
Item.HashType := HashType;
Item.HashID := HashTypeToHashID(HashType);
Item.FileName := FileName;
Item.ExpandFileName(FRootDirectory);
Item.CheckFileExists;
Item.Hash := Hash;
FItems.Add(Item);
end;
function TChecksumFile.GetCommentStr(ChecksumFormat: TChecksumFormat): string;
var
CommentChar: Char;
i: integer;
begin
Result := '';
if FComment.Count = 0 then Exit;
CommentChar := ';';
for i := 0 to FComment.Count - 1 do
Result := Result + CommentChar + FComment[i] + ENDL;
Result := Trim(Result);
end;
function TChecksumFile.GetCount: integer;
begin
Result := FItems.Count;
end;
function TChecksumFile.LoadFromFile(const FileName: string): Boolean;
var
sl: TStringList;
Item: TChecksumItem;
i, xp, xBracketOpenPos, xBracketClosePos: integer;
Line, s, Ext: string;
FirstChar: Char;
bValidItem: Boolean;
HashType: TJPHashType;
begin
Result := False;
Clear;
FValidFile := False;
if not FileExists(FileName) then Exit;
FRootDirectory := ExtractFileDir(FileName);
Ext := GetFileExt(FileName, True);
Ext := LowerCase(Ext);
sl := TStringList.Create;
try
sl.LoadFromFile(FileName);
for i := 0 to sl.Count - 1 do
begin
Line := Trim(sl[i]);
if Line = '' then Continue;
FirstChar := Line[1];
// ---------------- Comment --------------
if (FirstChar = ';') or (FirstChar = '#') then
begin
FComment.Add(Copy(Line, 2, Length(Line)));
Continue;
end;
bValidItem := False;
// --------------- BSD-style -----------------
// HashID (File Name) = HASH
// Eg.: SHA1 (PSGen_32bit_PORTABLE.zip) = 475d8c9d2526967a54e769f7ef4e8f465af38810
xBracketOpenPos := TStr.FirstCharPos(Line, '(');
xBracketClosePos := TStr.LastCharPos(Line, ')');
if (xBracketOpenPos > 0) and (xBracketClosePos > xBracketOpenPos) and (TStr.PosOf('=', Line) > xBracketClosePos) and (TStr.CharCount(Line, ' ') >= 3) then
begin
xp := Pos(' ', Line);
s := Copy(Line, 1, xp - 1);
s := Trim(s);
HashType := HashIDToHashType(s);
if HashType <> htNone then
begin
Item.HashType := HashType;
Item.HashID := s;
Line := Copy(Line, xp + 1, Length(Line));
Line := Trim(Line);
// Pozostało: '(file name) = HASH'
if not TStr.StartsStr('(', Line) then Continue;
Line := Copy(Line, 2, Length(Line));
xp := TStr.LastCharPos(Line, ')');
Item.FileName := Copy(Line, 1, xp - 1);
Line := Copy(Line, xp + 1, Length(Line));
// Pozostało: ' = HASH'
Line := RemoveSpaces(Line);
if not TStr.StartsStr('=', Line) then Continue;
Line := Copy(Line, 2, Length(Line));
if not TStr.IsValidHexStr(Line) then Continue;
Item.Hash := Line;
Item.ExpandFileName(FRootDirectory);
Item.CheckFileExists;
bValidItem := True;
FFileChecksumFormat := cfBSD;
FItems.Add(Item);
end;
end;
// file name HASH <-- SFV
// OR
// HASH *file name <-- eg. md5sum.exe
if not bValidItem then
begin
Line := Trim(sl[i]);
xp := TStr.LastCharPos(Line, ' ');
if xp = 0 then Continue;
if Ext = 'sfv' then
begin
s := Trim(Copy(Line, 1, xp - 1));
Line := Copy(Line, xp + 1, Length(Line));
TStr.TrimFromStart(Line, '*');
if TStr.IsValidHexStr(Line) and IsValidHashLen(Line, htCrc32) then
begin
Item.HashType := htCrc32;
Item.HashID := 'CRC32';
Item.FileName := s;
Item.Hash := Line;
Item.ExpandFileName(FRootDirectory);
Item.CheckFileExists;
FItems.Add(Item);
bValidItem := True;
FFileChecksumFormat := cfSFV;
end;
end;
// .md5, .sha1, .sha224 ...
if not bValidItem then
begin
Line := Trim(sl[i]);
HashType := GetHashTypeByFileExt(Ext);
if HashType = htNone then Continue;
xp := TStr.FirstCharPos(Line, ' ');
if xp = 0 then Continue;
s := Trim(Copy(Line, 1, xp - 1));
Line := Trim(Copy(Line, xp + 1, Length(Line)));
Line := TStr.TrimFromStart(Line, '*');
if not TStr.IsValidHexStr(s) then Continue;
Item.HashType := HashType;
Item.HashID := HashTypeToHashID(HashType);
Item.FileName := Line;
Item.Hash := s;
Item.ExpandFileName(FRootDirectory);
Item.CheckFileExists;
FItems.Add(Item);
bValidItem := True;
FFileChecksumFormat := cfDefault;
end;
end;
if not FValidFile then FValidFile := bValidItem;
end; // for i
Result := FValidFile;
finally
sl.Free;
end;
end;
function TChecksumFile.AsString(ChecksumFormat: TChecksumFormat = cfDefault): string;
var
i: integer;
Item: TChecksumItem;
sl: TStringList;
Line: string;
begin
sl := TStringList.Create;
try
for i := 0 to FItems.Count - 1 do
begin
Item := FItems[i];
case ChecksumFormat of
cfDefault: Line := Item.Hash + ' ' + FModeChar + Item.FileName;
cfBSD: Line := Item.HashID + ' (' + Item.FileName + ') = ' + Item.Hash;
cfSFV: Line := Item.FileName + ' ' + Item.Hash;
end;
sl.Add(Line);
end;
Result := Trim(sl.Text);
finally
sl.Free;
end;
end;
procedure TChecksumFile.SaveToFile(const FileName: string; ChecksumFormat: TChecksumFormat = cfDefault);
var
sl: TStringList;
begin
sl := TStringList.Create;
try
if FAddComment and (FComment.Count > 0) then sl.Add(GetCommentStr(ChecksumFormat));
sl.Add(AsString(ChecksumFormat));
sl.SaveToFile(FileName, TEncoding.UTF8);
finally
sl.Free;
end;
end;
function TChecksumFile.AsDebugStr: string;
var
s: string;
i: integer;
begin
s := '';
if FComment.Count > 0 then s := s + 'COMMENT' + ENDL + FComment.Text + ENDL;
s := s + 'FILES: ' + itos(Count) + ENDL + ENDL;
for i := 0 to FItems.Count - 1 do
s := s + 'File ' + itos(i + 1) + ENDL + FItems[i].AsDebugStr + ENDL + '----------------------------------------' + ENDL;
Result := s;
end;
{ TChecksumItem }
function TChecksumItem.AsDebugStr: string;
begin
Result :=
'File name: ' + FileName + ENDL +
'Expanded file name: ' + ExpandedFileName + ENDL +
'File exists: ' + BoolToStrYesNo(FileExists) + ENDL +
'HashType: ' + HashTypeToStr(HashType) + ENDL +
'HashID: ' + HashID + ENDL +
'Hash: ' + Hash;
end;
procedure TChecksumItem.CheckFileExists;
begin
FileExists := SysUtils.FileExists(ExpandedFileName);
end;
procedure TChecksumItem.ExpandFileName(const RootDir: string);
begin
if PathIsAbsolute(FileName) then ExpandedFileName := FileName
else ExpandedFileName := TStr.RemoveTrailingPathDelimiter(RootDir) + PathDelim + FileName;
end;
end.