-
Notifications
You must be signed in to change notification settings - Fork 5
/
IdSoapResourceFile.pas
423 lines (388 loc) · 11.4 KB
/
IdSoapResourceFile.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
unit IdSoapResourceFile;
{$I IdSoapDefines.inc}
interface
uses
Contnrs,
{$IFDEF UNICODE}
System.Generics.Collections,
System.Types,
{$ENDIF}
IdSoapDebug,
IdSoapUtilities;
type
TIdSoapResourceEntry = class(TIdBaseObject)
private
FResType: PChar;
FSize: Cardinal;
FDataVersion: Cardinal;
FCharacteristics: Cardinal;
FHeaderSize: Cardinal;
FVersion: Cardinal;
FData: Pointer;
FName: String;
FLanguage: Word;
FFlags: Word;
procedure SetSize(const Value: Cardinal);
function GetResType: PChar;
procedure SetResType(const Value: PChar);
procedure SetName(const Value: String);
public
constructor create;
destructor Destroy; override;
Property HeaderSize: Cardinal read FHeaderSize;
Property ResType: PChar read GetResType write SetResType; // eg RT_RCDATA
Property Name: String read FName write SetName;
Property DataVersion: Cardinal read FDataVersion write FDataVersion;
Property Flags: Word read FFlags write FFlags;
Property Language: Word read FLanguage write FLanguage;
Property Version: Cardinal read FVersion write FVersion;
Property Characteristics: Cardinal read FCharacteristics write FCharacteristics;
Property Data: Pointer read FData; // Dont forget to set the size BEFORE you move the data in
Property Size: Cardinal read FSize write SetSize;
end;
TIdSoapResourceFile = Class(TIdBaseObject)
private
FResources: TObjectList;
function GetCount: Integer;
function GetResourceByName(AName: String): TIdSoapResourceEntry;
function GetResources(AIndex: Integer): TIdSoapResourceEntry;
public
Constructor Create;
destructor Destroy; Override;
Procedure LoadFromFile(AFilename: String);
Procedure SaveToFile(AFilename: String);
Function Add(AResource: TIdSoapResourceEntry): Integer;
Procedure Delete(AIndex: Integer);
function Extract(AEntry: TIdSoapResourceEntry): TIdSoapResourceEntry;
procedure Clear;
property Count: Integer read GetCount;
Property Resources[AIndex: Integer]: TIdSoapResourceEntry read GetResources; default;
Property ResourceByName[AName: String]: TIdSoapResourceEntry read GetResourceByName;
end;
implementation
Uses
Classes,
SysUtils;
type
PCardinal = ^Cardinal;
ResourceHeaderArray = array [1..8] of Cardinal;
const
GValidResourceHeader : ResourceHeaderArray = (0,$20,$ffff,$ffff,0,0,0,0);
procedure FreeInternalResType(var ARes: PChar);
begin
if (Cardinal(ARes) and $ffff0000) <> 0 then // its a real string
begin
FreeMem(ARes);
end;
ARes := nil;
end;
function CopyInternalResourceType(ARes: PChar): PChar;
var
LLen: Integer;
begin
if (Cardinal(ARes) and $ffff0000) = 0 then
begin
result := ARes;
end
else
begin
LLen := (Length(ARes)+1) * Sizeof(Char);
GetMem(result,LLen);
move(ARes^,result^,LLen);
end;
end;
function SameHeader(const AHeader1,AHeader2: ResourceHeaderArray): Boolean;
var
i: Integer;
begin
for i:=1 to 8 do
begin
if AHeader1[i] <> AHeader2[i] then
begin
result := false;
exit;
end;
end;
result := true;
end;
{ TIdSoapResourceEntry }
constructor TIdSoapResourceEntry.create;
begin
inherited;
FData := nil;
FResType := nil;
end;
destructor TIdSoapResourceEntry.destroy;
begin
if Assigned(FData) then
begin
FreeMem(FData);
FData := nil;
end;
FreeInternalResType(FResType);
inherited;
end;
function TIdSoapResourceEntry.GetResType: PChar;
begin
result := FResType;
end;
procedure TIdSoapResourceEntry.SetSize(const Value: Cardinal);
begin
FSize := Value;
if Assigned(FData) then
begin
FreeMem(FData);
end;
FData := nil;
if Value > 0 then
begin
GetMem(FData,Value);
end;
end;
procedure TIdSoapResourceEntry.SetName(const Value: String);
begin
FName := AnsiUpperCase(Value); // for whatever reason, windows only seems to like upper case resource names
end;
procedure TIdSoapResourceEntry.SetResType(const Value: PChar);
begin
FreeInternalResType(FResType);
if (Cardinal(Value) and $ffff0000) <> 0 then
begin
FResType := CopyInternalResourceType(Value);
end
else
begin
FResType := Value;
end;
end;
{ TIdSoapResourceFile }
function TIdSoapResourceFile.Add(AResource: TIdSoapResourceEntry): Integer;
begin
result := -1;
if Assigned(AResource) then
begin
Result := FResources.Add(AResource);
end;
end;
procedure TIdSoapResourceFile.Clear;
begin
FResources.Clear;
end;
constructor TIdSoapResourceFile.Create;
begin
inherited;
FResources := TObjectList.Create;
FResources.OwnsObjects := True;
end;
procedure TIdSoapResourceFile.Delete(AIndex: Integer);
begin
FResources.Delete(AIndex);
end;
destructor TIdSoapResourceFile.Destroy;
begin
FreeAndNil(FResources);
Inherited;
end;
function TIdSoapResourceFile.Extract(AEntry: TIdSoapResourceEntry): TIdSoapResourceEntry;
begin
result := TObject(FResources.Extract(AEntry)) as TIdSoapResourceEntry;
end;
function TIdSoapResourceFile.GetCount: Integer;
begin
result := FResources.Count;
end;
function TIdSoapResourceFile.GetResourceByName(AName: String): TIdSoapResourceEntry;
var
i: Integer;
begin
result := nil;
AName := UpperCase(AName);
for i:=0 to Count-1 do
begin
if AName = Resources[i].Name then
begin
result := Resources[i];
break;
end;
end;
end;
function TIdSoapResourceFile.GetResources(AIndex: Integer): TIdSoapResourceEntry;
begin
result := FResources[AIndex] as TIdSoapResourceEntry;
end;
Function LoadIdent(AStream: TFileStream): PChar;
var
LWord: Word;
LString: String;
begin
AStream.ReadBuffer(LWord,sizeof(LWord));
if LWord = $ffff then
begin
AStream.ReadBuffer(LWord,sizeof(LWord));
result := PChar(LWord);
end
else
begin
LString := '';
while LWord <> 0 do
begin
LString := LString + Char(LWord);
AStream.ReadBuffer(LWord,sizeof(LWord));
end;
Result := CopyInternalResourceType(@LString[1]);
end;
end;
procedure SaveIdent(AStream: TFileStream; AData: PChar);
var
LCardinal: Cardinal;
LErr: Integer;
LWord: Word;
LWideChar: WideChar;
LWideString: WideString;
begin
if (Cardinal(AData) and $ffff0000) = 0 then
begin
LWord := $ffff;
AStream.WriteBuffer(LWord,sizeof(LWord));
LWord := Cardinal(AData) and $ffff;
AStream.WriteBuffer(LWord,sizeof(LWord));
end
else
begin
Assert(AData <> '','You must have a non empty string for a TYPE or resource NAME');
if copy(AData,1,1) = '#' then // its a number
begin
val(copy(AData,2,MaxInt),LCardinal,LErr);
if LErr <> 0 then
begin
raise Exception.Create('Invalid number for resource name "' + AData + '"');
end;
if LCardinal > $ffff then
begin
raise Exception.Create('Invalid WORD size "' + AData + '"');
end;
LWord := $ffff;
AStream.WriteBuffer(LWord,sizeof(LWord));
AStream.WriteBuffer(LCardinal,sizeof(Word)); // only write a word size here
end
else
begin
{$IFDEF UNICODE}
LWideString := AData;
{$ELSE}
LWideString := '';
repeat
LWideChar := WideChar(ord(AData^));
if LWideChar <> #0 then
begin
LWideString := LWideString + LWideChar;
end;
inc(AData);
until LWideChar = #0;
{$ENDIF}
AStream.WriteBuffer(LWideString[1],(length(LWideString)+1) * Sizeof(WideChar));
end;
end;
end;
procedure TIdSoapResourceFile.LoadFromFile(AFilename: String);
var
LStream: TFileStream;
LEntry: TIdSoapResourceEntry;
LCardinal: Cardinal;
LTemp: ResourceHeaderArray;
LNextPosition: Cardinal;
LPChar: PChar;
begin
LStream := TFileStream.Create(AFilename,fmOpenRead);
try
LStream.ReadBuffer(LTemp,sizeof(LTemp)); // read resource header
if not SameHeader(LTemp,GValidResourceHeader) then
begin
Raise Exception.Create('Invalid resource file');
end;
while LStream.Position < LStream.Size do
begin
LEntry := TIdSoapResourceEntry.Create;
try
LNextPosition := LStream.Position;
LStream.ReadBuffer(LCardinal,sizeof(LCardinal));
LEntry.Size := LCardinal;
LStream.ReadBuffer(LEntry.FHeaderSize,sizeof(LEntry.FHeaderSize));
LNextPosition := LNextPosition + LEntry.FHeaderSize;
LPChar := LoadIdent(LStream);
LEntry.ResType := LPChar;
FreeInternalResType(LPChar);
LEntry.Name := LoadIdent(LStream);
LStream.ReadBuffer(LEntry.FDataVersion,sizeof(LEntry.FDataVersion));
LStream.ReadBuffer(LEntry.FFlags,sizeof(LEntry.FFlags));
LStream.ReadBuffer(LEntry.FLanguage,sizeof(LEntry.FLanguage));
LStream.ReadBuffer(LEntry.FVersion,sizeof(LEntry.FVersion));
LStream.ReadBuffer(LEntry.FCharacteristics,sizeof(LEntry.FCharacteristics));
LStream.Position := LNextPosition;
LStream.ReadBuffer(LEntry.FData^,LEntry.Size);
Add(LEntry);
except
FreeAndNil(LEntry);
raise;
end;
end;
finally
FreeAndNil(LStream);
end;
end;
procedure TIdSoapResourceFile.SaveToFile(AFilename: String);
var
LStream: TFileStream;
LEntry: TIdSoapResourceEntry;
LMark,LEnd: Integer;
i: Integer;
b: Byte;
begin
LStream := TFileStream.Create(AFileName,fmCreate or fmOpenWrite);
try
LStream.WriteBuffer(GValidResourceHeader,Sizeof(GValidResourceHeader));
for i:=0 to Count-1 do
begin
LEntry := Resources[i];
LStream.WriteBuffer(LEntry.FSize,sizeof(LEntry.FSize));
LMark := LStream.Position; // might need to come back here to align stream
LStream.WriteBuffer(LEntry.FHeaderSize,sizeof(LEntry.FHeaderSize));
SaveIdent(LStream,LEntry.ResType);
SaveIdent(LStream,@LEntry.Name[1]);
LStream.WriteBuffer(LEntry.FDataVersion,sizeof(LEntry.FDataVersion));
LStream.WriteBuffer(LEntry.FLags,sizeof(LEntry.FFlags));
LStream.WriteBuffer(LEntry.FLanguage,sizeof(LEntry.FLanguage));
LStream.WriteBuffer(LEntry.FVersion,sizeof(LEntry.FVersion));
LStream.WriteBuffer(LEntry.FCharacteristics,sizeof(LEntry.FCharacteristics));
while (LStream.Position and 3) <> 0 do // we should be good and DWORD align it
begin
b := 0;
LStream.WriteBuffer(b,sizeof(b));
end;
LEnd := LStream.Position;
LStream.Position := LMark;
LEntry.FHeaderSize := LEnd - LMark + sizeof(Cardinal); // dont forget the Size
if LEntry.FHeaderSize < 32 then // must have at least 32 bytes
begin
LEntry.FHeaderSize := 32;
end;
LStream.WriteBuffer(LEntry.FHeaderSize,sizeof(LEntry.FHeaderSize));
LStream.Position := LEnd;
if LEntry.Size <> 0 then
begin
LStream.WriteBuffer(LEntry.Data^,LEntry.Size);
end;
if i < Count-1 then
begin
while (LStream.Position and 3) <> 0 do // we should be good and DWORD align it
begin
b := 0;
LStream.WriteBuffer(b,sizeof(b));
end;
end;
end;
finally
FreeAndNil(LStream);
end;
end;
end.