allow pasting hex numbers with prefix in Search Memory

This commit is contained in:
shocoman 2022-09-28 13:41:20 +07:00 committed by dragonmacher
parent 926286ee6f
commit 79e239f690
4 changed files with 69 additions and 6 deletions

View file

@ -710,6 +710,9 @@ class MemSearchDialog extends DialogComponentProvider {
public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
clearStatusText();
// allow pasting numbers in the forms like 0xABC or ABCh
str = removeNumberBasePrefixAndSuffix(str);
String currentText = getText(0, getLength());
String beforeOffset = currentText.substring(0, offs);
String afterOffset = currentText.substring(offs, currentText.length());
@ -788,6 +791,34 @@ class MemSearchDialog extends DialogComponentProvider {
}
return null;
}
private String removeNumberBasePrefixAndSuffix(String str) {
if (!(currentFormat instanceof HexSearchFormat || currentFormat instanceof BinarySearchFormat)) {
return str;
}
String numMaybe = str.strip();
String lowercase = numMaybe.toLowerCase();
if (currentFormat instanceof HexSearchFormat) {
if (lowercase.startsWith("0x")) {
numMaybe = numMaybe.substring(2);
} else if (lowercase.startsWith("$")) {
numMaybe = numMaybe.substring(1);
} else if (lowercase.endsWith("h")) {
numMaybe = numMaybe.substring(0, numMaybe.length() - 1);
}
} else {
if (lowercase.startsWith("0b")) {
numMaybe = numMaybe.substring(2);
}
}
// check if the resultant number looks valid for insertion (i.e. not empty)
if (!numMaybe.isEmpty()) {
return numMaybe;
}
return str;
}
}
boolean getShowAdvancedOptions() {

View file

@ -107,6 +107,17 @@ public class MemSearchBinaryTest extends AbstractMemSearchTest {
myTypeText("01110000 01110000");
assertEquals("01110000 01110000", valueField.getText());
}
@Test
public void testBinaryPasteNumberWithPrefix() {
// paste a number with a binary prefix;
// the prefix should be removed before the insertion
setValueText("0b00101010");
assertEquals("00101010", valueField.getText());
setValueText("0B1010 10");
assertEquals("1010 10", valueField.getText());
}
@Test
public void testBinarySearch() throws Exception {

View file

@ -187,6 +187,27 @@ public class MemSearchHexTest extends AbstractMemSearchTest {
assertEquals("01 23 45 67 89", valueField.getText());
}
@Test
public void testHexPasteNumberWithPrefixAndSuffix() {
// paste a number with a hex prefix;
// the prefix should be removed before the insertion
setValueText("0xabcdef");
assertEquals("abcdef", valueField.getText());
setValueText("$68000");
assertEquals("68000", valueField.getText());
// same for 'h' the suffix
setValueText("ABCDEFh");
assertEquals("ABCDEF", valueField.getText());
// should also somehow work with leading and trailing white spaces
setValueText(" 0X321 ");
assertEquals("321", valueField.getText().strip());
setValueText(" 123H ");
assertEquals("123", valueField.getText().strip());
}
@Test
public void testHexSearch() throws Exception {

View file

@ -109,8 +109,8 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
JTextField valueField = (JTextField) comboBox.getEditor().getEditorComponent();
assertEquals(
"01010101 10001011 11101100 10000001 11101100 ........ ........ ........ ........ ",
valueField.getText());
"01010101 10001011 11101100 10000001 11101100 ........ ........ ........ ........",
valueField.getText().strip());
}
@ -131,8 +131,8 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
JTextField valueField = (JTextField) comboBox.getEditor().getEditorComponent();
assertEquals(
"01010... 10001011 11...... 10000001 11101... ........ ........ ........ ........ ",
valueField.getText());
"01010... 10001011 11...... 10000001 11101... ........ ........ ........ ........",
valueField.getText().strip());
}
@ -153,8 +153,8 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
JTextField valueField = (JTextField) comboBox.getEditor().getEditorComponent();
assertEquals(
"01010101 10001011 11101100 10000001 11101100 00000100 00000001 00000000 00000000 ",
valueField.getText());
"01010101 10001011 11101100 10000001 11101100 00000100 00000001 00000000 00000000",
valueField.getText().strip());
}
/**