Skip to content

Commit

Permalink
Dex version 37 support
Browse files Browse the repository at this point in the history
Make dexlib able to read and write version 37 dex files.

Bug: 27809626

Change-Id: I3d0ca6201c7abe7763d2fb925e9ee1edbef24230
  • Loading branch information
allight committed Apr 4, 2016
1 parent 87d10da commit 40bbf5c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
24 changes: 22 additions & 2 deletions dexlib2/src/main/java/org/jf/dexlib2/dexbacked/raw/HeaderItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,14 @@
public class HeaderItem {
public static final int ITEM_SIZE = 0x70;

/**
* The magic numbers for dex files.
*
* They are: "dex\n035\0" and "dex\n037\0".
*/
public static final byte[][] MAGIC_VALUES= new byte[][] {
new byte[]{0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00},
new byte[]{0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x36, 0x00}};
new byte[]{0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x37, 0x00}};

public static final int LITTLE_ENDIAN_TAG = 0x12345678;
public static final int BIG_ENDIAN_TAG = 0x78563412;
Expand Down Expand Up @@ -225,6 +230,21 @@ private static String getEndianText(int endianTag) {
return "Invalid";
}


/**
* Get the higest magic number supported by Android for this api level.
* @return The dex file magic number
*/
public static byte[] getMagicForApi(int api) {
if (api < 24) {
// Prior to Android N we only support dex version 035.
return HeaderItem.MAGIC_VALUES[0];
} else {
// On android N and later we support dex version 037.
return HeaderItem.MAGIC_VALUES[1];
}
}

private static int getVersion(byte[] buf, int offset) {
if (buf.length - offset < 8) {
return 0;
Expand All @@ -241,7 +261,7 @@ private static int getVersion(byte[] buf, int offset) {
}
}
if (matches) {
return i==0?35:36;
return i==0?35:37;
}
}
return 0;
Expand Down
4 changes: 2 additions & 2 deletions dexlib2/src/main/java/org/jf/dexlib2/writer/DexWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1221,8 +1221,8 @@ private void writeMapItem(@Nonnull DexDataWriter writer, int type, int size, int
}

private void writeHeader(@Nonnull DexDataWriter writer, int dataOffset, int fileSize) throws IOException {
// always write the 035 version, there's no reason to use the 036 version for now
writer.write(HeaderItem.MAGIC_VALUES[0]);
// Write the appropriate header.
writer.write(HeaderItem.getMagicForApi(opcodes.api));

// checksum placeholder
writer.writeInt(0);
Expand Down

0 comments on commit 40bbf5c

Please sign in to comment.