Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BufferedInputStream: Allow jumping backwards to an already processed position #2344

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions jadx-cli/src/main/java/jadx/cli/tools/ConvertArscFile.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jadx.cli.tools;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
Expand Down Expand Up @@ -71,7 +70,7 @@ public static void main(String[] args) throws IOException {
}
} else {
// Load resources.arsc from extracted file
try (InputStream inputStream = new BufferedInputStream(Files.newInputStream(resFile))) {
try (InputStream inputStream = Files.newInputStream(resFile)) {
resTableParser.decode(inputStream);
}
}
Expand Down
3 changes: 3 additions & 0 deletions jadx-core/src/main/java/jadx/core/xmlgen/ParserStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ParserStream {

private final InputStream input;
private long readPos = 0;
private long markPos = 0;

public ParserStream(@NotNull InputStream inputStream) {
this.input = inputStream;
Expand Down Expand Up @@ -143,10 +144,12 @@ public void mark(int len) throws IOException {
throw new IOException("Mark not supported for input stream " + input.getClass());
}
input.mark(len);
markPos = readPos;
}

public void reset() throws IOException {
input.reset();
readPos = markPos;
}

public void readFully(byte[] b) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package jadx.core.xmlgen;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -79,7 +80,7 @@ public ResTableBinaryParser(RootNode root, boolean useRawResNames) {
@Override
public void decode(InputStream inputStream) throws IOException {
long start = System.currentTimeMillis();
is = new ParserStream(inputStream);
is = new ParserStream(new BufferedInputStream(inputStream, 32768));
resStorage = new ResourceStorage(root.getArgs().getSecurity());
decodeTableChunk();
resStorage.finish();
Expand Down Expand Up @@ -234,6 +235,7 @@ private void parseTypeChunk(long start, PackageChunk pkg) throws IOException {
/* int size = */
long chunkSize = is.readUInt32();
long chunkEnd = start + chunkSize;
is.mark((int) chunkSize);

// The type identifier this chunk is holding. Type IDs start at 1 (corresponding
// to the value of the type bits in a resource identifier). 0 is invalid.
Expand Down Expand Up @@ -286,7 +288,12 @@ private void parseTypeChunk(long start, PackageChunk pkg) throws IOException {
LOG.warn("End of chunk reached - ignoring remaining {} entries", entryCount - index);
break;
}
is.checkPos(entriesStart + offset, "Expected start of entry " + index);
long entryStartOffset = entriesStart + offset;
if (entryStartOffset < is.getPos()) {
// workaround for issue #2343: if the entryStartOffset is located before our current position
is.reset();
}
is.skipToPos(entryStartOffset, "Expected start of entry " + index);
parseEntry(pkg, id, index, config.getQualifiers());
}
if (chunkEnd > is.getPos()) {
Expand Down