Search in sources :

Example 1 with LexerErrorInterface

use of org.jf.smali.LexerErrorInterface in project atlas by alibaba.

the class SmaliMod method assembleSmaliFile.

public static boolean assembleSmaliFile(File smaliFile, DexBuilder dexBuilder, boolean verboseErrors, boolean printTokens) throws IOException, RecognitionException {
    CommonTokenStream tokens;
    LexerErrorInterface lexer;
    InputStream is = new FileInputStream(smaliFile);
    InputStreamReader reader = new InputStreamReader(is, "UTF-8");
    lexer = new smaliFlexLexer(reader);
    ((smaliFlexLexer) lexer).setSourceFile(smaliFile);
    tokens = new CommonTokenStream((TokenSource) lexer);
    if (printTokens) {
        tokens.getTokens();
        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());
        }
    }
    smaliParser parser = new smaliParser(tokens);
    parser.setVerboseErrors(verboseErrors);
    smaliParser.smali_file_return result = parser.smali_file();
    if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) {
        return false;
    }
    CommonTree t = (CommonTree) result.getTree();
    CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t);
    treeStream.setTokenStream(tokens);
    smaliTreeWalker dexGen = new smaliTreeWalker(treeStream);
    dexGen.setVerboseErrors(verboseErrors);
    dexGen.setDexBuilder(dexBuilder);
    dexGen.smali_file();
    return dexGen.getNumberOfSyntaxErrors() == 0;
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) TokenSource(org.antlr.runtime.TokenSource) InputStreamReader(java.io.InputStreamReader) CommonTree(org.antlr.runtime.tree.CommonTree) FileInputStream(java.io.FileInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Token(org.antlr.runtime.Token) org.jf.smali.smaliFlexLexer(org.jf.smali.smaliFlexLexer) FileInputStream(java.io.FileInputStream) org.jf.smali.smaliParser(org.jf.smali.smaliParser) org.jf.smali.smaliTreeWalker(org.jf.smali.smaliTreeWalker) LexerErrorInterface(org.jf.smali.LexerErrorInterface) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Example 2 with LexerErrorInterface

use of org.jf.smali.LexerErrorInterface 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();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CommonTokenStream(org.antlr.runtime.CommonTokenStream) InputStreamReader(java.io.InputStreamReader) CommonTree(org.antlr.runtime.tree.CommonTree) SmaliDexException(com.wuntee.oter.exception.SmaliDexException) ByteArrayAnnotatedOutput(org.jf.dexlib.Util.ByteArrayAnnotatedOutput) SmaliSyntaxException(com.wuntee.oter.exception.SmaliSyntaxException) org.jf.smali.smaliFlexLexer(org.jf.smali.smaliFlexLexer) DexFile(org.jf.dexlib.DexFile) FileInputStream(java.io.FileInputStream) org.jf.smali.smaliParser(org.jf.smali.smaliParser) FileOutputStream(java.io.FileOutputStream) org.jf.smali.smaliTreeWalker(org.jf.smali.smaliTreeWalker) LexerErrorInterface(org.jf.smali.LexerErrorInterface) File(java.io.File) DexFile(org.jf.dexlib.DexFile) CommonTreeNodeStream(org.antlr.runtime.tree.CommonTreeNodeStream)

Aggregations

FileInputStream (java.io.FileInputStream)2 InputStreamReader (java.io.InputStreamReader)2 CommonTokenStream (org.antlr.runtime.CommonTokenStream)2 CommonTree (org.antlr.runtime.tree.CommonTree)2 CommonTreeNodeStream (org.antlr.runtime.tree.CommonTreeNodeStream)2 LexerErrorInterface (org.jf.smali.LexerErrorInterface)2 org.jf.smali.smaliFlexLexer (org.jf.smali.smaliFlexLexer)2 org.jf.smali.smaliParser (org.jf.smali.smaliParser)2 org.jf.smali.smaliTreeWalker (org.jf.smali.smaliTreeWalker)2 SmaliDexException (com.wuntee.oter.exception.SmaliDexException)1 SmaliSyntaxException (com.wuntee.oter.exception.SmaliSyntaxException)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 LinkedHashSet (java.util.LinkedHashSet)1 Token (org.antlr.runtime.Token)1 TokenSource (org.antlr.runtime.TokenSource)1 DexFile (org.jf.dexlib.DexFile)1 ByteArrayAnnotatedOutput (org.jf.dexlib.Util.ByteArrayAnnotatedOutput)1