Skip to content

Commit

Permalink
feat: allow to load custom input (#1457)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Apr 21, 2022
1 parent 1ec127c commit 1832f2a
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 21 deletions.
2 changes: 1 addition & 1 deletion jadx-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies {

testImplementation 'org.apache.commons:commons-lang3:3.12.0'

testRuntimeOnly(project(':jadx-plugins:jadx-dex-input'))
testImplementation(project(':jadx-plugins:jadx-dex-input'))
testRuntimeOnly(project(':jadx-plugins:jadx-smali-input'))
testRuntimeOnly(project(':jadx-plugins:jadx-java-convert'))
testRuntimeOnly(project(':jadx-plugins:jadx-java-input'))
Expand Down
30 changes: 17 additions & 13 deletions jadx-core/src/main/java/jadx/api/JadxArgsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ public class JadxArgsValidator {

private static final Logger LOG = LoggerFactory.getLogger(JadxArgsValidator.class);

public static void validate(JadxArgs args) {
checkInputFiles(args);
public static void validate(JadxDecompiler jadx) {
JadxArgs args = jadx.getArgs();
checkInputFiles(jadx, args);
validateOutDirs(args);

if (LOG.isDebugEnabled()) {
LOG.debug("Effective jadx args: {}", args);
}
}

private static void checkInputFiles(JadxArgs args) {
private static void checkInputFiles(JadxDecompiler jadx, JadxArgs args) {
List<File> inputFiles = args.getInputFiles();
if (inputFiles.isEmpty()) {
if (inputFiles.isEmpty() && jadx.getCustomLoads().isEmpty()) {
throw new JadxArgsValidateException("Please specify input file");
}
for (File inputFile : inputFiles) {
Expand Down Expand Up @@ -66,19 +67,22 @@ private static void validateOutDirs(JadxArgs args) {

@NotNull
private static File makeDirFromInput(JadxArgs args) {
File outDir;
String outDirName;
File file = args.getInputFiles().get(0);
String name = file.getName();
int pos = name.lastIndexOf('.');
if (pos != -1) {
outDirName = name.substring(0, pos);
List<File> inputFiles = args.getInputFiles();
if (inputFiles.isEmpty()) {
outDirName = JadxArgs.DEFAULT_OUT_DIR;
} else {
outDirName = name + '-' + JadxArgs.DEFAULT_OUT_DIR;
File file = inputFiles.get(0);
String name = file.getName();
int pos = name.lastIndexOf('.');
if (pos != -1) {
outDirName = name.substring(0, pos);
} else {
outDirName = name + '-' + JadxArgs.DEFAULT_OUT_DIR;
}
}
LOG.info("output directory: {}", outDirName);
outDir = new File(outDirName);
return outDir;
return new File(outDirName);
}

private static void checkFile(File file) {
Expand Down
13 changes: 12 additions & 1 deletion jadx-core/src/main/java/jadx/api/JadxDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ public final class JadxDecompiler implements Closeable {

private final IDecompileScheduler decompileScheduler = new DecompilerScheduler(this);

private final List<ILoadResult> customLoads = new ArrayList<>();

public JadxDecompiler() {
this(new JadxArgs());
}
Expand All @@ -108,7 +110,7 @@ public JadxDecompiler(JadxArgs args) {

public void load() {
reset();
JadxArgsValidator.validate(args);
JadxArgsValidator.validate(this);
LOG.info("loading ...");
loadPlugins(args);
loadInputFiles();
Expand All @@ -132,11 +134,20 @@ private void loadInputFiles() {
loadedInputs.add(loadResult);
}
}
loadedInputs.addAll(customLoads);
if (LOG.isDebugEnabled()) {
LOG.debug("Loaded using {} inputs plugin in {} ms", loadedInputs.size(), System.currentTimeMillis() - start);
}
}

public void addCustomLoad(ILoadResult customLoad) {
customLoads.add(customLoad);
}

public List<ILoadResult> getCustomLoads() {
return customLoads;
}

private void reset() {
root = null;
classes = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private void setOutDirs(String outDir, String srcDir, String resDir) {
}

private void checkOutDirs(String outDir, String srcDir, String resDir) {
JadxArgsValidator.validate(args);
JadxArgsValidator.validate(new JadxDecompiler(args));
LOG.debug("Got dirs: out={}, src={}, res={}", args.getOutDir(), args.getOutDirSrc(), args.getOutDirRes());
assertThat(args.getOutDir(), is(toFile(outDir)));
assertThat(args.getOutDirSrc(), is(toFile(srcDir)));
Expand Down
18 changes: 18 additions & 0 deletions jadx-core/src/test/java/jadx/api/JadxDecompilerTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package jadx.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;

import jadx.core.utils.files.FileUtils;
import jadx.plugins.input.dex.DexInputPlugin;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
Expand Down Expand Up @@ -38,6 +42,20 @@ public void testExampleUsage() {
}
}

@Test
public void testDirectDexInput() throws IOException {
try (JadxDecompiler jadx = new JadxDecompiler();
InputStream in = new FileInputStream(getFileFromSampleDir("hello.dex"))) {
jadx.addCustomLoad(new DexInputPlugin().loadDexFromInputStream(in, "input"));
jadx.load();
for (JavaClass cls : jadx.getClasses()) {
System.out.println(cls.getCode());
}
assertThat(jadx.getClasses(), Matchers.hasSize(1));
assertThat(jadx.getErrorsCount(), Matchers.is(0));
}
}

private static final String TEST_SAMPLES_DIR = "test-samples/";

public static File getFileFromSampleDir(String fileName) {
Expand Down
Binary file added jadx-core/src/test/resources/test-samples/hello.dex
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ private List<DexReader> load(@Nullable File file, InputStream inputStream, Strin
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) {
Expand All @@ -80,6 +77,13 @@ private List<DexReader> load(@Nullable File file, InputStream inputStream, Strin
}
}

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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -13,6 +15,7 @@
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";
Expand All @@ -38,6 +41,20 @@ public ILoadResult loadFiles(List<Path> inputFiles, @Nullable Closeable closeabl
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void visitResources(Consumer<IResourceData> consumer) {

@Override
public void close() throws IOException {
dexReaders.clear();
if (closeable != null) {
closeable.close();
}
Expand Down

0 comments on commit 1832f2a

Please sign in to comment.