Merge remote-tracking branch

'origin/GP-2622_dragonmacher_PR-4623_shocoman_paste-hex-in-search-memory-window2'
(Closes #4623)
This commit is contained in:
Ryan Kurtz 2022-10-03 00:39:33 -04:00
commit 8f90b5929e
4 changed files with 84 additions and 21 deletions

View file

@ -580,10 +580,10 @@ class MemSearchDialog extends DialogComponentProvider {
memoryBlockGroup.add(loadedBlocks);
memoryBlockGroup.add(allBlocks);
loadedBlocks.setToolTipText(HTMLUtilities.toHTML(
"Only searches memory blocks that are loaded in a running executable.\n " +
"Ghidra now includes memory blocks for other data such as section headers.\n" +
"This option exludes these OTHER (non loaded) blocks."));
loadedBlocks.setToolTipText(HTMLUtilities
.toHTML("Only searches memory blocks that are loaded in a running executable.\n " +
"Ghidra now includes memory blocks for other data such as section headers.\n" +
"This option exludes these OTHER (non loaded) blocks."));
allBlocks.setToolTipText(
"Searches all memory blocks including blocks that are not actually loaded in a running executable");
@ -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,38 @@ 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

@ -15,8 +15,7 @@
*/
package ghidra.app.plugin.core.searchmem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.*;
import java.awt.Container;
import java.awt.Window;
@ -99,8 +98,7 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
performAction(searchMnemonicOperandsNoConstAction, cb.getProvider(), true);
MemSearchDialog dialog =
waitForDialogComponent(tool.getToolFrame(), MemSearchDialog.class, 2000);
MemSearchDialog dialog = waitForDialogComponent(MemSearchDialog.class);
assertNotNull(dialog);
Container component = dialog.getComponent();
@ -109,8 +107,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());
}
@ -121,8 +119,7 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
performAction(searchMnemonicNoOperandsNoConstAction, cb.getProvider(), true);
MemSearchDialog dialog =
waitForDialogComponent(tool.getToolFrame(), MemSearchDialog.class, 2000);
MemSearchDialog dialog = waitForDialogComponent(MemSearchDialog.class);
assertNotNull(dialog);
Container component = dialog.getComponent();
@ -131,8 +128,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());
}
@ -143,8 +140,7 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
performAction(searchMnemonicOperandsConstAction, cb.getProvider(), true);
MemSearchDialog dialog =
waitForDialogComponent(tool.getToolFrame(), MemSearchDialog.class, 2000);
MemSearchDialog dialog = waitForDialogComponent(MemSearchDialog.class);
assertNotNull(dialog);
Container component = dialog.getComponent();
@ -153,14 +149,14 @@ 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());
}
/**
* Tests that when multiple regions are selected, the user is notified via
* pop-up that this is not acceptable.
*
*
*/
@Test
public void testMultipleSelection() {
@ -178,10 +174,10 @@ public class MnemonicSearchPluginTest extends AbstractGhidraHeadedIntegrationTes
// Now invoke the menu option we want to test.
performAction(searchMnemonicOperandsConstAction, cb.getProvider(), false);
// Here's the main assert: If the code recognizes that we have multiple selection, the
// Here's the main assert: If the code recognizes that we have multiple selection, the
// MemSearchDialog will NOT be displayed (an error message pops up instead). So verify that
// the dialog is null and we're ok.
Window errorDialog = waitForWindow("Mnemonic Search Error", 2000);
Window errorDialog = waitForWindow("Mnemonic Search Error");
assertNotNull(errorDialog);
errorDialog.setVisible(false);
}