LIEF: Library to Instrument Executable Formats Version 1.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
20#include "LIEF/visibility.h"
21#include "LIEF/Object.hpp"
22#include "LIEF/enums.hpp"
23#include "LIEF/errors.hpp"
24#include "LIEF/span.hpp"
25
26#include <memory>
27#include <vector>
28#include <ostream>
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 Item& y(int16_t value) {
261 y_ = value;
262 return *this;
263 }
264 Item& cx(int16_t value) {
265 cx_ = value;
266 return *this;
267 }
268 Item& cy(int16_t value) {
269 cy_ = value;
270 return *this;
271 }
272
273 Item& id(int32_t value) {
274 id_ = value;
275 return *this;
276 }
277
278 Item& data(std::vector<uint8_t> creation_data) {
279 creation_data_ = std::move(creation_data);
280 return *this;
281 }
282
283 Item& clazz(std::u16string title) {
284 class_.string = std::move(title);
285 class_.ordinal = make_error_code(lief_errors::not_found);
286 return *this;
287 }
288
289 Item& clazz(uint16_t ord) {
290 class_.ordinal = ord;
291 return *this;
292 }
293
294 Item& title(std::u16string value) {
295 title_.string = std::move(value);
296 title_.ordinal = make_error_code(lief_errors::not_found);
297 return *this;
298 }
299
300 Item& title(uint16_t ord) {
301 title_.ordinal = ord;
302 return *this;
303 }
304
308 const ordinal_or_str_t& clazz() const {
309 return class_;
310 }
311
315 const ordinal_or_str_t& title() const {
316 return title_;
317 }
318
321 return creation_data_;
322 }
323
325 return creation_data_;
326 }
327
328 virtual ~Item() = default;
329
330 virtual std::string to_string() const = 0;
331
332 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Item& item) {
333 os << item.to_string();
334 return os;
335 }
336
337 protected:
338 uint32_t style_ = 0;
339 uint32_t extended_style_ = 0;
340
341 int16_t x_ = 0;
342 int16_t y_ = 0;
343 int16_t cx_ = 0;
344 int16_t cy_ = 0;
345
346 int32_t id_ = 0;
347
348 ordinal_or_str_t class_;
349 ordinal_or_str_t title_;
350 std::vector<uint8_t> creation_data_;
351 };
352
354 static dialogs_t parse(const ResourceData& node);
355 static dialogs_t parse(const uint8_t* buffer, size_t size);
356
357 ResourceDialog() = default;
360
363
365 type_(ty) {}
366
367 virtual std::unique_ptr<ResourceDialog> clone() const = 0;
368
369 TYPE type() const {
370 return type_;
371 }
372
376 uint32_t style() const {
377 return style_;
378 }
379
383 uint32_t extended_style() const {
384 return extended_style_;
385 }
386
389 int16_t x() const {
390 return x_;
391 }
392
395 int16_t y() const {
396 return y_;
397 }
398
400 int16_t cx() const {
401 return cx_;
402 }
403
405 int16_t cy() const {
406 return cy_;
407 }
408
410 style_ = value;
411 return *this;
412 }
413
415 extended_style_ = value;
416 return *this;
417 }
418
420 x_ = value;
421 return *this;
422 }
424 y_ = value;
425 return *this;
426 }
428 cx_ = value;
429 return *this;
430 }
432 cy_ = value;
433 return *this;
434 }
435
436 ResourceDialog& menu(std::u16string title) {
437 menu_.string = std::move(title);
439 return *this;
440 }
441
442 ResourceDialog& menu(uint16_t ord) {
443 menu_.ordinal = ord;
444 return *this;
445 }
446
448 window_class_.string = std::move(title);
449 window_class_.ordinal = make_error_code(lief_errors::not_found);
450 return *this;
451 }
452
454 window_class_.ordinal = ord;
455 return *this;
456 }
457
458 ResourceDialog& title(std::u16string value) LIEF_LIFETIMEBOUND {
459 title_ = std::move(value);
460 return *this;
461 }
462
464
466 bool has(DIALOG_STYLES style) const {
467 return (style_ & (uint32_t)style) != 0;
468 }
469
471 bool has(WINDOW_STYLES style) const {
472 return (style_ & (uint32_t)style) != 0;
473 }
474
477 return (extended_style_ & (uint32_t)style) != 0;
478 }
479
481 std::vector<DIALOG_STYLES> styles_list() const;
482
484 std::vector<WINDOW_STYLES> windows_styles_list() const;
485
487 std::vector<WINDOW_EXTENDED_STYLES> windows_ext_styles_list() const;
488
490 const std::u16string& title() const LIEF_LIFETIMEBOUND {
491 return title_;
492 }
493
495 std::string title_utf8() const;
496
498 const ordinal_or_str_t& menu() const {
499 return menu_;
500 }
501
505 return window_class_;
506 }
507
508 virtual std::string to_string() const = 0;
509
510 virtual ~ResourceDialog() = default;
511
512
515 template<class T>
516 const T* as() const {
517 static_assert(std::is_base_of<ResourceDialog, T>::value,
518 "Require ResourceDialog inheritance");
519 if (T::classof(this)) {
520 return static_cast<const T*>(this);
521 }
522 return nullptr;
523 }
524
525 friend LIEF_API std::ostream& operator<<(std::ostream& os,
526 const ResourceDialog& dialog) {
527 os << dialog.to_string();
528 return os;
529 }
530
531 protected:
532 static ok_error_t parse_menu(ResourceDialog& dialog, BinaryStream& stream);
533 static ok_error_t parse_class(ResourceDialog& dialog, BinaryStream& stream);
534 static ok_error_t parse_class(ResourceDialog::Item& dialog,
535 BinaryStream& stream);
536 static ok_error_t parse_title(ResourceDialog& dialog, BinaryStream& stream);
537 static ok_error_t parse_title(ResourceDialog::Item& dialog,
538 BinaryStream& stream);
539 static ok_error_t parse_creation_data(ResourceDialog::Item& item,
540 BinaryStream& stream);
541 TYPE type_ = TYPE::UNKNOWN;
542
543 uint32_t style_ = 0;
544 uint32_t extended_style_ = 0;
545
546 int16_t x_ = 0;
547 int16_t y_ = 0;
548 int16_t cx_ = 0;
549 int16_t cy_ = 0;
550
551 ordinal_or_str_t menu_;
552 ordinal_or_str_t window_class_;
553
554 std::u16string title_;
555};
556
561
562}
563}
564
569
570
571#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:34
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:308
Item & cy(int16_t value)
Definition ResourceDialog.hpp:268
Item & y(int16_t value)
Definition ResourceDialog.hpp:260
friend std::ostream & operator<<(std::ostream &os, const Item &item)
Definition ResourceDialog.hpp:332
Item & cx(int16_t value)
Definition ResourceDialog.hpp:264
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:315
Item & extended_style(uint32_t value)
Definition ResourceDialog.hpp:251
span< uint8_t > creation_data()
Definition ResourceDialog.hpp:324
virtual std::string to_string() const =0
Item & title(uint16_t ord)
Definition ResourceDialog.hpp:300
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:320
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:289
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:278
std::vector< CONTROL_STYLES > control_styles() const
List of CONTROL_STYLES used by this item.
Item & id(int32_t value)
Definition ResourceDialog.hpp:273
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:294
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:283
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:447
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:453
ResourceDialog(const ResourceDialog &)=default
const T * as() const
Helper to downcast a ResourceDialog into a ResourceDialogRegular or a ResourceDialogExtended.
Definition ResourceDialog.hpp:516
ResourceDialog & title(std::u16string value)
Definition ResourceDialog.hpp:458
ResourceDialog & title(const std::string &title)
friend std::ostream & operator<<(std::ostream &os, const ResourceDialog &dialog)
Definition ResourceDialog.hpp:525
ResourceDialog & menu(std::u16string title)
Definition ResourceDialog.hpp:436
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:400
bool has(WINDOW_EXTENDED_STYLES style) const
Check if the dialog uses the given extended window style.
Definition ResourceDialog.hpp:476
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:376
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:471
ResourceDialog & menu(uint16_t ord)
Definition ResourceDialog.hpp:442
ResourceDialog & extended_style(uint32_t value)
Definition ResourceDialog.hpp:414
ResourceDialog & operator=(ResourceDialog &&)=default
ResourceDialog & y(int16_t value)
Definition ResourceDialog.hpp:423
ResourceDialog & cx(int16_t value)
Definition ResourceDialog.hpp:427
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:498
ResourceDialog & style(uint32_t value)
Definition ResourceDialog.hpp:409
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:383
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:466
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:389
ResourceDialog & x(int16_t value)
Definition ResourceDialog.hpp:419
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:431
virtual ~ResourceDialog()=default
int16_t y() const
The y-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:395
int16_t cy() const
The height, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:405
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:504
const std::u16string & title() const
title of the dialog box
Definition ResourceDialog.hpp:490
TYPE type() const
Definition ResourceDialog.hpp:369
std::vector< DIALOG_STYLES > styles_list() const
List of DIALOG_STYLES used by this dialog.
ResourceDialog(TYPE ty)
Definition ResourceDialog.hpp:364
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: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:45