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

The following examples show how to use ghidra.program.model.listing.Data#getNumComponents() . 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: NSArray.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(Data objectData, Program program, TaskMonitor monitor)
		throws CancelledException {
	ReferenceManager referenceManager = program.getReferenceManager();
	for (int i = 0; i < objectData.getNumComponents(); ++i) {
		monitor.checkCanceled();
		Data component = objectData.getComponent(i);
		if (component.getFieldName().startsWith("value")) {
			long value = getValue(component);
			String name = BinaryPropertyListUtil.generateName(value);
			Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, name,
				err -> Msg.error(this, err));
			if (symbol != null) {
				referenceManager.addMemoryReference(component.getMinAddress(),
					symbol.getAddress(), RefType.DATA, SourceType.ANALYSIS, 0);
			}
		}
	}
}
 
Example 2
Source File: NSDictionary.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(Data objectData, Program program, TaskMonitor monitor)
		throws CancelledException {
	ReferenceManager referenceManager = program.getReferenceManager();
	for (int i = 0; i < objectData.getNumComponents(); ++i) {
		monitor.checkCanceled();
		Data component = objectData.getComponent(i);
		if (component.getFieldName().startsWith("key") ||
			component.getFieldName().startsWith("value")) {
			long value = getValue(component);
			String name = BinaryPropertyListUtil.generateName(value);
			Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, name,
				err -> Msg.error(this, err));
			if (symbol != null) {
				referenceManager.addMemoryReference(component.getMinAddress(),
					symbol.getAddress(), RefType.DATA, SourceType.ANALYSIS, 0);
			}
		}
	}
}
 
Example 3
Source File: NSSet.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void markup(Data objectData, Program program, TaskMonitor monitor)
		throws CancelledException {
	ReferenceManager referenceManager = program.getReferenceManager();
	for (int i = 0; i < objectData.getNumComponents(); ++i) {
		monitor.checkCanceled();
		Data component = objectData.getComponent(i);
		if (component.getFieldName().startsWith("value")) {
			long value = getValue(component);
			String name = BinaryPropertyListUtil.generateName(value);
			Symbol symbol = SymbolUtilities.getLabelOrFunctionSymbol(program, name,
				err -> Msg.error(this, err));
			if (symbol != null) {
				referenceManager.addMemoryReference(component.getMinAddress(),
					symbol.getAddress(), RefType.DATA, SourceType.ANALYSIS, 0);
			}
		}
	}
}
 
Example 4
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 5
Source File: CollapseAllDataAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private Data getTopLevelComponentData(ProgramLocation location) {
	if (location == null) {
		return null;
	}

	Address address = location.getAddress();
	if (address == null) {
		return null;
	}

	Program program = provider.getProgram();
	Data topLevelData = program.getListing().getDataContaining(address);

	if (topLevelData == null || topLevelData.getNumComponents() <= 0) {
		return null; // no child data components
	}

	return topLevelData;
}
 
Example 6
Source File: FixArrayStructReferencesScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
protected void run() throws Exception {
	Data data = getDataAt(currentAddress);
	if (data == null) {
		printerr("no data at " + currentAddress);
		return;
	}
	if (!data.isArray()) {
		printerr("data at " + currentAddress + " is not an array");
		return;
	}
	CodeManager codeManager = ((ProgramDB) currentProgram).getCodeManager();
	int numComponents = data.getNumComponents();
	monitor.setMessage("updating data references in array");
	monitor.initialize(numComponents);
	for (int ii = 0; ii < numComponents; ++ii) {
		if (monitor.isCancelled()) {
			printerr("cancelled");
			return;
		}
		monitor.incrementProgress(1);
		Data component = data.getComponent(ii);
		codeManager.updateDataReferences(component);
	}
}
 
Example 7
Source File: ChangeDataSettingsScript.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public void run() throws Exception {
	Data data = getDataAt(currentAddress);
	if (data == null) {
	    println("No data exists.");
	    return;
	}
	int componentCount = data.getNumComponents();
	if (componentCount == 0) {
	    println("Data does not have any sub-components.");
	    return;
	}
	for (int i = 0 ; i < componentCount ; ++i) {
		Data component = data.getComponent( i );
		if ( component == null ) {
			break; // something bad and unexpected has happened
		}
		SettingsDefinition [] settings = component.getDataType().getSettingsDefinitions();
		for (int j = 0; j < settings.length; j++) {
            if (settings[j] instanceof FormatSettingsDefinition) {
                FormatSettingsDefinition format = (FormatSettingsDefinition)settings[j];
                format.setChoice(component, FormatSettingsDefinition.DECIMAL);
            }
        }
	}
}
 
Example 8
Source File: BTreeAnnotationScript.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private void changeEndianSettings(Data data) throws Exception {
	for (int i = 0; i < data.getNumComponents(); ++i) {
		Data component = data.getComponent(i);
		SettingsDefinition[] settings = component.getDataType().getSettingsDefinitions();
		for (int j = 0; j < settings.length; ++j) {
			if (settings[j] instanceof EndianSettingsDefinition) {
				EndianSettingsDefinition setting = (EndianSettingsDefinition) settings[j];
				setting.setBigEndian(component, false);
			}
		}
	}
}
 
Example 9
Source File: ExpandAllDataAction.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private Data getComponentData(ProgramLocation location) {
	if (location == null) {
		return null;
	}
	Data data = DataUtilities.getDataAtLocation(location);

	if (data == null || data.getNumComponents() <= 0) {
		return null; // no expandable data at location
	}

	return data;
}
 
Example 10
Source File: OpenCloseFieldFactory.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the FactoryField for the given object at index index.
 * @param varWidth the amount of variable width spacing for any fields
 * before this one.
 * @param proxy the object whose properties should be displayed.
 */
@Override
public ListingField getField(ProxyObj<?> proxy, int varWidth) {
	Object obj = proxy.getObject();

	if (!enabled) {
		return null;
	}
	boolean canOpen = false;
	int indentLevel = 0;
	boolean isLast = false;
	if (obj instanceof Data) {
		Data data = (Data) obj;
		canOpen = (data.getNumComponents() > 0);
		indentLevel = computeIndentLevel(data);
		isLast = computeIsLast(data);
	}

	if (canOpen) {
		return new OpenCloseField(this, proxy, indentLevel, getMetrics(), startX + varWidth,
			width, isLast);
	}
	else if (indentLevel > 0) {
		return new IndentField(this, proxy, indentLevel, getMetrics(), startX + varWidth, width,
			isLast);
	}
	return null;
}
 
Example 11
Source File: ResourceDataDirectory.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private String setExtraCommentForMenuResource(Data data) throws MemoryAccessException {

		short MF_POPUP = 0x0010;
		short LAST = 0x0090;

		DumbMemBufferImpl buffer = new DumbMemBufferImpl(data.getMemory(), data.getAddress());

		StringBuilder comment = new StringBuilder();
		if (data.getBaseDataType().getName().equals("MenuResource")) {

			//get first structure

			int numComponents = data.getNumComponents();
			boolean topLevel = false;
			for (int i = 0; i < numComponents; i++) {
				DataType dt = data.getComponent(i).getBaseDataType();
				int offset = data.getComponent(i).getRootOffset();

				if (dt.getName().equals("MENUITEM_TEMPLATE_HEADER")) {

					int version = buffer.getShort(offset);
					if (version != 0x0000) {
						return null;
					}

					int menuItemOffset = buffer.getShort(offset + 2);
					if (menuItemOffset < 0) {
						return null;
					}

				}
				if (dt.getName().equals("word")) {
					short option = buffer.getShort(offset);

					if (option == MF_POPUP) {
						topLevel = true; //this type has no mtID to skip
					}
					else if (option == LAST) {
						topLevel = true;
						i++; //skip the mtID
					}
					else {
						topLevel = false;
						i++; //skip the mtID
					}
				}
				if (dt.getName().equals("unicode")) {
					if (topLevel) {
						comment.append("\n");
					}
					else {
						comment.append("  ");
					}

					String menuString = fixupStringRepForDisplay(
						data.getComponentAt(offset).getDefaultValueRepresentation());
					menuString = menuString.replaceAll("\"", "");
					if (menuString.equals("")) {
						comment.append("-------------------\n");
					}
					else {
						comment.append(menuString + "\n");
					}
				}

			}

		}
		return comment.toString();

	}
 
Example 12
Source File: DelayImportDataDirectory.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public void markup(Program program, boolean isBinary, TaskMonitor monitor, MessageLog log,
		NTHeader ntHeader) throws DuplicateNameException, CodeUnitInsertionException,
		DataTypeConflictException, IOException {

   	monitor.setMessage(program.getName()+": delay import(s)...");
	Address addr = PeUtils.getMarkupAddress(program, isBinary, ntHeader, virtualAddress);
	if (!program.getMemory().contains(addr)) {
		return;
	}
	createDirectoryBookmark(program, addr);
	AddressSpace space = program.getAddressFactory().getDefaultAddressSpace();
	for (DelayImportDescriptor descriptor : descriptors) {
		if (monitor.isCancelled()) {
			return;
		}
		//apply descriptor structure
		PeUtils.createData(program, addr, descriptor.toDataType(), log);
		createSymbol(program, addr,
			SymbolUtilities.getAddressAppendedName(DelayImportDescriptor.NAME, addr));

		Data data = program.getListing().getDataAt(addr);
		if (data == null || !data.isDefined()) continue;
		if (data.getNumComponents() < 7) {
			Msg.info(this, "Unexpected data at "+addr);
			continue;
		}

		//create string for descriptor dll name
		Address tmpAddr = addr(space, isBinary, descriptor, descriptor.getPointerToDLLName());
		createTerminatedString(program, tmpAddr, true, log);

		tmpAddr = addr(space, isBinary, descriptor, descriptor.getAddressOfModuleHandle());
		createSymbol(program, tmpAddr, SymbolUtilities.getAddressAppendedName(
			DelayImportDescriptor.NAME + "_Module_Handle", tmpAddr));

		tmpAddr = addr(space, isBinary, descriptor, descriptor.getAddressOfIAT());
		createSymbol(program, tmpAddr, SymbolUtilities.getAddressAppendedName(
			DelayImportDescriptor.NAME + "_IAT", tmpAddr));
		markupThunk(program, isBinary, space, descriptor, descriptor.getAddressOfIAT(),
			descriptor.getThunksIAT(), monitor, log);

		tmpAddr = addr(space, isBinary, descriptor, descriptor.getAddressOfINT());
		createSymbol(program, tmpAddr, SymbolUtilities.getAddressAppendedName(
			DelayImportDescriptor.NAME + "_INT", tmpAddr));
		markupThunk(program, isBinary, space, descriptor, descriptor.getAddressOfINT(),
			descriptor.getThunksINT(), monitor, log);

		// This table is optional
		if (descriptor.getAddressOfBoundIAT() != 0) {
			tmpAddr = addr(space, isBinary, descriptor, descriptor.getAddressOfBoundIAT());
			createSymbol(program, tmpAddr, SymbolUtilities.getAddressAppendedName(
				DelayImportDescriptor.NAME + "_Bound_IAT", tmpAddr));
			markupThunk(program, isBinary, space, descriptor, descriptor.getAddressOfBoundIAT(),
				descriptor.getThunksBoundIAT(), monitor, log);
		}

		// This table is optional
		if (descriptor.getAddressOfOriginalIAT() != 0) {
			tmpAddr = addr(space, isBinary, descriptor, descriptor.getAddressOfOriginalIAT());
			createSymbol(program, tmpAddr, SymbolUtilities.getAddressAppendedName(
				DelayImportDescriptor.NAME + "_Unload_IAT", tmpAddr));
			markupThunk(program, isBinary, space, descriptor,
				descriptor.getAddressOfOriginalIAT(), descriptor.getThunksUnloadIAT(), monitor,
				log);
		}


		markupImportByName(program, isBinary, space, descriptor, monitor, log);

		addr = addr.add(descriptor.sizeof());
	}
   }