From 1c58e2706c60e1b0b6f2ba70374d76946850e12c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Fri, 13 Apr 2018 12:13:57 +0200 Subject: [PATCH] Limit Hexdump Scrolling from 0 to RVA_MAX --- src/Cutter.h | 14 +++++++++- src/widgets/HexdumpWidget.cpp | 52 +++++++++++++++++++++++------------ 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/Cutter.h b/src/Cutter.h index 844edb52..ca03bbb2 100644 --- a/src/Cutter.h +++ b/src/Cutter.h @@ -29,8 +29,20 @@ #define Core() (CutterCore::getInstance()) +/*! + * \brief Type to be used for all kinds of addresses/offsets in r2 address space. + */ typedef ut64 RVA; -#define RVA_INVALID UT64_MAX + +/*! + * \brief Maximum value of RVA. Do NOT use this for specifying invalid values, use RVA_INVALID instead. + */ +#define RVA_MAX UT64_MAX + +/*! + * \brief Value for specifying an invalid RVA. + */ +#define RVA_INVALID RVA_MAX class RCoreLocked { diff --git a/src/widgets/HexdumpWidget.cpp b/src/widgets/HexdumpWidget.cpp index ead2dd08..87bfb8d6 100644 --- a/src/widgets/HexdumpWidget.cpp +++ b/src/widgets/HexdumpWidget.cpp @@ -796,9 +796,10 @@ void HexdumpWidget::removeBottomLinesWithoutScroll(QTextEdit *textEdit, int line QTextCursor textCursor = textEdit->textCursor(); for (int i = 0; i < lines; i++) { QTextCursor cursor(block); - cursor.select(QTextCursor::BlockUnderCursor); - cursor.removeSelectedText(); block = block.previous(); + //cursor.select(QTextCursor::BlockUnderCursor); + cursor.movePosition(QTextCursor::End, QTextCursor::KeepAnchor); + cursor.removeSelectedText(); } } @@ -837,29 +838,46 @@ void HexdumpWidget::scrollChanged() int firstLine = getDisplayedLined(ui->hexHexText); if (firstLine < (bufferLines / 2)) { - first_loaded_address -= bufferLines * cols; - auto hexdump = fetchHexdump(first_loaded_address, bufferLines); - prependWithoutScroll(ui->hexOffsetText, hexdump[0]); - prependWithoutScroll(ui->hexHexText, hexdump[1]); - prependWithoutScroll(ui->hexASCIIText, hexdump[2]); + int loadLines = bufferLines; + RVA shift = static_cast(loadLines * cols); + if (shift > first_loaded_address) { + loadLines = static_cast(first_loaded_address / cols); + shift = first_loaded_address; + } + first_loaded_address -= shift; + last_loaded_address -= shift; - removeBottomLinesWithoutScroll(ui->hexOffsetText, bufferLines); - removeBottomLinesWithoutScroll(ui->hexHexText, bufferLines); - removeBottomLinesWithoutScroll(ui->hexASCIIText, bufferLines); + if (loadLines > 0) { + auto hexdump = fetchHexdump(first_loaded_address, loadLines); + prependWithoutScroll(ui->hexOffsetText, hexdump[0]); + prependWithoutScroll(ui->hexHexText, hexdump[1]); + prependWithoutScroll(ui->hexASCIIText, hexdump[2]); - ui->hexOffsetText->verticalScrollBar()->setValue(ui->hexHexText->verticalScrollBar()->value()); - ui->hexASCIIText->verticalScrollBar()->setValue(ui->hexHexText->verticalScrollBar()->value()); + removeBottomLinesWithoutScroll(ui->hexOffsetText, loadLines); + removeBottomLinesWithoutScroll(ui->hexHexText, loadLines); + removeBottomLinesWithoutScroll(ui->hexASCIIText, loadLines); + ui->hexOffsetText->verticalScrollBar()->setValue(ui->hexHexText->verticalScrollBar()->value()); + ui->hexASCIIText->verticalScrollBar()->setValue(ui->hexHexText->verticalScrollBar()->value()); + } } int blocks = ui->hexHexText->document()->blockCount(); int lastLine = getDisplayedLined(ui->hexHexText, true); if (blocks - lastLine < (bufferLines / 2)) { - auto hexdump = fetchHexdump(last_loaded_address, bufferLines); - last_loaded_address += bufferLines * cols; - removeTopLinesWithoutScroll(ui->hexOffsetText, bufferLines); - removeTopLinesWithoutScroll(ui->hexHexText, bufferLines); - removeTopLinesWithoutScroll(ui->hexASCIIText, bufferLines); + int loadLines = bufferLines; + RVA shift = static_cast(loadLines * cols); + if (last_loaded_address > RVA_MAX - shift) { + shift = RVA_MAX - last_loaded_address; + loadLines = static_cast(shift / cols); + } + auto hexdump = fetchHexdump(last_loaded_address, loadLines); + last_loaded_address += shift; + first_loaded_address += shift; + + removeTopLinesWithoutScroll(ui->hexOffsetText, loadLines); + removeTopLinesWithoutScroll(ui->hexHexText, loadLines); + removeTopLinesWithoutScroll(ui->hexASCIIText, loadLines); appendWithoutScroll(ui->hexOffsetText, hexdump[0]); appendWithoutScroll(ui->hexHexText, hexdump[1]); appendWithoutScroll(ui->hexASCIIText, hexdump[2]);