org.jf.dexlib2.Opcodes Java Examples

The following examples show how to use org.jf.dexlib2.Opcodes. 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: DexBackedDexFile.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagicAndByteOrder(partialHeader, 0);

    byte[] buf = ByteStreams.toByteArray(is);
    return new DexBackedDexFile(opcodes, buf, 0, false);
}
 
Example #2
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static BaksmaliOptions getBuildOption(Iterable<? extends ClassDef> collection, int apiLevel) {
    BaksmaliOptions options = new BaksmaliOptions();

    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = true;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = apiLevel;
    if (!options.accessorComments) {
        options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(),collection);
    }

    return options;
}
 
Example #3
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
public static Set<DexBackedClassDef> scanClasses(File smaliDir, List<File> newFiles) throws PatchException {

        Set<DexBackedClassDef> classes = Sets.newHashSet();
        try {
            for (File newFile : newFiles) {
                DexBackedDexFile newDexFile = DexFileFactory.loadDexFile(newFile, Opcodes.getDefault());
                Set<? extends DexBackedClassDef> dexClasses = newDexFile.getClasses();
                classes.addAll(dexClasses);
            }

            final ClassFileNameHandler outFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");
            final ClassFileNameHandler inFileNameHandler = new ClassFileNameHandler(smaliDir, ".smali");

            for (DexBackedClassDef classDef : classes) {
                String className = classDef.getType();
                ApkPatch.currentClassType = null;
                AfBakSmali.disassembleClass(classDef, outFileNameHandler, getBuildOption(classes, 19), true, true);
                File smaliFile = inFileNameHandler.getUniqueFilenameForClass(className);
            }
        } catch (Exception e) {
            throw new PatchException(e);
        }
        return classes;
    }
 
Example #4
Source File: DexBackedDexFile.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private DexBackedDexFile(Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) {
    super(buf);
    this.type = DexFileDataType.FILETYPE;
    this.opcodes = opcodes;

    if (verifyMagic) {
        verifyMagicAndByteOrder(buf, offset);
    }

    stringCount = readSmallUint(HeaderItem.STRING_COUNT_OFFSET);
    stringStartOffset = readSmallUint(HeaderItem.STRING_START_OFFSET);
    typeCount = readSmallUint(HeaderItem.TYPE_COUNT_OFFSET);
    typeStartOffset = readSmallUint(HeaderItem.TYPE_START_OFFSET);
    protoCount = readSmallUint(HeaderItem.PROTO_COUNT_OFFSET);
    protoStartOffset = readSmallUint(HeaderItem.PROTO_START_OFFSET);
    fieldCount = readSmallUint(HeaderItem.FIELD_COUNT_OFFSET);
    fieldStartOffset = readSmallUint(HeaderItem.FIELD_START_OFFSET);
    methodCount = readSmallUint(HeaderItem.METHOD_COUNT_OFFSET);
    methodStartOffset = readSmallUint(HeaderItem.METHOD_START_OFFSET);
    classCount = readSmallUint(HeaderItem.CLASS_COUNT_OFFSET);
    classStartOffset = readSmallUint(HeaderItem.CLASS_START_OFFSET);
}
 
Example #5
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 #6
Source File: SmaliCodeUtils.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * 生成解析成smali代码的选项
 *
 * @return
 */
private static BaksmaliOptions createBaksmaliOptions(ClassDef classDef) {
    BaksmaliOptions options = new BaksmaliOptions();
    options.deodex = false;
    options.parameterRegisters = false;
    options.localsDirective = true;
    options.sequentialLabels = true;
    options.debugInfo = false;
    options.codeOffsets = false;
    options.accessorComments = false;
    options.registerInfo = 0;// 128
    options.inlineResolver = null;
    options.apiLevel = DEFAULT_API_LEVEL;
    List<ClassDef> classDefs = Lists.newArrayList();
    classDefs.add(classDef);
    options.syntheticAccessorResolver = new SyntheticAccessorResolver(Opcodes.getDefault(),classDefs);
    return options;
}
 
Example #7
Source File: DexDiffer.java    From atlas with Apache License 2.0 6 votes vote down vote up
/**
 * scan base Dex,get base Dex info
 */
private void scanBaseDexFile() throws IOException {
    for (File baseDexFile : baseDexFiles) {
        String s = baseDexFile.getName().substring(7);
        String dexNum = s.startsWith(".")? "0":s.substring(0,s.indexOf("."));
        DexBackedDexFile baseBackedDexFile = DexFileFactory.loadDexFile(baseDexFile, Opcodes.getDefault());
        final Set<? extends DexBackedClassDef> baseClassDefs = baseBackedDexFile.getClasses();
        baseClassDefs.forEach(new Consumer<DexBackedClassDef>() {
            @Override
            public void accept(DexBackedClassDef dexBackedClassDef) {
                if (dexBackedClassDef.getType().equals("Lcom/ali/mobisecenhance/ReflectMap;")){
                    noPatchDexNum = dexNum;
                }
            }
        });
        dexDiffInfo.setOldClasses(dexNum,baseClassDefs);
        for (DexBackedClassDef baseClassDef : baseClassDefs) {
            String className = getDalvikClassName(baseClassDef.getType());
            baseClassDefMap.put(className, baseClassDef);
        }
    }
}
 
Example #8
Source File: DexBackedDexFile.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
private DexBackedDexFile(Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) {
    super(buf);
    this.type = DexFileDataType.FILETYPE;
    this.opcodes = opcodes;

    if (verifyMagic) {
        verifyMagicAndByteOrder(buf, offset);
    }

    stringCount = readSmallUint(HeaderItem.STRING_COUNT_OFFSET);
    stringStartOffset = readSmallUint(HeaderItem.STRING_START_OFFSET);
    typeCount = readSmallUint(HeaderItem.TYPE_COUNT_OFFSET);
    typeStartOffset = readSmallUint(HeaderItem.TYPE_START_OFFSET);
    protoCount = readSmallUint(HeaderItem.PROTO_COUNT_OFFSET);
    protoStartOffset = readSmallUint(HeaderItem.PROTO_START_OFFSET);
    fieldCount = readSmallUint(HeaderItem.FIELD_COUNT_OFFSET);
    fieldStartOffset = readSmallUint(HeaderItem.FIELD_START_OFFSET);
    methodCount = readSmallUint(HeaderItem.METHOD_COUNT_OFFSET);
    methodStartOffset = readSmallUint(HeaderItem.METHOD_START_OFFSET);
    classCount = readSmallUint(HeaderItem.CLASS_COUNT_OFFSET);
    classStartOffset = readSmallUint(HeaderItem.CLASS_START_OFFSET);
}
 
Example #9
Source File: DexBackedDexFile.java    From zjdroid with Apache License 2.0 6 votes vote down vote up
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagicAndByteOrder(partialHeader, 0);

    byte[] buf = ByteStreams.toByteArray(is);
    return new DexBackedDexFile(opcodes, buf, 0, false);
}
 
Example #10
Source File: DexBackedDexFile.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagicAndByteOrder(partialHeader, 0);

    byte[] buf = ByteStreams.toByteArray(is);
    return new DexBackedDexFile(opcodes, buf, 0, false);
}
 
Example #11
Source File: DexBackedDexFile.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
public static DexBackedDexFile fromInputStream(@Nonnull Opcodes opcodes, @Nonnull InputStream is)
        throws IOException {
    if (!is.markSupported()) {
        throw new IllegalArgumentException("InputStream must support mark");
    }
    is.mark(44);
    byte[] partialHeader = new byte[44];
    try {
        ByteStreams.readFully(is, partialHeader);
    } catch (EOFException ex) {
        throw new NotADexFile("File is too short");
    } finally {
        is.reset();
    }

    verifyMagicAndByteOrder(partialHeader, 0);

    byte[] buf = ByteStreams.toByteArray(is);
    return new DexBackedDexFile(opcodes, buf, 0, false);
}
 
Example #12
Source File: ApkSmaliDecoder.java    From apk-dependency-graph with Apache License 2.0 6 votes vote down vote up
private DexBackedDexFile loadDexFile(
        File apkFile, String dexFilePath, int apiVersion)
        throws IOException {
    DexBackedDexFile dexFile = DexFileFactory.loadDexEntry(
        apkFile, dexFilePath, true, Opcodes.forApi(apiVersion));

    if (dexFile == null || dexFile.isOdexFile()) {
        throw new IOException(WARNING_DISASSEMBLING_ODEX_FILE);
    }

    return dexFile;
}
 
Example #13
Source File: DexBackedDexFile.java    From ZjDroid with Apache License 2.0 6 votes vote down vote up
private DexBackedDexFile(Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) {
    super(buf);
    this.type = DexFileDataType.FILETYPE;
    this.opcodes = opcodes;

    if (verifyMagic) {
        verifyMagicAndByteOrder(buf, offset);
    }

    stringCount = readSmallUint(HeaderItem.STRING_COUNT_OFFSET);
    stringStartOffset = readSmallUint(HeaderItem.STRING_START_OFFSET);
    typeCount = readSmallUint(HeaderItem.TYPE_COUNT_OFFSET);
    typeStartOffset = readSmallUint(HeaderItem.TYPE_START_OFFSET);
    protoCount = readSmallUint(HeaderItem.PROTO_COUNT_OFFSET);
    protoStartOffset = readSmallUint(HeaderItem.PROTO_START_OFFSET);
    fieldCount = readSmallUint(HeaderItem.FIELD_COUNT_OFFSET);
    fieldStartOffset = readSmallUint(HeaderItem.FIELD_START_OFFSET);
    methodCount = readSmallUint(HeaderItem.METHOD_COUNT_OFFSET);
    methodStartOffset = readSmallUint(HeaderItem.METHOD_START_OFFSET);
    classCount = readSmallUint(HeaderItem.CLASS_COUNT_OFFSET);
    classStartOffset = readSmallUint(HeaderItem.CLASS_START_OFFSET);
}
 
Example #14
Source File: DexBackedDexFile.java    From HeyGirl with Apache License 2.0 6 votes vote down vote up
private DexBackedDexFile(Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) {
    super(buf);
    this.type = DexFileDataType.FILETYPE;
    this.opcodes = opcodes;

    if (verifyMagic) {
        verifyMagicAndByteOrder(buf, offset);
    }

    stringCount = readSmallUint(HeaderItem.STRING_COUNT_OFFSET);
    stringStartOffset = readSmallUint(HeaderItem.STRING_START_OFFSET);
    typeCount = readSmallUint(HeaderItem.TYPE_COUNT_OFFSET);
    typeStartOffset = readSmallUint(HeaderItem.TYPE_START_OFFSET);
    protoCount = readSmallUint(HeaderItem.PROTO_COUNT_OFFSET);
    protoStartOffset = readSmallUint(HeaderItem.PROTO_START_OFFSET);
    fieldCount = readSmallUint(HeaderItem.FIELD_COUNT_OFFSET);
    fieldStartOffset = readSmallUint(HeaderItem.FIELD_START_OFFSET);
    methodCount = readSmallUint(HeaderItem.METHOD_COUNT_OFFSET);
    methodStartOffset = readSmallUint(HeaderItem.METHOD_START_OFFSET);
    classCount = readSmallUint(HeaderItem.CLASS_COUNT_OFFSET);
    classStartOffset = readSmallUint(HeaderItem.CLASS_START_OFFSET);
}
 
Example #15
Source File: DexFile.java    From apkfile with Apache License 2.0 5 votes vote down vote up
DexFile parse() throws IOException {
    EntropyCalculatingInputStream bis = new EntropyCalculatingInputStream(dexStream);
    dexFile = DexBackedDexFile.fromInputStream(Opcodes.forApi(TARGET_API), bis);
    entropy = bis.entropy();
    perplexity = bis.perplexity();
    size = bis.total();
    cacheLocalClasses(dexFile);

    return this;
}
 
Example #16
Source File: ClassName.java    From DexNameNormalizer with Apache License 2.0 5 votes vote down vote up
private static DexFile getDexFile(List<ClassDef> classes, int API) {

		Collections.sort(classes);

		return new DexFile() {

			@Nonnull
			@Override
			public Set<? extends ClassDef> getClasses() {
				// TODO Auto-generated method stub
				return new AbstractSet<ClassDef>() {

					@Override
					public Iterator<ClassDef> iterator() {
						// TODO Auto-generated method stub
						return classes.iterator();
					}

					@Override
					public int size() {
						// TODO Auto-generated method stub
						return classes.size();
					}

				};
			}

			@Override
			public Opcodes getOpcodes() {
				// TODO Auto-generated method stub
				return Opcodes.forApi(API);
			}
		};
	}
 
Example #17
Source File: SmaliDecoder.java    From ratel with Apache License 2.0 5 votes vote down vote up
private void decode() throws AndrolibException {
    try {
        final BaksmaliOptions options = new BaksmaliOptions();

        // options
        options.deodex = false;
        options.implicitReferences = false;
        options.parameterRegisters = true;
        options.localsDirective = true;
        options.sequentialLabels = true;
        options.debugInfo = mBakDeb;
        options.codeOffsets = false;
        options.accessorComments = false;
        options.registerInfo = 0;
        options.inlineResolver = null;

        // set jobs automatically
        int jobs = Runtime.getRuntime().availableProcessors();
        if (jobs > 6) {
            jobs = 6;
        }

        // create the dex
        DexBackedDexFile dexFile = DexFileFactory.loadDexEntry(mApkFile, mDexFile, true, Opcodes.forApi(mApi));

        if (dexFile.isOdexFile()) {
            throw new AndrolibException("Warning: You are disassembling an odex file without deodexing it.");
        }

        if (dexFile instanceof DexBackedOdexFile) {
            options.inlineResolver =
                    InlineMethodResolver.createInlineMethodResolver(((DexBackedOdexFile)dexFile).getOdexVersion());
        }

        Baksmali.disassembleDexFile(dexFile, mOutDir, jobs, options);
    } catch (IOException ex) {
        throw new AndrolibException(ex);
    }
}
 
Example #18
Source File: DexBackedOdexFile.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example #19
Source File: DexBackedOdexFile.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example #20
Source File: D8DexMergerTest.java    From bundletool with Apache License 2.0 5 votes vote down vote up
private static ImmutableSet<String> listClassesInDexFiles(Collection<Path> dexPaths)
    throws Exception {
  ImmutableSet.Builder<String> classes = ImmutableSet.builder();
  for (Path dexPath : dexPaths) {
    DexBackedDexFile dexFile = DexFileFactory.loadDexFile(dexPath.toFile(), Opcodes.getDefault());
    for (DexBackedClassDef clazz : dexFile.getClasses()) {
      classes.add(clazz.getType());
    }
  }
  return classes.build();
}
 
Example #21
Source File: SmaliDiffUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static Set<String> buildCode(File dexFile, DexDiffInfo info) throws IOException,
        RecognitionException {
    Set<String>classes = new HashSet<>();
    Set<DexBackedClassDef> classDefs = new HashSet<DexBackedClassDef>();
    classDefs.addAll(info.getModifiedClasses());
    classDefs.addAll(info.getAddedClasses());
    DexFileFactory.writeDexFile(dexFile.getAbsolutePath(), new DexFile() {
        @Nonnull
        @Override
        public Set<? extends ClassDef> getClasses() {
            return new AbstractSet<DexBackedClassDef>() {
                @Override
                public Iterator<DexBackedClassDef> iterator() {
                    return classDefs.iterator();
                }

                @Override
                public int size() {
                    return classDefs.size();
                }
            };
        }

        @Nonnull
        @Override
        public Opcodes getOpcodes() {
            return Opcodes.getDefault();
        }
    });

    for (ClassDef classDef:classDefs){
        classes.add(classDef.getType());
    }
    return classes;
}
 
Example #22
Source File: DexBackedDexFile.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public DexBackedDexFile(Opcodes opcodes,MemoryDexFileItemPointer pointer, MemoryReader reader){
	super(reader,pointer.getBaseAddr());
	this.pointer = pointer;
	this.opcodes = opcodes;
	this.reader = reader;
	this.type = DexFileDataType.MEMORYTYPE;
    stringCount = 0;
    stringStartOffset = this.pointer.getpStringIds()-this.pointer.getBaseAddr();
    typeCount = 0;
    typeStartOffset = this.pointer.getpTypeIds() - this.pointer.getBaseAddr();
    protoCount = 0;
    protoStartOffset = this.pointer.getpProtoIds() - this.pointer.getBaseAddr();
    fieldCount = 0;
    fieldStartOffset = this.pointer.getpFieldIds()-this.pointer.getBaseAddr();
    methodCount = 0;
    methodStartOffset = this.pointer.getpMethodIds()- this.pointer.getBaseAddr();
    classCount = this.pointer.getClassCount();
    classStartOffset = this.pointer.getpClassDefs() - this.pointer.getBaseAddr();
    Logger.log("the dexfile header item info start-->>>>>>>>>>");
    Logger.log("the stringStartOffset =" + stringStartOffset);
    Logger.log("the typeStartOffset =" + typeStartOffset);
    Logger.log("the protoStartOffset =" + protoStartOffset);
    Logger.log("the fieldStartOffset =" + fieldStartOffset);
    Logger.log("the methodStartOffset =" + methodStartOffset);
    Logger.log("the classStartOffset =" + classStartOffset);
    Logger.log("the classCount =" + classCount);
    Logger.log("the dexfile header item info end<<<<<<<<<<<--");
    
}
 
Example #23
Source File: DexBackedDexFile.java    From ZjDroid with Apache License 2.0 5 votes vote down vote up
public DexBackedDexFile(Opcodes opcodes,MemoryDexFileItemPointer pointer, MemoryReader reader){
	super(reader,pointer.getBaseAddr());
	this.pointer = pointer;
	this.opcodes = opcodes;
	this.reader = reader;
	this.type = DexFileDataType.MEMORYTYPE;
    stringCount = 0;
    stringStartOffset = this.pointer.getpStringIds()-this.pointer.getBaseAddr();
    typeCount = 0;
    typeStartOffset = this.pointer.getpTypeIds() - this.pointer.getBaseAddr();
    protoCount = 0;
    protoStartOffset = this.pointer.getpProtoIds() - this.pointer.getBaseAddr();
    fieldCount = 0;
    fieldStartOffset = this.pointer.getpFieldIds()-this.pointer.getBaseAddr();
    methodCount = 0;
    methodStartOffset = this.pointer.getpMethodIds()- this.pointer.getBaseAddr();
    classCount = this.pointer.getClassCount();
    classStartOffset = this.pointer.getpClassDefs() - this.pointer.getBaseAddr();
    Logger.log("the dexfile header item info start-->>>>>>>>>>");
    Logger.log("the stringStartOffset =" + stringStartOffset);
    Logger.log("the typeStartOffset =" + typeStartOffset);
    Logger.log("the protoStartOffset =" + protoStartOffset);
    Logger.log("the fieldStartOffset =" + fieldStartOffset);
    Logger.log("the methodStartOffset =" + methodStartOffset);
    Logger.log("the classStartOffset =" + classStartOffset);
    Logger.log("the classCount =" + classCount);
    Logger.log("the dexfile header item info end<<<<<<<<<<<--");
    
}
 
Example #24
Source File: PatchFieldTool.java    From atlas with Apache License 2.0 5 votes vote down vote up
public static void addField(String inFile, String outFile, DexBackedClassDef dexBackedClassDef, ImmutableField immutableField) throws IOException {

        DexFile dexFile = readDexFile(inFile);
        for (ClassDef classDef : dexFile.getClasses()) {
            if (dexBackedClassDef != null && dexBackedClassDef.getType().equals(classDef.getType())) {
                dexFile = addField(dexFile, classDef.getType(), immutableField);
            } else if (dexBackedClassDef == null) {
                dexFile = addField(dexFile, classDef.getType(), immutableField);

            }
        }
        reDexFile(dexFile);

        DexFileFactory.writeDexFile(outFile, new DexFile() {
            @Nonnull
            @Override
            public Set<? extends ClassDef> getClasses() {
                return new AbstractSet<ClassDef>() {
                    @Nonnull
                    @Override
                    public Iterator<ClassDef> iterator() {
                        return classes.iterator();
                    }

                    @Override
                    public int size() {
                        return classes.size();
                    }
                };
            }

            @Nonnull
            @Override
            public Opcodes getOpcodes() {
                return Opcodes.getDefault();
            }
        });
    }
 
Example #25
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 #26
Source File: PatchUtils.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static Map<String, ClassDef> getBundleClassDef(File file) throws IOException {
    HashMap<String, ClassDef> classDefMap = new HashMap<String, ClassDef>();
    File[] dexFiles = getDexFiles(file);
    HashSet<ClassDef> classDefs = new HashSet<ClassDef>();
    for (File dexFile : dexFiles) {
        classDefs.addAll(DexFileFactory.loadDexFile(dexFile, Opcodes.getDefault()).getClasses());
    }
    for (ClassDef classDef : classDefs) {
        classDefMap.put(classDef.getType(), classDef);
    }

    return classDefMap;
}
 
Example #27
Source File: DexBackedDexFile.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public DexBackedDexFile(Opcodes opcodes,MemoryDexFileItemPointer pointer, MemoryReader reader){
	super(reader,pointer.getBaseAddr());
	this.pointer = pointer;
	this.opcodes = opcodes;
	this.reader = reader;
	this.type = DexFileDataType.MEMORYTYPE;
    stringCount = 0;
    stringStartOffset = this.pointer.getpStringIds()-this.pointer.getBaseAddr();
    typeCount = 0;
    typeStartOffset = this.pointer.getpTypeIds() - this.pointer.getBaseAddr();
    protoCount = 0;
    protoStartOffset = this.pointer.getpProtoIds() - this.pointer.getBaseAddr();
    fieldCount = 0;
    fieldStartOffset = this.pointer.getpFieldIds()-this.pointer.getBaseAddr();
    methodCount = 0;
    methodStartOffset = this.pointer.getpMethodIds()- this.pointer.getBaseAddr();
    classCount = this.pointer.getClassCount();
    classStartOffset = this.pointer.getpClassDefs() - this.pointer.getBaseAddr();
    Logger.log("the dexfile header item info start-->>>>>>>>>>");
    Logger.log("the stringStartOffset =" + stringStartOffset);
    Logger.log("the typeStartOffset =" + typeStartOffset);
    Logger.log("the protoStartOffset =" + protoStartOffset);
    Logger.log("the fieldStartOffset =" + fieldStartOffset);
    Logger.log("the methodStartOffset =" + methodStartOffset);
    Logger.log("the classStartOffset =" + classStartOffset);
    Logger.log("the classCount =" + classCount);
    Logger.log("the dexfile header item info end<<<<<<<<<<<--");
    
}
 
Example #28
Source File: DexBackedOdexFile.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example #29
Source File: DexBackedOdexFile.java    From zjdroid with Apache License 2.0 5 votes vote down vote up
public static DexBackedOdexFile fromInputStream(@Nonnull Opcodes opcodes,
		@Nonnull InputStream is) throws IOException {
	if (!is.markSupported()) {
		throw new IllegalArgumentException("InputStream must support mark");
	}
	is.mark(8);
	byte[] partialHeader = new byte[8];
	try {
		ByteStreams.readFully(is, partialHeader);
	} catch (EOFException ex) {
		throw new NotADexFile("File is too short");
	} finally {
		is.reset();
	}

	verifyMagic(partialHeader);

	is.reset();
	byte[] odexBuf = new byte[OdexHeaderItem.ITEM_SIZE];
	ByteStreams.readFully(is, odexBuf);
	int dexOffset = OdexHeaderItem.getDexOffset(odexBuf);
	if (dexOffset > OdexHeaderItem.ITEM_SIZE) {
		ByteStreams.skipFully(is, dexOffset - OdexHeaderItem.ITEM_SIZE);
	}

	byte[] dexBuf = ByteStreams.toByteArray(is);

	return new DexBackedOdexFile(opcodes, odexBuf, dexBuf);
}
 
Example #30
Source File: DexBackedDexFile.java    From HeyGirl with Apache License 2.0 5 votes vote down vote up
public DexBackedDexFile(Opcodes opcodes,MemoryDexFileItemPointer pointer, MemoryReader reader){
	super(reader,pointer.getBaseAddr());
	this.pointer = pointer;
	this.opcodes = opcodes;
	this.reader = reader;
	this.type = DexFileDataType.MEMORYTYPE;
    stringCount = 0;
    stringStartOffset = this.pointer.getpStringIds()-this.pointer.getBaseAddr();
    typeCount = 0;
    typeStartOffset = this.pointer.getpTypeIds() - this.pointer.getBaseAddr();
    protoCount = 0;
    protoStartOffset = this.pointer.getpProtoIds() - this.pointer.getBaseAddr();
    fieldCount = 0;
    fieldStartOffset = this.pointer.getpFieldIds()-this.pointer.getBaseAddr();
    methodCount = 0;
    methodStartOffset = this.pointer.getpMethodIds()- this.pointer.getBaseAddr();
    classCount = this.pointer.getClassCount();
    classStartOffset = this.pointer.getpClassDefs() - this.pointer.getBaseAddr();
    Logger.log("the dexfile header item info start-->>>>>>>>>>");
    Logger.log("the stringStartOffset =" + stringStartOffset);
    Logger.log("the typeStartOffset =" + typeStartOffset);
    Logger.log("the protoStartOffset =" + protoStartOffset);
    Logger.log("the fieldStartOffset =" + fieldStartOffset);
    Logger.log("the methodStartOffset =" + methodStartOffset);
    Logger.log("the classStartOffset =" + classStartOffset);
    Logger.log("the classCount =" + classCount);
    Logger.log("the dexfile header item info end<<<<<<<<<<<--");
    
}