diff options
| author | KyleMiles <krm504@nyu.edu> | 2022-08-29 12:25:14 -0400 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2022-08-29 12:49:20 -0400 |
| commit | 3d1aaeb4d5e72ba6adcaa9eb24429879f5ad3250 (patch) | |
| tree | 6a81aed92dbc49aa14a370b962f15c0a34c967dd /rust | |
| parent | 69f7caca76cf05cfd5d3e48c6cd950aed20d7f97 (diff) | |
Rust API Examples : Fix uses of `InstructionTextToken::new`
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/examples/dwarfdump/src/lib.rs | 117 | ||||
| -rw-r--r-- | rust/examples/flowgraph/src/lib.rs | 9 |
2 files changed, 61 insertions, 65 deletions
diff --git a/rust/examples/dwarfdump/src/lib.rs b/rust/examples/dwarfdump/src/lib.rs index 7ecef114..d11ab532 100644 --- a/rust/examples/dwarfdump/src/lib.rs +++ b/rust/examples/dwarfdump/src/lib.rs @@ -1,9 +1,24 @@ +// Copyright 2021-2022 Vector 35 Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + use binaryninja::{ binaryview::{BinaryView, BinaryViewExt}, command::{register, Command}, databuffer::DataBuffer, - disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenType}, + disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenContents}, flowgraph::{BranchType, EdgeStyle, FlowGraph, FlowGraphNode, FlowGraphOption}, + string::BnString, }; use gimli::{ @@ -108,91 +123,79 @@ fn get_info_string<R: Reader>( let label_string = format!("#0x{:08x}", label_value); disassembly_lines.push(DisassemblyTextLine::from(vec![ InstructionTextToken::new( - InstructionTextTokenType::GotoLabelToken, - label_string.as_ref(), - label_value, + BnString::new(label_string), + InstructionTextTokenContents::GotoLabel(label_value), ), - InstructionTextToken::new(InstructionTextTokenType::TextToken, ":", 0), + InstructionTextToken::new(BnString::new(":"), InstructionTextTokenContents::Text), ])); disassembly_lines.push(DisassemblyTextLine::from(vec![InstructionTextToken::new( - InstructionTextTokenType::TypeNameToken, // TODO : KeywordToken? - die_node.tag().static_string().unwrap(), - 0, + BnString::new(die_node.tag().static_string().unwrap()), + InstructionTextTokenContents::TypeName, // TODO : KeywordToken? )])); let mut attrs = die_node.attrs(); while let Some(attr) = attrs.next().unwrap() { let mut attr_line: Vec<InstructionTextToken> = Vec::with_capacity(5); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IndentationToken, - &" ", - 0, + BnString::new(" "), + InstructionTextTokenContents::Indentation, )); let len; if let Some(n) = attr.name().static_string() { len = n.len(); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::FieldNameToken, - n, - 0, + BnString::new(n), + InstructionTextTokenContents::FieldName, )); } else { // This is rather unlikely, I think len = 1; attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::FieldNameToken, - &"?", - 0, + BnString::new("?"), + InstructionTextTokenContents::FieldName, )); } // On command line the magic number that looks good is 22, but that's too much whitespace in a basic block, so I chose 18 (22 is the max with the current padding provided) if len < 18 { attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::TextToken, - PADDING[18 - len], - 0, + BnString::new(PADDING[18 - len]), + InstructionTextTokenContents::Text, )); } attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::TextToken, - &" = ", - 0, + BnString::new(" = "), + InstructionTextTokenContents::Text, )); if let Ok(Some(addr)) = dwarf.attr_address(&unit, attr.value()) { let addr_string = format!("0x{:08x}", addr); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - addr_string.as_ref(), - addr, + BnString::new(addr_string), + InstructionTextTokenContents::Integer(addr), )); } else if let Ok(attr_reader) = dwarf.attr_string(&unit, attr.value()) { if let Ok(attr_string) = attr_reader.to_string() { attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::StringToken, - attr_string.as_ref(), - { - // TODO : name() might need to become dwo_name + BnString::new(attr_string.as_ref()), + InstructionTextTokenContents::String({ let (_, id, offset) = dwarf.lookup_offset_id(attr_reader.offset_id()).unwrap(); offset.into_u64() + view.section_by_name(id.name()).unwrap().start() - }, + }), )); } else { attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::TextToken, - &"??", - 0, + BnString::new("??"), + InstructionTextTokenContents::Text, )); } } else if let Encoding(type_class) = attr.value() { attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::TypeNameToken, - type_class.static_string().unwrap(), - 0, + BnString::new(type_class.static_string().unwrap()), + InstructionTextTokenContents::TypeName, )); } else if let UnitRef(offset) = attr.value() { let addr = match offset.to_unit_section_offset(unit) { @@ -202,58 +205,50 @@ fn get_info_string<R: Reader>( .into_u64(); let addr_string = format!("#0x{:08x}", addr); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::GotoLabelToken, - addr_string.as_ref(), - addr, + BnString::new(addr_string), + InstructionTextTokenContents::GotoLabel(addr), )); } else if let Flag(true) = attr.value() { attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - &"true", - 1, + BnString::new("true"), + InstructionTextTokenContents::Integer(1), )); } else if let Flag(false) = attr.value() { attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - &"false", - 1, + BnString::new("false"), + InstructionTextTokenContents::Integer(1), )); // Fall-back cases } else if let Some(value) = attr.u8_value() { let value_string = format!("{}", value); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - value_string.as_ref(), - value.into(), + BnString::new(value_string), + InstructionTextTokenContents::Integer(value.into()), )); } else if let Some(value) = attr.u16_value() { let value_string = format!("{}", value); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - value_string.as_ref(), - value.into(), + BnString::new(value_string), + InstructionTextTokenContents::Integer(value.into()), )); } else if let Some(value) = attr.udata_value() { let value_string = format!("{}", value); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - value_string.as_ref(), - value.into(), + BnString::new(value_string), + InstructionTextTokenContents::Integer(value.into()), )); } else if let Some(value) = attr.sdata_value() { let value_string = format!("{}", value); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::IntegerToken, - value_string.as_ref(), - value as u64, + BnString::new(value_string), + InstructionTextTokenContents::Integer(value as u64), )); } else { let attr_string = format!("{:?}", attr.value()); attr_line.push(InstructionTextToken::new( - InstructionTextTokenType::TextToken, - attr_string.as_ref(), - 0, + BnString::new(attr_string), + InstructionTextTokenContents::Text, )); } disassembly_lines.push(DisassemblyTextLine::from(attr_line)); diff --git a/rust/examples/flowgraph/src/lib.rs b/rust/examples/flowgraph/src/lib.rs index ac586922..f80879e5 100644 --- a/rust/examples/flowgraph/src/lib.rs +++ b/rust/examples/flowgraph/src/lib.rs @@ -1,17 +1,18 @@ use binaryninja::{ binaryview::{BinaryView, BinaryViewExt}, command::register, - disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenType}, + disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenContents}, flowgraph::{BranchType, EdgePenStyle, EdgeStyle, FlowGraph, FlowGraphNode, ThemeColor}, + string::BnString, }; fn test_graph(view: &BinaryView) { let graph = FlowGraph::new(); let disassembly_lines_a = vec![DisassemblyTextLine::from(vec![ - InstructionTextToken::new(InstructionTextTokenType::TextToken, "Li", 0), - InstructionTextToken::new(InstructionTextTokenType::TextToken, "ne", 0), - InstructionTextToken::new(InstructionTextTokenType::TextToken, " 1", 0), + InstructionTextToken::new(BnString::new("Li"), InstructionTextTokenContents::Text), + InstructionTextToken::new(BnString::new("ne"), InstructionTextTokenContents::Text), + InstructionTextToken::new(BnString::new(" 1"), InstructionTextTokenContents::Text), ])]; let node_a = FlowGraphNode::new(&graph); |
