Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-7253 Adjustments to Encode Decode

caheckman747d581b1af2
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc
index 129488c9f0..ec38d11c13 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.cc
@@ -15,6 +15,7 @@
*/
#include "marshal.hh"
#include "translate.hh"
+#include "type.hh"
namespace ghidra {
@@ -455,6 +456,31 @@ OpCode XmlDecode::readOpcode(AttributeId &attribId)
return opc;
}
+type_metatype XmlDecode::readDatatypeMeta(void)
+
+{
+ const Element *el = elStack.back();
+ string nm = el->getAttributeValue(attributeIndex);
+ type_metatype meta = string2metatype(nm);
+ return meta;
+}
+
+type_metatype XmlDecode::readDatatypeMeta(AttributeId &attribId)
+
+{
+ const Element *el = elStack.back();
+ string nm;
+ if (attribId == ATTRIB_CONTENT) {
+ nm = el->getContent();
+ }
+ else {
+ int4 index = findMatchingAttribute(el, attribId.getName());
+ nm = el->getAttributeValue(index);
+ }
+ type_metatype meta = string2metatype(nm);
+ return meta;
+}
+
void XmlEncode::newLine(void)
{
@@ -597,6 +623,24 @@ void XmlEncode::writeOpcode(const AttributeId &attribId,OpCode opc)
outStream << "\"";
}
+void XmlEncode::writeDatatypeMeta(const AttributeId &attribId,type_metatype metatype)
+
+{
+ string name;
+ metatype2string(metatype, name);
+ if (attribId == ATTRIB_CONTENT) {
+ if (tagStatus == tag_start) {
+ outStream << '>';
+ }
+ outStream << name;
+ tagStatus = tag_content;
+ return;
+ }
+ outStream << ' ' << attribId.getName() << "=\"";
+ outStream << name;
+ outStream << "\"";
+}
+
/// The integer is encoded, 7-bits per byte, starting with the most significant 7-bits.
/// The integer is decode from the \e current position, and the position is advanced.
/// \param len is the number of bytes to extract
@@ -676,10 +720,8 @@ void PackedDecode::skipAttributeRemaining(uint1 typeByte)
void PackedDecode::endIngest(int4 bufPos)
{
- endPos.seqIter = inStream.begin(); // Set position to beginning of stream
+ endPos.seqIter = inStream.begin(); // Set position to first buffer
if (endPos.seqIter != inStream.end()) {
- endPos.current = (*endPos.seqIter).start;
- endPos.end = (*endPos.seqIter).end;
// Make sure there is at least one character after ingested buffer
if (bufPos == BUFFER_SIZE) {
// Last buffer was entirely filled
@@ -689,10 +731,12 @@ void PackedDecode::endIngest(int4 bufPos)
}
uint1 *buf = inStream.back().start;
buf[bufPos] = ELEMENT_END;
+ inStream.back().end = buf + bufPos + 1;
+ endPos.current = (*endPos.seqIter).start; // Set position to start of buffer
+ endPos.end = (*endPos.seqIter).end;
} else {
throw DecoderError("Ended ingestion without any input");
}
-
}
PackedDecode::~PackedDecode(void)
@@ -1061,6 +1105,24 @@ OpCode PackedDecode::readOpcode(AttributeId &attribId)
return opc;
}
+type_metatype PackedDecode::readDatatypeMeta(void)
+
+{
+ int4 val = (int4)readSignedInteger();
+ if (val < 0 || val > TYPE_VOID)
+ throw DecoderError("Bad encoded metatype");
+ return (type_metatype)val;
+}
+
+type_metatype PackedDecode::readDatatypeMeta(AttributeId &attribId)
+
+{
+ findMatchingAttribute(attribId);
+ type_metatype metatype = readDatatypeMeta();
+ curPos = startPos;
+ return metatype;
+}
+
/// The value is either an unsigned integer, an address space index, or (the absolute value of) a signed integer.
/// A type header is passed in with the particular type code for the value already filled in.
/// This method then fills in the length code, outputs the full type header and the encoded bytes of the integer.
@@ -1180,18 +1242,28 @@ void PackedEncode::writeString(const AttributeId &attribId,const string &val)
{
uint8 length = val.length();
+ const char *ptr = val.c_str();
+ for(uint8 i=0;i<length;++i) {
+ if (ptr[i] == '\0')
+ throw LowlevelError("PackedEncode: string with null character");
+ }
writeHeader(ATTRIBUTE, attribId.getId());
writeInteger((TYPECODE_STRING << TYPECODE_SHIFT), length);
- outStream.write(val.c_str(), length);
+ outStream.write(ptr, length);
}
void PackedEncode::writeStringIndexed(const AttributeId &attribId,uint4 index,const string &val)
{
uint8 length = val.length();
+ const char *ptr = val.c_str();
+ for(uint8 i=0;i<length;++i) {
+ if (ptr[i] == '\0')
+ throw LowlevelError("PackedEncode: string with null character");
+ }
writeHeader(ATTRIBUTE, attribId.getId() + index);
writeInteger((TYPECODE_STRING << TYPECODE_SHIFT), length);
- outStream.write(val.c_str(), length);
+ outStream.write(ptr, length);
}
void PackedEncode::writeSpace(const AttributeId &attribId,const AddrSpace *spc)
@@ -1228,6 +1300,13 @@ void PackedEncode::writeOpcode(const AttributeId &attribId,OpCode opc)
writeInteger((TYPECODE_SIGNEDINT_POSITIVE << TYPECODE_SHIFT), opc);
}
+void PackedEncode::writeDatatypeMeta(const AttributeId &attribId,type_metatype metatype)
+
+{
+ writeHeader(ATTRIBUTE, attribId.getId());
+ writeInteger((TYPECODE_SIGNEDINT_POSITIVE << TYPECODE_SHIFT), metatype);
+}
+
// Common attributes. Attributes with multiple uses
AttributeId ATTRIB_CONTENT = AttributeId("XMLcontent",1);
AttributeId ATTRIB_ALIGN = AttributeId("align",2);
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh
index c9a913bc98..cd41bc6f6c 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/marshal.hh
@@ -26,6 +26,8 @@ namespace ghidra {
using std::list;
using std::unordered_map;
+enum type_metatype : int4; // Forward declaration
+
/// \brief An annotation for a data element to being transferred to/from a stream
///
/// This class parallels the XML concept of an \b attribute on an element. An AttributeId describes
@@ -284,6 +286,21 @@ public:
/// \return the OpCode associated with the attribute
virtual OpCode readOpcode(AttributeId &attribId)=0;
+ /// \brief Parse the current attribute as a metatype
+ ///
+ /// The last attribute, as returned by getNextAttributeId, is returned as a data-type metatype.
+ /// \return the metatype associated with the current attribute
+ virtual type_metatype readDatatypeMeta(void)=0;
+
+ /// \brief Find the specific attribute in the current element and return it as a metatype
+ ///
+ /// Search attributes from the current element for a match to the given attribute id.
+ /// Return this attribute as a metatype. If there is no matching attribute id, an exception is thrown.
+ /// Parse via getNextAttributeId is reset.
+ /// \param attribId is the specific attribute id to match
+ /// \return the metatype associated with the attribute
+ virtual type_metatype readDatatypeMeta(AttributeId &attribId)=0;
+
/// \brief Skip parsing of the next element
///
/// The element skipped is the one that would be opened by the next call to openElement.
@@ -372,6 +389,11 @@ public:
/// \param opc is the opcode
virtual void writeOpcode(const AttributeId &attribId,OpCode opc)=0;
+ /// \brief Write a metatype into the encoding, associating it with the given attribute
+ ///
+ /// \param attribId is the given attribute
+ /// \param metatype is the data-type metatype
+ virtual void writeDatatypeMeta(const AttributeId &attribId,type_metatype metatype)=0;
};
/// \brief An XML based decoder
@@ -417,6 +439,8 @@ public:
virtual AddrSpace *readSpace(const AttributeId &attribId);
virtual OpCode readOpcode(void);
virtual OpCode readOpcode(AttributeId &attribId);
+ virtual type_metatype readDatatypeMeta(void);
+ virtual type_metatype readDatatypeMeta(AttributeId &attribId);
};
/// \brief An XML based encoder
@@ -448,6 +472,7 @@ public:
virtual void writeStringIndexed(const AttributeId &attribId,uint4 index,const string &val);
virtual void writeSpace(const AttributeId &attribId,const AddrSpace *spc);
virtual void writeOpcode(const AttributeId &attribId,OpCode opc);
+ virtual void writeDatatypeMeta(const AttributeId &attribId,type_metatype metatype);
};
/// \brief Protocol format for PackedEncode and PackedDecode classes
@@ -570,6 +595,8 @@ public:
virtual AddrSpace *readSpace(const AttributeId &attribId);
virtual OpCode readOpcode(void);
virtual OpCode readOpcode(AttributeId &attribId);
+ virtual type_metatype readDatatypeMeta(void);
+ virtual type_metatype readDatatypeMeta(AttributeId &attribId);
};
/// \brief A byte-based encoder designed to marshal from the decompiler efficiently
@@ -590,6 +617,7 @@ public:
virtual void writeStringIndexed(const AttributeId &attribId,uint4 index,const string &val);
virtual void writeSpace(const AttributeId &attribId,const AddrSpace *spc);
virtual void writeOpcode(const AttributeId &attribId,OpCode opc);
+ virtual void writeDatatypeMeta(const AttributeId &attribId,type_metatype metatype);
};
/// An exception is thrown if the position currently points to the last byte in the stream
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
index 6b226036cb..e745f67ed0 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.cc
@@ -474,9 +474,7 @@ void Datatype::encodeBasic(type_metatype meta,int4 align,Encoder &encoder) const
encoder.writeUnsignedInteger(ATTRIB_ID, saveId);
}
encoder.writeSignedInteger(ATTRIB_SIZE, size);
- string metastring;
- metatype2string(meta,metastring);
- encoder.writeString(ATTRIB_METATYPE,metastring);
+ encoder.writeDatatypeMeta(ATTRIB_METATYPE, meta);
if (align > 0)
encoder.writeSignedInteger(ATTRIB_ALIGNMENT, align);
if ((flags & coretype)!=0)
@@ -749,7 +747,7 @@ void Datatype::decodeBasic(Decoder &decoder)
size = decoder.readSignedInteger();
}
else if (attrib == ATTRIB_METATYPE) {
- metatype = string2metatype(decoder.readString());
+ metatype = decoder.readDatatypeMeta();
}
else if (attrib == ATTRIB_CORE) {
if (decoder.readBool())
@@ -5138,7 +5136,7 @@ Datatype *TypeFactory::decodeTypeNoRef(Decoder &decoder,bool forcecore)
decoder.closeElement(elemId);
return ct;
}
- type_metatype meta = string2metatype(decoder.readString(ATTRIB_METATYPE));
+ type_metatype meta = decoder.readDatatypeMeta(ATTRIB_METATYPE);
switch(meta) {
case TYPE_PTR:
{
diff --git a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
index b32741b41c..4f8848eb9a 100644
--- a/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
+++ b/Ghidra/Features/Decompiler/src/decompile/cpp/type.hh
@@ -77,7 +77,7 @@ extern void print_data(ostream &s,uint1 *buffer,int4 size,const Address &baseadd
/// The core meta-types supported by the decompiler. These are sizeless templates
/// for the elements making up the type algebra. Index is important for Datatype::base2sub array.
-enum type_metatype {
+enum type_metatype : int4 {
TYPE_VOID = 17, ///< Standard "void" type, absence of type
TYPE_SPACEBASE = 16, ///< Placeholder for symbol/type look-up calculations
TYPE_UNKNOWN = 15, ///< An unknown low-level type. Treated as an unsigned integer.
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java
index 64d290e3fc..eb28cd9bee 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -27,7 +27,6 @@ import ghidra.framework.options.ToolOptions;
import ghidra.framework.plugintool.ServiceProvider;
import ghidra.program.model.address.*;
import ghidra.program.model.data.DataType;
-import ghidra.program.model.data.MetaDataType;
import ghidra.program.model.listing.*;
import ghidra.program.model.pcode.*;
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/MetaDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/MetaDataType.java
deleted file mode 100644
index fccda02918..0000000000
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/MetaDataType.java
+++ /dev/null
@@ -1,122 +0,0 @@
-/* ###
- * IP: GHIDRA
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package ghidra.program.model.data;
-
-public enum MetaDataType {
- // Enumerations are ordered in terms of how "specific" the data-type class is
- VOID, // "void" data-type
- UNKNOWN, // An unknown/undefined data-type
- INT, // Signed integer
- UINT, // Unsigned integer
- BOOL, // Boolean
- CODE, // Executable code
- FLOAT, // Floating-point
- PTR, // Pointer
- ARRAY, // Array
- STRUCT; // Structured data-type
-
- public static MetaDataType getMeta(DataType dt) {
- if (dt instanceof TypeDef) {
- dt = ((TypeDef) dt).getBaseDataType();
- }
- if (dt instanceof DefaultDataType || dt instanceof Undefined) {
- return UNKNOWN;
- }
- if (dt instanceof AbstractIntegerDataType) {
- if (dt instanceof BooleanDataType) {
- return BOOL;
- }
- if (((AbstractIntegerDataType) dt).isSigned()) {
- return INT;
- }
- return UINT;
- }
- if (dt instanceof Pointer) {
- return PTR;
- }
- if (dt instanceof Array) {
- return ARRAY;
- }
- if (dt instanceof Structure) {
- return STRUCT;
- }
- if (dt instanceof AbstractFloatDataType) {
- return FLOAT;
- }
- if (dt instanceof ArrayStringable) {
- return INT;
- }
- if (dt instanceof FunctionDefinition) {
- return CODE;
- }
- if (dt instanceof Enum) {
- return UINT;
- }
- if (dt instanceof AbstractStringDataType) {
- return ARRAY;
- }
- return STRUCT;
- }
-
- public static DataType getMostSpecificDataType(DataType a, DataType b) {
- DataType aCopy = a;
- DataType bCopy = b;
- for (;;) {
- if (a == null) {
- return bCopy;
- }
- if (b == null) {
- return aCopy;
- }
- MetaDataType aMeta = MetaDataType.getMeta(a);
- MetaDataType bMeta = MetaDataType.getMeta(b);
- int compare = aMeta.compareTo(bMeta);
- if (compare < 0) {
- return bCopy;
- }
- else if (compare > 0) {
- return aCopy;
- }
- if (aMeta == MetaDataType.PTR) {
- if (a instanceof TypeDef) {
- a = ((TypeDef) a).getBaseDataType();
- }
- if (b instanceof TypeDef) {
- b = ((TypeDef) b).getBaseDataType();
- }
- a = ((Pointer) a).getDataType();
- b = ((Pointer) b).getDataType();
- }
- else if (aMeta == MetaDataType.ARRAY) {
- if (a instanceof TypeDef) {
- a = ((TypeDef) a).getBaseDataType();
- }
- if (b instanceof TypeDef) {
- b = ((TypeDef) b).getBaseDataType();
- }
- if (!(a instanceof Array) || !(b instanceof Array)) {
- break;
- }
- a = ((Array) a).getDataType();
- b = ((Array) b).getDataType();
- }
- else {
- break;
- }
- }
- return aCopy;
- }
-}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/NoisyStructureBuilder.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/NoisyStructureBuilder.java
index c3e46b015e..80d4465679 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/NoisyStructureBuilder.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/NoisyStructureBuilder.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -19,6 +19,7 @@ import java.util.Iterator;
import java.util.Map.Entry;
import java.util.TreeMap;
+import ghidra.program.model.pcode.MetaDataType;
import ghidra.program.model.pcode.PartialUnion;
/**
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java
index 66102263d9..867c92df5d 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/DatatypeFilter.java
@@ -16,12 +16,13 @@
package ghidra.program.model.lang.protorules;
import static ghidra.program.model.pcode.AttributeId.*;
+import static ghidra.program.model.pcode.MetaDataType.*;
import java.io.IOException;
import ghidra.program.model.data.DataType;
import ghidra.program.model.pcode.Encoder;
-import ghidra.program.model.pcode.PcodeDataTypeManager;
+import ghidra.program.model.pcode.MetaDataType;
import ghidra.xml.*;
/**
@@ -80,12 +81,12 @@ public interface DatatypeFilter {
}
else if (nm.equals(HomogeneousAggregate.NAME_FLOAT)) {
filter = new HomogeneousAggregate(HomogeneousAggregate.NAME_FLOAT,
- PcodeDataTypeManager.TYPE_FLOAT, HomogeneousAggregate.DEFAULT_MAX_PRIMITIVES, 0, 0);
+ TYPE_FLOAT, HomogeneousAggregate.DEFAULT_MAX_PRIMITIVES, 0, 0);
}
else {
// If no other name matches, assume this is a decompiler metatype
- int meta = PcodeDataTypeManager.getMetatype(nm);
- if (meta == PcodeDataTypeManager.TYPE_ARRAY) {
+ MetaDataType meta = MetaDataType.get(nm);
+ if (meta == TYPE_ARRAY) {
filter = new ArrayFilter();
}
else {
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java
index 778fdd02b5..3d4ec39334 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/HomogeneousAggregate.java
@@ -17,6 +17,7 @@ package ghidra.program.model.lang.protorules;
import static ghidra.program.model.pcode.AttributeId.*;
import static ghidra.program.model.pcode.ElementId.*;
+import static ghidra.program.model.pcode.MetaDataType.*;
import java.io.IOException;
import java.util.Iterator;
@@ -24,7 +25,7 @@ import java.util.Map.Entry;
import ghidra.program.model.data.DataType;
import ghidra.program.model.pcode.Encoder;
-import ghidra.program.model.pcode.PcodeDataTypeManager;
+import ghidra.program.model.pcode.MetaDataType;
import ghidra.util.xml.SpecXmlUtils;
import ghidra.xml.*;
@@ -37,7 +38,7 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
public static final String NAME_FLOAT = "homogeneous-float-aggregate";
public static final int DEFAULT_MAX_PRIMITIVES = 4; // Maximum number of primitives in aggregate data-type
private String name;
- private int metaType; // The expected meta-type
+ private MetaDataType metaType; // The expected meta-type
private int maxPrimitives; // Maximum number of primitives in the aggregate
/**
@@ -45,13 +46,14 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
* @param nm is the name attribute associated with the tag
* @param meta is the expected element meta-type
*/
- public HomogeneousAggregate(String nm, int meta) {
+ public HomogeneousAggregate(String nm, MetaDataType meta) {
name = nm;
metaType = meta;
maxPrimitives = DEFAULT_MAX_PRIMITIVES;
}
- public HomogeneousAggregate(String nm, int meta, int maxPrim, int minSize, int maxSize) {
+ public HomogeneousAggregate(String nm, MetaDataType meta, int maxPrim, int minSize,
+ int maxSize) {
super(minSize, maxSize);
name = nm;
metaType = meta;
@@ -97,8 +99,8 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
@Override
public boolean filter(DataType dt) {
- int meta = PcodeDataTypeManager.getMetatype(dt);
- if (meta != PcodeDataTypeManager.TYPE_ARRAY && meta != PcodeDataTypeManager.TYPE_STRUCT) {
+ MetaDataType meta = MetaDataType.get(dt);
+ if (meta != TYPE_ARRAY && meta != TYPE_STRUCT) {
return false;
}
PrimitiveExtractor primitives = new PrimitiveExtractor(dt, true, false, 0, maxPrimitives);
@@ -107,7 +109,7 @@ public class HomogeneousAggregate extends SizeRestrictedFilter {
return false;
}
DataType base = primitives.get(0).dt;
- int baseMeta = PcodeDataTypeManager.getMetatype(base);
+ MetaDataType baseMeta = MetaDataType.get(base);
if (baseMeta != metaType) {
return false;
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MetaTypeFilter.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MetaTypeFilter.java
index a49a617f68..b45b1e31da 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MetaTypeFilter.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/MetaTypeFilter.java
@@ -22,7 +22,7 @@ import java.io.IOException;
import ghidra.program.model.data.DataType;
import ghidra.program.model.pcode.Encoder;
-import ghidra.program.model.pcode.PcodeDataTypeManager;
+import ghidra.program.model.pcode.MetaDataType;
import ghidra.xml.*;
/**
@@ -31,13 +31,13 @@ import ghidra.xml.*;
*/
public class MetaTypeFilter extends SizeRestrictedFilter {
- protected int metaType; // The meta-type this filter lets through
+ protected MetaDataType metaType; // The meta-type this filter lets through
/**
* Constructor for use with decode().
* @param meta is the data-type metatype to filter on
*/
- public MetaTypeFilter(int meta) {
+ public MetaTypeFilter(MetaDataType meta) {
metaType = meta;
}
@@ -47,7 +47,7 @@ public class MetaTypeFilter extends SizeRestrictedFilter {
* @param min is the minimum size in bytes
* @param max is the maximum size in bytes
*/
- public MetaTypeFilter(int meta, int min, int max) {
+ public MetaTypeFilter(MetaDataType meta, int min, int max) {
super(min, max);
metaType = meta;
}
@@ -83,7 +83,7 @@ public class MetaTypeFilter extends SizeRestrictedFilter {
@Override
public boolean filter(DataType dt) {
- if (PcodeDataTypeManager.getMetatype(dt) != metaType) {
+ if (MetaDataType.get(dt) != metaType) {
return false;
}
return filterOnSize(dt);
@@ -92,8 +92,7 @@ public class MetaTypeFilter extends SizeRestrictedFilter {
@Override
public void encode(Encoder encoder) throws IOException {
encoder.openElement(ELEM_DATATYPE);
- String meta = PcodeDataTypeManager.getMetatypeString(metaType);
- encoder.writeString(ATTRIB_NAME, meta);
+ encoder.writeString(ATTRIB_NAME, metaType.toString());
encodeAttributes(encoder);
encoder.closeElement(ELEM_DATATYPE);
}
@@ -101,7 +100,11 @@ public class MetaTypeFilter extends SizeRestrictedFilter {
@Override
public void restoreXml(XmlPullParser parser) throws XmlParseException {
XmlElement elem = parser.start(ELEM_DATATYPE.name());
- metaType = PcodeDataTypeManager.getMetatype(elem.getAttribute(ATTRIB_NAME.name()));
+ String nm = elem.getAttribute(ATTRIB_NAME.name());
+ metaType = MetaDataType.get(nm);
+ if (metaType == null) {
+ throw new XmlParseException("Unknown meta-type: " + nm);
+ }
restoreAttributesXml(elem);
parser.end(elem);
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java
index f7691aa4f3..2a87ae697f 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/lang/protorules/PrimitiveExtractor.java
@@ -15,10 +15,12 @@
*/
package ghidra.program.model.lang.protorules;
+import static ghidra.program.model.pcode.MetaDataType.*;
+
import java.util.ArrayList;
import ghidra.program.model.data.*;
-import ghidra.program.model.pcode.PcodeDataTypeManager;
+import ghidra.program.model.pcode.MetaDataType;
public class PrimitiveExtractor {
@@ -58,8 +60,7 @@ public class PrimitiveExtractor {
Primitive big) {
int endOff = big.offset + big.dt.getAlignedLength();
// If big data-type is a float, let smaller primitives override it, otherwise we keep the big primitive
- boolean useSmall =
- PcodeDataTypeManager.getMetatype(big.dt) == PcodeDataTypeManager.TYPE_FLOAT;
+ boolean useSmall = MetaDataType.get(big.dt) == TYPE_FLOAT;
while (point < small.size()) {
int curOff = small.get(point).offset;
if (curOff >= endOff) {
@@ -205,24 +206,24 @@ public class PrimitiveExtractor {
if (dt instanceof TypeDef) {
dt = ((TypeDef) dt).getBaseDataType();
}
- int metaType = PcodeDataTypeManager.getMetatype(dt);
+ MetaDataType metaType = MetaDataType.get(dt);
switch (metaType) {
- case PcodeDataTypeManager.TYPE_UNKNOWN:
+ case TYPE_UNKNOWN:
unknownElements = true;
// fall-thru
- case PcodeDataTypeManager.TYPE_INT:
- case PcodeDataTypeManager.TYPE_UINT:
- case PcodeDataTypeManager.TYPE_BOOL:
- case PcodeDataTypeManager.TYPE_CODE:
- case PcodeDataTypeManager.TYPE_FLOAT:
- case PcodeDataTypeManager.TYPE_PTR:
- case PcodeDataTypeManager.TYPE_PTRREL:
+ case TYPE_INT:
+ case TYPE_UINT:
+ case TYPE_BOOL:
+ case TYPE_CODE:
+ case TYPE_FLOAT:
+ case TYPE_PTR:
+ case TYPE_PTRREL:
if (primitives.size() >= max) {
return false;
}
primitives.add(new Primitive(dt, offset));
return true;
- case PcodeDataTypeManager.TYPE_ARRAY: {
+ case TYPE_ARRAY: {
if (arrayIsPrimitive && depth != 0) {
if (primitives.size() >= max) {
return false;
@@ -240,9 +241,9 @@ public class PrimitiveExtractor {
}
return true;
}
- case PcodeDataTypeManager.TYPE_UNION:
+ case TYPE_UNION:
return handleUnion((Union) dt, max, offset);
- case PcodeDataTypeManager.TYPE_STRUCT:
+ case TYPE_STRUCT:
break;
default:
return false;
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Decoder.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Decoder.java
index 582c3c3c6f..51015f1049 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Decoder.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Decoder.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -265,6 +265,22 @@ public interface Decoder extends ByteIngest {
*/
public int readOpcode(AttributeId attribId) throws DecoderException;
+ /**
+ * Parse the current attribute as a data-type meta-type (MetaDataType).
+ * The last attribute, as returned by getNextAttributeId, is returned as a meta-type.
+ * @return the meta-type associated with the current attribute
+ * @throws DecoderException if the expected value is not present
+ */
+ public MetaDataType readDataTypeMeta() throws DecoderException;
+
+ /**
+ * Find the specific attribute in the current element and return it as a data-type meta-type.
+ * @param attribId is the specific attribute id to match
+ * @return the meta-type associated with the current attribute
+ * @throws DecoderException if the expected value is not present
+ */
+ public MetaDataType readDataTypeMeta(AttributeId attribId) throws DecoderException;
+
/**
* Skip parsing of the next element
* The element skipped is the one that would be opened by the next call to openElement.
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java
index 15273c6041..faef83e056 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/Encoder.java
@@ -146,4 +146,12 @@ public interface Encoder {
* @throws IOException for errors in the underlying stream
*/
void writeOpcode(AttributeId attribId, int opcode) throws IOException;
+
+ /**
+ * Write a data-type metatype (MetaDataType) into the encoding.
+ * @param attribId is the attribute to associate with the metatype
+ * @param metatype is the metatype
+ * @throws IOException for errors in the underlying stream
+ */
+ void writeDataTypeMeta(AttributeId attribId, MetaDataType metatype) throws IOException;
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java
index edaee61836..4f7b455929 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/LinkedByteBuffer.java
@@ -33,6 +33,7 @@ public class LinkedByteBuffer {
public LinkedByteBuffer buffer; // The buffer object
public ArrayIter seqIter; // Linked-list node of the current page
public byte[] array; // The byte data of the current page
+ public int length; // Number of bytes stored in the array
public int current; // Position within page of current byte
/**
@@ -42,6 +43,7 @@ public class LinkedByteBuffer {
public void copy(Position pos) {
seqIter = pos.seqIter;
array = pos.array;
+ length = pos.length;
current = pos.current;
}
@@ -60,7 +62,7 @@ public class LinkedByteBuffer {
*/
public final byte getBytePlus1() throws DecoderException {
int plus1 = current + 1;
- if (plus1 == array.length) {
+ if (plus1 == length) {
ArrayIter iter = seqIter.next;
if (iter == null) {
iter = buffer.readNextPage(seqIter);
@@ -78,7 +80,7 @@ public class LinkedByteBuffer {
public final byte getNextByte() throws DecoderException {
byte res = array[current];
current += 1;
- if (current != array.length) {
+ if (current != length) {
return res;
}
if (seqIter.next == null) {
@@ -88,6 +90,7 @@ public class LinkedByteBuffer {
seqIter = seqIter.next;
}
array = seqIter.array;
+ length = seqIter.length;
current = 0;
return res;
}
@@ -98,8 +101,8 @@ public class LinkedByteBuffer {
* @throws DecoderException if the end of stream is reached
*/
public final void advancePosition(int skip) throws DecoderException {
- while (array.length - current <= skip) {
- skip -= (array.length - current);
+ while (length - current <= skip) {
+ skip -= (length - current);
if (seqIter.next == null) {
seqIter = buffer.readNextPage(seqIter);
}
@@ -107,6 +110,7 @@ public class LinkedByteBuffer {
seqIter = seqIter.next;
}
array = seqIter.array;
+ length = seqIter.length;
current = 0;
}
current += skip;
@@ -119,6 +123,7 @@ public class LinkedByteBuffer {
public static class ArrayIter {
public ArrayIter next; // The next-node in the list
public byte[] array; // Byte data contained in this page
+ public int length; // Number of bytes stored in the buffer
}
public final static int BUFFER_SIZE = 1024;
@@ -140,6 +145,7 @@ public class LinkedByteBuffer {
padValue = pad;
maxCount = max;
initialBuffer.array = new byte[BUFFER_SIZE];
+ initialBuffer.length = BUFFER_SIZE;
initialBuffer.next = null;
description = desc;
}
@@ -165,7 +171,7 @@ public class LinkedByteBuffer {
asNeededStream = stream;
currentPos = readPage(stream, initialBuffer);
if (currentPos < BUFFER_SIZE) {
- pad();
+ endIngest();
}
getStartPosition(start);
initialBuffer = null; // Let garbage collection pick up pages that are already parsed
@@ -202,6 +208,7 @@ public class LinkedByteBuffer {
currentBuffer.next = new ArrayIter();
currentBuffer = currentBuffer.next;
currentBuffer.array = new byte[BUFFER_SIZE];
+ currentBuffer.length = BUFFER_SIZE;
currentPos = 0;
}
}
@@ -250,6 +257,7 @@ public class LinkedByteBuffer {
currentBuffer.next = new ArrayIter();
currentBuffer = currentBuffer.next;
currentBuffer.array = new byte[BUFFER_SIZE];
+ currentBuffer.length = BUFFER_SIZE;
currentPos = 0;
}
}
@@ -272,6 +280,7 @@ public class LinkedByteBuffer {
currentBuffer.next = new ArrayIter();
currentBuffer = currentBuffer.next;
currentBuffer.array = new byte[BUFFER_SIZE];
+ currentBuffer.length = BUFFER_SIZE;
currentPos = 0;
}
currentBuffer.array[currentPos++] = tok;
@@ -283,22 +292,25 @@ public class LinkedByteBuffer {
}
/**
- * Add the padValue to the end of the buffer
+ * Add the padValue to the end of the buffer and mark valid bytes in the last buffer
*/
- public void pad() {
+ public void endIngest() {
if (currentPos == BUFFER_SIZE) {
byteCount += currentPos;
currentBuffer.next = new ArrayIter();
currentBuffer = currentBuffer.next;
currentBuffer.array = new byte[1];
+ currentBuffer.length = 1;
currentPos = 0;
}
currentBuffer.array[currentPos++] = (byte) padValue;
+ currentBuffer.length = currentPos; // Any bytes after are not in the buffer
}
public void getStartPosition(Position position) {
position.array = initialBuffer.array;
+ position.length = initialBuffer.length;
position.current = 0;
position.seqIter = initialBuffer;
}
@@ -318,6 +330,7 @@ public class LinkedByteBuffer {
currentBuffer = new ArrayIter();
buffer.next = currentBuffer;
currentBuffer.array = new byte[BUFFER_SIZE];
+ currentBuffer.length = BUFFER_SIZE;
try {
currentPos = readPage(asNeededStream, currentBuffer);
}
@@ -325,7 +338,7 @@ public class LinkedByteBuffer {
throw new DecoderException(e.getMessage());
}
if (currentPos < BUFFER_SIZE) {
- pad();
+ endIngest();
}
return buffer.next;
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/MetaDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/MetaDataType.java
new file mode 100644
index 0000000000..273b060392
--- /dev/null
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/MetaDataType.java
@@ -0,0 +1,270 @@
+/* ###
+ * IP: GHIDRA
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package ghidra.program.model.pcode;
+
+import ghidra.program.model.data.*;
+import ghidra.program.model.data.Enum;
+
+/**
+ * Data-type meta-types. Values must match the decompiler enumeration "type_metatype".
+ * Values are comparable. A lower value indicates a more "specific" data-type.
+ */
+public enum MetaDataType {
+ TYPE_VOID(17, "void"), // Standard "void" type, absence of type
+ TYPE_SPACEBASE(16, "spacebase"),
+ TYPE_UNKNOWN(15, "unknown"), // An unknown low-level type. Treated as an unsigned integer.
+ TYPE_INT(14, "int"), // Signed integer. Signed is considered less specific than unsigned in C
+ TYPE_UINT(13, "uint"), // Unsigned integer
+ TYPE_BOOL(12, "bool"), // Boolean
+ TYPE_CODE(11, "code"), // Data is actual executable code
+ TYPE_FLOAT(10, "float"), // Floating-point
+
+ TYPE_PTR(9, "ptr"), // Pointer data-type
+ TYPE_PTRREL(8, "ptrrel"), // Pointer relative to another data-type (specialization of TYPE_PTR)
+ TYPE_ARRAY(7, "array"), // Array data-type, made up of a sequence of "element" datatype
+ TYPE_ENUM_UINT(6, "enum_uint"), // Unsigned enumeration (specialization of TYPE_UINT)
+ TYPE_ENUM_INT(5, "enum_int"), // Signed enumeration (specialization of TYPE_INT)
+ TYPE_STRUCT(4, "struct"), // Structure data-type, made up of component datatypes
+ TYPE_UNION(3, "union"), // An overlapping union of multiple datatypes
+ TYPE_PARTIALENUM(2, "partenum"), // A piece of an enumeration
+ TYPE_PARTIALSTRUCT(1, "partstruct"), // A piece of a structure
+ TYPE_PARTIALUNION(0, "partunion"); // A piece of a union data-type
+
+ private int value; // Enum value matching decompiler
+ private String name; // Name for debug marshaling
+
+ private static MetaDataType METATYPE[] = new MetaDataType[18];
+
+ static {
+ for (MetaDataType meta : values()) {
+ METATYPE[meta.value] = meta;
+ }
+ }
+
+ private MetaDataType(int val, String nm) {
+ value = val;
+ name = nm;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ @Override
+ public String toString() {
+ return name;
+ }
+
+ public static MetaDataType getById(int id) {
+ if (id < 0 || id >= METATYPE.length)
+ return null;
+ return METATYPE[id];
+ }
+
+ /**
+ * Get the decompiler meta-type associated with a data-type.
+ * @param tp is the data-type
+ * @return the meta-type
+ */
+ public static MetaDataType get(DataType tp) {
+ if (tp instanceof TypeDef) {
+ tp = ((TypeDef) tp).getBaseDataType();
+ }
+ if (tp instanceof Undefined || tp instanceof DefaultDataType) {
+ return TYPE_UNKNOWN;
+ }
+ if (tp instanceof AbstractFloatDataType) {
+ return TYPE_FLOAT;
+ }
+ if (tp instanceof Pointer) {
+ return TYPE_PTR;
+ }
+ if (tp instanceof BooleanDataType) {
+ return TYPE_BOOL;
+ }
+ if (tp instanceof AbstractSignedIntegerDataType) {
+ return TYPE_INT;
+ }
+ if (tp instanceof AbstractUnsignedIntegerDataType) {
+ return TYPE_UINT;
+ }
+ if (tp instanceof Structure) {
+ return TYPE_STRUCT;
+ }
+ if (tp instanceof Union) {
+ return TYPE_UNION;
+ }
+ if (tp instanceof Array) {
+ return TYPE_ARRAY;
+ }
+ if (tp instanceof CharDataType) {
+ return ((CharDataType) tp).isSigned() ? TYPE_INT : TYPE_UINT;
+ }
+ if (tp instanceof WideCharDataType || tp instanceof WideChar16DataType ||
+ tp instanceof WideChar32DataType) {
+ return TYPE_INT;
+ }
+ if (tp instanceof Enum) {
+ return ((Enum) tp).isSigned() ? TYPE_INT : TYPE_UINT;
+ }
+ if (tp instanceof FunctionDefinition) {
+ return TYPE_CODE;
+ }
+ if (tp instanceof AbstractStringDataType) {
+ return TYPE_ARRAY;
+ }
+ return TYPE_UNKNOWN;
+ }
+
+ /**
+ * Convert an XML marshaling string to a metatype code
+ * @param name is the string
+ * @return the metatype code or null
+ */
+ public static MetaDataType get(String name) {
+ switch (name.charAt(0)) {
+ case 'p':
+ if (name.equals("ptr")) {
+ return TYPE_PTR;
+ }
+ if (name.equals("ptrrel")) {
+ return TYPE_PTRREL;
+ }
+ if (name.equals("partenum")) {
+ return TYPE_PARTIALENUM;
+ }
+ if (name.equals("partunion")) {
+ return TYPE_PARTIALUNION;
+ }
+ break;
+ case 'a':
+ if (name.equals("array")) {
+ return TYPE_ARRAY;
+ }
+ break;
+ case 's':
+ if (name.equals("struct")) {
+ return TYPE_STRUCT;
+ }
+ if (name.equals("spacebase")) {
+ return TYPE_SPACEBASE;
+ }
+ break;
+ case 'u':
+ if (name.equals("unknown")) {
+ return TYPE_UNKNOWN;
+ }
+ else if (name.equals("uint")) {
+ return TYPE_UINT;
+ }
+ else if (name.equals("union")) {
+ return TYPE_UNION;
+ }
+ break;
+ case 'i':
+ if (name.equals("int")) {
+ return TYPE_INT;
+ }
+ break;
+ case 'f':
+ if (name.equals("float")) {
+ return TYPE_FLOAT;
+ }
+ break;
+ case 'b':
+ if (name.equals("bool")) {
+ return TYPE_BOOL;
+ }
+ break;
+ case 'c':
+ if (name.equals("code")) {
+ return TYPE_CODE;
+ }
+ break;
+ case 'v':
+ if (name.equals("void")) {
+ return TYPE_VOID;
+ }
+ break;
+ case 'e':
+ if (name.equals("enum_int")) {
+ return TYPE_ENUM_INT;
+ }
+ if (name.equals("enum_uint")) {
+ return TYPE_ENUM_UINT;
+ }
+ break;
+ default:
+ break;
+ }
+ return null;
+ }
+
+ /**
+ * Return the most "specific" data-type between the two parameters, as determined
+ * by meta-type.
+ * @param a is the first to compare
+ * @param b is the second
+ * @return the most specific data-type
+ */
+ public static DataType getMostSpecificDataType(DataType a, DataType b) {
+ DataType aCopy = a;
+ DataType bCopy = b;
+ for (;;) {
+ if (a == null) {
+ return bCopy;
+ }
+ if (b == null) {
+ return aCopy;
+ }
+ MetaDataType aMeta = get(a);
+ MetaDataType bMeta = get(b);
+ if (bMeta.getValue() < aMeta.getValue()) {
+ return bCopy;
+ }
+ else if (aMeta.getValue() < bMeta.getValue()) {
+ return aCopy;
+ }
+ if (aMeta == TYPE_PTR) {
+ if (a instanceof TypeDef) {
+ a = ((TypeDef) a).getBaseDataType();
+ }
+ if (b instanceof TypeDef) {
+ b = ((TypeDef) b).getBaseDataType();
+ }
+ a = ((Pointer) a).getDataType();
+ b = ((Pointer) b).getDataType();
+ }
+ else if (aMeta == TYPE_ARRAY) {
+ if (a instanceof TypeDef) {
+ a = ((TypeDef) a).getBaseDataType();
+ }
+ if (b instanceof TypeDef) {
+ b = ((TypeDef) b).getBaseDataType();
+ }
+ if (!(a instanceof Array) || !(b instanceof Array)) {
+ break;
+ }
+ a = ((Array) a).getDataType();
+ b = ((Array) b).getDataType();
+ }
+ else {
+ break;
+ }
+ }
+ return aCopy;
+ }
+}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java
index 7516e3a4c5..38c9d44b4c 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedDecode.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -253,7 +253,7 @@ public class PackedDecode implements Decoder, Closeable {
@Override
public void endIngest() {
- inStream.pad();
+ inStream.endIngest();
inStream.getStartPosition(endPos);
}
@@ -514,7 +514,7 @@ public class PackedDecode implements Decoder, Closeable {
length = (int) readInteger(length);
attributeRead = true;
- int curLen = curPos.array.length - curPos.current;
+ int curLen = curPos.length - curPos.current;
if (curLen >= length) {
String res = new String(curPos.array, curPos.current, length);
curPos.advancePosition(length);
@@ -526,7 +526,7 @@ public class PackedDecode implements Decoder, Closeable {
length -= curLen;
curPos.advancePosition(curLen);
while (length > 0) {
- curLen = curPos.array.length - curPos.current;
+ curLen = curPos.length - curPos.current;
if (curLen > length) {
curLen = length;
}
@@ -614,4 +614,22 @@ public class PackedDecode implements Decoder, Closeable {
curPos.copy(startPos);
return opcode;
}
+
+ @Override
+ public MetaDataType readDataTypeMeta() throws DecoderException {
+ int val = (int) readSignedInteger();
+ MetaDataType meta = MetaDataType.getById(val);
+ if (meta == null) {
+ throw new DecoderException("PackedDecoder: bad meta-type");
+ }
+ return meta;
+ }
+
+ @Override
+ public MetaDataType readDataTypeMeta(AttributeId attribId) throws DecoderException {
+ findMatchingAttribute(attribId);
+ MetaDataType metatype = readDataTypeMeta();
+ curPos.copy(startPos);
+ return metatype;
+ }
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java
index c5e4f14ce5..e9f6eb1b2f 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PackedEncode.java
@@ -19,6 +19,7 @@ import static ghidra.program.model.pcode.PackedDecode.*;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.charset.StandardCharsets;
import ghidra.pcodeCPort.opcodes.OpCode;
import ghidra.program.model.address.AddressSpace;
@@ -159,7 +160,11 @@ public class PackedEncode implements Encoder {
@Override
public void writeString(AttributeId attribId, String val) throws IOException {
- byte[] bytes = val.getBytes();
+ byte[] bytes = val.getBytes(StandardCharsets.UTF_8);
+ for (int i = 0; i < bytes.length; ++i) {
+ if (bytes[i] == 0)
+ throw new IOException("PackedEncode: String with null character");
+ }
writeHeader(ATTRIBUTE, attribId.id());
writeInteger((TYPECODE_STRING << TYPECODE_SHIFT), bytes.length);
outStream.write(bytes);
@@ -167,7 +172,11 @@ public class PackedEncode implements Encoder {
@Override
public void writeStringIndexed(AttributeId attribId, int index, String val) throws IOException {
- byte[] bytes = val.getBytes();
+ byte[] bytes = val.getBytes(StandardCharsets.UTF_8);
+ for (int i = 0; i < bytes.length; ++i) {
+ if (bytes[i] == 0)
+ throw new IOException("PackedEncode: String with null character");
+ }
writeHeader(ATTRIBUTE, attribId.id() + index);
writeInteger((TYPECODE_STRING << TYPECODE_SHIFT), bytes.length);
outStream.write(bytes);
@@ -213,6 +222,12 @@ public class PackedEncode implements Encoder {
writeInteger((TYPECODE_SIGNEDINT_POSITIVE << TYPECODE_SHIFT), opcode);
}
+ @Override
+ public void writeDataTypeMeta(AttributeId attribId, MetaDataType metatype) throws IOException {
+ writeHeader(ATTRIBUTE, attribId.id());
+ writeInteger((TYPECODE_SIGNEDINT_POSITIVE << TYPECODE_SHIFT), metatype.getValue());
+ }
+
/**
* @return the underlying stream
*/
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java
index 629c44ff5b..50d2c76fb8 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/PcodeDataTypeManager.java
@@ -17,6 +17,7 @@ package ghidra.program.model.pcode;
import static ghidra.program.model.pcode.AttributeId.*;
import static ghidra.program.model.pcode.ElementId.*;
+import static ghidra.program.model.pcode.MetaDataType.*;
import java.io.IOException;
import java.util.HashMap;
@@ -34,7 +35,6 @@ import ghidra.program.model.lang.DecompilerLanguage;
import ghidra.program.model.listing.Program;
import ghidra.program.model.symbol.NameTransformer;
import ghidra.util.UniversalID;
-import ghidra.xml.XmlParseException;
/**
*
@@ -53,32 +53,18 @@ public class PcodeDataTypeManager {
private static final long DEFAULT_DECOMPILER_ID = 0xC000000000000000L; // ID for "undefined" (decompiler side)
private static final long CODE_DECOMPILER_ID = 0xE000000000000001L; // ID for internal "code" data-type
- public static final int TYPE_VOID = 14; // Standard "void" type, absence of type
- public static final int TYPE_UNKNOWN = 12; // An unknown low-level type. Treated as an unsigned integer.
- public static final int TYPE_INT = 11; // Signed integer. Signed is considered less specific than unsigned in C
- public static final int TYPE_UINT = 10; // Unsigned integer
- public static final int TYPE_BOOL = 9; // Boolean
- public static final int TYPE_CODE = 8; // Data is actual executable code
- public static final int TYPE_FLOAT = 7; // Floating-point
-
- public static final int TYPE_PTR = 6; // Pointer data-type
- public static final int TYPE_PTRREL = 5; // Pointer relative to another data-type (specialization of TYPE_PTR)
- public static final int TYPE_ARRAY = 4; // Array data-type, made up of a sequence of "element" datatype
- public static final int TYPE_STRUCT = 3; // Structure data-type, made up of component datatypes
- public static final int TYPE_UNION = 2; // An overlapping union of multiple datatypes
-
/**
* A mapping between a DataType and its (name,id) on the decompiler side
*/
private static class TypeMap {
public DataType dt; // Full datatype object
public String name; // Name of the datatype on decompiler side
- public String metatype; // extra decompiler metatype information for the type
+ public MetaDataType metatype; // extra decompiler metatype information for the type
public boolean isChar; // Is this a character data-type
public boolean isUtf; // Is this a UTF encoded character data-type
public long id; // Calculated id for type
- public TypeMap(DecompilerLanguage lang, BuiltIn d, String meta, boolean isChar,
+ public TypeMap(DecompilerLanguage lang, BuiltIn d, MetaDataType meta, boolean isChar,
boolean isUtf, DataTypeManager manager) {
dt = d;
name = d.getDecompilerDisplayName(lang);
@@ -88,7 +74,8 @@ public class PcodeDataTypeManager {
id = manager.getID(d.clone(manager)) | BUILTIN_ID_HEADER;
}
- public TypeMap(DataType d, String nm, String meta, boolean isChar, boolean isUtf, long id) {
+ public TypeMap(DataType d, String nm, MetaDataType meta, boolean isChar, boolean isUtf,
+ long id) {
dt = d;
name = nm;
metatype = meta;
@@ -246,9 +233,9 @@ public class PcodeDataTypeManager {
decoder.closeElementSkipping(el);
return findBaseType(name, id);
}
- String meta = decoder.readString(ATTRIB_METATYPE);
+ MetaDataType meta = decoder.readDataTypeMeta(ATTRIB_METATYPE);
DataType restype = null;
- if (meta.equals("ptr")) {
+ if (meta == TYPE_PTR) {
int size = (int) decoder.readSignedInteger(ATTRIB_SIZE);
if (decoder.peekElement() != 0) {
DataType dt = decodeDataType(decoder);
@@ -257,7 +244,7 @@ public class PcodeDataTypeManager {
restype = new PointerDataType(dt, useDefaultSize ? -1 : size, progDataTypes);
}
}
- else if (meta.equals("array")) {
+ else if (meta == TYPE_ARRAY) {
int arrsize = (int) decoder.readSignedInteger(ATTRIB_ARRAYSIZE);
if (decoder.peekElement() != 0) {
DataType dt = decodeDataType(decoder);
@@ -267,11 +254,11 @@ public class PcodeDataTypeManager {
restype = new ArrayDataType(dt, arrsize, dt.getLength(), progDataTypes);
}
}
- else if (meta.equals("spacebase")) { // Typically the type of "the whole stack"
+ else if (meta == TYPE_SPACEBASE) { // Typically the type of "the whole stack"
decoder.closeElementSkipping(el); // get rid of unused "addr" element
return voidDt;
}
- else if (meta.equals("struct")) {
+ else if (meta == TYPE_STRUCT) {
// We reach here if the decompiler invents a structure, apparently
// this is a band-aid so that we don't blow up
// just make an undefined data type of the appropriate size
@@ -279,30 +266,30 @@ public class PcodeDataTypeManager {
decoder.closeElementSkipping(el);
return Undefined.getUndefinedDataType(size);
}
- else if (meta.equals("int")) {
+ else if (meta == TYPE_INT) {
int size = (int) decoder.readSignedInteger(ATTRIB_SIZE);
decoder.closeElement(el);
return AbstractIntegerDataType.getSignedDataType(size, progDataTypes);
}
- else if (meta.equals("uint")) {
+ else if (meta == TYPE_UINT) {
int size = (int) decoder.readSignedInteger(ATTRIB_SIZE);
decoder.closeElement(el);
return AbstractIntegerDataType.getUnsignedDataType(size, progDataTypes);
}
- else if (meta.equals("float")) {
+ else if (meta == TYPE_FLOAT) {
int size = (int) decoder.readSignedInteger(ATTRIB_SIZE);
decoder.closeElement(el);
// NOTE: Float lookup by length must use "raw" encoding size since
return AbstractFloatDataType.getFloatDataType(size, progDataTypes);
}
- else if (meta.equals("partunion")) {
+ else if (meta == TYPE_PARTIALUNION) {
int size = (int) decoder.readSignedInteger(ATTRIB_SIZE);
int offset = (int) decoder.readSignedInteger(ATTRIB_OFFSET);
DataType dt = decodeDataType(decoder);
decoder.closeElement(el);
return new PartialUnion(progDataTypes, dt, offset, size);
}
- else if (meta.equals("partenum")) {
+ else if (meta == TYPE_PARTIALENUM) {
int size = (int) decoder.readSignedInteger(ATTRIB_SIZE);
// int offset = (int) decoder.readSignedInteger(ATTRIB_OFFSET);
// DataType dt = decodeDataType(decoder);
@@ -377,7 +364,7 @@ public class PcodeDataTypeManager {
else {
encodeNameIdAttributes(encoder, typeDef); // Use the typedef name and id
}
- encoder.writeString(ATTRIB_METATYPE, "ptr");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_PTR);
int ptrLen = type.getLength();
if (ptrLen <= 0) {
ptrLen = size;
@@ -445,7 +432,7 @@ public class PcodeDataTypeManager {
AddressSpace space) throws IOException {
Pointer pointer = (Pointer) type.getBaseDataType();
encoder.openElement(ELEM_TYPE);
- encoder.writeString(ATTRIB_METATYPE, "ptrrel");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_PTRREL);
encodeNameIdAttributes(encoder, type);
encoder.writeSignedInteger(ATTRIB_SIZE, pointer.getLength());
if (pointerWordSize != 1) {
@@ -483,7 +470,7 @@ public class PcodeDataTypeManager {
if (sz == 0) {
sz = size;
}
- encoder.writeString(ATTRIB_METATYPE, "array");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_ARRAY);
encoder.writeSignedInteger(ATTRIB_SIZE, sz);
encoder.writeSignedInteger(ATTRIB_ARRAYSIZE, type.getNumElements());
encodeTypeRef(encoder, type.getDataType(), type.getElementLength());
@@ -505,7 +492,7 @@ public class PcodeDataTypeManager {
type = new StructureDataType(type.getCategoryPath(), type.getName(), 1);
sz = type.getLength();
}
- encoder.writeString(ATTRIB_METATYPE, "struct");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_STRUCT);
encoder.writeSignedInteger(ATTRIB_SIZE, sz);
encoder.writeSignedInteger(ATTRIB_ALIGNMENT, type.getAlignment());
type = ClassUtils.getReplacementType(type, TYPE_REPLACEMENT_ENABLED);
@@ -551,7 +538,7 @@ public class PcodeDataTypeManager {
public void encodeUnion(Encoder encoder, Union unionType) throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, unionType);
- encoder.writeString(ATTRIB_METATYPE, "union");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_UNION);
encoder.writeSignedInteger(ATTRIB_SIZE, unionType.getLength());
encoder.writeSignedInteger(ATTRIB_ALIGNMENT, unionType.getAlignment());
DataTypeComponent[] comps = unionType.getDefinedComponents();
@@ -584,9 +571,9 @@ public class PcodeDataTypeManager {
private void encodeEnum(Encoder encoder, Enum type, int size) throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, type);
- String metatype = type.isSigned() ? "enum_int" : "enum_uint";
+ MetaDataType metatype = type.isSigned() ? TYPE_ENUM_INT : TYPE_ENUM_UINT;
String[] names = type.getNames();
- encoder.writeString(ATTRIB_METATYPE, metatype);
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, metatype);
encoder.writeSignedInteger(ATTRIB_SIZE, type.getLength());
for (String name : names) {
encoder.openElement(ELEM_VAL);
@@ -613,7 +600,7 @@ public class PcodeDataTypeManager {
if (sz <= 0) {
sz = size;
}
- encoder.writeString(ATTRIB_METATYPE, signed ? "int" : "uint");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, signed ? TYPE_INT : TYPE_UINT);
encoder.writeSignedInteger(ATTRIB_SIZE, sz);
if (sz == 1) {
encoder.writeBool(ATTRIB_CHAR, true);
@@ -633,7 +620,7 @@ public class PcodeDataTypeManager {
private void encodeWideCharDataType(Encoder encoder, DataType type) throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, type);
- encoder.writeString(ATTRIB_METATYPE, "int");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_INT);
encoder.writeSignedInteger(ATTRIB_SIZE, type.getLength());
encoder.writeBool(ATTRIB_UTF, true);
encoder.closeElement(ELEM_TYPE);
@@ -648,7 +635,7 @@ public class PcodeDataTypeManager {
private void encodeStringDataType(Encoder encoder, int size) throws IOException {
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, "");
- encoder.writeString(ATTRIB_METATYPE, "array");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_ARRAY);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeSignedInteger(ATTRIB_ARRAYSIZE, size);
encodeCharTypeRef(encoder, dataOrganization.getCharSize());
@@ -664,7 +651,7 @@ public class PcodeDataTypeManager {
private void encodeStringUTF8DataType(Encoder encoder, int size) throws IOException {
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, "");
- encoder.writeString(ATTRIB_METATYPE, "array");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_ARRAY);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeSignedInteger(ATTRIB_ARRAYSIZE, size);
encodeCharTypeRef(encoder, 1); // TODO: Need to ensure that UTF8 decoding applies
@@ -680,7 +667,7 @@ public class PcodeDataTypeManager {
private void encodeUnicodeDataType(Encoder encoder, int size) throws IOException {
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, "");
- encoder.writeString(ATTRIB_METATYPE, "array");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_ARRAY);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeSignedInteger(ATTRIB_ARRAYSIZE, size / 2);
encodeCharTypeRef(encoder, 2);
@@ -696,7 +683,7 @@ public class PcodeDataTypeManager {
private void encodeUnicode32DataType(Encoder encoder, int size) throws IOException {
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, "");
- encoder.writeString(ATTRIB_METATYPE, "array");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_ARRAY);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeSignedInteger(ATTRIB_ARRAYSIZE, size / 4);
encodeCharTypeRef(encoder, 4);
@@ -713,7 +700,7 @@ public class PcodeDataTypeManager {
throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, type);
- encoder.writeString(ATTRIB_METATYPE, "code");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_CODE);
encoder.writeSignedInteger(ATTRIB_SIZE, 1); // Force size of 1
CompilerSpec cspec = program.getCompilerSpec();
FunctionPrototype fproto = new FunctionPrototype(type, cspec, voidInputIsVarargs);
@@ -730,7 +717,7 @@ public class PcodeDataTypeManager {
private void encodeBooleanDataType(Encoder encoder, DataType type) throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, type);
- encoder.writeString(ATTRIB_METATYPE, "bool");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_BOOL);
encoder.writeSignedInteger(ATTRIB_SIZE, type.getLength());
encoder.closeElement(ELEM_TYPE);
}
@@ -751,7 +738,7 @@ public class PcodeDataTypeManager {
sz = size;
}
encodeNameIdAttributes(encoder, type);
- encoder.writeString(ATTRIB_METATYPE, signed ? "int" : "uint");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, signed ? TYPE_INT : TYPE_UINT);
encoder.writeSignedInteger(ATTRIB_SIZE, sz);
encoder.closeElement(ELEM_TYPE);
}
@@ -765,7 +752,7 @@ public class PcodeDataTypeManager {
private void encodeAbstractFloatDataType(Encoder encoder, DataType type) throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, type);
- encoder.writeString(ATTRIB_METATYPE, "float");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_FLOAT);
encoder.writeSignedInteger(ATTRIB_SIZE, type.getLength());
encoder.closeElement(ELEM_TYPE);
}
@@ -787,11 +774,11 @@ public class PcodeDataTypeManager {
}
encodeNameIdAttributes(encoder, type);
if (sz < 16) {
- encoder.writeString(ATTRIB_METATYPE, "unknown");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_UNKNOWN);
}
else {
// Build an "opaque" structure with no fields
- encoder.writeString(ATTRIB_METATYPE, "struct");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_STRUCT);
}
encoder.writeSignedInteger(ATTRIB_SIZE, sz);
if (isVarLength) {
@@ -810,7 +797,7 @@ public class PcodeDataTypeManager {
private void encodeOpaqueString(Encoder encoder, DataType type, int size) throws IOException {
encoder.openElement(ELEM_TYPE);
encodeNameIdAttributes(encoder, type);
- encoder.writeString(ATTRIB_METATYPE, "struct");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_STRUCT);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeBool(ATTRIB_OPAQUESTRING, true);
encoder.writeBool(ATTRIB_VARLENGTH, true);
@@ -828,7 +815,7 @@ public class PcodeDataTypeManager {
encoder.writeSignedInteger(ATTRIB_OFFSET, 1);
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, "");
- encoder.writeString(ATTRIB_METATYPE, "array");
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, TYPE_ARRAY);
encoder.writeSignedInteger(ATTRIB_SIZE, size);
encoder.writeSignedInteger(ATTRIB_ARRAYSIZE, size);
encoder.openElement(ELEM_TYPEREF);
@@ -848,12 +835,12 @@ public class PcodeDataTypeManager {
*/
public void encodeCompositePlaceholder(Encoder encoder, DataType type)
throws IOException {
- String metaString;
+ MetaDataType metatype;
if (type instanceof Structure) {
- metaString = "struct";
+ metatype = TYPE_STRUCT;
}
else if (type instanceof Union) {
- metaString = "union";
+ metatype = TYPE_UNION;
}
else {
return; //empty. Could throw AssertException.
@@ -861,7 +848,7 @@ public class PcodeDataTypeManager {
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, type.getDisplayName());
encoder.writeUnsignedInteger(ATTRIB_ID, progDataTypes.getID(type));
- encoder.writeString(ATTRIB_METATYPE, metaString);
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, metatype);
encoder.writeSignedInteger(ATTRIB_SIZE, type.getLength());
encoder.writeSignedInteger(ATTRIB_ALIGNMENT, type.getAlignment());
encoder.writeBool(ATTRIB_INCOMPLETE, true);
@@ -1189,41 +1176,41 @@ public class PcodeDataTypeManager {
private void generateCoreTypes() {
voidDt = new VoidDataType(progDataTypes);
coreBuiltin = new HashMap<Long, TypeMap>();
- TypeMap type = new TypeMap(displayLanguage, VoidDataType.dataType, "void", false, false,
+ TypeMap type = new TypeMap(displayLanguage, VoidDataType.dataType, TYPE_VOID, false, false,
builtInDataTypes);
coreBuiltin.put(type.id, type);
for (BuiltIn dt : Undefined.getUndefinedDataTypes()) {
- type = new TypeMap(displayLanguage, dt, "unknown", false, false, builtInDataTypes);
+ type = new TypeMap(displayLanguage, dt, TYPE_UNKNOWN, false, false, builtInDataTypes);
coreBuiltin.put(type.id, type);
}
for (BuiltIn dt : AbstractIntegerDataType.getSignedDataTypes(progDataTypes)) {
- type = new TypeMap(displayLanguage, dt, "int", false, false, builtInDataTypes);
+ type = new TypeMap(displayLanguage, dt, TYPE_INT, false, false, builtInDataTypes);
coreBuiltin.put(type.id, type);
}
for (BuiltIn dt : AbstractIntegerDataType.getUnsignedDataTypes(progDataTypes)) {
- type = new TypeMap(displayLanguage, dt, "uint", false, false, builtInDataTypes);
+ type = new TypeMap(displayLanguage, dt, TYPE_UINT, false, false, builtInDataTypes);
coreBuiltin.put(type.id, type);
}
for (BuiltIn dt : AbstractFloatDataType.getFloatDataTypes(progDataTypes)) {
- type = new TypeMap(displayLanguage, dt, "float", false, false, builtInDataTypes);
+ type = new TypeMap(displayLanguage, dt, TYPE_FLOAT, false, false, builtInDataTypes);
coreBuiltin.put(type.id, type);
}
- type = new TypeMap(DataType.DEFAULT, "code", "code", false, false, CODE_DECOMPILER_ID);
+ type = new TypeMap(DataType.DEFAULT, "code", TYPE_CODE, false, false, CODE_DECOMPILER_ID);
coreBuiltin.put(type.id, type);
// Set "char" datatype
BuiltIn charDataType = new CharDataType(progDataTypes);
- String charMetatype = null;
+ MetaDataType charMetatype;
boolean isChar = false;
boolean isUtf = false;
if (charDataType instanceof CharDataType && ((CharDataType) charDataType).isSigned()) {
- charMetatype = "int";
+ charMetatype = TYPE_INT;
}
else {
- charMetatype = "uint";
+ charMetatype = TYPE_UINT;
}
if (charDataType.getLength() == 1) {
isChar = true;
@@ -1237,20 +1224,21 @@ public class PcodeDataTypeManager {
// Set up the "wchar_t" datatype
WideCharDataType wideDataType = new WideCharDataType(progDataTypes);
- wCharMap = new TypeMap(displayLanguage, wideDataType, "int", false, true, builtInDataTypes);
+ wCharMap =
+ new TypeMap(displayLanguage, wideDataType, TYPE_INT, false, true, builtInDataTypes);
coreBuiltin.put(wCharMap.id, wCharMap);
if (wideDataType.getLength() != 2) {
- wChar16Map = new TypeMap(displayLanguage, new WideChar16DataType(progDataTypes), "int",
- false, true, builtInDataTypes);
+ wChar16Map = new TypeMap(displayLanguage, new WideChar16DataType(progDataTypes),
+ TYPE_INT, false, true, builtInDataTypes);
coreBuiltin.put(wChar16Map.id, wChar16Map);
}
else {
wChar16Map = wCharMap;
}
if (wideDataType.getLength() != 4) {
- wChar32Map = new TypeMap(displayLanguage, new WideChar32DataType(progDataTypes), "int",
- false, true, builtInDataTypes);
+ wChar32Map = new TypeMap(displayLanguage, new WideChar32DataType(progDataTypes),
+ TYPE_INT, false, true, builtInDataTypes);
coreBuiltin.put(wChar32Map.id, wChar32Map);
}
else {
@@ -1258,7 +1246,8 @@ public class PcodeDataTypeManager {
}
BuiltIn boolDataType = new BooleanDataType(progDataTypes);
- type = new TypeMap(displayLanguage, boolDataType, "bool", false, false, builtInDataTypes);
+ type =
+ new TypeMap(displayLanguage, boolDataType, TYPE_BOOL, false, false, builtInDataTypes);
coreBuiltin.put(type.id, type);
// Set aside the "byte" builtin for encoding byte references
@@ -1278,7 +1267,7 @@ public class PcodeDataTypeManager {
encoder.openElement(ELEM_TYPE);
encoder.writeString(ATTRIB_NAME, typeMap.name);
encoder.writeSignedInteger(ATTRIB_SIZE, typeMap.dt.getLength());
- encoder.writeString(ATTRIB_METATYPE, typeMap.metatype);
+ encoder.writeDataTypeMeta(ATTRIB_METATYPE, typeMap.metatype);
if (typeMap.isChar) {
encoder.writeBool(ATTRIB_CHAR, true);
}
@@ -1290,160 +1279,4 @@ public class PcodeDataTypeManager {
}
encoder.closeElement(ELEM_CORETYPES);
}
-
- /**
- * Get the decompiler meta-type associated with a data-type.
- * @param tp is the data-type
- * @return the meta-type
- */
- public static int getMetatype(DataType tp) {
- if (tp instanceof TypeDef) {
- tp = ((TypeDef) tp).getBaseDataType();
- }
- if (tp instanceof Undefined) {
- return TYPE_UNKNOWN;
- }
- if (tp instanceof AbstractFloatDataType) {
- return TYPE_FLOAT;
- }
- if (tp instanceof Pointer) {
- return TYPE_PTR;
- }
- if (tp instanceof BooleanDataType) {
- return TYPE_BOOL;
- }
- if (tp instanceof AbstractSignedIntegerDataType) {
- return TYPE_INT;
- }
- if (tp instanceof AbstractUnsignedIntegerDataType) {
- return TYPE_UINT;
- }
- if (tp instanceof Structure) {
- return TYPE_STRUCT;
- }
- if (tp instanceof Union) {
- return TYPE_UNION;
- }
- if (tp instanceof Array) {
- return TYPE_ARRAY;
- }
- if (tp instanceof CharDataType) {
- return ((CharDataType) tp).isSigned() ? TYPE_INT : TYPE_UINT;
- }
- if (tp instanceof WideCharDataType || tp instanceof WideChar16DataType ||
- tp instanceof WideChar32DataType) {
- return TYPE_INT;
- }
- if (tp instanceof Enum) {
- return ((Enum) tp).isSigned() ? TYPE_INT : TYPE_UINT;
- }
- if (tp instanceof FunctionDefinition) {
- return TYPE_CODE;
- }
- return TYPE_UNKNOWN;
- }
-
- /**
- * Convert an XML marshaling string to a metatype code
- * @param metaString is the string
- * @return the metatype code
- * @throws XmlParseException if the string does not represent a valid metatype
- */
- public static int getMetatype(String metaString) throws XmlParseException {
- switch (metaString.charAt(0)) {
- case 'p':
- if (metaString.equals("ptr")) {
- return TYPE_PTR;
- }
- else if (metaString.equals("ptrrel")) {
- return TYPE_PTRREL;
- }
- break;
- case 'a':
- if (metaString.equals("array")) {
- return TYPE_ARRAY;
- }
- break;
- case 's':
- if (metaString.equals("struct")) {
- return TYPE_STRUCT;
- }
- break;
- case 'u':
- if (metaString.equals("unknown")) {
- return TYPE_UNKNOWN;
- }
- else if (metaString.equals("uint")) {
- return TYPE_UINT;
- }
- else if (metaString.equals("union")) {
- return TYPE_UNION;
- }
- break;
- case 'i':
- if (metaString.equals("int")) {
- return TYPE_INT;
- }
- break;
- case 'f':
- if (metaString.equals("float")) {
- return TYPE_FLOAT;
- }
- break;
- case 'b':
- if (metaString.equals("bool")) {
- return TYPE_BOOL;
- }
- break;
- case 'c':
- if (metaString.equals("code")) {
- return TYPE_CODE;
- }
- break;
- case 'v':
- if (metaString.equals("void")) {
- return TYPE_VOID;
- }
- break;
- default:
- break;
- }
- throw new XmlParseException("Unknown metatype: " + metaString);
- }
-
- /**
- * Convert a decompiler metatype code to a string for XML marshaling
- * @param meta is the metatype
- * @return the marshaling string
- * @throws IOException is the metatype is invalid
- */
- public static String getMetatypeString(int meta) throws IOException {
- switch (meta) {
- case TYPE_VOID:
- return "void";
- case TYPE_UNKNOWN:
- return "unknown";
- case TYPE_INT:
- return "int";
- case TYPE_UINT:
- return "uint";
- case TYPE_BOOL:
- return "bool";
- case TYPE_CODE:
- return "code";
- case TYPE_FLOAT:
- return "float";
- case TYPE_PTR:
- return "ptr";
- case TYPE_PTRREL:
- return "ptrrel";
- case TYPE_ARRAY:
- return "array";
- case TYPE_STRUCT:
- return "struct";
- case TYPE_UNION:
- return "union";
- }
- throw new IOException("Unknown metatype");
- }
}
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java
index 7809b2eadb..cf852ae59a 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/XmlEncode.java
@@ -255,6 +255,24 @@ public class XmlEncode implements CachedEncoder {
writeOpcode(attribId, OpCode.getOpcode(opcode));
}
+ @Override
+ public void writeDataTypeMeta(AttributeId attribId, MetaDataType metatype) throws IOException {
+ String name = metatype.toString();
+ if (attribId == ATTRIB_CONTENT) {
+ if (tagStatus == TAG_START) {
+ buffer.append('>');
+ }
+ buffer.append(name);
+ tagStatus = TAG_CONTENT;
+ return;
+ }
+ buffer.append(' ');
+ buffer.append(attribId.name());
+ buffer.append("=\"");
+ buffer.append(name);
+ buffer.append("\"");
+ }
+
@Override
public void writeTo(OutputStream stream) throws IOException {
byte[] res = buffer.toString().getBytes();