org.jf.dexlib2.writer.builder.DexBuilder Java Examples

The following examples show how to use org.jf.dexlib2.writer.builder.DexBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SmaliBuilder.java    From ratel with Apache License 2.0 6 votes vote down vote up
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")) {
        try {
            if (!SmaliMod.assembleSmaliFile(inFile, dexBuilder, false, false)) {
                throw new AndrolibException("Could not smali file: " + fileName);
            }
        } catch (IOException | RecognitionException ex) {
            throw new AndrolibException(ex);
        }
    } else {
        LOGGER.warning("Unknown file type, ignoring: " + inFile);
    }
    inStream.close();
}
 
Example #2
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static Set<String> buildCode(File smaliDir, File dexFile, DexDiffInfo info) throws IOException,
        RecognitionException {
    Set<String> classes = new HashSet<String>();
    Set<DexBackedClassDef> classDefs = new HashSet<DexBackedClassDef>();
    classDefs.addAll(info.getModifiedClasses());
    classDefs.addAll(info.getAddedClasses());
    final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
    DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
    File smaliFile;
    String className;
    for (DexBackedClassDef classDef : classDefs) {
        ApkPatch.currentClassType = classDef.getType();
        className = TypeGenUtil.newType(classDef.getType());
        AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classDefs, 19), false, false);
        smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
        classes.add(className.substring(1, className.length() - 1).replace('/', '.'));
        SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true);
    }

    dexBuilder.writeTo(new FileDataStore(dexFile));

    return classes;
}
 
Example #3
Source File: SmaliMod.java    From ratel with Apache License 2.0 5 votes vote down vote up
public static boolean assembleSmaliFile(InputStream is,DexBuilder dexBuilder, boolean verboseErrors,
                                        boolean printTokens, File smaliFile) throws IOException, RecognitionException {

    // copy our filestream into a tmp file, so we don't overwrite
    File tmp = File.createTempFile("BRUT",".bak");
    tmp.deleteOnExit();

    OutputStream os = new FileOutputStream(tmp);
    IOUtils.copy(is, os);
    os.close();

    return assembleSmaliFile(tmp,dexBuilder, verboseErrors, printTokens);
}
 
Example #4
Source File: SmaliMod.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static boolean assembleSmaliFile(InputStream is,DexBuilder dexBuilder, boolean verboseErrors,
                                        boolean printTokens, File smaliFile) throws IOException, RecognitionException {

    // copy our filestream into a tmp file, so we don't overwrite
    File tmp = File.createTempFile("BRUT",".bak");
    tmp.deleteOnExit();

    OutputStream os = new FileOutputStream(tmp);
    IOUtils.copy(is, os);
    os.close();

    return assembleSmaliFile(tmp,dexBuilder, verboseErrors, printTokens);
}
 
Example #5
Source File: SmaliUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
/**
 * 将smali文件夹转换为dex文件
 * @param smaliFolder
 * @param outDexFile
 * @return
 */
public static boolean assembleSmaliFile(File smaliFolder,File outDexFile) throws IOException, RecognitionException {
    Collection<File> smaliFiles =  FileUtils.listFiles(smaliFolder, new String[]{"smali"}, true);
    if(null!= smaliFiles && smaliFiles.size() > 0){
        DexBuilder dexBuilder = new DexBuilder(Opcodes.getDefault());
        for(File smaliFile:smaliFiles){
            SmaliMod.assembleSmaliFile(smaliFile, dexBuilder, true, true);
        }
        dexBuilder.writeTo(new FileDataStore(outDexFile));
        return true;
    }else{
        return false;
    }
}
 
Example #6
Source File: StmtVisitor.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public StmtVisitor(SootMethod belongingMethod, DexBuilder belongingFile) {
	this.belongingMethod = belongingMethod;
	this.belongingFile = belongingFile;
	constantV = new ConstantVisitor(belongingFile, this);
	regAlloc = new RegisterAllocator();
	exprV = new ExprVisitor(this, constantV, regAlloc, belongingFile);
	insns = new ArrayList<Insn>();
	switchPayloads = new ArrayList<SwitchPayload>();
}
 
Example #7
Source File: ExprVisitor.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
public ExprVisitor(StmtVisitor stmtV, ConstantVisitor constantV,
		RegisterAllocator regAlloc, DexBuilder dexFile) {
	this.dexFile = dexFile;
	this.stmtV = stmtV;
	this.constantV = constantV;
	this.regAlloc = regAlloc;

	regAlloc.resetImmediateConstantsPool();
}
 
Example #8
Source File: DexPrinter.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
protected static BuilderFieldReference toFieldReference
   		(SootField f, DexBuilder belongingDexFile) {
   	FieldReference fieldRef = new ImmutableFieldReference
   			(SootToDexUtils.getDexClassName(f.getDeclaringClass().getName()),
   			f.getName(),
   			SootToDexUtils.getDexTypeDescriptor(f.getType()));
   	return belongingDexFile.internFieldReference(fieldRef);
}
 
Example #9
Source File: DexPrinter.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
protected static BuilderMethodReference toMethodReference
(SootMethodRef m, DexBuilder belongingDexFile) {
 	List<String> parameters = new ArrayList<String>();
 	for (Type t : m.parameterTypes())
 		parameters.add(SootToDexUtils.getDexTypeDescriptor(t));
 	MethodReference methodRef = new ImmutableMethodReference
 			(SootToDexUtils.getDexClassName(m.declaringClass().getName()),
 			m.name(),
 			parameters,
 			SootToDexUtils.getDexTypeDescriptor(m.returnType()));
 	return belongingDexFile.internMethodReference(methodRef);
 }
 
Example #10
Source File: SmaliMod.java    From ratel with Apache License 2.0 4 votes vote down vote up
public static boolean assembleSmaliFile(String smali, DexBuilder dexBuilder, boolean verboseErrors,
                                        boolean printTokens, File smaliFile) throws IOException, RuntimeException, RecognitionException {

    InputStream is = new ByteArrayInputStream(smali.getBytes());
    return assembleSmaliFile(is, dexBuilder, verboseErrors, printTokens, smaliFile);
}
 
Example #11
Source File: SmaliMod.java    From ratel with Apache License 2.0 4 votes vote down vote up
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) {
        is.close();
        reader.close();
        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();

    is.close();
    reader.close();

    return dexGen.getNumberOfSyntaxErrors() == 0;
}
 
Example #12
Source File: SmaliMod.java    From atlas with Apache License 2.0 4 votes vote down vote up
public static boolean assembleSmaliFile(String smali, DexBuilder dexBuilder, boolean verboseErrors,
                                        boolean printTokens, File smaliFile) throws IOException, RuntimeException, RecognitionException {

    InputStream is = new ByteArrayInputStream(smali.getBytes());
    return assembleSmaliFile(is, dexBuilder, verboseErrors, printTokens, smaliFile);
}
 
Example #13
Source File: StmtVisitor.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
protected DexBuilder getBelongingFile() {
	return belongingFile;
}
 
Example #14
Source File: DexPrinter.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public DexPrinter() {
	dexFile = DexBuilder.makeDexBuilder(19);
	//dexAnnotation = new DexAnnotation(dexFile);
}
 
Example #15
Source File: DexPrinter.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
protected static BuilderTypeReference toTypeReference
(Type t, DexBuilder belongingDexFile) {
 	return belongingDexFile.internTypeReference
 			(SootToDexUtils.getDexTypeDescriptor(t));
 }
 
Example #16
Source File: ConstantVisitor.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
public ConstantVisitor(DexBuilder dexFile, StmtVisitor stmtV) {
	this.stmtV = stmtV;
	this.dexFile = dexFile;
}