Commit 55fc4983 authored by Ahmed Ashour's avatar Ahmed Ashour Committed by skylot

refactor: use Path instead of File (PR #527)

parent ba6dd081
package jadx.core.deobf; package jadx.core.deobf;
import java.io.File; import static java.nio.charset.StandardCharsets.UTF_8;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -19,16 +22,16 @@ import jadx.core.dex.info.MethodInfo; ...@@ -19,16 +22,16 @@ import jadx.core.dex.info.MethodInfo;
class DeobfPresets { class DeobfPresets {
private static final Logger LOG = LoggerFactory.getLogger(DeobfPresets.class); private static final Logger LOG = LoggerFactory.getLogger(DeobfPresets.class);
private static final String MAP_FILE_CHARSET = "UTF-8"; private static final Charset MAP_FILE_CHARSET = UTF_8;
private final Deobfuscator deobfuscator; private final Deobfuscator deobfuscator;
private final File deobfMapFile; private final Path deobfMapFile;
private final Map<String, String> clsPresetMap = new HashMap<>(); private final Map<String, String> clsPresetMap = new HashMap<>();
private final Map<String, String> fldPresetMap = new HashMap<>(); private final Map<String, String> fldPresetMap = new HashMap<>();
private final Map<String, String> mthPresetMap = new HashMap<>(); private final Map<String, String> mthPresetMap = new HashMap<>();
public DeobfPresets(Deobfuscator deobfuscator, File deobfMapFile) { public DeobfPresets(Deobfuscator deobfuscator, Path deobfMapFile) {
this.deobfuscator = deobfuscator; this.deobfuscator = deobfuscator;
this.deobfMapFile = deobfMapFile; this.deobfMapFile = deobfMapFile;
} }
...@@ -37,12 +40,12 @@ class DeobfPresets { ...@@ -37,12 +40,12 @@ class DeobfPresets {
* Loads deobfuscator presets * Loads deobfuscator presets
*/ */
public void load() { public void load() {
if (!deobfMapFile.exists()) { if (!Files.exists(deobfMapFile)) {
return; return;
} }
LOG.info("Loading obfuscation map from: {}", deobfMapFile.getAbsoluteFile()); LOG.info("Loading obfuscation map from: {}", deobfMapFile.toAbsolutePath());
try { try {
List<String> lines = FileUtils.readLines(deobfMapFile, MAP_FILE_CHARSET); List<String> lines = Files.readAllLines(deobfMapFile, MAP_FILE_CHARSET);
for (String l : lines) { for (String l : lines) {
l = l.trim(); l = l.trim();
if (l.isEmpty() || l.startsWith("#")) { if (l.isEmpty() || l.startsWith("#")) {
...@@ -65,7 +68,7 @@ class DeobfPresets { ...@@ -65,7 +68,7 @@ class DeobfPresets {
} }
} }
} catch (IOException e) { } catch (IOException e) {
LOG.error("Failed to load deobfuscation map file '{}'", deobfMapFile.getAbsolutePath(), e); LOG.error("Failed to load deobfuscation map file '{}'", deobfMapFile.toAbsolutePath(), e);
} }
} }
...@@ -79,18 +82,18 @@ class DeobfPresets { ...@@ -79,18 +82,18 @@ class DeobfPresets {
public void save(boolean forceSave) { public void save(boolean forceSave) {
try { try {
if (deobfMapFile.exists()) { if (Files.exists(deobfMapFile)) {
if (forceSave) { if (forceSave) {
dumpMapping(); dumpMapping();
} else { } else {
LOG.warn("Deobfuscation map file '{}' exists. Use command line option '--deobf-rewrite-cfg' to rewrite it", LOG.warn("Deobfuscation map file '{}' exists. Use command line option '--deobf-rewrite-cfg' to rewrite it",
deobfMapFile.getAbsolutePath()); deobfMapFile.toAbsolutePath());
} }
} else { } else {
dumpMapping(); dumpMapping();
} }
} catch (IOException e) { } catch (IOException e) {
LOG.error("Failed to load deobfuscation map file '{}'", deobfMapFile.getAbsolutePath(), e); LOG.error("Failed to load deobfuscation map file '{}'", deobfMapFile.toAbsolutePath(), e);
} }
} }
...@@ -122,7 +125,7 @@ class DeobfPresets { ...@@ -122,7 +125,7 @@ class DeobfPresets {
list.add(String.format("m %s = %s", mth.getRawFullId(), mth.getAlias())); list.add(String.format("m %s = %s", mth.getRawFullId(), mth.getAlias()));
} }
Collections.sort(list); Collections.sort(list);
FileUtils.writeLines(deobfMapFile, MAP_FILE_CHARSET, list); Files.write(deobfMapFile, list, MAP_FILE_CHARSET);
if (LOG.isDebugEnabled()) { if (LOG.isDebugEnabled()) {
LOG.debug("Deobfuscation map file saved as: {}", deobfMapFile); LOG.debug("Deobfuscation map file saved as: {}", deobfMapFile);
} }
......
package jadx.core.deobf; package jadx.core.deobf;
import java.io.File; import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
...@@ -62,7 +63,12 @@ public class Deobfuscator { ...@@ -62,7 +63,12 @@ public class Deobfuscator {
private int fldIndex = 0; private int fldIndex = 0;
private int mthIndex = 0; private int mthIndex = 0;
@Deprecated
public Deobfuscator(JadxArgs args, @NotNull List<DexNode> dexNodes, File deobfMapFile) { public Deobfuscator(JadxArgs args, @NotNull List<DexNode> dexNodes, File deobfMapFile) {
this(args, dexNodes, deobfMapFile.toPath());
}
public Deobfuscator(JadxArgs args, @NotNull List<DexNode> dexNodes, Path deobfMapFile) {
this.args = args; this.args = args;
this.dexNodes = dexNodes; this.dexNodes = dexNodes;
......
package jadx.core.dex.visitors; package jadx.core.dex.visitors;
import java.io.File; import java.nio.file.Path;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.apache.commons.io.FilenameUtils;
import jadx.api.JadxArgs; import jadx.api.JadxArgs;
import jadx.core.Consts; import jadx.core.Consts;
import jadx.core.deobf.Deobfuscator; import jadx.core.deobf.Deobfuscator;
...@@ -33,13 +31,14 @@ public class RenameVisitor extends AbstractVisitor { ...@@ -33,13 +31,14 @@ public class RenameVisitor extends AbstractVisitor {
return; return;
} }
InputFile firstInputFile = dexNodes.get(0).getDexFile().getInputFile(); InputFile firstInputFile = dexNodes.get(0).getDexFile().getInputFile();
String firstInputFileName = firstInputFile.getFile().getAbsolutePath(); Path inputFilePath = firstInputFile.getFile().toPath();
String inputPath = FilenameUtils.getFullPathNoEndSeparator(firstInputFileName);
String inputName = FilenameUtils.getBaseName(firstInputFileName); String inputName = inputFilePath.getFileName().toString();
inputName = inputName.substring(0, inputName.lastIndexOf('.'));
File deobfMapFile = new File(inputPath, inputName + ".jobf"); Path deobfMapPath = inputFilePath.getParent().resolve(inputName + ".jobf");
JadxArgs args = root.getArgs(); JadxArgs args = root.getArgs();
deobfuscator = new Deobfuscator(args, dexNodes, deobfMapFile); deobfuscator = new Deobfuscator(args, dexNodes, deobfMapPath);
boolean deobfuscationOn = args.isDeobfuscationOn(); boolean deobfuscationOn = args.isDeobfuscationOn();
if (deobfuscationOn) { if (deobfuscationOn) {
deobfuscator.execute(); deobfuscator.execute();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment