From 68899cf3f3c91688e01084348578b1d41911983e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ryszard=20Wi=C5=9Bniewski?= Date: Fri, 3 Sep 2010 15:01:45 +0200 Subject: [PATCH] SmaliMod, BaksmaliMod: added modifications to original code. --- src/brut/androlib/mod/BaksmaliMod.java | 28 +++++++++++++++++++++++--- src/brut/androlib/mod/SmaliMod.java | 16 +++++++-------- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/src/brut/androlib/mod/BaksmaliMod.java b/src/brut/androlib/mod/BaksmaliMod.java index eee79744..eb0bb49d 100644 --- a/src/brut/androlib/mod/BaksmaliMod.java +++ b/src/brut/androlib/mod/BaksmaliMod.java @@ -16,6 +16,7 @@ */ package brut.androlib.mod; +import brut.androlib.src.TypeName; import java.io.*; import java.util.*; import java.util.regex.Matcher; @@ -32,7 +33,7 @@ import org.jf.dexlib.DexFile; */ public class BaksmaliMod { - public static void disassembleDexFile(String dexFilePath, DexFile dexFile, boolean deodex, String outputDirectory, + public static void disassembleDexFile(boolean debug, String dexFilePath, DexFile dexFile, boolean deodex, String outputDirectory, String[] classPathDirs, String bootClassPath, String extraBootClassPath, boolean noParameterRegisters, boolean useLocalsDirective, boolean useSequentialLabels, boolean outputDebugInfo, boolean addCodeOffsets, @@ -58,7 +59,10 @@ public class BaksmaliMod { }; } - if (registerInfo != 0 || deodex || verify) { + boolean analyze = ! ClassPath.dontLoadClassPath + && (registerInfo != 0 || deodex || verify); + + if (analyze) { try { String[] extraBootClassPathArray = null; if (extraBootClassPath != null && extraBootClassPath.length() > 0) { @@ -121,7 +125,7 @@ public class BaksmaliMod { * package name are separated by '/' */ - if (registerInfo != 0 || deodex || verify) { + if (analyze) { //If we are analyzing the bytecode, make sure that this class is loaded into the ClassPath. If it isn't //then there was some error while loading it, and we should skip it ClassPath.ClassDef classDef = ClassPath.getClassDef(classDefItem.getClassType(), false); @@ -141,6 +145,12 @@ public class BaksmaliMod { File smaliFile = fileNameHandler.getUniqueFilenameForClass(classDescriptor); + if (debug) { + String smaliPath = smaliFile.getPath(); + smaliFile = new File( + smaliPath.substring(0, smaliPath.length() - 6) + ".java"); + } + //create and initialize the top level string template ClassDefinition classDefinition = new ClassDefinition(classDefItem); @@ -167,7 +177,19 @@ public class BaksmaliMod { new FileOutputStream(smaliFile), "UTF8")); writer = new IndentingWriter(bufWriter); + + if (debug) { + TypeName name = TypeName.fromInternalName( + classDefItem.getClassType().getTypeDescriptor()); + writer.write("package " + name.package_ + "; class " + + name.getName(true, true) + " {/*\n\n"); + } + classDefinition.writeTo((IndentingWriter)writer); + + if (debug) { + writer.write("\n*/}\n"); + } } catch (Exception ex) { System.err.println("\n\nError occured while disassembling class " + classDescriptor.replace('/', '.') + " - skipping class"); ex.printStackTrace(); diff --git a/src/brut/androlib/mod/SmaliMod.java b/src/brut/androlib/mod/SmaliMod.java index d53c7aa8..802cbdcb 100644 --- a/src/brut/androlib/mod/SmaliMod.java +++ b/src/brut/androlib/mod/SmaliMod.java @@ -28,9 +28,10 @@ import org.jf.smali.*; */ public class SmaliMod { - private static boolean assembleSmaliFile(File smaliFile, DexFile dexFile, boolean verboseErrors, boolean oldLexer, - boolean printTokens) - throws Exception { + public static boolean assembleSmaliFile(InputStream smaliStream, + String name, DexFile dexFile, boolean verboseErrors, + boolean oldLexer, boolean printTokens) + throws IOException, RecognitionException { CommonTokenStream tokens; @@ -38,17 +39,16 @@ public class SmaliMod { LexerErrorInterface lexer; if (oldLexer) { - ANTLRFileStream input = new ANTLRFileStream(smaliFile.getAbsolutePath(), "UTF-8"); - input.name = smaliFile.getAbsolutePath(); + ANTLRInputStream input = new ANTLRInputStream(smaliStream, "UTF-8"); + input.name = name; lexer = new smaliLexer(input); tokens = new CommonTokenStream((TokenSource)lexer); } else { - FileInputStream fis = new FileInputStream(smaliFile.getAbsolutePath()); - InputStreamReader reader = new InputStreamReader(fis, "UTF-8"); + InputStreamReader reader = + new InputStreamReader(smaliStream, "UTF-8"); lexer = new smaliFlexLexer(reader); - ((smaliFlexLexer)lexer).setSourceFile(smaliFile); tokens = new CommonTokenStream((TokenSource)lexer); }