Smali and baksmali using SmaliMod and BaksmaliMod.

This commit is contained in:
Ryszard Wiśniewski 2010-09-03 15:05:07 +02:00
parent 68899cf3f3
commit 4219a8e878
2 changed files with 15 additions and 82 deletions

View File

@ -17,14 +17,12 @@
package brut.androlib.src;
import brut.androlib.AndrolibException;
import brut.androlib.mod.SmaliMod;
import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.antlr.runtime.RecognitionException;
import org.jf.dexlib.CodeItem;
import org.jf.dexlib.DexFile;
import org.jf.dexlib.Util.ByteArrayAnnotatedOutput;
import org.jf.smali.*;
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -42,7 +40,8 @@ public class DexFileBuilder {
public void addSmaliFile(InputStream smaliStream, String name)
throws AndrolibException {
try {
if (!assembleSmaliFile(smaliStream, name)) {
if (! SmaliMod.assembleSmaliFile(
smaliStream, name, mDexFile, false, false, false)) {
throw new AndrolibException(
"Could not smali file: " + smaliStream);
}
@ -82,38 +81,5 @@ public class DexFileBuilder {
return bytes;
}
private boolean assembleSmaliFile(InputStream smaliStream, String name)
throws IOException, RecognitionException {
ANTLRInputStream input = new ANTLRInputStream(smaliStream, "UTF8");
input.name = name;
smaliLexer lexer = new smaliLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
smaliParser parser = new smaliParser(tokens);
smaliParser.smali_file_return result = parser.smali_file();
if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfLexerErrors() > 0) {
return false;
}
CommonTree t = (CommonTree) result.getTree();
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
treeStream.setTokenStream(tokens);
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
dexGen.dexFile = mDexFile;
dexGen.smali_file();
if (dexGen.getNumberOfSyntaxErrors() > 0) {
return false;
}
return true;
}
private final DexFile mDexFile = new DexFile();
}

View File

@ -17,11 +17,9 @@
package brut.androlib.src;
import brut.androlib.AndrolibException;
import brut.androlib.mod.IndentingWriter;
import java.io.*;
import org.jf.baksmali.Adaptors.ClassDefinition;
import org.jf.baksmali.baksmali;
import org.jf.dexlib.ClassDefItem;
import brut.androlib.mod.BaksmaliMod;
import java.io.File;
import java.io.IOException;
import org.jf.dexlib.Code.Analysis.ClassPath;
import org.jf.dexlib.DexFile;
@ -42,51 +40,20 @@ public class SmaliDecoder {
}
private void decode() throws AndrolibException {
try {
baksmali.useLocalsDirective = true;
baksmali.useSequentialLabels = true;
if (mDebug) {
baksmali.registerInfo = org.jf.baksmali.main.DIFFPRE;
ClassPath.dontLoadClassPath = true;
}
if (mDebug) {
ClassPath.dontLoadClassPath = true;
}
DexFile dexFile = new DexFile(mApkFile);
for (ClassDefItem classDefItem :
dexFile.ClassDefsSection.getItems()) {
decodeClassDefItem(classDefItem);
}
try {
BaksmaliMod.disassembleDexFile(mDebug, mApkFile.getAbsolutePath(),
new DexFile(mApkFile), false, mOutDir.getAbsolutePath(),
null, null, null, false, true, true, true, false,
mDebug ? org.jf.baksmali.main.DIFFPRE : 0, false, false);
} catch (IOException ex) {
throw new AndrolibException(ex);
}
}
private void decodeClassDefItem(ClassDefItem classDefItem)
throws AndrolibException, IOException {
TypeName name = TypeName.fromInternalName(
classDefItem.getClassType().getTypeDescriptor());
File outFile = new File(mOutDir, name.getFilePath(true)
+ (mDebug ? ".java" : ".smali"));
if (outFile.exists()) {
throw new AndrolibException(
"File already exists: " + outFile);
}
outFile.getParentFile().mkdirs();
IndentingWriter indentWriter =
new IndentingWriter(new FileWriter(outFile));
if (mDebug) {
indentWriter.write("package " + name.package_ + "; class "
+ name.getName(true, true) + " {/*\n\n");
}
new ClassDefinition(classDefItem).writeTo(indentWriter);
if (mDebug) {
indentWriter.write("\n*/}\n");
}
indentWriter.close();
}
private final File mApkFile;
private final File mOutDir;
private final boolean mDebug;