From 344ea3793b47dfbce7dbfe7ce3a33bd6e325e35a Mon Sep 17 00:00:00 2001 From: amosshi Date: Tue, 23 Jun 2020 00:08:03 -0700 Subject: [PATCH] #8 ELF: fix header length issue --- .../java/org/freeinternals/commonlib/core/BytesTool.java | 5 +++-- .../commonlib/core/FileComponenPlaceHolder.java | 2 +- .../java/org/freeinternals/format/classfile/Opcode.java | 6 +++--- .../main/java/org/freeinternals/format/elf/Elf64_Ehdr.java | 2 +- .../src/main/java/org/freeinternals/format/elf/ElfFile.java | 2 +- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/core/BytesTool.java b/CommonLib/src/main/java/org/freeinternals/commonlib/core/BytesTool.java index 47ac793..7daa52e 100644 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/core/BytesTool.java +++ b/CommonLib/src/main/java/org/freeinternals/commonlib/core/BytesTool.java @@ -177,8 +177,8 @@ public static byte[] readZipEntryAsBytes(final ZipFile zipFile, final ZipEntry z final long fileSize = zipEntry.getSize(); final byte contents[] = new byte[(int) fileSize]; ByteBuffer byteBuf = ByteBuffer.allocate(contents.length); - InputStream is = null; - int bytesRead = 0; + InputStream is; + int bytesRead; int bytesAll = 0; try { @@ -216,6 +216,7 @@ public static void skip(final InputStream is, final long skip) throws IOExceptio } } + @Deprecated public static void skipBytes(final DataInput di, final int skip) throws IOException { long skippedBytes = di.skipBytes(skip); if (skippedBytes != skip) { diff --git a/CommonLib/src/main/java/org/freeinternals/commonlib/core/FileComponenPlaceHolder.java b/CommonLib/src/main/java/org/freeinternals/commonlib/core/FileComponenPlaceHolder.java index 79983f4..811ab8e 100644 --- a/CommonLib/src/main/java/org/freeinternals/commonlib/core/FileComponenPlaceHolder.java +++ b/CommonLib/src/main/java/org/freeinternals/commonlib/core/FileComponenPlaceHolder.java @@ -25,7 +25,7 @@ public FileComponenPlaceHolder(final PosDataInputStream posDataInputStream, int if (length > 0) { this.length = length; - BytesTool.skipBytes(posDataInputStream, length); + BytesTool.skip(posDataInputStream, length); } else { this.length = 0; } diff --git a/FormatCLASS/src/main/java/org/freeinternals/format/classfile/Opcode.java b/FormatCLASS/src/main/java/org/freeinternals/format/classfile/Opcode.java index f894857..fe14f1c 100755 --- a/FormatCLASS/src/main/java/org/freeinternals/format/classfile/Opcode.java +++ b/FormatCLASS/src/main/java/org/freeinternals/format/classfile/Opcode.java @@ -1623,7 +1623,7 @@ protected InstructionParsed parse(final int curPos, final PosDataInputStream pdi InstructionParsed parsed = new InstructionParsed(curPos, this.code); parsed.cpIndex = pdis.readUnsignedShort(); parsed.nArgs = pdis.readUnsignedByte(); - BytesTool.skipBytes(pdis, 1); + BytesTool.skip(pdis, 1); parsed.opCodeText = String.format("%s interface=%d, nargs=%d", this.name(), parsed.cpIndex, parsed.nArgs); return parsed; @@ -1639,7 +1639,7 @@ protected InstructionParsed parse(final int curPos, final PosDataInputStream pdi parsed.cpIndex = pdis.readUnsignedShort(); // Skip 2 zero bytes - BytesTool.skipBytes(pdis, 2); + BytesTool.skip(pdis, 2); parsed.opCodeText = this.name(); return parsed; } @@ -1985,7 +1985,7 @@ private void skipPad(final PosDataInputStream pdis) throws IOException { int skip = pdis.getPos() % 4; skip = (skip > 0) ? 4 - skip : skip; if (skip > 0) { - BytesTool.skipBytes(pdis, skip); + BytesTool.skip(pdis, skip); } } diff --git a/FormatELF/src/main/java/org/freeinternals/format/elf/Elf64_Ehdr.java b/FormatELF/src/main/java/org/freeinternals/format/elf/Elf64_Ehdr.java index e0c59b9..702c09b 100644 --- a/FormatELF/src/main/java/org/freeinternals/format/elf/Elf64_Ehdr.java +++ b/FormatELF/src/main/java/org/freeinternals/format/elf/Elf64_Ehdr.java @@ -90,7 +90,7 @@ public class Elf64_Ehdr extends FileComponent implements GenerateTreeNode { this.e_shnum = input.read_Elf64_Half(); this.e_shstrndx = input.read_Elf64_Half(); - super.length = input.getPos(); + super.length = input.getPos() - super.startPos; } /** diff --git a/FormatELF/src/main/java/org/freeinternals/format/elf/ElfFile.java b/FormatELF/src/main/java/org/freeinternals/format/elf/ElfFile.java index 50e4a49..9f5aad0 100644 --- a/FormatELF/src/main/java/org/freeinternals/format/elf/ElfFile.java +++ b/FormatELF/src/main/java/org/freeinternals/format/elf/ElfFile.java @@ -38,7 +38,7 @@ public ElfFile(File file) throws IOException, FileFormatException { PosDataInputStream input = new PosDataInputStream(new PosByteArrayInputStream(this.fileByteArray)); this.ident = new Identification(input); - + PosDataInputStreamElf inputElf = new PosDataInputStreamElf(new PosByteArrayInputStream(this.fileByteArray), this.ident.EI_DATA); BytesTool.skip(input, Identification.EI_NIDENT); this.header = new Elf64_Ehdr(inputElf);