LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ResourceVersion.hpp
Go to the documentation of this file.
1/* Copyright 2017 - 2026 R. Thomas
2 * Copyright 2017 - 2026 Quarkslab
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16#ifndef LIEF_PE_RESOURCE_VERSION_H
17#define LIEF_PE_RESOURCE_VERSION_H
18#include <cstdint>
19#include <ostream>
20#include <string>
21
22#include "LIEF/Object.hpp"
23#include "LIEF/errors.hpp"
24#include "LIEF/visibility.h"
25#include <optional>
26
29
30namespace LIEF {
31class BinaryStream;
32
33namespace PE {
34class ResourceData;
35
41 public:
43 static result<ResourceVersion> parse(const uint8_t* p, size_t sz);
45
49 enum class VERSION_OS : uint32_t {
52 UNKNOWN = 0x00000000,
53
55 DOS = 0x00010000,
56
58 OS216 = 0x00020000,
59
61 OS232 = 0x00030000,
62
64 NT = 0x00040000,
65
67 WINCE = 0x00050000,
68
70 WINDOWS16 = 0x00000001,
71
73 PM16 = 0x00000002,
74
76 PM32 = 0x00000003,
77
79 WINDOWS32 = 0x00000004,
80
82 DOS_WINDOWS16 = DOS | WINDOWS16,
83
85 DOS_WINDOWS32 = DOS | WINDOWS32,
86
89 OS216_PM16 = OS216 | PM16,
90
93 OS232_PM32 = OS232 | PM32,
94
96 NT_WINDOWS32 = NT | WINDOWS32,
97 };
98
99 enum class FILE_TYPE : uint32_t {
101 UNKNOWN = 0x00000000,
102
104 APP = 0x00000001,
105
107 DLL = 0x00000002,
108
111 DRV = 0x00000003,
112
115 FONT = 0x00000004,
116
118 VXD = 0x00000005,
119
121 STATIC_LIB = 0x00000007,
122 };
123
124 static constexpr auto DRV_K = uint64_t(1) << 33;
125 static constexpr auto FONT_K = uint64_t(1) << 34;
126
127 enum class FILE_TYPE_DETAILS : uint64_t {
129 UNKNOWN = 0x00000000,
131 DRV_PRINTER = 0x00000001 | DRV_K,
133 DRV_KEYBOARD = 0x00000002 | DRV_K,
135 DRV_LANGUAGE = 0x00000003 | DRV_K,
137 DRV_DISPLAY = 0x00000004 | DRV_K,
139 DRV_MOUSE = 0x00000005 | DRV_K,
141 DRV_NETWORK = 0x00000006 | DRV_K,
143 DRV_SYSTEM = 0x00000007 | DRV_K,
145 DRV_INSTALLABLE = 0x00000008 | DRV_K,
147 DRV_SOUND = 0x00000009 | DRV_K,
149 DRV_COMM = 0x0000000A | DRV_K,
150
151 DRV_INPUTMETHOD = 0x0000000B | DRV_K,
152
154 DRV_VERSIONED_PRINTER = 0x0000000C,
155
157 FONT_RASTER = 0x00000001 | FONT_K,
158
160 FONT_VECTOR = 0x00000002 | FONT_K,
161
163 FONT_TRUETYPE = 0x00000003 | FONT_K,
164 };
165
166 enum class FILE_FLAGS : uint32_t {
169 DEBUG = 0x00000001,
170
174 INFO_INFERRED = 0x00000010,
175
178 PATCHED = 0x00000004,
179
181 PRERELEASE = 0x00000002,
182
185 PRIVATEBUILD = 0x00000008,
186
191 SPECIALBUILD = 0x00000020,
192 };
193
194 static constexpr auto SIGNATURE_VALUE = 0xFEEF04BD;
198 uint32_t signature = 0;
199
203 uint32_t struct_version = 0;
204
208 uint32_t file_version_ms = 0;
209
213 uint32_t file_version_ls = 0;
214
218 uint32_t product_version_ms = 0;
219
223 uint32_t product_version_ls = 0;
224
227 uint32_t file_flags_mask = 0;
228
231 uint32_t file_flags = 0;
232
235 uint32_t file_os = 0;
236
239 uint32_t file_type = 0;
240
243 uint32_t file_subtype = 0;
244
247 uint32_t file_date_ms = 0;
248
251 uint32_t file_date_ls = 0;
252
254 bool has(FILE_FLAGS f) const {
255 return ((file_flags & file_flags_mask) & (uint32_t)f) != 0;
256 }
257
259 std::vector<FILE_FLAGS> flags() const;
260
262 if (file_subtype == 0) {
264 }
265
266 auto ty = FILE_TYPE(file_type);
267 if (ty == FILE_TYPE::DRV) {
269 }
270
271 if (ty == FILE_TYPE::FONT) {
273 }
274
276 }
277
278 std::string to_string() const;
279
280 LIEF_API friend std::ostream& operator<<(std::ostream& os,
281 const fixed_file_info_t& info) {
282 os << info.to_string();
283 return os;
284 }
285 };
286
289
292
293 ~ResourceVersion() override = default;
294
297 return fixed_file_info_;
298 }
299
301 return fixed_file_info_;
302 }
303
306 if (auto& info = string_file_info_) {
307 return &info.value();
308 }
309 return nullptr;
310 }
311
313 return const_cast<ResourceVersion*>(this)->string_file_info();
314 }
315
318 if (auto& info = var_file_info_) {
319 return &info.value();
320 }
321 return nullptr;
322 }
323
327 uint16_t type() const {
328 return type_;
329 }
330
332 const std::u16string& key() const {
333 return key_;
334 }
335
337 std::string key_u8() const;
338
339 ResourceVersion& type(uint16_t value) {
340 type_ = value;
341 return *this;
342 }
343
344 ResourceVersion& key(std::u16string value) {
345 key_ = std::move(value);
346 return *this;
347 }
349 return const_cast<ResourceVersion*>(this)->var_file_info();
350 }
351
353 var_file_info_ = std::move(info);
354 return *this;
355 }
356
358 string_file_info_ = std::move(info);
359 return *this;
360 }
361
362 void accept(Visitor& visitor) const override;
363
364 LIEF_API friend std::ostream& operator<<(std::ostream& os,
365 const ResourceVersion& version);
366
367 private:
368 ResourceVersion() = default;
369
370 static ok_error_t parse_children(ResourceVersion& version, BinaryStream& stream);
371 static ok_error_t parse_child(ResourceVersion& version, BinaryStream& stream);
372 static ok_error_t parse_fixed_file_info(ResourceVersion& version,
373 BinaryStream& stream);
374 static ok_error_t parse_str_file_info(ResourceVersion& version,
375 BinaryStream& stream);
376 static ok_error_t parse_var_file_info(ResourceVersion& version,
377 BinaryStream& stream);
378
379 uint16_t type_ = 0;
380 std::u16string key_;
381 fixed_file_info_t fixed_file_info_;
382
383 std::optional<ResourceStringFileInfo> string_file_info_;
384 std::optional<ResourceVarFileInfo> var_file_info_;
385};
386
390LIEF_API const char*
392
393}
394}
395
396#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:35
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:35
Representation of the StringFileInfo structure.
Definition ResourceStringFileInfo.hpp:40
Representation of the VarFileInfo structure.
Definition ResourceVarFileInfo.hpp:39
const fixed_file_info_t & file_info() const
Return the fixed file info (VS_FIXEDFILEINFO).
Definition ResourceVersion.hpp:296
ResourceVersion(ResourceVersion &&)=default
ResourceVersion & operator=(const ResourceVersion &)=default
const ResourceVarFileInfo * var_file_info() const
Definition ResourceVersion.hpp:348
fixed_file_info_t & file_info()
Definition ResourceVersion.hpp:300
const ResourceStringFileInfo * string_file_info() const
Definition ResourceVersion.hpp:312
~ResourceVersion() override=default
ResourceVersion & string_file_info(ResourceStringFileInfo info)
Definition ResourceVersion.hpp:357
std::string key_u8() const
The key as an utf8 string.
ResourceVersion(const ResourceVersion &)=default
static result< ResourceVersion > parse(const ResourceData &node)
ResourceVersion & key(std::u16string value)
Definition ResourceVersion.hpp:344
ResourceVarFileInfo * var_file_info()
Return the VarFileInfo element.
Definition ResourceVersion.hpp:317
ResourceStringFileInfo * string_file_info()
Return the StringFileInfo element.
Definition ResourceVersion.hpp:305
ResourceVersion & operator=(ResourceVersion &&)=default
ResourceVersion & var_file_info(ResourceVarFileInfo info)
Definition ResourceVersion.hpp:352
friend std::ostream & operator<<(std::ostream &os, const ResourceVersion &version)
uint16_t type() const
The type of data in the version resource.
Definition ResourceVersion.hpp:327
ResourceVersion & type(uint16_t value)
Definition ResourceVersion.hpp:339
static result< ResourceVersion > parse(BinaryStream &stream)
const std::u16string & key() const
The Unicode string L"VS_VERSION_INFO".
Definition ResourceVersion.hpp:332
static result< ResourceVersion > parse(const uint8_t *p, size_t sz)
void accept(Visitor &visitor) const override
Definition Visitor.hpp:212
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:119
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:79
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
LIEF namespace.
Definition Abstract/Binary.hpp:41
lief_version_t version()
Return the current version.
This structure represents the VS_FIXEDFILEINFO structure defined in verrsrc.h.
Definition ResourceVersion.hpp:48
FILE_FLAGS
Definition ResourceVersion.hpp:166
VERSION_OS
Definition ResourceVersion.hpp:49
uint32_t file_date_ms
The most significant 32 bits of the file's 64-bit binary creation date and time stamp.
Definition ResourceVersion.hpp:247
uint32_t file_subtype
The function of the file. The possible values depend on the value of file_type.
Definition ResourceVersion.hpp:243
FILE_TYPE_DETAILS file_type_details() const
Definition ResourceVersion.hpp:261
uint32_t product_version_ms
The most significant 32 bits of the binary version number of the product with which this file was dis...
Definition ResourceVersion.hpp:218
uint32_t file_flags
Contains a bitmask that specifies the Boolean attributes of the file. This member can include one or ...
Definition ResourceVersion.hpp:231
uint32_t signature
Contains the value 0xFEEF04BD. This is used with the szKey member of the VS_VERSIONINFO structure whe...
Definition ResourceVersion.hpp:198
uint32_t file_version_ls
The least significant 32 bits of the file's binary version number. This member is used with file_vers...
Definition ResourceVersion.hpp:213
friend std::ostream & operator<<(std::ostream &os, const fixed_file_info_t &info)
Definition ResourceVersion.hpp:280
uint32_t file_version_ms
The most significant 32 bits of the file's binary version number. This member is used with file_versi...
Definition ResourceVersion.hpp:208
static constexpr auto DRV_K
Definition ResourceVersion.hpp:124
FILE_TYPE
Definition ResourceVersion.hpp:99
@ FONT
The file contains a font. If dwFileType is VFT_FONT, dwFileSubtype contains a more specific descripti...
Definition ResourceVersion.hpp:115
@ DRV
The file contains a device driver. If dwFileType is VFT_DRV, dwFileSubtype contains a more specific d...
Definition ResourceVersion.hpp:111
std::vector< FILE_FLAGS > flags() const
List of FILE_FLAGS.
uint32_t product_version_ls
The least significant 32 bits of the binary version number of the product with which this file was di...
Definition ResourceVersion.hpp:223
uint32_t file_type
The general type of file. This member can be one of the values specified in FILE_TYPE....
Definition ResourceVersion.hpp:239
uint32_t struct_version
The binary version number of this structure. The high-order word of this member contains the major ve...
Definition ResourceVersion.hpp:203
FILE_TYPE_DETAILS
Definition ResourceVersion.hpp:127
@ UNKNOWN
The type is unknown by the system.
Definition ResourceVersion.hpp:129
uint32_t file_os
The operating system for which this file was designed. This member can be one of the values specified...
Definition ResourceVersion.hpp:235
uint32_t file_date_ls
The least significant 32 bits of the file's 64-bit binary creation date and time stamp.
Definition ResourceVersion.hpp:251
static constexpr auto FONT_K
Definition ResourceVersion.hpp:125
bool has(FILE_FLAGS f) const
Check if the given FILE_FLAGS is present.
Definition ResourceVersion.hpp:254
static constexpr auto SIGNATURE_VALUE
Definition ResourceVersion.hpp:194
uint32_t file_flags_mask
Contains a bitmask that specifies the valid bits in file_flags. A bit is valid only if it was defined...
Definition ResourceVersion.hpp:227
#define LIEF_API
Definition visibility.h:45