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 ABSALIGN = 0x0001,
76 SYSMODAL = 0x0002,
77 LOCALEDIT = 0x0020,
78 SETFONT = 0x0040,
79 MODALFRAME = 0x0080,
80 NOIDLEMSG = 0x0100,
81 SETFOREGROUND = 0x0200,
82 S3DLOOK = 0x0004,
83 FIXEDSYS = 0x0008,
84 NOFAILCREATE = 0x0010,
85 CONTROL = 0x0400,
86 CENTER = 0x0800,
87 CENTERMOUSE = 0x1000,
88 CONTEXTHELP = 0x2000,
89 SHELLFONT = SETFONT | FIXEDSYS,
90 };
91
93 enum class WINDOW_STYLES : uint32_t {
94 OVERLAPPED = 0x00000000,
95 POPUP = 0x80000000,
96 CHILD = 0x40000000,
97 MINIMIZE = 0x20000000,
98 VISIBLE = 0x10000000,
99 DISABLED = 0x08000000,
100 CLIPSIBLINGS = 0x04000000,
101 CLIPCHILDREN = 0x02000000,
102 MAXIMIZE = 0x01000000,
103 CAPTION = 0x00C00000,
104 BORDER = 0x00800000,
105 DLGFRAME = 0x00400000,
106 VSCROLL = 0x00200000,
107 HSCROLL = 0x00100000,
108 SYSMENU = 0x00080000,
109 THICKFRAME = 0x00040000,
110 GROUP = 0x00020000,
111 TABSTOP = 0x00010000,
112 };
113
116 enum class WINDOW_EXTENDED_STYLES : uint32_t {
117 DLGMODALFRAME = 0x00000001,
118 NOPARENTNOTIFY = 0x00000004,
119 TOPMOST = 0x00000008,
120 ACCEPTFILES = 0x00000010,
121 TRANSPARENT_STY = 0x00000020,
122 MDICHILD = 0x00000040,
123 TOOLWINDOW = 0x00000080,
124 WINDOWEDGE = 0x00000100,
125 CLIENTEDGE = 0x00000200,
126 CONTEXTHELP = 0x00000400,
127
128 RIGHT = 0x00001000,
129 LEFT = 0x00000000,
130 RTLREADING = 0x00002000,
131 LEFTSCROLLBAR = 0x00004000,
132
133 CONTROLPARENT = 0x00010000,
134 STATICEDGE = 0x00020000,
135 APPWINDOW = 0x00040000,
136 };
137
140 enum class CONTROL_STYLES : uint32_t {
141 TOP = 0x00000001,
142 NOMOVEY = 0x00000002,
143 BOTTOM = 0x00000003,
144 NORESIZE = 0x00000004,
145 NOPARENTALIGN = 0x00000008,
146 ADJUSTABLE = 0x00000020,
147 NODIVIDER = 0x00000040,
148 VERT = 0x00000080,
149 LEFT = VERT | TOP,
150 RIGHT = VERT | BOTTOM,
151 NOMOVEX = VERT | NOMOVEY,
152 };
153
161 public:
162 Item() = default;
163 Item(const Item&) = default;
164 Item& operator=(const Item&) = default;
165
166 Item(Item&&) = default;
167 Item& operator=(Item&&) = default;
168
169 enum class WINDOW_CLASS : uint32_t {
170 BUTTON = 0x0080,
171 EDIT = 0x0081,
172 STATIC = 0x0082,
173 LIST_BOX = 0x0083,
174 SCROLL_BAR = 0x0084,
175 COMBO_BOX = 0x0085,
176 };
177
180 uint32_t style() const {
181 return style_;
182 }
183
189 uint32_t extended_style() const {
190 return extended_style_;
191 }
192
194 int32_t id() const {
195 return id_;
196 }
197
199 bool has(WINDOW_STYLES style) const {
200 return (style_ & (uint32_t)style) != 0;
201 }
202
205 return (style_ & (uint32_t)style) != 0;
206 }
207
209 std::vector<WINDOW_STYLES> window_styles() const;
210
212 std::vector<CONTROL_STYLES> control_styles() const;
213
217 int16_t x() const {
218 return x_;
219 }
220
224 int16_t y() const {
225 return y_;
226 }
227
229 int16_t cx() const {
230 return cx_;
231 }
232
234 int16_t cy() const {
235 return cy_;
236 }
237
238 Item& style(uint32_t value) {
239 style_ = value;
240 return *this;
241 }
242
243 Item& extended_style(uint32_t value) {
244 extended_style_ = value;
245 return *this;
246 }
247
248 Item& x(int16_t value) {
249 x_ = value;
250 return *this;
251 }
252 Item& y(int16_t value) {
253 y_ = value;
254 return *this;
255 }
256 Item& cx(int16_t value) {
257 cx_ = value;
258 return *this;
259 }
260 Item& cy(int16_t value) {
261 cy_ = value;
262 return *this;
263 }
264
265 Item& id(int32_t value) {
266 id_ = value;
267 return *this;
268 }
269
270 Item& data(std::vector<uint8_t> creation_data) {
271 creation_data_ = std::move(creation_data);
272 return *this;
273 }
274
275 Item& clazz(std::u16string title) {
276 class_.string = std::move(title);
277 class_.ordinal = make_error_code(lief_errors::not_found);
278 return *this;
279 }
280
281 Item& clazz(uint16_t ord) {
282 class_.ordinal = ord;
283 return *this;
284 }
285
286 Item& title(std::u16string value) {
287 title_.string = std::move(value);
288 title_.ordinal = make_error_code(lief_errors::not_found);
289 return *this;
290 }
291
292 Item& title(uint16_t ord) {
293 title_.ordinal = ord;
294 return *this;
295 }
296
300 const ordinal_or_str_t& clazz() const {
301 return class_;
302 }
303
307 const ordinal_or_str_t& title() const {
308 return title_;
309 }
310
313 return creation_data_;
314 }
315
317 return creation_data_;
318 }
319
320 virtual ~Item() = default;
321
322 virtual std::string to_string() const = 0;
323
324 LIEF_API friend std::ostream& operator<<(std::ostream& os, const Item& item) {
325 os << item.to_string();
326 return os;
327 }
328
329 protected:
330 uint32_t style_ = 0;
331 uint32_t extended_style_ = 0;
332
333 int16_t x_ = 0;
334 int16_t y_ = 0;
335 int16_t cx_ = 0;
336 int16_t cy_ = 0;
337
338 int32_t id_ = 0;
339
340 ordinal_or_str_t class_;
341 ordinal_or_str_t title_;
342 std::vector<uint8_t> creation_data_;
343 };
344
346 static dialogs_t parse(const ResourceData& node);
347 static dialogs_t parse(const uint8_t* buffer, size_t size);
348
349 ResourceDialog() = default;
352
355
357 type_(ty) {}
358
359 virtual std::unique_ptr<ResourceDialog> clone() const = 0;
360
361 TYPE type() const {
362 return type_;
363 }
364
368 uint32_t style() const {
369 return style_;
370 }
371
375 uint32_t extended_style() const {
376 return extended_style_;
377 }
378
381 int16_t x() const {
382 return x_;
383 }
384
387 int16_t y() const {
388 return y_;
389 }
390
392 int16_t cx() const {
393 return cx_;
394 }
395
397 int16_t cy() const {
398 return cy_;
399 }
400
402 style_ = value;
403 return *this;
404 }
405
407 extended_style_ = value;
408 return *this;
409 }
410
412 x_ = value;
413 return *this;
414 }
416 y_ = value;
417 return *this;
418 }
420 cx_ = value;
421 return *this;
422 }
424 cy_ = value;
425 return *this;
426 }
427
428 ResourceDialog& menu(std::u16string title) {
429 menu_.string = std::move(title);
431 return *this;
432 }
433
434 ResourceDialog& menu(uint16_t ord) {
435 menu_.ordinal = ord;
436 return *this;
437 }
438
440 window_class_.string = std::move(title);
441 window_class_.ordinal = make_error_code(lief_errors::not_found);
442 return *this;
443 }
444
446 window_class_.ordinal = ord;
447 return *this;
448 }
449
450 ResourceDialog& title(std::u16string value) LIEF_LIFETIMEBOUND {
451 title_ = std::move(value);
452 return *this;
453 }
454
456
458 bool has(DIALOG_STYLES style) const {
459 return (style_ & (uint32_t)style) != 0;
460 }
461
463 bool has(WINDOW_STYLES style) const {
464 return (style_ & (uint32_t)style) != 0;
465 }
466
469 return (extended_style_ & (uint32_t)style) != 0;
470 }
471
473 std::vector<DIALOG_STYLES> styles_list() const;
474
476 std::vector<WINDOW_STYLES> windows_styles_list() const;
477
479 std::vector<WINDOW_EXTENDED_STYLES> windows_ext_styles_list() const;
480
482 const std::u16string& title() const LIEF_LIFETIMEBOUND {
483 return title_;
484 }
485
487 std::string title_utf8() const;
488
490 const ordinal_or_str_t& menu() const {
491 return menu_;
492 }
493
497 return window_class_;
498 }
499
500 virtual std::string to_string() const = 0;
501
502 virtual ~ResourceDialog() = default;
503
504
507 template<class T>
508 const T* as() const {
509 static_assert(std::is_base_of<ResourceDialog, T>::value,
510 "Require ResourceDialog inheritance");
511 if (T::classof(this)) {
512 return static_cast<const T*>(this);
513 }
514 return nullptr;
515 }
516
517 friend LIEF_API std::ostream& operator<<(std::ostream& os,
518 const ResourceDialog& dialog) {
519 os << dialog.to_string();
520 return os;
521 }
522
523 protected:
524 static ok_error_t parse_menu(ResourceDialog& dialog, BinaryStream& stream);
525 static ok_error_t parse_class(ResourceDialog& dialog, BinaryStream& stream);
526 static ok_error_t parse_class(ResourceDialog::Item& dialog,
527 BinaryStream& stream);
528 static ok_error_t parse_title(ResourceDialog& dialog, BinaryStream& stream);
529 static ok_error_t parse_title(ResourceDialog::Item& dialog,
530 BinaryStream& stream);
531 static ok_error_t parse_creation_data(ResourceDialog::Item& item,
532 BinaryStream& stream);
533 TYPE type_ = TYPE::UNKNOWN;
534
535 uint32_t style_ = 0;
536 uint32_t extended_style_ = 0;
537
538 int16_t x_ = 0;
539 int16_t y_ = 0;
540 int16_t cx_ = 0;
541 int16_t cy_ = 0;
542
543 ordinal_or_str_t menu_;
544 ordinal_or_str_t window_class_;
545
546 std::u16string title_;
547};
548
553
554}
555}
556
561
562
563#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:34
This class represents an element of the dialog. It can be for instance, a button, or a caption.
Definition ResourceDialog.hpp:160
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:300
Item & cy(int16_t value)
Definition ResourceDialog.hpp:260
Item & y(int16_t value)
Definition ResourceDialog.hpp:252
friend std::ostream & operator<<(std::ostream &os, const Item &item)
Definition ResourceDialog.hpp:324
Item & cx(int16_t value)
Definition ResourceDialog.hpp:256
bool has(WINDOW_STYLES style) const
Check if this item has the given WINDOW_STYLES.
Definition ResourceDialog.hpp:199
WINDOW_CLASS
Definition ResourceDialog.hpp:169
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:307
Item & extended_style(uint32_t value)
Definition ResourceDialog.hpp:243
span< uint8_t > creation_data()
Definition ResourceDialog.hpp:316
virtual std::string to_string() const =0
Item & title(uint16_t ord)
Definition ResourceDialog.hpp:292
Item & operator=(Item &&)=default
int32_t id() const
The control identifier.
Definition ResourceDialog.hpp:194
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:312
uint32_t style() const
The style of the control. This can be a combination of WINDOW_STYLES or CONTROL_STYLES.
Definition ResourceDialog.hpp:180
std::vector< WINDOW_STYLES > window_styles() const
List of WINDOW_STYLES used by this item.
Item & clazz(uint16_t ord)
Definition ResourceDialog.hpp:281
int16_t cx() const
The width, in dialog box units, of the control.
Definition ResourceDialog.hpp:229
bool has(CONTROL_STYLES style) const
Check if this item has the given CONTROL_STYLES.
Definition ResourceDialog.hpp:204
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:189
Item & style(uint32_t value)
Definition ResourceDialog.hpp:238
int16_t cy() const
The height, in dialog box units, of the control.
Definition ResourceDialog.hpp:234
Item & operator=(const Item &)=default
Item & x(int16_t value)
Definition ResourceDialog.hpp:248
Item & data(std::vector< uint8_t > creation_data)
Definition ResourceDialog.hpp:270
std::vector< CONTROL_STYLES > control_styles() const
List of CONTROL_STYLES used by this item.
Item & id(int32_t value)
Definition ResourceDialog.hpp:265
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:224
Item & title(std::u16string value)
Definition ResourceDialog.hpp:286
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:217
Item & clazz(std::u16string title)
Definition ResourceDialog.hpp:275
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:439
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:140
WINDOW_EXTENDED_STYLES
From https://docs.microsoft.com/en-us/windows/win32/winmsg/extended-window-styles.
Definition ResourceDialog.hpp:116
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:445
ResourceDialog(const ResourceDialog &)=default
const T * as() const
Helper to downcast a ResourceDialog into a ResourceDialogRegular or a ResourceDialogExtended.
Definition ResourceDialog.hpp:508
ResourceDialog & title(std::u16string value)
Definition ResourceDialog.hpp:450
ResourceDialog & title(const std::string &title)
friend std::ostream & operator<<(std::ostream &os, const ResourceDialog &dialog)
Definition ResourceDialog.hpp:517
ResourceDialog & menu(std::u16string title)
Definition ResourceDialog.hpp:428
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:392
bool has(WINDOW_EXTENDED_STYLES style) const
Check if the dialog uses the given extended window style.
Definition ResourceDialog.hpp:468
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:368
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:463
ResourceDialog & menu(uint16_t ord)
Definition ResourceDialog.hpp:434
ResourceDialog & extended_style(uint32_t value)
Definition ResourceDialog.hpp:406
ResourceDialog & operator=(ResourceDialog &&)=default
ResourceDialog & y(int16_t value)
Definition ResourceDialog.hpp:415
ResourceDialog & cx(int16_t value)
Definition ResourceDialog.hpp:419
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:490
ResourceDialog & style(uint32_t value)
Definition ResourceDialog.hpp:401
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:375
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:458
WINDOW_STYLES
From: https://docs.microsoft.com/en-us/windows/win32/winmsg/window-styles.
Definition ResourceDialog.hpp:93
int16_t x() const
The x-coordinate, in dialog box units, of the upper-left corner of the dialog box.
Definition ResourceDialog.hpp:381
ResourceDialog & x(int16_t value)
Definition ResourceDialog.hpp:411
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:423
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:387
int16_t cy() const
The height, in dialog box units, of the dialog box.
Definition ResourceDialog.hpp:397
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:496
const std::u16string & title() const
title of the dialog box
Definition ResourceDialog.hpp:482
TYPE type() const
Definition ResourceDialog.hpp:361
std::vector< DIALOG_STYLES > styles_list() const
List of DIALOG_STYLES used by this dialog.
ResourceDialog(TYPE ty)
Definition ResourceDialog.hpp:356
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: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:54
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