Ghidra is a software reverse engineering (SRE) framework
GP-444 cleanup LEB128, consolidate impls, fix bug w/reading bad value, signed values
dev747368c9aaea4252f7
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/BinaryReader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/BinaryReader.javaindex 9dca6ebcdc..c07c127c00 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/BinaryReader.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/BinaryReader.java@@ -74,11 +74,20 @@ public class BinaryReader {*/public BinaryReader clone(long newIndex) {BinaryReader clone = new BinaryReader(provider, isLittleEndian());- clone.converter = converter;clone.currentIndex = newIndex;return clone;}+ /**+ * Returns an independent clone of this reader positioned at the same index.+ *+ * @return a independent clone of this reader positioned at the same index+ */+ @Override+ public BinaryReader clone() {+ return clone(currentIndex);+ }+/*** Returns true if this reader will extract values in little endian,* otherwise in big endian.diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/FileEntry.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/FileEntry.javaindex 9a73c6127c..90f7c49b12 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/FileEntry.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/FileEntry.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -19,33 +18,34 @@ package ghidra.app.util.bin.format.dwarf.line;import java.io.IOException;import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.format.dwarf4.LEB128;public class FileEntry {private String fileName;- private LEB128 directoryIndex;- private LEB128 lastModifiedTime;- private LEB128 fileLengthInBytes;+ private long directoryIndex;+ private long lastModifiedTime;+ private long fileLengthInBytes;FileEntry(BinaryReader reader) throws IOException {fileName = reader.readNextAsciiString();if (fileName.length() == 0) {return;}- directoryIndex = new LEB128(reader, false);- lastModifiedTime = new LEB128(reader, false);- fileLengthInBytes = new LEB128(reader, false);+ directoryIndex = LEB128.readAsLong(reader, false);+ lastModifiedTime = LEB128.readAsLong(reader, false);+ fileLengthInBytes = LEB128.readAsLong(reader, false);}public String getFileName() {return fileName;}- public LEB128 getDirectoryIndex() {+ public long getDirectoryIndex() {return directoryIndex;}- public LEB128 getLastModifiedTime() {+ public long getLastModifiedTime() {return lastModifiedTime;}- public LEB128 getFileLengthInBytes() {+ public long getFileLengthInBytes() {return fileLengthInBytes;}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/LEB128.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/LEB128.javadeleted file mode 100644index 90c2ba9d44..0000000000--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/LEB128.java+++ /dev/null@@ -1,48 +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.app.util.bin.format.dwarf.line;--import java.io.IOException;--import ghidra.app.util.bin.BinaryReader;--class LEB128 {- private long value;-- LEB128(BinaryReader reader, boolean isSigned) throws IOException {- if (isSigned) {- throw new UnsupportedOperationException();- }- int shift = 0;- while (true) {- int nextByte = reader.readNextByte() & 0xff;- value |= ((nextByte & 0x7f) << shift);- if ((nextByte & 0x80) == 0) {- break;- }- shift += 7;- }- }-- long getValue() {- return value;- }-- @Override- public String toString() {- return "0x" + Long.toHexString(value);- }-}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramInstructions.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramInstructions.javaindex 55db62c520..c38ceab821 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramInstructions.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramInstructions.java@@ -16,6 +16,7 @@package ghidra.app.util.bin.format.dwarf.line;import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.format.dwarf4.LEB128;import java.io.IOException;@@ -92,7 +93,7 @@ public final class StatementProgramInstructions {}private void executeExtended(int opcode) throws IOException {- LEB128 length = new LEB128(reader, false);+ long length = LEB128.readAsLong(reader, false);long oldIndex = reader.getPointerIndex();int extendedOpcode = reader.readNextByte();@@ -110,7 +111,7 @@ public final class StatementProgramInstructions {throw new UnsupportedOperationException();}- if (oldIndex + length.getValue() != reader.getPointerIndex()) {+ if (oldIndex + length != reader.getPointerIndex()) {throw new IllegalStateException("Index values do not match!");}}@@ -122,23 +123,23 @@ public final class StatementProgramInstructions {break;}case DW_LNS_advance_pc: {- LEB128 value = new LEB128(reader, false);- machine.address += (value.getValue() * prologue.getMinimumInstructionLength());+ long value = LEB128.readAsLong(reader, false);+ machine.address += (value * prologue.getMinimumInstructionLength());break;}case DW_LNS_advance_line: {- LEB128 value = new LEB128(reader, false);- machine.line += value.getValue();+ long value = LEB128.readAsLong(reader, false);+ machine.line += value;break;}case DW_LNS_set_file: {- LEB128 value = new LEB128(reader, false);- machine.file = (int) value.getValue();+ long value = LEB128.readAsLong(reader, false);+ machine.file = (int) value;break;}case DW_LNS_set_column: {- LEB128 value = new LEB128(reader, false);- machine.column = (int) value.getValue();+ long value = LEB128.readAsLong(reader, false);+ machine.column = (int) value;break;}case DW_LNS_negate_statement: {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramPrologue.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramPrologue.javaindex ffba47f952..fa353c695c 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramPrologue.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf/line/StatementProgramPrologue.java@@ -168,10 +168,10 @@ public class StatementProgramPrologue {* @param directoryIndex the directory index* @return the directory or current directory*/- public String getDirectoryByIndex(LEB128 directoryIndex) {- if (directoryIndex.getValue() == 0) {+ public String getDirectoryByIndex(long directoryIndex) {+ if (directoryIndex == 0) {return ".";}- return includeDirectories.get((int)directoryIndex.getValue() - 1);+ return includeDirectories.get((int)directoryIndex - 1);}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAbbreviation.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAbbreviation.javaindex c484ed7116..0c5738c9d2 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAbbreviation.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAbbreviation.java@@ -43,11 +43,11 @@ public class DWARFAbbreviationTaskMonitor monitor)throws IOException, CancelledException {- int ac = LEB128.decode32u(reader);+ int ac = LEB128.readAsUInt32(reader);if (ac == 0) {return null;}- int tag = LEB128.decode32u(reader);+ int tag = LEB128.readAsUInt32(reader);DWARFChildren hasChildren = DWARFChildren.find((int) reader.readNextByte());// Read each attribute specification until attribute and its value is 0diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAttributeSpecification.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAttributeSpecification.javaindex 8f10672f53..b870e27544 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAttributeSpecification.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFAttributeSpecification.java@@ -38,8 +38,8 @@ public class DWARFAttributeSpecification {* @throws IOException*/public static DWARFAttributeSpecification read(BinaryReader reader) throws IOException {- int attribute = LEB128.decode32u(reader);- DWARFForm attributeForm = DWARFForm.find(LEB128.decode32u(reader));+ int attribute = LEB128.readAsUInt32(reader);+ DWARFForm attributeForm = DWARFForm.find(LEB128.readAsUInt32(reader));return attribute != 0 && attributeForm != DWARFForm.NULL? new DWARFAttributeSpecification(attribute, attributeForm) : null;diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.javaindex fb7215fed6..1bed4feea9 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DWARFLine.java@@ -201,9 +201,9 @@ public class DWARFLine {// This entry exists only if the length of the string is more than 0if (this.name.length() > 0) {- this.directory_index = LEB128.decode(reader, false);- this.modification_time = LEB128.decode(reader, false);- this.length = LEB128.decode(reader, false);+ this.directory_index = LEB128.readAsLong(reader, false);+ this.modification_time = LEB128.readAsLong(reader, false);+ this.length = LEB128.readAsLong(reader, false);}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DebugInfoEntry.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DebugInfoEntry.javaindex 9c5e885253..ebfcc9c6ba 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DebugInfoEntry.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/DebugInfoEntry.java@@ -53,7 +53,7 @@ public class DebugInfoEntry {public static DebugInfoEntry read(BinaryReader reader, DWARFCompilationUnit unit,DWARFAttributeFactory attributeFactory) throws IOException {long offset = reader.getPointerIndex();- int abbreviationCode = LEB128.decode32u(reader);+ int abbreviationCode = LEB128.readAsUInt32(reader);// Check for terminator DIEif (abbreviationCode == 0) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/LEB128.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/LEB128.javaindex 10277d6fa3..e9b3e19e52 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/LEB128.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/LEB128.java@@ -18,75 +18,198 @@ package ghidra.app.util.bin.format.dwarf4;import java.io.IOException;import ghidra.app.util.bin.BinaryReader;-import ghidra.util.NumberUtil;-public final class LEB128 {+import ghidra.app.util.bin.ByteArrayProvider;++/**+ * Class to hold result of reading a LEB128 value, along with size and position metadata.+ * <p>+ * Note: If a LEB128 value that would result in a native value longer than 64bits is attempted to+ * be read, an {@link IOException} will be thrown, and the stream's position will be left at the last read byte.+ * <p>+ * If this was a valid (but overly large) LEB128, the caller's stream will be left still pointing to LEB data.+ * <p>+ */+public class LEB128 {++ private final long offset;+ private final long value;+ private final int byteLength;++ private LEB128(long offset, long value, int byteLength) {+ this.offset = offset;+ this.value = value;+ this.byteLength = byteLength;+ }++ /**+ * Returns the value as an unsigned int32. If the actual value+ * is outside the positive range of a java int (ie. 0.. {@link Integer#MAX_VALUE}),+ * an exception is thrown.+ *+ * @return int in the range of 0 to {@link Integer#MAX_VALUE}+ * @throws IOException if value is outside range+ */+ public int asUInt32() throws IOException {+ ensureInt32u(value);+ return (int) value;+ }++ /**+ * Returns the value as an signed int32. If the actual value+ * is outside the range of a java int (ie. {@link Integer#MIN_VALUE}.. {@link Integer#MAX_VALUE}),+ * an exception is thrown.+ *+ * @return int in the range of {@link Integer#MIN_VALUE} to {@link Integer#MAX_VALUE}+ * @throws IOException if value is outside range+ */+ public int asInt32() throws IOException {+ ensureInt32s(value);+ return (int) value;+ }++ /**+ * Returns the value as a 64bit primitive long. Interpreting the signed-ness of the+ * value will depend on the way the value was read (ie. if {@link #readSignedValue(BinaryReader)}+ * vs. {@link #readUnsignedValue(BinaryReader)} was used).+ *+ * @return long value.+ */+ public long asLong() {+ return value;+ }++ /**+ * Returns the offset of the LEB128 value in the stream it was read from.+ *+ * @return stream offset of the LEB128 value+ */+ public long getOffset() {+ return offset;+ }/**- * Decode a LEB128 signed number and return it as a java 32 bit int.+ * Returns the number of bytes that were used to store the LEB128 value in the stream+ * it was read from.+ *+ * @return number of bytes used to store the read LEB128 value+ */+ public int getLength() {+ return byteLength;+ }++ @Override+ public String toString() {+ return String.format("LEB128: value: %d, offset: %d, byteLength: %d", value, offset,+ byteLength);+ }++ /**+ * Reads a LEB128 value from the BinaryReader and returns a {@link LEB128} instance+ * that contains the value along with size and position metadata.+ * <p>+ * See {@link #readAsLong(BinaryReader, boolean)}.+ *+ * @param reader {@link BinaryReader} to read bytes from+ * @param isSigned true if the value is signed+ * @return a {@link LEB128} instance with the read LEB128 value with metadata+ * @throws IOException if an I/O error occurs or value is outside the range of a java+ * 64 bit int+ */+ public static LEB128 readValue(BinaryReader reader, boolean isSigned) throws IOException {+ long offset = reader.getPointerIndex();+ long value = LEB128.readAsLong(reader, isSigned);+ int size = (int) (reader.getPointerIndex() - offset);+ return new LEB128(offset, value, size);+ }++ /**+ * Reads an unsigned LEB128 value from the BinaryReader and returns a {@link LEB128} instance+ * that contains the value along with size and position metadata.+ * <p>+ * See {@link #readAsLong(BinaryReader, boolean)}.+ *+ * @param reader {@link BinaryReader} to read bytes from+ * @return a {@link LEB128} instance with the read LEB128 value with metadata+ * @throws IOException if an I/O error occurs or value is outside the range of a java+ * 64 bit int+ */+ public static LEB128 readUnsignedValue(BinaryReader reader) throws IOException {+ return readValue(reader, false);+ }++ /**+ * Reads an signed LEB128 value from the BinaryReader and returns a {@link LEB128} instance+ * that contains the value along with size and position metadata.+ * <p>+ * See {@link #readAsLong(BinaryReader, boolean)}.+ *+ * @param reader {@link BinaryReader} to read bytes from+ * @return a {@link LEB128} instance with the read LEB128 value with metadata+ * @throws IOException if an I/O error occurs or value is outside the range of a java+ * 64 bit int+ */+ public static LEB128 readSignedValue(BinaryReader reader) throws IOException {+ return readValue(reader, true);+ }++ /**+ * Reads a LEB128 signed number from the BinaryReader and returns it as a java 32 bit int.* <p>* If the value of the number can not fit in the int type, an {@link IOException} will* be thrown.*- * @param reader- * @return- * @throws IOException+ * @param reader {@link BinaryReader} to read bytes from+ * @return signed int32 value+ * @throws IOException if error reading bytes or value is outside the+ * range of a signed int32*/- public static int decode32s(BinaryReader reader) throws IOException {- long tmp = decode(reader, true);- if (tmp < Integer.MIN_VALUE || tmp > Integer.MAX_VALUE) {- throw new IOException(- "LEB128 value out of range for java 32 bit signed int: " + Long.toString(tmp));- }-+ public static int readAsInt32(BinaryReader reader) throws IOException {+ long tmp = readAsLong(reader, true);+ ensureInt32s(tmp);return (int) tmp;}/**- * Decode a LEB128 unsigned number and return it as a java 32 bit int.+ * Reads a LEB128 unsigned number from the BinaryReader and returns it as a java 32 bit int.* <p>* If the value of the number can not fit in the positive range of the int type,* an {@link IOException} will be thrown.*- * @param reader- * @return- * @throws IOException+ * @param reader {@link BinaryReader} to read bytes from+ * @return unsigned int32 value 0..Integer.MAX_VALUE+ * @throws IOException if error reading bytes or value is outside the+ * positive range of a java 32 bit int (ie. 0..Integer.MAX_VALUE)*/- public static int decode32u(BinaryReader reader) throws IOException {- long tmp = decode(reader, false);-- // NOTE: will only be lt 0 if tmp's value was larger than what fits in signed long and it wrapped.- if (tmp < 0 || tmp > Integer.MAX_VALUE) {- throw new IOException("LEB128 value out of range for java 32 bit unsigned int: " +- Long.toUnsignedString(tmp));- }-+ public static int readAsUInt32(BinaryReader reader) throws IOException {+ long tmp = readAsLong(reader, false);+ ensureInt32u(tmp);return (int) tmp;}/**- * Decodes a LEB128 number using a binary reader and stores it in a long.+ * Reads a LEB128 number from the BinaryReader and returns it as a java 64 bit long int.* <p>- * Large unsigned integers that use 64 bits will be returned in java's native+ * Large unsigned integers that use all 64 bits are be returned in a java native* 'long' type, which is signed. It is up to the caller to treat the value as unsigned.* <p>* Large integers that use more than 64 bits will cause an IOException to be thrown.* <p>- * @param reader the binary reader+ * @param reader {@link BinaryReader} to read bytes from* @param isSigned true if the value is signed- * @throws IOException if an I/O error occurs+ * @return long integer value. Caller must treat it as unsigned if isSigned parameter was+ * set to false+ * @throws IOException if an I/O error occurs or value is outside the range of a java+ * 64 bit int*/- public static long decode(BinaryReader reader, boolean isSigned) throws IOException {+ public static long readAsLong(BinaryReader reader, boolean isSigned) throws IOException {int nextByte = 0;int shift = 0;long value = 0;- boolean overflow = false;while (true) {nextByte = reader.readNextUnsignedByte();if (shift == 70 || (isSigned == false && shift == 63 && nextByte > 1)) {- // if the value being read is more than 64 bits long mark it as overflow.- // keep reading the rest of the number so the caller is not left in the- // middle of the LEB128 number's guts.- overflow = true;+ throw new IOException(+ "Unsupported LEB128 value, too large to fit in 64bit java long variable");}// must cast to long before shifting otherwise shift values greater than 32 cause problems@@ -97,63 +220,63 @@ public final class LEB128 {break;}}- if (overflow) {- throw new IOException(- "Unsupported LEB128 value, too large to fit in 64bit java long variable");- }if ((isSigned) && (shift < Long.SIZE) && ((nextByte & 0x40) != 0)) {- value |= -(1 << shift);+ // 0x40 is the new 'high' sign bit since 0x80 is the continuation flag+ value |= (-1L << shift);}return value;}/**- * Decodes a LEB128 number using a byte array and stores it in a long.- * This function cannot read numbers larger than Long.MAX_VALUE.+ * Decodes a LEB128 number from a byte array and returns it as a long.+ * <p>+ * See {@link #readAsLong(BinaryReader, boolean)}.+ ** @param bytes the bytes representing the LEB128 number* @param isSigned true if the value is signed- * @throws IOException+ * @return long integer value. Caller must treat it as unsigned if isSigned parameter was+ * set to false+ * @throws IOException if error reading bytes or value is outside the+ * range of a java 64 bit int*/public static long decode(byte[] bytes, boolean isSigned) throws IOException {return decode(bytes, 0, isSigned);}/**- * Decodes a LEB128 number using an offset into a byte array and stores it in a long.- * This function cannot read numbers larger than Long.MAX_VALUE.+ * Decodes a LEB128 number from a byte array and returns it as a long.+ * <p>+ * See {@link #readAsLong(BinaryReader, boolean)}.+ ** @param bytes the bytes representing the LEB128 number- * @param offset offset into the byte array+ * @param offset offset in byte array of where to start reading bytes* @param isSigned true if the value is signed- * @throws IOException+ * @return long integer value. Caller must treat it as unsigned if isSigned parameter was+ * set to false+ * @throws IOException if error reading bytes or value is outside the+ * range of a java 64 bit int*/public static long decode(byte[] bytes, int offset, boolean isSigned) throws IOException {- int nextByte = 0;- int shift = 0;- long value = 0;- for (int i = offset; i < bytes.length; i++) {- nextByte = bytes[i] & NumberUtil.UNSIGNED_BYTE_MASK;-- if (shift == 70 || (isSigned == false && shift == 63 && nextByte > 1)) {- throw new IOException(- "Unsupported LEB128 value, too large to fit in 64bit java long variable");- }-- // must cast to long before shifting otherwise shift values greater than 32 cause problems- value |= ((long) (nextByte & 0x7F)) << shift;-- shift += 7;+ ByteArrayProvider bap = new ByteArrayProvider(bytes);+ BinaryReader br = new BinaryReader(bap, true);+ br.setPointerIndex(offset);+ return readAsLong(br, isSigned);+ }- if ((nextByte & 0x80) == 0) {- break;- }- }- if ((isSigned) && (shift < Long.SIZE) && ((nextByte & 0x40) != 0)) {- long tmp1 = (1L << shift);- long tmp2 = -tmp1;- value |= tmp2;+ private static void ensureInt32u(long value) throws IOException {+ if (value < 0 || value > Integer.MAX_VALUE) {+ throw new IOException("LEB128 value out of range for java 32 bit unsigned int: " ++ Long.toUnsignedString(value));}+ }- return value;+ private static void ensureInt32s(long value) throws IOException {+ if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {+ throw new IOException(+ "LEB128 value out of range for java 32 bit signed int: " ++ Long.toString(value));+ }}+}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/attribs/DWARFAttributeFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/attribs/DWARFAttributeFactory.javaindex 331f3d0502..c1b18fc653 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/attribs/DWARFAttributeFactory.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/attribs/DWARFAttributeFactory.java@@ -73,7 +73,7 @@ public class DWARFAttributeFactory {return new DWARFNumericAttribute(uoffset + unit.getStartOffset());}case DW_FORM_ref_udata: {- long uoffset = LEB128.decode(reader, false);+ long uoffset = LEB128.readAsLong(reader, false);return new DWARFNumericAttribute(uoffset + unit.getStartOffset());}@@ -103,7 +103,7 @@ public class DWARFAttributeFactory {return new DWARFBlobAttribute(reader.readNextByteArray(length));}case DW_FORM_block: {- int length = LEB128.decode32u(reader);+ int length = LEB128.readAsUInt32(reader);if (length < 0 || length > MAX_BLOCK4_SIZE) {throw new IOException("Invalid/bad dw_form_block size: " + length);}@@ -121,12 +121,12 @@ public class DWARFAttributeFactory {case DW_FORM_data8:return new DWARFNumericAttribute(reader.readNextLong());case DW_FORM_sdata:- return new DWARFNumericAttribute(LEB128.decode(reader, true));+ return new DWARFNumericAttribute(LEB128.readAsLong(reader, true));case DW_FORM_udata:- return new DWARFNumericAttribute(LEB128.decode(reader, false));+ return new DWARFNumericAttribute(LEB128.readAsLong(reader, false));case DW_FORM_exprloc: {- int length = LEB128.decode32u(reader);+ int length = LEB128.readAsUInt32(reader);if (length < 0 || length > MAX_BLOCK4_SIZE) {throw new IOException("Invalid/bad dw_form_exprloc size: " + length);}@@ -157,7 +157,7 @@ public class DWARFAttributeFactory {// Indirect Formcase DW_FORM_indirect:- DWARFForm formValue = DWARFForm.find(LEB128.decode32u(reader));+ DWARFForm formValue = DWARFForm.find(LEB128.readAsUInt32(reader));DWARFAttributeValue value = read(reader, unit, formValue);return new DWARFIndirectAttribute(value, formValue);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/expression/DWARFExpression.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/expression/DWARFExpression.javaindex c5b8f66e45..3743f31fbe 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/expression/DWARFExpression.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/dwarf4/expression/DWARFExpression.java@@ -137,9 +137,9 @@ public class DWARFExpression {case U_LONG:return reader.readNextLong(); /* & there is no mask for ulong */case S_LEB128:- return LEB128.decode(reader, true);+ return LEB128.readAsLong(reader, true);case U_LEB128:- return LEB128.decode(reader, false);+ return LEB128.readAsLong(reader, false);case SIZED_BLOB:throw new IOException("Can't read SIZED_BLOB as a Long value");case DWARF_INT:diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/AndroidElfRelocationTableDataType.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/AndroidElfRelocationTableDataType.javaindex 1b4b37bc19..4840bb148d 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/AndroidElfRelocationTableDataType.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/AndroidElfRelocationTableDataType.java@@ -92,7 +92,7 @@ public class AndroidElfRelocationTableDataType extends DynamicDataType {static LEB128Info parse(BinaryReader reader, boolean signed) throws IOException {long nextPos = reader.getPointerIndex();- long value = LEB128.decode(reader, signed);+ long value = LEB128.readAsLong(reader, signed);long pos = reader.getPointerIndex();int size = (int) (pos - nextPos);return new LEB128Info((int) nextPos, value, size);@@ -132,7 +132,7 @@ public class AndroidElfRelocationTableDataType extends DynamicDataType {// NOTE: assumes 2-GByte MemBuffer limitint offset = (int) reader.getPointerIndex();- long groupSize = LEB128.decode(reader, true);+ long groupSize = LEB128.readAsLong(reader, true);if (groupSize > remainingRelocations) {Msg.debug(this, "Group relocation count " + groupSize +" exceeded total count " + remainingRelocations);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.javaindex 5f80da427b..c4a8bcbaa5 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/bin/format/elf/ElfRelocationTable.java@@ -205,20 +205,20 @@ public class ElfRelocationTable implements ElfFileSection, ByteArrayConverter {try {int relocationIndex = 0;- long remainingRelocations = LEB128.decode(reader, true);- long offset = LEB128.decode(reader, true);+ long remainingRelocations = LEB128.readAsLong(reader, true);+ long offset = LEB128.readAsLong(reader, true);long addend = 0;while (remainingRelocations > 0) {- long groupSize = LEB128.decode(reader, true);+ long groupSize = LEB128.readAsLong(reader, true);if (groupSize > remainingRelocations) {Msg.warn(this, "Group relocation count " + groupSize +" exceeded total count " + remainingRelocations);break;}- long groupFlags = LEB128.decode(reader, true);+ long groupFlags = LEB128.readAsLong(reader, true);boolean groupedByInfo =(groupFlags & AndroidElfRelocationGroup.RELOCATION_GROUPED_BY_INFO_FLAG) != 0;boolean groupedByDelta = (groupFlags &@@ -228,22 +228,22 @@ public class ElfRelocationTable implements ElfFileSection, ByteArrayConverter {boolean groupHasAddend =(groupFlags & AndroidElfRelocationGroup.RELOCATION_GROUP_HAS_ADDEND_FLAG) != 0;- long groupOffsetDelta = groupedByDelta ? LEB128.decode(reader, true) : 0;- long groupRInfo = groupedByInfo ? LEB128.decode(reader, true) : 0;+ long groupOffsetDelta = groupedByDelta ? LEB128.readAsLong(reader, true) : 0;+ long groupRInfo = groupedByInfo ? LEB128.readAsLong(reader, true) : 0;if (groupedByAddend && groupHasAddend) {- addend += LEB128.decode(reader, true);+ addend += LEB128.readAsLong(reader, true);}for (int i = 0; i < groupSize; i++) {- offset += groupedByDelta ? groupOffsetDelta : LEB128.decode(reader, true);+ offset += groupedByDelta ? groupOffsetDelta : LEB128.readAsLong(reader, true);- long info = groupedByInfo ? groupRInfo : LEB128.decode(reader, true);+ long info = groupedByInfo ? groupRInfo : LEB128.readAsLong(reader, true);long rAddend = 0;if (groupHasAddend) {if (!groupedByAddend) {- addend += LEB128.decode(reader, true);+ addend += LEB128.readAsLong(reader, true);}rAddend = addend;}diff --git a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/LEB128Test.java b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/LEB128Test.javaindex 6983990862..de4c6b4a79 100644--- a/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/LEB128Test.java+++ b/Ghidra/Features/Base/src/test/java/ghidra/app/util/bin/format/dwarf4/LEB128Test.java@@ -15,154 +15,199 @@*/package ghidra.app.util.bin.format.dwarf4;-import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.ByteArrayProvider;+import static org.junit.Assert.*;import java.io.IOException;+import java.util.List;import org.junit.Assert;import org.junit.Test;+import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.ByteArrayProvider;+import ghidra.util.NumericUtilities;+public class LEB128Test {private static final boolean BR_IS_LITTLE_ENDIAN = true;- private BinaryReader br(byte... bytes) {+ private BinaryReader br(int... intBytes) {+ byte[] bytes = new byte[intBytes.length];+ for (int i = 0; i < intBytes.length; i++) {+ bytes[i] = (byte) intBytes[i];+ }return new BinaryReader(new ByteArrayProvider(bytes), BR_IS_LITTLE_ENDIAN);}- /**- * Test reading the largest unsigned LEB128 int that we can handle (64 used bits).- * <p>- *- * @throws IOException- */- @Test- public void testMax64bitUnsigned() throws IOException {- byte[] bytes = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x01 };+ static class TestEntry {+ long expectedValue;+ byte[] bytes;- long value = LEB128.decode(bytes, false);-- // -1 signed long == MAX unsigned- Assert.assertEquals(-1, value);- }-- /**- * Test reading a number that is larger than 32 bits to ensure that all the shifting- * done in the LEB128 reader doesn't have a 32bit fault.- * @throws IOException- */- @Test- public void test36bitUnsigned() throws IOException {- byte[] bytes = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,- (byte) 0x01 };-- long value = LEB128.decode(bytes, false);- Assert.assertEquals(0xfffffffffL, value);+ TestEntry(long expectedValue, byte[] bytes) {+ this.expectedValue = expectedValue;+ this.bytes = bytes;+ }}- @Test- public void testSignedNeg2() throws IOException {- byte[] bytes = new byte[] { (byte) 0x7e };-- long value = LEB128.decode(bytes, true);- Assert.assertEquals(-2, value);+ private TestEntry te(long expectedValue, int... intBytes) {+ byte[] bytes = new byte[intBytes.length];+ for (int i = 0; i < intBytes.length; i++) {+ bytes[i] = (byte) intBytes[i];+ }+ return new TestEntry(expectedValue, bytes);}- @Test- public void testSignedNeg127() throws IOException {- byte[] bytes = new byte[] { (byte) 0x81, (byte) 0x7f };-- long value = LEB128.decode(bytes, true);- Assert.assertEquals(-127, value);- }+ private List<TestEntry> unsignedTestEntries = List.of(+ // misc+ te(0L, 0x80, 0x80, 0x80, 0x80, 0x80, 0x0), // Tests reading a zero value that is encoded in non-optimal way.+ te(-1L, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01), // -1 == MAX unsigned long+ te(0xf_ffff_ffffL, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01), // more than 32 bits to test shifting > 32bits++ // 1 byte+ te(1L, 0x01),+ te(63L, 0x3f),+ te(64L, 0x40),++ // 1 byte to 2 byte transition+ te(125L, 0x7d),+ te(126L, 0x7e),+ te(127L, 0x7f),+ te(128L, 0x80, 0x01),+ te(129L, 0x81, 0x01),+ te(130L, 0x82, 0x01),+ te(131L, 0x83, 0x01),++ te(254L, 0xfe, 0x01),+ te(255L, 0xff, 0x01),+ te(256L, 0x80, 0x02),+ te(257L, 0x81, 0x02),++ // 2 byte to 3 byte transition+ te(16382L, 0xfe, 0x7f),+ te(16383L, 0xff, 0x7f),+ te(16384L, 0x80, 0x80, 0x01),+ te(16385L, 0x81, 0x80, 0x01),++ // 3 byte to 4 byte transition+ te(2097151L, 0xff, 0xff, 0x7f),+ te(2097152L, 0x80, 0x80, 0x80, 0x01),+ te(2097153L, 0x81, 0x80, 0x80, 0x01),++ // 4 byte to 5 byte transition+ te(268435455L, 0xff, 0xff, 0xff, 0x7f),+ te(268435456L, 0x80, 0x80, 0x80, 0x80, 0x01),+ te(268435457L, 0x81, 0x80, 0x80, 0x80, 0x01),++ // 5 byte to 6 byte transition+ te(34359738367L, 0xff, 0xff, 0xff, 0xff, 0x7f),+ te(34359738368L, 0x80, 0x80, 0x80, 0x80, 0x80, 0x01),+ te(34359738369L, 0x81, 0x80, 0x80, 0x80, 0x80, 0x01)+ //+ );++ private List<TestEntry> signedTestEntries = List.of(+ // misc+ te(-2130303778817L, 0xff, 0xff, 0xff, 0xff, 0xff, 0x41),++ // 1 byte positive stuff+ te(0L, 0x00),+ te(1L, 0x01),++ // 1 byte to 2 byte transition (positive)+ te(63L, 0x3f),+ te(64L, 0xc0, 0x00),+ te(65L, 0xc1, 0x00),+ te(66L, 0xc2, 0x00),++ te(126L, 0xfe, 0x00),+ te(127L, 0xff, 0x00),+ te(128L, 0x80, 0x01),+ te(129L, 0x81, 0x01),++ te(254L, 0xfe, 0x01),+ te(255L, 0xff, 0x01),+ te(256L, 0x80, 0x02),+ te(257L, 0x81, 0x02),++ // 2 byte to 3 byte transition+ te(8190L, 0xfe, 0x3f),+ te(8191L, 0xff, 0x3f),+ te(8192L, 0x80, 0xc0, 0x00),+ te(8193L, 0x81, 0xc0, 0x00),++ // 1 byte negative stuff+ te(-1L, 0x7f),+ te(-2L, 0x7e),+ te(-3L, 0x7d),+ te(-4L, 0x7c),+ te(-5L, 0x7b),+ te(-6L, 0x7a),++ // 1 byte to 2 byte transition (negative)+ te(-64L, 0x40),+ te(-65L, 0xbf, 0x7f),++ te(-127, 0x81, 0x7f),+ te(-128, 0x80, 0x7f),+ te(-129, 0xff, 0x7e),++ // 2 byte to 3 byte transition (negative)+ te(-8191L, 0x81, 0x40),+ te(-8192L, 0x80, 0x40),+ te(-8193L, 0xff, 0xbf, 0x7f),+ te(-8194L, 0xfe, 0xbf, 0x7f)++ );@Test- public void testSignedNeg128() throws IOException {- byte[] bytes = new byte[] { (byte) 0x80, (byte) 0x7f };-- long value = LEB128.decode(bytes, true);- Assert.assertEquals(-128, value);+ public void testUnsignedTestEntries() throws IOException {+ testTestEntries(unsignedTestEntries, false, "Unsigned TestEntry");}@Test- public void testSignedNeg129() throws IOException {- byte[] bytes = new byte[] { (byte) 0xff, (byte) 0x7e };-- long value = LEB128.decode(bytes, true);- Assert.assertEquals(-129, value);+ public void testSignedTestEntries() throws IOException {+ testTestEntries(signedTestEntries, true, "Signed TestEntry");}- @Test- public void testSigned129() throws IOException {- byte[] bytes = new byte[] { (byte) 0x81, (byte) 0x1 };-- long value = LEB128.decode(bytes, true);- Assert.assertEquals(129, value);+ public void testTestEntries(List<TestEntry> testEntries, boolean signed, String name)+ throws IOException {+ for (int i = 0; i < testEntries.size(); i++) {+ TestEntry te = testEntries.get(i);+ BinaryReader br =+ new BinaryReader(new ByteArrayProvider(te.bytes), BR_IS_LITTLE_ENDIAN);+ long actualValue = LEB128.readAsLong(br, signed);+ assertEquals(String.format(+ "%s[%d] failed: leb128(%s) != %d. Expected=%d / %x, actual=%d / %x",+ name, i, NumericUtilities.convertBytesToString(te.bytes), te.expectedValue,+ te.expectedValue, te.expectedValue, actualValue, actualValue), te.expectedValue,+ actualValue);+ assertEquals(String.format("%s[%d] failed: left-over bytes: %d", name, i,+ te.bytes.length - br.getPointerIndex()), te.bytes.length, br.getPointerIndex());+ }}- /**- * Tests reading an zero value that is encoded in non-optimal way.- * @throws IOException- */- @Test- public void testAltZeroEncoded() throws IOException {- byte[] bytes = new byte[] { (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80,- (byte) 0x0 };-- long value = LEB128.decode(bytes, false);- Assert.assertEquals(0, value);- }+ @Test(expected = IOException.class)+ public void testToolargeUnsigned() throws IOException {+ // Test reading a unsigned LEB128 that is just 1 bit too large for a java 64 bit long int.- /**- * Test a 36bit signed negative value.- *- * @throws IOException- */- @Test- public void test36bitSignedNeg() throws IOException {- byte[] bytes = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,- (byte) 0x41 };+ BinaryReader br = br(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02);- long value = LEB128.decode(bytes, true);- Assert.assertEquals(-2130303778817L, value);+ long value = LEB128.readAsLong(br, false);+ Assert.fail(+ "Should not be able to read a LEB128 that is larger than what can fit in java 64bit long int: " ++ value);}- /**- * Test reading a unsigned LEB128 that is just 1 bit too large for a java 64 bit long int.- * @throws IOException- */@Test- public void testToolargeUnsigned() throws IOException {- byte[] bytes = new byte[] { (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x02 };-- try {- long value = LEB128.decode(bytes, false);- Assert.fail(- "Should not be able to read a LEB128 that is larger than what can fit in java 64bit long int: " +- value);- }- catch (IOException ioe) {- // good- }- }+ public void testTooLargeValueBinaryReaderStreamPosition() {+ // Test that the BinaryReader stream is 'correctly' positioned after the LEB128 bytes after+ // reading a LEB128 that is too large for a java 64 bit long int.- /**- * Test that the BinaryReader stream is correctly positioned after the LEB128 bytes after- * reading a LEB128 that is too large for a java 64 bit long int.- *- * @throws IOException- */- @Test- public void testToolargeBinaryReaderStreamPosition() throws IOException {- BinaryReader br = br((byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,- (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,- (byte) 0x02, (byte) 0x1, (byte) 0x2);+ BinaryReader br =+ br(0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x02, 0x1, 0x2);try {- long value = LEB128.decode(br, false);+ long value = LEB128.readAsLong(br, false);Assert.fail("Should not be able to read a LEB128 that is larger than what can fit in java 64bit long int: " +Long.toUnsignedString(value));@@ -171,38 +216,23 @@ public class LEB128Test {// good}- Assert.assertEquals(1, br.readNextByte() & 0xFF);- Assert.assertEquals(2, br.readNextByte() & 0xFF);+ Assert.assertEquals(10, br.getPointerIndex());}- /**- * Test uint32 max- *- * @throws IOException- */@Testpublic void testUint32Max() throws IOException {- int value =- LEB128.decode32u(br((byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x07));+ int value = LEB128.readAsUInt32(br(0xff, 0xff, 0xff, 0xff, 0x07));Assert.assertEquals(Integer.MAX_VALUE, value);}- /**- * Test uint32 max overflow with 0xff_ff_ff_ff- *- * @throws IOException- */- @Test+ @Test(expected = IOException.class)public void testUint32Overflow() throws IOException {- try {- int value = LEB128.decode32u(- br((byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0x0f));- Assert.fail(- "Should not be able to read a LEB128 that is larger than what can fit in java 32 bit int: " +- Integer.toUnsignedString(value));- }- catch (IOException ioe) {- // good- }++ // Test uint32 max overflow with 0xff_ff_ff_ff++ int value = LEB128.readAsUInt32(br(0xff, 0xff, 0xff, 0xff, 0x0f));+ Assert.fail(+ "Should not be able to read a LEB128 that is larger than what can fit in java 32 bit int: " ++ Integer.toUnsignedString(value));}}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/AnnotationElement.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/AnnotationElement.javaindex 7b69dc5483..6e731e06cb 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/AnnotationElement.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/AnnotationElement.java@@ -15,18 +15,14 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-public class AnnotationElement implements StructConverter {private int nameIndex;@@ -34,10 +30,9 @@ public class AnnotationElement implements StructConverter {private EncodedValue value;public AnnotationElement( BinaryReader reader ) throws IOException {- nameIndex = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );-- nameIndexLength = Leb128.unsignedLeb128Size( nameIndex );- reader.setPointerIndex( reader.getPointerIndex( ) + nameIndexLength );+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ nameIndex = leb128.asUInt32();+ nameIndexLength = leb128.getLength();value = new EncodedValue( reader );}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ClassDataItem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ClassDataItem.javaindex 2896ed4f30..065f7702e9 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ClassDataItem.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/ClassDataItem.java@@ -15,15 +15,15 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import java.util.*;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;+import ghidra.app.util.bin.format.dwarf4.LEB128;import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-import java.util.*;-public class ClassDataItem implements StructConverter {private int staticFieldsSize;@@ -36,27 +36,27 @@ public class ClassDataItem implements StructConverter {private int directMethodsSizeLength;// in bytesprivate int virtualMethodsSizeLength;// in bytes- private List< EncodedField > staticFields = new ArrayList< EncodedField >( );- private List< EncodedField > instancesFields = new ArrayList< EncodedField >( );- private List< EncodedMethod > directMethods = new ArrayList< EncodedMethod >( );- private List< EncodedMethod > virtualMethods = new ArrayList< EncodedMethod >( );+ private List< EncodedField > staticFields = new ArrayList< >( );+ private List< EncodedField > instancesFields = new ArrayList< >( );+ private List< EncodedMethod > directMethods = new ArrayList< >( );+ private List< EncodedMethod > virtualMethods = new ArrayList< >( );public ClassDataItem( BinaryReader reader ) throws IOException {- staticFieldsSize = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- staticFieldsSizeLength = Leb128.unsignedLeb128Size( staticFieldsSize );- reader.readNextByteArray( staticFieldsSizeLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ staticFieldsSize = leb128.asUInt32();+ staticFieldsSizeLength = leb128.getLength();- instanceFieldsSize = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- instanceFieldsSizeLength = Leb128.unsignedLeb128Size( instanceFieldsSize );- reader.readNextByteArray( instanceFieldsSizeLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ instanceFieldsSize = leb128.asUInt32();+ instanceFieldsSizeLength = leb128.getLength();- directMethodsSize = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- directMethodsSizeLength = Leb128.unsignedLeb128Size( directMethodsSize );- reader.readNextByteArray( directMethodsSizeLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ directMethodsSize = leb128.asUInt32();+ directMethodsSizeLength = leb128.getLength();- virtualMethodsSize = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- virtualMethodsSizeLength = Leb128.unsignedLeb128Size( virtualMethodsSize );- reader.readNextByteArray( virtualMethodsSizeLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ virtualMethodsSize = leb128.asUInt32();+ virtualMethodsSizeLength = leb128.getLength();for ( int i = 0 ; i < staticFieldsSize ; ++i ) {staticFields.add( new EncodedField( reader ) );diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.javaindex bf61a79dcf..184f5b4913 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoItem.java@@ -15,18 +15,14 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-public class DebugInfoItem implements StructConverter {private int lineStart;@@ -38,30 +34,25 @@ public class DebugInfoItem implements StructConverter {private byte [] stateMachineOpcodes;public DebugInfoItem( BinaryReader reader ) throws IOException {- lineStart = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- lineStartLength = Leb128.unsignedLeb128Size( lineStart );- reader.readNextByteArray( lineStartLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ lineStart = leb128.asUInt32();+ lineStartLength = leb128.getLength();- parametersSize = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- parametersSizeLength = Leb128.unsignedLeb128Size( parametersSize );- reader.readNextByteArray( parametersSizeLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ parametersSize = leb128.asUInt32();+ parametersSizeLength = leb128.getLength();parameterNames = new int[ parametersSize ];parameterNamesLengths = new int[ parametersSize ];for ( int i = 0 ; i < parametersSize ; ++i ) {- int value = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int valueLength = Leb128.unsignedLeb128Size( value );- reader.readNextByteArray( valueLength );// consume leb...-- parameterNames[ i ] = value - 1;// uleb128p1+ leb128 = LEB128.readUnsignedValue(reader);- parameterNamesLengths[ i ] = valueLength;+ parameterNames[i] = leb128.asUInt32() - 1;// uleb128p1+ parameterNamesLengths[i] = leb128.getLength();}- long startIndex = reader.getPointerIndex( );- int count = DebugInfoStateMachineReader.computeLength( reader );- reader.setPointerIndex( startIndex );+ int count = DebugInfoStateMachineReader.computeLength( reader.clone() );stateMachineOpcodes = reader.readNextByteArray( count );}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoStateMachineReader.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoStateMachineReader.javaindex 21ec208813..bd8ab387c7 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoStateMachineReader.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/DebugInfoStateMachineReader.java@@ -15,102 +15,64 @@*/package ghidra.file.formats.android.dex.format;-import ghidra.app.util.bin.BinaryReader;-import ghidra.file.formats.android.dex.util.Leb128;-import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.format.dwarf4.LEB128;+class DebugInfoStateMachineReader {+ private static final int MAX_SIZE = 0x10000; // 64kstatic int computeLength( BinaryReader reader ) throws IOException {- int length = 0;+ long start = reader.getPointerIndex();- while ( true ) {-- if ( length > 0x10000 ) {//don't loop forever!- return 0;- }+ while (reader.getPointerIndex() - start < MAX_SIZE) {byte opcode = reader.readNextByte( );- ++length;-switch( opcode ) {case DebugStateMachineOpCodes.DBG_END_SEQUENCE: {- return length;//done!+ return (int) (reader.getPointerIndex() - start);//done!}case DebugStateMachineOpCodes.DBG_ADVANCE_PC: {- int advance = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int advanceLength = Leb128.unsignedLeb128Size( advance );- reader.setPointerIndex( reader.getPointerIndex( ) + advanceLength );- length += advanceLength;+ LEB128.readAsUInt32(reader);break;}case DebugStateMachineOpCodes.DBG_ADVANCE_LINE: {- int advance = Leb128.readSignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int advanceLength = Leb128.signedLeb128Size( advance );- reader.setPointerIndex( reader.getPointerIndex( ) + advanceLength );- length += advanceLength;+ LEB128.readAsUInt32(reader);break;}case DebugStateMachineOpCodes.DBG_START_LOCAL: {- int register = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int registerLength = Leb128.unsignedLeb128Size( register );- reader.setPointerIndex( reader.getPointerIndex( ) + registerLength );- length += registerLength;+ int register = LEB128.readAsUInt32(reader);//TODO uleb128p1- int name = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int nameLength = Leb128.unsignedLeb128Size( name );- reader.setPointerIndex( reader.getPointerIndex( ) + nameLength );- length += nameLength;+ int name = LEB128.readAsUInt32(reader);//TODO uleb128p1- int type = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int typeLength = Leb128.unsignedLeb128Size( type );- reader.setPointerIndex( reader.getPointerIndex( ) + typeLength );- length += typeLength;+ int type = LEB128.readAsUInt32(reader);break;}case DebugStateMachineOpCodes.DBG_START_LOCAL_EXTENDED: {- int register = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int registerLength = Leb128.unsignedLeb128Size( register );- reader.setPointerIndex( reader.getPointerIndex( ) + registerLength );- length += registerLength;+ int register = LEB128.readAsUInt32(reader);//TODO uleb128p1- int name = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int nameLength = Leb128.unsignedLeb128Size( name );- reader.setPointerIndex( reader.getPointerIndex( ) + nameLength );- length += nameLength;+ int name = LEB128.readAsUInt32(reader);//TODO uleb128p1- int type = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int typeLength = Leb128.unsignedLeb128Size( type );- reader.setPointerIndex( reader.getPointerIndex( ) + typeLength );- length += typeLength;+ int type = LEB128.readAsUInt32(reader);//TODO uleb128p1- int signature = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int signatureLength = Leb128.unsignedLeb128Size( signature );- reader.setPointerIndex( reader.getPointerIndex( ) + signatureLength );- length += signatureLength;+ int signature = LEB128.readAsUInt32(reader);break;}case DebugStateMachineOpCodes.DBG_END_LOCAL: {- int register = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int registerLength = Leb128.unsignedLeb128Size( register );- reader.setPointerIndex( reader.getPointerIndex( ) + registerLength );- length += registerLength;+ int register = LEB128.readAsUInt32(reader);break;}case DebugStateMachineOpCodes.DBG_RESTART_LOCAL: {- int register = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int registerLength = Leb128.unsignedLeb128Size( register );- reader.setPointerIndex( reader.getPointerIndex( ) + registerLength );- length += registerLength;+ int register = LEB128.readAsUInt32(reader);break;}case DebugStateMachineOpCodes.DBG_SET_PROLOGUE_END: {@@ -121,10 +83,7 @@ class DebugInfoStateMachineReader {}case DebugStateMachineOpCodes.DBG_SET_FILE: {//TODO uleb128p1- int name = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- int nameLength = Leb128.unsignedLeb128Size( name );- reader.setPointerIndex( reader.getPointerIndex( ) + nameLength );- length += nameLength;+ int name = LEB128.readAsUInt32(reader);break;}default: {@@ -132,5 +91,7 @@ class DebugInfoStateMachineReader {}}}++ return 0;}}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedAnnotation.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedAnnotation.javaindex b29e8fe690..aa9f4ed5cd 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedAnnotation.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedAnnotation.java@@ -15,31 +15,31 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import java.util.*;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;+import ghidra.app.util.bin.format.dwarf4.LEB128;import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-import java.util.*;-public class EncodedAnnotation implements StructConverter {private int typeIndex;private int typeIndexLength;// in bytesprivate int size;private int sizeLength;// in bytes- private List< AnnotationElement > elements = new ArrayList< AnnotationElement >( );+ private List< AnnotationElement > elements = new ArrayList< >( );public EncodedAnnotation( BinaryReader reader ) throws IOException {- typeIndex = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- typeIndexLength = Leb128.unsignedLeb128Size( typeIndex );- reader.readNextByteArray( typeIndexLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ typeIndex = leb128.asUInt32();+ typeIndexLength = leb128.getLength();- size = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- sizeLength = Leb128.unsignedLeb128Size( size );- reader.readNextByteArray( sizeLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ size = leb128.asUInt32();+ sizeLength = leb128.getLength();for ( int i = 0 ; i < size ; ++i ) {elements.add( new AnnotationElement( reader ) );diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedArray.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedArray.javaindex 664993ba11..fa6454f602 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedArray.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedArray.java@@ -21,7 +21,7 @@ import java.util.List;import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;+import ghidra.app.util.bin.format.dwarf4.LEB128;import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;@@ -33,18 +33,16 @@ public class EncodedArray implements StructConverter {private byte [] values;public EncodedArray( BinaryReader reader ) throws IOException {- size = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- sizeLength = Leb128.unsignedLeb128Size( size );- reader.readNextByteArray( sizeLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ size = leb128.asUInt32();+ sizeLength = leb128.getLength();- long oldIndex = reader.getPointerIndex( );- List< EncodedValue > valuesList = new ArrayList< EncodedValue >( );+ BinaryReader evReader = reader.clone();+ List< EncodedValue > valuesList = new ArrayList< >( );for ( int i = 0 ; i < size ; ++i ) {- valuesList.add( new EncodedValue( reader ) );+ valuesList.add(new EncodedValue(evReader));}- int nBytes = (int) ( reader.getPointerIndex() - oldIndex );-- reader.setPointerIndex(oldIndex);+ int nBytes = (int) (evReader.getPointerIndex() - reader.getPointerIndex());values = reader.readNextByteArray(nBytes); // Re-read the encoded values as a byte array}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandler.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandler.javaindex 55b22196a4..5bcf91a62c 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandler.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandler.java@@ -15,41 +15,37 @@*/package ghidra.file.formats.android.dex.format;-import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;-import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;import java.util.ArrayList;import java.util.List;+import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.StructConverter;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;+import ghidra.util.exception.DuplicateNameException;+public class EncodedCatchHandler implements StructConverter {private int size;private int sizeLength;// in bytes- private List< EncodedTypeAddressPair > handlers = new ArrayList< EncodedTypeAddressPair >( );+ private List< EncodedTypeAddressPair > handlers = new ArrayList< >( );private int catchAllAddress;private int catchAllAddressLength;public EncodedCatchHandler( BinaryReader reader ) throws IOException {- size = Leb128.readSignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- sizeLength = Leb128.signedLeb128Size( size );- reader.readNextByteArray( sizeLength );// consume leb...+ LEB128 leb128 = LEB128.readSignedValue(reader);+ size = leb128.asInt32();+ sizeLength = leb128.getLength();for ( int i = 0 ; i < Math.abs( size ) ; ++i ) {handlers.add( new EncodedTypeAddressPair( reader ) );}if ( size <= 0 ) {// This element is only present if size is non-positive.- catchAllAddress = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- catchAllAddressLength = Leb128.unsignedLeb128Size( catchAllAddress );- reader.readNextByteArray( catchAllAddressLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ catchAllAddress = leb128.asUInt32();+ catchAllAddressLength = leb128.getLength();}}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandlerList.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandlerList.javaindex 958fc3bc60..5df1321781 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandlerList.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedCatchHandlerList.java@@ -15,30 +15,26 @@*/package ghidra.file.formats.android.dex.format;-import ghidra.app.util.bin.BinaryReader;-import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;-import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;import java.util.ArrayList;import java.util.List;+import ghidra.app.util.bin.BinaryReader;+import ghidra.app.util.bin.StructConverter;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;+import ghidra.util.exception.DuplicateNameException;+public class EncodedCatchHandlerList implements StructConverter {private int size;private int sizeLength;// in bytes- private List< EncodedCatchHandler > handlers = new ArrayList< EncodedCatchHandler >( );+ private List< EncodedCatchHandler > handlers = new ArrayList< >( );public EncodedCatchHandlerList( BinaryReader reader ) throws IOException {- size = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- sizeLength = Leb128.unsignedLeb128Size( size );- reader.readNextByteArray( sizeLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ size = leb128.asUInt32();+ sizeLength = leb128.getLength();for ( int i = 0 ; i < size ; ++i ) {handlers.add( new EncodedCatchHandler( reader ) );diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedField.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedField.javaindex ff5897bb25..1b98b51a8a 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedField.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedField.java@@ -15,18 +15,14 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-public class EncodedField implements StructConverter {private long _fileOffset;@@ -38,15 +34,15 @@ public class EncodedField implements StructConverter {private int accessFlagsLength;// in bytespublic EncodedField( BinaryReader reader ) throws IOException {- _fileOffset = reader.getPointerIndex( );- fieldIndexDifference = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- fieldIndexDifferenceLength = Leb128.unsignedLeb128Size( fieldIndexDifference );- reader.readNextByteArray( fieldIndexDifferenceLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ _fileOffset = leb128.getOffset();+ fieldIndexDifference = leb128.asUInt32();+ fieldIndexDifferenceLength = leb128.getLength();- accessFlags = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- accessFlagsLength = Leb128.unsignedLeb128Size( accessFlags );- reader.readNextByteArray( accessFlagsLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ accessFlags = leb128.asUInt32();+ accessFlagsLength = leb128.getLength();}public long getFileOffset( ) {diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedMethod.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedMethod.javaindex d4abcd496a..a07e6342fc 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedMethod.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedMethod.java@@ -15,14 +15,14 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;+import ghidra.app.util.bin.format.dwarf4.LEB128;import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-public class EncodedMethod implements StructConverter {private long _fileOffset;@@ -39,29 +39,22 @@ public class EncodedMethod implements StructConverter {private CodeItem codeItem;public EncodedMethod( BinaryReader reader ) throws IOException {- _fileOffset = reader.getPointerIndex( );- methodIndexDifference = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- methodIndexDifferenceLength = Leb128.unsignedLeb128Size( methodIndexDifference );- reader.readNextByteArray( methodIndexDifferenceLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ _fileOffset = leb128.getOffset();+ methodIndexDifference = leb128.asUInt32();+ methodIndexDifferenceLength = leb128.getLength();- accessFlags = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- accessFlagsLength = Leb128.unsignedLeb128Size( accessFlags );- reader.readNextByteArray( accessFlagsLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ accessFlags = leb128.asUInt32();+ accessFlagsLength = leb128.getLength();- codeOffset = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- codeOffsetLength = Leb128.unsignedLeb128Size( codeOffset );- reader.readNextByteArray( codeOffsetLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ codeOffset = leb128.asUInt32();+ codeOffsetLength = leb128.getLength();if ( codeOffset > 0 ) {- long oldIndex = reader.getPointerIndex( );- try {- reader.setPointerIndex( codeOffset );- codeItem = new CodeItem( reader );- }- finally {- reader.setPointerIndex( oldIndex );- }+ codeItem = new CodeItem( reader.clone(codeOffset) );}}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedTypeAddressPair.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedTypeAddressPair.javaindex 031cba8fa9..e8c70f5e54 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedTypeAddressPair.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/EncodedTypeAddressPair.java@@ -15,18 +15,14 @@*/package ghidra.file.formats.android.dex.format;+import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.IOException;-public class EncodedTypeAddressPair implements StructConverter {private int typeIndex;@@ -36,13 +32,13 @@ public class EncodedTypeAddressPair implements StructConverter {private int addressLength;// in bytespublic EncodedTypeAddressPair( BinaryReader reader ) throws IOException {- typeIndex = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- typeIndexLength = Leb128.unsignedLeb128Size( typeIndex );- reader.readNextByteArray( typeIndexLength );// consume leb...+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ typeIndex = leb128.asUInt32();+ typeIndexLength = leb128.getLength();- address = Leb128.readUnsignedLeb128( reader.readByteArray( reader.getPointerIndex( ), 5 ) );- addressLength = Leb128.unsignedLeb128Size( address );- reader.readNextByteArray( addressLength );// consume leb...+ leb128 = LEB128.readUnsignedValue(reader);+ address = leb128.asUInt32();+ addressLength = leb128.getLength();}public int getTypeIndex( ) {diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.javaindex 68905a5902..867182230f 100644--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java+++ b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/format/StringDataItem.java@@ -15,52 +15,41 @@*/package ghidra.file.formats.android.dex.format;+import java.io.ByteArrayInputStream;+import java.io.IOException;+import ghidra.app.util.bin.BinaryReader;import ghidra.app.util.bin.StructConverter;-import ghidra.file.formats.android.dex.util.Leb128;-import ghidra.program.model.data.ArrayDataType;-import ghidra.program.model.data.CategoryPath;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.StructureDataType;+import ghidra.app.util.bin.format.dwarf4.LEB128;+import ghidra.program.model.data.*;import ghidra.util.exception.DuplicateNameException;-import java.io.ByteArrayInputStream;-import java.io.IOException;-public class StringDataItem implements StructConverter {+ private static final int MAX_STRING_LEN = 0x200000; // 2Mb'ishprivate int stringLength;private int lebLength;private int actualLength;private String string;- public StringDataItem( StringIDItem stringItem, BinaryReader reader ) throws IOException {- long oldIndex = reader.getPointerIndex( );- try {- reader.setPointerIndex( stringItem.getStringDataOffset( ) );- stringLength = Leb128.readUnsignedLeb128( reader.readByteArray( stringItem.getStringDataOffset( ), 5 ) );+ public StringDataItem(StringIDItem stringItem, BinaryReader reader) throws IOException {+ reader = reader.clone(stringItem.getStringDataOffset());+ LEB128 leb128 = LEB128.readUnsignedValue(reader);+ stringLength = leb128.asUInt32();+ lebLength = leb128.getLength();+ long nullTermIndex =+ getIndexOfByteValue(reader, reader.getPointerIndex(), MAX_STRING_LEN, (byte) 0);+ actualLength = (int) (nullTermIndex - reader.getPointerIndex() + 1);+ byte[] stringBytes = reader.readNextByteArray(actualLength);- lebLength = Leb128.unsignedLeb128Size( stringLength );+ ByteArrayInputStream in = new ByteArrayInputStream(stringBytes);- reader.readNextByteArray( lebLength );// consume leb...+ char[] out = new char[stringLength];- actualLength = computeActualLength( reader );-- byte [] stringBytes = reader.readNextByteArray( actualLength );-- ByteArrayInputStream in = new ByteArrayInputStream( stringBytes );-- char [] out = new char[ stringLength ];-- string = ModifiedUTF8.decode( in, out );- }- finally {- reader.setPointerIndex( oldIndex );- }+ string = ModifiedUTF8.decode(in, out);}- public String getString( ) {+ public String getString() {return string;}@@ -73,15 +62,18 @@ public class StringDataItem implements StructConverter {return structure;}- private int computeActualLength( BinaryReader reader ) throws IOException {- int count = 0;- while ( count < 0x200000 ) {// don't run forever!- if ( reader.readByte( reader.getPointerIndex( ) + count ) == 0x0 ) {- break;+ private static long getIndexOfByteValue(BinaryReader reader, long startIndex, int maxLen,+ byte byteValueToFind) throws IOException {+ long maxIndex = startIndex + maxLen;+ long currentIndex = startIndex;+ while (currentIndex < maxIndex) {+ byte b = reader.readByte(currentIndex);+ if (b == byteValueToFind) {+ return currentIndex;}- ++count;+ currentIndex++;}- return count + 1;+ return currentIndex;}}diff --git a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/util/Leb128.java b/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/util/Leb128.javadeleted file mode 100644index 5dd7e9437f..0000000000--- a/Ghidra/Features/FileFormats/src/main/java/ghidra/file/formats/android/dex/util/Leb128.java+++ /dev/null@@ -1,202 +0,0 @@-/* ###- * IP: Apache License 2.0- */-/*- * Copyright (C) 2008 The Android Open Source Project- *- * 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.file.formats.android.dex.util;--import java.io.ByteArrayInputStream;-import java.io.ByteArrayOutputStream;--import com.googlecode.d2j.DexException;--import ghidra.util.NumericUtilities;--/**- * Reads and writes DWARFv3 LEB 128 signed and unsigned integers. See DWARF v3 section 7.6.- */-public final class Leb128 {- private Leb128() {- }-- /**- * Gets the number of bytes in the unsigned LEB128 encoding of the given value.- *- * @param value- * the value in question- * @return its write size, in bytes- */- public static int unsignedLeb128Size( int value ) {- // TODO: This could be much cleverer.-- int remaining = value >> 7;- int count = 0;-- while ( remaining != 0 ) {- remaining >>= 7;- count++;- }-- return count + 1;- }-- /**- * Gets the number of bytes in the signed LEB128 encoding of the given value.- *- * @param value- * the value in question- * @return its write size, in bytes- */- public static int signedLeb128Size( int value ) {- // TODO: This could be much cleverer.-- int remaining = value >> 7;- int count = 0;- boolean hasMore = true;- int end = ( ( value & Integer.MIN_VALUE ) == 0 ) ? 0 : -1;-- while ( hasMore ) {- hasMore = ( remaining != end ) || ( ( remaining & 1 ) != ( ( value >> 6 ) & 1 ) );-- value = remaining;- remaining >>= 7;- count++;- }-- return count;-- // ByteArrayOutputStream out = new ByteArrayOutputStream( );- // int remaining = value >>> 7;- //- // while ( remaining != 0 ) {- // out.write( ( byte ) ( ( value & 0x7f ) | 0x80 ) );- // value = remaining;- // remaining >>>= 7;- // }- //- // out.write( ( byte ) ( value & 0x7f ) );- //- // return out.toByteArray( ).length;- }-- public static int readSignedLeb128( byte [] bytes ) {- return readSignedLeb128( new ByteArrayInputStream( bytes ) );- }-- /**- * Reads an signed integer from {@code in}.- */- public static int readSignedLeb128( ByteArrayInputStream in ) {- int result = 0;- int cur;- int count = 0;- int signBits = -1;-- do {- cur = in.read( ) & 0xff;- result |= ( cur & 0x7f ) << ( count * 7 );- signBits <<= 7;- count++;- }- while ( ( ( cur & 0x80 ) == 0x80 ) && count < 5 );-- if ( ( cur & 0x80 ) == 0x80 ) {- throw new DexException( "invalid LEB128 sequence" );- }-- // Sign extend if appropriate- if ( ( ( signBits >> 1 ) & result ) != 0 ) {- result |= signBits;- }-- return result;- }-- public static int readUnsignedLeb128( byte [] bytes ) {- return readUnsignedLeb128( new ByteArrayInputStream( bytes ) );- }-- /**- * Reads an unsigned integer from {@code in}.- */- public static int readUnsignedLeb128( ByteArrayInputStream in ) {- int result = 0;- int cur;- int count = 0;-- do {- cur = in.read( ) & 0xff;- result |= ( cur & 0x7f ) << ( count * 7 );- count++;- }- while ( ( ( cur & 0x80 ) == 0x80 ) && count < 5 );-- if ( ( cur & 0x80 ) == 0x80 ) {- throw new DexException( "invalid LEB128 sequence" );- }-- return result;- }-- /**- * Writes {@code value} as an unsigned integer to {@code out}, starting at {@code offset}. Returns the number of bytes written.- */- public static void writeUnsignedLeb128( ByteArrayOutputStream out, int value ) {- int remaining = value >>> 7;-- while ( remaining != 0 ) {- out.write( ( byte ) ( ( value & 0x7f ) | 0x80 ) );- value = remaining;- remaining >>>= 7;- }-- out.write( ( byte ) ( value & 0x7f ) );- }-- /**- * Writes {@code value} as a signed integer to {@code out}, starting at {@code offset}. Returns the number of bytes written.- */- public static void writeSignedLeb128( ByteArrayOutputStream out, int value ) {- int remaining = value >> 7;- boolean hasMore = true;- int end = ( ( value & Integer.MIN_VALUE ) == 0 ) ? 0 : -1;-- while ( hasMore ) {- hasMore = ( remaining != end ) || ( ( remaining & 1 ) != ( ( value >> 6 ) & 1 ) );-- out.write( ( byte ) ( ( value & 0x7f ) | ( hasMore ? 0x80 : 0 ) ) );- value = remaining;- remaining >>= 7;- }- }-- public static void main( String [] args ) {-- //ByteArrayOutputStream out = new ByteArrayOutputStream( );- //writeSignedLeb128( out, -12345 );-- //System.out.println( "array length: " + out.toByteArray( ).length );-- //System.out.println( "actual length: " + signedLeb128Size( -12345 ) );-- //System.out.println( readSignedLeb128( out.toByteArray( ) ) );-- System.out.println( readUnsignedLeb128( NumericUtilities.convertStringToBytes("807f" ) ) );--- }-}