@Nonnull public static DexBackedDexFile fromInputStream(@Nullable Opcodes opcodes, @Nonnull InputStream is) throws IOException { DexUtil.verifyDexHeader(is); byte[] buf = ByteStreams.toByteArray(is); return new DexBackedDexFile(opcodes, buf, 0, false); }
protected DexBackedDexFile(@Nullable Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) { super(buf, offset); int dexVersion; if (verifyMagic) { dexVersion = DexUtil.verifyDexHeader(buf, offset); } else { dexVersion = HeaderItem.getVersion(buf, offset); } if (opcodes == null) { this.opcodes = Opcodes.forDexVersion(dexVersion); } else { this.opcodes = opcodes; } 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); mapOffset = readSmallUint(HeaderItem.MAP_OFFSET); }
protected boolean isDex(@Nonnull ZipFile zipFile, @Nonnull ZipEntry zipEntry) throws IOException { InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); try { DexUtil.verifyDexHeader(inputStream); } catch (NotADexFile ex) { return false; } catch (InvalidFile ex) { return false; } catch (UnsupportedFile ex) { return false; } finally { inputStream.close(); } return true; }
@Nonnull public static DexBackedDexFile fromInputStream(@Nullable Opcodes opcodes, @Nonnull InputStream is) throws IOException { DexUtil.verifyDexHeader(is); byte[] buf = ByteStreams.toByteArray(is); return new DexBackedDexFile(opcodes, buf, 0, false); }
protected boolean isDex(@Nonnull ZipFile zipFile, @Nonnull ZipEntry zipEntry) throws IOException { InputStream inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry)); try { DexUtil.verifyDexHeader(inputStream); } catch (NotADexFile ex) { return false; } catch (InvalidFile ex) { return false; } catch (UnsupportedFile ex) { return false; } finally { inputStream.close(); } return true; }
/** * Reads in the dex header from the given input stream and verifies that it is valid and a supported version * * The inputStream must support mark(), and will be reset to initial position upon exiting the method * * @param inputStream An input stream that is positioned at a dex header * @return The dex version * @throws NotADexFile If the file is not a dex file * @throws InvalidFile If the header appears to be a dex file, but is not valid for some reason * @throws UnsupportedFile If the dex header is valid, but uses unsupported functionality */ public static int verifyDexHeader(@Nonnull InputStream inputStream) throws IOException { if (!inputStream.markSupported()) { throw new IllegalArgumentException("InputStream must support mark"); } inputStream.mark(44); byte[] partialHeader = new byte[44]; try { ByteStreams.readFully(inputStream, partialHeader); } catch (EOFException ex) { throw new NotADexFile("File is too short"); } finally { inputStream.reset(); } return verifyDexHeader(partialHeader, 0); }
/** * Reads in the dex header from the given input stream and verifies that it is valid and a supported version * * The inputStream must support mark(), and will be reset to initial position upon exiting the method * * @param inputStream An input stream that is positioned at a dex header * @return The dex version * @throws NotADexFile If the file is not a dex file * @throws InvalidFile If the header appears to be a dex file, but is not valid for some reason * @throws UnsupportedFile If the dex header is valid, but uses unsupported functionality */ public static int verifyDexHeader(@Nonnull InputStream inputStream) throws IOException { if (!inputStream.markSupported()) { throw new IllegalArgumentException("InputStream must support mark"); } inputStream.mark(44); byte[] partialHeader = new byte[44]; try { ByteStreams.readFully(inputStream, partialHeader); } catch (EOFException ex) { throw new NotADexFile("File is too short"); } finally { inputStream.reset(); } return verifyDexHeader(partialHeader, 0); }
protected DexBackedDexFile(@Nullable Opcodes opcodes, @Nonnull byte[] buf, int offset, boolean verifyMagic) { super(buf, offset); int dexVersion; if (verifyMagic) { dexVersion = DexUtil.verifyDexHeader(buf, offset); } else { dexVersion = HeaderItem.getVersion(buf, offset); } if (opcodes == null) { this.opcodes = Opcodes.forDexVersion(dexVersion); } else { this.opcodes = opcodes; } 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); mapOffset = readSmallUint(HeaderItem.MAP_OFFSET); }