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 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#ifndef LIEF_PE_RESOURCE_DIALOG_H
16#define LIEF_PE_RESOURCE_DIALOG_H
17
18#include "LIEF/Object.hpp"
20#include "LIEF/enums.hpp"
21#include "LIEF/errors.hpp"
22#include "LIEF/span.hpp"
23#include "LIEF/visibility.h"
24#include <string_view>
25
26#include <memory>
27#include <ostream>
28#include <vector>
29
30namespace LIEF {
31class BinaryStream;
32namespace PE {
33
34class ResourceData;
35
44 public:
45 using dialogs_t = std::vector<std::unique_ptr<ResourceDialog>>;
46
52 std::u16string string;
53
54 bool is_defined() const {
55 return ordinal || !string.empty();
56 }
57
58 operator bool() const {
59 return is_defined();
60 }
61
62 std::string to_string() const;
63 };
64
66 enum class TYPE {
67 UNKNOWN = 0,
68 REGULAR,
69 EXTENDED,
70 };
71
74 enum class DIALOG_STYLES : uint32_t {
75 // clang-format off
76 ABSALIGN = 0x0001,
77 SYSMODAL = 0x0002,
78 LOCALEDIT = 0x0020,
79 SETFONT = 0x0040,
80 MODALFRAME = 0x0080,
81 NOIDLEMSG = 0x0100,
82 SETFOREGROUND = 0x0200,
83 S3DLOOK = 0x0004,
84 FIXEDSYS = 0x0008,
85 NOFAILCREATE = 0x0010,
86 CONTROL = 0x0400,
87 CENTER = 0x0800,
88 CENTERMOUSE = 0x1000,
89 CONTEXTHELP = 0x2000,
90 SHELLFONT = SETFONT | FIXEDSYS,
91 // clang-format on
92 };
93
95 enum class WINDOW_STYLES : uint32_t {
96 // clang-format off
97 OVERLAPPED = 0x00000000,
98 POPUP = 0x80000000,
99 CHILD = 0x40000000,
100 MINIMIZE = 0x20000000,
101 VISIBLE = 0x10000000,
102 DISABLED = 0x08000000,
103 CLIPSIBLINGS = 0x04000000,
104 CLIPCHILDREN = 0x02000000,
105 MAXIMIZE = 0x01000000,
106 CAPTION = 0x00C00000,
107 BORDER = 0x00800000,
108 DLGFRAME = 0x00400000,
109 VSCROLL = 0x00200000,
110 HSCROLL = 0x00100000,
111 SYSMENU = 0x00080000,
112 THICKFRAME = 0x00040000,
113 GROUP = 0x00020000,
114 TABSTOP = 0x00010000,
115 // clang-format on
116 };
117
120 enum class WINDOW_EXTENDED_STYLES : uint32_t {
121 // clang-format off
122 DLGMODALFRAME = 0x00000001,
123 NOPARENTNOTIFY = 0x00000004,
124 TOPMOST = 0x00000008,
125 ACCEPTFILES = 0x00000010,
126 TRANSPARENT_STY = 0x00000020,
127 MDICHILD = 0x00000040,
128 TOOLWINDOW = 0x00000080,
129 WINDOWEDGE = 0x00000100,
130 CLIENTEDGE = 0x00000200,
131 CONTEXTHELP = 0x00000400,
132
133 RIGHT = 0x00001000,
134 LEFT = 0x00000000,
135 RTLREADING = 0x00002000,
136 LEFTSCROLLBAR = 0x00004000,
137
138 CONTROLPARENT = 0x00010000,
139 STATICEDGE = 0x00020000,
140 APPWINDOW = 0x00040000,
141 // clang-format on
142 };
143
146 enum class CONTROL_STYLES : uint32_t {
147 // clang-format off
148 TOP = 0x00000001,
149 NOMOVEY = 0x00000002,
150 BOTTOM = 0x00000003,
151 NORESIZE = 0x00000004,
152 NOPARENTALIGN = 0x00000008,
153 ADJUSTABLE = 0x00000020,
154 NODIVIDER = 0x00000040,
155 VERT = 0x00000080,
156 LEFT = VERT | TOP,
157 RIGHT = VERT | BOTTOM,
158 NOMOVEX = VERT | NOMOVEY,
159 // clang-format on
160 };
161
169 public:
170 Item() = default;
171 Item(const Item&) = default;
172 Item& operator=(const Item&) = default;
173
174 Item(Item&&) = default;
175 Item& operator=(Item&&) = default;
176
177 enum class WINDOW_CLASS : uint32_t {
178 BUTTON = 0x0080,
179 EDIT = 0x0081,
180 STATIC = 0x0082,
181 LIST_BOX = 0x0083,
182 SCROLL_BAR = 0x0084,
183 COMBO_BOX = 0x0085,
184 };
185
188 uint32_t style() const {
189 return style_;
190 }
191
197 uint32_t extended_style() const {
198 return extended_style_;
199 }
200
202 int32_t id() const {
203 return id_;
204 }
205
207 bool has(WINDOW_STYLES style) const {
208 return (style_ & (uint32_t)style) != 0;
209 }
210
213 return (style_ & (uint32_t)style) != 0;
214 }
215
217 std::vector<WINDOW_STYLES> window_styles() const;
218
220 std::vector<CONTROL_STYLES> control_styles() const;
221
225 int16_t x() const {
226 return x_;
227 }
228
232 int16_t y() const {
233 return y_;
234 }
235
237 int16_t cx() const {
238 return cx_;
239 }
240
242 int16_t cy() const {
243 return cy_;
244 }
245
246 Item& style(uint32_t value) {
247 style_ = value;
248 return *this;
249 }
250
251 Item& extended_style(uint32_t value) {
252 extended_style_ = value;
253 return *this;
254 }
255
256 Item& x(int16_t value) {
257 x_ = value;
258 return *this;
259 }
260
261 Item& y(int16_t value) {
262 y_ = value;
263 return *this;
264 }
265
266 Item& cx(int16_t value) {
267 cx_ = value;
268 return *this;
269 }
270
271 Item& cy(int16_t value) {
272 cy_ = value;
273 return *this;
274 }
275
276 Item& id(int32_t value) {
277 id_ = value;
278 return *this;
279 }
280
281 Item& data(std::vector<uint8_t> creation_data) {
282 creation_data_ = std::move(creation_data);
283 return *this;
284 }
285
286 Item& clazz(std::u16string title) {
287 class_.string = std::move(title);
288 class_.ordinal = make_error_code(lief_errors::not_found);
289 return *this;
290 }
291
292 Item& clazz(uint16_t ord) {
293 class_.ordinal = ord;
294 return *this;
295 }
296
297 Item& title(std::u16string value) {
298 title_.string = std::move(value);
299 title_.ordinal = make_error_code(lief_errors::not_found);
300 return *this;
301 }
302
303 Item& title(uint16_t ord) {
304 title_.ordinal = ord;
305 return *this;
306 }
307
311 const ordinal_or_str_t& clazz() const {
312 return class_;
313 }
314
318 const ordinal_or_str_t& title() const {
319 return title_;
320 }
321
324 return creation_data_;
325 }
326
328 return creation_data_;
329 }
330
331 virtual ~Item() = default;
332
333 virtual std::string to_string() const = 0;
334
335 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Item& item) {
336 os << item.to_string();
337 return os;
338 }
339
340 protected:
341 uint32_t style_ = 0;
342 uint32_t extended_style_ = 0;
343
344 int16_t x_ = 0;
345 int16_t y_ = 0;
346 int16_t cx_ = 0;
347 int16_t cy_ = 0;
348
349 int32_t id_ = 0;
350
351 ordinal_or_str_t class_;
352 ordinal_or_str_t title_;
353 std::vector<uint8_t> creation_data_;
354 };
355
357 static dialogs_t parse(const ResourceData& node);
358 static dialogs_t parse(const uint8_t* buffer, size_t size);
359
360 ResourceDialog() = default;
363
366
368 type_(ty) {}
369
370 virtual std::unique_ptr<ResourceDialog> clone() const = 0;
371
372 TYPE type() const {
373 return type_;
374 }
375
379 uint32_t style() const {
380 return style_;
381 }
382
386 uint32_t extended_style() const {
387 return extended_style_;
388 }
389
392 int16_t x() const {
393 return x_;
394 }
395
398 int16_t y() const {
399 return y_;
400 }
401
403 int16_t cx() const {
404 return cx_;
405 }
406
408 int16_t cy() const {
409 return cy_;
410 }
411
413 style_ = value;
414 return *this;
415 }
416
418 extended_style_ = value;
419 return *this;
420 }
421
423 x_ = value;
424 return *this;
425 }
426
428 y_ = value;
429 return *this;
430 }
431
433 cx_ = value;
434 return *this;
435 }
436
438 cy_ = value;
439 return *this;
440 }
441
442 ResourceDialog& menu(std::u16string title) {
443 menu_.string = std::move(title);
445 return *this;
446 }
447
448 ResourceDialog& menu(uint16_t ord) {
449 menu_.ordinal = ord;
450 return *this;
451 }
452
454 window_class_.string = std::move(title);
455 window_class_.ordinal = make_error_code(lief_errors::not_found);
456 return *this;
457 }
458
460 window_class_.ordinal = ord;
461 return *this;
462 }
463
464 ResourceDialog& title(std::u16string value) LIEF_LIFETIMEBOUND {
465 title_ = std::move(value);
466 return *this;
467 }
468
470
472 bool has(DIALOG_STYLES style) const {
473 return (style_ & (uint32_t)style) != 0;
474 }
475
477 bool has(WINDOW_STYLES style) const {
478 return (style_ & (uint32_t)style) != 0;
479 }
480
483 return (extended_style_ & (uint32_t)style) != 0;
484 }
485
487 std::vector<DIALOG_STYLES> styles_list() const;
488
490 std::vector<WINDOW_STYLES> windows_styles_list() const;
491
493 std::vector<WINDOW_EXTENDED_STYLES> windows_ext_styles_list() const;
494
496 const std::u16string& title() const LIEF_LIFETIMEBOUND {
497 return title_;
498 }
499
501 std::string title_utf8() const;
502
504 const ordinal_or_str_t& menu() const {
505 return menu_;
506 }
507
511 return window_class_;
512 }
513
514 virtual std::string to_string() const = 0;
515
516 ~ResourceDialog() override = default;
517
518
521 template<class T>
522 const T* as() const {
523 static_assert(std::is_base_of_v<ResourceDialog, T>,
524 "Require ResourceDialog inheritance");
525 if (T::classof(this)) {
526 return static_cast<const T*>(this);
527 }
528 return nullptr;
529 }
530
531 friend LIEF_API std::ostream& operator<<(std::ostream& os,
532 const ResourceDialog& dialog) {
533 os << dialog.to_string();
534 return os;
535 }
536
537 protected:
538 static ok_error_t parse_menu(ResourceDialog& dialog, BinaryStream& stream);
539 static ok_error_t parse_class(ResourceDialog& dialog, BinaryStream& stream);
540 static ok_error_t parse_class(ResourceDialog::Item& dialog,
541 BinaryStream& stream);
542 static ok_error_t parse_title(ResourceDialog& dialog, BinaryStream& stream);
543 static ok_error_t parse_title(ResourceDialog::Item& dialog,
544 BinaryStream& stream);
545 static ok_error_t parse_creation_data(ResourceDialog::Item& item,
546 BinaryStream& stream);
547 TYPE type_ = TYPE::UNKNOWN;
548
549 uint32_t style_ = 0;
550 uint32_t extended_style_ = 0;
551
552 int16_t x_ = 0;
553 int16_t y_ = 0;
554 int16_t cx_ = 0;
555 int16_t cy_ = 0;
556
557 ordinal_or_str_t menu_;
558 ordinal_or_str_t window_class_;
559
560 std::u16string title_;
561};
562
567
568}
569}
570
575
576
577#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:34
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:35
This class represents an element of the dialog. It can be for instance, a button, or a caption.
Definition ResourceDialog.hpp:168
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:311
Item & cy(int16_t value)
Definition ResourceDialog.hpp:271
Item & y(int16_t value)
Definition ResourceDialog.hpp:261
friend std::ostream & operator<<(std::ostream &os, const Item &item)
Definition ResourceDialog.hpp:335
Item & cx(int16_t value)
Definition ResourceDialog.hpp:266
bool has(WINDOW_STYLES style) const
Check if this item has the given WINDOW_STYLES.
Definition ResourceDialog.hpp:207
WINDOW_CLASS
Definition ResourceDialog.hpp:177
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:318
Item & extended_style(uint32_t value)
Definition ResourceDialog.hpp:251
span< uint8_t > creation_data()
Definition ResourceDialog.hpp:327
virtual std::string to_string() const =0
Item & title(uint16_t ord)
Definition ResourceDialog.hpp:303
Item & operator=(Item &&)=default
int32_t id() const
The control identifier.
Definition ResourceDialog.hpp:202
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:323
uint32_t style() const
The style of the control. This can be a combination of WINDOW_STYLES or CONTROL_STYLES.
Definition ResourceDialog.hpp:188
std::vector< WINDOW_STYLES > window_styles() const
List of WINDOW_STYLES used by this item.
Item & clazz(uint16_t ord)
Definition ResourceDialog.hpp:292
int16_t cx() const
The width, in dialog box units, of the control.
Definition ResourceDialog.hpp:237
bool has(CONTROL_STYLES style) const
Check if this item has the given CONTROL_STYLES.
Definition ResourceDialog.hpp:212
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:197
Item & style(uint32_t value)
Definition ResourceDialog.hpp:246
int16_t cy() const
The height, in dialog box units, of the control.
Definition ResourceDialog.hpp:242
Item & operator=(const Item &)=default
Item & x(int16_t value)
Definition ResourceDialog.hpp:256
Item & data(std::vector< uint8_t > creation_data)
Definition ResourceDialog.hpp:281
std::vector< CONTROL_STYLES > control_styles() const
List of CONTROL_STYLES used by this item.
Item & id(int32_t value)
Definition ResourceDialog.hpp:276
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:232
Item & title(std::u16string value)
Definition ResourceDialog.hpp:297
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:225
Item & clazz(std::u16string title)
Definition ResourceDialog.hpp:286
This class is the base class for either a regular (legacy) Dialog or an extended Dialog....
Definition ResourceDialog.hpp:43
ResourceDialog & window_class(std::u16string title)
Definition ResourceDialog.hpp:453
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:146
WINDOW_EXTENDED_STYLES
From https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles.
Definition ResourceDialog.hpp:120
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:459
ResourceDialog(const ResourceDialog &)=default
const T * as() const
Helper to downcast a ResourceDialog into a ResourceDialogRegular or a ResourceDialogExtended.
Definition ResourceDialog.hpp:522
ResourceDialog & title(std::u16string value)
Definition ResourceDialog.hpp:464
friend std::ostream & operator<<(std::ostream &os, const ResourceDialog &dialog)
Definition ResourceDialog.hpp:531
ResourceDialog & menu(std::u16string title)
Definition ResourceDialog.hpp:442
ResourceDialog & title(std::string_view title)
DIALOG_STYLES
From: https://learn.microsoft.com/en-us/windows/win32/dlgbox/dialog-box-styles.
Definition ResourceDialog.hpp:74
int16_t cx() const
The width, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:403
bool has(WINDOW_EXTENDED_STYLES style) const
Check if the dialog uses the given extended window style.
Definition ResourceDialog.hpp:482
~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:379
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:477
ResourceDialog & menu(uint16_t ord)
Definition ResourceDialog.hpp:448
ResourceDialog & extended_style(uint32_t value)
Definition ResourceDialog.hpp:417
ResourceDialog & operator=(ResourceDialog &&)=default
ResourceDialog & y(int16_t value)
Definition ResourceDialog.hpp:427
ResourceDialog & cx(int16_t value)
Definition ResourceDialog.hpp:432
TYPE
Enum for discriminating the kind of the Dialog (regular vs extended).
Definition ResourceDialog.hpp:66
const ordinal_or_str_t & menu() const
ordinal or name value of a menu resource.
Definition ResourceDialog.hpp:504
ResourceDialog & style(uint32_t value)
Definition ResourceDialog.hpp:412
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:386
std::vector< std::unique_ptr< ResourceDialog > > dialogs_t
Definition ResourceDialog.hpp:45
bool has(DIALOG_STYLES style) const
Check if the dialog uses the given dialog style.
Definition ResourceDialog.hpp:472
WINDOW_STYLES
From: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles.
Definition ResourceDialog.hpp:95
int16_t x() const
The x-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:392
ResourceDialog & x(int16_t value)
Definition ResourceDialog.hpp:422
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:437
int16_t y() const
The y-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:398
int16_t cy() const
The height, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:408
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:510
const std::u16string & title() const
title of the dialog box
Definition ResourceDialog.hpp:496
TYPE type() const
Definition ResourceDialog.hpp:372
std::vector< DIALOG_STYLES > styles_list() const
List of DIALOG_STYLES used by this dialog.
ResourceDialog(TYPE ty)
Definition ResourceDialog.hpp:367
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:118
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:78
#define LIEF_LIFETIMEBOUND
Definition compiler_attributes.hpp:72
#define ENABLE_BITMASK_OPERATORS(X)
Definition enums.hpp:23
@ not_found
Definition errors.hpp:25
tl::unexpected< lief_errors > make_error_code(lief_errors e)
Create a standard error code from lief_errors.
Definition errors.hpp:54
Namespace related to the LIEF's PE module.
Definition Abstract/Header.hpp:31
const char * to_string(CODE_PAGES e)
@ RIGHT
Definition AcceleratorCodes.hpp:58
@ LEFT
Definition AcceleratorCodes.hpp:56
@ T
Definition AcceleratorCodes.hpp:96
@ CONTROL
Definition AcceleratorCodes.hpp:36
LIEF namespace.
Definition Abstract/Binary.hpp:41
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:50
bool is_defined() const
Definition ResourceDialog.hpp:54
std::u16string string
Definition ResourceDialog.hpp:52
result< uint16_t > ordinal
Definition ResourceDialog.hpp:51
#define LIEF_API
Definition visibility.h:44