From abd7f75c4955197ae79e8226782e9826d8fbc507 Mon Sep 17 00:00:00 2001 From: iximeow Date: Sat, 19 May 2018 16:35:38 -0700 Subject: add feature to seek to dword under cursor --- main.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/main.rs b/main.rs index 0578d0f..18a3504 100644 --- a/main.rs +++ b/main.rs @@ -789,6 +789,38 @@ fn interface_loop(state: &mut Program) { state.status = format!("ctrl? {:?}", vec); } } + Event::Key(Key::Char('g')) => { + // follow (dword) + if state.view.size() < 4 { + state.status = "not enough bytes to seek a dword (need at least four)".to_string(); + } else { + let read_from = if state.view.size() - state.cursor < 4 { + state.view.size() - 4 + } else { + state.cursor + }; + + let dword_bytes = state.view.get_bytes(state.cursor, 4); + if dword_bytes.len() == 4 { + let dword: u64 = + ((dword_bytes[0] as u32) | + ((dword_bytes[1] as u32) << 8) | + ((dword_bytes[2] as u32) << 16) | + ((dword_bytes[3] as u32) << 24)) as u64; + if dword < state.view.size() { + state.status = format!("Seeked to 0x{:x}", dword); + state.seek_to(dword as u64); + } else { + state.status = format!("Cannot seek to 0x{:x} - invalid address.", dword); + } + } else { + state.status = format!( + "not enough bytes to seek a dword (got {}) - should be unreachable since we already checked for this", + dword_bytes.len() + ); + } + } + } Event::Key(Key::Char(x)) => { // TODO: how does this work on non-US keyboards? keyboards without a-fA-F? let new_value = state.current_edit_view().apply_sym(state.view.get_byte(state.cursor), x as u8); -- cgit v1.1