feat: allow to load custom input (#1457)

This commit is contained in:
Skylot
2022-04-21 13:21:13 +01:00
parent 1ec127c3cb
commit 1832f2aee3
9 changed files with 74 additions and 21 deletions
@@ -64,10 +64,7 @@ public class DexFileLoader {
if (isStartWithBytes(magic, DexConsts.DEX_FILE_MAGIC) || fileName.endsWith(".dex")) {
in.reset();
byte[] content = readAllBytes(in);
if (options.isVerifyChecksum()) {
DexCheckSum.verify(content);
}
DexReader dexReader = new DexReader(getNextUniqId(), fileName, content);
DexReader dexReader = loadDexReader(fileName, content);
return Collections.singletonList(dexReader);
}
if (file != null) {
@@ -80,6 +77,13 @@ public class DexFileLoader {
}
}
public DexReader loadDexReader(String fileName, byte[] content) {
if (options.isVerifyChecksum()) {
DexCheckSum.verify(content);
}
return new DexReader(getNextUniqId(), fileName, content);
}
private List<DexReader> collectDexFromZip(File file) {
List<DexReader> result = new ArrayList<>();
try {
@@ -1,7 +1,9 @@
package jadx.plugins.input.dex;
import java.io.Closeable;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;
import java.util.Map;
@@ -13,6 +15,7 @@ import jadx.api.plugins.input.data.ILoadResult;
import jadx.api.plugins.input.data.impl.EmptyLoadResult;
import jadx.api.plugins.options.JadxPluginOptions;
import jadx.api.plugins.options.OptionDescription;
import jadx.api.plugins.utils.CommonFileUtils;
public class DexInputPlugin implements JadxInputPlugin, JadxPluginOptions {
public static final String PLUGIN_ID = "dex-input";
@@ -38,6 +41,20 @@ public class DexInputPlugin implements JadxInputPlugin, JadxPluginOptions {
return new DexLoadResult(dexReaders, closeable);
}
public ILoadResult loadDex(byte[] content, @Nullable String fileName) {
String fileLabel = fileName == null ? "input.dex" : fileName;
DexReader dexReader = loader.loadDexReader(fileLabel, content);
return new DexLoadResult(Collections.singletonList(dexReader), null);
}
public ILoadResult loadDexFromInputStream(InputStream in, @Nullable String fileLabel) {
try {
return loadDex(CommonFileUtils.loadBytes(in), fileLabel);
} catch (Exception e) {
throw new DexException("Failed to read input stream", e);
}
}
@Override
public void setOptions(Map<String, String> options) {
this.options.apply(options);
@@ -34,7 +34,6 @@ public class DexLoadResult implements ILoadResult {
@Override
public void close() throws IOException {
dexReaders.clear();
if (closeable != null) {
closeable.close();
}