-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathResList.pas
More file actions
1480 lines (1257 loc) · 37.5 KB
/
ResList.pas
File metadata and controls
1480 lines (1257 loc) · 37.5 KB
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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
unit ResList;
// Object encapsulating a list of icon and cursor resources
// WARNING: Currently, saving to NE format is destructive!!
interface
uses
{$IFDEF WINDOWS} Windows, {$ENDIF} LclIntf, LclType, LResources,
SysUtils, Classes, DocClass, StreamEx;
type
TResIconType2 = (ritPage, ritGroup);
const
// First index: is it an icon(true) or cursor(false)?
resIconType: array[boolean, TResIconType2] of string =
(('1', '12'), ('3', '14'));
type
TResFileType = (rftNone, rftExe, rftRes);
TArrayOfInteger = array of integer;
const
// Default extension for saving -- that's why .icl is default instead of .exe!
rftDefaultExt: array[TResFileType] of string = ('', '.icl', '.res');
AnyLanguage = $feed;
type
// Valid resource names: the set of strings returned by resValidName
TResEntry = class(TPersistent)
private
FData: Pointer;
FDataSize: integer;
procedure SetDataSize(const Value: integer);
public
_Type: string;
Name: string;
Language: word;
property Data: Pointer read FData;
property DataSize: integer read FDataSize write SetDataSize;
constructor Create;
destructor Destroy; override;
procedure Assign(Src: TPersistent); override;
procedure Clear;
end;
TChangeIconOp = (ciDelete, ciNewLanguage);
TExeFormat = (exeNone, exeNE, exePE);
{ TResList }
TResList = class(TPersistent)
private
FItems: TList; // list of TResEntry;
function GetEntryCount: integer;
function GetEntry(Index: integer): TResEntry;
// Load/save Windows resource files
function LoadFromRes(const FileName: string): boolean;
function SaveToRes(const FileName: string): boolean;
// 16-bit executable files
function LoadFromNE(const FileName: string): boolean;
function SaveToNE(const FileName: string): boolean; // always destructive
// 32-bit executable files
function LoadFromPE(const FileName: string): boolean;
function SaveToPE(const FileName: string): boolean;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
property EntryCount: integer read GetEntryCount;
property Entries[Index: integer]: TResEntry read GetEntry; default;
// Language can be AnyLanguage
function IndexOf(const AType, AName: string; ALanguage: word;
CanCreate: boolean): integer;
function NewEntry: TResEntry;
procedure DeleteEntry(Index: integer);
procedure Append(rl: TResList);
// Looks for a free interval of resource ID's of the given Type and Language
function GetFreeInterval(const AType: string; ALanguage: word;
Count: integer): integer;
// Get indexes of all the resource entries for individual resource pages
// where e is the group resource entry
function GetPages(e: TResEntry; out Index: TArrayOfInteger): boolean;
// Get an icon or cursor
function GetIconToFile(IsIcon: boolean; const AName: string; ALanguage: word;
const fn: string): boolean;
function GetIconToStream(IsIcon: boolean; const AName: string; ALanguage: word;
s: TStream): boolean;
function GetIconToDoc(IsIcon: boolean; const AName: string; ALanguage: word;
Doc: TIconDoc): boolean;
procedure SetResourceFromMemory(const AType: string; const AName: string;
ALanguage: word; AData: Pointer; ADataSize: integer);
// Set an icon or cursor
procedure SetIconFromStream(IsIcon: boolean; const AName: string; ALanguage: word;
s: TStream);
procedure SetIconFromFile(IsIcon: boolean; const AName: string; ALanguage: word;
const fn: string);
// Delete icon/Set language
function ChangeIcon(IsIcon: boolean; const AName: string; ALanguage: word;
Op: TChangeIconOp; Param: integer): boolean;
// Load/save
function LoadFromFile(const FileName: string): TResFileType;
// if ExeFormat is given, then forces saving in the given format
// else, detects format of file (PE or NE)
function SaveToFile(const FileName: string; FileType: TResFileType;
ExeFormat: TExeFormat = exeNone): boolean;
end;
// Detects file type from stream contents
function DetectResFileType(s: TStream): TResFileType;
// Detects file type from extension of filename
function DetectResFileTypeFromName(const fn: string): TResFileType;
function exeDetectFormat(const FileName: string): TExeFormat;
function CanHandlePE: boolean;
function resValidName(const s: string): string;
implementation
uses
Math, Dialogs, bmExUtils, ICO, dlgDebug;
type
IMAGE_DOS_HEADER = packed record
e_magic: word;
e_cblp: word;
e_cp: word;
e_crlc: word;
e_cparhdr: word;
e_minalloc: word;
e_maxalloc: word;
e_ss: word;
e_sp: word;
e_csum: word;
e_ip: word;
e_cs: word;
e_lfarlc: word;
e_ovno: word;
e_res: array[0..3] of word;
e_oemid: word;
e_oeminfo: word;
e_res2: array[0..9] of word;
e_lfanew: longint;
end;
IMAGE_OS2_HEADER = packed record
ne_magic: word;
ne_ver: char;
ne_rev: char;
ne_enttab: word;
ne_cbenttab: word;
ne_crc: longint;
ne_flags: word;
ne_autodata: word;
ne_heap: word;
ne_stack: word;
ne_csip: longint;
ne_sssp: longint;
ne_cseg: word;
ne_cmod: word;
ne_cbnrestab: word;
ne_segtab: word;
ne_rsrctab: word;
ne_restab: word;
ne_modtab: word;
ne_imptab: word;
ne_nrestab: word;
ne_cmovent: word;
ne_align: word;
ne_cres: word;
ne_exetyp: word;
ne_flagsothers: word;
ne_pretthunks: word;
ne_psegrefbytes: word;
ne_swaparea: word;
ne_expver: word;
end;
TNAMEINFO = packed record
Offset, Len, Flags, ID, Handle, Usage: word;
end;
// When stored as a resource, cursor hot spot is written to
// the RT_CURSOR resource
// PRES_ICONDIRENTRY = ^RES_ICONDIRENTRY;
RES_ICONDIRENTRY = packed record
d: packed record
case Integer of
0: (bWidth, bHeight, bColorCount, bReserved: byte;);
1: (wWidth, wHeight: word;);
end;
wPlanes, wBitCount: word;
dwBytesInRes: Cardinal;
nID: word;
end;
PRES_ICONHEADER = ^RES_ICONHEADER;
RES_ICONHEADER = packed record
id: ICONDIR;
ide: array[0..0] of RES_ICONDIRENTRY;
end;
function exeDetect(Data: Pointer; Size: integer): boolean;
begin
Result := (Size >= 2) and (PChar(Data)[0] = 'M') and (PChar(Data)[1] = 'Z');
end;
function resDetect(Data: Pointer; Size: integer): boolean;
begin
Result := (Size >= 16) and
(PIntegerArray(Data)[2] = $0000ffff) and
(PIntegerArray(Data)[3] = $0000ffff);
end;
function DetectResFileType;
const
MAX_HEADER_SIZE = 100;
var
oldPos, HeaderSize: integer;
Header: array[0..MAX_HEADER_SIZE-1] of byte;
begin
Result := rftNone;
try
oldPos := s.Position;
HeaderSize := s.Read(Header, MAX_HEADER_SIZE);
s.Position := oldPos;
if exeDetect(@Header, HeaderSize) then Result := rftExe else
if resDetect(@Header, HeaderSize) then Result := rftRes;
except
end;
end;
function DetectResFileTypeFromName;
var
Ext: string;
begin
Ext := UpperCase(ExtractFileExt(fn));
if (Ext = '.EXE') or (Ext = '.DLL') or (Ext = '.SCR') or (Ext = '.CPL') or
(Ext = '.OCX') or (Ext = '.VBX') or (Ext = '.BPL') or (Ext = '.ICL') or
(Ext = '.IL') then Result := rftExe else
if Ext = '.RES' then Result := rftRes else
Result := rftNone;
end;
function exeDetectFormat;
var
s: TStream;
dosh: IMAGE_DOS_HEADER;
magic: string;
begin
Result := exeNone;
try
s := TFileStream.Create(FileName, fmOpenRead);
try
s.ReadBuffer(dosh, SizeOf(dosh));
s.Position := dosh.e_lfanew;
SetLength(magic, 2);
s.ReadBuffer(magic[1], 2);
finally
s.Free;
end;
if magic = 'NE' then Exit(exeNE);
if magic = 'PE' then Exit(exePE);
except
end;
end;
function CanHandlePE: boolean;
begin
{$ifdef WINDOWS}
Result := true;
{$else}
Result := false;
{$endif}
end;
function resValidName;
var
i: integer;
begin
Result := UpperCase(s);
for i := 1 to Length(Result) do
if not (Result[i] in ['A'..'Z', '0'..'9', '_']) then Result[i] := '_';
if Result = '0' then Result := '_0';
end;
// Misc
function ResNameToStr(sz: PChar): string;
begin
if PtrUInt(sz) < $10000 then
Result := IntToStr(PtrUInt(sz)) else
Result := String(sz);
end;
function ResNameIsNumber(const s: string): boolean;
var
i: integer;
begin
Result := TryStrToInt(s, i) and (i >= 0) and (i < $10000);
end;
function StrToResNameW(const s: string): PWideChar;
begin
if ResNameIsNumber(s) then
PtrInt(Result) := StrToInt(s) else
Result := PWideChar(WideString(s));
end;
// TResEntry
procedure TResEntry.SetDataSize;
begin
if FDataSize <> Value then
begin
FDataSize := Value;
FreeAndNilMem(FData);
if DataSize > 0 then
FData := GetMem(DataSize);
end;
end;
constructor TResEntry.Create;
begin
inherited;
_Type := '';
Name := '';
Language := 0;
FData := nil;
FDataSize := 0;
Clear;
end;
destructor TResEntry.Destroy;
begin
DataSize := 0;
inherited;
end;
procedure TResEntry.Assign;
var
x: TResEntry;
begin
if Src = Self then Exit;
if Src is TResEntry then
begin
x := Src as TResEntry;
_Type := x._Type;
Name := x.Name;
Language := x.Language;
DataSize := x.DataSize;
Move(x.Data^, Data^, DataSize);
end else
inherited;
end;
procedure TResEntry.Clear;
begin
_Type := '';
Name := '';
Language := 0;
DataSize := 0;
end;
// For sorting a list of TResEntry
function TResEntry_Compare(Item1, Item2: Pointer): Integer;
var
a: TResEntry absolute Item1;
b: TResEntry absolute Item2;
begin
if a._Type < b._Type then Exit(-1);
if a._Type > b._Type then Exit(1);
if a.Name < b.Name then Exit(-1);
if a.Name > b.Name then Exit(1);
if a.Language < b.Language then Exit(-1);
if a.Language > b.Language then Exit(1);
Result := 0;
end;
// TResList
function TResList.GetEntryCount: integer;
begin
Result := FItems.Count;
end;
function TResList.GetEntry(Index: integer): TResEntry;
begin
Result := FItems[Index];
end;
constructor TResList.Create;
begin
inherited;
FItems := TList.Create;
end;
destructor TResList.Destroy;
begin
Clear;
FItems.Free;
inherited;
end;
procedure TResList.Clear;
var
i: integer;
begin
for i := 0 to FItems.Count - 1 do Entries[i].Free;
FItems.Clear;
end;
function TResList.IndexOf(const AType, AName: string; ALanguage: word;
CanCreate: boolean): integer;
begin
for Result := 0 to EntryCount - 1 do with Entries[Result] do
if (_Type = AType) and (Name = AName) and
((Language = ALanguage) or (ALanguage = AnyLanguage)) then
Exit;
if CanCreate then
begin
with NewEntry do
begin
_Type := AType;
Name := AName;
Language := ALanguage;
end;
Result := EntryCount - 1;
end else
Result := -1;
end;
function TResList.NewEntry: TResEntry;
begin
Result := TResEntry.Create;
FItems.Add(Result);
end;
procedure TResList.DeleteEntry(Index: integer);
begin
Entries[Index].Free;
FItems.Delete(Index);
end;
procedure TResList.Append(rl: TResList);
var
i: integer;
begin
for i := 0 to rl.EntryCount - 1 do NewEntry.Assign(rl[i]);
end;
function TResList.GetFreeInterval(const AType: string; ALanguage: word;
Count: integer): integer;
var
i: integer;
begin
// initialize start of interval
Result := -1;
for i := 1 to $ffff do
if IndexOf(AType, IntToStr(i), ALanguage, False) < 0 then
begin
// new free place
if Result < 0 then Result := i;
// found enough free places?
if i - Result + 1 >= Count then Exit;
end else
// occupied place
Result := -1;
// If we haven't exited sofar, there is no free interval
Result := -1;
end;
function TResList.GetPages(e: TResEntry; out Index: TArrayOfInteger): boolean;
var
rih: PRES_ICONHEADER;
IsIcon: boolean;
i: integer;
id: string;
begin
Result := False;
// heuristic: idCount must not be too big
rih := e.Data;
if rih.id.idCount > 256 then Exit;
IsIcon := (e._Type = resIconType[True, ritGroup]);
SetLength(Index, rih.id.idCount);
for i := 0 to rih.id.idCount - 1 do
begin
id := IntToStr(rih.ide[i].nID);
// first attempt: use same language as icon directory
Index[i] := IndexOf(resIconType[IsIcon, ritPage],
id, e.Language, False);
if Index[i] < 0 then
// second attempt: use any language
begin
Index[i] := IndexOf(resIconType[IsIcon, ritPage],
id, AnyLanguage, False);
// failed?
if Index[i] < 0 then Exit;
end;
end; // for i
Result := True;
end;
function TResList.GetIconToDoc(IsIcon: boolean; const AName: string; ALanguage: word;
Doc: TIconDoc): boolean;
var
s: TMemoryStream;
begin
Result := False;
s := TMemoryStream.Create;
try
if not GetIconToStream(IsIcon, AName, ALanguage, s) then
Exit;
s.Position := 0;
icoLoadFromStream(Doc, s);
finally
s.Free;
end;
// success
Result := True;
end;
function TResList.GetIconToFile(IsIcon: boolean; const AName: string;
ALanguage: word; const fn: string): boolean;
var
s: TFileStream;
begin
Result := False;
s := TFileStream.Create(fn, fmCreate);
try
Result := GetIconToStream(IsIcon, AName, ALanguage, s);
finally
s.Free;
end;
end;
function TResList.GetIconToStream(IsIcon: boolean; const AName: string; ALanguage: word;
s: TStream): boolean;
var
i, j, iDir, Size, MaxSize: integer;
Offset: Cardinal;
e: TResEntry;
p: Pointer;
rih: PRES_ICONHEADER;
ide: ICONDIRENTRY;
SubIndex, IconPageBytes: TArrayOfInteger;
begin
Result := False;
// search for main icon directory resource
iDir := IndexOf(resIconType[IsIcon, ritGroup], AName, ALanguage, False);
if iDir < 0 then Exit;
e := Entries[iDir];
{$IFDEF GFIEDEBUG}
Log('Loading resource icon/cursor '+e._Type+' '+e.Name+', size '+IntToStr(e.DataSize));
{$ENDIF}
// get subindexes for page resources
if not GetPages(e, SubIndex) then Exit;
{$IFDEF GFIEDEBUG}
Log(IntToStr(Length(SubIndex))+' pages');
{$ENDIF}
rih := e.Data;
// convert resource directory
s.WriteBuffer(rih.id, SizeOf(rih.id));
Offset := SizeOf(ICONDIR) + Length(SubIndex) * SizeOf(ICONDIRENTRY);
SetLength(IconPageBytes, Length(SubIndex));
for i := 0 to Length(SubIndex) - 1 do
with rih.ide[i] do
begin
FillChar(ide, SizeOf(ide), 0);
ide.dwBytesInRes := dwBytesInRes;
if IsIcon then
begin
ide.bWidth := d.bWidth;
ide.bHeight := d.bHeight;
ide.bColorCount := d.bColorCount;
end else
begin
ide.bWidth := d.wWidth;
ide.bHeight := d.wHeight div 2;
// - 4, because hot spot (2 words) is stored along bitmaps in cursor resources
dec(ide.dwBytesInRes, 4);
// we will load the cursor hot spot coordinates later
end;
// common
ide.dwImageOffset := Offset;
IconPageBytes[i] := ide.dwBytesInRes;
inc(Offset, ide.dwBytesInRes);
// write
s.WriteBuffer(ide, SizeOf(ide));
end; // for i := pages
// copy pages
for i := 0 to Length(SubIndex) - 1 do
begin
e := Entries[SubIndex[i]];
p := e.Data;
Size := IconPageBytes[i];
MaxSize := e.DataSize;
// if cursor -> load hot spot from Data
if not IsIcon then
begin
j := s.Position;
s.Position := SizeOf(ICONDIR) + SizeOf(ICONDIRENTRY)*i + 4;
// hot spot X and Y are stored in Data in words -> 4 bytes total
s.WriteBuffer(p^, 4);
inc(PByte(p), 4);
dec(MaxSize, 4);
s.Position := j;
end;
// write data bytes
s.WriteBuffer(p^, Min(Size, MaxSize));
end; // for i
// success
Result := True;
end;
procedure TResList.SetResourceFromMemory(const AType: string; const AName: string;
ALanguage: word; AData: Pointer; ADataSize: integer);
var
e: TResEntry;
begin
e := NewEntry;
e._Type := AType;
e.Name := AName;
e.Language := ALanguage;
e.DataSize := ADataSize;
Move(AData^, e.Data^, ADataSize);
end;
procedure TResList.SetIconFromStream(IsIcon: boolean; const AName: string;
ALanguage: word; s: TStream);
var
i, SubIndex: integer;
p: Pointer;
ZeroPoint: Cardinal;
eMain, e: TResEntry;
rih: PRES_ICONHEADER;
id: ICONDIR;
ide: array of ICONDIRENTRY;
begin
ZeroPoint := s.Position;
s.ReadBuffer(id, SizeOf(id));
if id.idCount = 0 then
Exit;
// delete existing icon
ChangeIcon(IsIcon, AName, ALanguage, ciDelete, 0);
SetLength(ide, id.idCount);
s.ReadBuffer(ide[0], id.idCount * SizeOf(ICONDIRENTRY));
// create resource icon header
eMain := NewEntry;
eMain._Type := resIconType[IsIcon, ritGroup];
eMain.Name := AName;
eMain.Language := ALanguage;
eMain.DataSize := SizeOf(ICONDIR) + SizeOf(RES_ICONDIRENTRY) * id.idCount;
FillChar(eMain.Data^, eMain.DataSize, 0);
rih := eMain.Data;
with rih.id do
begin
idReserved := 0; idType := IfThen(IsIcon, ID_ICO, ID_CUR);
idCount := id.idCount;
end;
// allocate space for RT_ICON/RT_CURSOR resources
SubIndex := GetFreeInterval(resIconType[IsIcon, ritPage], ALanguage, id.idCount);
// read subresources
for i := 0 to id.idCount - 1 do
begin
e := NewEntry;
e._Type := resIconType[IsIcon, ritPage];
e.Name := IntToStr(SubIndex + i);
e.Language := ALanguage;
e.DataSize := Byte(IfThen(IsIcon, 0, 4)) + ide[i].dwBytesInRes;
p := e.Data;
if not IsIcon then
// save cursor hot spot to subresource
begin
PWord(p)^ := ide[i].wPlanes_XHotSpot; inc(PWord(p));
PWord(p)^ := ide[i].wBitCount_YHotSpot; inc(PWord(p));
end;
// save image bytes to memory stream
s.Position := Int64(ZeroPoint) + ide[i].dwImageOffset;
s.ReadBuffer(p^, ide[i].dwBytesInRes);
// update RES_ICONDIRENTRY structure in main (header) resource
with rih.ide[i] do
begin
dwBytesInRes := e.DataSize;
nID := SubIndex + i;
if IsIcon then
begin
d.bWidth := ide[i].bWidth;
d.bHeight := ide[i].bHeight;
d.bColorCount := ide[i].bColorCount;
end else
begin
d.wWidth := ide[i].bWidth;
d.wHeight := ide[i].bHeight;
end;
end; // with RES_ICONDIRENTRY
end; // for i := Pages
end;
procedure TResList.SetIconFromFile(IsIcon: boolean; const AName: string;
ALanguage: word; const fn: string);
var
s: TFileStream;
begin
s := TFileStream.Create(fn, fmOpenRead);
try
SetIconFromStream(IsIcon, AName, ALanguage, s);
finally
s.Free;
end;
end;
function TResList.ChangeIcon(IsIcon: boolean; const AName: string;
ALanguage: word; Op: TChangeIconOp; Param: integer): boolean;
var
i: integer;
e: TResEntry;
rih: PRES_ICONHEADER;
procedure OperateOn(Index: integer);
begin
if Index >= 0 then
case Op of
ciDelete: DeleteEntry(Index);
ciNewLanguage: Entries[Index].Language := Param;
end; // case Op
end;
begin
Result := False;
// search for main icon directory resource
i := IndexOf(resIconType[IsIcon, ritGroup], AName, ALanguage, False);
if i < 0 then Exit;
e := Entries[i];
// verify header
rih := e.Data;
if (rih.id.idReserved <> 0) or (rih.id.idCount > 256) then Exit;
// operate on subresources
for i := 0 to rih.id.idCount - 1 do with rih.ide[i] do
OperateOn(IndexOf(resIconType[IsIcon, ritPage], IntToStr(nID), ALanguage, False));
// operate on main resource
OperateOn(IndexOf(resIconType[IsIcon, ritGroup], AName, ALanguage, False));
// success
Result := True;
end;
//****************************************
// Loading/Saving resources from/to RES files
// read a type or name string from a resource file
function resReadString(s: TStream): string;
var
w: word;
begin
s.ReadBuffer(w, 2);
if w = $ffff then
// integral identifier
begin
s.ReadBuffer(w, 2);
Result := IntToStr(w);
end else
// text ID
begin
Result := '';
while w <> 0 do
begin
Result := Result + Char(w);
s.ReadBuffer(w, 2);
end;
// padding
if not Odd(Length(Result)) then s.Seek(2, soFromCurrent);
end;
end;
// write a type or name string to a resource file
procedure resWriteString(st: TStream; const s: string);
var
i: integer;
w: word;
begin
if ResNameIsNumber(s) then
begin
w := $ffff; st.WriteBuffer(w, 2);
w := StrToInt(s); st.WriteBuffer(w, 2);
end else
begin
for i := 1 to Length(s) do
begin
w := Word(s[i]); st.WriteBuffer(w, 2);
end;
// trailing zero and padding
st.Seek(2 + PadTo4((Length(s) + 1) * 2), soFromCurrent);
end;
end;
type
TResInfoTail = packed record
DataVersion: Cardinal;
MemoryFlags, Language: word;
Version, Characteristics: Cardinal;
end;
procedure resReadResource(s: TStream; e: TResEntry);
var
i: integer;
rit: TResInfoTail;
begin
// read header
s.ReadBuffer(i, 4);
e.DataSize := i;
s.Seek(4, soFromCurrent); // HeaderSize is not important
e._Type := resReadString(s);
e.Name := resReadString(s);
s.ReadBuffer(rit, SizeOf(rit)); // additional header fields
e.Language := rit.Language;
// read data
s.ReadBuffer(e.Data^, e.DataSize);
s.Seek(PadTo4(e.DataSize), soFromCurrent);
end;
procedure resWriteResource(s: TStream; const _Type, Name: string;
Language: word; Data: Pointer; Size: integer);
var
pHeader, pData: integer;
dw: Cardinal;
rit: TResInfoTail;
begin
FillChar(rit, SizeOf(rit), 0);
rit.MemoryFlags := $1050; // MOVEABLE, PRELOAD, DISCARDABLE
rit.Language := Language;
// header
pHeader := s.Position;
dw := Size; s.WriteBuffer(dw, 4); // DataSize
s.Seek(4, soFromCurrent); // HeaderSize written later
resWriteString(s, _Type);
resWriteString(s, Name);
s.WriteBuffer(rit, SizeOf(rit)); // additional header fields
pData := s.Position;
s.Position := pHeader + 4;
dw := pData - pHeader; s.WriteBuffer(dw, 4);
s.Position := pData;
// data, write padding as well
s.WriteBuffer(Data^, Size);
s.Seek(PadTo4(Size), soFromCurrent);
end;
function TResList.LoadFromRes(const FileName: string): boolean;
var
b: boolean;
s: TStream;
e: TResEntry;
begin
Result := False;
try
s := TFileStream.Create(FileName, fmOpenRead);
try
e := TResEntry.Create;
try
resReadResource(s, e);
b := (e._Type = '0') and (e.Name = '0');
finally
e.Free;
end;
if not b then Exit;
Clear;
while s.Position < s.Size do
begin
e := TResEntry.Create;
resReadResource(s, e);
if e.DataSize <> 0 then FItems.Add(e) else
begin
// Heuristics: last resource? padding zeroes?
b := (e._Type = '') and (e.Name = '') and (e.Language = 0);
e.Free;
if b then Break;
end;
end; // while not eof
finally
s.Free;
end;
// success
Result := True;
except
end;
end;
function TResList.SaveToRes(const FileName: string): boolean;
var
i: integer;
s: TStream;