Improve handling of the dex and odex magic values

git-svn-id: https://smali.googlecode.com/svn/trunk@469 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com
2009-09-13 19:57:25 +00:00
parent 343848c849
commit 4080fe659d
3 changed files with 13 additions and 31 deletions

View File

@ -277,12 +277,8 @@ public class DexFile
byte[] magic = FileUtils.readFile(file, 0, 8);
byte[] dexMagic, odexMagic;
try {
dexMagic = HeaderItem.MAGIC.getBytes("US-ASCII");
odexMagic = OdexHeaderItem.MAGIC.getBytes("US-ASCII");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
dexMagic = HeaderItem.MAGIC;
odexMagic = OdexHeaderItem.MAGIC;
boolean isDex = true;
this.isOdex = true;
@ -306,6 +302,7 @@ public class DexFile
} else if (isDex) {
in = new ByteArrayInput(FileUtils.readFile(file));
} else {
StringBuilder sb = new StringBuilder();
sb.append("bad magic value:");
for (int i=0; i<8; i++) {

View File

@ -39,7 +39,8 @@ public class HeaderItem extends Item<HeaderItem> {
* the file format magic number, represented as the
* low-order bytes of a string
*/
public static final String MAGIC = "dex\n035" + '\0';
public static final byte[] MAGIC = new byte[] {0x64, 0x65, 0x78, 0x0A, 0x30, 0x33, 0x35, 0x00};//"dex\n035" + '\0';
/** size of this section, in bytes */
private static final int HEADER_SIZE = 0x70;
@ -58,17 +59,10 @@ public class HeaderItem extends Item<HeaderItem> {
/** {@inheritDoc} */
protected void readItem(Input in, ReadContext readContext) {
byte[] expectedMagic;
try {
expectedMagic = MAGIC.getBytes("US-ASCII");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
byte[] readMagic = in.readBytes(8);
for (int i=0; i<8; i++) {
if (expectedMagic[i] != readMagic[i]) {
if (MAGIC[i] != readMagic[i]) {
throw new RuntimeException("The magic value is not the expected value");
}
}
@ -145,15 +139,13 @@ public class HeaderItem extends Item<HeaderItem> {
/** {@inheritDoc} */
protected void writeItem(AnnotatedOutput out) {
byte[] magic;
try {
magic = MAGIC.getBytes("US-ASCII");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
StringBuilder magicBuilder = new StringBuilder();
for (int i=0; i<8; i++) {
magicBuilder.append((char)MAGIC[i]);
}
out.annotate("magic: " + Utf8Utils.escapeString(MAGIC));
out.write(magic);
out.annotate("magic: " + Utf8Utils.escapeString(magicBuilder.toString()));
out.write(MAGIC);
out.annotate("checksum");
out.writeInt(0);

View File

@ -38,7 +38,7 @@ public class OdexHeaderItem {
* the file format magic number, represented as the
* low-order bytes of a string
*/
public static final String MAGIC = "dey\n035" + '\0';
public static final byte[] MAGIC = new byte[] {0x64, 0x65, 0x79, 0x0A, 0x30, 0x33, 0x35, 0x00};//"dey\n035" + '\0';
public final byte[] magic;
public final int dexOffset;
@ -52,15 +52,8 @@ public class OdexHeaderItem {
public OdexHeaderItem(Input in) {
magic = in.readBytes(8);
byte[] expectedMagic;
try {
expectedMagic = MAGIC.getBytes("US-ASCII");
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException(ex);
}
for (int i=0; i<8; i++) {
if (expectedMagic[i] != magic[i]) {
if (MAGIC[i] != magic[i]) {
throw new RuntimeException("The magic value is not the expected value");
}
}