decompiler  1.0.0
Classes | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
ghidra::StringManager Class Referenceabstract

Storage for decoding and storing strings associated with an address. More...

#include <stringmanage.hh>

Inheritance diagram for ghidra::StringManager:
ghidra::GhidraStringManager ghidra::StringManagerUnicode

Classes

class  StringData
 String data (a sequence of bytes) stored by StringManager. More...
 

Public Member Functions

 StringManager (int4 max)
 Constructor. More...
 
virtual ~StringManager (void)
 Destructor.
 
void clear (void)
 Clear out any cached strings.
 
bool isString (const Address &addr, Datatype *charType)
 
virtual const vector< uint1 > & getStringData (const Address &addr, Datatype *charType, bool &isTrunc)=0
 Retrieve string data at the given address as a UTF8 byte array. More...
 
void encode (Encoder &encoder) const
 Encode cached strings to a stream. More...
 
void decode (Decoder &decoder)
 Restore string cache from a stream. More...
 

Static Public Member Functions

static bool hasCharTerminator (const uint1 *buffer, int4 size, int4 charsize)
 Check for a unicode string terminator. More...
 
static int4 readUtf16 (const uint1 *buf, bool bigend)
 Read a UTF16 code point from a byte array. More...
 
static void writeUtf8 (ostream &s, int4 codepoint)
 Write unicode character to stream in UTF8 encoding. More...
 
static int4 getCodepoint (const uint1 *buf, int4 charsize, bool bigend, int4 &skip)
 Extract next unicode codepoint. More...
 

Protected Attributes

map< Address, StringDatastringMap
 Map from address to string data.
 
int4 maximumChars
 Maximum characters in a string before truncating.
 

Detailed Description

Storage for decoding and storing strings associated with an address.

Looks at data in the loadimage to determine if it represents a "string". Decodes the string for presentation in the output. Stores the decoded string until its needed for presentation.

Constructor & Destructor Documentation

◆ StringManager()

ghidra::StringManager::StringManager ( int4  max)

Constructor.

Parameters
maxis the maximum number of characters to allow before truncating string

References maximumChars.

Member Function Documentation

◆ decode()

void ghidra::StringManager::decode ( Decoder decoder)

Restore string cache from a stream.

Parse a <stringmanage> element, with <string> children.

Parameters
decoderis the stream decoder

References ghidra::StringManager::StringData::byteData, ghidra::Decoder::closeElement(), ghidra::Address::decode(), ghidra::StringManager::StringData::isTruncated, ghidra::Decoder::openElement(), ghidra::Decoder::readBool(), ghidra::Decoder::readString(), and stringMap.

Referenced by clear().

◆ encode()

void ghidra::StringManager::encode ( Encoder encoder) const

Encode cached strings to a stream.

Encode <stringmanage> element, with <string> children.

Parameters
encoderis the stream encoder

References ghidra::StringManager::StringData::byteData, ghidra::Encoder::closeElement(), ghidra::StringManager::StringData::isTruncated, ghidra::Encoder::openElement(), stringMap, ghidra::Encoder::writeBool(), and ghidra::Encoder::writeString().

Referenced by clear().

◆ getCodepoint()

int4 ghidra::StringManager::getCodepoint ( const uint1 *  buf,
int4  charsize,
bool  bigend,
int4 &  skip 
)
static

Extract next unicode codepoint.

One or more bytes is consumed from the array, and the number of bytes used is passed back.

Parameters
bufis a pointer to the bytes in the character array
charsizeis 1 for UTF8, 2 for UTF16, or 4 for UTF32
bigendis true for big endian encoding of the UTF element
skipis a reference for passing back the number of bytes consumed
Returns
the codepoint or -1 if the encoding is invalid

References readUtf16().

Referenced by ghidra::StringManagerUnicode::checkCharacters(), clear(), ghidra::PrintLanguage::escapeCharacterData(), and ghidra::StringManagerUnicode::writeUnicode().

◆ getStringData()

virtual const vector<uint1>& ghidra::StringManager::getStringData ( const Address addr,
Datatype charType,
bool &  isTrunc 
)
pure virtual

Retrieve string data at the given address as a UTF8 byte array.

If the address does not represent string data, a zero length vector is returned. Otherwise, the string data is fetched, converted to a UTF8 encoding, cached and returned.

Parameters
addris the given address
charTypeis a character data-type indicating the encoding
isTruncpasses back whether the string is truncated
Returns
the byte array of UTF8 data

Implemented in ghidra::StringManagerUnicode, and ghidra::GhidraStringManager.

Referenced by clear(), isString(), and ghidra::PrintC::printCharacterConstant().

◆ hasCharTerminator()

bool ghidra::StringManager::hasCharTerminator ( const uint1 *  buffer,
int4  size,
int4  charsize 
)
static

Check for a unicode string terminator.

Parameters
bufferis the byte buffer
sizeis the number of bytes in the buffer
charsizeis the presumed size (in bytes) of character elements
Returns
true if a string terminator is found

Referenced by clear(), and ghidra::StringManagerUnicode::getStringData().

◆ isString()

bool ghidra::StringManager::isString ( const Address addr,
Datatype charType 
)

Returns true if the data is some kind of complete string. A given character data-type can be used as a hint for the encoding. The string decoding can be cached internally.

Parameters
addris the given address
charTypeis the given character data-type
Returns
true if the address represents string data

References getStringData().

Referenced by ghidra::RulePtrsubCharConstant::applyOp(), and clear().

◆ readUtf16()

int4 ghidra::StringManager::readUtf16 ( const uint1 *  buf,
bool  bigend 
)
inlinestatic

Read a UTF16 code point from a byte array.

Pull the first two bytes from the byte array and combine them in the indicated endian order

Parameters
bufis the byte array
bigendis true to request big endian encoding
Returns
the decoded UTF16 element

Referenced by clear(), and getCodepoint().

◆ writeUtf8()

void ghidra::StringManager::writeUtf8 ( ostream &  s,
int4  codepoint 
)
static

Write unicode character to stream in UTF8 encoding.

Encode the given unicode codepoint as UTF8 (1, 2, 3, or 4 bytes) and write the bytes to the stream.

Parameters
sis the output stream
codepointis the unicode codepoint

Referenced by clear(), ghidra::PrintJava::printUnicode(), ghidra::PrintC::printUnicode(), and ghidra::StringManagerUnicode::writeUnicode().


The documentation for this class was generated from the following files: