use of org.jf.dexlib.DexFile in project otertool by wuntee.
the class SmaliWorkshop method buildDex.
private static void buildDex(File smaliSourceDirectory, File destination) throws RecognitionException, SmaliSyntaxException, SmaliDexException, IOException {
DexFile dexFile = new DexFile();
// Load files into set
logger.debug("Loading smali files");
LinkedHashSet<File> filesToProcess = new LinkedHashSet<File>();
getSmaliFilesInDir(smaliSourceDirectory, filesToProcess);
// Process each file
logger.debug("Processing files");
for (File file : filesToProcess) {
logger.debug("Processing: " + file.getAbsolutePath());
FileInputStream fis = new FileInputStream(file.getAbsolutePath());
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
LexerErrorInterface lexer = new smaliFlexLexer(reader);
((smaliFlexLexer) lexer).setSourceFile(file);
CommonTokenStream tokens = new CommonTokenStream((TokenSource) lexer);
smaliParser parser = new smaliParser(tokens);
smaliParser.smali_file_return result = parser.smali_file();
// Errors
if (parser.getNumberOfSyntaxErrors() > 0) {
throw new SmaliSyntaxException(file, parser.getNumberOfSyntaxErrors());
}
if (lexer.getNumberOfSyntaxErrors() > 0) {
throw new SmaliSyntaxException(file, lexer.getNumberOfSyntaxErrors());
}
CommonTree t = (CommonTree) result.getTree();
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
treeStream.setTokenStream(tokens);
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
dexGen.dexFile = dexFile;
dexGen.smali_file();
if (dexGen.getNumberOfSyntaxErrors() > 0) {
throw new SmaliDexException(file, dexGen.getNumberOfSyntaxErrors());
}
}
// Calculate signatures and write file
logger.debug("Writing dex file.");
dexFile.place();
ByteArrayAnnotatedOutput out = new ByteArrayAnnotatedOutput();
dexFile.writeTo(out);
byte[] bytes = out.toByteArray();
DexFile.calcSignature(bytes);
DexFile.calcChecksum(bytes);
FileOutputStream fileOutputStream = new FileOutputStream(destination);
fileOutputStream.write(bytes);
fileOutputStream.close();
}
use of org.jf.dexlib.DexFile in project otertool by wuntee.
the class SmaliWorkshop method getSmaliSource.
public static Map<String, File> getSmaliSource(File sourceSmaliOrDexFile, File destinationDirectory) throws IOException {
Map<String, File> ret = new HashMap<String, File>();
DexFile dexFile = new DexFile(sourceSmaliOrDexFile);
IndentingWriter idWriter;
for (ClassDefItem c : dexFile.ClassDefsSection.getItems()) {
File classFile = SmaliWorkshop.createSmaliClassFile(destinationDirectory, c);
String className = SmaliWorkshop.classDefItemToFilename(c);
logger.debug("Got class: " + className + " [" + classFile + "]");
BufferedWriter fileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(classFile)));
idWriter = new IndentingWriter(fileWriter);
ClassDefinition cd = new ClassDefinition(c);
cd.writeTo(idWriter);
ret.put(className, classFile);
idWriter.close();
}
return (sortMapByKey(ret));
}
Aggregations