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
//! # pstruct
//!
//! A Rust procedural macro for generating pointer struct implementations with field offset access. The purpose of this crate is to minimize stack space, preventing struct copies<sup>1</sup>, while still allowing ergonomic field access. This macro abstracts away a lot of the pain of interacting with pointers to structs, such as casting, pointer arithmetic, transmutation, etc.
//!
//! A big inspiration for need for this crate was minimizing stack space for functions which use WinAPI structs, as they are often massive in size.
//!
//!
//! ## Features
//!
//! - Generate pointer structs with field offset access methods
//! - Support for arrays of pointers with indexing
//! - Pointer reinterpretation capabilities
//! - Safe and ergonomic field access
//!
//! ## Attributes
//!
//! ### `offset`
//!
//! The main attribute for specifying field offsets and behavior:
//!
//! - Basic usage: `#[offset(0x1)]` - Specifies the offset from base address
//! - Reinterpret: `#[offset(0x1, reinterpret)]` - Reinterprets the pointer at the given offset as another pointer type.
//! - Array: `#[offset(0x1, array(size))]` - Defines an array of pointers and generates a getter method for indexing into the array.
//!
//! ## Safety
//!
//! This crate involves heavy unsafe operations when accessing fields. Consumers of this crate are responsible for:
//! - Ensuring correct memory layout, alignment, bounds, etc.
//! - Ensuring offsets are valid for the data structure and all memory is readable.
//! - Proper lifetime management when constructing a PStruct from a raw pointer. *Lifetimes are enforced for you when creating a PStruct from a &[T]*.
//! - Bounds checking when accessing array fields.
//!
//! ## License
//!
//! Licensed under either of
//!
//! * Apache License, Version 2.0
//! ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
//! * MIT license
//! ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
//!
//! at your option.
//!
//! ## Contribution
//!
//! Unless you explicitly state otherwise, any contribution intentionally submitted
//! for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
//! dual licensed as above, without any additional terms or conditions.
//!
//! ## Notes
//! <sup>1</sup> You could always use references to prevent the copy, but this still doesn't solve the problem of manually defining structs with padding bytes which gets very tedious and is very common for those working with WinAPI (PEB, TEB, etc).
extern crate proc_macro;
use *;
use FromAttr;
use TokenStream;
/// Generates a pointer struct from a struct definition.
///
/// The original struct definition is discarded, while a new struct is generated with a `P` prefix.
/// This new struct contains methods for reading the fields of the pointer struct.
/// Clone and Copy derives are added if they don't exist, and the generated type is `#[repr(transparent)]` to allow for transmutation from pointers.
///
/// ## Offset Attribute
///
/// The `offset` attribute is used to specify the offset of the field from the base address.
/// This offset is added to the base address to return the field.
///
/// **Default behavior for generated offset getters is to use [read_unaligned][core::ptr::read_unaligned] to read the field, copying it's value and returning it from the function.**
///
/// ### Sub attributes:
/// - `reinterpret`: Overrides default behavior, reinterprets the returned pointer from base address + offset as a different pointer type.
/// - `array`: Allows you to define the field as an array of pointers. This generates a method which allows for indexing into the array.
///
///
/// # Example
///
/// ```rust
/// use pstruct::p_struct;
///
/// // Define a byte array to simulate struct data.
/// let byte_array: &[u8] = &[
/// 69, // `field_b` (offset 0)
/// 255, 255, 255, 255, // `field_a` (offset 1)
/// 5, 0, 10, 0, 15, 0, 20, 0, 25, 0, // `field_d` (offset 5)
/// 40, // `field_c` (offset 0xF)
/// 1, 2, 3, 4 // Field `field_e` (offset 0x10)
/// ];
///
/// // Define a pointer struct using the macro.
/// p_struct! {
/// pub struct Example {
/// #[offset(0x1)]
/// field_a: u32,
/// #[offset(0x0)]
/// field_b: u8,
/// #[offset(0xF, reinterpret)]
/// // Reinterprets the pointer from base + 0xF (via transmute) as a *const u8
/// // Use this feature to return pointers to a value rather than the value itself.
/// field_c: *const u8,
/// #[offset(0x5, array(5, size_t = 2))]
/// // Defines the field as an array of pointers, allowing for indexing into the array.
/// // This is useful for returning arrays of pointers.
/// field_d: *const u16,
/// // This is an array of 4 u8s, but the size of each element is determined by the size_fn attribute.
/// #[offset(0x10, array(4, size_fn = "core::mem::size_of::<u8>()"))]
/// field_e: *const u8,
/// }
/// }
///
/// let example_ptr = PExample::from(byte_array);
/// assert_eq!(unsafe { example_ptr.field_b() }, 69);
/// assert_eq!(unsafe { example_ptr.field_a() }, u32::MAX);
///
/// let field_d_1 = unsafe { example_ptr.get_field_d(0).unwrap() };
/// let field_d_2 = unsafe { example_ptr.get_field_d(1).unwrap() };
/// assert_eq!(unsafe { *field_d_1 }, 5u16);
/// assert_eq!(unsafe { *field_d_2 }, 10u16);
/// let reconstruct_array = unsafe { core::slice::from_raw_parts(example_ptr.field_d(), 5) };
/// assert_eq!(reconstruct_array, [5, 10, 15, 20, 25]);
/// assert_eq!(unsafe { *(example_ptr.field_c().as_ref().unwrap()) }, 40u8);
/// assert_eq!(unsafe { *example_ptr.get_field_e(0).unwrap() }, 1u8);
/// assert_eq!(unsafe { *example_ptr.get_field_e(1).unwrap() }, 2u8);
///
///
/// ```