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
19#include "LIEF/visibility.h"
20#include "LIEF/Object.hpp"
21#include "LIEF/enums.hpp"
22#include "LIEF/errors.hpp"
23#include "LIEF/span.hpp"
24
25#include <memory>
26#include <vector>
27#include <ostream>
28
29namespace LIEF {
30class BinaryStream;
31namespace PE {
32
33class ResourceData;
34
43 public:
44 using dialogs_t = std::vector<std::unique_ptr<ResourceDialog>>;
45
51 std::u16string string;
52
53 bool is_defined() const {
54 return ordinal || !string.empty();
55 }
56
57 operator bool() const {
58 return is_defined();
59 }
60
61 std::string to_string() const;
62 };
63
65 enum class TYPE {
66 UNKNOWN = 0,
67 REGULAR,
68 EXTENDED,
69 };
70
73 enum class DIALOG_STYLES : uint32_t {
74 ABSALIGN = 0x0001,
75 SYSMODAL = 0x0002,
76 LOCALEDIT = 0x0020,
77 SETFONT = 0x0040,
78 MODALFRAME = 0x0080,
79 NOIDLEMSG = 0x0100,
80 SETFOREGROUND = 0x0200,
81 S3DLOOK = 0x0004,
82 FIXEDSYS = 0x0008,
83 NOFAILCREATE = 0x0010,
84 CONTROL = 0x0400,
85 CENTER = 0x0800,
86 CENTERMOUSE = 0x1000,
87 CONTEXTHELP = 0x2000,
88 SHELLFONT = SETFONT | FIXEDSYS,
89 };
90
92 enum class WINDOW_STYLES : uint32_t {
93 OVERLAPPED = 0x00000000,
94 POPUP = 0x80000000,
95 CHILD = 0x40000000,
96 MINIMIZE = 0x20000000,
97 VISIBLE = 0x10000000,
98 DISABLED = 0x08000000,
99 CLIPSIBLINGS = 0x04000000,
100 CLIPCHILDREN = 0x02000000,
101 MAXIMIZE = 0x01000000,
102 CAPTION = 0x00C00000,
103 BORDER = 0x00800000,
104 DLGFRAME = 0x00400000,
105 VSCROLL = 0x00200000,
106 HSCROLL = 0x00100000,
107 SYSMENU = 0x00080000,
108 THICKFRAME = 0x00040000,
109 GROUP = 0x00020000,
110 TABSTOP = 0x00010000,
111 };
112
115 enum class WINDOW_EXTENDED_STYLES : uint32_t {
116 DLGMODALFRAME = 0x00000001,
117 NOPARENTNOTIFY = 0x00000004,
118 TOPMOST = 0x00000008,
119 ACCEPTFILES = 0x00000010,
120 TRANSPARENT_STY = 0x00000020,
121 MDICHILD = 0x00000040,
122 TOOLWINDOW = 0x00000080,
123 WINDOWEDGE = 0x00000100,
124 CLIENTEDGE = 0x00000200,
125 CONTEXTHELP = 0x00000400,
126
127 RIGHT = 0x00001000,
128 LEFT = 0x00000000,
129 RTLREADING = 0x00002000,
130 LEFTSCROLLBAR = 0x00004000,
131
132 CONTROLPARENT = 0x00010000,
133 STATICEDGE = 0x00020000,
134 APPWINDOW = 0x00040000,
135 };
136
139 enum class CONTROL_STYLES : uint32_t {
140 TOP = 0x00000001,
141 NOMOVEY = 0x00000002,
142 BOTTOM = 0x00000003,
143 NORESIZE = 0x00000004,
144 NOPARENTALIGN = 0x00000008,
145 ADJUSTABLE = 0x00000020,
146 NODIVIDER = 0x00000040,
147 VERT = 0x00000080,
148 LEFT = VERT | TOP,
149 RIGHT = VERT | BOTTOM,
150 NOMOVEX = VERT | NOMOVEY,
151 };
152
160 public:
161 Item() = default;
162 Item(const Item&) = default;
163 Item& operator=(const Item&) = default;
164
165 Item(Item&&) = default;
166 Item& operator=(Item&&) = default;
167
168 enum class WINDOW_CLASS : uint32_t {
169 BUTTON = 0x0080,
170 EDIT = 0x0081,
171 STATIC = 0x0082,
172 LIST_BOX = 0x0083,
173 SCROLL_BAR = 0x0084,
174 COMBO_BOX = 0x0085,
175 };
176
179 uint32_t style() const {
180 return style_;
181 }
182
188 uint32_t extended_style() const {
189 return extended_style_;
190 }
191
193 int32_t id() const {
194 return id_;
195 }
196
198 bool has(WINDOW_STYLES style) const {
199 return (style_ & (uint32_t)style) != 0;
200 }
201
204 return (style_ & (uint32_t)style) != 0;
205 }
206
208 std::vector<WINDOW_STYLES> window_styles() const;
209
211 std::vector<CONTROL_STYLES> control_styles() const;
212
216 int16_t x() const {
217 return x_;
218 }
219
223 int16_t y() const {
224 return y_;
225 }
226
228 int16_t cx() const {
229 return cx_;
230 }
231
233 int16_t cy() const {
234 return cy_;
235 }
236
237 Item& style(uint32_t value) {
238 style_ = value;
239 return *this;
240 }
241
242 Item& extended_style(uint32_t value) {
243 extended_style_ = value;
244 return *this;
245 }
246
247 Item& x(int16_t value) {
248 x_ = value;
249 return *this;
250 }
251 Item& y(int16_t value) {
252 y_ = value;
253 return *this;
254 }
255 Item& cx(int16_t value) {
256 cx_ = value;
257 return *this;
258 }
259 Item& cy(int16_t value) {
260 cy_ = value;
261 return *this;
262 }
263
264 Item& id(int32_t value) {
265 id_ = value;
266 return *this;
267 }
268
269 Item& data(std::vector<uint8_t> creation_data) {
270 creation_data_ = std::move(creation_data);
271 return *this;
272 }
273
274 Item& clazz(std::u16string title) {
275 class_.string = std::move(title);
276 class_.ordinal = make_error_code(lief_errors::not_found);
277 return *this;
278 }
279
280 Item& clazz(uint16_t ord) {
281 class_.ordinal = ord;
282 return *this;
283 }
284
285 Item& title(std::u16string value) {
286 title_.string = std::move(value);
287 title_.ordinal = make_error_code(lief_errors::not_found);
288 return *this;
289 }
290
291 Item& title(uint16_t ord) {
292 title_.ordinal = ord;
293 return *this;
294 }
295
299 const ordinal_or_str_t& clazz() const {
300 return class_;
301 }
302
306 const ordinal_or_str_t& title() const {
307 return title_;
308 }
309
312 return creation_data_;
313 }
314
316 return creation_data_;
317 }
318
319 virtual ~Item() = default;
320
321 virtual std::string to_string() const = 0;
322
323 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Item& item) {
324 os << item.to_string();
325 return os;
326 }
327
328 protected:
329 uint32_t style_ = 0;
330 uint32_t extended_style_ = 0;
331
332 int16_t x_ = 0;
333 int16_t y_ = 0;
334 int16_t cx_ = 0;
335 int16_t cy_ = 0;
336
337 int32_t id_ = 0;
338
339 ordinal_or_str_t class_;
340 ordinal_or_str_t title_;
341 std::vector<uint8_t> creation_data_;
342 };
343
345 static dialogs_t parse(const ResourceData& node);
346 static dialogs_t parse(const uint8_t* buffer, size_t size);
347
348 ResourceDialog() = default;
351
354
356 type_(ty) {}
357
358 virtual std::unique_ptr<ResourceDialog> clone() const = 0;
359
360 TYPE type() const {
361 return type_;
362 }
363
367 uint32_t style() const {
368 return style_;
369 }
370
374 uint32_t extended_style() const {
375 return extended_style_;
376 }
377
380 int16_t x() const {
381 return x_;
382 }
383
386 int16_t y() const {
387 return y_;
388 }
389
391 int16_t cx() const {
392 return cx_;
393 }
394
396 int16_t cy() const {
397 return cy_;
398 }
399
400 ResourceDialog& style(uint32_t value) {
401 style_ = value;
402 return *this;
403 }
404
406 extended_style_ = value;
407 return *this;
408 }
409
410 ResourceDialog& x(int16_t value) {
411 x_ = value;
412 return *this;
413 }
414 ResourceDialog& y(int16_t value) {
415 y_ = value;
416 return *this;
417 }
418 ResourceDialog& cx(int16_t value) {
419 cx_ = value;
420 return *this;
421 }
422 ResourceDialog& cy(int16_t value) {
423 cy_ = value;
424 return *this;
425 }
426
427 ResourceDialog& menu(std::u16string title) {
428 menu_.string = std::move(title);
430 return *this;
431 }
432
433 ResourceDialog& menu(uint16_t ord) {
434 menu_.ordinal = ord;
435 return *this;
436 }
437
439 window_class_.string = std::move(title);
440 window_class_.ordinal = make_error_code(lief_errors::not_found);
441 return *this;
442 }
443
445 window_class_.ordinal = ord;
446 return *this;
447 }
448
449 ResourceDialog& title(std::u16string value) {
450 title_ = std::move(value);
451 return *this;
452 }
453
454 ResourceDialog& title(const std::string& title);
455
457 bool has(DIALOG_STYLES style) const {
458 return (style_ & (uint32_t)style) != 0;
459 }
460
462 bool has(WINDOW_STYLES style) const {
463 return (style_ & (uint32_t)style) != 0;
464 }
465
468 return (extended_style_ & (uint32_t)style) != 0;
469 }
470
472 std::vector<DIALOG_STYLES> styles_list() const;
473
475 std::vector<WINDOW_STYLES> windows_styles_list() const;
476
478 std::vector<WINDOW_EXTENDED_STYLES> windows_ext_styles_list() const;
479
481 const std::u16string& title() const {
482 return title_;
483 }
484
486 std::string title_utf8() const;
487
489 const ordinal_or_str_t& menu() const {
490 return menu_;
491 }
492
496 return window_class_;
497 }
498
499 virtual std::string to_string() const = 0;
500
501 virtual ~ResourceDialog() = default;
502
503
506 template<class T>
507 const T* as() const {
508 static_assert(std::is_base_of<ResourceDialog, T>::value,
509 "Require ResourceDialog inheritance");
510 if (T::classof(this)) {
511 return static_cast<const T*>(this);
512 }
513 return nullptr;
514 }
515
516 friend LIEF_API std::ostream& operator<<(std::ostream& os,
517 const ResourceDialog& dialog) {
518 os << dialog.to_string();
519 return os;
520 }
521
522 protected:
523 static ok_error_t parse_menu(ResourceDialog& dialog, BinaryStream& stream);
524 static ok_error_t parse_class(ResourceDialog& dialog, BinaryStream& stream);
525 static ok_error_t parse_class(ResourceDialog::Item& dialog,
526 BinaryStream& stream);
527 static ok_error_t parse_title(ResourceDialog& dialog, BinaryStream& stream);
528 static ok_error_t parse_title(ResourceDialog::Item& dialog,
529 BinaryStream& stream);
530 static ok_error_t parse_creation_data(ResourceDialog::Item& item,
531 BinaryStream& stream);
532 TYPE type_ = TYPE::UNKNOWN;
533
534 uint32_t style_ = 0;
535 uint32_t extended_style_ = 0;
536
537 int16_t x_ = 0;
538 int16_t y_ = 0;
539 int16_t cx_ = 0;
540 int16_t cy_ = 0;
541
542 ordinal_or_str_t menu_;
543 ordinal_or_str_t window_class_;
544
545 std::u16string title_;
546};
547
552
553}
554}
555
560
561
562#endif
Class that is used to a read stream of data from different sources.
Definition BinaryStream.hpp:33
Class which represents a Data Node in the PE resources tree.
Definition ResourceData.hpp:33
This class represents an element of the dialog. It can be for instance, a button, or a caption.
Definition ResourceDialog.hpp:159
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:299
Item & cy(int16_t value)
Definition ResourceDialog.hpp:259
Item & y(int16_t value)
Definition ResourceDialog.hpp:251
friend std::ostream & operator<<(std::ostream &os, const Item &item)
Definition ResourceDialog.hpp:323
Item & cx(int16_t value)
Definition ResourceDialog.hpp:255
bool has(WINDOW_STYLES style) const
Check if this item has the given WINDOW_STYLES.
Definition ResourceDialog.hpp:198
WINDOW_CLASS
Definition ResourceDialog.hpp:168
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:306
Item & extended_style(uint32_t value)
Definition ResourceDialog.hpp:242
span< uint8_t > creation_data()
Definition ResourceDialog.hpp:315
virtual std::string to_string() const =0
Item & title(uint16_t ord)
Definition ResourceDialog.hpp:291
Item & operator=(Item &&)=default
int32_t id() const
The control identifier.
Definition ResourceDialog.hpp:193
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:311
uint32_t style() const
The style of the control. This can be a combination of WINDOW_STYLES or CONTROL_STYLES.
Definition ResourceDialog.hpp:179
std::vector< WINDOW_STYLES > window_styles() const
List of WINDOW_STYLES used by this item.
Item & clazz(uint16_t ord)
Definition ResourceDialog.hpp:280
int16_t cx() const
The width, in dialog box units, of the control.
Definition ResourceDialog.hpp:228
bool has(CONTROL_STYLES style) const
Check if this item has the given CONTROL_STYLES.
Definition ResourceDialog.hpp:203
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:188
Item & style(uint32_t value)
Definition ResourceDialog.hpp:237
int16_t cy() const
The height, in dialog box units, of the control.
Definition ResourceDialog.hpp:233
Item & operator=(const Item &)=default
Item & x(int16_t value)
Definition ResourceDialog.hpp:247
Item & data(std::vector< uint8_t > creation_data)
Definition ResourceDialog.hpp:269
std::vector< CONTROL_STYLES > control_styles() const
List of CONTROL_STYLES used by this item.
Item & id(int32_t value)
Definition ResourceDialog.hpp:264
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:223
Item & title(std::u16string value)
Definition ResourceDialog.hpp:285
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:216
Item & clazz(std::u16string title)
Definition ResourceDialog.hpp:274
This class is the base class for either a regular (legacy) Dialog or an extended Dialog....
Definition ResourceDialog.hpp:42
ResourceDialog & window_class(std::u16string title)
Definition ResourceDialog.hpp:438
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:139
WINDOW_EXTENDED_STYLES
From https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles.
Definition ResourceDialog.hpp:115
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:444
ResourceDialog(const ResourceDialog &)=default
const T * as() const
Helper to downcast a ResourceDialog into a ResourceDialogRegular or a ResourceDialogExtended.
Definition ResourceDialog.hpp:507
ResourceDialog & title(std::u16string value)
Definition ResourceDialog.hpp:449
ResourceDialog & title(const std::string &title)
friend std::ostream & operator<<(std::ostream &os, const ResourceDialog &dialog)
Definition ResourceDialog.hpp:516
ResourceDialog & menu(std::u16string title)
Definition ResourceDialog.hpp:427
DIALOG_STYLES
From: https://learn.microsoft.com/en-us/windows/win32/dlgbox/dialog-box-styles.
Definition ResourceDialog.hpp:73
int16_t cx() const
The width, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:391
bool has(WINDOW_EXTENDED_STYLES style) const
Check if the dialog used to given extended window style.
Definition ResourceDialog.hpp:467
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:367
virtual std::unique_ptr< ResourceDialog > clone() const =0
bool has(WINDOW_STYLES style) const
Check if the dialog used to given window style.
Definition ResourceDialog.hpp:462
ResourceDialog & menu(uint16_t ord)
Definition ResourceDialog.hpp:433
ResourceDialog & extended_style(uint32_t value)
Definition ResourceDialog.hpp:405
ResourceDialog & operator=(ResourceDialog &&)=default
ResourceDialog & y(int16_t value)
Definition ResourceDialog.hpp:414
ResourceDialog & cx(int16_t value)
Definition ResourceDialog.hpp:418
TYPE
Enum for discriminating the kind of the Dialog (regular vs extended).
Definition ResourceDialog.hpp:65
const ordinal_or_str_t & menu() const
ordinal or name value of a menu resource.
Definition ResourceDialog.hpp:489
ResourceDialog & style(uint32_t value)
Definition ResourceDialog.hpp:400
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:374
std::vector< std::unique_ptr< ResourceDialog > > dialogs_t
Definition ResourceDialog.hpp:44
bool has(DIALOG_STYLES style) const
Check if the dialog used to given dialog style.
Definition ResourceDialog.hpp:457
WINDOW_STYLES
From: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles.
Definition ResourceDialog.hpp:92
int16_t x() const
The x-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:380
ResourceDialog & x(int16_t value)
Definition ResourceDialog.hpp:410
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:422
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:386
int16_t cy() const
The height, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:396
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:495
const std::u16string & title() const
title of the dialog box
Definition ResourceDialog.hpp:481
TYPE type() const
Definition ResourceDialog.hpp:360
std::vector< DIALOG_STYLES > styles_list() const
List of DIALOG_STYLES used by this dialog.
ResourceDialog(TYPE ty)
Definition ResourceDialog.hpp:355
Opaque structure that is used by LIEF to avoid writing result<void> f(...). Instead,...
Definition errors.hpp:114
Wrapper that contains an Object (T) or an error.
Definition errors.hpp:77
#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 an standard error code from lief_errors.
Definition errors.hpp:53
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:40
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:49
bool is_defined() const
Definition ResourceDialog.hpp:53
std::u16string string
Definition ResourceDialog.hpp:51
result< uint16_t > ordinal
Definition ResourceDialog.hpp:50
#define LIEF_API
Definition visibility.h:43