-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUntDataStreamObject.pas
580 lines (468 loc) · 16.2 KB
/
UntDataStreamObject.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
unit UntDataStreamObject;
interface
uses WinAPI.Windows, System.Classes, System.SysUtils, Generics.Collections,
RegularExpressions;
type
TEnumDataStream = class;
TADSBackupStatus = (absTotal, absPartial, absError);
TDataStream = class
private
FOwner : TEnumDataStream;
FStreamName : String;
FStreamSize : Int64;
{@M}
function GetStreamPath() : String;
public
{@C}
constructor Create(AOwner : TEnumDataStream; AStreamName : String; AStreamSize : Int64);
{@M}
function CopyFileToADS(AFileName : String) : Boolean;
function BackupFromADS(ADestPath : String) : Boolean;
function DeleteFromADS() : Boolean;
{@G/S}
property StreamName : String read FStreamName;
property StreamSize : Int64 read FStreamSize;
property StreamPath : String read GetStreamPath;
end;
TEnumDataStream = class
private
FTargetFile : String;
FItems : TObjectList<TDataStream>;
FForceBackUpReadMethod : Boolean;
{@M}
function Enumerate_FindFirstStream() : Int64;
function Enumerate_BackupRead() : Int64;
function ExtractADSName(ARawName : String) : String;
function CopyFromTo(AFrom, ATo : String) : Boolean;
function GetDataStreamFromName(AStreamName : String) : TDataStream;
public
{@C}
constructor Create(ATargetFile : String; AEnumerateNow : Boolean = True; AForceBackUpReadMethod : Boolean = False);
destructor Destroy(); override;
{@M}
function Refresh() : Int64;
function CopyFileToADS(AFilePath : String) : Boolean;
function BackupFromADS(ADataStream : TDataStream; ADestPath : String) : Boolean; overload;
function DeleteFromADS(ADataStream : TDataStream) : Boolean; overload;
function BackupAllFromADS(ADestPath : String) : TADSBackupStatus;
function BackupFromADS(AStreamName, ADestPath : String) : Boolean; overload;
function DeleteFromADS(AStreamName : String) : Boolean; overload;
{@G}
property TargetFile : String read FTargetFile;
property Items : TObjectList<TDataStream> read FItems;
end;
implementation
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TEnumDataStream
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
{
FindFirstStream / FindNextStream API Definition
}
type
_STREAM_INFO_LEVELS = (FindStreamInfoStandard, FindStreamInfoMaxInfoLevel);
TStreamInfoLevels = _STREAM_INFO_LEVELS;
_WIN32_FIND_STREAM_DATA = record
StreamSize : LARGE_INTEGER;
cStreamName : array[0..(MAX_PATH + 36)] of WideChar;
end;
TWin32FindStreamData = _WIN32_FIND_STREAM_DATA;
var hKernel32 : THandle;
_FindFirstStreamW : function(lpFileName : LPCWSTR; InfoLevel : TStreamInfoLevels; lpFindStreamData : LPVOID; dwFlags : DWORD) : THandle; stdcall;
_FindNextStreamW : function(hFindStream : THandle; lpFindStreamData : LPVOID) : BOOL; stdcall;
{-------------------------------------------------------------------------------
Return the ADS name from it raw name (:<name>:$DATA)
-------------------------------------------------------------------------------}
function TEnumDataStream.ExtractADSName(ARawName : String) : String;
var AMatch : TMatch;
AName : String;
begin
result := ARawName;
///
AName := '';
AMatch := TRegEx.Match(ARawName, ':(.*):');
if (AMatch.Groups.Count < 2) then
Exit();
result := AMatch.Groups.Item[1].Value;
end;
{-------------------------------------------------------------------------------
Scan for ADS using method N�1 (FindFirstStream / FindNextStream). Work since
Microsoft Windows Vista.
-------------------------------------------------------------------------------}
function TEnumDataStream.Enumerate_FindFirstStream() : Int64;
var hStream : THandle;
AData : TWin32FindStreamData;
procedure ProcessDataStream();
var ADataStream : TDataStream;
begin
if (String(AData.cStreamName).CompareTo('::$DATA') = 0) then
Exit();
///
ADataStream := TDataStream.Create(self, ExtractADSName(String(AData.cStreamName)), Int64(AData.StreamSize));
FItems.Add(ADataStream);
end;
begin
result := 0;
///
self.FItems.Clear();
if NOT FileExists(FTargetFile) then
Exit(-1);
if (NOT Assigned(@_FindFirstStreamW)) or (NOT Assigned(@_FindNextStreamW)) then
Exit(-2);
FillChar(AData, SizeOf(TWin32FindStreamData), #0);
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findfirststreamw
hStream := _FindFirstStreamW(PWideChar(FTargetFile), FindStreamInfoStandard, @AData, 0);
if (hStream = INVALID_HANDLE_VALUE) then begin
case GetLastError() of
ERROR_HANDLE_EOF : begin
Exit(-3); // No ADS Found
end;
ERROR_INVALID_PARAMETER : begin
Exit(-4); // Not compatible
end;
else begin
Exit(-5);
end;
end;
end;
ProcessDataStream();
// https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-findnextstreamw
while True do begin
FillChar(AData, SizeOf(TWin32FindStreamData), #0);
if NOT _FindNextStreamW(hStream, @AData) then
break;
ProcessDataStream();
end;
///
result := self.FItems.Count;
end;
{-------------------------------------------------------------------------------
Scan for ADS using method N�2 (BackupRead()). Works since
Microsoft Windows XP.
-------------------------------------------------------------------------------}
function TEnumDataStream.Enumerate_BackupRead() : Int64;
var hFile : THandle;
AStreamId : TWIN32StreamID;
ABytesRead : Cardinal;
pContext : Pointer;
ALowByteSeeked : Cardinal;
AHighByteSeeked : Cardinal;
AName : String;
ABytesToRead : Cardinal;
ASeekTo : LARGE_INTEGER;
AClose : Boolean;
begin
result := 0;
AClose := False;
///
hFile := CreateFile(
PWideChar(self.TargetFile),
GENERIC_READ,
FILE_SHARE_READ,
nil,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0
);
if (hFile = INVALID_HANDLE_VALUE) then
Exit(-1);
try
pContext := nil;
try
while True do begin
FillChar(AStreamId, SizeOf(TWIN32StreamID), #0);
///
{
Read Stream
}
ABytesToRead := SizeOf(TWIN32StreamID) - 4; // We don't count "cStreamName"
if NOT BackupRead(hFile, @AStreamId, ABytesToRead, ABytesRead, False, False, pContext) then
break;
AClose := True;
if (ABytesRead = 0) then
break;
ASeekTo.QuadPart := (AStreamId.Size + AStreamId.dwStreamNameSize);
case AStreamId.dwStreamId of
{
Deadling with ADS Only
}
BACKUP_ALTERNATE_DATA : begin
if (AStreamId.dwStreamNameSize > 0) then begin
{
Read ADS Name
}
ABytesToRead := AStreamId.dwStreamNameSize;
SetLength(AName, (ABytesToRead div SizeOf(WideChar)));
if BackupRead(hFile, PByte(AName), ABytesToRead, ABytesRead, False, False, pContext) then begin
Dec(ASeekTo.QuadPart, ABytesRead); // Already done
FItems.Add(TDataStream.Create(self, ExtractADSName(AName), AStreamId.Size));
end;
end;
end;
end;
{
Goto Next Stream.
}
if NOT BackupSeek(hFile, ASeekTo.LowPart, ASeekTo.HighPart, ALowByteSeeked, AHighByteSeeked, pContext) then
break;
(*
//////////////////////////////////////////////////////////////////////
// BackupSeek() Alternative (Manual method)
//////////////////////////////////////////////////////////////////////
var ABuffer : array[0..2096-1] of byte;
// ...
while True do begin
if (ASeekTo.QuadPart < SizeOf(ABuffer)) then
ABytesToRead := ASeekTo.QuadPart
else
ABytesToRead := SizeOf(ABuffer);
if ABytesToRead = 0 then
break;
if NOT BackupRead(hFile, PByte(@ABuffer), ABytesToRead, ABytesRead, False, False, pContext) then
break;
///
Dec(ASeekTo.QuadPart, ABytesRead);
if (ASeekTo.QuadPart <= 0) then
break;
end;
// ...
//////////////////////////////////////////////////////////////////////
*)
end;
finally
if AClose then
BackupRead(hFile, nil, 0, ABytesRead, True, False, pContext);
end;
finally
CloseHandle(hFile);
end;
end;
{-------------------------------------------------------------------------------
Refresh embedded data stream objects using Windows API. Returns number of
data stream objects or an error identifier.
-------------------------------------------------------------------------------}
function TEnumDataStream.Refresh() : Int64;
var AVersion : TOSVersion;
begin
result := 0;
///
if (AVersion.Major >= 6) then begin
{
Vista and above
}
if self.FForceBackUpReadMethod then
result := self.Enumerate_BackupRead()
else
result := self.Enumerate_FindFirstStream();
end else if (AVersion.Major = 5) and (AVersion.Minor >= 1) then begin
{
Windows XP / Server 2003 & R2
}
result := self.Enumerate_BackupRead();
end else begin
// Unsupported (???)
end;
end;
{-------------------------------------------------------------------------------
Refresh ADS Files and retrieve one ADS file by it name.
-------------------------------------------------------------------------------}
function TEnumDataStream.GetDataStreamFromName(AStreamName : String) : TDataStream;
var I : Integer;
AStream : TDataStream;
begin
result := nil;
///
if (self.Refresh() > 0) then begin
for I := 0 to self.Items.count -1 do begin
AStream := self.Items.Items[i];
if NOT Assigned(AStream) then
continue;
///
if (String.Compare(AStream.StreamName, AStreamName, True) = 0) then
result := AStream;
end;
end;
end;
{-------------------------------------------------------------------------------
ADS Classic Actions
- Copy file to current ADS Location.
- Copy ADS item to destination path.
- Delete ADS Item.
-------------------------------------------------------------------------------}
function TEnumDataStream.CopyFromTo(AFrom, ATo : String) : Boolean;
var hFromFile : THandle;
hToFile : THandle;
ABuffer : array[0..4096-1] of byte;
ABytesRead : Cardinal;
ABytesWritten : Cardinal;
begin
result := False;
///
hFromFile := INVALID_HANDLE_VALUE;
hToFile := INVALID_HANDLE_VALUE;
try
hFromFile := CreateFile(PWideChar(AFrom), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, 0, 0);
if (hFromFile = INVALID_HANDLE_VALUE) then
Exit();
hToFile := CreateFile(
PWideChar(ATo),
GENERIC_WRITE,
FILE_SHARE_WRITE,
nil,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
0
);
if (hToFile = INVALID_HANDLE_VALUE) then
Exit();
///
while True do begin
{
Read
}
if NOT ReadFile(hFromFile, ABuffer, SizeOf(ABuffer), ABytesRead, nil) then
Exit();
if ABytesRead = 0 then
break; // Success
{
Write
}
if NOT WriteFile(hToFile, ABuffer, ABytesRead, ABytesWritten, nil) then
Exit();
if (ABytesWritten <> ABytesRead) then
Exit();
end;
///
result := True;
finally
if hFromFile <> INVALID_HANDLE_VALUE then
CloseHandle(hFromFile);
if hToFile <> INVALID_HANDLE_VALUE then
CloseHandle(hToFile);
///
self.Refresh();
end;
end;
function TEnumDataStream.CopyFileToADS(AFilePath : String) : Boolean;
begin
result := CopyFromTo(AFilePath, Format('%s:%s', [self.FTargetFile, ExtractFileName(AFilePath)]));
end;
function TEnumDataStream.BackupFromADS(ADataStream : TDataStream; ADestPath : String) : Boolean;
begin
result := False;
if NOT Assigned(ADataStream) then
Exit();
result := CopyFromTo(ADataStream.StreamPath, Format('%s%s', [IncludeTrailingPathDelimiter(ADestPath), ADataStream.StreamName]));
end;
function TEnumDataStream.DeleteFromADS(ADataStream : TDataStream) : Boolean;
begin
result := DeleteFile(ADataStream.StreamPath);
end;
function TEnumDataStream.BackupAllFromADS(ADestPath : String) : TADSBackupStatus;
var I : integer;
AStream : TDataStream;
begin
result := absError;
///
if (self.Refresh() > 0) then begin
for I := 0 to self.Items.count -1 do begin
AStream := self.Items.Items[i];
if NOT Assigned(AStream) then
continue;
///
if AStream.BackupFromADS(ADestPath) and (result <> absPartial) then
result := absTotal
else
result := absPartial;
end;
end;
end;
function TEnumDataStream.BackupFromADS(AStreamName, ADestPath : String) : Boolean;
var AStream : TDataStream;
begin
result := False;
///
AStream := self.GetDataStreamFromName(AStreamName);
if Assigned(AStream) then
result := self.BackupFromADS(AStream, ADestPath);
end;
function TEnumDataStream.DeleteFromADS(AStreamName : String) : Boolean;
var AStream : TDataStream;
begin
result := False;
///
AStream := self.GetDataStreamFromName(AStreamName);
if Assigned(AStream) then
result := self.DeleteFromADS(AStream);
end;
{-------------------------------------------------------------------------------
___constructor
-------------------------------------------------------------------------------}
constructor TEnumDataStream.Create(ATargetFile : String; AEnumerateNow : Boolean = True; AForceBackUpReadMethod : Boolean = False);
begin
self.FTargetFile := ATargetFile;
self.FForceBackUpReadMethod := AForceBackupReadMethod;
FItems := TObjectList<TDataStream>.Create();
FItems.OwnsObjects := True;
if AEnumerateNow then
self.Refresh();
end;
{-------------------------------------------------------------------------------
___destructor
-------------------------------------------------------------------------------}
destructor TEnumDataStream.Destroy();
begin
if Assigned(FItems) then
FreeAndNil(FItems);
///
inherited Destroy();
end;
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TDataStream
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++}
constructor TDataStream.Create(AOwner : TEnumDataStream; AStreamName : String; AStreamSize : Int64);
begin
self.FOwner := AOwner;
self.FStreamName := AStreamName;
self.FStreamSize := AStreamSize;
end;
{-------------------------------------------------------------------------------
Generate Stream Path Accordingly
-------------------------------------------------------------------------------}
function TDataStream.GetStreamPath() : String;
begin
result := '';
if NOT Assigned(FOwner) then
Exit();
result := Format('%s:%s', [FOwner.TargetFile, self.FStreamName]);
end;
{-------------------------------------------------------------------------------
ADS Classic Actions (Redirected to Owner Object)
-------------------------------------------------------------------------------}
function TDataStream.CopyFileToADS(AFileName : String) : Boolean;
begin
if Assigned(FOwner) then
result := FOwner.CopyFileToADS(AFileName);
end;
function TDataStream.BackupFromADS(ADestPath : String) : Boolean;
begin
if Assigned(FOwner) then
result := FOwner.BackupFromADS(self, ADestPath);
end;
function TDataStream.DeleteFromADS() : Boolean;
begin
if Assigned(FOwner) then
result := FOwner.DeleteFromADS(self);
end;
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
initialization
_FindFirstStreamW := nil;
_FindNextStreamW := nil;
hKernel32 := LoadLibrary('KERNEL32.DLL');
if (hKernel32 > 0) then begin
@_FindFirstStreamW := GetProcAddress(hKernel32, 'FindFirstStreamW');
@_FindNextStreamW := GetProcAddress(hKernel32, 'FindNextStreamW');
end;
finalization
_FindFirstStreamW := nil;
_FindNextStreamW := nil;
end.