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

The following examples show how to use ghidra.program.model.listing.Data#getLength() . 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: IPCAnalyzer.java    From Ghidra-Switch-Loader with ISC License 7 votes vote down vote up
protected int createPointer(Program program, Address address)
{
    Data d = program.getListing().getDataAt(address);
    
    if (d == null) 
    {
        try 
        {
            d = program.getListing().createData(address, PointerDataType.dataType, 8);
        } 
        catch (CodeUnitInsertionException | DataTypeConflictException e) 
        {
            Msg.error(this, String.format("Failed to create pointer at 0x%X", address.getOffset()), e);
        }
    }
    
    return d.getLength();
}
 
Example 2
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 3
Source File: PortableExecutableBinaryAnalysisCommand.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void processStringTable(Address address) throws Exception {
	Data dwordData = createDWord(address);

	createFragment("StringTable", dwordData.getMinAddress(), dwordData.getLength());

	int usedBytes = dwordData.getLength();
	int totalBytes = getInt(address);

	Address stringAddress = address.add(4);

	while (usedBytes < totalBytes) {
		if (monitor.isCancelled()) {
			break;
		}

		Data stringData = createAsciiString(stringAddress);
		setEOLComment(stringAddress, "");
		createFragment("StringTable", stringData.getMinAddress(), stringData.getLength());

		usedBytes += stringData.getLength();

		stringAddress = stringAddress.add(stringData.getLength());
	}
}
 
Example 4
Source File: CreateStructureInStructureCmd.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
DataType initializeStructureData( Program program, Structure localStructure ){
    
    Data data = program.getListing().getDataContaining( 
        getStructureAddress() );           
    Data comp1 = data.getComponent( fromPath );
    Data comp2 = data.getComponent(toPath);
    int dataLength = (comp2.getParentOffset() + comp2.getLength())
        - comp1.getParentOffset();
    
    DataType parentDataType = comp1.getParent().getBaseDataType();        
    if ( !(parentDataType instanceof Structure) ){
        throw new IllegalArgumentException(
            "Data not in a structure");
    }        
    Structure originalStructure = (Structure) parentDataType;        
    
    // clear and initialize the original structure and then get the new
    // data
    clearStruct(originalStructure, comp1.getParentOffset(), dataLength );
    originalStructure.replace(comp1.getComponentIndex(), 
        localStructure, localStructure.getLength());
    comp1 = data.getComponent( fromPath );
    
    return comp1.getDataType();
}
 
Example 5
Source File: NXProgramBuilder.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
protected int createString(Address address) throws CodeUnitInsertionException, DataTypeConflictException 
{
    Data d = this.program.getListing().getDataAt(address);
    
    if (d == null || !TerminatedStringDataType.dataType.isEquivalent(d.getDataType())) 
    {
        d = this.program.getListing().createData(address, TerminatedStringDataType.dataType, -1);
    }
    
    return d.getLength();
}
 
Example 6
Source File: NXProgramBuilder.java    From Ghidra-Switch-Loader with ISC License 5 votes vote down vote up
protected int createPointer(Address address) throws CodeUnitInsertionException, DataTypeConflictException
{
    NXOAdapter adapter = this.nxo.getAdapter();
    Data d = this.program.getListing().getDataAt(address);
    
    if (d == null || !PointerDataType.dataType.isEquivalent(d.getDataType())) 
    {
        d = this.program.getListing().createData(address, PointerDataType.dataType, adapter.getOffsetSize());
    }
    
    return d.getLength();
}
 
Example 7
Source File: DataMatchProgramCorrelator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private VTMatchInfo generateMatchFromMatchedData(VTMatchSet matchSet, MatchedData matchedData) {

		Address sourceAddress = matchedData.getADataAddress();
		Address destinationAddress = matchedData.getBDataAddress();

		VTScore similarity = new VTScore(1.000);
		VTScore confidence =
			new VTScore(10.0 / (matchedData.getBMatchNum() * matchedData.getAMatchNum()));

		Data sourceData = matchedData.getAData();
		int sourceLength = sourceData.getLength();

		VTMatchInfo matchInfo = new VTMatchInfo(matchSet);

		matchInfo.setSimilarityScore(similarity);
		matchInfo.setConfidenceScore(confidence);
		matchInfo.setSourceLength(sourceLength);
		//yes I meant to put sourceLength here
		// if dest data is defined it has to be same length to get here
		// if not defined, it has to be same length or it wouldn't have matched in the first place
		matchInfo.setSourceAddress(sourceAddress);
		matchInfo.setDestinationLength(sourceLength);
		matchInfo.setDestinationAddress(destinationAddress);
		matchInfo.setTag(null);
		matchInfo.setAssociationType(VTAssociationType.DATA);

		return matchInfo;
	}
 
Example 8
Source File: SimilarDataProgramCorrelator.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private static int getDataLength(Listing listing, Address address) {
	Data data = listing.getDataAt(address);
	return data.getLength();
}