Ghidra 11.3.2
Ghidra internal decompiler documentation.
|
A byte-based decoder designed to marshal info to the decompiler efficiently. More...
#include <marshal.hh>
Classes | |
class | ByteChunk |
A bounded array of bytes. More... | |
class | Position |
An iterator into input stream. More... | |
Public Member Functions | |
PackedDecode (const AddrSpaceManager *spcManager) | |
Constructor. | |
virtual void | ingestStream (istream &s) |
Prepare to decode a given stream. | |
virtual uint4 | peekElement (void) |
Peek at the next child element of the current parent, without traversing in (opening) it. | |
virtual uint4 | openElement (void) |
Open (traverse into) the next child element of the current parent. | |
virtual uint4 | openElement (const ElementId &elemId) |
Open (traverse into) the next child element, which must be of a specific type. | |
virtual void | closeElement (uint4 id) |
Close the current element. | |
virtual void | closeElementSkipping (uint4 id) |
Close the current element, skipping any child elements that have not yet been parsed. | |
virtual void | rewindAttributes (void) |
Reset attribute traversal for the current element. | |
virtual uint4 | getNextAttributeId (void) |
Get the next attribute id for the current element. | |
virtual uint4 | getIndexedAttributeId (const AttributeId &attribId) |
Get the id for the (current) attribute, assuming it is indexed. | |
virtual bool | readBool (void) |
Parse the current attribute as a boolean value. | |
virtual bool | readBool (const AttributeId &attribId) |
Find and parse a specific attribute in the current element as a boolean value. | |
virtual intb | readSignedInteger (void) |
Parse the current attribute as a signed integer value. | |
virtual intb | readSignedInteger (const AttributeId &attribId) |
Find and parse a specific attribute in the current element as a signed integer. | |
virtual intb | readSignedIntegerExpectString (const string &expect, intb expectval) |
Parse the current attribute as either a signed integer value or a string. | |
virtual intb | readSignedIntegerExpectString (const AttributeId &attribId, const string &expect, intb expectval) |
Find and parse a specific attribute in the current element as either a signed integer or a string. | |
virtual uintb | readUnsignedInteger (void) |
Parse the current attribute as an unsigned integer value. | |
virtual uintb | readUnsignedInteger (const AttributeId &attribId) |
Find and parse a specific attribute in the current element as an unsigned integer. | |
virtual string | readString (void) |
Parse the current attribute as a string. | |
virtual string | readString (const AttributeId &attribId) |
Find the specific attribute in the current element and return it as a string. | |
virtual AddrSpace * | readSpace (void) |
Parse the current attribute as an address space. | |
virtual AddrSpace * | readSpace (const AttributeId &attribId) |
Find the specific attribute in the current element and return it as an address space. | |
virtual OpCode | readOpcode (void) |
Parse the current attribute as a p-code OpCode. | |
virtual OpCode | readOpcode (AttributeId &attribId) |
Find the specific attribute in the current element and return it as an OpCode. | |
![]() | |
Decoder (const AddrSpaceManager *spc) | |
Base constructor. | |
const AddrSpaceManager * | getAddrSpaceManager (void) const |
Get the manager used for address space decoding. | |
virtual | ~Decoder (void) |
Destructor. | |
void | skipElement (void) |
Skip parsing of the next element. | |
Static Public Attributes | |
static const int4 | BUFFER_SIZE = 1024 |
The size, in bytes, of a single cached chunk of the input stream. | |
Protected Member Functions | |
uint1 * | allocateNextInputBuffer (int4 pad) |
Allocate the next chunk of space in the input stream. | |
void | endIngest (int4 bufPos) |
Finish set up for reading input stream. | |
Private Member Functions | |
uint1 | getByte (Position &pos) |
Get the byte at the current position, do not advance. | |
uint1 | getBytePlus1 (Position &pos) |
Get the byte following the current byte, do not advance position. | |
uint1 | getNextByte (Position &pos) |
Get the byte at the current position and advance to the next byte. | |
void | advancePosition (Position &pos, int4 skip) |
Advance the position by the given number of bytes. | |
uint8 | readInteger (int4 len) |
Read an integer from the current position given its length in bytes. | |
uint4 | readLengthCode (uint1 typeByte) |
Extract length code from type byte. | |
void | findMatchingAttribute (const AttributeId &attribId) |
Find attribute matching the given id in open element. | |
void | skipAttribute (void) |
Skip over the attribute at the current position. | |
void | skipAttributeRemaining (uint1 typeByte) |
Skip over remaining attribute data, after a mismatch. | |
Private Attributes | |
list< ByteChunk > | inStream |
Incoming raw data as a sequence of byte arrays. | |
Position | startPos |
Position at the start of the current open element. | |
Position | curPos |
Position of the next attribute as returned by getNextAttributeId. | |
Position | endPos |
Ending position after all attributes in current open element. | |
bool | attributeRead |
Has the last attribute returned by getNextAttributeId been read. | |
Additional Inherited Members | |
![]() | |
const AddrSpaceManager * | spcManager |
Manager for decoding address space attributes. | |
A byte-based decoder designed to marshal info to the decompiler efficiently.
The decoder expects an encoding as described in PackedFormat. When ingested, the stream bytes are held in a sequence of arrays (ByteChunk). During decoding, this object maintains a Position in the stream at the start and end of the current open element, and a Position of the next attribute to read to facilitate getNextAttributeId() and associated read*() methods.
|
inlineprivate |
Advance the position by the given number of bytes.
An exception is thrown of position is advanced past the end of the stream
pos | is the position being advanced |
skip | is the number of bytes to advance |
References ghidra::PackedDecode::Position::current, ghidra::PackedDecode::Position::end, inStream, and ghidra::PackedDecode::Position::seqIter.
Referenced by readString(), skipAttribute(), and skipAttributeRemaining().
|
inlineprotected |
Allocate the next chunk of space in the input stream.
Allocate an array of BUFFER_SIZE bytes and add it to the in-memory stream
pad | is the number of bytes of padding to add to the allocation size, above BUFFER_SIZE |
References BUFFER_SIZE, and inStream.
Referenced by ingestStream(), and ghidra::sla::FormatDecode::ingestStream().
|
virtual |
Close the current element.
The data for the current element is considered fully processed. If the element has additional children, an exception is thrown. The stream must indicate the end of the element in some way.
id | is the id of the element to close (which must be the current element) |
Implements ghidra::Decoder.
References ghidra::PackedFormat::ELEMENT_END, ghidra::PackedFormat::ELEMENTID_MASK, endPos, getNextByte(), ghidra::PackedFormat::HEADER_MASK, ghidra::PackedFormat::HEADEREXTEND_MASK, ghidra::PackedFormat::RAWDATA_BITSPERBYTE, and ghidra::PackedFormat::RAWDATA_MASK.
Referenced by closeElementSkipping(), ghidra::ContextGhidra::getTrackedSet(), ghidra::InjectPayloadGhidra::inject(), and ghidra::ExecutablePcodeGhidra::inject().
|
virtual |
Close the current element, skipping any child elements that have not yet been parsed.
This closes the given element, which must be current. If there are child elements that have not been parsed, this is not considered an error, and they are skipped over in the parse.
id | is the id of the element to close (which must be the current element) |
Implements ghidra::Decoder.
References closeElement(), ghidra::PackedFormat::ELEMENT_END, ghidra::PackedFormat::ELEMENT_START, endPos, getByte(), ghidra::PackedFormat::HEADER_MASK, and openElement().
|
protected |
Finish set up for reading input stream.
Set decoder to beginning of the stream. Add padding to end of the stream.
bufPos | is the number of bytes used by the last input buffer |
References BUFFER_SIZE, ghidra::PackedDecode::Position::current, ghidra::PackedFormat::ELEMENT_END, ghidra::PackedDecode::Position::end, endPos, inStream, and ghidra::PackedDecode::Position::seqIter.
Referenced by ingestStream(), and ghidra::sla::FormatDecode::ingestStream().
|
private |
Find attribute matching the given id in open element.
The current position is reset to the start of the current open element. Attributes are scanned and skipped until the attribute matching the given id is found. The current position is set to the start of the matching attribute, in preparation for one of the read*() methods. If the id is not found an exception is thrown.
attribId | is the attribute id to scan for. |
References ghidra::PackedFormat::ATTRIBUTE, curPos, ghidra::PackedFormat::ELEMENTID_MASK, getByte(), getBytePlus1(), ghidra::AttributeId::getId(), ghidra::AttributeId::getName(), ghidra::PackedFormat::HEADER_MASK, ghidra::PackedFormat::HEADEREXTEND_MASK, ghidra::PackedFormat::RAWDATA_BITSPERBYTE, ghidra::PackedFormat::RAWDATA_MASK, skipAttribute(), and startPos.
Referenced by readBool(), readOpcode(), readSignedInteger(), readSignedIntegerExpectString(), readSpace(), readString(), and readUnsignedInteger().
|
inlineprivate |
Get the byte following the current byte, do not advance position.
An exception is thrown if the position currently points to the last byte in the stream
pos | is the position in the stream to look ahead from |
References ghidra::PackedDecode::Position::current, ghidra::PackedDecode::Position::end, inStream, and ghidra::PackedDecode::Position::seqIter.
Referenced by findMatchingAttribute(), getNextAttributeId(), and peekElement().
|
virtual |
Get the id for the (current) attribute, assuming it is indexed.
Assuming the previous call to getNextAttributeId() returned the id of ATTRIB_UNKNOWN, reinterpret the attribute as being an indexed form of the given attribute. If the attribute matches, return this indexed id, otherwise return ATTRIB_UNKNOWN.
attribId | is the attribute being indexed |
Implements ghidra::Decoder.
References ghidra::AttributeId::getId().
|
virtual |
Get the next attribute id for the current element.
Attributes are automatically set up for traversal using this method, when the element is opened. If all attributes have been traversed (or there are no attributes), 0 is returned.
Implements ghidra::Decoder.
References ghidra::PackedFormat::ATTRIBUTE, attributeRead, curPos, ghidra::PackedFormat::ELEMENTID_MASK, getByte(), getBytePlus1(), ghidra::PackedFormat::HEADER_MASK, ghidra::PackedFormat::HEADEREXTEND_MASK, ghidra::PackedFormat::RAWDATA_BITSPERBYTE, ghidra::PackedFormat::RAWDATA_MASK, and skipAttribute().
|
inlineprivate |
Get the byte at the current position and advance to the next byte.
An exception is thrown if there are no additional bytes in the stream
pos | is the position of the byte |
References ghidra::PackedDecode::Position::current, ghidra::PackedDecode::Position::end, inStream, and ghidra::PackedDecode::Position::seqIter.
Referenced by closeElement(), openElement(), readBool(), readInteger(), readSignedInteger(), readSignedIntegerExpectString(), readSpace(), readString(), readUnsignedInteger(), and skipAttribute().
|
virtual |
Prepare to decode a given stream.
Called once before any decoding. Currently this is assumed to make an internal copy of the stream data, i.e. the input stream is cleared before any decoding takes place.
s | is the given input stream to be decode |
Implements ghidra::Decoder.
Reimplemented in ghidra::sla::FormatDecode.
References allocateNextInputBuffer(), BUFFER_SIZE, and endIngest().
|
virtual |
Open (traverse into) the next child element, which must be of a specific type.
The child becomes the current parent, and its attributes are initialized for use with getNextAttributeId. The child must match the given element id or an exception is thrown.
elemId | is the given element id to match |
Implements ghidra::Decoder.
References ghidra::ElementId::getId(), ghidra::ElementId::getName(), and openElement().
|
virtual |
Open (traverse into) the next child element of the current parent.
The child becomes the current parent. The list of attributes is initialized for use with getNextAttributeId.
Implements ghidra::Decoder.
References ghidra::PackedFormat::ATTRIBUTE, attributeRead, curPos, ghidra::PackedFormat::ELEMENT_START, ghidra::PackedFormat::ELEMENTID_MASK, endPos, getByte(), getNextByte(), ghidra::PackedFormat::HEADER_MASK, ghidra::PackedFormat::HEADEREXTEND_MASK, ghidra::PackedFormat::RAWDATA_BITSPERBYTE, ghidra::PackedFormat::RAWDATA_MASK, skipAttribute(), and startPos.
Referenced by closeElementSkipping(), ghidra::ContextGhidra::getTrackedSet(), ghidra::InjectPayloadGhidra::inject(), ghidra::ExecutablePcodeGhidra::inject(), ghidra::GhidraTranslate::oneInstruction(), and openElement().
|
virtual |
Peek at the next child element of the current parent, without traversing in (opening) it.
The element id is returned, which can be compared to ElementId labels. If there are no remaining child elements to traverse, 0 is returned.
Implements ghidra::Decoder.
References ghidra::PackedFormat::ELEMENT_START, ghidra::PackedFormat::ELEMENTID_MASK, endPos, getByte(), getBytePlus1(), ghidra::PackedFormat::HEADER_MASK, ghidra::PackedFormat::HEADEREXTEND_MASK, ghidra::PackedFormat::RAWDATA_BITSPERBYTE, and ghidra::PackedFormat::RAWDATA_MASK.
Referenced by ghidra::InjectPayloadGhidra::inject(), ghidra::ExecutablePcodeGhidra::inject(), and ghidra::GhidraTranslate::oneInstruction().
|
virtual |
Find and parse a specific attribute in the current element as a boolean value.
The set of attributes for the current element is searched for a match to the given attribute id. This attribute is then parsed as a boolean and its value returned. If there is no attribute matching the id, an exception is thrown. Parsing via getNextAttributeId is reset.
attribId | is the specific attribute id to match |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readBool(), and startPos.
|
virtual |
Parse the current attribute as a boolean value.
The last attribute, as returned by getNextAttributeId, is treated as a boolean, and its value is returned.
Implements ghidra::Decoder.
References attributeRead, curPos, getNextByte(), ghidra::PackedFormat::HEADEREXTEND_MASK, ghidra::PackedFormat::LENGTHCODE_MASK, ghidra::PackedFormat::TYPECODE_BOOLEAN, and ghidra::PackedFormat::TYPECODE_SHIFT.
Referenced by readBool().
|
private |
Read an integer from the current position given its length in bytes.
The integer is encoded, 7-bits per byte, starting with the most significant 7-bits. The integer is decode from the current position, and the position is advanced.
len | is the number of bytes to extract |
References curPos, getNextByte(), ghidra::PackedFormat::RAWDATA_BITSPERBYTE, and ghidra::PackedFormat::RAWDATA_MASK.
Referenced by readSignedInteger(), readSpace(), readString(), readUnsignedInteger(), skipAttribute(), and skipAttributeRemaining().
|
virtual |
Find the specific attribute in the current element and return it as an OpCode.
Search attributes from the current element for a match to the given attribute id. Return this attribute as an OpCode. If there is no matching attribute id, an exception is thrown. Parse via getNextAttributeId is reset.
attribId | is the specific attribute id to match |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readOpcode(), and startPos.
|
virtual |
Parse the current attribute as a p-code OpCode.
The last attribute, as returned by getNextAttributeId, is returned as an OpCode.
Implements ghidra::Decoder.
References ghidra::CPUI_MAX, and readSignedInteger().
Referenced by readOpcode().
|
virtual |
Find and parse a specific attribute in the current element as a signed integer.
The set of attributes for the current element is searched for a match to the given attribute id. This attribute is then parsed as a signed integer and its value returned. If there is no attribute matching the id, an exception is thrown. Parsing via getNextAttributeId is reset.
attribId | is the specific attribute id to match |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readSignedInteger(), and startPos.
|
virtual |
Parse the current attribute as a signed integer value.
The last attribute, as returned by getNextAttributeId, is treated as a signed integer, and its value is returned.
Implements ghidra::Decoder.
References attributeRead, curPos, getNextByte(), ghidra::PackedFormat::HEADEREXTEND_MASK, readInteger(), readLengthCode(), skipAttributeRemaining(), ghidra::PackedFormat::TYPECODE_SHIFT, ghidra::PackedFormat::TYPECODE_SIGNEDINT_NEGATIVE, and ghidra::PackedFormat::TYPECODE_SIGNEDINT_POSITIVE.
Referenced by ghidra::GhidraTranslate::oneInstruction(), readOpcode(), readSignedInteger(), and readSignedIntegerExpectString().
|
virtual |
Find and parse a specific attribute in the current element as either a signed integer or a string.
If the attribute is an integer, its value is parsed and returned. If the attribute is encoded as a string, it must match an expected string passed to this method. In this case, a predetermined integer value is passed back, indicating a matching string was parsed. If the attribute neither matches the expected string nor is an integer, the return value is undefined. If there is no attribute matching the id, an exception is thrown.
attribId | is the specific attribute id to match |
expect | is the string to expect, if the attribute is not encoded as an integer |
expectval | is the integer value to return if the attribute matches the expected string |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readSignedIntegerExpectString(), and startPos.
|
virtual |
Parse the current attribute as either a signed integer value or a string.
If the attribute is an integer, its value is returned. If the attribute is a string, it must match an expected string passed to the method, and a predetermined integer value associated with the string is returned. If the attribute neither matches the expected string nor is an integer, the return value is undefined.
expect | is the string value to expect if the attribute is encoded as a string |
expectval | is the integer value to return if the attribute matches the expected string |
Implements ghidra::Decoder.
References curPos, getNextByte(), ghidra::PackedFormat::HEADEREXTEND_MASK, readSignedInteger(), readString(), ghidra::PackedFormat::TYPECODE_SHIFT, and ghidra::PackedFormat::TYPECODE_STRING.
Referenced by readSignedIntegerExpectString().
|
virtual |
Find the specific attribute in the current element and return it as an address space.
Search attributes from the current element for a match to the given attribute id. Return this attribute as an address space. If there is no attribute matching the id, an exception is thrown. Parse via getNextAttributeId is reset.
attribId | is the specific attribute id to match |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readSpace(), and startPos.
|
virtual |
Parse the current attribute as an address space.
The last attribute, as returned by getNextAttributeId, is returned as an address space.
Implements ghidra::Decoder.
References attributeRead, curPos, ghidra::AddrSpaceManager::getJoinSpace(), getNextByte(), ghidra::AddrSpaceManager::getSpace(), ghidra::AddrSpaceManager::getStackSpace(), ghidra::PackedFormat::HEADEREXTEND_MASK, readInteger(), readLengthCode(), skipAttributeRemaining(), ghidra::Decoder::spcManager, ghidra::PackedFormat::SPECIALSPACE_JOIN, ghidra::PackedFormat::SPECIALSPACE_STACK, ghidra::PackedFormat::TYPECODE_ADDRESSSPACE, ghidra::PackedFormat::TYPECODE_SHIFT, and ghidra::PackedFormat::TYPECODE_SPECIALSPACE.
Referenced by readSpace().
|
virtual |
Find the specific attribute in the current element and return it as a string.
The set of attributes for the current element is searched for a match to the given attribute id. This attribute is then returned as a string. If there is no attribute matching the id, and exception is thrown. Parse via getNextAttributeId is reset.
attribId | is the specific attribute id to match |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readString(), and startPos.
|
virtual |
Parse the current attribute as a string.
The last attribute, as returned by getNextAttributeId, is returned as a string.
Implements ghidra::Decoder.
References advancePosition(), attributeRead, curPos, ghidra::PackedDecode::Position::current, ghidra::PackedDecode::Position::end, getNextByte(), ghidra::PackedFormat::HEADEREXTEND_MASK, readInteger(), readLengthCode(), skipAttributeRemaining(), ghidra::PackedFormat::TYPECODE_SHIFT, and ghidra::PackedFormat::TYPECODE_STRING.
Referenced by readSignedIntegerExpectString(), and readString().
|
virtual |
Find and parse a specific attribute in the current element as an unsigned integer.
The set of attributes for the current element is searched for a match to the given attribute id. This attribute is then parsed as an unsigned integer and its value returned. If there is no attribute matching the id, an exception is thrown. Parsing via getNextAttributeId is reset.
attribId | is the specific attribute id to match |
Implements ghidra::Decoder.
References curPos, findMatchingAttribute(), readUnsignedInteger(), and startPos.
|
virtual |
Parse the current attribute as an unsigned integer value.
The last attribute, as returned by getNextAttributeId, is treated as an unsigned integer, and its value is returned.
Implements ghidra::Decoder.
References attributeRead, curPos, getNextByte(), ghidra::PackedFormat::HEADEREXTEND_MASK, readInteger(), readLengthCode(), skipAttributeRemaining(), ghidra::PackedFormat::TYPECODE_SHIFT, and ghidra::PackedFormat::TYPECODE_UNSIGNEDINT.
Referenced by readUnsignedInteger().
|
virtual |
Reset attribute traversal for the current element.
Attributes for a single element can be traversed more than once using the getNextAttributeId method.
Implements ghidra::Decoder.
References attributeRead, curPos, and startPos.
|
private |
Skip over the attribute at the current position.
The attribute at the current position is scanned enough to determine its length, and the position is advanced to the following byte.
References advancePosition(), curPos, getNextByte(), ghidra::PackedFormat::HEADEREXTEND_MASK, readInteger(), readLengthCode(), ghidra::PackedFormat::TYPECODE_BOOLEAN, ghidra::PackedFormat::TYPECODE_SHIFT, ghidra::PackedFormat::TYPECODE_SPECIALSPACE, and ghidra::PackedFormat::TYPECODE_STRING.
Referenced by findMatchingAttribute(), getNextAttributeId(), and openElement().
|
private |
Skip over remaining attribute data, after a mismatch.
This assumes the header and type byte have been read. Decode type and length info and finish skipping over the attribute so that the next call to getNextAttributeId() is on cut.
typeByte | is the previously scanned type byte |
References advancePosition(), curPos, readInteger(), readLengthCode(), ghidra::PackedFormat::TYPECODE_BOOLEAN, ghidra::PackedFormat::TYPECODE_SHIFT, ghidra::PackedFormat::TYPECODE_SPECIALSPACE, and ghidra::PackedFormat::TYPECODE_STRING.
Referenced by readSignedInteger(), readSpace(), readString(), and readUnsignedInteger().