forked from jrsoftware/issrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompResUpdate.pas
375 lines (336 loc) · 12.1 KB
/
CompResUpdate.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
unit CompResUpdate;
{
Inno Setup
Copyright (C) 1997-2010 Jordan Russell
Portions by Martijn Laan
For conditions of distribution and use, see LICENSE.TXT.
Resource update functions used by the compiler only
$jrsoftware: issrc/Projects/CompResUpdate.pas,v 1.25 2010/04/05 20:53:41 jr Exp $
}
interface
uses
Windows, SysUtils, FileClass, VerInfo;
{$I VERSION.INC}
procedure UpdateIcons(const FileName, IcoFileName: String);
procedure UpdateVersionInfo(const F: TFile;
const NewBinaryFileVersion, NewBinaryProductVersion: TFileVersionNumbers;
const NewCompanyName, NewFileDescription, NewTextFileVersion, NewLegalCopyright,
NewProductName, NewTextProductVersion: String);
implementation
uses
ResUpdate{$IFDEF UNICODE}, Math{$ENDIF};
procedure Error(const Msg: String);
begin
raise Exception.Create('Resource update error: ' + Msg);
end;
procedure ErrorWithLastError(const Msg: String);
begin
Error(Msg + ' (' + IntToStr(GetLastError) + ')');
end;
procedure UpdateVersionInfo(const F: TFile;
const NewBinaryFileVersion, NewBinaryProductVersion: TFileVersionNumbers;
const NewCompanyName, NewFileDescription, NewTextFileVersion, NewLegalCopyright,
NewProductName, NewTextProductVersion: String);
function WideStrsEqual(P1, P2: PWideChar): Boolean;
function WideUpCase(C: WideChar): WideChar;
begin
Result := C;
if (Result >= 'a') and (Result <= 'z') then
Dec(Result, Ord('a') - Ord('A'));
end;
begin
while True do begin
if WideUpCase(P1^) <> WideUpCase(P2^) then begin
Result := False;
Exit;
end;
if P1^ = #0 then
Break;
Inc(P1);
Inc(P2);
end;
Result := True;
end;
procedure BumpToDWordBoundary(var P: Pointer);
begin
if Cardinal(P) and 3 <> 0 then
Cardinal(P) := (Cardinal(P) or 3) + 1;
end;
function QueryValue(P: Pointer; Path: PWideChar; var Buf: Pointer;
var BufLen: Cardinal): Boolean;
var
EndP: Pointer;
ValueLength: Cardinal;
begin
Result := False;
Cardinal(EndP) := Cardinal(P) + PWord(P)^;
Inc(PWord(P));
ValueLength := PWord(P)^;
Inc(PWord(P));
Inc(PWord(P));
if WideStrsEqual(PWideChar(P), Path) then begin
Inc(PWideChar(P), lstrlenW(P) + 1);
BumpToDWordBoundary(P);
Inc(Path, lstrlenW(Path) + 1);
if Path^ = #0 then begin
{ Found the requested value }
Buf := P;
BufLen := ValueLength;
Result := True;
end
else begin
{ Handle children.
Note: Like VerQueryValue, we always treat ValueLength as a byte count
when looking for child nodes. Many resource compilers, including
Borland's, wrongly set ValueLength to a *character* count on string
nodes. But since we never try to query for a child of a string node,
that doesn't matter here. }
Inc(Cardinal(P), ValueLength);
BumpToDWordBoundary(P);
while Cardinal(P) < Cardinal(EndP) do begin
Result := QueryValue(P, Path, Buf, BufLen);
if Result then
Exit;
Inc(Cardinal(P), PWord(P)^);
BumpToDWordBoundary(P);
end;
end;
end;
end;
procedure ReplaceWithRealCopyrightSymbols(const Value: PWideChar);
var
Len, I, J: Integer;
begin
Len := lstrlenW(Value);
for I := 0 to Len-3 do begin
if (Value[I] = '(') and (Value[I+1] = 'C') and (Value[I+2] = ')') then begin
Value[I] := WideChar($00A9);
{ Shift back two characters }
for J := I+1 to Len-3 do
Value[J] := Value[J+2];
Value[Len-2] := ' ';
Value[Len-1] := ' ';
end;
end;
end;
procedure UpdateStringValue(P: Pointer; const Path: PWideChar; NewValue: String);
var
Value: PWideChar;
ValueLen: Cardinal;
begin
if not QueryValue(P, Path, Pointer(Value), ValueLen) then
Error('Unexpected version resource format (1)');
{$IFDEF UNICODE}
Move(Pointer(NewValue)^, Value^, (Min(Length(NewValue), lstrlenW(Value)))*SizeOf(Char));
{$ELSE}
MultiByteToWideChar(CP_ACP, 0, PChar(NewValue), Length(NewValue), Value, lstrlenW(Value));
{$ENDIF}
ReplaceWithRealCopyrightSymbols(Value);
end;
procedure UpdateFixedFileInfo(P: Pointer; const Path: PWideChar; NewFileVersion, NewProductVersion: TFileVersionNumbers);
var
FixedFileInfo: PVSFixedFileInfo;
ValueLen: Cardinal;
begin
if not QueryValue(P, Path, Pointer(FixedFileInfo), ValueLen) then
Error('Unexpected version resource format (2)');
if FixedFileInfo.dwSignature <> $FEEF04BD then
Error('Unexpected version resource format (3)');
FixedFileInfo.dwFileVersionLS := NewFileVersion.LS;
FixedFileInfo.dwFileVersionMS := NewFileVersion.MS;
FixedFileInfo.dwProductVersionLS := NewProductVersion.LS;
FixedFileInfo.dwProductVersionMS := NewProductVersion.MS;
end;
var
ResOffset, ResSize: Cardinal;
VersRes: Pointer;
begin
{ Locate the resource }
ResSize := SeekToResourceData(F, Cardinal(RT_VERSION), 1);
ResOffset := F.Position.Lo;
GetMem(VersRes, ResSize);
try
{ Read the resource }
F.ReadBuffer(VersRes^, ResSize);
{ Update the resource }
UpdateStringValue(VersRes, 'VS_VERSION_INFO'#0'StringFileInfo'#0'000004b0'#0'CompanyName'#0, NewCompanyName);
UpdateStringValue(VersRes, 'VS_VERSION_INFO'#0'StringFileInfo'#0'000004b0'#0'FileDescription'#0, NewFileDescription);
UpdateStringValue(VersRes, 'VS_VERSION_INFO'#0'StringFileInfo'#0'000004b0'#0'FileVersion'#0, NewTextFileVersion);
UpdateStringValue(VersRes, 'VS_VERSION_INFO'#0'StringFileInfo'#0'000004b0'#0'LegalCopyright'#0, NewLegalCopyright);
UpdateStringValue(VersRes, 'VS_VERSION_INFO'#0'StringFileInfo'#0'000004b0'#0'ProductName'#0, NewProductName);
UpdateStringValue(VersRes, 'VS_VERSION_INFO'#0'StringFileInfo'#0'000004b0'#0'ProductVersion'#0, NewTextProductVersion);
UpdateFixedFileInfo(VersRes, 'VS_VERSION_INFO'#0, NewBinaryFileVersion, NewBinaryProductVersion);
{ Write the updated resource }
F.Seek(ResOffset);
F.WriteBuffer(VersRes^, ResSize);
finally
FreeMem(VersRes);
end;
end;
function EnumLangsFunc(hModule: Cardinal; lpType, lpName: PAnsiChar; wLanguage: Word; lParam: Integer): BOOL; stdcall;
begin
PWord(lParam)^ := wLanguage;
Result := False;
end;
function GetResourceLanguage(hModule: Cardinal; lpType, lpName: PChar; var wLanguage: Word): Boolean;
begin
wLanguage := 0;
EnumResourceLanguages(hModule, lpType, lpName, @EnumLangsFunc, Integer(@wLanguage));
Result := True;
end;
procedure UpdateIcons(const FileName, IcoFileName: String);
type
PIcoItemHeader = ^TIcoItemHeader;
TIcoItemHeader = packed record
Width: Byte;
Height: Byte;
Colors: Byte;
Reserved: Byte;
Planes: Word;
BitCount: Word;
ImageSize: DWORD;
end;
PIcoItem = ^TIcoItem;
TIcoItem = packed record
Header: TIcoItemHeader;
Offset: DWORD;
end;
PIcoHeader = ^TIcoHeader;
TIcoHeader = packed record
Reserved: Word;
Typ: Word;
ItemCount: Word;
Items: array [0..MaxInt shr 4 - 1] of TIcoItem;
end;
PGroupIconDirItem = ^TGroupIconDirItem;
TGroupIconDirItem = packed record
Header: TIcoItemHeader;
Id: Word;
end;
PGroupIconDir = ^TGroupIconDir;
TGroupIconDir = packed record
Reserved: Word;
Typ: Word;
ItemCount: Word;
Items: array [0..MaxInt shr 4 - 1] of TGroupIconDirItem;
end;
function IsValidIcon(P: Pointer; Size: Cardinal): Boolean;
var
ItemCount: Cardinal;
begin
Result := False;
if Size < Cardinal(SizeOf(Word) * 3) then
Exit;
if (PChar(P)[0] = 'M') and (PChar(P)[1] = 'Z') then
Exit;
ItemCount := PIcoHeader(P).ItemCount;
if Size < Cardinal((SizeOf(Word) * 3) + (ItemCount * SizeOf(TIcoItem))) then
Exit;
P := @PIcoHeader(P).Items;
while ItemCount > Cardinal(0) do begin
if (Cardinal(PIcoItem(P).Offset + PIcoItem(P).Header.ImageSize) < Cardinal(PIcoItem(P).Offset)) or
(Cardinal(PIcoItem(P).Offset + PIcoItem(P).Header.ImageSize) > Cardinal(Size)) then
Exit;
Inc(PIcoItem(P));
Dec(ItemCount);
end;
Result := True;
end;
var
H: THandle;
M: HMODULE;
R: HRSRC;
Res: HGLOBAL;
GroupIconDir, NewGroupIconDir: PGroupIconDir;
I: Integer;
wLanguage: Word;
F: TFile;
Ico: PIcoHeader;
N: Cardinal;
NewGroupIconDirSize: LongInt;
begin
if Win32Platform <> VER_PLATFORM_WIN32_NT then
Error('Only supported on Windows NT and above');
Ico := nil;
try
{ Load the icons }
F := TFile.Create(IcoFileName, fdOpenExisting, faRead, fsRead);
try
N := F.CappedSize;
if Cardinal(N) > Cardinal($100000) then { sanity check }
Error('Icon file is too large');
GetMem(Ico, N);
F.ReadBuffer(Ico^, N);
finally
F.Free;
end;
{ Ensure the icon is valid }
if not IsValidIcon(Ico, N) then
Error('Icon file is invalid');
{ Update the resources }
H := BeginUpdateResource(PChar(FileName), False);
if H = 0 then
ErrorWithLastError('BeginUpdateResource failed (1)');
try
M := LoadLibraryEx(PChar(FileName), 0, LOAD_LIBRARY_AS_DATAFILE);
if M = 0 then
ErrorWithLastError('LoadLibraryEx failed (1)');
try
{ Load the 'MAINICON' group icon resource }
R := FindResource(M, 'MAINICON', RT_GROUP_ICON);
if R = 0 then
ErrorWithLastError('FindResource failed (1)');
Res := LoadResource(M, R);
if Res = 0 then
ErrorWithLastError('LoadResource failed (1)');
GroupIconDir := LockResource(Res);
if GroupIconDir = nil then
ErrorWithLastError('LockResource failed (1)');
{ Delete 'MAINICON' }
if not GetResourceLanguage(M, RT_GROUP_ICON, 'MAINICON', wLanguage) then
Error('GetResourceLanguage failed (1)');
if not UpdateResource(H, RT_GROUP_ICON, 'MAINICON', wLanguage, nil, 0) then
ErrorWithLastError('UpdateResource failed (1)');
{ Delete the RT_ICON icon resources that belonged to 'MAINICON' }
for I := 0 to GroupIconDir.ItemCount-1 do begin
if not GetResourceLanguage(M, RT_ICON, MakeIntResource(GroupIconDir.Items[I].Id), wLanguage) then
Error('GetResourceLanguage failed (2)');
if not UpdateResource(H, RT_ICON, MakeIntResource(GroupIconDir.Items[I].Id), wLanguage, nil, 0) then
ErrorWithLastError('UpdateResource failed (2)');
end;
{ Build the new group icon resource }
NewGroupIconDirSize := 3*SizeOf(Word)+Ico.ItemCount*SizeOf(TGroupIconDirItem);
GetMem(NewGroupIconDir, NewGroupIconDirSize);
try
{ Build the new group icon resource }
NewGroupIconDir.Reserved := GroupIconDir.Reserved;
NewGroupIconDir.Typ := GroupIconDir.Typ;
NewGroupIconDir.ItemCount := Ico.ItemCount;
for I := 0 to NewGroupIconDir.ItemCount-1 do begin
NewGroupIconDir.Items[I].Header := Ico.Items[I].Header;
NewGroupIconDir.Items[I].Id := I+1; //assumes that there aren't any icons left
end;
{ Update 'MAINICON' }
for I := 0 to NewGroupIconDir.ItemCount-1 do
if not UpdateResource(H, RT_ICON, MakeIntResource(NewGroupIconDir.Items[I].Id), 1033, Pointer(DWORD(Ico) + Ico.Items[I].Offset), Ico.Items[I].Header.ImageSize) then
ErrorWithLastError('UpdateResource failed (3)');
{ Update the icons }
if not UpdateResource(H, RT_GROUP_ICON, 'MAINICON', 1033, NewGroupIconDir, NewGroupIconDirSize) then
ErrorWithLastError('UpdateResource failed (4)');
finally
FreeMem(NewGroupIconDir);
end;
finally
FreeLibrary(M);
end;
except
EndUpdateResource(H, True); { discard changes }
raise;
end;
if not EndUpdateResource(H, False) then
ErrorWithLastError('EndUpdateResource failed');
finally
FreeMem(Ico);
end;
end;
end.