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 Details

    • defaultSettings

      protected Settings defaultSettings
  • Constructor Details

    • 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 Details

    • 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 a Composite may, or may not, be assigned a component length of 0. The method DataTypeComponent.usesZeroLengthComponent(DataType) is used to make this determination.

      Specified by:
      isZeroLength in interface DataType
      Overrides:
      isZeroLength in class AbstractDataType
      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 bitfield
      bitOffset - 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 data type that it has the given parent
      TODO: This method is reserved for internal DB use.
      Specified by:
      addParent in interface DataType
      Overrides:
      addParent in class AbstractDataType
      Parameters:
      dt - parent data type
    • 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 as getLength().
      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 class Object
    • equals

      public final boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getDefaultSettings

      public Settings getDefaultSettings()
      Description copied from interface: DataType
      Gets the settings for this data type. The settings may have underlying default settings and may in turn become defaults for instance-specific settings (e.g., Data or DataTypeComponent). It is important to note that these settings are tied to a specific DataType instantiation so it is important to understand the scope of its use. Example: The BuiltInDataTypeManager has its own set of DataType instances which are separate from those which have been instantiated or resolved to a specific Program/Archive DataTypeManager. Settings manipulation may be disabled by default in some instances.
      Returns:
      the 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:
    • 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 of this DataType as a number of 8-bit bytes.

      For primitive datatypes this reflects the smallest varnode which can be used to contain its value (i.e., raw data length).

      Example: For x86 32-bit gcc an 80-bit long double raw data length of 10-bytes will fit within a floating point register while its aligned-length of 12-bytes is used by the gcc compiler for data/array/component allocations to maintain alignment (i.e., sizeof(long double) ).

      NOTE: Other than the VoidDataType, no datatype should ever return 0, even if DataType.isZeroLength(), and only Dynamic/FactoryDataType datatypes should return -1. If DataType.isZeroLength() is true a length of 1 should be returned. Where a zero-length datatype can be handled (e.g., Composite) the DataType.isZeroLength() method should be used.

      Returns:
      the length of this DataType
    • getAlignedLength

      public int getAlignedLength()
      Description copied from interface: DataType
      Get the aligned-length of this datatype as a number of 8-bit bytes.

      For primitive datatypes this is equivalent to the C/C++ "sizeof" operation within source code and should be used when determining Array element length or component sizing for a Composite. For Pointer, Composite and Array types this will return the same value as DataType.getLength().

      Example: For x86 32-bit gcc an 80-bit long double raw data length of 10-bytes will fit within a floating point register while its aligned-length of 12-bytes is used by the gcc compiler for data/array/component allocations to maintain alignment (i.e., sizeof(long double) ).

      NOTE: Other than the VoidDataType, no datatype should ever return 0, even if DataType.isZeroLength(), and only Dynamic / FactoryDataType / FunctionDefinition datatypes should return -1. If DataType.isZeroLength() is true a length of 1 should be returned.

      Returns:
      byte length of binary encoding.
    • getDescription

      public String getDescription()
      Description copied from interface: DataType
      Get a String briefly describing this DataType.
      Returns:
      a one-liner describing this DataType.
    • getValue

      public Object getValue(MemBuffer buf, Settings settings, int length)
      Description copied from interface: DataType
      Returns the interpreted data value as an instance of the advertised value class.

      For instance, Pointer data types should return an Address object (or null), or integer data types should return a Scalar object.

      Parameters:
      buf - the data buffer
      settings - the settings to use.
      length - indicates the maximum number of bytes that may be consumed by a Dynamic datatype, otherwise this value is ignored. A value of -1 may be specified to allow a Dynamic datatype to determine the length based upon the actual data bytes
      Returns:
      the data object, or null if data is invalid
    • getValueClass

      public Class<?> getValueClass(Settings settings)
      Description copied from interface: DataType
      Get the Class of the value Object to be returned by this datatype (see DataType.getValue(MemBuffer, Settings, int)).
      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 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.
    • 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 String toString()
      Overrides:
      toString in class AbstractDataType