This commit is contained in:
Connor Tumbleson 2013-06-24 11:46:26 -05:00
parent b0cee3c500
commit ca67c00f83
3 changed files with 105 additions and 114 deletions

View File

@ -20,7 +20,7 @@ import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import org.jf.dexlib.DexFile;
import org.jf.dexlib2.writer.builder.DexBuilder;
import org.jf.smali.*;
/**
@ -28,58 +28,53 @@ import org.jf.smali.*;
*/
public class SmaliMod {
public static boolean assembleSmaliFile(InputStream smaliStream,
String name, DexFile dexFile, boolean verboseErrors,
boolean oldLexer, boolean printTokens) throws IOException,
RecognitionException {
CommonTokenStream tokens;
public static boolean assembleSmaliFile(File smaliFile,DexBuilder dexBuilder, boolean verboseErrors,
boolean printTokens) throws IOException,
RecognitionException {
boolean lexerErrors = false;
LexerErrorInterface lexer;
CommonTokenStream tokens;
LexerErrorInterface lexer;
InputStreamReader reader = new InputStreamReader(smaliStream, "UTF-8");
FileInputStream fis = new FileInputStream(smaliFile.getAbsolutePath());
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
lexer = new smaliFlexLexer(reader);
tokens = new CommonTokenStream((TokenSource) lexer);
lexer = new smaliFlexLexer(reader);
((smaliFlexLexer)lexer).setSourceFile(smaliFile);
tokens = new CommonTokenStream((TokenSource) lexer);
if (printTokens) {
tokens.getTokens();
if (printTokens) {
tokens.getTokens();
for (int i = 0; i < tokens.size(); i++) {
Token token = tokens.get(i);
if (token.getChannel() == BaseRecognizer.HIDDEN) {
continue;
}
for (int i=0; i<tokens.size(); i++) {
Token token = tokens.get(i);
if (token.getChannel() == smaliParser.HIDDEN) {
continue;
}
System.out.println(smaliParser.tokenNames[token.getType()]
+ ": " + token.getText());
}
}
System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
}
}
smaliParser parser = new smaliParser(tokens);
parser.setVerboseErrors(verboseErrors);
smaliParser parser = new smaliParser(tokens);
parser.setVerboseErrors(verboseErrors);
smaliParser.smali_file_return result = parser.smali_file();
smaliParser.smali_file_return result = parser.smali_file();
if (parser.getNumberOfSyntaxErrors() > 0
|| lexer.getNumberOfSyntaxErrors() > 0) {
return false;
}
if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
return false;
}
CommonTree t = (CommonTree) result.getTree();
CommonTree t = (CommonTree) result.getTree();
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
treeStream.setTokenStream(tokens);
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
treeStream.setTokenStream(tokens);
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
dexGen.dexFile = dexFile;
dexGen.smali_file();
dexGen.setVerboseErrors(verboseErrors);
dexGen.setDexBuilder(dexBuilder);
dexGen.smali_file();
if (dexGen.getNumberOfSyntaxErrors() > 0) {
return false;
}
return true;
}
return dexGen.getNumberOfSyntaxErrors() == 0;
}
}

View File

@ -16,69 +16,58 @@
package brut.androlib.src;
import brut.androlib.AndrolibException;
import brut.androlib.mod.SmaliMod;
import java.io.*;
import org.antlr.runtime.RecognitionException;
import org.jf.dexlib.CodeItem;
import org.jf.dexlib.DexFile;
import org.jf.dexlib.Util.ByteArrayAnnotatedOutput;
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
*/
public class DexFileBuilder {
public void addSmaliFile(File smaliFile) throws AndrolibException {
try {
addSmaliFile(new FileInputStream(smaliFile),
smaliFile.getAbsolutePath());
} catch (FileNotFoundException ex) {
throw new AndrolibException(ex);
}
}
public void addSmaliFile(InputStream smaliStream, String name)
throws AndrolibException {
try {
if (!SmaliMod.assembleSmaliFile(smaliStream, name, mDexFile, false,
false, false)) {
throw new AndrolibException("Could not smali file: " + name);
}
} catch (IOException ex) {
throw new AndrolibException(ex);
} catch (RecognitionException ex) {
throw new AndrolibException(ex);
}
}
public void writeTo(File dexFile) throws AndrolibException {
try {
OutputStream out = new FileOutputStream(dexFile);
out.write(getAsByteArray());
out.close();
} catch (IOException ex) {
throw new AndrolibException("Could not write dex to file: "
+ dexFile, ex);
}
}
public byte[] getAsByteArray() {
mDexFile.place();
for (CodeItem codeItem : mDexFile.CodeItemsSection.getItems()) {
codeItem.fixInstructions(true, true);
}
mDexFile.place();
ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
mDexFile.writeTo(out);
byte[] bytes = out.toByteArray();
DexFile.calcSignature(bytes);
DexFile.calcChecksum(bytes);
return bytes;
}
private final DexFile mDexFile = new DexFile();
// public void addSmaliFile(File smaliFile, DexBuilder dexBuilder) throws AndrolibException {
// try {
// addSmaliFile(new FileInputStream(smaliFile), smaliFile, smaliFile.getAbsolutePath(), dexBuilder);
// } catch (FileNotFoundException ex) {
// throw new AndrolibException(ex);
// }
// }
//
// public void addSmaliFile(InputStream smaliStream,File smaliFile, String name, DexBuilder dexBuilder)
// throws AndrolibException {
// try {
// if (!SmaliMod.assembleSmaliFile(smaliStream, smaliFile, name, dexBuilder, false, false)) {
// throw new AndrolibException("Could not smali file: " + name);
// }
// } catch (IOException ex) {
// throw new AndrolibException(ex);
// } catch (RecognitionException ex) {
// throw new AndrolibException(ex);
// }
// }
//
// public void writeTo(File dexFile) throws AndrolibException {
// try {
// OutputStream out = new FileOutputStream(dexFile);
// out.write(getAsByteArray());
// out.close();
// } catch (IOException ex) {
// throw new AndrolibException("Could not write dex to file: "
// + dexFile, ex);
// }
// }
//
// public byte[] getAsByteArray() {
// mDexFile.place();
// for (CodeItem codeItem : mDexFile.CodeItemsSection.getItems()) {
// codeItem.fixInstructions(true, true);
// }
//
// mDexFile.place();
//
// ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
// mDexFile.writeTo(out);
// byte[] bytes = out.toByteArray();
//
// DexFile.calcSignature(bytes);
// DexFile.calcChecksum(bytes);
//
// return bytes;
// }
}

View File

@ -17,6 +17,7 @@
package brut.androlib.src;
import brut.androlib.AndrolibException;
import brut.androlib.mod.SmaliMod;
import brut.androlib.res.util.ExtFile;
import brut.directory.DirectoryException;
import java.io.*;
@ -24,7 +25,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.logging.Logger;
import org.antlr.runtime.RecognitionException;
import org.apache.commons.io.IOUtils;
import org.jf.dexlib2.writer.builder.DexBuilder;
/**
* @author Ryszard Wiśniewski <brut.alll@gmail.com>
@ -45,25 +49,30 @@ public class SmaliBuilder {
private void build() throws AndrolibException {
try {
mDexBuilder = new DexFileBuilder();
for (String fileName : mSmaliDir.getDirectory().getFiles(true)) {
buildFile(fileName);
DexBuilder dexBuilder = DexBuilder.makeDexBuilder();
for (String fileName : mSmaliDir.getDirectory().getFiles(true)) {
buildFile(fileName, dexBuilder);
}
mDexBuilder.writeTo(mDexFile);
} catch (IOException ex) {
throw new AndrolibException(ex);
} catch (DirectoryException ex) {
dexBuilder.writeTo(mDexFile.getAbsolutePath());
} catch (IOException | DirectoryException ex) {
throw new AndrolibException(ex);
}
}
private void buildFile(String fileName) throws AndrolibException,
private void buildFile(String fileName, DexBuilder dexBuilder) throws AndrolibException,
IOException {
File inFile = new File(mSmaliDir, fileName);
InputStream inStream = new FileInputStream(inFile);
if (fileName.endsWith(".smali")) {
mDexBuilder.addSmaliFile(inFile);
try {
if (!SmaliMod.assembleSmaliFile(inFile,dexBuilder, false, false)) {
throw new AndrolibException("Could not smali file: " + fileName);
}
} catch (IOException | RecognitionException ex) {
throw new AndrolibException(ex);
}
return;
}
if (!fileName.endsWith(".java")) {
@ -99,16 +108,14 @@ public class SmaliBuilder {
out.append(line).append('\n');
}
}
mDexBuilder.addSmaliFile(IOUtils.toInputStream(out.toString()),
fileName);
//mDexBuilder.addSmaliFile(IOUtils.toInputStream(out.toString()),
//fileName);
}
private final ExtFile mSmaliDir;
private final File mDexFile;
private final HashMap<String, Boolean> mFlags;
private DexFileBuilder mDexBuilder;
private final static Logger LOGGER = Logger.getLogger(SmaliBuilder.class
.getName());
}