-
Notifications
You must be signed in to change notification settings - Fork 0
/
Res.pas
210 lines (188 loc) · 6.14 KB
/
Res.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
unit Res;
interface
uses Classes,FileIO,PE;
const
RT_VERSION = 16;
{
Resource Types:
1: Cursor
2: Bitmap
3: Icon
4: Menu
5: Dialog
6: String
7: Fontdir
8: Font
9: Accelerator
10: RCData
11: MessageTable
16: Version
17: dlginclude
19: plugplay
20: vxd
21: anicursor
22: aniicon
23: html
}
Type
TResourceDir = packed record
characteristics : DWORD; // Resource flags, reserved for future use; currently set to zero.
timeDateStamp : DWORD; // Time the resource data was created by the resource compiler.
versionMajor : WORD; // Major version number, set by the user.
versionMinor : WORD; // Minor version number.
cNameEntries : WORD; // Number of directory entries, immediately following the table, that use strings to identify Type, Name, or Language (depending on the level of the table).
cIDEntries : WORD; // Number of directory entries, immediately following the Name entries, that use numeric identifiers for Type, Name, or Language.
end;
PResourceDir = ^TResourceDir;
TResourceDirEntry = packed record
name : DWORD; // RVA Address of integer or string that gives the Type, Name, or Language identifier, depending on level of table.
RVA : DWORD; // RVA High bit 0. Address of a Resource Data Entry (a leaf).
// RVA High bit 1. Lower 31 bits are the address of another Resource Directory Table (the next level down).
end;
PResourceDirEntry = ^TResourceDirEntry;
TResourceEntry = packed record
OffsetToData : DWORD;
Size : DWORD;
CodePage : DWORD;
Reserved : DWORD
end;
PResourceEntry = ^TResourceEntry;
procedure parse_resource_dir(F:TFileStream;var Header:TImageSectionHeader; Ofs:Cardinal; Level,ResType:Integer);
Implementation
uses SysUtils;
procedure parse_version_info(F:TFileStream;Ofs,Size:Cardinal);
var
marker:Int64;
len:Integer;
v1,v2,v3,v4:Word;
// VS_VERSION_INFO
wLength:Word;
wValueLength:Word;
wType:Word; // 1 = Text data, 0 = binary data (USUALLY 0)
szKey:Array[1..16] of WideChar; // UNICODE "VS_VERSION_INFO" with trailing 0
fix_ver:TVSFixedFileInfo;
begin
// StringFileInfo
// unsigned short int wLength - with Children
// unsigned short int wValueLength = 0
// unsigned short int wType (1 = Text data, 0 = binary data)
// WCHAR szKey[] = "StringFileInfo" in UNICODE
// padding to 8 byte boundary
// Children - 0 or 1 struct StringTable, followed by 0 or 1 struct VarFileInfo
// StringTable
// wLength - with Children
// wValueLength = 0
// wType (1 = Text data, 0 = binary data)
// WCHAR szKey[] - 8 digit hexadecimal as UNICODE string (MSB - Lang, LBS - Codepage)
// padding to 8 byte boundary
// struct String Children[]
// String
// wLength
// wValueLength - length of Value
// wType
// WHCAR szKey[] - UNICODE name (Comments, CompanyName, FileDescription, FileVersion, InternalName, LegalCopyright, LegalTrademarks, OriginalFilename, PrivateBuild - only if VS_FF_PRIVATEBUILD, ProductName, ProductVersion, SpecialBuild - if VS_FF_SPECIALBUILD)
// padding to 8 byte boundary
// WCHAR Value[]
// VarFileInfo
// wLength - with Children
// wValueLength = 0
// wType
// WCHAR szKey[] = "VarFileInfo" in UNICODE
// padding to 8 byte boundary
// struct Var Children[]
// Var
// wLength
// wValueLength - length of Value
// wType
// WCHAR szKey[] = "Translation" in UNICODE
// padding to 8 byte boundary
// DWORD Value[] - 1 or more (Lang,CodePage) pairs
marker:=F.Position;
F.Seek(Ofs,soFromBeginning);
wLength:=ReadWord(F);
wValueLength:=ReadWord(F);
wType:=ReadWord(F);
ReadBin(F,@szKey,Sizeof(szKey));
// Padding1 - several words to 4 byte boundary
while F.Position Mod 4 <>0 do
len:=ReadWord(F);
if wValueLength<>0 then
begin
// should read wValueLength bytes in theory
read_fixed_ver(F,fix_ver);
if fix_ver.dwSignature = $FEEF04BD then
begin
v1 := fix_ver.dwFileVersionMS shr 16;
v2 := fix_ver.dwFileVersionMS and $ffff;
v3 := fix_ver.dwFileVersionLS shr 16;
v4 := fix_ver.dwFileVersionLS and $ffff;
WriteLn(Format('%u.%u.%u.%u',[v1,v2,v3,v4]));
end
else WriteLn(Format('FileVer signature: %x',[fix_ver.dwSignature]));
end;
F.Seek(marker,soFromBeginning);
end;
procedure read_resource_dir(F:TFileStream; Adres:Cardinal;var ResDir:TResourceDir);
var
marker:Int64;
begin
marker:=F.Position;
F.Seek(Adres,soFromBeginning);
F.ReadBuffer(ResDir,SizeOf(ResDir));
F.Seek(marker,soFromBeginning);
end;
procedure read_resource_dir_entry(F:TFileStream; Adres:Cardinal;var DirEntry:TResourceDirEntry; Ofs:Cardinal);
var
marker:Int64;
begin
marker:=F.Position;
f.Seek(Adres+Ofs,soFromBeginning);
DirEntry.Name:=ReadInt(F);
DirEntry.RVA:=ReadInt(F);
f.Seek(marker,soFromBeginning);
end;
procedure read_resource_data(F:TFileStream; Adres:Cardinal;var ResData:TResourceEntry; Ofs:Cardinal);
var
marker:Int64;
begin
marker:=F.Position;
F.Seek(Adres+Ofs,soFromBeginning);
F.ReadBuffer(ResData,SizeOf(ResData));
F.Seek(marker,soFromBeginning);
end;
procedure parse_resource_dir(F:TFileStream;var Header:TImageSectionHeader; Ofs:Cardinal; Level,ResType:Integer);
var
ResDir:TResourceDir;
DirEntry:TResourceDirEntry;
ResData:TResourceEntry;
count,t:Integer;
begin
read_resource_dir(F,Header.PointerToRawData+Ofs,ResDir);
Inc(Ofs,SizeOf(Resdir));
count:=ResDir.cNameEntries+ResDir.cIDEntries;
if count=0 then
begin
Writeln(Format('Zero entries in resource section %s',[PAnsiChar(Header.Name[0])]));
Exit;
End;
for t:=0 To count-1 do
begin
read_resource_dir_entry(F,Header.PointerToRawData,DirEntry,Ofs);
if ((Level=0) and (DirEntry.name = RT_VERSION)) or (Level>0) then
begin
if (DirEntry.RVA and $80000000)=0 then
begin
read_resource_data(F,Header.PointerToRawData,ResData,DirEntry.RVA);
if ResType=RT_VERSION then
parse_version_info(F,Header.PointerToRawData+(ResData.OffsetToData-Header.VirtualAddress),ResData.Size);
end
else
begin
if level=0 then parse_resource_dir(F,Header,DirEntry.RVA and $7fffffff,level+1,DirEntry.Name)
else parse_resource_dir(F,Header,DirEntry.RVA and $7fffffff,level+1,ResType);
end;
End;
Inc(Ofs,SizeOf(DirEntry));
end;
end;
end.