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

The following examples show how to use ghidra.program.model.listing.Data#getParent() . 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: ToggleExpandCollapseDataAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Data getClosestComponentDataUnit(ProgramLocation location) {
	if (location == null) {
		return null;
	}

	Data data = DataUtilities.getDataAtLocation(location);

	if (data == null) {
		return null;
	}

	if (data.getNumComponents() > 0) {
		return data;
	}

	return data.getParent();
}
 
Example 2
Source File: ProgramStructureProviderContext.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public ProgramStructureProviderContext(Program program, ProgramLocation loc) {
	this.program = program;

	int dataPath[] = loc.getComponentPath();
	Data data = program.getListing().getDefinedDataContaining(loc.getAddress());
	data = data.getComponent(dataPath);
	this.addr = data.getMinAddress();
	myoffset = data.getParentOffset();
	data = data.getParent();
	struct = (Structure) data.getDataType();
}
 
Example 3
Source File: UnionLocationDescriptor.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Data getParent(Data data) {
	Data parent = data.getParent();
	if (parent == null) {
		return data;
	}

	Data nextParent = getParent(parent);
	if (nextParent != null) {
		return nextParent;
	}
	return data;
}
 
Example 4
Source File: FieldNameFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getFieldName(Data data) {
	Data parent = data.getParent();
	if (parent != null && (parent.getDataType() instanceof Array)) {
		String indexStr = format.prefix +
			Integer.toString(data.getComponentIndex(), format.radix) + format.postfix;
		return "[" + indexStr + "]";
	}
	return data.getFieldName();
}
 
Example 5
Source File: OpenCloseFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Computes if the given data is the last component at its level.
 */
private boolean computeIsLast(Data data) {
	Data parent = data.getParent();
	if (parent != null) {
		Data d2 = parent.getComponent(parent.getNumComponents() - 1);
		if (d2 == data) {
			return true;
		}
	}
	return false;
}
 
Example 6
Source File: OpenCloseFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Computes the sub-component level of the given data object.
 */
private int computeIndentLevel(Data data) {
	int indentLevel = 0;
	while ((data = data.getParent()) != null) {
		indentLevel++;
	}
	return indentLevel;
}
 
Example 7
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 8
Source File: DataAction4Test.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Test
public void testStructureModifyInsideArray() throws Exception {

	// Create structure (length = 0x20)

	makeSelection(0x01006a00, 0x01006a1f);
	//assertNull("No data expected", getContextData());

	doCreateStructureAction();
	clearSelection();

	Data structData = getContextData();
	assertNotNull(structData);
	assertTrue(structData.isStructure());
	DataType structDt = structData.getDataType();

	doAction(CREATE_ARRAY, false);

	final NumberInputDialog dlg1 = waitForDialogComponent(NumberInputDialog.class);
	assertNotNull("Expected element count input dialog", dlg1);

	Runnable r = () -> dlg1.setInput(5);
	runSwing(r);
	waitForPostedSwingRunnables();

	pressButtonByText(dlg1, "OK");

	waitForPostedSwingRunnables();

	Set<DockingActionIf> actions = getActionsByOwner(tool, plugin.getName());
	checkOnArray(actions, structDt, 5);

	// Expand structure
	cb.toggleOpen(getContextData());

	gotoLocation(0x01006a00, new int[] { 0 });

	// Expand structure
	cb.toggleOpen(getContextData());

	gotoLocation(0x01006a00, new int[] { 0, 0 });

	doAction(DEFINE_BYTE, true);

	actions = getActionsByOwner(tool, plugin.getName());
	checkOnDefined(actions, ByteDataType.class);

	gotoLocation(0x01006a01, new int[] { 0, 1 });

	doAction(DEFINE_FLOAT, true);

	actions = getActionsByOwner(tool, plugin.getName());
	checkOnDefined(actions, FloatDataType.class);

	Data pdata = getContextData().getParent();
	assertNotNull(pdata);
	assertTrue(pdata.isStructure());
	Structure struct = (Structure) pdata.getDataType();
	assertEquals(0x20, struct.getLength());
	DataTypeComponent[] structComps = struct.getComponents();
	assertTrue(structComps[0].getDataType() instanceof ByteDataType);
	assertTrue(structComps[1].getDataType() instanceof FloatDataType);

	pdata = pdata.getParent();
	assertNotNull(pdata);
	assertTrue(pdata.isArray());
	assertEquals(5 * 0x20, pdata.getLength());

	assertNull(pdata.getParent());

}