Skip to content

Commit

Permalink
#11 DEX format - parse items - encoded_value in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
amosshi committed Aug 28, 2021
1 parent 72fac36 commit f8a0826
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 109 deletions.
2 changes: 1 addition & 1 deletion BinaryInternalsViewer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>freeinternals.format.dex</artifactId>
<version>0.2</version>
<version>0.7</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down
2 changes: 1 addition & 1 deletion FormatDEX/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<version>3.0</version>
</parent>
<artifactId>freeinternals.format.dex</artifactId>
<version>0.2</version>
<version>0.7</version>
<packaging>jar</packaging>

<name>freeinternals :: FormatDEX</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,30 @@ public Type_uleb128p1 Dex_uleb128p1() throws IOException, FileFormatException {
return new Type_uleb128p1(uleb128.value - 1, uleb128.length);
}

public Double readDouble(int length) throws IOException{
System.out.println(this.getClass().getSimpleName() + " VALUE_DOUBLE value_arg " + (length - 1) + " at 0x" + Integer.toHexString(this.getPos()) + " - to implment");

byte[] raw = new byte[length];
int rb = this.read(raw);
if (rb != length) {
throw new IOException(String.format("Cannot read enough bytes for double. expected=%d readbytes=%d", length, rb));
}

return Double.MAX_VALUE;
}

public Float readFloat(int length) throws IOException{
System.out.println(this.getClass().getSimpleName() + " VALUE_FLOAT value_arg " + (length -1) + " at 0x" + Integer.toHexString(this.getPos()) + " - to implment");

byte[] raw = new byte[length];
int rb = this.read(raw);
if (rb != length) {
throw new IOException(String.format("Cannot read enough bytes for float. expected=%d readbytes=%d", length, rb));
}

return Float.MIN_VALUE;
}

/**
* Read 3-byte int.
*/
Expand Down Expand Up @@ -329,90 +353,141 @@ private int readInt3InLittleEndian() throws IOException {
}

private long readLong5() throws IOException {
final byte[] readBuffer = new byte[BYTE_LENGTH_8];
super.readFully(readBuffer, 3, BYTE_LENGTH_5);

// TODO
return (((long) readBuffer[BYTE_OFFSET_7] << SHIFT_56)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
final byte[] readBuffer = new byte[BYTE_LENGTH_5];
super.readFully(readBuffer);

if ((readBuffer[BYTE_OFFSET_4] & 0x80) > 0) {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong5 NEGATIVE----");
return (0xFFFFFF0000000000L
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255)));
} else {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong5 POSITIVE----");
return ( ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255)));
}
}

private long readLong5InLittleEndian() throws IOException {
final byte[] readBuffer = new byte[BYTE_LENGTH_8];
super.readFully(readBuffer, 3, BYTE_LENGTH_5);

// TODO
return (((long) readBuffer[BYTE_OFFSET_7] << SHIFT_56)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
final byte[] readBuffer = new byte[BYTE_LENGTH_5];
super.readFully(readBuffer);

if ((readBuffer[BYTE_OFFSET_4] & 0x80) > 0) {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong5InLittleEndian NEGATIVE----");
return (0xFFFFFF0000000000L
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
} else {
return ( ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
}
}

private long readLong6() throws IOException {
final byte[] readBuffer = new byte[BYTE_LENGTH_8];
super.readFully(readBuffer, 2, BYTE_LENGTH_6);

// TODO
return (((long) readBuffer[BYTE_OFFSET_7] << SHIFT_56)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
final byte[] readBuffer = new byte[BYTE_LENGTH_6];
super.readFully(readBuffer);

if ((readBuffer[BYTE_OFFSET_5] & 0x80) > 0) {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong6 NEGATIVE----");
return (0xFFFF000000000000L
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255)));
} else {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong6 POSITIVE----");
return ( ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255)));
}
}
private long readLong6InLittleEndian() throws IOException {
final byte[] readBuffer = new byte[BYTE_LENGTH_8];
super.readFully(readBuffer, 2, BYTE_LENGTH_6);

// TODO
return (((long) readBuffer[BYTE_OFFSET_7] << SHIFT_56)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
final byte[] readBuffer = new byte[BYTE_LENGTH_6];
super.readFully(readBuffer);

if ((readBuffer[BYTE_OFFSET_5] & 0x80) > 0) {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong6InLittleEndian NEGATIVE----");
return (0xFFFF000000000000L
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
} else {
return ( ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
}
}

private long readLong7() throws IOException {
final byte[] readBuffer = new byte[BYTE_LENGTH_8];
super.readFully(readBuffer, 1, BYTE_LENGTH_7);

// TODO
return (((long) readBuffer[BYTE_OFFSET_7] << SHIFT_56)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
final byte[] readBuffer = new byte[BYTE_LENGTH_7];
super.readFully(readBuffer);

if ((readBuffer[BYTE_OFFSET_6] & 0x80) > 0) {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong7 NEGATIVE----");
return (0xFF00000000000000L
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255)));
} else {
System.out.println("TODO test case at 0x" + Integer.toHexString(this.getPos()) + " ------------------- to verify ---- readLong7 POSITIVE----");
return ( ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255)));
}
}
private long readLong7InLittleEndian() throws IOException {
final byte[] readBuffer = new byte[BYTE_LENGTH_8];
super.readFully(readBuffer, 1, BYTE_LENGTH_7);

// TODO
return (((long) readBuffer[BYTE_OFFSET_7] << SHIFT_56)
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
final byte[] readBuffer = new byte[BYTE_LENGTH_7];
super.readFully(readBuffer);

if ((readBuffer[BYTE_OFFSET_6] & 0x80) > 0) {
return (0xFF00000000000000L
| ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
} else {
return ( ((long) (readBuffer[BYTE_OFFSET_6] & BYTE_MAX_255) << SHIFT_48)
| ((long) (readBuffer[BYTE_OFFSET_5] & BYTE_MAX_255) << SHIFT_40)
| ((long) (readBuffer[BYTE_OFFSET_4] & BYTE_MAX_255) << SHIFT_32)
| ((long) (readBuffer[BYTE_OFFSET_3] & BYTE_MAX_255) << SHIFT_24)
| ((long) (readBuffer[BYTE_OFFSET_2] & BYTE_MAX_255) << SHIFT_16)
| ((long) (readBuffer[BYTE_OFFSET_1] & BYTE_MAX_255) << SHIFT_8)
| ((long) (readBuffer[BYTE_OFFSET_0] & BYTE_MAX_255)));
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public class encoded_value extends FileComponent implements GenerateTreeNodeDexF
this.union_value_long = this.read_long(stream, this.value_arg);
break;
case VALUE_FLOAT:
this.union_value_float = this.read_float(stream, this.value_arg);
this.union_value_float = stream.readFloat(this.value_arg + 1);
break;
case VALUE_DOUBLE:
this.union_value_double = this.read_double(stream, this.value_arg);
this.union_value_double = stream.readDouble(this.value_arg + 1);
break;
case VALUE_METHOD_TYPE:
this.union_value_method_type = this.read_uint(stream, this.value_arg);
Expand Down Expand Up @@ -231,7 +231,6 @@ private Type_long read_long(final PosDataInputStreamDex stream, final int arg) t
case 4:
case 5:
case 6:
System.out.println(this.getClass().getSimpleName() + " : read_long for bytes 5/6/7 at 0x" + Integer.toHexString(stream.getPos()).toUpperCase() + " ======= to verify =======");
result = stream.Dex_long(arg + 1);
break;

Expand All @@ -250,38 +249,6 @@ private Type_long read_long(final PosDataInputStreamDex stream, final int arg) t
return result;
}

private Float read_float(final PosDataInputStreamDex stream, final int arg) throws IOException{
Float result = Float.MIN_VALUE; // TODO convert the bytes to float

if (arg != 3) {
System.out.println(this.getClass().getSimpleName() + " VALUE_FLOAT value_arg is not 3 but " + this.value_arg + " at 0x" + Integer.toHexString(stream.getPos()) + " - to implment");
}

byte[] raw = new byte[arg + 1];
int rb = stream.read(raw);
if (rb != arg + 1) {
throw new IOException(String.format("Cannot read enough bytes for float. expected=%d readbytes=%d", arg + 1, rb));
}

return result;
}

private Double read_double(final PosDataInputStreamDex stream, final int arg) throws IOException{
Double result = Double.MAX_VALUE; // TODO convert the bytes to double

if (arg != 7) {
System.out.println(this.getClass().getSimpleName() + " VALUE_DOUBLE value_arg is not 7 but " + this.value_arg + " at 0x" + Integer.toHexString(stream.getPos()) + " - to implment");
}

byte[] raw = new byte[arg + 1];
int rb = stream.read(raw);
if (rb != arg + 1) {
throw new IOException(String.format("Cannot read enough bytes for double. expected=%d readbytes=%d", arg + 1, rb));
}

return result;
}

private Type_short read_short(final PosDataInputStreamDex stream, final int arg) throws FileFormatException, IOException{
Type_short result = null;
switch (arg) {
Expand Down

0 comments on commit f8a0826

Please sign in to comment.