Skip to content

Ghidra is a software reverse engineering (SRE) framework

Don't use StringUtilties.toQuotedString.

Alessandro Gatti0c97e44bf784
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java
index 63ab6f4293..1f4e742c21 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/AbstractIntegerDataType.java
@@ -18,13 +18,13 @@ package ghidra.program.model.data;
import java.math.BigInteger;
import ghidra.docking.settings.*;
+import ghidra.program.model.data.RenderUnicodeSettingsDefinition.RENDER_ENUM;
import ghidra.program.model.mem.ByteMemBufferImpl;
import ghidra.program.model.mem.MemBuffer;
import ghidra.program.model.scalar.Scalar;
import ghidra.util.BigEndianDataConverter;
import ghidra.util.LittleEndianDataConverter;
import ghidra.util.StringFormat;
-import ghidra.util.StringUtilities;
/**
* Base type for integer data types such as {@link CharDataType chars}, {@link IntegerDataType ints},
@@ -281,17 +281,10 @@ public abstract class AbstractIntegerDataType extends BuiltIn implements ArraySt
}
MemBuffer memBuf = new ByteMemBufferImpl(null, bytes, true);
- StringDataInstance instance = new StringDataInstance(this, settings, memBuf, nominalLen);
- if (bytes.length == 1) {
- return instance.getCharRepresentation();
- }
-
- String stringRepresentation = instance.getStringRepresentation();
- if (stringRepresentation.length() != nominalLen) {
- stringRepresentation = StringUtilities.toQuotedString(bytes);
- }
-
- return stringRepresentation;
+ StringDataInstance instance = new StringDataInstance(this, settings, memBuf,
+ nominalLen, RENDER_ENUM.ESC_SEQ);
+ return bytes.length == 1 ? instance.getCharRepresentation() :
+ instance.getStringRepresentation();
}
String valStr;
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java
index 2ee4c710b0..2c6767698a 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/data/StringDataInstance.java
@@ -205,6 +205,37 @@ public class StringDataInstance {
this.length = length;
}
+ /**
+ * Creates a string instance using the data in the {@link MemBuffer} and the settings
+ * pulled from the {@link AbstractStringDataType string data type} but using the given
+ * {@link RenderUnicodeSettingsDefinition.RENDER_ENUM rendering setting}.
+ *
+ * @param stringDataType {@link AbstractStringDataType} common string base data type.
+ * @param settings {@link Settings} attached to the data location.
+ * @param buf {@link MemBuffer} containing the data.
+ * @param length Length passed from the caller to the datatype. -1 indicates a 'probe'
+ * trying to detect the length of an unknown string, otherwise it will be the length
+ * of the containing field of the data instance.
+ * @param renderSettings How to render the instance contents.
+ */
+ public StringDataInstance(DataType dataType, Settings settings, MemBuffer buf, int length,
+ RenderUnicodeSettingsDefinition.RENDER_ENUM renderSettings) {
+ settings = (settings == null) ? SettingsImpl.NO_SETTINGS : settings;
+ this.buf = buf;
+ this.charsetName = getCharsetNameFromDataTypeOrSettings(dataType, settings);
+ this.charSize = CharsetInfo.getInstance().getCharsetCharSize(charsetName);
+ // NOTE: for now only handle padding for charSize == 1
+ this.paddedCharSize =
+ charSize == 1 ? getDataOrganization(dataType).getCharSize() : charSize;
+ this.stringLayout = getLayoutFromDataType(dataType);
+ this.showTranslation = TRANSLATION.isShowTranslated(settings);
+ this.translatedValue = TRANSLATION.getTranslatedValue(settings);
+ this.renderSetting = renderSettings;
+ this.endianSetting = ENDIAN.getEndianess(settings, null);
+
+ this.length = length;
+ }
+
private StringDataInstance(StringDataInstance copyFrom, StringLayoutEnum newLayout,
MemBuffer newBuf, int newLen) {
this.charSize = copyFrom.charSize;
@@ -234,6 +265,9 @@ public class StringDataInstance {
if (dataType instanceof AbstractStringDataType) {
return ((AbstractStringDataType) dataType).getStringLayout();
}
+ if (dataType instanceof AbstractIntegerDataType) {
+ return StringLayoutEnum.FIXED_LEN;
+ }
return StringLayoutEnum.NULL_TERMINATED_BOUNDED;
}