LIEF: Library to Instrument Executable Formats Version 2.0.0
Loading...
Searching...
No Matches
ResourceDialog.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_DIALOG_H
17#define LIEF_PE_RESOURCE_DIALOG_H
18
19#include "LIEF/Object.hpp"
21#include "LIEF/enums.hpp"
22#include "LIEF/errors.hpp"
23#include "LIEF/span.hpp"
24#include "LIEF/visibility.h"
25#include <string_view>
26
27#include <memory>
28#include <ostream>
29#include <vector>
30
31namespace LIEF {
32class BinaryStream;
33namespace PE {
34
35class ResourceData;
36
45 public:
46 using dialogs_t = std::vector<std::unique_ptr<ResourceDialog>>;
47
53 std::u16string string;
54
55 bool is_defined() const {
56 return ordinal || !string.empty();
57 }
58
59 operator bool() const {
60 return is_defined();
61 }
62
63 std::string to_string() const;
64 };
65
67 enum class TYPE {
68 UNKNOWN = 0,
69 REGULAR,
70 EXTENDED,
71 };
72
75 enum class DIALOG_STYLES : uint32_t {
76 // clang-format off
77 ABSALIGN = 0x0001,
78 SYSMODAL = 0x0002,
79 LOCALEDIT = 0x0020,
80 SETFONT = 0x0040,
81 MODALFRAME = 0x0080,
82 NOIDLEMSG = 0x0100,
83 SETFOREGROUND = 0x0200,
84 S3DLOOK = 0x0004,
85 FIXEDSYS = 0x0008,
86 NOFAILCREATE = 0x0010,
87 CONTROL = 0x0400,
88 CENTER = 0x0800,
89 CENTERMOUSE = 0x1000,
90 CONTEXTHELP = 0x2000,
91 SHELLFONT = SETFONT | FIXEDSYS,
92 // clang-format on
93 };
94
96 enum class WINDOW_STYLES : uint32_t {
97 // clang-format off
98 OVERLAPPED = 0x00000000,
99 POPUP = 0x80000000,
100 CHILD = 0x40000000,
101 MINIMIZE = 0x20000000,
102 VISIBLE = 0x10000000,
103 DISABLED = 0x08000000,
104 CLIPSIBLINGS = 0x04000000,
105 CLIPCHILDREN = 0x02000000,
106 MAXIMIZE = 0x01000000,
107 CAPTION = 0x00C00000,
108 BORDER = 0x00800000,
109 DLGFRAME = 0x00400000,
110 VSCROLL = 0x00200000,
111 HSCROLL = 0x00100000,
112 SYSMENU = 0x00080000,
113 THICKFRAME = 0x00040000,
114 GROUP = 0x00020000,
115 TABSTOP = 0x00010000,
116 // clang-format on
117 };
118
121 enum class WINDOW_EXTENDED_STYLES : uint32_t {
122 // clang-format off
123 DLGMODALFRAME = 0x00000001,
124 NOPARENTNOTIFY = 0x00000004,
125 TOPMOST = 0x00000008,
126 ACCEPTFILES = 0x00000010,
127 TRANSPARENT_STY = 0x00000020,
128 MDICHILD = 0x00000040,
129 TOOLWINDOW = 0x00000080,
130 WINDOWEDGE = 0x00000100,
131 CLIENTEDGE = 0x00000200,
132 CONTEXTHELP = 0x00000400,
133
134 RIGHT = 0x00001000,
135 LEFT = 0x00000000,
136 RTLREADING = 0x00002000,
137 LEFTSCROLLBAR = 0x00004000,
138
139 CONTROLPARENT = 0x00010000,
140 STATICEDGE = 0x00020000,
141 APPWINDOW = 0x00040000,
142 // clang-format on
143 };
144
147 enum class CONTROL_STYLES : uint32_t {
148 // clang-format off
149 TOP = 0x00000001,
150 NOMOVEY = 0x00000002,
151 BOTTOM = 0x00000003,
152 NORESIZE = 0x00000004,
153 NOPARENTALIGN = 0x00000008,
154 ADJUSTABLE = 0x00000020,
155 NODIVIDER = 0x00000040,
156 VERT = 0x00000080,
157 LEFT = VERT | TOP,
158 RIGHT = VERT | BOTTOM,
159 NOMOVEX = VERT | NOMOVEY,
160 // clang-format on
161 };
162
170 public:
171 Item() = default;
172 Item(const Item&) = default;
173 Item& operator=(const Item&) = default;
174
175 Item(Item&&) = default;
176 Item& operator=(Item&&) = default;
177
178 enum class WINDOW_CLASS : uint32_t {
179 BUTTON = 0x0080,
180 EDIT = 0x0081,
181 STATIC = 0x0082,
182 LIST_BOX = 0x0083,
183 SCROLL_BAR = 0x0084,
184 COMBO_BOX = 0x0085,
185 };
186
189 uint32_t style() const {
190 return style_;
191 }
192
198 uint32_t extended_style() const {
199 return extended_style_;
200 }
201
203 int32_t id() const {
204 return id_;
205 }
206
208 bool has(WINDOW_STYLES style) const {
209 return (style_ & (uint32_t)style) != 0;
210 }
211
214 return (style_ & (uint32_t)style) != 0;
215 }
216
218 std::vector<WINDOW_STYLES> window_styles() const;
219
221 std::vector<CONTROL_STYLES> control_styles() const;
222
226 int16_t x() const {
227 return x_;
228 }
229
233 int16_t y() const {
234 return y_;
235 }
236
238 int16_t cx() const {
239 return cx_;
240 }
241
243 int16_t cy() const {
244 return cy_;
245 }
246
247 Item& style(uint32_t value) {
248 style_ = value;
249 return *this;
250 }
251
252 Item& extended_style(uint32_t value) {
253 extended_style_ = value;
254 return *this;
255 }
256
257 Item& x(int16_t value) {
258 x_ = value;
259 return *this;
260 }
261 Item& y(int16_t value) {
262 y_ = value;
263 return *this;
264 }
265 Item& cx(int16_t value) {
266 cx_ = value;
267 return *this;
268 }
269 Item& cy(int16_t value) {
270 cy_ = value;
271 return *this;
272 }
273
274 Item& id(int32_t value) {
275 id_ = value;
276 return *this;
277 }
278
279 Item& data(std::vector<uint8_t> creation_data) {
280 creation_data_ = std::move(creation_data);
281 return *this;
282 }
283
284 Item& clazz(std::u16string title) {
285 class_.string = std::move(title);
286 class_.ordinal = make_error_code(lief_errors::not_found);
287 return *this;
288 }
289
290 Item& clazz(uint16_t ord) {
291 class_.ordinal = ord;
292 return *this;
293 }
294
295 Item& title(std::u16string value) {
296 title_.string = std::move(value);
297 title_.ordinal = make_error_code(lief_errors::not_found);
298 return *this;
299 }
300
301 Item& title(uint16_t ord) {
302 title_.ordinal = ord;
303 return *this;
304 }
305
309 const ordinal_or_str_t& clazz() const {
310 return class_;
311 }
312
316 const ordinal_or_str_t& title() const {
317 return title_;
318 }
319
322 return creation_data_;
323 }
324
326 return creation_data_;
327 }
328
329 virtual ~Item() = default;
330
331 virtual std::string to_string() const = 0;
332
333 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Item& item) {
334 os << item.to_string();
335 return os;
336 }
337
338 protected:
339 uint32_t style_ = 0;
340 uint32_t extended_style_ = 0;
341
342 int16_t x_ = 0;
343 int16_t y_ = 0;
344 int16_t cx_ = 0;
345 int16_t cy_ = 0;
346
347 int32_t id_ = 0;
348
349 ordinal_or_str_t class_;
350 ordinal_or_str_t title_;
351 std::vector<uint8_t> creation_data_;
352 };
353
355 static dialogs_t parse(const ResourceData& node);
356 static dialogs_t parse(const uint8_t* buffer, size_t size);
357
358 ResourceDialog() = default;
361
364
366 type_(ty) {}
367
368 virtual std::unique_ptr<ResourceDialog> clone() const = 0;
369
370 TYPE type() const {
371 return type_;
372 }
373
377 uint32_t style() const {
378 return style_;
379 }
380
384 uint32_t extended_style() const {
385 return extended_style_;
386 }
387
390 int16_t x() const {
391 return x_;
392 }
393
396 int16_t y() const {
397 return y_;
398 }
399
401 int16_t cx() const {
402 return cx_;
403 }
404
406 int16_t cy() const {
407 return cy_;
408 }
409
411 style_ = value;
412 return *this;
413 }
414
416 extended_style_ = value;
417 return *this;
418 }
419
421 x_ = value;
422 return *this;
423 }
425 y_ = value;
426 return *this;
427 }
429 cx_ = value;
430 return *this;
431 }
433 cy_ = value;
434 return *this;
435 }
436
437 ResourceDialog& menu(std::u16string title) {
438 menu_.string = std::move(title);
440 return *this;
441 }
442
443 ResourceDialog& menu(uint16_t ord) {
444 menu_.ordinal = ord;
445 return *this;
446 }
447
449 window_class_.string = std::move(title);
450 window_class_.ordinal = make_error_code(lief_errors::not_found);
451 return *this;
452 }
453
455 window_class_.ordinal = ord;
456 return *this;
457 }
458
459 ResourceDialog& title(std::u16string value) LIEF_LIFETIMEBOUND {
460 title_ = std::move(value);
461 return *this;
462 }
463
465
467 bool has(DIALOG_STYLES style) const {
468 return (style_ & (uint32_t)style) != 0;
469 }
470
472 bool has(WINDOW_STYLES style) const {
473 return (style_ & (uint32_t)style) != 0;
474 }
475
478 return (extended_style_ & (uint32_t)style) != 0;
479 }
480
482 std::vector<DIALOG_STYLES> styles_list() const;
483
485 std::vector<WINDOW_STYLES> windows_styles_list() const;
486
488 std::vector<WINDOW_EXTENDED_STYLES> windows_ext_styles_list() const;
489
491 const std::u16string& title() const LIEF_LIFETIMEBOUND {
492 return title_;
493 }
494
496 std::string title_utf8() const;
497
499 const ordinal_or_str_t& menu() const {
500 return menu_;
501 }
502
506 return window_class_;
507 }
508
509 virtual std::string to_string() const = 0;
510
511 ~ResourceDialog() override = default;
512
513
516 template<class T>
517 const T* as() const {
518 static_assert(std::is_base_of_v<ResourceDialog, T>,
519 "Require ResourceDialog inheritance");
520 if (T::classof(this)) {
521 return static_cast<const T*>(this);
522 }
523 return nullptr;
524 }
525
526 friend LIEF_API std::ostream& operator<<(std::ostream& os,
527 const ResourceDialog& dialog) {
528 os << dialog.to_string();
529 return os;
530 }
531
532 protected:
533 static ok_error_t parse_menu(ResourceDialog& dialog, BinaryStream& stream);
534 static ok_error_t parse_class(ResourceDialog& dialog, BinaryStream& stream);
535 static ok_error_t parse_class(ResourceDialog::Item& dialog,
536 BinaryStream& stream);
537 static ok_error_t parse_title(ResourceDialog& dialog, BinaryStream& stream);
538 static ok_error_t parse_title(ResourceDialog::Item& dialog,
539 BinaryStream& stream);
540 static ok_error_t parse_creation_data(ResourceDialog::Item& item,
541 BinaryStream& stream);
542 TYPE type_ = TYPE::UNKNOWN;
543
544 uint32_t style_ = 0;
545 uint32_t extended_style_ = 0;
546
547 int16_t x_ = 0;
548 int16_t y_ = 0;
549 int16_t cx_ = 0;
550 int16_t cy_ = 0;
551
552 ordinal_or_str_t menu_;
553 ordinal_or_str_t window_class_;
554
555 std::u16string title_;
556};
557
562
563}
564}
565
570
571
572#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:36
This class represents an element of the dialog. It can be for instance, a button, or a caption.
Definition ResourceDialog.hpp:169
const ordinal_or_str_t & clazz() const
Window class of the control. This can be either: a string that specifies the name of a registered win...
Definition ResourceDialog.hpp:309
Item & cy(int16_t value)
Definition ResourceDialog.hpp:269
Item & y(int16_t value)
Definition ResourceDialog.hpp:261
friend std::ostream & operator<<(std::ostream &os, const Item &item)
Definition ResourceDialog.hpp:333
Item & cx(int16_t value)
Definition ResourceDialog.hpp:265
bool has(WINDOW_STYLES style) const
Check if this item has the given WINDOW_STYLES.
Definition ResourceDialog.hpp:208
WINDOW_CLASS
Definition ResourceDialog.hpp:178
const ordinal_or_str_t & title() const
Title of the item which can be either: a string that specifies the initial text or an ordinal value o...
Definition ResourceDialog.hpp:316
Item & extended_style(uint32_t value)
Definition ResourceDialog.hpp:252
span< uint8_t > creation_data()
Definition ResourceDialog.hpp:325
virtual std::string to_string() const =0
Item & title(uint16_t ord)
Definition ResourceDialog.hpp:301
Item & operator=(Item &&)=default
int32_t id() const
The control identifier.
Definition ResourceDialog.hpp:203
Item(const Item &)=default
span< const uint8_t > creation_data() const
Creation data that is passed to the control's window procedure.
Definition ResourceDialog.hpp:321
uint32_t style() const
The style of the control. This can be a combination of WINDOW_STYLES or CONTROL_STYLES.
Definition ResourceDialog.hpp:189
std::vector< WINDOW_STYLES > window_styles() const
List of WINDOW_STYLES used by this item.
Item & clazz(uint16_t ord)
Definition ResourceDialog.hpp:290
int16_t cx() const
The width, in dialog box units, of the control.
Definition ResourceDialog.hpp:238
bool has(CONTROL_STYLES style) const
Check if this item has the given CONTROL_STYLES.
Definition ResourceDialog.hpp:213
uint32_t extended_style() const
The extended styles for a window. This member is not used to create controls in dialog boxes,...
Definition ResourceDialog.hpp:198
Item & style(uint32_t value)
Definition ResourceDialog.hpp:247
int16_t cy() const
The height, in dialog box units, of the control.
Definition ResourceDialog.hpp:243
Item & operator=(const Item &)=default
Item & x(int16_t value)
Definition ResourceDialog.hpp:257
Item & data(std::vector< uint8_t > creation_data)
Definition ResourceDialog.hpp:279
std::vector< CONTROL_STYLES > control_styles() const
List of CONTROL_STYLES used by this item.
Item & id(int32_t value)
Definition ResourceDialog.hpp:274
int16_t y() const
The y-coordinate, in dialog box units, of the upper-left corner of the control. This coordinate is al...
Definition ResourceDialog.hpp:233
Item & title(std::u16string value)
Definition ResourceDialog.hpp:295
int16_t x() const
The x-coordinate, in dialog box units, of the upper-left corner of the control. This coordinate is al...
Definition ResourceDialog.hpp:226
Item & clazz(std::u16string title)
Definition ResourceDialog.hpp:284
This class is the base class for either a regular (legacy) Dialog or an extended Dialog....
Definition ResourceDialog.hpp:44
ResourceDialog & window_class(std::u16string title)
Definition ResourceDialog.hpp:448
virtual std::string to_string() const =0
std::vector< WINDOW_STYLES > windows_styles_list() const
List of WINDOW_STYLES used by this dialog.
CONTROL_STYLES
From: https://learn.microsoft.com/en-us/windows/win32/controls/common-control-styles.
Definition ResourceDialog.hpp:147
WINDOW_EXTENDED_STYLES
From https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles.
Definition ResourceDialog.hpp:121
ResourceDialog(ResourceDialog &&)=default
static dialogs_t parse(const ResourceData &node)
Parse dialogs from the given resource data node.
ResourceDialog & window_class(uint16_t ord)
Definition ResourceDialog.hpp:454
ResourceDialog(const ResourceDialog &)=default
const T * as() const
Helper to downcast a ResourceDialog into a ResourceDialogRegular or a ResourceDialogExtended.
Definition ResourceDialog.hpp:517
ResourceDialog & title(std::u16string value)
Definition ResourceDialog.hpp:459
friend std::ostream & operator<<(std::ostream &os, const ResourceDialog &dialog)
Definition ResourceDialog.hpp:526
ResourceDialog & menu(std::u16string title)
Definition ResourceDialog.hpp:437
ResourceDialog & title(std::string_view title)
DIALOG_STYLES
From: https://learn.microsoft.com/en-us/windows/win32/dlgbox/dialog-box-styles.
Definition ResourceDialog.hpp:75
int16_t cx() const
The width, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:401
bool has(WINDOW_EXTENDED_STYLES style) const
Check if the dialog uses the given extended window style.
Definition ResourceDialog.hpp:477
~ResourceDialog() override=default
uint32_t style() const
The style of the dialog box. This member can be a combination of window style values (such as WINDOW_...
Definition ResourceDialog.hpp:377
virtual std::unique_ptr< ResourceDialog > clone() const =0
bool has(WINDOW_STYLES style) const
Check if the dialog uses the given window style.
Definition ResourceDialog.hpp:472
ResourceDialog & menu(uint16_t ord)
Definition ResourceDialog.hpp:443
ResourceDialog & extended_style(uint32_t value)
Definition ResourceDialog.hpp:415
ResourceDialog & operator=(ResourceDialog &&)=default
ResourceDialog & y(int16_t value)
Definition ResourceDialog.hpp:424
ResourceDialog & cx(int16_t value)
Definition ResourceDialog.hpp:428
TYPE
Enum for discriminating the kind of the Dialog (regular vs extended).
Definition ResourceDialog.hpp:67
const ordinal_or_str_t & menu() const
ordinal or name value of a menu resource.
Definition ResourceDialog.hpp:499
ResourceDialog & style(uint32_t value)
Definition ResourceDialog.hpp:410
uint32_t extended_style() const
The extended styles for a window. This member is not used to create dialog boxes, but applications th...
Definition ResourceDialog.hpp:384
std::vector< std::unique_ptr< ResourceDialog > > dialogs_t
Definition ResourceDialog.hpp:46
bool has(DIALOG_STYLES style) const
Check if the dialog uses the given dialog style.
Definition ResourceDialog.hpp:467
WINDOW_STYLES
From: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles.
Definition ResourceDialog.hpp:96
int16_t x() const
The x-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:390
ResourceDialog & x(int16_t value)
Definition ResourceDialog.hpp:420
ResourceDialog & operator=(const ResourceDialog &)=default
std::vector< WINDOW_EXTENDED_STYLES > windows_ext_styles_list() const
List of WINDOW_EXTENDED_STYLES used by this dialog.
std::string title_utf8() const
title of the dialog box
ResourceDialog & cy(int16_t value)
Definition ResourceDialog.hpp:432
int16_t y() const
The y-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:396
int16_t cy() const
The height, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:406
static dialogs_t parse(const uint8_t *buffer, size_t size)
const ordinal_or_str_t & window_class() const
ordinal of a predefined system window class or name of a registered window class
Definition ResourceDialog.hpp:505
const std::u16string & title() const
title of the dialog box
Definition ResourceDialog.hpp:491
TYPE type() const
Definition ResourceDialog.hpp:370
std::vector< DIALOG_STYLES > styles_list() const
List of DIALOG_STYLES used by this dialog.
ResourceDialog(TYPE ty)
Definition ResourceDialog.hpp:365
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
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:24
@ not_found
Definition errors.hpp:26
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:55
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:32
const char * to_string(CODE_PAGES e)
@ RIGHT
Definition AcceleratorCodes.hpp:59
@ LEFT
Definition AcceleratorCodes.hpp:57
@ T
Definition AcceleratorCodes.hpp:97
@ CONTROL
Definition AcceleratorCodes.hpp:37
LIEF namespace.
Definition Abstract/Binary.hpp:42
tcb::span< ElementType, Extent > span
Definition span.hpp:22
This structure wraps either an ordinal value (uint16_t) or a string. The ordinal value should refer t...
Definition ResourceDialog.hpp:51
bool is_defined() const
Definition ResourceDialog.hpp:55
std::u16string string
Definition ResourceDialog.hpp:53
result< uint16_t > ordinal
Definition ResourceDialog.hpp:52
#define LIEF_API
Definition visibility.h:45