Ghidra is a software reverse engineering (SRE) framework
GP-1616 created a two step selection action
ghidravore13574ecfb239
diff --git a/Ghidra/Features/Base/certification.manifest b/Ghidra/Features/Base/certification.manifestindex 3d5c46938e..916c438c64 100644--- a/Ghidra/Features/Base/certification.manifest+++ b/Ghidra/Features/Base/certification.manifest@@ -899,6 +899,7 @@ src/main/resources/help/Dummy_HelpSet.hs||GHIDRA||reviewed||END|src/main/resources/help/empty.htm||GHIDRA||||END|src/main/resources/images/2leftarrow.png||Nuvola Icons - LGPL 2.1|||Nuvola icon set|END|src/main/resources/images/2rightarrow.png||Nuvola Icons - LGPL 2.1|||Nuvola icon set|END|+src/main/resources/images/ArmedMarkSelection.png||GHIDRA||||END|src/main/resources/images/Array.png||GHIDRA||||END|src/main/resources/images/B.gif||GHIDRA||||END|src/main/resources/images/BookShelf.png||GHIDRA||||END|@@ -927,6 +928,7 @@ src/main/resources/images/Instructions_32.png||GHIDRA||||END|src/main/resources/images/L.gif||GHIDRA||||END|src/main/resources/images/LocalVariable.gif||GHIDRA||||END|src/main/resources/images/M.gif||GHIDRA||||END|+src/main/resources/images/MarkSelection.png||GHIDRA||||END|src/main/resources/images/Merge.png||GHIDRA||||END|src/main/resources/images/MultiDuplicateData.png||GHIDRA||||END|src/main/resources/images/Namespace.gif||GHIDRA||||END|@@ -1082,6 +1084,7 @@ src/main/resources/images/lock.gif||GHIDRA||||END|src/main/resources/images/lock.png||FAMFAMFAM Icons - CC 2.5|||famfamfam silk icon set|END|src/main/resources/images/magnifier.png||FAMFAMFAM Icons - CC 2.5||||END|src/main/resources/images/media-flash.png||Oxygen Icons - LGPL 3.0|||Oxygen icon theme (dual license; LGPL or CC-SA-3.0)|END|+src/main/resources/images/media-playback-stop.png||Oxygen Icons - LGPL 3.0|||Oxygen icon theme (dual license; LGPL or CC-SA-3.0)|END|src/main/resources/images/mem_chip3.png||GHIDRA||reviewed||END|src/main/resources/images/memory16.gif||GHIDRA||||END|src/main/resources/images/menu16.gif||GHIDRA||||END|diff --git a/Ghidra/Features/Base/src/main/help/help/topics/Selection/Selecting.htm b/Ghidra/Features/Base/src/main/help/help/topics/Selection/Selecting.htmindex 9224625e17..28596c0c13 100644--- a/Ghidra/Features/Base/src/main/help/help/topics/Selection/Selecting.htm+++ b/Ghidra/Features/Base/src/main/help/help/topics/Selection/Selecting.htm@@ -182,7 +182,16 @@is not in the current selection.<BR></TD></TR>+ <TR>+ <TD valign="top" align="center"><A name=+ "Mark_And_Select"></A>Mark and Select</TD>+ <TD valign="top">Creates a selection in a two step process. The first time+ the action is invoked, the current location is marked. The second time the+ action is invoked, a selection is created from the marked location to the+ current location.<BR>+ </TD>+ </TR><TR><TD width="131" valign="top" align="center"><A name="Data"></A>Data</TD>diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeBrowserSelectionPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeBrowserSelectionPlugin.javaindex cb7893a65f..0467a98b2d 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeBrowserSelectionPlugin.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/CodeBrowserSelectionPlugin.java@@ -55,6 +55,7 @@ import resources.ResourceManager;//@formatter:onpublic class CodeBrowserSelectionPlugin extends Plugin {+ private static final String SELECT_GROUP = "Select Group";private static final String SELECTION_LIMIT_OPTION_NAME = "Table From Selection Limit";private static final int DEFAULT_TABLE_LIMIT = 20000;@@ -75,7 +76,7 @@ public class CodeBrowserSelectionPlugin extends Plugin {private void createActions() {new ActionBuilder("Select All", getName()).menuPath(ToolConstants.MENU_SELECTION, "&All in View")- .menuGroup("Select Group", "a")+ .menuGroup(SELECT_GROUP, "a").keyBinding("ctrl A").supportsDefaultToolContext(true).helpLocation(new HelpLocation(HelpTopics.SELECTION, "Select All"))@@ -86,7 +87,7 @@ public class CodeBrowserSelectionPlugin extends Plugin {new ActionBuilder("Clear Selection", getName()).menuPath(ToolConstants.MENU_SELECTION, "&Clear Selection")- .menuGroup("Select Group", "b")+ .menuGroup(SELECT_GROUP, "b").supportsDefaultToolContext(true).helpLocation(new HelpLocation(HelpTopics.SELECTION, "Clear Selection")).withContext(CodeViewerActionContext.class)@@ -97,7 +98,7 @@ public class CodeBrowserSelectionPlugin extends Plugin {new ActionBuilder("Select Complement", getName()).menuPath(ToolConstants.MENU_SELECTION, "&Complement")- .menuGroup("Select Group", "c")+ .menuGroup(SELECT_GROUP, "c").supportsDefaultToolContext(true).helpLocation(new HelpLocation(HelpTopics.SELECTION, "Select Complement")).withContext(CodeViewerActionContext.class)@@ -105,6 +106,8 @@ public class CodeBrowserSelectionPlugin extends Plugin {.onAction(c -> ((CodeViewerProvider) c.getComponentProvider()).selectComplement()).buildAndInstall(tool);+ tool.addAction(new MarkAndSelectionAction(getName(), SELECT_GROUP, "d"));+new ActionBuilder("Create Table From Selection", getName()).menuPath(ToolConstants.MENU_SELECTION, "Create Table From Selection").menuGroup("SelectUtils")@@ -114,6 +117,7 @@ public class CodeBrowserSelectionPlugin extends Plugin {.inWindow(ActionBuilder.When.CONTEXT_MATCHES).onAction(c -> createTable((CodeViewerProvider) c.getComponentProvider())).buildAndInstall(tool);+}private void createTable(CodeViewerProvider componentProvider) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/MarkAndSelectionAction.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/MarkAndSelectionAction.javanew file mode 100644index 0000000000..e16ec4d06c--- /dev/null+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/codebrowser/MarkAndSelectionAction.java@@ -0,0 +1,161 @@+/* ###+ * IP: GHIDRA+ *+ * 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.+ */+package ghidra.app.plugin.core.codebrowser;++import javax.swing.Icon;+import javax.swing.ImageIcon;++import docking.ActionContext;+import docking.action.*;+import docking.tool.ToolConstants;+import ghidra.app.context.NavigatableActionContext;+import ghidra.app.nav.Navigatable;+import ghidra.app.util.HelpTopics;+import ghidra.program.model.address.Address;+import ghidra.program.util.ProgramLocation;+import ghidra.program.util.ProgramSelection;+import ghidra.util.HelpLocation;+import resources.MultiIconBuilder;+import resources.ResourceManager;++/**+ * Actions for creating a selection using two distinct steps. The first time the action+ * is invoked, it records the current location as the start of a selection. The second time the+ * action is invoked it creates a selection from the recorded location to the current location.+ */+public class MarkAndSelectionAction extends ToggleDockingAction {+ private Navigatable markedNavigatable;+ private ProgramLocation markedLocation;+ private Icon unarmedIcon;+ private Icon armedIcon;++ public MarkAndSelectionAction(String owner, String group, String subGroup) {+ super("Mark and Select", owner);+ buildIcons();+ MenuData menuData =+ new MenuData(new String[] { ToolConstants.MENU_SELECTION, "Mark Selection Start" });+ menuData.setMenuGroup(group);+ menuData.setMenuSubGroup(subGroup);+ setMenuBarData(menuData);+ setKeyBindingData(new KeyBindingData("m"));+ setToolBarData(+ new ToolBarData(unarmedIcon, ToolConstants.TOOLBAR_GROUP_THREE, "Z"));++ setHelpLocation((new HelpLocation(HelpTopics.SELECTION, "Mark_And_Select")));+ setSupportsDefaultToolContext(true);+ addToWindowWhen(NavigatableActionContext.class);++ }++ private void buildIcons() {+ ImageIcon baseImage = ResourceManager.loadImage("images/MarkSelection.png");+ ImageIcon mediaStart = ResourceManager.loadImage("images/media-playback-start.png");+ ImageIcon mediaStop = ResourceManager.loadImage("images/media-playback-stop.png");++ MultiIconBuilder builder = new MultiIconBuilder(baseImage);+ builder.addLowerRightIcon(mediaStart, 12, 12);+ unarmedIcon = builder.build();++ builder = new MultiIconBuilder(baseImage);+ builder.addLowerRightIcon(mediaStop, 12, 12);+ armedIcon = builder.build();+ }++ @Override+ public boolean isEnabledForContext(ActionContext context) {+ if (context instanceof NavigatableActionContext) {+ return ((NavigatableActionContext) context).getLocation() != null;+ }+ return false;+ }++ @Override+ public final boolean isValidContext(ActionContext context) {+ return context instanceof NavigatableActionContext;+ }++ @Override+ public void actionPerformed(ActionContext context) {+ if (context instanceof NavigatableActionContext) {+ actionPerformed((NavigatableActionContext) context);+ }+ }++ protected void actionPerformed(NavigatableActionContext context) {+ Navigatable navigatable = context.getNavigatable();+ if (isArmed(navigatable)) {+ createSelection(navigatable);+ }+ else {+ armSelection(navigatable);+ }++ updateAction();+ }++ private void updateAction() {+ String menuName = "Mark Selection Start";+ String description = "Mark current location for selection start";+ Icon icon = unarmedIcon;++ if (markedLocation != null) {+ Address address = markedLocation.getByteAddress();+ menuName = "Create Selection from " + address;+ description = "Create seletion from marked location: " + address;+ icon = armedIcon;+ }++ MenuData menuBarData = getMenuBarData();+ menuBarData.setMenuItemName(menuName);+ setDescription(description);+ getToolBarData().setIcon(icon);++ }++ private void armSelection(Navigatable navigatable) {+ markedNavigatable = navigatable;+ markedLocation = navigatable.getLocation();+ if (markedLocation == null) {+ markedNavigatable = null;+ }+ }++ private void createSelection(Navigatable navigatable) {+ ProgramLocation location = navigatable.getLocation();+ Address start = markedLocation.getByteAddress();+ Address end = location.getByteAddress();+ ProgramSelection selection = new ProgramSelection(start, end);+ navigatable.setSelection(selection);++ // set back to unarmed+ markedLocation = null;+ markedNavigatable = null;+ }++ private boolean isArmed(Navigatable navigatable) {+ if (markedNavigatable == null || markedLocation == null) {+ return false;+ }+ if (navigatable != markedNavigatable) {+ return false;+ }+ if (markedLocation.getProgram() != navigatable.getProgram()) {+ return false;+ }+ return true;+ }++}diff --git a/Ghidra/Features/Base/src/main/resources/images/MarkSelection.png b/Ghidra/Features/Base/src/main/resources/images/MarkSelection.pngnew file mode 100644index 0000000000..e4d342570fBinary files /dev/null and b/Ghidra/Features/Base/src/main/resources/images/MarkSelection.png differdiff --git a/Ghidra/Features/Base/src/main/resources/images/media-playback-stop.png b/Ghidra/Features/Base/src/main/resources/images/media-playback-stop.pngnew file mode 100644index 0000000000..180280e8baBinary files /dev/null and b/Ghidra/Features/Base/src/main/resources/images/media-playback-stop.png differdiff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/select/MarkAndSelectActionTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/select/MarkAndSelectActionTest.javanew file mode 100644index 0000000000..7d8351b1f4--- /dev/null+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/select/MarkAndSelectActionTest.java@@ -0,0 +1,129 @@+/* ###+ * IP: GHIDRA+ *+ * 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.+ */+package ghidra.app.plugin.core.select;++import static org.junit.Assert.*;++import org.junit.*;++import docking.action.ToggleDockingAction;+import ghidra.app.plugin.core.codebrowser.CodeBrowserPlugin;+import ghidra.app.plugin.core.codebrowser.CodeBrowserSelectionPlugin;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.address.Address;+import ghidra.program.model.listing.Program;+import ghidra.program.util.ProgramSelection;+import ghidra.test.*;++public class MarkAndSelectActionTest extends AbstractGhidraHeadedIntegrationTest {++ private TestEnv env;+ private Program program;+ private PluginTool tool;+ private CodeBrowserPlugin cb;+ private ToyProgramBuilder builder;+ private ToggleDockingAction markAndSelectAction;++ public MarkAndSelectActionTest() {+ super();+ }++ @Before+ public void setUp() throws Exception {+ env = new TestEnv();+ builder = new ToyProgramBuilder("program", false);+ program = buildProgram();+ tool = env.launchDefaultTool(program);+ cb = env.getPlugin(CodeBrowserPlugin.class);+ CodeBrowserSelectionPlugin selectPlugin = env.getPlugin(CodeBrowserSelectionPlugin.class);+ markAndSelectAction = (ToggleDockingAction) getAction(selectPlugin, "Mark and Select");+ }++ private Program buildProgram() throws Exception {+ builder.createMemory(".text", "0x1001000", 1000);+ return builder.getProgram();+ }++ @After+ public void tearDown() throws Exception {+ builder.dispose();+ env.dispose();+ }++ @Test+ public void testMarkAndSelectAction() {+ assertNoSelection();+ assertMarkAndSelectActionArmed(false);++ goTo("0x1001010");+ invokeMarkAndSelectAction();+ assertNoSelection();+ assertMarkAndSelectActionArmed(true);++ goTo("0x1001020");+ invokeMarkAndSelectAction();+ assertSelection("0x1001010", "0x1001020");+ assertMarkAndSelectActionArmed(false);+ }++ @Test+ public void testMarkAndSelectReplacesCurrentSelection() {+ createSelection("0x1001000", "0x1001002");+ assertSelection("0x1001000", "0x1001002");+ assertMarkAndSelectActionArmed(false);++ goTo("0x1001010");+ invokeMarkAndSelectAction();++ goTo("0x1001020");+ invokeMarkAndSelectAction();+ assertSelection("0x1001010", "0x1001020");+ }++ private void createSelection(String string, String string2) {+ Address from = addr(string);+ Address to = addr(string2);+ runSwing(() -> cb.getProvider().setSelection(new ProgramSelection(from, to)));+ }++ private void assertMarkAndSelectActionArmed(boolean armed) {+ assertEquals(armed, runSwing(() -> markAndSelectAction.isSelected()));+ }++ private void assertSelection(String startAddr, String endAddr) {+ ProgramSelection selection = cb.getCurrentSelection();+ Address start = addr(startAddr);+ Address end = addr(endAddr);+ assertEquals(new ProgramSelection(start, end), selection);+ }++ private void invokeMarkAndSelectAction() {+ performAction(markAndSelectAction, cb.getProvider().getActionContext(null), true);+ }++ private void goTo(String addrString) {+ cb.goToField(addr(addrString), "Address", 0, 0);+ }++ private void assertNoSelection() {+ ProgramSelection currentSelection = cb.getCurrentSelection();+ assertTrue(currentSelection.isEmpty());+ }++ private Address addr(String addrString) {+ return program.getAddressFactory().getAddress(addrString);+ }+}diff --git a/Ghidra/Framework/Generic/src/main/java/resources/MultiIconBuilder.java b/Ghidra/Framework/Generic/src/main/java/resources/MultiIconBuilder.javaindex d96bfb8383..0f0abe84ae 100644--- a/Ghidra/Framework/Generic/src/main/java/resources/MultiIconBuilder.java+++ b/Ghidra/Framework/Generic/src/main/java/resources/MultiIconBuilder.java@@ -63,6 +63,24 @@ public class MultiIconBuilder {}+ /**+ * Adds the specified icon as an overlay to the base icon at the given offsets and scaled+ * to the specified width and height.+ *+ * @param icon the icon to overlay+ * @param w width of the overlaid icon+ * @param h height of the overlaid icon+ * @param x the horizontal offset of the overlay with respect to the base icon+ * @param y the vertical offset of the overlay with respect to the base icon+ * @return this builder (for chaining)+ */+ public MultiIconBuilder addIcon(Icon icon, int w, int h, int x, int y) {+ ImageIcon scaled = ResourceManager.getScaledIcon(icon, w, h);+ TranslateIcon txIcon = new TranslateIcon(scaled, x, y);+ multiIcon.addIcon(txIcon);+ return this;+ }+/*** Adds the given icon as an overlay to the base icon, to the lower-right*