BitFields - added Edit Bitfield Action for Unaligned Structures.

This commit is contained in:
ghidra1
2019-05-13 18:28:30 -04:00
parent 0a22915976
commit 5ac462441a
5 changed files with 105 additions and 6 deletions
@@ -193,7 +193,7 @@ public class BitFieldEditorPanel extends JPanel {
private JComponent createDataTypeChoiceEditor() {
dtChoiceEditor = new DataTypeSelectionEditor(dtmService, -1, AllowedDataTypes.BITFIELD_USE);
dtChoiceEditor = new DataTypeSelectionEditor(dtmService, -1, AllowedDataTypes.BITFIELD_BASE_TYPE);
dtChoiceEditor.setConsumeEnterKeyPress(false);
dtChoiceEditor.setTabCommitsEdit(true);
//dtChoiceEditor.setPreferredDataTypeManager(composite.getDataTypeManager());
@@ -0,0 +1,97 @@
/* ###
* 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.compositeeditor;
import java.awt.Component;
import java.awt.Window;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import docking.ActionContext;
import docking.DockingWindowManager;
import ghidra.program.model.data.DataTypeComponent;
import ghidra.program.model.data.Structure;
import ghidra.util.exception.AssertException;
/**
* Action for use in the composite data type editor.
* This action has help associated with it.
*/
public class EditBitFieldAction extends CompositeEditorTableAction {
private final static String ACTION_NAME = "Edit Bitfield";
private final static String GROUP_NAME = BITFIELD_ACTION_GROUP;
private final static String DESCRIPTION = "Edit an existing bitfield";
private static String[] popupPath = new String[] { ACTION_NAME };
public EditBitFieldAction(CompositeEditorProvider provider) {
super(provider, EDIT_ACTION_PREFIX + ACTION_NAME, GROUP_NAME, popupPath, null, null);
setDescription(DESCRIPTION);
if (!(model instanceof CompEditorModel)) {
throw new AssertException("unsupported use");
}
adjustEnablement();
}
private DataTypeComponent getUnalignedBitFieldComponent() {
CompEditorModel editorModel = (CompEditorModel) model;
if ((editorModel.viewComposite instanceof Structure) &&
!editorModel.viewComposite.isInternallyAligned() &&
editorModel.getNumSelectedRows() == 1) {
int rowIndex = model.getSelectedRows()[0];
if (rowIndex < model.getNumComponents()) {
DataTypeComponent dtComponent = model.getComponent(rowIndex);
if (dtComponent.isBitFieldComponent()) {
return dtComponent;
}
}
}
return null;
}
@Override
public void actionPerformed(ActionContext context) {
CompEditorModel editorModel = (CompEditorModel) model;
DataTypeComponent dtComponent = getUnalignedBitFieldComponent();
if (dtComponent == null) {
return;
}
BitFieldEditorDialog dlg = new BitFieldEditorDialog(editorModel.viewComposite,
provider.dtmService, dtComponent.getOrdinal(),
ordinal -> refreshTableAndSelection(editorModel, ordinal));
Component c = provider.getComponent();
Window w = SwingUtilities.windowForComponent(c);
DockingWindowManager.showDialog(w, dlg, c);
requestTableFocus();
}
private void refreshTableAndSelection(CompEditorModel editorModel, int ordinal) {
editorModel.fireTableDataChanged();
editorModel.compositeInfoChanged();
JTable editorTable = provider.getTable();
editorTable.getSelectionModel().setSelectionInterval(ordinal, ordinal);
}
@Override
public void adjustEnablement() {
setEnabled(getUnalignedBitFieldComponent() != null);
}
}
@@ -70,7 +70,8 @@ public class StructureEditorProvider extends CompositeEditorProvider {
new EditFieldAction(this),
new HexNumbersAction(this),
new CreateInternalStructureAction(this),
new AddBitFieldAction(this)
new AddBitFieldAction(this),
new EditBitFieldAction(this)
};
//@formatter:on
}
@@ -64,7 +64,8 @@ public class UnionEditorProvider extends CompositeEditorProvider {
new EditComponentAction(this),
new EditFieldAction(this),
new HexNumbersAction(this),
new AddBitFieldAction(this)
new AddBitFieldAction(this),
new EditBitFieldAction(this)
};
//@formatter:on
}
@@ -28,7 +28,7 @@ public class DataTypeParser {
public enum AllowedDataTypes {
/**
* All data-types are permitted
* All data-types are permitted (excluding bitfields)
*/
ALL,
/**
@@ -51,7 +51,7 @@ public class DataTypeParser {
* Only Enums, Integer types and those Typedefs based on them
* for use as a bitfield base datatype
*/
BITFIELD_USE
BITFIELD_BASE_TYPE
}
private DataTypeManager sourceDataTypeManager; // may be null
@@ -186,7 +186,7 @@ public class DataTypeParser {
throw new InvalidDataTypeException("fixed-length or string data-type required");
}
break;
case BITFIELD_USE:
case BITFIELD_BASE_TYPE:
if (!BitFieldDataType.isValidBaseDataType(dt)) {
throw new InvalidDataTypeException(
"enum or integer derived data-type required");