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 27, 2021
1 parent 567077c commit 72fac36
Show file tree
Hide file tree
Showing 10 changed files with 350 additions and 95 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class PosDataInputStream extends DataInputStream implements DataInputEx {

public static final byte[] EMPTY_BYTE_ARRAY = {};

/**
* Number in bytes for Java <code>byte</code> type.
*/
Expand All @@ -39,60 +39,60 @@ public class PosDataInputStream extends DataInputStream implements DataInputEx {
/**
* Shift Operators, offset with 8.
*/
private static final int SHIFT_8 = 8;
protected static final int SHIFT_8 = 8;
/**
* Shift Operators, offset with 16.
*/
private static final int SHIFT_16 = 16;
protected static final int SHIFT_16 = 16;
/**
* Shift Operators, offset with 24.
*/
private static final int SHIFT_24 = 24;
protected static final int SHIFT_24 = 24;
/**
* Shift Operators, offset with 32.
*/
private static final int SHIFT_32 = 32;
protected static final int SHIFT_32 = 32;
/**
* Shift Operators, offset with 40.
*/
private static final int SHIFT_40 = 40;
protected static final int SHIFT_40 = 40;
/**
* Shift Operators, offset with 48.
*/
private static final int SHIFT_48 = 48;
protected static final int SHIFT_48 = 48;
/**
* Shift Operators, offset with 56.
*/
private static final int SHIFT_56 = 56;
protected static final int SHIFT_56 = 56;
/**
* Half Byte length: 4.
*/
private static final int BYTE_LENGTH_4 = 4;
protected static final int BYTE_LENGTH_4 = 4;
/**
* Full Byte length: 8.
*/
private static final int BYTE_LENGTH_8 = 8;
protected static final int BYTE_LENGTH_8 = 8;

/** Byte offset 0. */
private static final int BYTE_OFFSET_0 = 0;
protected static final int BYTE_OFFSET_0 = 0;
/** Byte offset 1. */
private static final int BYTE_OFFSET_1 = 1;
protected static final int BYTE_OFFSET_1 = 1;
/** Byte offset 2. */
private static final int BYTE_OFFSET_2 = 2;
protected static final int BYTE_OFFSET_2 = 2;
/** Byte offset 3. */
private static final int BYTE_OFFSET_3 = 3;
protected static final int BYTE_OFFSET_3 = 3;
/** Byte offset 4. */
private static final int BYTE_OFFSET_4 = 4;
protected static final int BYTE_OFFSET_4 = 4;
/** Byte offset 5. */
private static final int BYTE_OFFSET_5 = 5;
protected static final int BYTE_OFFSET_5 = 5;
/** Byte offset 6. */
private static final int BYTE_OFFSET_6 = 6;
protected static final int BYTE_OFFSET_6 = 6;
/** Byte offset 7. */
private static final int BYTE_OFFSET_7 = 7;
protected static final int BYTE_OFFSET_7 = 7;
/**
* Byte max value: 255.
*/
private static final int BYTE_MAX_255 = 255;
protected static final int BYTE_MAX_255 = 255;

/**
* New line character: LINE FEED (LF).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public final class UITool {
*
* @see #left(String)
*/
public static final int TREENODE_STRING_MAXLEN = 30;
public static final int TREENODE_STRING_MAXLEN = 64;

private UITool() {
}
Expand Down
32 changes: 15 additions & 17 deletions FormatDEX/src/main/java/org/freeinternals/format/dex/DexFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
Expand Down Expand Up @@ -65,8 +62,10 @@ public final class DexFile extends FileFormat {
* monotonically over time as the format evolves.
* </p>
*/
public static final List<Byte> DEX_FILE_MAGIC1 = Collections.unmodifiableList(Arrays.asList(new Byte[]{'d', 'e', 'x', '\n'}));
public static final List<Byte> DEX_FILE_MAGIC2 = Collections.unmodifiableList(Arrays.asList(new Byte[]{'0', '3', '5', '\0'}));
@SuppressWarnings("java:S2386")
static final byte[] DEX_FILE_MAGIC1 = new byte[]{'d', 'e', 'x', '\n'};
@SuppressWarnings("java:S2386")
static final byte[] DEX_FILE_MAGIC2 = new byte[]{'0', '3', '5', '\0'};

/**
* Magic value part 1.
Expand Down Expand Up @@ -110,22 +109,21 @@ public DexFile(File file) throws IOException, FileFormatException {
super(file);

// Check the file signature
this.magic1 = new byte[DEX_FILE_MAGIC1.size()];
this.magic2 = new byte[DEX_FILE_MAGIC2.size()];
System.arraycopy(super.fileByteArray, 0, magic1, 0, DEX_FILE_MAGIC1.size());
System.arraycopy(super.fileByteArray, 4, magic2, 0, DEX_FILE_MAGIC2.size());

byte[] magic1Const = new byte[]{DEX_FILE_MAGIC1.get(0), DEX_FILE_MAGIC1.get(1), DEX_FILE_MAGIC1.get(2), DEX_FILE_MAGIC1.get(3)};
if (!BytesTool.isByteArraySame(magic1Const, magic1)
|| magic2[DEX_FILE_MAGIC2.size() - 1] != DEX_FILE_MAGIC2.get(DEX_FILE_MAGIC2.size() - 1)) {
this.magic1 = new byte[DEX_FILE_MAGIC1.length];
this.magic2 = new byte[DEX_FILE_MAGIC2.length];
System.arraycopy(super.fileByteArray, 0, magic1, 0, DEX_FILE_MAGIC1.length);
System.arraycopy(super.fileByteArray, 4, magic2, 0, DEX_FILE_MAGIC2.length);

if (!BytesTool.isByteArraySame(DEX_FILE_MAGIC1, magic1)
|| magic2[DEX_FILE_MAGIC2.length - 1] != DEX_FILE_MAGIC2[DEX_FILE_MAGIC2.length - 1]) {
throw new FileFormatException("This is not a valid DEX file, because the DEX file signature does not exist at the beginning of this file.");
}

// Parse section by section
PosDataInputStream parseEndian = new PosDataInputStream(new PosByteArrayInputStream(super.fileByteArray));

BytesTool.skip(parseEndian, DEX_FILE_MAGIC1.size());
BytesTool.skip(parseEndian, DEX_FILE_MAGIC2.size());
BytesTool.skip(parseEndian, DEX_FILE_MAGIC1.length);
BytesTool.skip(parseEndian, DEX_FILE_MAGIC2.length);
BytesTool.skip(parseEndian, Type_uint.LENGTH); // checksum
BytesTool.skip(parseEndian, 20); // signature
BytesTool.skip(parseEndian, Type_uint.LENGTH); // file_size
Expand Down Expand Up @@ -153,8 +151,8 @@ public DexFile(File file) throws IOException, FileFormatException {
SortedMap<Long, Class<?>> todoData = new TreeMap<>();

// Header
BytesTool.skip(stream, DEX_FILE_MAGIC1.size());
BytesTool.skip(stream, DEX_FILE_MAGIC2.size());
BytesTool.skip(stream, DEX_FILE_MAGIC1.length);
BytesTool.skip(stream, DEX_FILE_MAGIC2.length);
this.header = new header_item(stream);

// string_ids
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
package org.freeinternals.format.dex;

import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.logging.Logger;
import javax.swing.Icon;
import javax.swing.tree.DefaultMutableTreeNode;
import org.freeinternals.commonlib.core.FileComponent;
import org.freeinternals.commonlib.ui.GenerateTreeNode;
import org.freeinternals.commonlib.ui.JTreeNodeFileComponent;
import org.freeinternals.commonlib.ui.UITool;
Expand Down Expand Up @@ -78,22 +76,22 @@ private void generate_magic(DefaultMutableTreeNode parentNode, DexFile dexFile)

DefaultMutableTreeNode magicNode = new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPos,
DexFile.DEX_FILE_MAGIC1.size() + DexFile.DEX_FILE_MAGIC2.size(),
DexFile.DEX_FILE_MAGIC1.length + DexFile.DEX_FILE_MAGIC2.length,
"magic"));
parentNode.add(magicNode);

magicNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPos,
DexFile.DEX_FILE_MAGIC1.size(),
DexFile.DEX_FILE_MAGIC1.length,
"magic 1: " + new String(dexFile.magic1, StandardCharsets.UTF_8),
UITool.icon4Magic(),
GenerateTreeNodeDexFile.MESSAGES.getString("msg_dex_file_magic1")
)));
startPos += DexFile.DEX_FILE_MAGIC1.size();
startPos += DexFile.DEX_FILE_MAGIC1.length;

magicNode.add(new DefaultMutableTreeNode(new JTreeNodeFileComponent(
startPos,
DexFile.DEX_FILE_MAGIC2.size(),
DexFile.DEX_FILE_MAGIC2.length,
"magic 2: " + new String(dexFile.magic2, StandardCharsets.UTF_8),
UITool.icon4Magic(),
GenerateTreeNodeDexFile.MESSAGES.getString("msg_dex_file_magic2")
Expand Down
Loading

0 comments on commit 72fac36

Please sign in to comment.