lief 1.0.0

Official Rust bindings for LIEF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
use super::chpe_metadata_arm64;
use super::chpe_metadata_x86;
use super::dynamic_relocation::DynamicRelocation;
use super::enclave_configuration::EnclaveConfiguration;
use super::volatile_metadata::VolatileMetadata;
use crate::common::{FromFFI, into_optional};
use crate::pe::code_integrity::CodeIntegrity;
use crate::{declare_iterator, to_conv_opt, to_opt};
use bitflags::bitflags;
use lief_ffi as ffi;
use std::marker::PhantomData;

/// This structure represents the load configuration data associated with the
/// `IMAGE_LOAD_CONFIG_DIRECTORY`.
///
/// This structure is frequently updated by Microsoft to add new metadata.
///
/// Reference: <https://github.com/MicrosoftDocs/sdk-api/blob/cbeab4d371e8bc7e352c4d3a4c5819caa08c6a1c/sdk-api-src/content/winnt/ns-winnt-image_load_config_directory64.md#L2>
pub struct LoadConfiguration<'a> {
    ptr: cxx::UniquePtr<ffi::PE_LoadConfiguration>,
    _owner: PhantomData<&'a ffi::PE_Binary>,
}

impl LoadConfiguration<'_> {
    /// Characteristics of the structure which is defined by its size
    pub fn characteristics(&self) -> u32 {
        self.ptr.characteristics()
    }

    /// Size of the current structure
    pub fn size(&self) -> u32 {
        self.ptr.size()
    }

    /// The date and time stamp value
    pub fn timedatestamp(&self) -> u32 {
        self.ptr.timedatestamp()
    }

    /// Major version
    pub fn major_version(&self) -> u16 {
        self.ptr.major_version()
    }

    /// Minor version
    pub fn minor_version(&self) -> u16 {
        self.ptr.minor_version()
    }

    /// The global flags that control system behavior. For more information, see `Gflags.exe`.
    pub fn global_flags_clear(&self) -> u32 {
        self.ptr.global_flags_clear()
    }

    /// The global flags that control system behavior. For more information, see `Gflags.exe`.
    pub fn global_flags_set(&self) -> u32 {
        self.ptr.global_flags_set()
    }

    /// The critical section default time-out value.
    pub fn critical_section_default_timeout(&self) -> u32 {
        self.ptr.critical_section_default_timeout()
    }

    /// The size of the minimum block that must be freed before it is freed (de-committed), in bytes.
    /// This value is advisory.
    pub fn decommit_free_block_threshold(&self) -> u64 {
        self.ptr.decommit_free_block_threshold()
    }

    /// The size of the minimum total memory that must be freed in the process heap before it is
    /// freed (de-committed), in bytes. This value is advisory.
    pub fn decommit_total_free_threshold(&self) -> u64 {
        self.ptr.decommit_total_free_threshold()
    }

    /// The VA of a list of addresses where the `LOCK` prefix is used. These will be replaced by
    /// `NOP` on single-processor systems. This member is available only for x86.
    pub fn lock_prefix_table(&self) -> u64 {
        self.ptr.lock_prefix_table()
    }

    /// The maximum allocation size, in bytes. This member is obsolete and is used only for
    /// debugging purposes.
    pub fn maximum_allocation_size(&self) -> u64 {
        self.ptr.maximum_allocation_size()
    }

    /// The maximum block size that can be allocated from heap segments, in bytes.
    pub fn virtual_memory_threshold(&self) -> u64 {
        self.ptr.virtual_memory_threshold()
    }

    /// The process affinity mask. For more information, see `GetProcessAffinityMask`. This member
    /// is available only for `.exe` files.
    pub fn process_affinity_mask(&self) -> u64 {
        self.ptr.process_affinity_mask()
    }

    /// The process heap flags. For more information, see `HeapCreate`.
    pub fn process_heap_flags(&self) -> u32 {
        self.ptr.process_heap_flags()
    }

    /// The service pack version.
    pub fn csd_version(&self) -> u16 {
        self.ptr.csd_version()
    }

    /// See: [`LoadConfiguration::dependent_load_flags`]
    pub fn reserved1(&self) -> u16 {
        self.ptr.reserved1()
    }

    /// Alias for [`LoadConfiguration::reserved1`].
    ///
    /// The default load flags used when the operating system resolves the
    /// statically linked imports of a module. For more information, see
    /// `LoadLibraryEx`.
    pub fn dependent_load_flags(&self) -> u16 {
        self.ptr.dependent_load_flags()
    }

    /// Reserved for use by the system.
    pub fn editlist(&self) -> u64 {
        self.ptr.editlist()
    }

    /// A pointer to a cookie that is used by Visual C++ or GS implementation.
    pub fn security_cookie(&self) -> u64 {
        self.ptr.security_cookie()
    }

    /// The VA of the sorted table of RVAs of each valid, unique handler in the image. This member
    /// is available only for x86.
    pub fn se_handler_table(&self) -> Option<u64> {
        to_opt!(&lief_ffi::PE_LoadConfiguration::se_handler_table, &self);
    }

    /// The count of unique handlers in the table. This member is available only for x86.
    pub fn se_handler_count(&self) -> Option<u64> {
        to_opt!(&lief_ffi::PE_LoadConfiguration::se_handler_count, &self);
    }

    /// Return the list of the function RVA in the SEH table (if any)
    pub fn seh_functions(&self) -> Vec<u32> {
        Vec::from(self.ptr.seh_functions().as_slice())
    }

    /// The VA where Control Flow Guard check-function pointer is stored.
    pub fn guard_cf_check_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_cf_check_function_pointer,
            &self
        );
    }

    /// The VA where Control Flow Guard dispatch-function pointer is stored.
    pub fn guard_cf_dispatch_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_cf_dispatch_function_pointer,
            &self
        );
    }

    /// The VA of the sorted table of RVAs of each Control Flow Guard function in the image.
    pub fn guard_cf_function_table(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_cf_function_table,
            &self
        );
    }

    /// The count of unique RVAs in the [`LoadConfiguration::guard_cf_function_table`] table.
    pub fn guard_cf_function_count(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_cf_function_count,
            &self
        );
    }

    /// Iterator over the Control Flow Guard functions referenced by
    /// [`LoadConfiguration::guard_cf_function_table`]
    pub fn guard_cf_functions(&self) -> GuardCFFunctions<'_> {
        GuardCFFunctions::new(self.ptr.guard_cf_functions())
    }

    /// Control Flow Guard related flags.
    pub fn guard_flags(&self) -> Option<ImageGuardFlags> {
        to_conv_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_flags,
            &self,
            |e: u32| ImageGuardFlags::from(e)
        );
    }

    /// Code integrity information.
    pub fn code_integrity(&self) -> Option<CodeIntegrity<'_>> {
        into_optional(self.ptr.code_integrity())
    }

    /// The VA where Control Flow Guard address taken IAT table is stored.
    pub fn guard_address_taken_iat_entry_table(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_address_taken_iat_entry_table,
            &self
        );
    }

    /// The count of unique RVAs in the table pointed by
    /// [`LoadConfiguration::guard_address_taken_iat_entry_table`].
    pub fn guard_address_taken_iat_entry_count(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_address_taken_iat_entry_count,
            &self
        );
    }

    /// Iterator over the functions referenced by
    /// [`LoadConfiguration::guard_address_taken_iat_entry_table`]
    pub fn guard_address_taken_iat_entries(&self) -> GuardAddressTakenIATEntries<'_> {
        GuardAddressTakenIATEntries::new(self.ptr.guard_address_taken_iat_entries())
    }

    /// The VA where Control Flow Guard long jump target table is stored.
    pub fn guard_long_jump_target_table(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_long_jump_target_table,
            &self
        );
    }

    /// The count of unique RVAs in the table pointed by
    /// [`LoadConfiguration::guard_long_jump_target_table`].
    pub fn guard_long_jump_target_count(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_long_jump_target_count,
            &self
        );
    }

    /// Iterator over the functions referenced by
    /// [`LoadConfiguration::guard_long_jump_target_table`]
    pub fn guard_long_jump_targets(&self) -> GuardLongJumpTargets<'_> {
        GuardLongJumpTargets::new(self.ptr.guard_long_jump_targets())
    }

    /// VA of pointing to a `IMAGE_DYNAMIC_RELOCATION_TABLE`
    pub fn dynamic_value_reloc_table(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::dynamic_value_reloc_table,
            &self
        );
    }

    /// Alias for [`LoadConfiguration::chpe_metadata_pointer`]
    pub fn hybrid_metadata_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::hybrid_metadata_pointer,
            &self
        );
    }

    /// VA to the extra Compiled Hybrid Portable Executable (CHPE) metadata.
    pub fn chpe_metadata_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::chpe_metadata_pointer,
            &self
        );
    }

    /// Compiled Hybrid Portable Executable (CHPE) metadata (if any)
    pub fn chpe_metadata(&self) -> Option<CHPEMetadata<'_>> {
        into_optional(self.ptr.chpe_metadata())
    }

    /// VA of the failure routine
    pub fn guard_rf_failure_routine(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_rf_failure_routine,
            &self
        );
    }

    /// VA of the failure routine `fptr`.
    pub fn guard_rf_failure_routine_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_rf_failure_routine_function_pointer,
            &self
        );
    }

    /// Offset of dynamic relocation table relative to the relocation table
    pub fn dynamic_value_reloctable_offset(&self) -> Option<u32> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::dynamic_value_reloctable_offset,
            &self
        );
    }

    /// The section index of the dynamic value relocation table
    pub fn dynamic_value_reloctable_section(&self) -> Option<u16> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::dynamic_value_reloctable_section,
            &self
        );
    }

    /// Return an iterator over the Dynamic relocations (DVRT)
    pub fn dynamic_relocations(&self) -> DynamicRelocations<'_> {
        DynamicRelocations::new(self.ptr.dynamic_relocations())
    }

    /// Must be zero
    pub fn reserved2(&self) -> Option<u16> {
        to_opt!(&lief_ffi::PE_LoadConfiguration::reserved2, &self);
    }

    /// VA of the Function verifying the stack pointer
    pub fn guard_rf_verify_stackpointer_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_rf_verify_stackpointer_function_pointer,
            &self
        );
    }

    pub fn hotpatch_table_offset(&self) -> Option<u32> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::hotpatch_table_offset,
            &self
        );
    }

    pub fn reserved3(&self) -> Option<u32> {
        to_opt!(&lief_ffi::PE_LoadConfiguration::reserved3, &self);
    }

    pub fn enclave_config(&self) -> Option<EnclaveConfiguration<'_>> {
        into_optional(self.ptr.enclave_config())
    }

    pub fn enclave_configuration_ptr(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::enclave_configuration_ptr,
            &self
        );
    }

    pub fn volatile_metadata_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::volatile_metadata_pointer,
            &self
        );
    }

    pub fn volatile_metadata(&self) -> Option<VolatileMetadata<'_>> {
        into_optional(self.ptr.volatile_metadata())
    }

    pub fn guard_eh_continuation_table(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_eh_continuation_table,
            &self
        );
    }

    pub fn guard_eh_continuation_count(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_eh_continuation_count,
            &self
        );
    }

    /// Iterator over the Guard EH continuation functions referenced by
    /// [`LoadConfiguration::guard_eh_continuation_table`]
    pub fn guard_eh_continuation_functions(&self) -> GuardEhContinuationFunctions<'_> {
        GuardEhContinuationFunctions::new(self.ptr.guard_eh_continuation_functions())
    }

    pub fn guard_xfg_check_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_xfg_check_function_pointer,
            &self
        );
    }

    pub fn guard_xfg_dispatch_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_xfg_dispatch_function_pointer,
            &self
        );
    }

    pub fn guard_xfg_table_dispatch_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_xfg_table_dispatch_function_pointer,
            &self
        );
    }

    pub fn cast_guard_os_determined_failure_mode(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::cast_guard_os_determined_failure_mode,
            &self
        );
    }

    pub fn guard_memcpy_function_pointer(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::guard_memcpy_function_pointer,
            &self
        );
    }

    pub fn uma_function_pointers(&self) -> Option<u64> {
        to_opt!(
            &lief_ffi::PE_LoadConfiguration::uma_function_pointers,
            &self
        );
    }

    /// Set the characteristics value
    pub fn set_characteristics(&mut self, value: u32) {
        self.ptr.pin_mut().set_characteristics(value);
    }

    /// Set the size value
    pub fn set_size(&mut self, value: u32) {
        self.ptr.pin_mut().set_size(value);
    }

    /// Set the timedatestamp value
    pub fn set_timedatestamp(&mut self, value: u32) {
        self.ptr.pin_mut().set_timedatestamp(value);
    }

    /// Set the major version
    pub fn set_major_version(&mut self, value: u16) {
        self.ptr.pin_mut().set_major_version(value);
    }

    /// Set the minor version
    pub fn set_minor_version(&mut self, value: u16) {
        self.ptr.pin_mut().set_minor_version(value);
    }

    /// Set the global flags to clear
    pub fn set_global_flags_clear(&mut self, value: u32) {
        self.ptr.pin_mut().set_global_flags_clear(value);
    }

    /// Set the global flags to set
    pub fn set_global_flags_set(&mut self, value: u32) {
        self.ptr.pin_mut().set_global_flags_set(value);
    }

    /// Set the critical section default timeout
    pub fn set_critical_section_default_timeout(&mut self, value: u32) {
        self.ptr
            .pin_mut()
            .set_critical_section_default_timeout(value);
    }

    /// Set the decommit free block threshold
    pub fn set_decommit_free_block_threshold(&mut self, value: u64) {
        self.ptr.pin_mut().set_decommit_free_block_threshold(value);
    }

    /// Set the decommit total free threshold
    pub fn set_decommit_total_free_threshold(&mut self, value: u64) {
        self.ptr.pin_mut().set_decommit_total_free_threshold(value);
    }

    /// Set the lock prefix table VA
    pub fn set_lock_prefix_table(&mut self, value: u64) {
        self.ptr.pin_mut().set_lock_prefix_table(value);
    }

    /// Set the maximum allocation size
    pub fn set_maximum_allocation_size(&mut self, value: u64) {
        self.ptr.pin_mut().set_maximum_allocation_size(value);
    }

    /// Set the virtual memory threshold
    pub fn set_virtual_memory_threshold(&mut self, value: u64) {
        self.ptr.pin_mut().set_virtual_memory_threshold(value);
    }

    /// Set the process affinity mask
    pub fn set_process_affinity_mask(&mut self, value: u64) {
        self.ptr.pin_mut().set_process_affinity_mask(value);
    }

    /// Set the process heap flags
    pub fn set_process_heap_flags(&mut self, value: u32) {
        self.ptr.pin_mut().set_process_heap_flags(value);
    }

    /// Set the CSD version
    pub fn set_csd_version(&mut self, value: u16) {
        self.ptr.pin_mut().set_csd_version(value);
    }

    /// Set reserved1
    pub fn set_reserved1(&mut self, value: u16) {
        self.ptr.pin_mut().set_reserved1(value);
    }

    /// Set the dependent load flags
    pub fn set_dependent_load_flags(&mut self, value: u16) {
        self.ptr.pin_mut().set_dependent_load_flags(value);
    }

    /// Set the editlist VA
    pub fn set_editlist(&mut self, value: u32) {
        self.ptr.pin_mut().set_editlist(value);
    }

    /// Set the security cookie VA
    pub fn set_security_cookie(&mut self, value: u64) {
        self.ptr.pin_mut().set_security_cookie(value);
    }

    /// Set the SE handler table VA
    pub fn set_se_handler_table(&mut self, value: u64) {
        self.ptr.pin_mut().set_se_handler_table(value);
    }

    /// Set the SE handler count
    pub fn set_se_handler_count(&mut self, value: u64) {
        self.ptr.pin_mut().set_se_handler_count(value);
    }

    /// Set the guard CF check function pointer VA
    pub fn set_guard_cf_check_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_cf_check_function_pointer(value);
    }

    /// Set the guard CF dispatch function pointer VA
    pub fn set_guard_cf_dispatch_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_cf_dispatch_function_pointer(value);
    }

    /// Set the guard CF function table VA
    pub fn set_guard_cf_function_table(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_cf_function_table(value);
    }

    /// Set the guard CF function count
    pub fn set_guard_cf_function_count(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_cf_function_count(value);
    }

    /// Set the guard flags
    pub fn set_guard_flags(&mut self, flags: ImageGuardFlags) {
        self.ptr.pin_mut().set_guard_flags(flags.bits());
    }

    /// Set the guard address taken IAT entry table VA
    pub fn set_guard_address_taken_iat_entry_table(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_address_taken_iat_entry_table(value);
    }

    /// Set the guard address taken IAT entry count
    pub fn set_guard_address_taken_iat_entry_count(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_address_taken_iat_entry_count(value);
    }

    /// Set the guard long jump target table VA
    pub fn set_guard_long_jump_target_table(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_long_jump_target_table(value);
    }

    /// Set the guard long jump target count
    pub fn set_guard_long_jump_target_count(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_long_jump_target_count(value);
    }

    /// Set the dynamic value relocation table VA
    pub fn set_dynamic_value_reloc_table(&mut self, value: u64) {
        self.ptr.pin_mut().set_dynamic_value_reloc_table(value);
    }

    /// Set the hybrid metadata pointer VA
    pub fn set_hybrid_metadata_pointer(&mut self, value: u64) {
        self.ptr.pin_mut().set_hybrid_metadata_pointer(value);
    }

    /// Set the guard RF failure routine VA
    pub fn set_guard_rf_failure_routine(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_rf_failure_routine(value);
    }

    /// Set the guard RF failure routine function pointer VA
    pub fn set_guard_rf_failure_routine_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_rf_failure_routine_function_pointer(value);
    }

    /// Set the dynamic value relocation table offset
    pub fn set_dynamic_value_reloctable_offset(&mut self, value: u32) {
        self.ptr
            .pin_mut()
            .set_dynamic_value_reloctable_offset(value);
    }

    /// Set the dynamic value relocation table section
    pub fn set_dynamic_value_reloctable_section(&mut self, value: u16) {
        self.ptr
            .pin_mut()
            .set_dynamic_value_reloctable_section(value);
    }

    /// Set reserved2
    pub fn set_reserved2(&mut self, value: u16) {
        self.ptr.pin_mut().set_reserved2(value);
    }

    /// Set the guard RF verify stack pointer function pointer VA
    pub fn set_guard_rf_verify_stackpointer_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_rf_verify_stackpointer_function_pointer(value);
    }

    /// Set the hotpatch table offset
    pub fn set_hotpatch_table_offset(&mut self, value: u32) {
        self.ptr.pin_mut().set_hotpatch_table_offset(value);
    }

    /// Set reserved3
    pub fn set_reserved3(&mut self, value: u32) {
        self.ptr.pin_mut().set_reserved3(value);
    }

    /// Set the enclave configuration pointer VA
    pub fn set_enclave_configuration_ptr(&mut self, value: u64) {
        self.ptr.pin_mut().set_enclave_configuration_ptr(value);
    }

    /// Set the volatile metadata pointer VA
    pub fn set_volatile_metadata_pointer(&mut self, value: u64) {
        self.ptr.pin_mut().set_volatile_metadata_pointer(value);
    }

    /// Set the guard EH continuation table VA
    pub fn set_guard_eh_continuation_table(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_eh_continuation_table(value);
    }

    /// Set the guard EH continuation count
    pub fn set_guard_eh_continuation_count(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_eh_continuation_count(value);
    }

    /// Set the guard XFG check function pointer VA
    pub fn set_guard_xfg_check_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_xfg_check_function_pointer(value);
    }

    /// Set the guard XFG dispatch function pointer VA
    pub fn set_guard_xfg_dispatch_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_xfg_dispatch_function_pointer(value);
    }

    /// Set the guard XFG table dispatch function pointer VA
    pub fn set_guard_xfg_table_dispatch_function_pointer(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_guard_xfg_table_dispatch_function_pointer(value);
    }

    /// Set the CastGuard OS determined failure mode VA
    pub fn set_cast_guard_os_determined_failure_mode(&mut self, value: u64) {
        self.ptr
            .pin_mut()
            .set_cast_guard_os_determined_failure_mode(value);
    }

    /// Set the guard memcpy function pointer VA
    pub fn set_guard_memcpy_function_pointer(&mut self, value: u64) {
        self.ptr.pin_mut().set_guard_memcpy_function_pointer(value);
    }

    /// Set the UMA function pointers VA
    pub fn set_uma_function_pointers(&mut self, value: u64) {
        self.ptr.pin_mut().set_uma_function_pointers(value);
    }
}

impl<'a> FromFFI<ffi::PE_LoadConfiguration> for LoadConfiguration<'a> {
    fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_LoadConfiguration>) -> Self {
        Self {
            ptr,
            _owner: PhantomData,
        }
    }
}

impl std::fmt::Debug for LoadConfiguration<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("LoadConfiguration")
            .field("size", &self.size())
            .field("timedatestamp", &self.timedatestamp())
            .field("major_version", &self.major_version())
            .field("minor_version", &self.minor_version())
            .field("global_flags_clear", &self.global_flags_clear())
            .field("global_flags_set", &self.global_flags_set())
            .field(
                "critical_section_default_timeout",
                &self.critical_section_default_timeout(),
            )
            .field(
                "decommit_free_block_threshold",
                &self.decommit_free_block_threshold(),
            )
            .field(
                "decommit_total_free_threshold",
                &self.decommit_total_free_threshold(),
            )
            .field("lock_prefix_table", &self.lock_prefix_table())
            .field("maximum_allocation_size", &self.maximum_allocation_size())
            .field("virtual_memory_threshold", &self.virtual_memory_threshold())
            .field("process_affinity_mask", &self.process_affinity_mask())
            .field("process_heap_flags", &self.process_heap_flags())
            .field("csd_version", &self.csd_version())
            .field("reserved1", &self.reserved1())
            .field("dependent_load_flags", &self.dependent_load_flags())
            .field("editlist", &self.editlist())
            .field("security_cookie", &self.security_cookie())
            .field("se_handler_table", &self.se_handler_table())
            .field("se_handler_count", &self.se_handler_count())
            .field(
                "guard_cf_check_function_pointer",
                &self.guard_cf_check_function_pointer(),
            )
            .field(
                "guard_cf_dispatch_function_pointer",
                &self.guard_cf_dispatch_function_pointer(),
            )
            .field("guard_cf_function_table", &self.guard_cf_function_table())
            .field("guard_cf_function_count", &self.guard_cf_function_count())
            .field("guard_flags", &self.guard_flags())
            .field(
                "guard_address_taken_iat_entry_table",
                &self.guard_address_taken_iat_entry_table(),
            )
            .field(
                "guard_address_taken_iat_entry_count",
                &self.guard_address_taken_iat_entry_count(),
            )
            .field(
                "guard_long_jump_target_table",
                &self.guard_long_jump_target_table(),
            )
            .field(
                "guard_long_jump_target_count",
                &self.guard_long_jump_target_count(),
            )
            .field(
                "dynamic_value_reloc_table",
                &self.dynamic_value_reloc_table(),
            )
            .field("hybrid_metadata_pointer", &self.hybrid_metadata_pointer())
            .field("chpe_metadata_pointer", &self.chpe_metadata_pointer())
            .field("guard_rf_failure_routine", &self.guard_rf_failure_routine())
            .field(
                "guard_rf_failure_routine_function_pointer",
                &self.guard_rf_failure_routine_function_pointer(),
            )
            .field(
                "dynamic_value_reloctable_offset",
                &self.dynamic_value_reloctable_offset(),
            )
            .field(
                "dynamic_value_reloctable_section",
                &self.dynamic_value_reloctable_section(),
            )
            .field("reserved2", &self.reserved2())
            .field(
                "guard_rf_verify_stackpointer_function_pointer",
                &self.guard_rf_verify_stackpointer_function_pointer(),
            )
            .field("hotpatch_table_offset", &self.hotpatch_table_offset())
            .field("reserved3", &self.reserved3())
            .field(
                "enclave_configuration_ptr",
                &self.enclave_configuration_ptr(),
            )
            .field(
                "volatile_metadata_pointer",
                &self.volatile_metadata_pointer(),
            )
            .field(
                "guard_eh_continuation_table",
                &self.guard_eh_continuation_table(),
            )
            .field(
                "guard_eh_continuation_count",
                &self.guard_eh_continuation_count(),
            )
            .field(
                "guard_xfg_check_function_pointer",
                &self.guard_xfg_check_function_pointer(),
            )
            .field(
                "guard_xfg_dispatch_function_pointer",
                &self.guard_xfg_dispatch_function_pointer(),
            )
            .field(
                "guard_xfg_table_dispatch_function_pointer",
                &self.guard_xfg_table_dispatch_function_pointer(),
            )
            .field(
                "cast_guard_os_determined_failure_mode",
                &self.cast_guard_os_determined_failure_mode(),
            )
            .field(
                "guard_memcpy_function_pointer",
                &self.guard_memcpy_function_pointer(),
            )
            .field("uma_function_pointers", &self.uma_function_pointers())
            .finish()
    }
}

pub struct GuardFunction<'a> {
    ptr: cxx::UniquePtr<ffi::PE_LoadConfiguration_guard_function_t>,
    _owner: PhantomData<&'a ffi::PE_LoadConfiguration>,
}

impl<'a> FromFFI<ffi::PE_LoadConfiguration_guard_function_t> for GuardFunction<'a> {
    fn from_ffi(ptr: cxx::UniquePtr<ffi::PE_LoadConfiguration_guard_function_t>) -> Self {
        Self {
            ptr,
            _owner: PhantomData,
        }
    }
}

impl GuardFunction<'_> {
    /// RVA of the function
    pub fn rva(&self) -> u32 {
        self.ptr.rva()
    }

    /// Additional information whose meaning is not officially documented
    pub fn extra(&self) -> u32 {
        self.ptr.extra()
    }
}

impl std::fmt::Debug for GuardFunction<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("GuardFunction")
            .field("rva", &self.rva())
            .field("extra", &self.extra())
            .finish()
    }
}

bitflags! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
    pub struct ImageGuardFlags: u32 {
        const NONE = 0x0;

        /// Module performs control flow integrity checks using system-supplied
        /// support.
        const CF_INSTRUMENTED = 0x100;

        /// Module performs control flow and write integrity checks.
        const CFW_INSTRUMENTED = 0x200;

        /// Module contains valid control flow target metadata.
        const CF_FUNCTION_TABLE_PRESENT = 0x400;

        /// Module does not make use of the /GS security cookie.
        const SECURITY_COOKIE_UNUSED = 0x800;

        /// Module supports read only delay load IAT.
        const PROTECT_DELAYLOAD_IAT = 0x1000;

        /// Delayload import table in its own .didat section (with nothing else in it)
        /// that can be freely reprotected.
        const DELAYLOAD_IAT_IN_ITS_OWN_SECTION = 0x2000;

        /// Module contains suppressed export information. This also infers that the
        /// address taken IAT table is also present in the load config.
        const CF_EXPORT_SUPPRESSION_INFO_PRESENT = 0x4000;

        /// Module enables suppression of exports.
        const CF_ENABLE_EXPORT_SUPPRESSION = 0x8000;

        /// Module contains longjmp target information.
        const CF_LONGJUMP_TABLE_PRESENT = 0x10000;

        /// Module contains return flow instrumentation and metadata
        const RF_INSTRUMENTED = 0x20000;

        /// Module requests that the OS enable return flow protection
        const RF_ENABLE = 0x40000;

        /// Module requests that the OS enable return flow protection in strict mode
        const RF_STRICT = 0x80000;

        /// Module was built with retpoline support
        const RETPOLINE_PRESENT = 0x100000;

        /// Module contains EH continuation target information.
        const EH_CONTINUATION_TABLE_PRESENT = 0x200000;

        /// Module was built with xfg (deprecated)
        const XFG_ENABLED = 0x00800000;

        /// Module has CastGuard instrumentation present
        const CASTGUARD_PRESENT = 0x01000000;

        /// Module has Guarded Memcpy instrumentation present
        const MEMCPY_PRESENT = 0x02000000;
    }
}

impl From<u32> for ImageGuardFlags {
    fn from(value: u32) -> Self {
        ImageGuardFlags::from_bits_truncate(value)
    }
}
impl From<ImageGuardFlags> for u32 {
    fn from(value: ImageGuardFlags) -> Self {
        value.bits()
    }
}
impl std::fmt::Display for ImageGuardFlags {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        bitflags::parser::to_writer(self, f)
    }
}

#[derive(Debug)]
pub enum CHPEMetadata<'a> {
    ARM64(chpe_metadata_arm64::CHPEMetadata<'a>),
    X86(chpe_metadata_x86::CHPEMetadata<'a>),
}

impl<'a> FromFFI<ffi::PE_CHPEMetadata> for CHPEMetadata<'a> {
    fn from_ffi(ffi_entry: cxx::UniquePtr<ffi::PE_CHPEMetadata>) -> Self {
        unsafe {
            let obj_ref = ffi_entry.as_ref().unwrap();
            if ffi::PE_CHPEMetadataARM64::classof(obj_ref) {
                let raw = {
                    type From = cxx::UniquePtr<ffi::PE_CHPEMetadata>;
                    type To = cxx::UniquePtr<ffi::PE_CHPEMetadataARM64>;
                    std::mem::transmute::<From, To>(ffi_entry)
                };
                CHPEMetadata::ARM64(chpe_metadata_arm64::CHPEMetadata::from_ffi(raw))
            } else if ffi::PE_CHPEMetadataX86::classof(obj_ref) {
                let raw = {
                    type From = cxx::UniquePtr<ffi::PE_CHPEMetadata>;
                    type To = cxx::UniquePtr<ffi::PE_CHPEMetadataX86>;
                    std::mem::transmute::<From, To>(ffi_entry)
                };
                CHPEMetadata::X86(chpe_metadata_x86::CHPEMetadata::from_ffi(raw))
            } else {
                panic!("unsupported architecture");
            }
        }
    }
}

/// Trait shared by all architecture-specific CHPEMetadata
pub trait AsCHPEMetadata {
    #[doc(hidden)]
    fn as_generic(&self) -> &ffi::PE_CHPEMetadata;

    /// Version of the structure
    fn version(&self) -> u32 {
        self.as_generic().version()
    }
}

impl std::fmt::Display for &dyn AsCHPEMetadata {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.as_generic().to_string())
    }
}

impl AsCHPEMetadata for CHPEMetadata<'_> {
    fn as_generic(&self) -> &ffi::PE_CHPEMetadata {
        match &self {
            CHPEMetadata::ARM64(entry) => entry.as_generic(),

            CHPEMetadata::X86(entry) => entry.as_generic(),
        }
    }
}

declare_iterator!(
    GuardCFFunctions,
    GuardFunction<'a>,
    ffi::PE_LoadConfiguration_guard_function_t,
    ffi::PE_LoadConfiguration,
    ffi::PE_LoadConfiguration_it_guard_cf_functions
);

declare_iterator!(
    GuardAddressTakenIATEntries,
    GuardFunction<'a>,
    ffi::PE_LoadConfiguration_guard_function_t,
    ffi::PE_LoadConfiguration,
    ffi::PE_LoadConfiguration_it_guard_address_taken_iat_entries
);

declare_iterator!(
    GuardLongJumpTargets,
    GuardFunction<'a>,
    ffi::PE_LoadConfiguration_guard_function_t,
    ffi::PE_LoadConfiguration,
    ffi::PE_LoadConfiguration_it_guard_long_jump_targets
);

declare_iterator!(
    GuardEhContinuationFunctions,
    GuardFunction<'a>,
    ffi::PE_LoadConfiguration_guard_function_t,
    ffi::PE_LoadConfiguration,
    ffi::PE_LoadConfiguration_it_guard_eh_continuation
);

declare_iterator!(
    DynamicRelocations,
    DynamicRelocation<'a>,
    ffi::PE_DynamicRelocation,
    ffi::PE_LoadConfiguration,
    ffi::PE_LoadConfiguration_it_dynamic_relocations
);