| 1 |
/* |
| 2 |
* Copyright (C) 2010 Ryszard Wiśniewski <[email protected]> |
| 3 |
* Copyright (C) 2010 Connor Tumbleson <[email protected]> |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
package brut.androlib.mod; |
| 18 |
|
| 19 |
import org.antlr.runtime.CommonTokenStream; |
| 20 |
import org.antlr.runtime.RecognitionException; |
| 21 |
import org.antlr.runtime.Token; |
| 22 |
import org.antlr.runtime.tree.CommonTree; |
| 23 |
import org.antlr.runtime.tree.CommonTreeNodeStream; |
| 24 |
import org.jf.dexlib2.writer.builder.DexBuilder; |
| 25 |
import org.jf.smali.smaliFlexLexer; |
| 26 |
import org.jf.smali.smaliParser; |
| 27 |
import org.jf.smali.smaliTreeWalker; |
| 28 |
|
| 29 |
import java.io.*; |
| 30 |
import java.nio.charset.StandardCharsets; |
| 31 |
|
| 32 |
public class SmaliMod { |
| 33 |
public static boolean assembleSmaliFile(File smaliFile,DexBuilder dexBuilder, int apiLevel, boolean verboseErrors, |
| 34 |
boolean printTokens) throws IOException, RecognitionException { |
| 35 |
|
| 36 |
CommonTokenStream tokens; |
| 37 |
smaliFlexLexer lexer; |
| 38 |
|
| 39 |
InputStream is = new FileInputStream(smaliFile); |
| 40 |
InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8); |
| 41 |
|
| 42 |
lexer = new smaliFlexLexer(reader, apiLevel); |
| 43 |
(lexer).setSourceFile(smaliFile); |
| 44 |
tokens = new CommonTokenStream(lexer); |
| 45 |
|
| 46 |
if (printTokens) { |
| 47 |
tokens.getTokens(); |
| 48 |
|
| 49 |
for (int i=0; i<tokens.size(); i++) { |
| 50 |
Token token = tokens.get(i); |
| 51 |
if (token.getChannel() == smaliParser.HIDDEN) { |
| 52 |
continue; |
| 53 |
} |
| 54 |
|
| 55 |
System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText()); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
smaliParser parser = new smaliParser(tokens); |
| 60 |
parser.setApiLevel(apiLevel); |
| 61 |
parser.setVerboseErrors(verboseErrors); |
| 62 |
|
| 63 |
smaliParser.smali_file_return result = parser.smali_file(); |
| 64 |
|
| 65 |
if (parser.getNumberOfSyntaxErrors() > 0 || lexer.getNumberOfSyntaxErrors() > 0) { |
| 66 |
is.close(); |
| 67 |
reader.close(); |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
CommonTree t = result.getTree(); |
| 72 |
|
| 73 |
CommonTreeNodeStream treeStream = new CommonTreeNodeStream(t); |
| 74 |
treeStream.setTokenStream(tokens); |
| 75 |
|
| 76 |
smaliTreeWalker dexGen = new smaliTreeWalker(treeStream); |
| 77 |
dexGen.setApiLevel(apiLevel); |
| 78 |
dexGen.setVerboseErrors(verboseErrors); |
| 79 |
dexGen.setDexBuilder(dexBuilder); |
| 80 |
dexGen.smali_file(); |
| 81 |
|
| 82 |
is.close(); |
| 83 |
reader.close(); |
| 84 |
|
| 85 |
return dexGen.getNumberOfSyntaxErrors() == 0; |
| 86 |
} |
| 87 |
} |