Java Code Examples for ghidra.program.model.data.DataType#getName()

The following examples show how to use ghidra.program.model.data.DataType#getName() . 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: AnnotationElement.java    From ghidra with Apache License 2.0 6 votes vote down vote up
@Override
public DataType toDataType( ) throws DuplicateNameException, IOException {
	DataType encodeValueDataType = value.toDataType( );

	String name = "annotation_element" + "_" + nameIndexLength + "_" + encodeValueDataType.getName( );

	Structure structure = new StructureDataType( name, 0 );

	structure.add( new ArrayDataType( BYTE, nameIndexLength, BYTE.getLength( ) ), "nameIndex", null );

	structure.add( encodeValueDataType, "value", null );

	structure.setCategoryPath( new CategoryPath( "/dex/annotation_element" ) );
	// try {
	// structure.setName( name + "_" + structure.getLength( ) );
	// }
	// catch ( Exception e ) {
	// // ignore
	// }
	return structure;
}
 
Example 2
Source File: FindReferencesToDataTypeAction.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private void updateMenuName(DataType type) {

		if (type == null) {
			return; // not sure if this can happen
		}

		String typeName = type.getName();
		String menuName = "Find Uses of " + typeName;

		String fieldName = getDataTypeField();
		if (fieldName != null) {
			menuName += '.' + fieldName;
		}

		MenuData data = getPopupMenuData().cloneData();
		data.setMenuPath(new String[] { LocationReferencesService.MENU_GROUP, menuName });
		setPopupMenuData(data);
	}
 
Example 3
Source File: CodeUnitTableCellData.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private String getDataPath(Data data) {
	String path = data.getComponentPathName();
	int dotIndex = path.indexOf(".");
	if (dotIndex != -1) {
		path = path.substring(dotIndex + 1);
	}

	Data parent = data.getParent();
	DataType parentType = parent.getDataType();

	String separator = ".";
	if (parentType instanceof Array) {
		separator = "";
	}

	return " (" + parentType.getName() + separator + path + ")";
}
 
Example 4
Source File: PreviewTableCellData.java    From ghidra with Apache License 2.0 6 votes vote down vote up
private String getDataPath(Data data) {
	String path = data.getComponentPathName();
	int dotIndex = path.indexOf(".");
	if (dotIndex != -1) {
		path = path.substring(dotIndex + 1);
	}

	Data parent = data.getParent();
	DataType parentType = parent.getDataType();

	String separator = ".";
	if (parentType instanceof Array) {
		separator = "";
	}

	return " (" + parentType.getName() + separator + path + ")";
}
 
Example 5
Source File: DataTypeStringable.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public DataTypeStringable(DataType dataType, DataTypeManager dataTypeManager, int length) {
	super(SHORT_NAME);
	UniversalID universalID = dataTypeManager.getUniversalID();
	this.managerUniversalID = universalID.getValue();
	this.dataTypeID = dataTypeManager.getID(dataType);
	this.dataTypeName = dataType.getName();
	this.length = length;
}
 
Example 6
Source File: AbstractExternalMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param parameter
 * @param expectedDataType
 */
void checkParameterDataType(Parameter parameter, DataType expectedDataType) {
	DataType parameterDataType = parameter.getDataType();
	String failureMessage =
		"Expected data type '" + expectedDataType.getName() + "' for parameter " +
			(parameter.getOrdinal() + 1) + " but was '" + parameterDataType.getName() + "'";
	assertTrue(failureMessage, expectedDataType.isEquivalent(parameterDataType));
}
 
Example 7
Source File: AbstractExternalMergerTest.java    From ghidra with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param function
 * @param expectedDataType
 */
void checkFunctionReturnType(Function function, DataType expectedDataType) {
	DataType functionReturnType = function.getReturnType();
	String failureMessage = "Expected return type '" + expectedDataType.getName() +
		"' but was '" + functionReturnType.getName() + "'";
	assertTrue(failureMessage, expectedDataType.isEquivalent(functionReturnType));
}
 
Example 8
Source File: LocalVariableInfo.java    From ghidra with Apache License 2.0 5 votes vote down vote up
static LocalVariableInfo createLocalVariableInfo(String localVariableInfoString, Program program) {

		try {
			StringTokenizer tokenizer =
				new StringTokenizer(localVariableInfoString, Stringable.DELIMITER);
			tokenizer.nextToken(); // the first element is the class name

			long managerUniversalID = Long.parseLong(tokenizer.nextToken());
			long dataTypeID = Long.parseLong(tokenizer.nextToken());
			String dataTypeName = tokenizer.nextToken();

			DataType dt = getDataType(program, managerUniversalID, dataTypeID);
			if (dt == null || !dt.getName().equals(dataTypeName)) {
				throw new AssertException("Data type name/ID mismatch " + dt.getName() +
					" doesn't match " + dataTypeName + ".");
			}

			int firstUseOffset = Integer.parseInt(tokenizer.nextToken());
			String localVariableName = tokenizer.nextToken();
			SourceType sourceType = SourceType.valueOf(tokenizer.nextToken());
			String comment = tokenizer.nextToken();

			VariableStorage storage = VariableStorage.deserialize(program, tokenizer.nextToken());

			LocalVariableInfo localVarInfo =
				new LocalVariableInfo(localVariableName, firstUseOffset, dt, storage, program,
					sourceType);
			localVarInfo.setComment(comment);

			return localVarInfo;
		}
		catch (Exception e) {
			throw new AssertException("Failed to deserialize local variable (" +
				localVariableInfoString + "): " + e.getMessage());
		}
	}
 
Example 9
Source File: DataAction2Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllDefaultStructDataSettings() throws Exception {

	List<DataType> builtIns = getBuiltInDataTypesAsFavorites();
	for (DataType type : builtIns) {
		String actionName = "Define " + type.getName();
		manipulateAllSettings(true, true, false, actionName);
		manipulateAllSettings(true, true, true, actionName);
	}
}
 
Example 10
Source File: DataAction1Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllStructDataSettings() throws Exception {

	List<DataType> builtIns = getBuiltInDataTypesAsFavorites();
	for (DataType type : builtIns) {
		String actionName = "Define " + type.getName();
		manipulateAllSettings(false, true, false, actionName);
		manipulateAllSettings(true, true, true, actionName);
	}
}
 
Example 11
Source File: DataAction3Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllDataSettings() throws Exception {

	List<DataType> builtIns = getBuiltInDataTypesAsFavorites();
	for (DataType type : builtIns) {
		String actionName = "Define " + type.getName();
		manipulateAllSettings(false, false, false, actionName);
	}
}
 
Example 12
Source File: DataAction3Test.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Test
public void testAllDefaultDataSettings() throws Exception {

	List<DataType> builtIns = getBuiltInDataTypesAsFavorites();
	for (DataType type : builtIns) {
		String actionName = "Define " + type.getName();
		manipulateAllSettings(true, false, false, actionName);
	}
}
 
Example 13
Source File: DefaultDataTypeHTMLRepresentation.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private String getArrayDescription(Array array) {
	DataType baseDataType = array.getDataType();
	if (baseDataType instanceof Array) {
		return getArrayDescription((Array) baseDataType);
	}

	return "Array of " + baseDataType.getName();
}
 
Example 14
Source File: DefaultDataTypeHTMLRepresentation.java    From ghidra with Apache License 2.0 5 votes vote down vote up
private TextLine buildHeader(DataType dataType) {
	if (dataType instanceof Array) {
		Array array = (Array) dataType;
		return new TextLine(getArrayDescription(array));
	}

	String description = dataType.getDescription();
	if (description == null || description.length() == 0) {
		return new TextLine(dataType.getName());
	}
	return new TextLine(description);
}
 
Example 15
Source File: DataTypeComparator.java    From ghidra with Apache License 2.0 5 votes vote down vote up
public int compare(DataType dt1, DataType dt2) {
        String name1 = dt1.getName();
        String name2 = dt2.getName();

// TODO: should built-ins always come first in the list? (in case we have an 'a' named archive?)        
        
        // if the names are the same, then sort by the path            
        if ( name1.equalsIgnoreCase( name2 ) ) {

            if ( !name1.equals( name2 ) ) {
                // let equivalent names be sorted by case ('-' for lower-case first)
                return -name1.compareTo( name2 );
            }
            
            String dtmName1 = dt1.getDataTypeManager().getName();
            String dtmName2 = dt2.getDataTypeManager().getName();

            // if they have the same name, and are in the same DTM, then compare paths
            if ( dtmName1.equalsIgnoreCase( dtmName2 ) ) {
                return dt1.getPathName().compareToIgnoreCase( dt2.getPathName() );
            }

            return dtmName1.compareToIgnoreCase( dtmName2 );
        }

        return name1.compareToIgnoreCase( name2 );
    }
 
Example 16
Source File: Ext4Inode.java    From ghidra with Apache License 2.0 5 votes vote down vote up
@Override
public DataType toDataType() throws DuplicateNameException, IOException {
	DataType iBlockDataType = i_block.toDataType();
	Structure structure = new StructureDataType("ext4_inode_"+iBlockDataType.getName( ), 0);
	structure.add(WORD, "i_mode", null);
	structure.add(WORD, "i_uid", null);
	structure.add(DWORD, "i_size_lo", null);
	structure.add(DWORD, "i_atime", null);
	structure.add(DWORD, "i_ctime", null);
	structure.add(DWORD, "i_mtime", null);
	structure.add(DWORD, "i_dtime", null);
	structure.add(WORD, "i_gid", null);
	structure.add(WORD, "i_links_count", null);
	structure.add(DWORD, "i_blocks_lo", null);
	structure.add(DWORD, "i_flags", null);
	structure.add(DWORD, "i_osd1", null);
	structure.add(iBlockDataType, "i_block", null);
	structure.add(DWORD, "i_generation", null);
	structure.add(DWORD, "i_file_acl_lo", null);
	structure.add(DWORD, "i_size_high", null);
	structure.add(DWORD, "i_obso_faddr", null);
	structure.add(new ArrayDataType(BYTE, 12, BYTE.getLength()), "i_osd2", null); //12 bytes long
	structure.add(WORD, "i_extra_isize", null);
	structure.add(WORD, "i_checksum_hi", null);
	structure.add(DWORD, "i_ctime_extra", null);
	structure.add(DWORD, "i_mtime_extra", null);
	structure.add(DWORD, "i_atime_extra", null);
	structure.add(DWORD, "i_crtime", null);
	structure.add(DWORD, "i_crtime_extra", null);
	structure.add(DWORD, "i_version_hi", null);
	structure.add(DWORD, "i_projid", null);
	return structure;
}
 
Example 17
Source File: AbstractExternalMergerTest.java    From ghidra with Apache License 2.0 4 votes vote down vote up
void checkDataType(DataType expectedDataType, DataType actualDataType) {
	String failureMessage = "Expected external data type '" + expectedDataType.getName() +
		"' but was '" + ((actualDataType != null) ? actualDataType.getName() : null) + "'";
	assertTrue(failureMessage, expectedDataType.isEquivalent(actualDataType));
}
 
Example 18
Source File: DataTypeColumnTypeMapper.java    From ghidra with Apache License 2.0 4 votes vote down vote up
@Override
public String convert(DataType dataType) {
	return dataType.getName();
}
 
Example 19
Source File: ObjectiveC2_DecompilerMessageAnalyzer.java    From ghidra with Apache License 2.0 4 votes vote down vote up
private String getNameFromOffset(Program program, long offset, Varnode input, boolean isClass,
		boolean isMethod) {
	String name;
	Address address = getAddressInProgram(program, offset);
	if (address == null) {
		return null;
	}
	MemoryBlock block = program.getMemory().getBlock(address);
	if (block == null) {
		return null;
	}

	if (isIvarBlock(block) || isObjcConstBlock(block)) {
		name = getIvarName(program, address);
	}
	else if (isMessageRefsBlock(block)) {
		name = getFixupMethodName(program, address);
	}
	else if (isCFStringBlock(block)) {
		name = getCFString(program, address);
		if (name != null) {
			if (name.startsWith("\\")) {
				name = "\\" + name;
			}
			name = "\"" + name + "\"";
		}
	}
	else if (isDataBlock(block)) {
		name = getDataName(program, address);
		if (name != null) {
			if (name.startsWith("\\")) {
				name = "\\" + name;
			}
			name = "\"" + name + "\"";
		}
	}
	else {
		Data nameData = program.getListing().getDataAt(address);
		if (nameData == null) {
			Function function = program.getListing().getFunctionAt(address);
			if (function != null && !function.getName().contains("_objc_msgSend")) {
				DataType returnType = function.getReturnType();
				name = returnType.getName();
				return name;
			}
			return null;
		}
		name = getNameFromData(program, input, isClass, isMethod, address, nameData);
	}
	return name;
}
 
Example 20
Source File: FunctionUtils.java    From ghidra with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a FieldStringInfo object for the given function's return type.  This info contains
 * the return type string and its location in the function signature.
 * 
 * @param function The function from which to get the return type.
 * @param functionSignatureString The function signature string from which to get the return
 * type string.
 * @return Returns a FieldStringInfo object for the given function's return type.
 */
public static FieldStringInfo getFunctionReturnTypeStringInfo(Function function,
		String functionSignatureString) {
	DataType returnType = function.getReturnType();
	return new FieldStringInfo(functionSignatureString, returnType.getName(), 0);
}