Class BitFieldDataType
- java.lang.Object
-
- ghidra.program.model.data.AbstractDataType
-
- ghidra.program.model.data.BitFieldDataType
-
- All Implemented Interfaces:
DataType
public class BitFieldDataType extends AbstractDataType
BitFieldDataType
provides a means of defining a minimally sized bit-field for use within data structures. The length (i.e., storage size) of this bitfield datatype is the minimum number of bytes required to contain the bitfield at its specified offset. The effective bit-size of a bitfield will be limited by the size of the base datatype whose size may be controlled by its associated datatype manager and data organization (e.g.,IntegerDataType
).NOTE: Instantiation of this datatype implementation is intended for internal use only. Creating and manipulating bitfields should be accomplished directly via Structure or Union bitfield methods.
-
-
Field Summary
Fields Modifier and Type Field Description protected Settings
defaultSettings
-
Fields inherited from class ghidra.program.model.data.AbstractDataType
categoryPath, dataMgr, name
-
Fields inherited from interface ghidra.program.model.data.DataType
CONFLICT_SUFFIX, DEFAULT, NO_LAST_CHANGE_TIME, NO_SOURCE_SYNC_TIME, VOID
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
BitFieldDataType(DataType baseDataType, int bitSize)
Construct a bit-field type based upon a supported baseDataType.protected
BitFieldDataType(DataType baseDataType, int bitSize, int bitOffset)
Construct a bit-field type based upon a specified base type.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addParent(DataType dt)
Inform this datatype that it has the given parentstatic void
checkBaseDataType(DataType baseDataType)
Check a bitfield base datatypeBitFieldDataType
clone(DataTypeManager dtm)
Clone this bitfield to a new datatype manager.DataType
copy(DataTypeManager dtm)
Returns a clone of this built-in DataTypeboolean
equals(java.lang.Object obj)
int
getAlignment()
Gets the alignment to be used when aligning this datatype within another datatype.DataType
getBaseDataType()
Get the base datatype associated with this bit-field (e.g., int, long, etc., or TypeDef to supported base type)int
getBaseTypeSize()
Get the size of the base data type based upon the associated data organization.int
getBitOffset()
Get the bit offset of the least-significant bit relative to bit-0 of the base datatype (i.e., least significant bit).int
getBitSize()
Get the effective bit size of this bit-field which may not exceed the size of the base datatype.int
getDeclaredBitSize()
Get the declared bit size of this bit-field which may be larger than the effective size which could be truncated.Settings
getDefaultSettings()
Gets the default settings for this datatype.java.lang.String
getDescription()
Get a String briefly describing this DataType.static int
getEffectiveBitSize(int declaredBitSize, int baseTypeByteSize)
Get the effective bit-size based upon the specified base type size.int
getLength()
Get the length (number of 8-bit bytes) of this DataType.static int
getMinimumStorageSize(int bitSize)
Get the minimum storage size in bytes for a given size in bits.static int
getMinimumStorageSize(int bitSize, int bitOffset)
Get the minimum storage size in bytes for a given size in bits with the specified bitOffset (lsb position within big endian storage)AbstractIntegerDataType
getPrimitiveBaseDataType()
Get the base datatype associated with this bit-field (e.g., int, long, etc., or TypeDef to supported base type)java.lang.String
getRepresentation(MemBuffer buf, Settings settings, int length)
Get bytes from memory in a printable format for this type.SettingsDefinition[]
getSettingsDefinitions()
Gets a list of all the settingsDefinitions used by this datatype.int
getStorageSize()
Get the packing storage size in bytes associated with this bit-field which may be larger than the base type associated with the fields original definition.java.lang.Object
getValue(MemBuffer buf, Settings settings, int length)
Get the data in the form of the appropriate Object for this DataType.java.lang.Class<?>
getValueClass(Settings settings)
Get the Class of the value to be returned by this datatype.int
hashCode()
boolean
isEquivalent(DataType dt)
Check if the given datatype is equivalent to this datatype.static boolean
isValidBaseDataType(DataType baseDataType)
Check if a specified baseDataType is valid for use with a bitfieldboolean
isZeroLength()
Indicates this datatype is defined with a zero length.void
setDefaultSettings(Settings settings)
Set the default settings for this datatype.java.lang.String
toString()
-
Methods inherited from class ghidra.program.model.data.AbstractDataType
dataTypeAlignmentChanged, dataTypeDeleted, dataTypeNameChanged, dataTypeReplaced, dataTypeSizeChanged, dependsOn, encodeRepresentation, encodeValue, getCategoryPath, getDataOrganization, getDataTypeManager, getDataTypePath, getDefaultAbbreviatedLabelPrefix, getDefaultLabelPrefix, getDefaultLabelPrefix, getDefaultOffcutLabelPrefix, getDisplayName, getDocs, getLastChangeTime, getLastChangeTimeInSourceArchive, getMnemonic, getName, getParents, getPathName, getSourceArchive, getUniversalID, hasLanguageDependantLength, isDeleted, isEncodable, isNotYetDefined, removeParent, replaceWith, setCategoryPath, setDescription, setLastChangeTime, setLastChangeTimeInSourceArchive, setName, setNameAndCategory, setSourceArchive
-
-
-
-
Field Detail
-
defaultSettings
protected Settings defaultSettings
-
-
Constructor Detail
-
BitFieldDataType
protected BitFieldDataType(DataType baseDataType, int bitSize, int bitOffset) throws InvalidDataTypeException
Construct a bit-field type based upon a specified base type. The baseDataType will take precedence if specified. Either baseType or baseDatatype must be specified.- Parameters:
baseDataType
- base data type (integer/enum type or typedef to same). This bitfield will adopt the same datatype manager as this base type.bitSize
- size of bit-field expressed as number of bits (0..255). The effective bit size may be reduced based upon the specified base datatype size.bitOffset
- right shift factor within storage unit when viewed as a big-endian dd scalar value. Based upon minimal storage bitOffset should be in the range 0 to 7.- Throws:
InvalidDataTypeException
-
BitFieldDataType
protected BitFieldDataType(DataType baseDataType, int bitSize) throws InvalidDataTypeException
Construct a bit-field type based upon a supported baseDataType.- Parameters:
baseDataType
- a supported primitive integer data type or TypeDef to such a type. The baseType must already be cloned to the target datatype manager.bitSize
- size of bit-field expressed as number of bits- Throws:
InvalidDataTypeException
- if specified baseDataType is not permitted
-
-
Method Detail
-
isZeroLength
public boolean isZeroLength()
Description copied from interface:DataType
Indicates this datatype is defined with a zero length.This method should not be confused with
DataType.isNotYetDefined()
which indicates that nothing but the name and basic type is known.NOTE: a zero-length datatype must return a length of 1 via
DataType.getLength()
. Zero-length datatypes used as a component within aComposite
may, or may not, be assigned a component length of 0. The methodDataTypeComponent.usesZeroLengthComponent(DataType)
is used to make this determination.- Specified by:
isZeroLength
in interfaceDataType
- Overrides:
isZeroLength
in classAbstractDataType
- Returns:
- true if type definition has a length of 0, else false
-
getEffectiveBitSize
public static int getEffectiveBitSize(int declaredBitSize, int baseTypeByteSize)
Get the effective bit-size based upon the specified base type size. A bit size larger than the base type size will truncated to the base type size.- Parameters:
declaredBitSize
-baseTypeByteSize
-- Returns:
- effective bit-size
-
getMinimumStorageSize
public static int getMinimumStorageSize(int bitSize)
Get the minimum storage size in bytes for a given size in bits. This does not consider the bit offset which may increase the required storage.- Parameters:
bitSize
- number of bits within bitfield- Returns:
- minimum storage size in bytes
-
getMinimumStorageSize
public static int getMinimumStorageSize(int bitSize, int bitOffset)
Get the minimum storage size in bytes for a given size in bits with the specified bitOffset (lsb position within big endian storage)- Parameters:
bitSize
- number of bits within bitfieldbitOffset
- normalized bitfield offset within storage (lsb)- Returns:
- minimum storage size in bytes
-
checkBaseDataType
public static void checkBaseDataType(DataType baseDataType) throws InvalidDataTypeException
Check a bitfield base datatype- Parameters:
baseDataType
- bitfield base data type (Enum, AbstractIntegerDataType and derived TypeDefs permitted)- Throws:
InvalidDataTypeException
- if baseDataType is invalid as a bitfield base type.
-
isValidBaseDataType
public static boolean isValidBaseDataType(DataType baseDataType)
Check if a specified baseDataType is valid for use with a bitfield- Parameters:
baseDataType
- bitfield base data type (Enum, AbstractIntegerDataType and derived TypeDefs permitted)- Returns:
- true if baseDataType is valid else false
-
addParent
public void addParent(DataType dt)
Description copied from interface:DataType
Inform this datatype that it has the given parentTODO: This method is reserved for internal DB use.
- Specified by:
addParent
in interfaceDataType
- Overrides:
addParent
in classAbstractDataType
- Parameters:
dt
- parent datatype
-
getBaseTypeSize
public int getBaseTypeSize()
Get the size of the base data type based upon the associated data organization.- Returns:
- base type size
-
getStorageSize
public int getStorageSize()
Get the packing storage size in bytes associated with this bit-field which may be larger than the base type associated with the fields original definition. Returned value is the same asgetLength()
.- Returns:
- packing storage size in bytes
-
getBitSize
public int getBitSize()
Get the effective bit size of this bit-field which may not exceed the size of the base datatype.- Returns:
- bit size
-
getDeclaredBitSize
public int getDeclaredBitSize()
Get the declared bit size of this bit-field which may be larger than the effective size which could be truncated.- Returns:
- bit size as defined by the field construction/declaration.
-
getBitOffset
public int getBitOffset()
Get the bit offset of the least-significant bit relative to bit-0 of the base datatype (i.e., least significant bit). This corresponds to the right-shift amount within the base data type when viewed as a big-endian value.- Returns:
- bit offset
-
getBaseDataType
public DataType getBaseDataType()
Get the base datatype associated with this bit-field (e.g., int, long, etc., or TypeDef to supported base type)- Returns:
- base data type
-
getPrimitiveBaseDataType
public AbstractIntegerDataType getPrimitiveBaseDataType()
Get the base datatype associated with this bit-field (e.g., int, long, etc., or TypeDef to supported base type)- Returns:
- base data type
-
getSettingsDefinitions
public final SettingsDefinition[] getSettingsDefinitions()
Gets a list of all the settingsDefinitions used by this datatype.- Returns:
- a list of the settingsDefinitions used by this datatype.
-
isEquivalent
public final boolean isEquivalent(DataType dt)
Description copied from interface:DataType
Check if the given datatype is equivalent to this datatype.The precise meaning of "equivalent" is datatype dependent.
NOTE: if invoked by a DB object or manager it should be invoked on the DataTypeDB object passing the other datatype as the argument.- Parameters:
dt
- the datatype being tested for equivalence.- Returns:
- true if the if the given datatype is equivalent to this datatype.
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public final boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
getDefaultSettings
public Settings getDefaultSettings()
Description copied from interface:DataType
Gets the default settings for this datatype.- Returns:
- the default settings for this datatype.
-
copy
public final DataType copy(DataTypeManager dtm)
Returns a clone of this built-in DataType- Parameters:
dtm
- the data-type manager instance whose data-organization should apply.- Returns:
- new instanceof of this datatype
- See Also:
DataType.copy(ghidra.program.model.data.DataTypeManager)
-
clone
public BitFieldDataType clone(DataTypeManager dtm)
Clone this bitfield to a new datatype manager. This may change the effective bit size and storage size of the resulting datatype based upon the data organization of the specified dtm.- Parameters:
dtm
- target datatype manager- Returns:
- new instance or same instance of dtm is unchanged.
-
getLength
public int getLength()
Description copied from interface:DataType
Get the length (number of 8-bit bytes) of this DataType.NOTE: No datatype should ever return 0, even if
DataType.isZeroLength()
, and onlyDynamic
datatypes should return -1. IfDataType.isZeroLength()
is true a length of 1 should be returned. Where a zero-length datatype can be handled (e.g.,Composite
) theDataType.isZeroLength()
method should be used.- Returns:
- the length of this DataType
-
getDescription
public java.lang.String getDescription()
Description copied from interface:DataType
Get a String briefly describing this DataType.- Returns:
- a one-liner describing this DataType.
-
getValue
public java.lang.Object getValue(MemBuffer buf, Settings settings, int length)
Description copied from interface:DataType
Get the data in the form of the appropriate Object for this DataType.For instance if the datatype is an AddressDT, return an Address object. a Byte, return a Scalar* (maybe this should be a Byte) a Float, return a Float
- Parameters:
buf
- the data buffer.settings
- the settings to use.length
- the number of bytes to get the value from.- Returns:
- the data Object.
-
getValueClass
public java.lang.Class<?> getValueClass(Settings settings)
Description copied from interface:DataType
Get the Class of the value to be returned by this datatype.- Parameters:
settings
- the relevant settings to use or null for default.- Returns:
- Class of the value to be returned by this datatype or null if it can vary or is unspecified. Types which correspond to a string or char array will return the String class.
-
getRepresentation
public java.lang.String getRepresentation(MemBuffer buf, Settings settings, int length)
Description copied from interface:DataType
Get bytes from memory in a printable format for this type.- Parameters:
buf
- the data.settings
- the settings to use for the representation.length
- the number of bytes to represent.- Returns:
- the representation of the data in this format, never null.
-
setDefaultSettings
public void setDefaultSettings(Settings settings)
Description copied from interface:DataType
Set the default settings for this datatype.TODO: This method is reserved for internal DB use.
- Parameters:
settings
- the settings to be used as this dataTypes default settings.
-
getAlignment
public int getAlignment()
Description copied from interface:DataType
Gets the alignment to be used when aligning this datatype within another datatype.- Returns:
- this datatype's alignment.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classAbstractDataType
-
-