-
Notifications
You must be signed in to change notification settings - Fork 35
/
NtUtils.SysUtils.pas
1497 lines (1229 loc) · 36 KB
/
NtUtils.SysUtils.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
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
unit NtUtils.SysUtils;
{
The module includes miscellaneous functions to compensate for missing
System.SysUtils.
}
interface
uses
Ntapi.WinNt, NtUtils, DelphiUtils.AutoObjects, DelphiUtils.Arrays,
DelphiApi.Reflection;
const
BOM_LE = #$FEFF;
BOM_BE = #$FFFE;
DEFAULT_PATH_SEPARATOR = '\';
DEFAULT_EXTENSION_SEPARATOR = '.';
type
TIntegerSize = (
isByte,
isWord,
isCardinal,
isUInt64,
isUIntPtr =
{$IF SizeOf(UIntPtr) = SizeOf(UInt64)}isUInt64{$ELSE}isCardinal{$ENDIF}
);
TNumericSystem = (nsBinary, nsOctal, nsDecimal, nsHexadecimal);
TNumericSystems = set of TNumericSystem;
TRtlxParameterLocation = record
FirstCharIndex: Integer;
LastCharIndex: Integer;
end;
const
NUMERIC_SYSTEM_RADIX: array [TNumericSystem] of Byte = (2, 8, 10, 16);
// Strings
// Return a string if non-empty or a default string
function RtlxStringOrDefault(
[opt] const Value: String;
const Default: String
): String;
// Create string from a potentially zero-terminated buffer
function RtlxCaptureString(
[in] Buffer: PWideChar;
MaxChars: Cardinal
): String;
// Change byte order for each character of a string
procedure RtlxSwapEndiannessString(
var S: String
);
// Create a string from a buffer honoring its byte order mask
function RtlxSetStringWithEndian(
Buffer: PWideChar;
Length: Cardinal
): String;
// Create string from a potentially zero-terminated buffer
function RtlxCaptureAnsiString(
[in] Buffer: PAnsiChar;
MaxChars: Cardinal
): AnsiString;
// Make a string by repeating a character
function RtlxBuildString(
Char: WideChar;
Count: Cardinal
): String;
// Convert an array of strings to a wide multi-zero-terminated string
function RtlxBuildWideMultiSz(
const Strings: TArray<String>
): IMemory<PWideMultiSz>;
// Convert an array of strings to an ANSI multi-zero-terminated string
function RtlxBuildAnsiMultiSz(
const Strings: TArray<AnsiString>
): IMemory<PAnsiMultiSz>;
// Convert a wide multi-zero-terminated string into an array of string
function RtlxParseWideMultiSz(
[in] Buffer: PWideMultiSz;
[NumberOfElements] MaximumLength: Cardinal = $FFFFFFFF
): TArray<String>;
// Convert an ANSI multi-zero-terminated string into an array of string
function RtlxParseAnsiMultiSz(
[in] Buffer: PAnsiMultiSz;
[NumberOfElements] MaximumLength: Cardinal = $FFFFFFFF
): TArray<AnsiString>;
// Compare two unicode strings in a case-(in)sensitive way
function RtlxCompareStrings(
const StringA: String;
const StringB: String;
CaseSensitive: Boolean = False
): Integer;
// Get a comparer callback for string arrays
function RtlxGetStringComparer(
CaseSensitive: Boolean = False
): TComparer<String>;
// Compare two ANSI strings in a case-(in)sensitive way
function RtlxCompareAnsiStrings(
const StringA: AnsiString;
const StringB: AnsiString;
CaseSensitive: Boolean = False
): Integer;
// Get a comparer callback for ANSI string arrays
function RtlxGetAnsiStringComparer(
CaseSensitive: Boolean = False
): TComparer<AnsiString>;
// Check if two unicode strings are equal in a case-(in)sensitive way
function RtlxEqualStrings(
const StringA: String;
const StringB: String;
CaseSensitive: Boolean = False
): Boolean;
// Get an equality check callback for string arrays
function RtlxGetEqualityCheckString(
CaseSensitive: Boolean = False
): TEqualityCheck<String>;
// Check if two ANSI strings are equal in a case-(in)sensitive way
function RtlxEqualAnsiStrings(
const StringA: AnsiString;
const StringB: AnsiString;
CaseSensitive: Boolean = False
): Boolean;
// Get an equality check callback for ANSI string arrays
function RtlxGetEqualityCheckAnsiString(
CaseSensitive: Boolean = False
): TEqualityCheck<AnsiString>;
// Compute a hash of a string
function RtlxHashString(
const Source: String;
CaseSensitive: Boolean = False
): Cardinal;
// Check if a string has a matching prefix
function RtlxPrefixString(
const Prefix: String;
const S: String;
CaseSensitive: Boolean = False
): Boolean;
// Check if a string has a matching prefix and remove it
function RtlxPrefixStripString(
const Prefix: String;
var S: String;
CaseSensitive: Boolean = False
): Boolean;
// Check if an ANSI string has a matching prefix
function RtlxPrefixAnsiString(
const Prefix: AnsiString;
const S: AnsiString;
CaseSensitive: Boolean = False
): Boolean;
// Check if an ANSI string has a matching prefix and remove it
function RtlxPrefixStripAnsiString(
const Prefix: AnsiString;
var S: AnsiString;
CaseSensitive: Boolean = False
): Boolean;
// Check if a string has a matching suffix
function RtlxSuffixString(
const Suffix: String;
const S: String;
CaseSensitive: Boolean = False
): Boolean;
// Check if a string has a matching suffix and remove it
function RtlxSuffixStripString(
const Suffix: String;
var S: String;
CaseSensitive: Boolean = False
): Boolean;
// Convert a string to lower case
function RtlxLowerString(
const Source: String
): String;
// Convert a string to upper case
function RtlxUpperString(
const Source: String
): String;
// Format a string similar to System.SysUtils.Format but using ntdll's CRT
// Differences:
// - supports %wZ for TNtUnicodeString
// - supports %z for TNtAnsiString
// - does not support floating point formats
// For more details, see:
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions
function RtlxFormatString(
const Format: String;
const Args: array of const
): String;
// Integers
// Switch a 32-bit integer between big- and little-endian
function RtlxSwapEndianness(
Value: Cardinal
): Cardinal;
// Convert a 32-bit integer to a string
function RtlxUIntToStr(
Value: Cardinal;
Base: TNumericSystem = nsHexadecimal;
Width: Cardinal = 0;
PrefixBases: TNumericSystems = [nsBinary, nsOctal, nsHexadecimal]
): String;
// Convert a 64-bit integer to a string
function RtlxUInt64ToStr(
Value: UInt64;
Base: TNumericSystem = nsHexadecimal;
Width: Cardinal = 0;
PrefixBases: TNumericSystems = [nsBinary, nsOctal, nsHexadecimal]
): String;
// Convert a native-size integer to a string
function RtlxUIntPtrToStr(
Value: UIntPtr;
Base: TNumericSystem = nsHexadecimal;
Width: Cardinal = 0;
PrefixBases: TNumericSystems = [nsBinary, nsOctal, nsHexadecimal]
): String;
// Convert a pointer value to a string
function RtlxPtrToStr(
Value: Pointer;
Width: Cardinal = 8;
AddHexPrefix: Boolean = True
): String;
// Convert a string to an 64-bit integer
function RtlxStrToUInt64(
const S: String;
out Value: UInt64;
DefaultBase: TNumericSystem = nsDecimal;
RecognizeBases: TNumericSystems = [nsDecimal, nsHexadecimal];
AllowMinusSign: Boolean = True;
ValueSize: TIntegerSize = isUInt64
): Boolean;
// Convert a string to a 32-bit integer
function RtlxStrToUInt(
const S: String;
out Value: Cardinal;
DefaultBase: TNumericSystem = nsDecimal;
RecognizeBases: TNumericSystems = [nsDecimal, nsHexadecimal];
AllowMinusSign: Boolean = True
): Boolean;
// Convert a string to a natively-sized integer
function RtlxStrToUIntPtr(
const S: String;
out Value: UIntPtr;
DefaultBase: TNumericSystem = nsDecimal;
RecognizeBases: TNumericSystems = [nsDecimal, nsHexadecimal];
AllowMinusSign: Boolean = True
): Boolean;
// Random
// Generate a random number
function RtlxRandom: Cardinal;
// Generate a random GUID
function RtlxRandomGuid: TGuid;
// GUIDs
// Convert a GUID to a string
function RtlxGuidToString(
const Guid: TGuid
): String;
// Try to parse a string containing a GUID
function RtlxStringToGuid(
const GuidString: String;
out Guid: TGuid
): TNtxStatus;
// Paths
// Split the path into the parent (directory) and child (filename) components
function RtlxSplitPath(
const Path: String;
out ParentName: String;
out ChildName: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR
): Boolean;
// Extract a parent component from a path
function RtlxExtractRootPath(
const Path: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR
): String;
// Extract a child component from a path
function RtlxExtractNamePath(
const Path: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR
): String;
// Extract a file extension a path
function RtlxExtractExtensionPath(
const Path: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR;
const ExtensionSeparator: Char = DEFAULT_EXTENSION_SEPARATOR
): String;
// Construct a filename with a different extension
function RtlxReplaceExtensionPath(
const Path: String;
const NewExtension: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR;
const ExtensionSeparator: Char = DEFAULT_EXTENSION_SEPARATOR
): String;
// Check if one path is under another path
// NOTE: only use on normalized & final paths
function RtlxIsPathUnderRoot(
const Path: String;
const Root: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR
): Boolean;
// Join to strings using a path separator
function RtlxCombinePaths(
const Parent: String;
const Child: String;
const PathSeparator: Char = DEFAULT_PATH_SEPARATOR
): String;
// Comman lines
// Splits the command line into parameters.
// When given an image name, will try to recognize it as the zero parameter.
function RtlxParseCommandLine(
const CommandLine: String;
[opt] const ImageName: String = ''
): TArray<TRtlxParameterLocation>;
// Determine the number of available command line parameter
function RtlxParamCount: Integer;
// Retrieve a command line parameter by index
function RtlxParamStr(
Index: Integer;
Unquote: Boolean = True
): String;
// Retrieve all command line parameters starting from an index
function RtlxParamStrFrom(
Index: Integer
): String;
implementation
uses
Ntapi.ntrtl, Ntapi.ntdef, Ntapi.crt, Ntapi.ntpebteb, NtUtils.Synchronization;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function RtlxStringOrDefault;
begin
if Value <> '' then
Result := Value
else
Result := Default;
end;
function RtlxCaptureString;
var
Finish: PWideChar;
Count: Cardinal;
begin
Finish := Buffer;
Count := 0;
while (Count < MaxChars) and (Finish^ <> #0) do
begin
Inc(Finish);
Inc(Count);
end;
SetString(Result, Buffer, Count);
end;
procedure RtlxSwapEndiannessString;
var
i: Integer;
begin
for i := Low(S) to High(S) do
S[i] := Chr((Word(Ord(S[i])) shr 8) or (Word(Ord(S[i])) shl 8));
end;
function RtlxSetStringWithEndian;
begin
if Length < 1 then
Exit('');
if (Buffer[0] = BOM_LE) or (Buffer[0] = BOM_BE) then
begin
// Known bytes order; skip it and copy the rest
SetString(Result, PWideChar(@Buffer[1]), Pred(Length));
// Swap order if necessary
if Buffer[0] = BOM_BE then
RtlxSwapEndiannessString(Result);
end
else
begin
// Unspecified; copy entirely
SetString(Result, Buffer, Length);
end;
end;
function RtlxCaptureAnsiString;
var
Finish: PAnsiChar;
Count: Cardinal;
begin
Finish := Buffer;
Count := 0;
while (Count < MaxChars) and (Finish^ <> #0) do
begin
Inc(Finish);
Inc(Count);
end;
SetString(Result, Buffer, Count);
end;
function RtlxBuildString;
var
i: Integer;
begin
SetLength(Result, Count);
for i := Low(Result) to High(Result) do
Result[i] := Char;
end;
function RtlxBuildWideMultiSz;
var
i: Integer;
Size: Cardinal;
Buffer: PWideMultiSz;
begin
// Always include two terminating zeros
Size := 2 * SizeOf(WideChar);
for i := 0 to High(Strings) do
Inc(Size, StringSizeZero(Strings[i]));
// Allocate a buffer for all strings + additional zero terminators
IMemory(Result) := Auto.AllocateDynamic(Size);
Buffer := Result.Data;
for i := 0 to High(Strings) do
begin
MarshalString(Strings[i], Buffer);
Inc(PByte(Buffer), StringSizeZero(Strings[i]));
end;
end;
function RtlxBuildAnsiMultiSz;
var
i: Integer;
Size: Cardinal;
Buffer: PAnsiMultiSz;
begin
// Always include two terminating zeros
Size := 2 * SizeOf(AnsiChar);
for i := 0 to High(Strings) do
Inc(Size, Succ(Length(Strings[i])) * SizeOf(AnsiChar));
// Allocate a buffer for all strings + additional zero terminators
Imemory(Result) := Auto.AllocateDynamic(Size);
Buffer := Result.Data;
for i := 0 to High(Strings) do
begin
Size := Succ(Length(Strings[i])) * SizeOf(AnsiChar);
Move(PAnsiChar(Strings[i])^, Buffer^, Size);
Inc(PByte(Buffer), Size);
end;
end;
function RtlxParseWideMultiSz;
var
Count, j: Integer;
pCurrentChar, pItemStart, pBlockEnd: PWideChar;
begin
// Save where the buffer ends to make sure we don't pass this point
pBlockEnd := PWideChar(Buffer) + MaximumLength;
// Count strings
Count := 0;
pCurrentChar := PWideChar(Buffer);
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
begin
// Skip one zero-terminated string
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
Inc(pCurrentChar);
Inc(Count);
Inc(pCurrentChar);
end;
SetLength(Result, Count);
// Save the content
j := 0;
pCurrentChar := PWideChar(Buffer);
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
begin
// Parse one string
Count := 0;
pItemStart := pCurrentChar;
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
begin
Inc(pCurrentChar);
Inc(Count);
end;
// Save it
SetString(Result[j], pItemStart, Count);
Inc(j);
Inc(pCurrentChar);
end;
end;
function RtlxParseAnsiMultiSz;
var
Count, j: Integer;
pCurrentChar, pItemStart, pBlockEnd: PAnsiChar;
begin
// Save where the buffer ends to make sure we don't pass this point
pBlockEnd := PAnsiChar(Buffer) + MaximumLength;
// Count strings
Count := 0;
pCurrentChar := PAnsiChar(Buffer);
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
begin
// Skip one zero-terminated string
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
Inc(pCurrentChar);
Inc(Count);
Inc(pCurrentChar);
end;
SetLength(Result, Count);
// Save the content
j := 0;
pCurrentChar := PAnsiChar(Buffer);
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
begin
// Parse one string
Count := 0;
pItemStart := pCurrentChar;
while (pCurrentChar < pBlockEnd) and (pCurrentChar^ <> #0) do
begin
Inc(pCurrentChar);
Inc(Count);
end;
// Save it
SetString(Result[j], pItemStart, Count);
Inc(j);
Inc(pCurrentChar);
end;
end;
function RtlxCompareStrings;
var
StringAStr, StringBStr: TNtUnicodeString;
RemainingLengthA, RemainingLengthB: Cardinal;
begin
// RtlCompareUnicodeString uses UNICODE_STRINGs which can only address
// up to 32k characters. For longer strings, perform comparison in blocks.
StringAStr.Buffer := PWideChar(StringA);
StringBStr.Buffer := PWideChar(StringB);
RemainingLengthA := Length(StringA);
RemainingLengthB := Length(StringB);
repeat
// Compute the size of a block for string A
if RemainingLengthA > MAX_UNICODE_STRING then
begin
StringAStr.Length := MAX_UNICODE_STRING * SizeOf(WideChar);
Dec(RemainingLengthA, MAX_UNICODE_STRING);
end
else
begin
StringAStr.Length := RemainingLengthA * SizeOf(WideChar);
RemainingLengthA := 0;
end;
StringAStr.MaximumLength := StringAStr.Length;
// Compute the size of a block for string B
if RemainingLengthB > MAX_UNICODE_STRING then
begin
StringBStr.Length := MAX_UNICODE_STRING * SizeOf(WideChar);
Dec(RemainingLengthB, MAX_UNICODE_STRING);
end
else
begin
StringBStr.Length := RemainingLengthB * SizeOf(WideChar);
RemainingLengthB := 0;
end;
StringBStr.MaximumLength := StringBStr.Length;
// Compare the string blocks
Result := RtlCompareUnicodeString(StringAStr, StringBStr, not CaseSensitive);
// Did it find a difference already?
if Result <> 0 then
Break;
// Did both strings run out?
if (RemainingLengthA = 0) and (RemainingLengthB = 0) then
Break;
// Advance the buffers to the next block
Inc(PByte(StringAStr.Buffer), StringAStr.Length);
Inc(PByte(StringBStr.Buffer), StringBStr.Length);
until False;
end;
function RtlxGetStringComparer;
begin
Result := function (const A, B: String): Integer
begin
Result := RtlxCompareStrings(A, B, CaseSensitive);
end;
end;
function RtlxCompareAnsiStrings;
var
StringAStr, StringBStr: TNtAnsiString;
RemainingLengthA, RemainingLengthB: Cardinal;
begin
// RtlCompareString uses ANSI_STRINGs which can only address up to 65k
// characters. For longer strings, perform comparison in blocks.
StringAStr.Buffer := PAnsiChar(StringA);
StringBStr.Buffer := PAnsiChar(StringB);
RemainingLengthA := Length(StringA);
RemainingLengthB := Length(StringB);
repeat
// Compute the size of a block for string A
if RemainingLengthA > MAX_ANSI_STRING then
begin
StringAStr.Length := MAX_ANSI_STRING * SizeOf(AnsiChar);
Dec(RemainingLengthA, MAX_ANSI_STRING);
end
else
begin
StringAStr.Length := RemainingLengthA * SizeOf(AnsiChar);
RemainingLengthA := 0;
end;
StringAStr.MaximumLength := StringAStr.Length;
// Compute the size of a block for string B
if RemainingLengthB > MAX_ANSI_STRING then
begin
StringBStr.Length := MAX_ANSI_STRING * SizeOf(AnsiChar);
Dec(RemainingLengthB, MAX_ANSI_STRING);
end
else
begin
StringBStr.Length := RemainingLengthB * SizeOf(AnsiChar);
RemainingLengthB := 0;
end;
StringBStr.MaximumLength := StringBStr.Length;
// Compare the string blocks
Result := RtlCompareString(StringAStr, StringBStr, not CaseSensitive);
// Did it find a difference already?
if Result <> 0 then
Break;
// Did both strings run out?
if (RemainingLengthA = 0) and (RemainingLengthB = 0) then
Break;
// Advance the buffers to the next block
Inc(PByte(StringAStr.Buffer), StringAStr.Length);
Inc(PByte(StringBStr.Buffer), StringBStr.Length);
until False;
end;
function RtlxGetAnsiStringComparer;
begin
Result := function (const A, B: AnsiString): Integer
begin
Result := RtlxCompareAnsiStrings(A, B, CaseSensitive);
end;
end;
function RtlxEqualStrings;
var
StringAStr, StringBStr: TNtUnicodeString;
RemainingLength: Cardinal;
begin
// Shortcut for strings of different lengths
if Length(StringA) <> Length(StringB) then
Exit(False);
// RtlEqualUnicodeString uses UNICODE_STRINGs which can only address
// up to 32k characters. For longer strings, perform comparison in blocks.
StringAStr.Buffer := PWideChar(StringA);
StringBStr.Buffer := PWideChar(StringB);
RemainingLength := Length(StringA);
repeat
// Compute the size of a block
if RemainingLength > MAX_UNICODE_STRING then
begin
StringAStr.Length := MAX_UNICODE_STRING * SizeOf(WideChar);
StringBStr.Length := MAX_UNICODE_STRING * SizeOf(WideChar);
Dec(RemainingLength, MAX_UNICODE_STRING);
end
else
begin
StringAStr.Length := RemainingLength * SizeOf(WideChar);
StringBStr.Length := RemainingLength * SizeOf(WideChar);
RemainingLength := 0;
end;
StringAStr.MaximumLength := StringAStr.Length;
StringBStr.MaximumLength := StringBStr.Length;
// Compare the string blocks
Result := RtlEqualUnicodeString(StringAStr, StringBStr, not CaseSensitive);
// Did it find a difference already?
if not Result then
Break;
// Did the strings run out?
if RemainingLength = 0 then
Break;
// Advance the buffers to the next block
Inc(PByte(StringAStr.Buffer), StringAStr.Length);
Inc(PByte(StringBStr.Buffer), StringBStr.Length);
until False;
end;
function RtlxGetEqualityCheckString;
begin
Result := function (const A, B: String): Boolean
begin
Result := RtlxEqualStrings(A, B, CaseSensitive);
end;
end;
function RtlxEqualAnsiStrings;
var
StringAStr, StringBStr: TNtAnsiString;
RemainingLength: Cardinal;
begin
// Shortcut for strings of different lengths
if Length(StringA) <> Length(StringB) then
Exit(False);
// RtlEqualString uses ANSI_STRINGs which can only address up to 65k
// characters. For longer strings, perform comparison in blocks.
StringAStr.Buffer := PAnsiChar(StringA);
StringBStr.Buffer := PAnsiChar(StringB);
RemainingLength := Length(StringA);
repeat
// Compute the size of a block
if RemainingLength > MAX_ANSI_STRING then
begin
StringAStr.Length := MAX_ANSI_STRING * SizeOf(AnsiChar);
StringBStr.Length := MAX_ANSI_STRING * SizeOf(AnsiChar);
Dec(RemainingLength, MAX_ANSI_STRING);
end
else
begin
StringAStr.Length := RemainingLength * SizeOf(AnsiChar);
StringBStr.Length := RemainingLength * SizeOf(AnsiChar);
RemainingLength := 0;
end;
StringAStr.MaximumLength := StringAStr.Length;
StringBStr.MaximumLength := StringBStr.Length;
// Compare the string blocks
Result := RtlEqualString(StringAStr, StringBStr, not CaseSensitive);
// Did it find a difference already?
if not Result then
Break;
// Did the strings run out?
if RemainingLength = 0 then
Break;
// Advance the buffers to the next block
Inc(PByte(StringAStr.Buffer), StringAStr.Length);
Inc(PByte(StringBStr.Buffer), StringBStr.Length);
until False;
end;
function RtlxGetEqualityCheckAnsiString;
begin
Result := function (const A, B: AnsiString): Boolean
begin
Result := RtlxEqualAnsiStrings(A, B, CaseSensitive);
end;
end;
function RtlxHashString;
var
SourceStr: TNtUnicodeString;
begin
if not RtlxInitUnicodeString(SourceStr, Source).IsSuccess or not
NT_SUCCESS(RtlHashUnicodeString(SourceStr, not CaseSensitive,
HASH_STRING_ALGORITHM_DEFAULT, Result)) then
Result := Length(Source);
end;
function RtlxPrefixString;
begin
if Length(Prefix) > Length(S) then
Exit(False);
Result := RtlxEqualStrings(Prefix, Copy(S, 1, Length(Prefix)), CaseSensitive);
end;
function RtlxPrefixStripString;
begin
Result := RtlxPrefixString(Prefix, S, CaseSensitive);
if Result then
Delete(S, Low(S), Length(Prefix));
end;
function RtlxPrefixAnsiString;
begin
if Length(Prefix) > Length(S) then
Exit(False);
Result := RtlxEqualAnsiStrings(Prefix, Copy(S, 1, Length(Prefix)),
CaseSensitive);
end;
function RtlxPrefixStripAnsiString;
begin
Result := RtlxPrefixAnsiString(Prefix, S, CaseSensitive);
if Result then
Delete(S, Low(S), Length(Prefix));
end;
function RtlxSuffixString;
begin
if Length(Suffix) > Length(S) then
Exit(False);
Result := RtlxEqualStrings(Suffix, Copy(S, Length(S) - Length(Suffix) + 1,
Length(Suffix)), CaseSensitive);
end;
function RtlxSuffixStripString;
begin
Result := RtlxSuffixString(Suffix, S, CaseSensitive);
if Result then
Delete(S, Low(S) + High(S) - Length(Suffix), Length(Suffix));
end;
function RtlxLowerString;
var
i: Integer;
begin
// Make a writable unique copy to modify
Result := Source;
UniqueString(Result);
for i := Low(Result) to High(Result) do
Result[i] := RtlDowncaseUnicodeChar(Result[i]);
end;
function RtlxUpperString;
var
i: Integer;
begin
// Make a writable unique copy to modify
Result := Source;
UniqueString(Result);
for i := Low(Result) to High(Result) do
Result[i] := RtlUpcaseUnicodeChar(Result[i]);
end;
function RtlxpAllocateVarArgs(
const Args: array of const
): IMemory;
var
Buffer: Pointer;
i: Integer;
begin
Result := Auto.AllocateDynamic(Length(Args) * SizeOf(Pointer));
Buffer := Result.Data;
for i := 0 to High(Args) do
begin
case Args[i].VType of
vtInteger: Integer(Buffer^) := Args[i].VInteger;
vtBoolean: Boolean(Buffer^) := Args[i].VBoolean;
vtChar: AnsiChar(Buffer^) := Args[i].VChar;
vtExtended: Double(Buffer^) := Double(Args[i].VExtended^);
vtString: Pointer(Buffer^) := Args[i].VString;
vtPointer: Pointer(Buffer^) := Args[i].VPointer;
vtPChar: Pointer(Buffer^) := Args[i].VPChar;
vtObject: Pointer(Buffer^) := Args[i].VObject;
vtClass: Pointer(Buffer^) := Args[i].VClass;
vtWideChar: WideChar(Buffer^) := Args[i].VWideChar;
vtPWideChar: Pointer(Buffer^) := Args[i].VPWideChar;
vtAnsiString: Pointer(Buffer^) := Args[i].VAnsiString;
vtCurrency: Pointer(Buffer^) := Args[i].VCurrency;
vtVariant: Pointer(Buffer^) := Args[i].VVariant;
vtInterface: Pointer(Buffer^) := Args[i].VInterface;
vtWideString: Pointer(Buffer^) := Args[i].VWideString;
vtInt64: Int64(Buffer^) := Args[i].VInt64^;
vtUnicodeString: Pointer(Buffer^) := Args[i].VUnicodeString;
end;
Inc(PByte(Buffer), SizeOf(Pointer));
end;
end;
function RtlxFormatString;
var
Buffer: IMemory<PWideChar>;
VarArgsBuffer: IMemory;
NewSize: Cardinal;
Count: Integer;
begin
NewSize := $100;
VarArgsBuffer := RtlxpAllocateVarArgs(Args);
repeat
IMemory(Buffer) := Auto.AllocateDynamic(NewSize);