mirror of
https://github.com/revanced/Apktool.git
synced 2025-01-21 09:17:35 +01:00
[SmaliMod] adjust handling for stream, files and dexlib2
This commit is contained in:
parent
3acff3ef34
commit
5aa18eee2d
@ -20,6 +20,7 @@ import java.io.*;
|
||||
import org.antlr.runtime.*;
|
||||
import org.antlr.runtime.tree.CommonTree;
|
||||
import org.antlr.runtime.tree.CommonTreeNodeStream;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.jf.dexlib2.writer.builder.DexBuilder;
|
||||
import org.jf.smali.*;
|
||||
|
||||
@ -28,53 +29,73 @@ import org.jf.smali.*;
|
||||
*/
|
||||
public class SmaliMod {
|
||||
|
||||
public static boolean assembleSmaliFile(File smaliFile,DexBuilder dexBuilder, boolean verboseErrors,
|
||||
boolean printTokens) throws IOException,
|
||||
RecognitionException {
|
||||
public static boolean assembleSmaliFile(String smali, DexBuilder dexBuilder, boolean verboseErrors,
|
||||
boolean printTokens, File smaliFile) throws IOException, RuntimeException, RecognitionException {
|
||||
|
||||
CommonTokenStream tokens;
|
||||
LexerErrorInterface lexer;
|
||||
InputStream is = new ByteArrayInputStream(smali.getBytes());
|
||||
return assembleSmaliFile(is, dexBuilder, verboseErrors, printTokens, smaliFile);
|
||||
}
|
||||
|
||||
FileInputStream fis = new FileInputStream(smaliFile.getAbsolutePath());
|
||||
InputStreamReader reader = new InputStreamReader(fis, "UTF-8");
|
||||
public static boolean assembleSmaliFile(InputStream is,DexBuilder dexBuilder, boolean verboseErrors,
|
||||
boolean printTokens, File smaliFile) throws IOException, RecognitionException {
|
||||
|
||||
lexer = new smaliFlexLexer(reader);
|
||||
((smaliFlexLexer)lexer).setSourceFile(smaliFile);
|
||||
tokens = new CommonTokenStream((TokenSource) lexer);
|
||||
// copy our filestream into a tmp file, so we don't overwrite
|
||||
File tmp = File.createTempFile("BRUT",".bak");
|
||||
tmp.deleteOnExit();
|
||||
|
||||
if (printTokens) {
|
||||
tokens.getTokens();
|
||||
OutputStream os = new FileOutputStream(tmp);
|
||||
IOUtils.copy(is, os);
|
||||
os.close();
|
||||
|
||||
for (int i=0; i<tokens.size(); i++) {
|
||||
Token token = tokens.get(i);
|
||||
if (token.getChannel() == smaliParser.HIDDEN) {
|
||||
continue;
|
||||
return assembleSmaliFile(tmp,dexBuilder, verboseErrors, printTokens);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user