Java Code Examples for ghidra.program.model.listing.Data#getBaseDataType()

The following examples show how to use ghidra.program.model.listing.Data#getBaseDataType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StringDataInstance.java    From ghidra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a new {@link StringDataInstance} using the bytes in the data codeunit.
 * <p>
 * @param data {@link Data} item
 * @return new {@link StringDataInstance}, never NULL.  See {@link #NULL_INSTANCE}.
 */
public static StringDataInstance getStringDataInstance(Data data) {
	if (data == null) {
		return NULL_INSTANCE;
	}
	DataType dt = data.getBaseDataType();
	if (dt instanceof AbstractStringDataType) {
		return ((AbstractStringDataType) dt).getStringDataInstance(data, data,
			data.getLength());
	}
	if (dt instanceof Array && !data.isInitializedMemory()) {
		ArrayStringable arrayStringable =
			ArrayStringable.getArrayStringable(((Array) dt).getDataType());
		if (arrayStringable != null && arrayStringable.hasStringValue(data)) {
			return new StringDataInstance(arrayStringable, data, data, data.getLength(), true);
		}
	}
	return NULL_INSTANCE;

}
 
Example 2
Source File: StringDataInstance.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the {@link Data} instance is a 'string'.
 *
 * @param data {@link Data} instance to test, null ok.
 * @return boolean true if string data.
 */
public static boolean isString(Data data) {
	if (data == null || !data.isInitializedMemory()) {
		return false;
	}
	DataType dt = data.getBaseDataType();
	if (dt instanceof AbstractStringDataType) {
		return true;
	}
	if (dt instanceof Array) {
		ArrayStringable as = ArrayStringable.getArrayStringable(((Array) dt).getDataType());
		return (as != null) && as.hasStringValue(data);
	}
	return false;
}
 
Example 3
Source File: StringDataInstance.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns true if the {@link Data} instance is one of the many 'char' data types.
 * 
 * @param data {@link Data} instance to test, null ok
 * @return boolean true if char data 
 */
public static boolean isChar(Data data) {
	if (data == null) {
		return false;
	}
	DataType dt = data.getBaseDataType();
	return (dt instanceof CharDataType) || (dt instanceof WideCharDataType) ||
		(dt instanceof WideChar16DataType) || (dt instanceof WideChar32DataType);
}
 
Example 4
Source File: StringTableSearchTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoMakeLabelOnExistingString() throws Exception {

	StringTableProvider provider = performSearch();
	StringTableModel model = (StringTableModel) getInstanceField("stringModel", provider);
	GhidraTable table = (GhidraTable) getInstanceField("table", provider);
	toggleDefinedStateButtons(provider, false, true, false, false);
	waitForTableModel(model);

	// *************************************************************************
	// make sure that no label is made when auto label is disabled but that the string is
	// *******************************************************************************
	setAutoLabelCheckbox(provider, false);

	// select row Address 404fde: \n\rString6\n\r)
	selectRows(table, addr(0x404fde));
	assertEquals("00404fde",
		getModelValue(model, table.getSelectedRow(), addressColumnIndex).toString());

	// make string with auto label selected
	DockingAction makeStringAction =
		(DockingAction) getInstanceField("makeStringAction", provider);
	performAction(makeStringAction, model);

	// make sure label not created but the string is
	Symbol sym = program.getSymbolTable().getPrimarySymbol(addr(0x404fde));
	assertEquals(null, sym);

	Data d = listing.getDataAt(addr(0x404fde));
	DataType dt = d.getBaseDataType();
	assertTrue(dt instanceof StringDataType);
	assertEquals("\n\rString6\n\r", d.getValue());

}
 
Example 5
Source File: StringTableSearchTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testMakeStringOverUndefined() throws Exception {

	StringTableProvider provider = performSearch();
	StringTableModel model = (StringTableModel) getInstanceField("stringModel", provider);
	GhidraTable table = (GhidraTable) getInstanceField("table", provider);
	waitForTableModel(model);
	setAutoLabelCheckbox(provider, true);

	// select row with String4
	Address addr = addr(0x40504c);
	selectRows(table, addr);
	assertTrue(table.getSelectedRow() >= 0);

	// make undefined
	int id = program.startTransaction("Create Data");
	listing.createData(addr, Undefined.getUndefinedDataType(1));
	program.endTransaction(id, true);
	Data data = listing.getDefinedDataAt(addr);
	assertNotNull(data);

	// make string
	DockingAction makeStringAction =
		(DockingAction) getInstanceField("makeStringAction", provider);
	performAction(makeStringAction, model, false);

	// test that the string was actually made correctly
	Data d = listing.getDataAt(addr(0x40504c));
	DataType dt = d.getBaseDataType();
	assertTrue(dt instanceof StringDataType);
	assertEquals("String4", d.getValue());
}
 
Example 6
Source File: CreateDataInStructureBackgroundCmd.java    From ghidra with Apache License 2.0 4 votes vote down vote up
/**
	 * 
	 * @see ghidra.framework.cmd.BackgroundCommand#applyTo(ghidra.framework.model.DomainObject, ghidra.util.task.TaskMonitor)
	 */
	@Override
	public boolean applyTo(DomainObject obj, TaskMonitor monitor) {

		Program program = (Program) obj;
		Data data = program.getListing().getDefinedDataContaining(addr);
		Data startData = data.getComponent(startPath);
		if (startData == null) {
			setStatusMsg("Component data not found");
			return false;
		}

		Data parent = startData.getParent();
		DataType parentDataType = parent.getBaseDataType();

		if (!(parentDataType instanceof Structure)) {
			setStatusMsg("Range based operation only supported within Structures");
			return false;
		}

		DataType existingDT = startData.getDataType();
		int startIndex = startData.getComponentIndex();
		Data lastComp = parent.getComponentAt(
			(int) (startData.getMinAddress().subtract(parent.getMinAddress()) + length - 1));
		int endIndex = lastComp.getComponentIndex();

		Structure struct = (Structure) parentDataType;

		// TODO: Is looking at the first component data-type (existingDT) the right thing to do for a selection ??

		if (newDataType instanceof FactoryDataType) {
			setStatusMsg("Factory data-type not allowed in structure: " + newDataType.getName());
			return false;
		}

		newDataType = newDataType.clone(program.getDataTypeManager());
		newDataType =
			DataUtilities.reconcileAppliedDataType(existingDT, newDataType, stackPointers);

		if (newDataType instanceof Dynamic && !((Dynamic) newDataType).canSpecifyLength()) {
			setStatusMsg(
				"Non-sizable Dynamic data-type not allowed in structure: " + newDataType.getName());
			return false;
		}

		for (int i = endIndex; i >= startIndex; i--) {
			struct.clearComponent(i);
		}

		if (newDataType != DataType.DEFAULT) {
			int index = startIndex;
			int numCreated = 0;
			while (length > 0) {
				try {
//			        MemBuffer memBuf = new ProgramStructureProviderContext(program,addr, 
//    	    	    					struct, struct.getComponent(index).getOffset());
					DataTypeInstance dti =
						DataTypeInstance.getDataTypeInstance(newDataType, length);
					if (dti == null || dti.getLength() > length) {
						break;
					}
					DataTypeComponent dtc =
						struct.replace(index, dti.getDataType(), dti.getLength());
					length -= dtc.getLength();
					numCreated++;
					index++;
				}

				catch (Exception e) {
					setStatusMsg(e.getMessage());
					return false;
				}
			}
			if (numCreated == 0) {
				setStatusMsg("Not enough space");
				return false;
			}
		}
		return true;
	}
 
Example 7
Source File: StringTable_BE_Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testSearchSelection() throws Exception {
	final AddressSet set = new AddressSet();
	set.addRange(addr(0x100), addr(0x1000));

	// select the address set
	cbPlugin.firePluginEvent(new ProgramSelectionPluginEvent(cbPlugin.getName(),
		new ProgramSelection(set), program));

	SearchStringDialog dialog = getDialog();

	// turn off null Terminate box
	JCheckBox cb = (JCheckBox) findButton(dialog.getComponent(), "Require Null Termination");
	cb.setSelected(false);

	pressButtonByText(dialog.getComponent(), "Search");

	@SuppressWarnings("unchecked")
	StringTableProvider provider =
		((List<StringTableProvider>) getInstanceField("transientProviders", plugin)).get(0);
	StringTableModel model = (StringTableModel) getInstanceField("stringModel", provider);
	GhidraTable table = (GhidraTable) getInstanceField("table", provider);
	waitForTableModel(model);

	setAutoLabelCheckbox(provider, true);

	// test the results size
	assertEquals(4, model.getRowCount());

	// test the first and last ones
	assertEquals(addr(0x100), addressCell(model, 0));
	assertEquals(addr(0x400), addressCell(model, 3));

	// ********************** test Make Unicode String
	// select row for address 400
	Address address = addr(0x400);
	selectRows(table, address);

	// make string
	DockingAction makeStringAction =
		(DockingAction) getInstanceField("makeStringAction", provider);
	performAction(makeStringAction, true);
	waitForTableModel(model);

	// test that the string was actually made correctly
	// test that the string was actually made correctly
	Data d = program.getListing().getDataAt(addr(0x400));
	assertEquals(12, d.getLength());

	String str = (String) d.getValue();
	assertEquals("abcdef", str);

	DataType dt = d.getBaseDataType();
	assertEquals("unicode", dt.getName());

	// test that the label was made correctly
	Symbol sym = program.getSymbolTable().getPrimarySymbol(addr(0x400));
	assertEquals("u_abcdef@00000400", sym.getName());

	selectRows(table, address);
	int selectedRow = table.getSelectedRow();

	// test that the table was updated with the label and preview
	assertEquals("u_abcdef@00000400", labelCell(model, selectedRow));

	CodeUnitTableCellData value = previewCell(model, selectedRow);
	assertEquals("unicode u\"abcdef\"", value.getDisplayString());

}
 
Example 8
Source File: StringTableSearchTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testAutoLabelDoesntOverwriteUserLabel() throws Exception {
	// *****TEST4********************************************************************************************
	// make sure that when there is a user-defined label, no data yet - label not changed but string created
	//*******************************************************************************************************

	StringTableProvider provider = performSearch();
	StringTableModel model = (StringTableModel) getInstanceField("stringModel", provider);
	GhidraTable table = (GhidraTable) getInstanceField("table", provider);
	toggleDefinedStateButtons(provider, false, true, false, false);
	waitForTableModel(model);
	setAutoLabelCheckbox(provider, true);

	// select row (Address 40503c - String2)
	selectRows(table, addr(0x40503c));
	assertEquals("0040503c",
		getModelValue(model, table.getSelectedRow(), addressColumnIndex).toString());

	// make a user-defined label
	Symbol sym = program.getSymbolTable().getPrimarySymbol(addr(0x40503c));
	assertEquals(null, sym);
	int txId = program.startTransaction("Create Label");
	boolean commit;
	try {
		program.getSymbolTable().createLabel(addr(0x40503c), "testLabel",
			SourceType.USER_DEFINED);
		commit = true;
	}
	catch (InvalidInputException exc) {
		commit = false;
	}
	program.endTransaction(txId, commit);

	// the createSymbol call will trigger notifications in the Swing thread that we need
	// to finish before we can move on
	waitForPostedSwingRunnables();
	// make string with auto label selected
	DockingAction makeStringAction =
		(DockingAction) getInstanceField("makeStringAction", provider);
	performAction(makeStringAction, model);

	// make sure new label is made primary and second label is still there as secondary one
	sym = program.getSymbolTable().getPrimarySymbol(addr(0x40503c));
	assertEquals("s_String2", sym.getName());
	Symbol symArray[] = program.getSymbolTable().getSymbols(addr(0x40503c));
	assertEquals(2, symArray.length);
	assertEquals("s_String2", symArray[0].getName());
	assertEquals("testLabel", symArray[1].getName());

	Data d = listing.getDataAt(addr(0x40503c));
	DataType dt = d.getBaseDataType();
	assertTrue(dt instanceof StringDataType);
	assertEquals("String2", d.getValue());
}
 
Example 9
Source File: StringTableSearchTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testMakeCollision() throws Exception {

	StringTableProvider provider = performSearch();
	StringTableModel model = (StringTableModel) getInstanceField("stringModel", provider);
	GhidraTable table = (GhidraTable) getInstanceField("table", provider);
	waitForTableModel(model);
	setAutoLabelCheckbox(provider, true);

	// select row with String4
	selectRows(table, addr(0x40504c));
	assertTrue(table.getSelectedRow() >= 0);

	// make string
	DockingAction makeStringAction =
		(DockingAction) getInstanceField("makeStringAction", provider);
	performAction(makeStringAction, model);

	// test that the string was actually made correctly
	Data d = listing.getDataAt(addr(0x40504c));
	DataType dt = d.getBaseDataType();
	assertTrue(dt instanceof StringDataType);
	assertEquals("String4", d.getValue());

	// test that the length of the string created was correct to make
	// sure that the \n and 00 was included in the created data
	assertEquals(8, d.getLength());

	// test that the label was made
	Symbol sym = program.getSymbolTable().getPrimarySymbol(addr(0x40504c));
	assertEquals("s_String4", sym.getName());
	waitForPostedSwingRunnables();

	// try to make char array
	//	select row with String4
	// select row with String4
	selectRows(table, addr(0x40504c));

	DockingAction makeCharArrayAction =
		(DockingAction) getInstanceField("makeCharArrayAction", provider);
	assertTrue(!makeCharArrayAction.isEnabledForContext(null));

}