Skip to content

Commit 4219a8e

Browse files
committed
Smali and baksmali using SmaliMod and BaksmaliMod.
1 parent 68899cf commit 4219a8e

File tree

2 files changed

+15
-82
lines changed

2 files changed

+15
-82
lines changed

src/brut/androlib/src/DexFileBuilder.java

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,12 @@
1717
package brut.androlib.src;
1818

1919
import brut.androlib.AndrolibException;
20+
import brut.androlib.mod.SmaliMod;
2021
import java.io.*;
21-
import org.antlr.runtime.*;
22-
import org.antlr.runtime.tree.CommonTree;
23-
import org.antlr.runtime.tree.CommonTreeNodeStream;
22+
import org.antlr.runtime.RecognitionException;
2423
import org.jf.dexlib.CodeItem;
2524
import org.jf.dexlib.DexFile;
2625
import org.jf.dexlib.Util.ByteArrayAnnotatedOutput;
27-
import org.jf.smali.*;
2826

2927
/**
3028
* @author Ryszard Wiśniewski <[email protected]>
@@ -42,7 +40,8 @@ public void addSmaliFile(File smaliFile) throws AndrolibException {
4240
public void addSmaliFile(InputStream smaliStream, String name)
4341
throws AndrolibException {
4442
try {
45-
if (!assembleSmaliFile(smaliStream, name)) {
43+
if (! SmaliMod.assembleSmaliFile(
44+
smaliStream, name, mDexFile, false, false, false)) {
4645
throw new AndrolibException(
4746
"Could not smali file: " + smaliStream);
4847
}
@@ -82,38 +81,5 @@ public byte[] getAsByteArray() {
8281
return bytes;
8382
}
8483

85-
private boolean assembleSmaliFile(InputStream smaliStream, String name)
86-
throws IOException, RecognitionException {
87-
ANTLRInputStream input = new ANTLRInputStream(smaliStream, "UTF8");
88-
input.name = name;
89-
90-
smaliLexer lexer = new smaliLexer(input);
91-
92-
CommonTokenStream tokens = new CommonTokenStream(lexer);
93-
smaliParser parser = new smaliParser(tokens);
94-
95-
smaliParser.smali_file_return result = parser.smali_file();
96-
97-
if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfLexerErrors() > 0) {
98-
return false;
99-
}
100-
101-
CommonTree t = (CommonTree) result.getTree();
102-
103-
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
104-
treeStream.setTokenStream(tokens);
105-
106-
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
107-
108-
dexGen.dexFile = mDexFile;
109-
dexGen.smali_file();
110-
111-
if (dexGen.getNumberOfSyntaxErrors() > 0) {
112-
return false;
113-
}
114-
115-
return true;
116-
}
117-
11884
private final DexFile mDexFile = new DexFile();
11985
}

src/brut/androlib/src/SmaliDecoder.java

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,9 @@
1717
package brut.androlib.src;
1818

1919
import brut.androlib.AndrolibException;
20-
import brut.androlib.mod.IndentingWriter;
21-
import java.io.*;
22-
import org.jf.baksmali.Adaptors.ClassDefinition;
23-
import org.jf.baksmali.baksmali;
24-
import org.jf.dexlib.ClassDefItem;
20+
import brut.androlib.mod.BaksmaliMod;
21+
import java.io.File;
22+
import java.io.IOException;
2523
import org.jf.dexlib.Code.Analysis.ClassPath;
2624
import org.jf.dexlib.DexFile;
2725

@@ -42,51 +40,20 @@ private SmaliDecoder(File apkFile, File outDir, boolean debug) {
4240
}
4341

4442
private void decode() throws AndrolibException {
45-
try {
46-
baksmali.useLocalsDirective = true;
47-
baksmali.useSequentialLabels = true;
48-
if (mDebug) {
49-
baksmali.registerInfo = org.jf.baksmali.main.DIFFPRE;
50-
ClassPath.dontLoadClassPath = true;
51-
}
43+
if (mDebug) {
44+
ClassPath.dontLoadClassPath = true;
45+
}
5246

53-
DexFile dexFile = new DexFile(mApkFile);
54-
for (ClassDefItem classDefItem :
55-
dexFile.ClassDefsSection.getItems()) {
56-
decodeClassDefItem(classDefItem);
57-
}
47+
try {
48+
BaksmaliMod.disassembleDexFile(mDebug, mApkFile.getAbsolutePath(),
49+
new DexFile(mApkFile), false, mOutDir.getAbsolutePath(),
50+
null, null, null, false, true, true, true, false,
51+
mDebug ? org.jf.baksmali.main.DIFFPRE : 0, false, false);
5852
} catch (IOException ex) {
5953
throw new AndrolibException(ex);
6054
}
6155
}
6256

63-
private void decodeClassDefItem(ClassDefItem classDefItem)
64-
throws AndrolibException, IOException {
65-
TypeName name = TypeName.fromInternalName(
66-
classDefItem.getClassType().getTypeDescriptor());
67-
File outFile = new File(mOutDir, name.getFilePath(true)
68-
+ (mDebug ? ".java" : ".smali"));
69-
70-
if (outFile.exists()) {
71-
throw new AndrolibException(
72-
"File already exists: " + outFile);
73-
}
74-
outFile.getParentFile().mkdirs();
75-
76-
IndentingWriter indentWriter =
77-
new IndentingWriter(new FileWriter(outFile));
78-
79-
if (mDebug) {
80-
indentWriter.write("package " + name.package_ + "; class "
81-
+ name.getName(true, true) + " {/*\n\n");
82-
}
83-
new ClassDefinition(classDefItem).writeTo(indentWriter);
84-
if (mDebug) {
85-
indentWriter.write("\n*/}\n");
86-
}
87-
indentWriter.close();
88-
}
89-
9057
private final File mApkFile;
9158
private final File mOutDir;
9259
private final boolean mDebug;

0 commit comments

Comments
 (0)