-
Notifications
You must be signed in to change notification settings - Fork 6
/
CommonUtilities.pas
760 lines (675 loc) · 19.5 KB
/
CommonUtilities.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
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
unit CommonUtilities;
interface
uses
Winapi.Windows, Classes, Controls, ComCtrls, SysUtils, TypInfo, Winapi.Messages, Winapi.ImageHlp,
StrUtils;
type
SysInt = NativeInt;
SysUInt = NativeUInt;
PSysInt = PNativeInt;
PSysUInt = PNativeUInt;
THotKey = packed record
case Integer of
1:(
ControlKey: Byte;
Reserved1: Byte;
Key: Byte;
Reserved2: Byte;
);
2:(
AsDWORD: DWord;
);
end;
Int = packed record
case Integer of
1: ( ULo, UHi: Word );
2: ( SLo, SHi: Smallint );
3: ( Int: LongInt );
4: ( Dword: DWORD );
end;
// TSortOrder = (soNone, soASC, soDESC);
TStringArray = array of string;
procedure ErrorBox(const Message: string);
procedure MsgBox(const Message: string);
function MakeHotKey(Key: Word; ControlKey: TShiftState): THotKey;
//procedure ListViewStdKeyDownProc(Sender: TObject; var Key: Word; Shift: TShiftState);
function RecurseGetProperty(AObj: TPersistent; const APropName: string): string;
function RecurseSetProperty(AObj: TPersistent; const APropName, APropValue: string): Boolean;
function Parse(var S: string; const Separator: string): string;
function CBool(N: Cardinal): Cardinal; inline;
function GetShiftState: TShiftState;
procedure FreezeControl(AControl: TWinControl);
procedure UnfreezeControl(AControl: TWinControl);
function MyIntToHex(N: Cardinal): string;
function MyIntToHex64(N: UInt64): string;
function MyOrdinal(N: Int64): string;
function UndecorateCPPName(const Name: string): string;
function MakeRect(Left, Top, Right, Bottom: Integer): TRect; inline;
function MakePoint(X, Y: Integer): TPoint; inline;
function LangDesc(Lang: DWORD): string;
procedure CheckLastError;
function FileTimeToDateTime(AFileTime: TFileTime): TDateTime;
function GluePath(const Pieces: array of string): string;
function GetFileList(
const Dir: string; const Mask: string; IncludeSubDirs: Boolean = False): TStringArray;
function GetLangString(ALang: Integer): string;
function GetPluginFileName: string;
function CRC32(CRC: Cardinal; Buffer: PByte; Len: Cardinal): Cardinal;
function IIF(ACondition: Boolean; ATrue, AFalse: Pointer): Pointer; inline; overload;
function IIF(ACondition: Boolean; ATrue, AFalse: Int64): Int64; inline; overload;
function IIF(ACondition: Boolean; ATrue, AFalse: UInt64): UInt64; inline; overload;
{$IFNDEF WIN64}
function IIF(ACondition: Boolean; ATrue, AFalse: Integer): Integer; inline; overload;
{$ENDIF}
function GetFileSize(const AFileName: string): UInt64;
procedure ReadBufferFromFile(
const AFileName: string; ABuffer: Pointer; ASize: Integer);
const
SMsgBoxTitle = 'PE Viewer';
MK_ALT = $20;
HK_C_TAB: THotKey = (ControlKey: MK_CONTROL; Key: VK_TAB);
HK_CS_TAB: THotKey = (ControlKey: MK_CONTROL or MK_SHIFT; Key: VK_TAB);
HK_AC_TAB: THotKey = (ControlKey: MK_ALT or MK_CONTROL; Key: VK_TAB);
HK_ACS_TAB: THotKey = (ControlKey: MK_ALT or MK_CONTROL or MK_SHIFT; Key: VK_TAB);
HK_C_C: THotKey = (ControlKey: MK_CONTROL; Key: Ord('C'));
HK_C_A: THotKey = (ControlKey: MK_CONTROL; Key: Ord('A'));
HK_TAB: THotKey = (Key: VK_TAB);
HK_S_TAB: THotKey = (ControlKey: MK_SHIFT; Key: VK_TAB);
type
{$M+}
TStringStore = class
public
constructor Create; virtual;
function GetString(const AStringName: string): string;
function ConvertNameToValue(const AFieldName: string): string;
end;
{$M-}
implementation
{ TStringStore }
function TStringStore.ConvertNameToValue(const AFieldName: string): string;
begin
Result := ReplaceStr(AFieldName, '_', ' ');
end;
constructor TStringStore.Create;
var
pl: PPropList;
cnt, i: Integer;
pi: PPropInfo;
begin
cnt := GetPropList(Self, pl);
try
for i := 0 to cnt - 1 do
begin
pi := pl[i];
if (pi.PropType^.Kind in [tkString, tkUString, tkChar, tkWChar]) then
begin
SetPropValue(Self, pi, ConvertNameToValue(GetPropName(pi)));
end;
end;
finally
FreeMem(pl);
end;
end;
function TStringStore.GetString(const AStringName: string): string;
var
pi: PPropInfo;
begin
Result := ConvertNameToValue(AStringName);
pi := GetPropInfo(Self, AStringName, [tkString, tkUString]);
if (pi <> nil) then
Result := GetPropValue(Self, pi, True);
end;
procedure ErrorBox(const Message: string);
begin
MessageBox(0, PChar(Message), SMsgBoxTitle, MB_OK or MB_ICONINFORMATION or MB_TASKMODAL);
end;
procedure MsgBox(const Message: string);
begin
MessageBox(0, PChar(Message), SMsgBoxTitle, MB_OK or MB_ICONINFORMATION or MB_TASKMODAL);
end;
function MakeHotKey(Key: Word; ControlKey: TShiftState): THotKey;
begin
Result.AsDWORD := 0;
Result.Key := Key;
if (ssShift in ControlKey) then
Result.ControlKey := Result.ControlKey or MK_SHIFT;
if (ssCtrl in ControlKey) then
Result.ControlKey := Result.ControlKey or MK_CONTROL;
if (ssAlt in ControlKey) then
Result.ControlKey := Result.ControlKey or MK_ALT;
end;
{procedure ListViewStdKeyDownProc(Sender: TObject; var Key: Word; Shift: TShiftState);
var
hk: THotKey;
s: string;
i: Integer;
item: TListItem;
lv: TListView;
begin
Exit;
lv := Sender as TListView;
hk := MakeHotKey(Key, Shift);
if (hk.AsDWORD = HK_C_C.AsDWORD) then
begin
s := '';
if (lv.SelCount > 0) then
begin
for i := 0 to lv.Items.Count - 1 do
begin
item := lv.Items[i];
if (item.Selected) then
begin
s := s + item.Caption + ': ' + Trim(item.SubItems.DelimitedText) + #13#10;
end;
end;
end
else
begin
for i := 0 to lv.Items.Count - 1 do
begin
item := lv.Items[i];
s := s + item.Caption + ': ' + Trim(item.SubItems.DelimitedText) + #13#10;
end;
end;
Clipboard.AsText := s;
end
else if (hk.AsDWORD = HK_C_A.AsDWORD) then
begin
for i := 0 to lv.Items.Count - 1 do
begin
item := lv.Items[i];
item.Selected := True;
end;
end;
end;}
function RecurseSetProperty(AObj: TPersistent; const APropName, APropValue: string): Boolean;
var
dp, bp, collectionIndex: Integer;
curPropName, newPropName: string;
pi: PPropInfo;
newObj: TPersistent;
begin
Result := False;
dp := Pos('.', APropName);
try
if (dp > 0) then
begin
curPropName := Copy(APropName, 1, dp - 1);
newPropName := Copy(APropName, dp + 1, MaxInt);
bp := Pos('[', curPropName);
if (bp > 0) then
begin
if (curPropName[Length(curPropName)] <> ']') then
begin
Result := False;
Exit;
end;
collectionIndex := StrToInt(Copy(curPropName, bp + 1, Length(curPropName) - bp - 1));
curPropName := Copy(curPropName, 1, bp - 1);
end
else
collectionIndex := -1;
pi := GetPropInfo(AObj, curPropName);
if (pi <> nil) then
newObj := TPersistent(GetObjectProp(AObj, curPropName, TPersistent))
else
newObj := TPersistent(AObj.FieldAddress(curPropName)^);
if (collectionIndex >= 0) then
newObj := (newObj as TCollection).Items[collectionIndex];
Result := RecurseSetProperty(newObj, newPropName, APropValue);
end
else
begin
pi := GetPropInfo(AObj, APropName);
if (pi <> nil) and not (pi.PropType^.Kind in [tkClass, tkClassRef]) then
begin
SetPropValue(AObj, pi, APropValue);
Result := True;
end;
end;
except
Result := False;
end;
end;
function RecurseGetProperty(AObj: TPersistent; const APropName: string): string;
var
dp, bp, collectionIndex: Integer;
curPropName, newPropName: string;
pi: PPropInfo;
newObj: TPersistent;
begin
dp := Pos('.', APropName);
try
if (dp > 0) then
begin
curPropName := Copy(APropName, 1, dp - 1);
newPropName := Copy(APropName, dp + 1, MaxInt);
bp := Pos('[', curPropName);
if (bp > 0) then
begin
if (curPropName[Length(curPropName)] <> ']') then
begin
Result := '';
Exit;
end;
collectionIndex := StrToInt(Copy(curPropName, bp + 1, Length(curPropName) - bp - 1));
curPropName := Copy(curPropName, 1, bp - 1);
end
else
collectionIndex := -1;
pi := GetPropInfo(AObj, curPropName);
if (pi <> nil) then
newObj := TPersistent(GetObjectProp(AObj, curPropName, TPersistent))
else
newObj := TPersistent(AObj.FieldAddress(curPropName)^);
if (collectionIndex >= 0) then
newObj := (newObj as TCollection).Items[collectionIndex];
Result := RecurseGetProperty(newObj, newPropName);
end
else
begin
pi := GetPropInfo(AObj, APropName);
if (pi <> nil) and not (pi.PropType^.Kind in [tkClass, tkClassRef]) then
Result := GetPropValue(AObj, pi, True)
else
Result := '';
end;
except
Result := '';
end;
end;
function Parse(var S: string; const Separator: string): string;
var
p: Integer;
begin
p := Pos(Separator, S);
if (p <= 0) then
p := Length(S);
Result := Copy(S, 1, p - 1);
Delete(S, 1, p);
end;
function CBool(N: Cardinal): Cardinal;
begin
if (N = 0) then
Result := N
else
Result := 1;
end;
function GetShiftState: TShiftState;
begin
Result := [];
if GetKeyState(VK_SHIFT) < 0 then Include(Result, ssShift);
if GetKeyState(VK_CONTROL) < 0 then Include(Result, ssCtrl);
if GetKeyState(VK_MENU) < 0 then Include(Result, ssAlt);
end;
procedure FreezeControl(AControl: TWinControl);
begin
AControl.Perform(WM_SETREDRAW, 0, 0);
end;
procedure UnfreezeControl(AControl: TWinControl);
begin
AControl.Perform(WM_SETREDRAW, 1, 0);
end;
function MyIntToHex(N: Cardinal): string;
begin
Result := '0x' + IntToHex(N, 8);
end;
function MyIntToHex64(N: UInt64): string;
begin
Result := '0x' + IntToHex(N, 16);
end;
function MyOrdinal(N: Int64): string;
begin
if (N = 0) then
Result := ''
else
Result := IntToStr(N);
end;
function UndecorateCPPName(const Name: string): string;
var
s: AnsiString;
begin
SetLength(s, 512);
SetLength(
s,
UnDecorateSymbolName(PAnsiChar(AnsiString(Name)), @s[1], 512, UNDNAME_COMPLETE));
Result := string(s);
end;
function MakeRect(Left, Top, Right, Bottom: Integer): TRect;
begin
Result.Left := Left;
Result.Top := Top;
Result.Right := Right;
Result.Bottom := Bottom;
end;
function MakePoint(X, Y: Integer): TPoint;
begin
Result.X := X;
Result.Y := Y;
end;
function LangDesc(Lang: DWORD): string;
begin
SetLength(Result, 513);
SetLength(Result, VerLanguageName(Lang, @Result[1], 512));
end;
procedure CheckLastError;
var
ec: Cardinal;
begin
ec := GetLastError;
if (ec <> 0) then
RaiseLastOSError(ec);
end;
function FileTimeToDateTime(AFileTime: TFileTime): TDateTime;
var
ModifiedTime: TFileTime;
SystemTime: TSystemTime;
begin
Result := 0;
if (Int64(AFileTime) = 0) then
Exit;
try
FileTimeToLocalFileTime(AFileTime, ModifiedTime);
FileTimeToSystemTime(ModifiedTime, SystemTime);
Result := SystemTimeToDateTime(SystemTime);
except
Result := Now; // Something to return in case of error
end;
end;
function GluePath(const Pieces: array of string): string;
var
i, l: Integer;
s: string;
begin
Result := '';
l := Low(Pieces);
for i := l to High(Pieces) do
begin
if (i > l) then
begin
s := Pieces[i];
if (s <> '') and (s[1] = PathDelim) then
raise Exception.Create('Invalid path piece');
if (Result <> '') and not IsPathDelimiter(Result, Length(Result)) then
Result := Result + PathDelim;
Result := Result + s;
end
else
Result := Pieces[i];
end;
end;
procedure DynArrayAppend(var A: Pointer; const Append: Pointer; ItemsTypeInfo: Pointer); overload;
var
l, la: Integer;
begin
la := DynArraySize(Append);
if (la > 0) then
begin
l := DynArraySize(A);
SetLength(TBoundArray(A), l + la);
if (ItemsTypeInfo <> nil) then
CopyArray(@TBoundArray(A)[l], @TBoundArray(Append)[0], ItemsTypeInfo, la)
else
Move(TBoundArray(Append)[0], TBoundArray(A)[l], la * SizeOf(TBoundArray(A)[0]));
end;
end;
procedure DynArrayAppend(var A: TStringArray; const Append: TStringArray); overload;
begin
DynArrayAppend(Pointer(A), Pointer(Append), TypeInfo(string));
end;
procedure DynArrayInsert(var A: Pointer; const Piece: Pointer; Idx: Integer; ItemsTypeInfo: Pointer);
const
isz = SizeOf(TBoundArray(A)[0]);
var
l, la: Integer;
begin
la := Length(TBoundArray(Piece));
if (la > 0) then
begin
if (Idx < 0) or (Idx > (la - 1)) then
raise Exception.Create('Index %d is out of bounds');
l := Length(TBoundArray(A));
if (idx = l) then
begin
DynArrayAppend(A, Piece, ItemsTypeInfo);
end
else
begin
SetLength(TBoundArray(A), l + la);
if (ItemsTypeInfo <> nil) then
begin
CopyArray(@TBoundArray(A)[Idx + la], @TBoundArray(A)[Idx], ItemsTypeInfo, l - idx);
CopyArray(@TBoundArray(A)[Idx], @TBoundArray(Piece)[0], ItemsTypeInfo, la);
end
else
begin
Move(TBoundArray(A)[Idx], TBoundArray(A)[Idx + la], (l - idx) * isz);
Move(TBoundArray(Piece)[0], TBoundArray(A)[Idx], la * isz);
end;
end;
end;
end;
procedure DynArrayAddItem(var A: TStringArray; Item: string);
var
l: Integer;
begin
l := Length(TStringArray(A));
SetLength(TStringArray(A), l + 1);
TStringArray(A)[l] := Item;
end;
function GetFileList(const Dir: string; const Mask: string; IncludeSubDirs: Boolean): TStringArray;
var
sr: TSearchRec;
r, i: Integer;
s, fn: string;
subDirItems: TStringArray;
begin
SetLength(Result, 0);
if (Dir <> '') then
begin
s := IncludeTrailingPathDelimiter(Dir);
r := FindFirst(s + Mask, faAnyFile and not faDirectory, sr);
while (r = 0) do
begin
if (sr.Attr and faDirectory <> faDirectory) then
begin
fn := s + sr.Name;
DynArrayAddItem(Result, sr.Name);
end;
r := FindNext(sr);
end;
SysUtils.FindClose(sr);
if IncludeSubDirs then
begin
r := FindFirst(s + '*.*', faDirectory, sr);
while (r = 0) do
begin
if (sr.Attr and faDirectory = faDirectory) and (sr.Name <> '.') and (sr.Name <> '..') then
begin
fn := s + sr.Name;
subDirItems := GetFileList(fn, Mask, IncludeSubDirs);
if (Length(subDirItems) > 0) then
begin
for i := 0 to Length(subDirItems) - 1 do
subDirItems[i] := sr.Name + PathDelim + subDirItems[i];
DynArrayAppend(Result, subDirItems);
end;
end;
r := FindNext(sr);
end;
SysUtils.FindClose(sr);
end;
end;
end;
function GetLangString(ALang: Integer): string;
begin
Result := IntToStr(ALang) + ' (0x' + IntToHex(ALang, 4) + ') ' + LangDesc(ALang);
end;
var
PluginFileName: string;
function GetPluginFileName: string;
begin
if (PluginFileName = '') then
begin
SetLength(PluginFileName, MAX_PATH);
SetLength(PluginFileName, GetModuleFileName(HInstance, @PluginFilename[1], MAX_PATH));
end;
Result := PluginFileName;
end;
var
// used to calculate the running CRC of a bunch of bytes,
// this table is dynamically created in order to save space if never needed
CRCTable: array of Cardinal;
procedure MakeCRCTable;
// creates the CRC table when it is needed the first time
var
C: Cardinal;
N, K : Integer;
Poly: Cardinal; // polynomial exclusive-or pattern
const
// terms of polynomial defining this CRC (except x^32)
P: array [0..13] of Byte = (0, 1, 2, 4, 5, 7, 8, 10, 11, 12, 16, 22, 23, 26);
begin
// make exclusive-or pattern from polynomial ($EDB88320)
SetLength(CRCTable, 256);
Poly:=0;
for N:=0 to SizeOf(P) - 1 do
Poly:=Poly or (1 shl (31 - P[N]));
for N:=0 to 255 do
begin
C:=N;
for K:=0 to 7 do
begin
if (C and 1)<>0 then C:=Poly xor (C shr 1)
else C:=C shr 1;
end;
CRCTable[N]:=C;
end;
end;
function CRC32(CRC: Cardinal; Buffer: PByte; Len: Cardinal): Cardinal;
// Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
// x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
//
// Polynomials over GF(2) are represented in binary, one bit per coefficient,
// with the lowest powers in the most significant bit. Then adding polynomials
// is just exclusive-or, and multiplying a polynomial by x is a right shift by
// one. If we call the above polynomial p, and represent a byte as the
// polynomial q, also with the lowest power in the most significant bit (so the
// byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
// where a mod b means the remainder after dividing a by b.
//
// This calculation is done using the shift-register method of multiplying and
// taking the remainder. The register is initialized to zero, and for each
// incoming bit, x^32 is added mod p to the register if the bit is a one (where
// x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
// x (which is shifting right by one and adding x^32 mod p if the bit shifted
// out is a one). We start with the highest power (least significant bit) of
// q and repeat for all eight bits of q.
//
// The table is simply the CRC of all possible eight bit values. This is all
// the information needed to generate CRC's on data a byte at a time for all
// combinations of CRC register values and incoming bytes.
begin
if Buffer = nil then Result:=0
else
begin
if CRCTable = nil then MakeCRCTable;
CRC:=CRC xor $FFFFFFFF;
while Len >= 8 do
begin
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
CRC:=CRCTable[Byte(CRC) xor Buffer^] xor (CRC shr 8);
Inc(Buffer);
Dec(Len, 8);
end;
while Len > 0 do
begin
CRC:=CRCTable[(CRC xor Buffer^) and $FF] xor (CRC shr 8);
Inc(Buffer);
Dec(Len);
end;
Result:=CRC xor $FFFFFFFF;
end;
end;
function IIF(ACondition: Boolean; ATrue, AFalse: Pointer): Pointer; inline;
begin
if (ACondition) then
Result := ATrue
else
Result := AFalse;
end;
function IIF(ACondition: Boolean; ATrue, AFalse: Int64): Int64; inline;
begin
if (ACondition) then
Result := ATrue
else
Result := AFalse;
end;
function IIF(ACondition: Boolean; ATrue, AFalse: UInt64): UInt64; inline;
begin
if (ACondition) then
Result := ATrue
else
Result := AFalse;
end;
{$IFNDEF WIN64}
function IIF(ACondition: Boolean; ATrue, AFalse: Integer): Integer; inline;
begin
if (ACondition) then
Result := ATrue
else
Result := AFalse;
end;
{$ENDIF}
function GetFileSize(const AFileName: string): UInt64;
var
f: THandle;
begin
f :=
CreateFile(
PChar(AFileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if (f <> INVALID_HANDLE_VALUE) then
begin
try
Int64Rec(Result).Lo := Winapi.Windows.GetFileSize(f, @Int64Rec(Result).Hi);
finally
CloseHandle(f);
end;
end
else
Result := 0;
end;
procedure ReadBufferFromFile(
const AFileName: string; ABuffer: Pointer; ASize: Integer);
var
f: THandle;
br: Cardinal;
begin
f :=
CreateFile(
PChar(AFileName), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if (f <> INVALID_HANDLE_VALUE) then
begin
try
ReadFile(f, ABuffer^, ASize, br, nil);
finally
CloseHandle(f);
end;
end
else
RaiseLastOSError;
end;
end.