Class AbstractStringDataType

All Implemented Interfaces:
BuiltInDataType, DataType, DataTypeWithCharset, Dynamic, ExtensionPoint
Direct Known Subclasses:
PascalString255DataType, PascalStringDataType, PascalUnicodeDataType, StringDataType, StringUTF8DataType, TerminatedStringDataType, TerminatedUnicode32DataType, TerminatedUnicodeDataType, Unicode32DataType, UnicodeDataType

public abstract class AbstractStringDataType extends BuiltIn implements Dynamic, DataTypeWithCharset
Common base class for all Ghidra string DataTypes.

See StringDataType for information about string variations and configuration details.

Sub-classes generally only need to implement a constructor that calls the mega-constructor AbstractStringDataType.AbstractStringDataType(lots,of,params) and the DataType.clone(DataTypeManager) method.

  • Field Details

    • COMMON_STRING_SETTINGS_DEFS

      public static final SettingsDefinition[] COMMON_STRING_SETTINGS_DEFS
    • COMMON_WITH_CHARSET_STRING_SETTINGS_DEFS

      public static final SettingsDefinition[] COMMON_WITH_CHARSET_STRING_SETTINGS_DEFS
    • DEFAULT_UNICODE_LABEL

      public static final String DEFAULT_UNICODE_LABEL
      See Also:
    • DEFAULT_UNICODE_LABEL_PREFIX

      public static final String DEFAULT_UNICODE_LABEL_PREFIX
      See Also:
    • DEFAULT_UNICODE_ABBREV_PREFIX

      public static final String DEFAULT_UNICODE_ABBREV_PREFIX
      See Also:
    • DEFAULT_LABEL

      public static final String DEFAULT_LABEL
      See Also:
    • DEFAULT_LABEL_PREFIX

      public static final String DEFAULT_LABEL_PREFIX
      See Also:
    • DEFAULT_ABBREV_PREFIX

      public static final String DEFAULT_ABBREV_PREFIX
      See Also:
    • USE_CHARSET_DEF_DEFAULT

      public static final String USE_CHARSET_DEF_DEFAULT
      A symbolic name to signal that the null value being passed for the charset name param indicates that the default charset (ie. ASCII) should be used.
  • Constructor Details

    • AbstractStringDataType

      protected AbstractStringDataType(String name, String mnemonic, String defaultLabel, String defaultLabelPrefix, String defaultAbbrevLabelPrefix, String description, String charsetName, DataType replacementDataType, StringLayoutEnum stringLayout, DataTypeManager dtm)
      Protected constructor used by derived types to provide all their datatype details.

      Parameters:
      name - Name of this datatype
      mnemonic - Mnemonic of this datatype
      defaultLabel - Label string for this datatype. See defaultLabel.
      defaultLabelPrefix - Label prefix string for this datatype. See defaultLabelPrefix.
      defaultAbbrevLabelPrefix - Abbreviated label prefix for this datatype. See defaultAbbrevLabelPrefix.
      description - Description of this datatype.
      charsetName - Charset name for this string datatype. If null the settings of the data instance will be queried for a charset.
      replacementDataType - Replacement DataType.
      stringLayout - stringLayout controls how the string is laid out in memory.
      dtm - DataTypeManager for this datatype, null ok.
  • Method Details

    • getMnemonic

      public String getMnemonic(Settings settings)
      Description copied from interface: DataType
      Get the mnemonic for this DataType.
      Specified by:
      getMnemonic in interface DataType
      Overrides:
      getMnemonic in class AbstractDataType
      Parameters:
      settings - settings which may influence the result or null
      Returns:
      the mnemonic for this DataType.
    • getDefaultLabelPrefix

      public String getDefaultLabelPrefix()
      Description copied from interface: DataType
      Returns the appropriate string to use as the default label prefix in the absence of any data.
      Specified by:
      getDefaultLabelPrefix in interface DataType
      Overrides:
      getDefaultLabelPrefix in class AbstractDataType
      Returns:
      the default label prefix or null if none specified.
    • getDefaultAbbreviatedLabelPrefix

      public String getDefaultAbbreviatedLabelPrefix()
      Description copied from interface: DataType
      Returns the prefix to use for this datatype when an abbreviated prefix is desired.

      For example, some datatypes will built a large default label, at which is is more desirable to have a shortened prefix.

      Specified by:
      getDefaultAbbreviatedLabelPrefix in interface DataType
      Overrides:
      getDefaultAbbreviatedLabelPrefix in class AbstractDataType
      Returns:
      the prefix to use for this datatype when an abbreviated prefix is desired. May return null.
    • getDescription

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

      protected SettingsDefinition[] getBuiltInSettingsDefinitions()
      Description copied from class: BuiltIn
      Gets a list of all the settingsDefinitions used by this datatype.
      Overrides:
      getBuiltInSettingsDefinitions in class BuiltIn
      Returns:
      a list of the settingsDefinitions used by this datatype.
    • getStringDataInstance

      public StringDataInstance getStringDataInstance(MemBuffer buf, Settings settings, int length)
      Creates a new StringDataInstance using the bytes in the supplied MemBuffer and options provided by this DataType.

      Parameters:
      buf - the data.
      settings - the settings to use for the representation.
      length - the number of bytes to represent.
      Returns:
      a new StringDataInstance, never null.
    • getStringLayout

      public StringLayoutEnum getStringLayout()
      Returns:
      StringLayoutEnum settinEnum stringLayoutype.
    • getCharsetName

      public String getCharsetName(Settings settings)
      Description copied from interface: DataTypeWithCharset
      Get the character set for a specific data type and settings
      Specified by:
      getCharsetName in interface DataTypeWithCharset
      Parameters:
      settings - data instance settings
      Returns:
      Charset for this datatype and settings
    • 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.

      Specified by:
      getLength in interface DataType
      Returns:
      the length of this DataType
    • canSpecifyLength

      public boolean canSpecifyLength()
      Description copied from interface: Dynamic
      Determine if the length may be specified for an instanceof this datatype (e.g., Data, Array, DataTypeComponent, etc.).
      Specified by:
      canSpecifyLength in interface Dynamic
      Returns:
      true if a user-specified length can be used, else false
    • getLength

      public int getLength(MemBuffer buf, int maxLength)
      Description copied from interface: Dynamic
      Compute the length for this data-type which corresponds to the specified memory location.
      Specified by:
      getLength in interface Dynamic
      Parameters:
      buf - memory location
      maxLength - maximum number of bytes to consume in computing length, or -1 for unspecified.
      Returns:
      data length or -1 if it could not be determined. Returned length may exceed maxLength if data-type does not supported constrained lengths.
    • 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)).
      Specified by:
      getValueClass in interface DataType
      Overrides:
      getValueClass in class DataTypeImpl
      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.
    • isEncodable

      public boolean isEncodable()
      Description copied from interface: DataType
      Check if this type supports encoding (patching)

      If unsupported, DataType.encodeValue(Object, MemBuffer, Settings, int) and DataType.encodeRepresentation(String, MemBuffer, Settings, int) will always throw an exception. Actions which rely on either encode method should not be displayed if the applicable datatype is not encodable.

      Specified by:
      isEncodable in interface DataType
      Overrides:
      isEncodable in class AbstractDataType
      Returns:
      true if encoding is supported
    • 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.

      Specified by:
      getValue in interface DataType
      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
    • encodeValue

      public byte[] encodeValue(Object value, MemBuffer buf, Settings settings, int length) throws DataTypeEncodeException
      Description copied from interface: DataType
      Encode bytes from an Object appropriate for this DataType.

      Converts the given object to the byte encoding and returns it. When appropriate, this should seek the nearest encoding to the specified value, since the object may come from a user script. For example, a floating-point value may be rounded. Invalid values should be rejected with a DataTypeEncodeException.

      Specified by:
      encodeValue in interface DataType
      Overrides:
      encodeValue in class AbstractDataType
      Parameters:
      value - the desired value.
      buf - a buffer representing the eventual destination of the bytes.
      settings - the settings to use.
      length - the expected length of the result, usually the length of the data unit, or -1 to let the type choose the length. It may be ignored, e.g., for fixed-length types.
      Returns:
      the encoded value.
      Throws:
      DataTypeEncodeException - if the value cannot be encoded for any reason, e.g., incorrect type, not enough space, buffer overflow, unsupported (see DataType.isEncodable()).
    • 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.
      Specified by:
      getRepresentation in interface DataType
      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.
    • encodeRepresentation

      public byte[] encodeRepresentation(String repr, MemBuffer buf, Settings settings, int length) throws DataTypeEncodeException
      Description copied from interface: DataType
      Encode bytes according to the display format for this type.

      Converts the given representation to the byte encoding and returns it. When appropriate, this should seek the nearest encoding to the specified value, since the representation is likely coming from user input. For example, a floating-point value may be rounded. Invalid representations should be rejected with a DataTypeEncodeException.

      Specified by:
      encodeRepresentation in interface DataType
      Overrides:
      encodeRepresentation in class AbstractDataType
      Parameters:
      repr - the representation of the desired value, as in DataType.getRepresentation(MemBuffer, Settings, int). The supported formats depend on the specific datatype and its settings.
      buf - a buffer representing the eventual destination of the bytes.
      settings - the settings to use for the representation.
      length - the expected length of the result, usually the length of the data unit, or -1 to let the type choose the length. It may be ignored, e.g., for fixed-length types.
      Returns:
      the encoded value.
      Throws:
      DataTypeEncodeException - if the value cannot be encoded for any reason, e.g., incorrect format, not enough space, buffer overflow, unsupported (see DataType.isEncodable()).
    • getDefaultLabelPrefix

      public String getDefaultLabelPrefix(MemBuffer buf, Settings settings, int len, DataTypeDisplayOptions options)
      Description copied from interface: DataType
      Returns the appropriate string to use as the default label prefix.
      Specified by:
      getDefaultLabelPrefix in interface DataType
      Overrides:
      getDefaultLabelPrefix in class AbstractDataType
      Parameters:
      buf - memory buffer containing the bytes.
      settings - the Settings object
      len - the length of the data.
      options - options for how to format the default label prefix.
      Returns:
      the default label prefix or null if none specified.
    • getDefaultOffcutLabelPrefix

      public String getDefaultOffcutLabelPrefix(MemBuffer buf, Settings settings, int len, DataTypeDisplayOptions options, int offcutLength)
      Description copied from interface: DataType
      Returns the appropriate string to use as the default label prefix.

      This takes into account the fact that there exists a reference to the data that references offcutLength bytes into this type

      Specified by:
      getDefaultOffcutLabelPrefix in interface DataType
      Overrides:
      getDefaultOffcutLabelPrefix in class AbstractDataType
      Parameters:
      buf - memory buffer containing the bytes.
      settings - the Settings object
      len - the length of the data.
      options - options for how to format the default label prefix.
      offcutLength - offset into datatype
      Returns:
      the default label prefix.
    • getReplacementBaseType

      public DataType getReplacementBaseType()
      Description copied from interface: Dynamic
      Returns a suitable replacement base data-type for pointers and arrays when exporting to C code
      Specified by:
      getReplacementBaseType in interface Dynamic
      Returns:
      suitable base data-type for this Dynamic data-type