forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtract.pas
380 lines (335 loc) · 11.8 KB
/
Extract.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
unit Extract;
{
Inno Setup
Copyright (C) 1997-2010 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
TFileExtractor class
$jrsoftware: issrc/Projects/Extract.pas,v 1.30 2010/03/13 18:51:37 jr Exp $
}
interface
uses
Windows, SysUtils, Int64Em, FileClass, Compress, Struct, ArcFour;
type
TExtractorProgressProc = procedure(Bytes: Cardinal);
TFileExtractor = class
private
FDecompressor: array[Boolean] of TCustomDecompressor;
FSourceF: TFile;
FOpenedSlice, FChunkFirstSlice, FChunkLastSlice: Integer;
FChunkStartOffset: Longint;
FChunkBytesLeft, FChunkDecompressedBytesRead: Integer64;
FNeedReset: Boolean;
FChunkCompressed, FChunkEncrypted: Boolean;
FCryptContext: TArcFourContext;
FCryptKey: String;
FEntered: Integer;
procedure DecompressBytes(var Buffer; Count: Cardinal);
class function FindSliceFilename(const ASlice: Integer): String;
procedure OpenSlice(const ASlice: Integer);
function ReadProc(var Buf; Count: Longint): Longint;
public
constructor Create(ADecompressorClass: TCustomDecompressorClass);
destructor Destroy; override;
procedure DecompressFile(const FL: TSetupFileLocationEntry; const DestF: TFile;
const ProgressProc: TExtractorProgressProc; const VerifyChecksum: Boolean);
procedure SeekTo(const FL: TSetupFileLocationEntry;
const ProgressProc: TExtractorProgressProc);
property CryptKey: String write FCryptKey;
end;
function FileExtractor: TFileExtractor;
procedure FreeFileExtractor;
implementation
uses
PathFunc, CmnFunc2, Main, Msgs, MsgIDs, InstFunc, CompressZlib, bzlib,
LZMADecomp, SHA1, Logging, NewDisk;
var
FFileExtractor: TFileExtractor;
function FileExtractor: TFileExtractor;
const
DecompClasses: array[TSetupCompressMethod] of TCustomDecompressorClass =
(TStoredDecompressor, TZDecompressor, TBZDecompressor, TLZMA1Decompressor,
TLZMA2Decompressor);
begin
if FFileExtractor = nil then
FFileExtractor := TFileExtractor.Create(DecompClasses[SetupHeader.CompressMethod]);
Result := FFileExtractor;
end;
procedure FreeFileExtractor;
begin
FreeAndNil(FFileExtractor);
end;
procedure SourceIsCorrupted(const AReason: String);
begin
Log('Source file corrupted: ' + AddPeriod(AReason));
raise Exception.Create(SetupMessages[msgSourceIsCorrupted]);
end;
{ TFileExtractor }
constructor TFileExtractor.Create(ADecompressorClass: TCustomDecompressorClass);
begin
inherited Create;
FOpenedSlice := -1;
FChunkFirstSlice := -1;
{ Create one 'decompressor' for use with uncompressed chunks, and another
for use with compressed chunks }
FDecompressor[False] := TStoredDecompressor.Create(ReadProc);
FDecompressor[True] := ADecompressorClass.Create(ReadProc);
end;
destructor TFileExtractor.Destroy;
begin
FSourceF.Free;
FDecompressor[True].Free;
FDecompressor[False].Free;
inherited;
end;
var
LastSourceDir: String;
class function TFileExtractor.FindSliceFilename(const ASlice: Integer): String;
var
Major, Minor: Integer;
Prefix, F1, F2, Path: String;
begin
Prefix := PathChangeExt(PathExtractName(SetupLdrOriginalFilename), '');
Major := ASlice div SetupHeader.SlicesPerDisk + 1;
Minor := ASlice mod SetupHeader.SlicesPerDisk;
if SetupHeader.SlicesPerDisk = 1 then
F1 := Format('%s-%d.bin', [Prefix, Major])
else
F1 := Format('%s-%d%s.bin', [Prefix, Major, Chr(Ord('a') + Minor)]);
F2 := Format('..\DISK%d\', [Major]) + F1;
if LastSourceDir <> '' then begin
Result := AddBackslash(LastSourceDir) + F1;
if NewFileExists(Result) then Exit;
end;
Result := AddBackslash(SourceDir) + F1;
if NewFileExists(Result) then Exit;
if LastSourceDir <> '' then begin
Result := PathExpand(AddBackslash(LastSourceDir) + F2);
if NewFileExists(Result) then Exit;
end;
Result := PathExpand(AddBackslash(SourceDir) + F2);
if NewFileExists(Result) then Exit;
Path := SourceDir;
LogFmt('Asking user for new disk containing "%s".', [F1]);
if SelectDisk(Major, F1, Path) then begin
LastSourceDir := Path;
Result := AddBackslash(Path) + F1;
end
else
Abort;
end;
procedure TFileExtractor.OpenSlice(const ASlice: Integer);
var
Filename: String;
TestDiskSliceID: TDiskSliceID;
DiskSliceHeader: TDiskSliceHeader;
begin
if FOpenedSlice = ASlice then
Exit;
FOpenedSlice := -1;
FreeAndNil(FSourceF);
if SetupLdrOffset1 = 0 then
Filename := FindSliceFilename(ASlice)
else
Filename := SetupLdrOriginalFilename;
FSourceF := TFile.Create(Filename, fdOpenExisting, faRead, fsRead);
if SetupLdrOffset1 = 0 then begin
if FSourceF.Read(TestDiskSliceID, SizeOf(TestDiskSliceID)) <> SizeOf(TestDiskSliceID) then
SourceIsCorrupted('Invalid slice header (1)');
if TestDiskSliceID <> DiskSliceID then
SourceIsCorrupted('Invalid slice header (2)');
if FSourceF.Read(DiskSliceHeader, SizeOf(DiskSliceHeader)) <> SizeOf(DiskSliceHeader) then
SourceIsCorrupted('Invalid slice header (3)');
if FSourceF.Size.Lo <> DiskSliceHeader.TotalSize then
SourceIsCorrupted('Invalid slice header (4)');
end;
FOpenedSlice := ASlice;
end;
procedure TFileExtractor.DecompressBytes(var Buffer; Count: Cardinal);
begin
try
FDecompressor[FChunkCompressed].DecompressInto(Buffer, Count);
except
{ If DecompressInto raises an exception, force a decompressor reset &
re-seek the next time SeekTo is called by setting FNeedReset to True.
We don't want to get stuck in an endless loop with the decompressor
in e.g. a data error state. Also, we have no way of knowing if
DecompressInto successfully decompressed some of the requested bytes
before the exception was raised. }
FNeedReset := True;
raise;
end;
Inc64(FChunkDecompressedBytesRead, Count);
end;
procedure TFileExtractor.SeekTo(const FL: TSetupFileLocationEntry;
const ProgressProc: TExtractorProgressProc);
procedure InitDecryption;
var
Salt: TSetupSalt;
Context: TSHA1Context;
Hash: TSHA1Digest;
begin
{ Read the salt }
if FSourceF.Read(Salt, SizeOf(Salt)) <> SizeOf(Salt) then
SourceIsCorrupted('Failed to read salt');
{ Initialize the key, which is the SHA-1 hash of the salt plus FCryptKey }
SHA1Init(Context);
SHA1Update(Context, Salt, SizeOf(Salt));
SHA1Update(Context, Pointer(FCryptKey)^, Length(FCryptKey)*SizeOf(FCryptKey[1]));
Hash := SHA1Final(Context);
ArcFourInit(FCryptContext, Hash, SizeOf(Hash));
{ The compiler discards the first 1000 bytes for extra security,
so we must as well }
ArcFourDiscard(FCryptContext, 1000);
end;
procedure Discard(Count: Integer64);
var
Buf: array[0..65535] of Byte;
BufSize: Cardinal;
begin
try
while True do begin
BufSize := SizeOf(Buf);
if (Count.Hi = 0) and (Count.Lo < BufSize) then
BufSize := Count.Lo;
if BufSize = 0 then
Break;
DecompressBytes(Buf, BufSize);
Dec64(Count, BufSize);
if Assigned(ProgressProc) then
ProgressProc(0);
end;
except
on E: ECompressDataError do
SourceIsCorrupted(E.Message);
end;
end;
var
TestCompID: TCompID;
Diff: Integer64;
begin
if FEntered <> 0 then
InternalError('Cannot call file extractor recursively');
Inc(FEntered);
try
if (foChunkEncrypted in FL.Flags) and (FCryptKey = '') then
InternalError('Cannot read an encrypted file before the key has been set');
{ Is the file in a different chunk than the current one?
Or, is the file in a part of the current chunk that we've already passed?
Or, did a previous decompression operation fail, necessitating a reset? }
if (FChunkFirstSlice <> FL.FirstSlice) or
(FChunkStartOffset <> FL.StartOffset) or
(Compare64(FL.ChunkSuboffset, FChunkDecompressedBytesRead) < 0) or
FNeedReset then begin
FChunkFirstSlice := -1;
FDecompressor[foChunkCompressed in FL.Flags].Reset;
FNeedReset := False;
OpenSlice(FL.FirstSlice);
FSourceF.Seek(SetupLdrOffset1 + FL.StartOffset);
if FSourceF.Read(TestCompID, SizeOf(TestCompID)) <> SizeOf(TestCompID) then
SourceIsCorrupted('Failed to read CompID');
if Longint(TestCompID) <> Longint(ZLIBID) then
SourceIsCorrupted('Invalid CompID');
if foChunkEncrypted in FL.Flags then
InitDecryption;
FChunkFirstSlice := FL.FirstSlice;
FChunkLastSlice := FL.LastSlice;
FChunkStartOffset := FL.StartOffset;
FChunkBytesLeft := FL.ChunkCompressedSize;
FChunkDecompressedBytesRead.Hi := 0;
FChunkDecompressedBytesRead.Lo := 0;
FChunkCompressed := foChunkCompressed in FL.Flags;
FChunkEncrypted := foChunkEncrypted in FL.Flags;
end;
{ Need to seek forward in the chunk? }
if Compare64(FL.ChunkSuboffset, FChunkDecompressedBytesRead) > 0 then begin
Diff := FL.ChunkSuboffset;
Dec6464(Diff, FChunkDecompressedBytesRead);
Discard(Diff);
end;
finally
Dec(FEntered);
end;
end;
function TFileExtractor.ReadProc(var Buf; Count: Longint): Longint;
var
Buffer: Pointer;
Left, Res: Cardinal;
begin
Buffer := @Buf;
Left := Count;
if (FChunkBytesLeft.Hi = 0) and (FChunkBytesLeft.Lo < Left) then
Left := FChunkBytesLeft.Lo;
Result := Left;
while Left <> 0 do begin
Res := FSourceF.Read(Buffer^, Left);
Dec64(FChunkBytesLeft, Res);
{ Decrypt the data after reading from the file }
if FChunkEncrypted then
ArcFourCrypt(FCryptContext, Buffer^, Buffer^, Res);
if Left = Res then
Break
else begin
Dec(Left, Res);
Inc(Longint(Buffer), Res);
{ Go to next disk }
if FOpenedSlice >= FChunkLastSlice then
{ Already on the last slice, so the file must be corrupted... }
SourceIsCorrupted('Already on last slice');
OpenSlice(FOpenedSlice + 1);
end;
end;
end;
procedure TFileExtractor.DecompressFile(const FL: TSetupFileLocationEntry;
const DestF: TFile; const ProgressProc: TExtractorProgressProc;
const VerifyChecksum: Boolean);
var
BytesLeft: Integer64;
Context: TSHA1Context;
AddrOffset: LongWord;
BufSize: Cardinal;
Buf: array[0..65535] of Byte;
{ ^ *must* be the same buffer size used by the compiler (TCompressionHandler),
otherwise the TransformCallInstructions call will break }
begin
if FEntered <> 0 then
InternalError('Cannot call file extractor recursively');
Inc(FEntered);
try
BytesLeft := FL.OriginalSize;
{ To avoid file system fragmentation, preallocate all of the bytes in the
destination file }
DestF.Seek64(BytesLeft);
DestF.Truncate;
DestF.Seek(0);
SHA1Init(Context);
try
AddrOffset := 0;
while True do begin
BufSize := SizeOf(Buf);
if (BytesLeft.Hi = 0) and (BytesLeft.Lo < BufSize) then
BufSize := BytesLeft.Lo;
if BufSize = 0 then
Break;
DecompressBytes(Buf, BufSize);
if foCallInstructionOptimized in FL.Flags then begin
TransformCallInstructions(Buf, BufSize, False, AddrOffset);
Inc(AddrOffset, BufSize); { may wrap, but OK }
end;
Dec64(BytesLeft, BufSize);
SHA1Update(Context, Buf, BufSize);
DestF.WriteBuffer(Buf, BufSize);
if Assigned(ProgressProc) then
ProgressProc(BufSize);
end;
except
on E: ECompressDataError do
SourceIsCorrupted(E.Message);
end;
if VerifyChecksum and not SHA1DigestsEqual(SHA1Final(Context), FL.SHA1Sum) then
SourceIsCorrupted('SHA-1 hash mismatch');
finally
Dec(FEntered);
end;
end;
end.