binaryninja/high_level_il/
token_emitter.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
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
use std::ptr::NonNull;

use binaryninjacore_sys::*;

use crate::disassembly::{
    DisassemblySettings, DisassemblyTextLine, InstructionTextToken, InstructionTextTokenContext,
    InstructionTextTokenType,
};
use crate::high_level_il::HighLevelILFunction;
use crate::language_representation::{OperatorPrecedence, SymbolDisplayResult, SymbolDisplayType};
use crate::rc::{Array, Ref, RefCountable};
use crate::variable::Variable;

pub type ScopeType = BNScopeType;
pub type TokenEmitterExpr = BNTokenEmitterExpr;
pub type BraceRequirement = BNBraceRequirement;

#[derive(PartialEq, Eq, Hash)]
pub struct HighLevelILTokenEmitter {
    handle: NonNull<BNHighLevelILTokenEmitter>,
}

impl HighLevelILTokenEmitter {
    pub(crate) unsafe fn from_raw(handle: NonNull<BNHighLevelILTokenEmitter>) -> Self {
        Self { handle }
    }

    /// Returns the list of [`InstructionTextToken`] on the current line.
    pub fn current_tokens(&self) -> Array<InstructionTextToken> {
        let mut count = 0;
        let array =
            unsafe { BNHighLevelILTokenEmitterGetCurrentTokens(self.handle.as_ptr(), &mut count) };
        unsafe { Array::new(array, count, ()) }
    }

    /// Returns the list of [`DisassemblyTextLine`] in the output.
    pub fn lines(&self) -> Array<DisassemblyTextLine> {
        let mut count = 0;
        let array = unsafe { BNHighLevelILTokenEmitterGetLines(self.handle.as_ptr(), &mut count) };
        unsafe { Array::new(array, count, ()) }
    }

    pub fn prepend_collapse_blank_indicator(&self) {
        unsafe { BNHighLevelILTokenPrependCollapseBlankIndicator(self.handle.as_ptr()) };
    }

    pub fn prepend_collapse_indicator(&self, context: InstructionTextTokenContext, hash: u64) {
        unsafe {
            BNHighLevelILTokenPrependCollapseIndicator(self.handle.as_ptr(), context.into(), hash)
        };
    }

    pub fn has_collapsible_regions(&self) -> bool {
        unsafe { BNHighLevelILTokenEmitterHasCollapsableRegions(self.handle.as_ptr()) }
    }

    pub fn set_has_collapsible_regions(&self, state: bool) {
        unsafe { BNHighLevelILTokenEmitterSetHasCollapsableRegions(self.handle.as_ptr(), state) };
    }

    pub fn append(&self, token: InstructionTextToken) {
        let mut raw_token = InstructionTextToken::into_raw(token);
        unsafe { BNHighLevelILTokenEmitterAppend(self.handle.as_ptr(), &mut raw_token) };
        InstructionTextToken::free_raw(raw_token);
    }

    /// Starts a new line in the output.
    pub fn init_line(&self) {
        unsafe { BNHighLevelILTokenEmitterInitLine(self.handle.as_ptr()) };
    }

    // TODO: Difference from `init_line`?
    /// Starts a new line in the output.
    pub fn new_line(&self) {
        unsafe { BNHighLevelILTokenEmitterNewLine(self.handle.as_ptr()) };
    }

    /// Increases the indentation level by one.
    pub fn increase_indent(&self) {
        unsafe { BNHighLevelILTokenEmitterIncreaseIndent(self.handle.as_ptr()) };
    }

    /// Decreases the indentation level by one.
    pub fn decrease_indent(&self) {
        unsafe { BNHighLevelILTokenEmitterDecreaseIndent(self.handle.as_ptr()) };
    }

    /// Indicates that visual separation of scopes is desirable at the current position.
    ///
    /// By default, this will insert a blank line, but this can be configured by the user.
    pub fn scope_separator(&self) {
        unsafe { BNHighLevelILTokenEmitterScopeSeparator(self.handle.as_ptr()) };
    }

    /// Begins a new scope. Insertion of newlines and braces will be handled using the current settings.
    pub fn begin_scope(&self, ty: ScopeType) {
        unsafe { BNHighLevelILTokenEmitterBeginScope(self.handle.as_ptr(), ty) };
    }

    /// Ends the current scope.
    ///
    /// The type `ty` should be equal to what was passed to [`HighLevelILTokenEmitter::begin_scope`].
    pub fn end_scope(&self, ty: ScopeType) {
        unsafe { BNHighLevelILTokenEmitterEndScope(self.handle.as_ptr(), ty) };
    }

    /// Continues the previous scope with a new associated scope. This is most commonly used for else statements.
    ///
    /// If `force_same_line` is true, the continuation will always be placed on the same line as the previous scope.
    pub fn scope_continuation(&self, force_same_line: bool) {
        unsafe {
            BNHighLevelILTokenEmitterScopeContinuation(self.handle.as_ptr(), force_same_line)
        };
    }

    /// Finalizes the previous scope, indicating that there are no more associated scopes.
    pub fn finalize_scope(&self) {
        unsafe { BNHighLevelILTokenEmitterFinalizeScope(self.handle.as_ptr()) };
    }

    /// Forces there to be no indentation for the next line.
    pub fn no_indent_for_this_line(&self) {
        unsafe { BNHighLevelILTokenEmitterNoIndentForThisLine(self.handle.as_ptr()) };
    }

    /// Begins a region of tokens that always have zero confidence.
    pub fn begin_force_zero_confidence(&self) {
        unsafe { BNHighLevelILTokenEmitterBeginForceZeroConfidence(self.handle.as_ptr()) };
    }

    /// Ends a region of tokens that always have zero confidence.
    pub fn end_force_zero_confidence(&self) {
        unsafe { BNHighLevelILTokenEmitterEndForceZeroConfidence(self.handle.as_ptr()) };
    }

    /// Sets the current expression. Returning the [`CurrentTokenEmitterExpr`] which when dropped
    /// will restore the previously active [`TokenEmitterExpr`].
    pub fn set_current_expr(&self, expr: TokenEmitterExpr) -> CurrentTokenEmitterExpr {
        let previous_expr =
            unsafe { BNHighLevelILTokenEmitterSetCurrentExpr(self.handle.as_ptr(), expr) };
        CurrentTokenEmitterExpr::new(self.to_owned(), expr, previous_expr)
    }

    fn restore_current_expr(&self, expr: TokenEmitterExpr) {
        unsafe { BNHighLevelILTokenEmitterRestoreCurrentExpr(self.handle.as_ptr(), expr) };
    }

    /// Finalizes the outputted lines.
    pub fn finalize(&self) {
        unsafe { BNHighLevelILTokenEmitterFinalize(self.handle.as_ptr()) };
    }

    /// Appends `(`.
    pub fn append_open_paren(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendOpenParen(self.handle.as_ptr()) };
    }

    /// Appends `)`.
    pub fn append_close_paren(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendCloseParen(self.handle.as_ptr()) };
    }

    /// Appends `[`.
    pub fn append_open_bracket(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendOpenBracket(self.handle.as_ptr()) };
    }

    /// Appends `]`.
    pub fn append_close_bracket(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendCloseBracket(self.handle.as_ptr()) };
    }

    /// Appends `{`.
    pub fn append_open_brace(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendOpenBrace(self.handle.as_ptr()) };
    }

    /// Appends `}`.
    pub fn append_close_brace(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendCloseBrace(self.handle.as_ptr()) };
    }

    /// Appends `;`.
    pub fn append_semicolon(&self) {
        unsafe { BNHighLevelILTokenEmitterAppendSemicolon(self.handle.as_ptr()) };
    }

    /// Sets the requirement for insertion of braces around scopes in the output.
    pub fn set_brace_requirement(&self, required: BraceRequirement) {
        unsafe { BNHighLevelILTokenEmitterSetBraceRequirement(self.handle.as_ptr(), required) };
    }

    /// Sets whether cases within switch statements should always have braces around them.
    pub fn set_braces_around_switch_cases(&self, braces: bool) {
        unsafe {
            BNHighLevelILTokenEmitterSetBracesAroundSwitchCases(self.handle.as_ptr(), braces)
        };
    }

    /// Sets whether braces should default to being on the same line as the statement that begins the scope.
    ///
    /// If the user has explicitly set a preference, this setting will be ignored and the user's preference will be used instead.
    pub fn set_default_braces_on_same_line(&self, same_line: bool) {
        unsafe {
            BNHighLevelILTokenEmitterSetDefaultBracesOnSameLine(self.handle.as_ptr(), same_line)
        };
    }

    /// Sets whether omitting braces around single-line scopes is allowed.
    pub fn set_simple_scope_allowed(&self, allowed: bool) {
        unsafe { BNHighLevelILTokenEmitterSetSimpleScopeAllowed(self.handle.as_ptr(), allowed) };
    }

    pub fn brace_requirement(&self) -> BraceRequirement {
        unsafe { BNHighLevelILTokenEmitterGetBraceRequirement(self.handle.as_ptr()) }
    }

    pub fn has_braces_around_switch_cases(&self) -> bool {
        unsafe { BNHighLevelILTokenEmitterHasBracesAroundSwitchCases(self.handle.as_ptr()) }
    }

    pub fn default_braces_on_same_line(&self) -> bool {
        unsafe { BNHighLevelILTokenEmitterGetDefaultBracesOnSameLine(self.handle.as_ptr()) }
    }

    pub fn is_simple_scope_allowed(&self) -> bool {
        unsafe { BNHighLevelILTokenEmitterIsSimpleScopeAllowed(self.handle.as_ptr()) }
    }

    /// Appends a size token for the given size in the High Level IL syntax.
    pub fn append_size_token(&self, size: usize, ty: InstructionTextTokenType) {
        unsafe { BNAddHighLevelILSizeToken(size, ty, self.handle.as_ptr()) }
    }

    /// Appends a floating point size token for the given size in the High Level IL syntax.
    pub fn append_float_size_token(&self, size: usize, ty: InstructionTextTokenType) {
        unsafe { BNAddHighLevelILFloatSizeToken(size, ty, self.handle.as_ptr()) }
    }

    /// Appends tokens for access to a variable.
    pub fn append_var_text_token(
        &self,
        func: &HighLevelILFunction,
        var: Variable,
        expr_index: usize,
        size: usize,
    ) {
        unsafe {
            BNAddHighLevelILVarTextToken(
                func.handle,
                &BNVariable::from(var),
                self.handle.as_ptr(),
                expr_index,
                size,
            )
        }
    }

    /// Appends tokens for a constant integer value.
    pub fn append_integer_text_token(
        &self,
        func: &HighLevelILFunction,
        expr_index: usize,
        val: i64,
        size: usize,
    ) {
        unsafe {
            BNAddHighLevelILIntegerTextToken(
                func.handle,
                expr_index,
                val,
                size,
                self.handle.as_ptr(),
            )
        }
    }

    /// Appends tokens for accessing an array by constant index.
    pub fn append_array_index_token(
        &self,
        func: &HighLevelILFunction,
        expr_index: usize,
        val: i64,
        size: usize,
        address: Option<u64>,
    ) {
        unsafe {
            BNAddHighLevelILArrayIndexToken(
                func.handle,
                expr_index,
                val,
                size,
                self.handle.as_ptr(),
                address.unwrap_or(0),
            )
        }
    }

    /// Appends tokens for displaying a constant pointer value.
    ///
    /// If `allow_short_string` is true, then a string will be shown even if it is "short".
    pub fn append_pointer_text_token(
        &self,
        func: &HighLevelILFunction,
        expr_index: usize,
        val: i64,
        settings: &DisassemblySettings,
        symbol_display: SymbolDisplayType,
        precedence: OperatorPrecedence,
        allow_short_string: bool,
    ) -> SymbolDisplayResult {
        unsafe {
            BNAddHighLevelILPointerTextToken(
                func.handle,
                expr_index,
                val,
                self.handle.as_ptr(),
                settings.handle,
                symbol_display,
                precedence,
                allow_short_string,
            )
        }
    }

    /// Appends tokens for a constant value.
    pub fn append_constant_text_token(
        &self,
        func: &HighLevelILFunction,
        expr_index: usize,
        val: i64,
        size: usize,
        settings: &DisassemblySettings,
        precedence: OperatorPrecedence,
    ) {
        unsafe {
            BNAddHighLevelILConstantTextToken(
                func.handle,
                expr_index,
                val,
                size,
                self.handle.as_ptr(),
                settings.handle,
                precedence,
            )
        }
    }
}

unsafe impl Send for HighLevelILTokenEmitter {}
unsafe impl Sync for HighLevelILTokenEmitter {}

unsafe impl RefCountable for HighLevelILTokenEmitter {
    unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
        let handle = BNNewHighLevelILTokenEmitterReference(handle.handle.as_ptr());
        let handle = NonNull::new(handle).unwrap();
        Ref::new(HighLevelILTokenEmitter { handle })
    }

    unsafe fn dec_ref(handle: &Self) {
        BNFreeHighLevelILTokenEmitter(handle.handle.as_ptr())
    }
}

impl ToOwned for HighLevelILTokenEmitter {
    type Owned = Ref<Self>;

    fn to_owned(&self) -> Self::Owned {
        unsafe { RefCountable::inc_ref(self) }
    }
}

/// Manages the currently active [`TokenEmitterExpr`] for the given [`HighLevelILTokenEmitter`].
///
/// When this object is destroyed, the previously active [`TokenEmitterExpr`] will become active again.
pub struct CurrentTokenEmitterExpr {
    pub emitter: Ref<HighLevelILTokenEmitter>,
    pub expr: TokenEmitterExpr,
    pub previous_expr: TokenEmitterExpr,
}

impl CurrentTokenEmitterExpr {
    pub fn new(
        emitter: Ref<HighLevelILTokenEmitter>,
        expr: TokenEmitterExpr,
        previous_expr: TokenEmitterExpr,
    ) -> Self {
        Self {
            emitter,
            expr,
            previous_expr,
        }
    }
}

impl Drop for CurrentTokenEmitterExpr {
    fn drop(&mut self) {
        self.emitter.restore_current_expr(self.previous_expr);
    }
}