-
Notifications
You must be signed in to change notification settings - Fork 0
/
MasmFormatter.rs
215 lines (183 loc) · 6.94 KB
/
MasmFormatter.rs
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
/*
Iced (Dis)Assembler
C-Compatible Exports
TetzkatLipHoka 2022-2024
*/
use iced_x86::{Instruction, Formatter, MasmFormatter};
use crate::SymbolResolver::{TSymbolResolver, TSymbolResolverCallback};
use crate::OptionsProvider::{TFormatterOptionsProvider, TFormatterOptionsProviderCallback};
use crate::OutputCallback::TFormatterOutput;
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Masm-Formatter
pub(crate) struct TMasmFormatter {
pub Formatter : MasmFormatter,
pub Output : String,
}
// Creates a Masm formatter
//
// # Arguments
// - `symbol_resolver`: Symbol resolver or `None`
// - `options_provider`: Operand options provider or `None`
#[no_mangle]
pub extern "C" fn MasmFormatter_Create( SymbolResolver : Option<TSymbolResolverCallback>, OptionsProvider : Option<TFormatterOptionsProviderCallback>, UserData : *const usize ) -> *mut TMasmFormatter {
if !SymbolResolver.is_none() && !OptionsProvider.is_none() {
let symbols = Box::new( TSymbolResolver { callback:SymbolResolver, userData:UserData });
let options = Box::new( TFormatterOptionsProvider { callback:OptionsProvider, userData:UserData });
Box::into_raw( Box::new( TMasmFormatter { Formatter: MasmFormatter::with_options( Some( symbols ), Some( options ) ), Output: String::new() } ) )
} else if !SymbolResolver.is_none() {
let symbols = Box::new( TSymbolResolver { callback:SymbolResolver, userData:UserData });
Box::into_raw( Box::new( TMasmFormatter { Formatter: MasmFormatter::with_options( Some( symbols ), None ), Output: String::new() } ) )
} else if !OptionsProvider.is_none() {
let options = Box::new( TFormatterOptionsProvider { callback:OptionsProvider, userData:UserData });
Box::into_raw( Box::new( TMasmFormatter { Formatter: MasmFormatter::with_options( None, Some( options ) ), Output: String::new() } ) )
} else {
Box::into_raw( Box::new( TMasmFormatter { Formatter: MasmFormatter::with_options( None, None ), Output: String::new() } ) )
}
}
// Format Instruction
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_Format( Formatter: *mut TMasmFormatter, Instruction: *mut Instruction, Output : *mut *const u8, Size : *mut usize ) {
if Formatter.is_null() {
return;
}
if Instruction.is_null() {
return;
}
if Output.is_null() {
return;
}
if Size.is_null() {
return;
}
let mut obj = Box::from_raw( Formatter );
obj.Output.clear();
obj.Formatter.format( Instruction.as_mut().unwrap(), &mut obj.Output );
let newsize = obj.Output.len()+1;
obj.Output.as_mut_vec().resize( newsize, 0 );
(*Output) = obj.Output.as_ptr();
(*Size) = obj.Output.len();
Box::into_raw( obj );
}
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_FormatCallback( Formatter: *mut TMasmFormatter, Instruction: *mut Instruction, FormatterOutput : *mut TFormatterOutput ) {
if Formatter.is_null() {
return;
}
if Instruction.is_null() {
return;
}
if FormatterOutput.is_null() {
return;
}
let mut obj = Box::from_raw( Formatter );
let mut output = Box::from_raw( FormatterOutput );
obj.Formatter.format( Instruction.as_mut().unwrap(), output.as_mut() );
Box::into_raw( output );
Box::into_raw( obj );
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// ( masm only ): Add a `DS` segment override even if it's not present. Used if it's 16/32-bit code and mem op is a displ
//
// Default | Value | Example
// --------|-------|--------
// 👍 | `true` | `mov eax,ds:[ 12345678 ]`
// _ | `false` | `mov eax,[ 12345678 ]`
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_GetAddDsPrefix32( Formatter: *mut TMasmFormatter ) -> bool {
if Formatter.is_null() {
return false;
}
let mut obj = Box::from_raw( Formatter );
let value = obj.Formatter.options_mut().masm_add_ds_prefix32();
Box::into_raw( obj );
return value;
}
// ( masm only ): Add a `DS` segment override even if it's not present. Used if it's 16/32-bit code and mem op is a displ
//
// Default | Value | Example
// --------|-------|--------
// 👍 | `true` | `mov eax,ds:[ 12345678 ]`
// _ | `false` | `mov eax,[ 12345678 ]`
//
// # Arguments
// * `value`: New value
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_SetAddDsPrefix32( Formatter: *mut TMasmFormatter, Value : bool ) -> bool {
if Formatter.is_null() {
return false;
}
let mut obj = Box::from_raw( Formatter );
obj.Formatter.options_mut().set_masm_add_ds_prefix32( Value );
Box::into_raw( obj );
return true;
}
// ( masm only ): Show symbols in brackets
//
// Default | Value | Example
// --------|-------|--------
// 👍 | `true` | `[ ecx+symbol ]` / `[ symbol ]`
// _ | `false` | `symbol[ ecx ]` / `symbol`
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_GetSymbolDisplacementInBrackets( Formatter: *mut TMasmFormatter ) -> bool {
if Formatter.is_null() {
return false;
}
let mut obj = Box::from_raw( Formatter );
let value = obj.Formatter.options_mut().masm_symbol_displ_in_brackets();
Box::into_raw( obj );
return value;
}
// ( masm only ): Show symbols in brackets
//
// Default | Value | Example
// --------|-------|--------
// 👍 | `true` | `[ ecx+symbol ]` / `[ symbol ]`
// _ | `false` | `symbol[ ecx ]` / `symbol`
//
// # Arguments
// * `value`: New value
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_SetSymbolDisplacementInBrackets( Formatter: *mut TMasmFormatter, Value : bool ) -> bool {
if Formatter.is_null() {
return false;
}
let mut obj = Box::from_raw( Formatter );
obj.Formatter.options_mut().set_masm_symbol_displ_in_brackets( Value );
Box::into_raw( obj );
return true;
}
// ( masm only ): Show displacements in brackets
//
// Default | Value | Example
// --------|-------|--------
// 👍 | `true` | `[ ecx+1234h ]`
// _ | `false` | `1234h[ ecx ]`
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_GetDisplacementInBrackets( Formatter: *mut TMasmFormatter ) -> bool {
if Formatter.is_null() {
return false;
}
let mut obj = Box::from_raw( Formatter );
let value = obj.Formatter.options_mut().masm_displ_in_brackets();
Box::into_raw( obj );
return value;
}
// ( masm only ): Show displacements in brackets
//
// Default | Value | Example
// --------|-------|--------
// 👍 | `true` | `[ ecx+1234h ]`
// _ | `false` | `1234h[ ecx ]`
//
// # Arguments
// * `value`: New value
#[no_mangle]
pub unsafe extern "C" fn MasmFormatter_SetDisplacementInBrackets( Formatter: *mut TMasmFormatter, Value : bool ) -> bool {
if Formatter.is_null() {
return false;
}
let mut obj = Box::from_raw( Formatter );
obj.Formatter.options_mut().set_masm_displ_in_brackets( Value );
Box::into_raw( obj );
return true;
}