-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCP.pas
305 lines (277 loc) · 9.35 KB
/
DCP.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
unit DCP;
interface
(*
The *.DCP (Delphi Compiled Package) support routines and data types
of the DCU32INT utility by Alexei Hmelnov.
----------------------------------------------------------------------------
E-Mail: [email protected]
http://hmelnov.icc.ru/DCU/
----------------------------------------------------------------------------
See the file "readme.txt" for more details.
------------------------------------------------------------------------
IMPORTANT NOTE:
This software is provided 'as-is', without any expressed or implied warranty.
In no event will the author be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented, you must not
claim that you wrote the original software.
2. Altered source versions must be plainly marked as such, and must not
be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
uses
SysUtils, Classes;
type
PDCPHdr = ^TDCPHdr;
TDCPHdr = record
{ %$IF MSIL;
ulong X0 //DCPIL files contain additional DWORD
%$END }
nRequires, nContains: LongInt;
SzContains: Cardinal;
pContains: Cardinal;
// The rest of header is ignored here
end;
PDCPUnitHdr = ^TDCPUnitHdr;
TDCPUnitHdr = record
Magic: LongInt;
FileSize: LongInt;
end;
PDCPUnitInfo = ^TDCPUnitInfo;
TDCPUnitInfo = record
pData: Cardinal; // PDCPUnitHdr
F, FT: Cardinal;
bplCode, bplData: LongInt;
{
%$IF Ver>4; //Not checked for D4
ulong bplBSS
%$END
%$IF (Ver>=9)or MSIL;
ulong X
%$END
PChar Name
%$IF Ver>=9;
PChar Name1
%$END }
end;
TDCPLoadState = (dcplNotLoaded, dcplHeaderLoaded, dcplAllLoaded);
TDCPackage = class(TStringList)
protected
FFName: String; // for the two phase loading mode
FMemPtr: Pointer;
FMemSize, FSizeR: Cardinal;
FOk: boolean;
FLoaded: TDCPLoadState;
procedure AllLoadedRequired;
function GetFileData(i: Integer): PDCPUnitHdr;
function CheckDCPOfs(Ofs: Cardinal; const Msg: String): Pointer;
public
destructor Destroy; override;
function Load(const FN: String; IsMain: boolean): boolean;
function GetFileByName(const Name: String): PDCPUnitHdr;
property FileData[i: Integer]: PDCPUnitHdr read GetFileData;
end;
var
TwoPhaseDCPLoad: boolean = true; // false => load all data at once,
// true => load header and all the other data before fetching 1st unit
TwoPhaseDCPSizeLimit: Cardinal = 65536; // Heuristic limit, smaller or eq packages won't be loaded in two phases
TwoPhaseInitSize: Cardinal = $800; // Should be < TwoPhaseDCPSizeLimit and >$24 (The largest package header size)
implementation
type
TIncPtr = PAnsiChar;
procedure DCPErr(const Msg: String);
begin
raise Exception.Create(Msg);
end;
{ TDCPackage. }
destructor TDCPackage.Destroy;
begin
if FMemPtr <> Nil then
FreeMem(FMemPtr, FMemSize);
inherited Destroy;
end;
function TDCPackage.CheckDCPOfs(Ofs: Cardinal; const Msg: String): Pointer;
begin
if (Ofs < 0) or (Ofs >= FMemSize) then
DCPErr(Msg);
Result := TIncPtr(FMemPtr) + Ofs;
end;
type
TMagicChars = array [0 .. 3] of AnsiChar;
const
DCPMagic: TMagicChars = 'PKG'#0;
DCPMagicX: TMagicChars = 'PKX0';
DCPMagicMask = $00FFFFFF;
function TDCPackage.Load(const FN: String; IsMain: boolean): boolean;
var
F: File;
TwoPhase, isMSIL: boolean;
Magic: LongInt;
DP: Pointer;
SzRq: Cardinal;
VerCh: AnsiChar;
Hdr: PDCPHdr;
NP, EP: PAnsiChar;
UI, UI0: PDCPUnitInfo;
UD: PDCPUnitHdr;
i, l, NChk: Integer;
NS: String;
begin
if FLoaded > dcplNotLoaded then begin
Result := FOk;
Exit;
end;
TwoPhase := false;
FLoaded := dcplAllLoaded;
Result := false;
FFName := FN;
AssignFile(F, FN);
FileMode := 0; // Read only, helps with DCUs on CD
Reset(F, 1);
try
FMemSize := FileSize(F);
if FMemSize < $40 then
DCPErr('Package file is too small.');
GetMem(FMemPtr, FMemSize);
// We'll alloc all the memory at once anyway, cause even for DXE6\win64
// all the *.DCP files take less than 200Mb - easily fits into memory of any computer
FSizeR := FMemSize;
if TwoPhaseDCPLoad and not IsMain and (FMemSize > TwoPhaseDCPSizeLimit) then begin
FLoaded := dcplHeaderLoaded;
TwoPhase := true;
FSizeR := TwoPhaseInitSize;
end;
BlockRead(F, FMemPtr^, FSizeR);
isMSIL := UpCase(FN[Length(FN)]) = 'L'; // DCPIL
DP := FMemPtr;
Magic := LongInt(DP^);
Inc(TIncPtr(DP), SizeOf(LongInt));
if Magic = LongInt(DCPMagicX) then begin
VerCh := 'X';
NChk := 3;
end
else begin
NChk := 0;
if (Magic and DCPMagicMask) <> LongInt(DCPMagic) then
DCPErr('Wrong PKG magic.');
VerCh := TMagicChars(Magic)[3];
if (VerCh < '4') or (VerCh > '9') or (VerCh = '6') or (VerCh = '8') then
DCPErr('Wrong PKG version.');
end;
if isMSIL then
Inc(TIncPtr(DP), SizeOf(LongInt)); // MSIL
Hdr := DP;
if (Hdr^.nRequires > $100 { Empirical limitation } ) or (Hdr^.nRequires < 0) then
DCPErr('Package requires too much.');
UI0 := CheckDCPOfs(Hdr^.pContains, 'Wrong contains pointer.');
EP := CheckDCPOfs(Hdr^.pContains + Hdr^.SzContains, 'Wrong contains size.');
if TwoPhase then begin
SzRq := Hdr^.pContains + Hdr^.SzContains;
if SzRq > FSizeR then begin
BlockRead(F, TIncPtr(FMemPtr)[FSizeR], SzRq - FSizeR);
FSizeR := SzRq;
end;
end;
finally
Close(F);
end;
if (EP - 1)^ <> #0 then // Make sure that the last char is #0 => StrLen is safe
DCPErr('Bad package Contains table end.');
repeat // The loop is used to detect the valid version of DCP with PKX0 magic
UI := UI0;
try
EP := TIncPtr(UI0);
for i := 0 to Hdr^.nContains - 1 do begin
if (TIncPtr(UI) - TIncPtr(UI0) + SizeOf(TDCPUnitInfo) >= Hdr^.SzContains) then
break;
UD := CheckDCPOfs(UI^.pData, 'Wrong unit data pointer.');
if not TwoPhase then
CheckDCPOfs(UI^.pData + UD^.FileSize - 1, 'Wrong unit data size');
NP := TIncPtr(UI) + SizeOf(TDCPUnitInfo);
if VerCh > '4' then
Inc(NP, SizeOf(LongInt));
if (VerCh >= '9') or isMSIL then
Inc(NP, SizeOf(LongInt));
if (VerCh >= 'X') then
Inc(NP, (5 + NChk) * SizeOf(LongInt));
if (TIncPtr(NP) - TIncPtr(UI0) >= Hdr^.SzContains) then
break;
EP := StrEnd(NP);
l := EP - NP;
if l > 255 then
DCPErr('Too long unit name.');
SetString(NS, NP, l);
AddObject(NS, Pointer(UD));
Inc(EP);
if VerCh >= '9' then begin
if (TIncPtr(EP) - TIncPtr(UI0) >= Hdr^.SzContains) then begin
Inc(EP); // make it >Hdr^.SzContains
break;
end;
EP := StrEnd(EP);
Inc(EP);
end;
UI := Pointer(EP);
end;
if (EP - TIncPtr(UI0) = Hdr^.SzContains) then
break { Good NChk found };
if (NChk <= 0) then
DCPErr('Bad Contains size.');
except
if NChk <= 0 then
raise;
end;
Dec(NChk, 3);
Clear;
until NChk < 0;
FOk := true;
Result := true;
end;
procedure TDCPackage.AllLoadedRequired;
var
F: File;
SzR: Integer;
begin
FLoaded := dcplAllLoaded;
if FSizeR >= FMemSize then
Exit { Paranoic };
AssignFile(F, FFName);
FileMode := 0; // Read only, helps with DCUs on CD
Reset(F, 1);
try
Seek(F, FSizeR);
BlockRead(F, TIncPtr(FMemPtr)[FSizeR], FMemSize - FSizeR, SzR);
Inc(FSizeR, SzR);
if FSizeR < FMemSize then begin
FillChar(TIncPtr(FMemPtr)[FSizeR], FMemSize - FSizeR, 0); // The error is not reported, but the memory will be zeroed
FOk := false;
end;
finally
Close(F);
end;
end;
function TDCPackage.GetFileByName(const Name: String): PDCPUnitHdr;
var
i: Integer;
begin
Result := Nil;
i := IndexOf(Name);
if i < 0 then
Exit;
if (FLoaded = dcplHeaderLoaded) and FOk then
AllLoadedRequired;
Result := PDCPUnitHdr(Objects[i]);
if TIncPtr(Result) - TIncPtr(FMemPtr) + Result^.FileSize > FMemSize then
Result := Nil; // For TwoPhase mode
end;
function TDCPackage.GetFileData(i: Integer): PDCPUnitHdr;
begin
if (FLoaded = dcplHeaderLoaded) and FOk then
AllLoadedRequired;
Result := PDCPUnitHdr(Objects[i]);
end;
end.