Skip to content

Commit

Permalink
Merge fe03c85 into b2f41e9
Browse files Browse the repository at this point in the history
  • Loading branch information
jiqimaogou committed Nov 8, 2016
2 parents b2f41e9 + fe03c85 commit 68b214d
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 50 deletions.
6 changes: 3 additions & 3 deletions jadx-core/src/main/java/jadx/api/ResourceFile.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package jadx.api;

import jadx.core.xmlgen.ResContainer;

import java.io.File;

import jadx.core.xmlgen.ResContainer;

public class ResourceFile {

public static final class ZipRef {
Expand Down Expand Up @@ -56,7 +56,7 @@ void setZipRef(ZipRef zipRef) {
this.zipRef = zipRef;
}

ZipRef getZipRef() {
public ZipRef getZipRef() {
return zipRef;
}

Expand Down
56 changes: 35 additions & 21 deletions jadx-core/src/main/java/jadx/api/ResourcesLoader.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package jadx.api;

import jadx.api.ResourceFile.ZipRef;
import jadx.core.codegen.CodeWriter;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.files.InputFile;
import jadx.core.xmlgen.ResContainer;
import jadx.core.xmlgen.ResTableParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
Expand All @@ -19,8 +15,13 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.api.ResourceFile.ZipRef;
import jadx.core.codegen.CodeWriter;
import jadx.core.utils.Utils;
import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.files.InputFile;
import jadx.core.xmlgen.ResContainer;
import jadx.core.xmlgen.ResTableParser;

import static jadx.core.utils.files.FileUtils.READ_BUFFER_SIZE;
import static jadx.core.utils.files.FileUtils.close;
Expand Down Expand Up @@ -51,30 +52,35 @@ public interface ResourceDecoder {
}

public static ResContainer decodeStream(ResourceFile rf, ResourceDecoder decoder) throws JadxException {
ZipRef zipRef = rf.getZipRef();
if (zipRef == null) {
return null;
}
ZipFile zipFile = null;
InputStream inputStream = null;
ResContainer result = null;
try {
zipFile = new ZipFile(zipRef.getZipFile());
ZipEntry entry = zipFile.getEntry(zipRef.getEntryName());
if (entry == null) {
throw new IOException("Zip entry not found: " + zipRef);
long size;
ZipRef zipRef = rf.getZipRef();
if (zipRef == null) {
File file = new File(rf.getName());
inputStream = new BufferedInputStream(new FileInputStream(file));
size = file.length();
} else {
zipFile = new ZipFile(zipRef.getZipFile());
ZipEntry entry = zipFile.getEntry(zipRef.getEntryName());
if (entry == null) {
throw new IOException("Zip entry not found: " + zipRef);
}
inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
size = entry.getSize();
}
inputStream = new BufferedInputStream(zipFile.getInputStream(entry));
result = decoder.decode(entry.getSize(), inputStream);
result = decoder.decode(size, inputStream);
} catch (Exception e) {
throw new JadxException("Error decode: " + zipRef.getEntryName(), e);
throw new JadxException("Error decode: " + rf.getName(), e);
} finally {
try {
if (zipFile != null) {
zipFile.close();
}
} catch (Exception e) {
LOG.error("Error close zip file: {}", zipRef, e);
LOG.error("Error close zip file: {}", rf.getName(), e);
}
close(inputStream);
}
Expand Down Expand Up @@ -133,6 +139,7 @@ private void loadFile(List<ResourceFile> list, File file) {
}
} catch (IOException e) {
LOG.debug("Not a zip file: {}", file.getAbsolutePath());
addResourceFile(list, file);
} finally {
if (zip != null) {
try {
Expand All @@ -144,6 +151,13 @@ private void loadFile(List<ResourceFile> list, File file) {
}
}

private void addResourceFile(List<ResourceFile> list, File file) {
String name = file.getAbsolutePath();
ResourceType type = ResourceType.getFileType(name);
ResourceFile rf = new ResourceFile(jadxRef, name, type);
list.add(rf);
}

private void addEntry(List<ResourceFile> list, File zipFile, ZipEntry entry) {
if (entry.isDirectory()) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import jadx.core.dex.info.MethodInfo;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.DexNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.RootNode;
Expand All @@ -18,6 +19,7 @@

import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.io.FilenameUtils;
Expand All @@ -33,13 +35,17 @@ public class RenameVisitor extends AbstractVisitor {
public void init(RootNode root) {
IJadxArgs args = root.getArgs();

InputFile firstInputFile = root.getDexNodes().get(0).getDexFile().getInputFile();
List<DexNode> dexNodes = root.getDexNodes();
if (dexNodes.size() == 0) {
return;
}
InputFile firstInputFile = dexNodes.get(0).getDexFile().getInputFile();
final String firstInputFileName = firstInputFile.getFile().getAbsolutePath();
final String inputPath = FilenameUtils.getFullPathNoEndSeparator(firstInputFileName);
final String inputName = FilenameUtils.getBaseName(firstInputFileName);

File deobfMapFile = new File(inputPath, inputName + ".jobf");
deobfuscator = new Deobfuscator(args, root.getDexNodes(), deobfMapFile);
deobfuscator = new Deobfuscator(args, dexNodes, deobfMapFile);
boolean deobfuscationOn = args.isDeobfuscationOn();
if (deobfuscationOn) {
deobfuscator.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ public static boolean handleAppResField(CodeWriter code, ClassGen clsGen, ClassI
}

private static ClassNode makeClass(RootNode root, String clsName) {
DexNode firstDex = root.getDexNodes().get(0);
List<DexNode> dexNodes = root.getDexNodes();
if (dexNodes.size() == 0) {
return null;
}
DexNode firstDex = dexNodes.get(0);
ClassInfo r = ClassInfo.fromName(firstDex, clsName);
return new ClassNode(firstDex, r);
}
Expand Down
20 changes: 20 additions & 0 deletions jadx-core/src/main/java/jadx/core/utils/files/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.OutputStream;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;

import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -100,4 +102,22 @@ public static File prepareFile(File file) {
makeDirsForFile(file);
return file;
}

public static boolean isZipFile(final File file) throws IOException {
ZipFile zipFile = null;
try {
zipFile = new ZipFile(file);
return zipFile.entries().hasMoreElements();
} catch (ZipException e) {
return false;
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
LOG.error(e.getMessage());
}
}
}
}
}
23 changes: 11 additions & 12 deletions jadx-core/src/main/java/jadx/core/utils/files/InputFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,22 @@ private void searchDexFiles() throws IOException, DecodeException {
addDexFile(loadFromClassFile(file));
return;
}
if (fileName.endsWith(".apk") || fileName.endsWith(".zip")) {
loadFromZip(".dex");
return;
}
if (fileName.endsWith(".jar")) {
// check if jar contains '.dex' files
if (FileUtils.isZipFile(file)) {
// check if zip contains '.dex' files
if (loadFromZip(".dex")) {
return;
}
addDexFile(loadFromJar(file));
return;
}
if (fileName.endsWith(".aar")) {
loadFromZip(".jar");
if (fileName.endsWith(".jar")) {
addDexFile(loadFromJar(file));
return;
}
if (fileName.endsWith(".aar")) {
loadFromZip(".jar");
return;
}
return;
}
throw new DecodeException("Unsupported input file format: " + file);
//throw new DecodeException("Unsupported input file format: " + file);
}

private void addDexFile(Dex dexBuf) throws IOException {
Expand Down
4 changes: 2 additions & 2 deletions jadx-core/src/main/java/jadx/core/xmlgen/ResTableParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ private PackageChunk parsePackage() throws IOException {
}

PackageChunk pkg = new PackageChunk(id, name, typeStrings, keyStrings);
if (id == 0x7F) {
//if (id == 0x7F) {
resStorage.setAppPackage(name);
}
//}

while (is.getPos() < endPos) {
long chunkStart = is.getPos();
Expand Down
23 changes: 15 additions & 8 deletions jadx-gui/src/main/java/jadx/gui/treemodel/JRoot.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package jadx.gui.treemodel;

import jadx.api.ResourceFile;
import jadx.gui.JadxWrapper;
import jadx.gui.treemodel.JResource.JResType;
import jadx.gui.utils.Utils;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.io.File;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.regex.Pattern;

import javax.swing.Icon;
import javax.swing.ImageIcon;

import jadx.api.ResourceFile;
import jadx.gui.JadxWrapper;
import jadx.gui.treemodel.JResource.JResType;
import jadx.gui.utils.Utils;

public class JRoot extends JNode {
private static final long serialVersionUID = 8888495789773527342L;

Expand Down Expand Up @@ -45,7 +46,13 @@ private List<JResource> getHierarchyResources(List<ResourceFile> resources) {
JResource root = new JResource(null, "Resources", JResType.ROOT);
String splitPathStr = Pattern.quote(File.separator);
for (ResourceFile rf : resources) {
String[] parts = new File(rf.getName()).getPath().split(splitPathStr);
String rfName;
if (rf.getZipRef() != null) {
rfName = rf.getName();
} else {
rfName = new File(rf.getName()).getName();
}
String[] parts = new File(rfName).getPath().split(splitPathStr);
JResource curRf = root;
int count = parts.length;
for (int i = 0; i < count; i++) {
Expand Down
2 changes: 1 addition & 1 deletion jadx-gui/src/main/java/jadx/gui/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public void run() {
public void openFile() {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setAcceptAllFileFilterUsed(true);
String[] exts = {"apk", "dex", "jar", "class", "zip", "aar"};
String[] exts = {"apk", "dex", "jar", "class", "zip", "aar", "arsc"};
String description = "supported files: " + Arrays.toString(exts).replace('[', '(').replace(']', ')');
fileChooser.setFileFilter(new FileNameExtensionFilter(description, exts));
fileChooser.setToolTipText(NLS.str("file.open"));
Expand Down

0 comments on commit 68b214d

Please sign in to comment.