mirror of
https://github.com/revanced/smali.git
synced 2025-05-04 16:44:25 +02:00
Add alternative constructor
Add constructor that can use a bytestream so we can parse a DexFile completely in memory
This commit is contained in:
parent
a0c24f1c9f
commit
4ab8df84b0
@ -278,51 +278,72 @@ public class DexFile
|
|||||||
* @see #getPreserveSignedRegisters
|
* @see #getPreserveSignedRegisters
|
||||||
* @throws IOException if an IOException occurs
|
* @throws IOException if an IOException occurs
|
||||||
*/
|
*/
|
||||||
public DexFile(File file, boolean preserveSignedRegisters, boolean skipInstructions)
|
public DexFile(File file, boolean preserveSignedRegisters, boolean skipInstructions) throws IOException {
|
||||||
|
this(preserveSignedRegisters, skipInstructions);
|
||||||
|
InputStream dexFileInputStream = null;
|
||||||
|
ZipFile zipFile = null;
|
||||||
|
byte[] magic = FileUtils.readFile(file, 0, 8);
|
||||||
|
long fileLength;
|
||||||
|
//do we have a zip file?
|
||||||
|
if (magic[0] == 0x50 && magic[1] == 0x4B) {
|
||||||
|
zipFile = new ZipFile(file);
|
||||||
|
ZipEntry zipEntry = zipFile.getEntry("classes.dex");
|
||||||
|
if (zipEntry == null) {
|
||||||
|
throw new NoClassesDexException("zip file " + file.getName() + " does not contain a classes.dex " +
|
||||||
|
"file");
|
||||||
|
}
|
||||||
|
fileLength = zipEntry.getSize();
|
||||||
|
if (fileLength < 40) {
|
||||||
|
throw new RuntimeException("The classes.dex file in " + file.getName() + " is too small to be a" +
|
||||||
|
" valid dex file");
|
||||||
|
} else if (fileLength > Integer.MAX_VALUE) {
|
||||||
|
throw new RuntimeException("The classes.dex file in " + file.getName() + " is too large to read in");
|
||||||
|
}
|
||||||
|
dexFileInputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
fileLength = file.length();
|
||||||
|
if (fileLength < 40) {
|
||||||
|
throw new RuntimeException(file.getName() + " is too small to be a valid dex file");
|
||||||
|
}
|
||||||
|
if (fileLength < 40) {
|
||||||
|
throw new RuntimeException(file.getName() + " is too small to be a valid dex file");
|
||||||
|
} else if (fileLength > Integer.MAX_VALUE) {
|
||||||
|
throw new RuntimeException(file.getName() + " is too large to read in");
|
||||||
|
}
|
||||||
|
dexFileInputStream = new FileInputStream(file);
|
||||||
|
}
|
||||||
|
intializeHelper(dexFileInputStream,fileLength,preserveSignedRegisters,skipInstructions);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a new DexFile instance from a given input stream,
|
||||||
|
* and optionally keep track of any registers in the debug information that are signed,
|
||||||
|
* so they will be written in the same format.
|
||||||
|
* @param dexFileInputStream The dex file to read in
|
||||||
|
* @param dexFileLength length of the dex file to be read
|
||||||
|
* @param preserveSignedRegisters If true, keep track of any registers in the debug information
|
||||||
|
* that are signed, so they will be written in the same format.
|
||||||
|
* @param skipInstructions If true, skip the instructions in any code item.
|
||||||
|
* @see #getPreserveSignedRegisters
|
||||||
|
* @throws IOException if an IOException occurs
|
||||||
|
*/
|
||||||
|
public DexFile(InputStream dexFileInputStream, long dexFileLength, boolean preserveSignedRegisters, boolean skipInstructions)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
this(preserveSignedRegisters, skipInstructions);
|
this(preserveSignedRegisters, skipInstructions);
|
||||||
|
intializeHelper(dexFileInputStream, dexFileLength, preserveSignedRegisters,skipInstructions);
|
||||||
|
}
|
||||||
|
|
||||||
long fileLength;
|
private void intializeHelper(InputStream inputStream, long dexFileLength, boolean preserveSignedRegisters, boolean skipInstructions)
|
||||||
byte[] magic = FileUtils.readFile(file, 0, 8);
|
throws IOException {
|
||||||
|
|
||||||
InputStream inputStream = null;
|
|
||||||
Input in = null;
|
Input in = null;
|
||||||
ZipFile zipFile = null;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//do we have a zip file?
|
byte[] magic = new byte[8];
|
||||||
if (magic[0] == 0x50 && magic[1] == 0x4B) {
|
int magicRead = inputStream.read(magic);
|
||||||
zipFile = new ZipFile(file);
|
|
||||||
ZipEntry zipEntry = zipFile.getEntry("classes.dex");
|
|
||||||
if (zipEntry == null) {
|
|
||||||
throw new NoClassesDexException("zip file " + file.getName() + " does not contain a classes.dex " +
|
|
||||||
"file");
|
|
||||||
}
|
|
||||||
fileLength = zipEntry.getSize();
|
|
||||||
if (fileLength < 40) {
|
|
||||||
throw new RuntimeException("The classes.dex file in " + file.getName() + " is too small to be a" +
|
|
||||||
" valid dex file");
|
|
||||||
} else if (fileLength > Integer.MAX_VALUE) {
|
|
||||||
throw new RuntimeException("The classes.dex file in " + file.getName() + " is too large to read in");
|
|
||||||
}
|
|
||||||
inputStream = new BufferedInputStream(zipFile.getInputStream(zipEntry));
|
|
||||||
|
|
||||||
inputStream.mark(8);
|
if(magicRead < 8){
|
||||||
for (int i=0; i<8; i++) {
|
throw new RuntimeException("Error reading Magic bytes");
|
||||||
magic[i] = (byte)inputStream.read();
|
|
||||||
}
|
|
||||||
inputStream.reset();
|
|
||||||
} else {
|
|
||||||
fileLength = file.length();
|
|
||||||
if (fileLength < 40) {
|
|
||||||
throw new RuntimeException(file.getName() + " is too small to be a valid dex file");
|
|
||||||
}
|
|
||||||
if (fileLength < 40) {
|
|
||||||
throw new RuntimeException(file.getName() + " is too small to be a valid dex file");
|
|
||||||
} else if (fileLength > Integer.MAX_VALUE) {
|
|
||||||
throw new RuntimeException(file.getName() + " is too large to read in");
|
|
||||||
}
|
|
||||||
inputStream = new FileInputStream(file);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
byte[] dexMagic, odexMagic;
|
byte[] dexMagic, odexMagic;
|
||||||
@ -367,7 +388,7 @@ public class DexFile
|
|||||||
odexDependencies = new OdexDependencies(
|
odexDependencies = new OdexDependencies(
|
||||||
new ByteArrayInput(FileUtils.readStream(inputStream, odexHeader.depsLength)));
|
new ByteArrayInput(FileUtils.readStream(inputStream, odexHeader.depsLength)));
|
||||||
} else if (isDex) {
|
} else if (isDex) {
|
||||||
in = new ByteArrayInput(FileUtils.readStream(inputStream, (int)fileLength));
|
in = new ByteArrayInput(FileUtils.readStream(inputStream, (int)dexFileLength));
|
||||||
} else {
|
} else {
|
||||||
StringBuffer sb = new StringBuffer("bad magic value:");
|
StringBuffer sb = new StringBuffer("bad magic value:");
|
||||||
for (int i=0; i<8; i++) {
|
for (int i=0; i<8; i++) {
|
||||||
@ -380,9 +401,6 @@ public class DexFile
|
|||||||
if (inputStream != null) {
|
if (inputStream != null) {
|
||||||
inputStream.close();
|
inputStream.close();
|
||||||
}
|
}
|
||||||
if (zipFile != null) {
|
|
||||||
zipFile.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadContext readContext = new ReadContext();
|
ReadContext readContext = new ReadContext();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user