- * 03 04 61 62 C2 A9 00
- * ^ Offset should be here
- *
- *
- * @param buffer The buffer containing the string to decode.
- * @param offset Offset into the buffer where the string resides.
- * @param type The encoding type that the {@link BinaryResourceString} is encoded in.
- * @return The decoded string.
- */
- public static String decodeString(ByteBuffer buffer, int offset, Type type) {
- int length;
- int characterCount = decodeLength(buffer, offset, type);
- offset += computeLengthOffset(characterCount, type);
- // UTF-8 strings have 2 lengths: the number of characters, and then the encoding length.
- // UTF-16 strings, however, only have 1 length: the number of characters.
- if (type == Type.UTF8) {
- length = decodeLength(buffer, offset, type);
- offset += computeLengthOffset(length, type);
- } else {
- length = characterCount * 2;
+
+ private BinaryResourceString() {
+ } // Private constructor
+
+ /**
+ * Given a buffer and an offset into the buffer, returns a String. The {@code offset} is the
+ * 0-based byte offset from the start of the buffer where the string resides. This should be the
+ * location in memory where the string's character count, followed by its byte count, and then
+ * followed by the actual string is located.
+ *
+ *
+ * 03 04 61 62 C2 A9 00
+ * ^ Offset should be here
+ *
+ *
+ * @param buffer The buffer containing the string to decode.
+ * @param offset Offset into the buffer where the string resides.
+ * @param type The encoding type that the {@link BinaryResourceString} is encoded in.
+ * @return The decoded string.
+ */
+ public static String decodeString(ByteBuffer buffer, int offset, Type type) {
+ int length;
+ int characterCount = decodeLength(buffer, offset, type);
+ offset += computeLengthOffset(characterCount, type);
+ // UTF-8 strings have 2 lengths: the number of characters, and then the encoding length.
+ // UTF-16 strings, however, only have 1 length: the number of characters.
+ if (type == Type.UTF8) {
+ length = decodeLength(buffer, offset, type);
+ offset += computeLengthOffset(length, type);
+ } else {
+ length = characterCount * 2;
+ }
+ return new String(buffer.array(), offset, length, type.charset());
}
- return new String(buffer.array(), offset, length, type.charset());
- }
-
- /**
- * Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
- * Strings are prefixed by 2 values. The first is the number of characters in the string.
- * The second is the encoding length (number of bytes in the string).
- *
- *
- *
- * @param str The string to be encoded.
- * @param type The encoding type that the {@link BinaryResourceString} should be encoded in.
- * @return The encoded string.
- */
- public static byte[] encodeString(String str, Type type) {
- byte[] bytes = str.getBytes(type.charset());
- // The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
- ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
- encodeLength(output, str.length(), type);
- if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length.
- encodeLength(output, bytes.length, type);
+
+ /**
+ * Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
+ * Strings are prefixed by 2 values. The first is the number of characters in the string.
+ * The second is the encoding length (number of bytes in the string).
+ *
+ *
+ *
+ * @param str The string to be encoded.
+ * @param type The encoding type that the {@link BinaryResourceString} should be encoded in.
+ * @return The encoded string.
+ */
+ public static byte[] encodeString(String str, Type type) {
+ byte[] bytes = str.getBytes(type.charset());
+ // The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
+ ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
+ encodeLength(output, str.length(), type);
+ if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length.
+ encodeLength(output, bytes.length, type);
+ }
+ output.write(bytes);
+ // NULL-terminate the string
+ if (type == Type.UTF8) {
+ output.write(0);
+ } else {
+ output.writeShort(0);
+ }
+ return output.toByteArray();
}
- output.write(bytes);
- // NULL-terminate the string
- if (type == Type.UTF8) {
- output.write(0);
- } else {
- output.writeShort(0);
+
+ private static void encodeLength(ByteArrayDataOutput output, int length, Type type) {
+ if (length < 0) {
+ output.write(0);
+ return;
+ }
+ if (type == Type.UTF8) {
+ if (length > 0x7F) {
+ output.write(((length & 0x7F00) >> 8) | 0x80);
+ }
+ output.write(length & 0xFF);
+ } else { // UTF-16
+ // TODO(acornwall): Replace output with a little-endian output.
+ if (length > 0x7FFF) {
+ int highBytes = ((length & 0x7FFF0000) >> 16) | 0x8000;
+ output.write(highBytes & 0xFF);
+ output.write((highBytes & 0xFF00) >> 8);
+ }
+ int lowBytes = length & 0xFFFF;
+ output.write(lowBytes & 0xFF);
+ output.write((lowBytes & 0xFF00) >> 8);
+ }
}
- return output.toByteArray();
- }
- private static void encodeLength(ByteArrayDataOutput output, int length, Type type) {
- if (length < 0) {
- output.write(0);
- return;
+ private static int computeLengthOffset(int length, Type type) {
+ return (type == Type.UTF8 ? 1 : 2) * (length >= (type == Type.UTF8 ? 0x80 : 0x8000) ? 2 : 1);
}
- if (type == Type.UTF8) {
- if (length > 0x7F) {
- output.write(((length & 0x7F00) >> 8) | 0x80);
- }
- output.write(length & 0xFF);
- } else { // UTF-16
- // TODO(acornwall): Replace output with a little-endian output.
- if (length > 0x7FFF) {
- int highBytes = ((length & 0x7FFF0000) >> 16) | 0x8000;
- output.write(highBytes & 0xFF);
- output.write((highBytes & 0xFF00) >> 8);
- }
- int lowBytes = length & 0xFFFF;
- output.write(lowBytes & 0xFF);
- output.write((lowBytes & 0xFF00) >> 8);
+
+ private static int decodeLength(ByteBuffer buffer, int offset, Type type) {
+ return type == Type.UTF8 ? decodeLengthUTF8(buffer, offset) : decodeLengthUTF16(buffer, offset);
}
- }
-
- private static int computeLengthOffset(int length, Type type) {
- return (type == Type.UTF8 ? 1 : 2) * (length >= (type == Type.UTF8 ? 0x80 : 0x8000) ? 2 : 1);
- }
-
- private static int decodeLength(ByteBuffer buffer, int offset, Type type) {
- return type == Type.UTF8 ? decodeLengthUTF8(buffer, offset) : decodeLengthUTF16(buffer, offset);
- }
-
- private static int decodeLengthUTF8(ByteBuffer buffer, int offset) {
- // UTF-8 strings use a clever variant of the 7-bit integer for packing the string length.
- // If the first byte is >= 0x80, then a second byte follows. For these values, the length
- // is WORD-length in big-endian & 0x7FFF.
- int length = UnsignedBytes.toInt(buffer.get(offset));
- if ((length & 0x80) != 0) {
- length = ((length & 0x7F) << 8) | UnsignedBytes.toInt(buffer.get(offset + 1));
+
+ private static int decodeLengthUTF8(ByteBuffer buffer, int offset) {
+ // UTF-8 strings use a clever variant of the 7-bit integer for packing the string length.
+ // If the first byte is >= 0x80, then a second byte follows. For these values, the length
+ // is WORD-length in big-endian & 0x7FFF.
+ int length = UnsignedBytes.toInt(buffer.get(offset));
+ if ((length & 0x80) != 0) {
+ length = ((length & 0x7F) << 8) | UnsignedBytes.toInt(buffer.get(offset + 1));
+ }
+ return length;
}
- return length;
- }
-
- private static int decodeLengthUTF16(ByteBuffer buffer, int offset) {
- // UTF-16 strings use a clever variant of the 7-bit integer for packing the string length.
- // If the first word is >= 0x8000, then a second word follows. For these values, the length
- // is DWORD-length in big-endian & 0x7FFFFFFF.
- int length = (buffer.getShort(offset) & 0xFFFF);
- if ((length & 0x8000) != 0) {
- length = ((length & 0x7FFF) << 16) | (buffer.getShort(offset + 2) & 0xFFFF);
+
+ private static int decodeLengthUTF16(ByteBuffer buffer, int offset) {
+ // UTF-16 strings use a clever variant of the 7-bit integer for packing the string length.
+ // If the first word is >= 0x8000, then a second word follows. For these values, the length
+ // is DWORD-length in big-endian & 0x7FFFFFFF.
+ int length = (buffer.getShort(offset) & 0xFFFF);
+ if ((length & 0x8000) != 0) {
+ length = ((length & 0x7FFF) << 16) | (buffer.getShort(offset + 2) & 0xFFFF);
+ }
+ return length;
}
- return length;
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/BinaryResourceValue.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/BinaryResourceValue.java
index b66a531571..50e8d9e447 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/BinaryResourceValue.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/BinaryResourceValue.java
@@ -26,126 +26,176 @@
import java.util.Map;
import java.util.Objects;
-/** Represents a single typed resource value. */
+/**
+ * Represents a single typed resource value.
+ */
public class BinaryResourceValue implements SerializableResource {
- /** Resource type codes. */
- public enum Type {
- /** {@code data} is either 0 (undefined) or 1 (empty). */
- NULL(0x00),
- /** {@code data} holds a {@link ResourceTableChunk} entry reference. */
- REFERENCE(0x01),
- /** {@code data} holds an attribute resource identifier. */
- ATTRIBUTE(0x02),
- /** {@code data} holds an index into the containing resource table's string pool. */
- STRING(0x03),
- /** {@code data} holds a single-precision floating point number. */
- FLOAT(0x04),
- /** {@code data} holds a complex number encoding a dimension value, such as "100in". */
- DIMENSION(0x05),
- /** {@code data} holds a complex number encoding a fraction of a container. */
- FRACTION(0x06),
- /** {@code data} holds a dynamic {@link ResourceTableChunk} entry reference. */
- DYNAMIC_REFERENCE(0x07),
- /** {@code data} holds a dynamic attribute resource identifier. */
- DYNAMIC_ATTRIBUTE(0x08),
- /** {@code data} is a raw integer value of the form n..n. */
- INT_DEC(0x10),
- /** {@code data} is a raw integer value of the form 0xn..n. */
- INT_HEX(0x11),
- /** {@code data} is either 0 (false) or 1 (true). */
- INT_BOOLEAN(0x12),
- /** {@code data} is a raw integer value of the form #aarrggbb. */
- INT_COLOR_ARGB8(0x1c),
- /** {@code data} is a raw integer value of the form #rrggbb. */
- INT_COLOR_RGB8(0x1d),
- /** {@code data} is a raw integer value of the form #argb. */
- INT_COLOR_ARGB4(0x1e),
- /** {@code data} is a raw integer value of the form #rgb. */
- INT_COLOR_RGB4(0x1f);
-
- private final byte code;
-
- private static final Map FROM_BYTE;
-
- static {
- Builder builder = ImmutableMap.builder();
- for (Type type : values()) {
- builder.put(type.code(), type);
- }
- FROM_BYTE = builder.build();
+ /**
+ * Resource type codes.
+ */
+ public enum Type {
+ /**
+ * {@code data} is either 0 (undefined) or 1 (empty).
+ */
+ NULL(0x00),
+ /**
+ * {@code data} holds a {@link ResourceTableChunk} entry reference.
+ */
+ REFERENCE(0x01),
+ /**
+ * {@code data} holds an attribute resource identifier.
+ */
+ ATTRIBUTE(0x02),
+ /**
+ * {@code data} holds an index into the containing resource table's string pool.
+ */
+ STRING(0x03),
+ /**
+ * {@code data} holds a single-precision floating point number.
+ */
+ FLOAT(0x04),
+ /**
+ * {@code data} holds a complex number encoding a dimension value, such as "100in".
+ */
+ DIMENSION(0x05),
+ /**
+ * {@code data} holds a complex number encoding a fraction of a container.
+ */
+ FRACTION(0x06),
+ /**
+ * {@code data} holds a dynamic {@link ResourceTableChunk} entry reference.
+ */
+ DYNAMIC_REFERENCE(0x07),
+ /**
+ * {@code data} holds a dynamic attribute resource identifier.
+ */
+ DYNAMIC_ATTRIBUTE(0x08),
+ /**
+ * {@code data} is a raw integer value of the form n..n.
+ */
+ INT_DEC(0x10),
+ /**
+ * {@code data} is a raw integer value of the form 0xn..n.
+ */
+ INT_HEX(0x11),
+ /**
+ * {@code data} is either 0 (false) or 1 (true).
+ */
+ INT_BOOLEAN(0x12),
+ /**
+ * {@code data} is a raw integer value of the form #aarrggbb.
+ */
+ INT_COLOR_ARGB8(0x1c),
+ /**
+ * {@code data} is a raw integer value of the form #rrggbb.
+ */
+ INT_COLOR_RGB8(0x1d),
+ /**
+ * {@code data} is a raw integer value of the form #argb.
+ */
+ INT_COLOR_ARGB4(0x1e),
+ /**
+ * {@code data} is a raw integer value of the form #rgb.
+ */
+ INT_COLOR_RGB4(0x1f);
+
+ private final byte code;
+
+ private static final Map FROM_BYTE;
+
+ static {
+ Builder builder = ImmutableMap.builder();
+ for (Type type : values()) {
+ builder.put(type.code(), type);
+ }
+ FROM_BYTE = builder.build();
+ }
+
+ Type(int code) {
+ this.code = UnsignedBytes.checkedCast(code);
+ }
+
+ public byte code() {
+ return code;
+ }
+
+ public static Type fromCode(byte code) {
+ return Preconditions.checkNotNull(FROM_BYTE.get(code), "Unknown resource type: %s", code);
+ }
+ }
+
+ /**
+ * The serialized size in bytes of a {@link BinaryResourceValue}.
+ */
+ public static final int SIZE = 8;
+
+ public int size;
+ public Type type;
+ public int data;
+
+ public static BinaryResourceValue create(ByteBuffer buffer) {
+ int size = (buffer.getShort() & 0xFFFF);
+ buffer.get(); // Unused
+ Type type = Type.fromCode(buffer.get());
+ int data = buffer.getInt();
+ return new BinaryResourceValue(size, type, data);
+ }
+
+ public BinaryResourceValue(int size, Type type, int data) {
+ this.size = size;
+ this.type = type;
+ this.data = data;
+ }
+
+ /**
+ * The length in bytes of this value.
+ */
+ public int size() {
+ return size;
+ }
+
+ /**
+ * The raw data type of this value.
+ */
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * The actual 4-byte value; interpretation of the value depends on {@code dataType}.
+ */
+ public int data() {
+ return data;
+ }
+
+ @Override
+ public byte[] toByteArray() {
+ return toByteArray(false);
}
- Type(int code) {
- this.code = UnsignedBytes.checkedCast(code);
+ @Override
+ public byte[] toByteArray(boolean shrink) {
+ ByteBuffer buffer = ByteBuffer.allocate(SIZE).order(ByteOrder.LITTLE_ENDIAN);
+ buffer.putShort((short) size());
+ buffer.put((byte) 0); // Unused
+ buffer.put(type().code());
+ buffer.putInt(data());
+ return buffer.array();
}
- public byte code() {
- return code;
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ BinaryResourceValue that = (BinaryResourceValue) o;
+ return size == that.size &&
+ data == that.data &&
+ type == that.type;
}
- public static Type fromCode(byte code) {
- return Preconditions.checkNotNull(FROM_BYTE.get(code), "Unknown resource type: %s", code);
+ @Override
+ public int hashCode() {
+ return Objects.hash(size, type, data);
}
- }
-
- /** The serialized size in bytes of a {@link BinaryResourceValue}. */
- public static final int SIZE = 8;
-
- private final int size;
- private final Type type;
- private final int data;
-
- public static BinaryResourceValue create(ByteBuffer buffer) {
- int size = (buffer.getShort() & 0xFFFF);
- buffer.get(); // Unused
- Type type = Type.fromCode(buffer.get());
- int data = buffer.getInt();
- return new BinaryResourceValue(size, type, data);
- }
-
- public BinaryResourceValue(int size, Type type, int data) {
- this.size = size;
- this.type = type;
- this.data = data;
- }
-
- /** The length in bytes of this value. */
- public int size() { return size; }
-
- /** The raw data type of this value. */
- public Type type() { return type; }
-
- /** The actual 4-byte value; interpretation of the value depends on {@code dataType}. */
- public int data() { return data; }
-
- @Override
- public byte[] toByteArray() {
- return toByteArray(false);
- }
-
- @Override
- public byte[] toByteArray(boolean shrink) {
- ByteBuffer buffer = ByteBuffer.allocate(SIZE).order(ByteOrder.LITTLE_ENDIAN);
- buffer.putShort((short) size());
- buffer.put((byte) 0); // Unused
- buffer.put(type().code());
- buffer.putInt(data());
- return buffer.array();
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- BinaryResourceValue that = (BinaryResourceValue)o;
- return size == that.size &&
- data == that.data &&
- type == that.type;
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(size, type, data);
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/Chunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/Chunk.java
index 0d26614865..665b8cd06b 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/Chunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/Chunk.java
@@ -30,272 +30,296 @@
import java.nio.ByteOrder;
import java.util.Map;
-/** Represents a generic chunk. */
+/**
+ * Represents a generic chunk.
+ */
public abstract class Chunk implements SerializableResource {
- /** Types of chunks that can exist. */
- public enum Type {
- NULL(0x0000),
- STRING_POOL(0x0001),
- TABLE(0x0002),
- XML(0x0003),
- XML_START_NAMESPACE(0x0100),
- XML_END_NAMESPACE(0x0101),
- XML_START_ELEMENT(0x0102),
- XML_END_ELEMENT(0x0103),
- XML_CDATA(0x0104),
- XML_RESOURCE_MAP(0x0180),
- TABLE_PACKAGE(0x0200),
- TABLE_TYPE(0x0201),
- TABLE_TYPE_SPEC(0x0202),
- TABLE_LIBRARY(0x0203);
-
- private final short code;
-
- private static final Map FROM_SHORT;
-
- static {
- Builder builder = ImmutableMap.builder();
- for (Type type : values()) {
- builder.put(type.code(), type);
- }
- FROM_SHORT = builder.build();
+ /**
+ * Types of chunks that can exist.
+ */
+ public enum Type {
+ NULL(0x0000),
+ STRING_POOL(0x0001),
+ TABLE(0x0002),
+ XML(0x0003),
+ XML_START_NAMESPACE(0x0100),
+ XML_END_NAMESPACE(0x0101),
+ XML_START_ELEMENT(0x0102),
+ XML_END_ELEMENT(0x0103),
+ XML_CDATA(0x0104),
+ XML_RESOURCE_MAP(0x0180),
+ TABLE_PACKAGE(0x0200),
+ TABLE_TYPE(0x0201),
+ TABLE_TYPE_SPEC(0x0202),
+ TABLE_LIBRARY(0x0203);
+
+ private final short code;
+
+ private static final Map FROM_SHORT;
+
+ static {
+ Builder builder = ImmutableMap.builder();
+ for (Type type : values()) {
+ builder.put(type.code(), type);
+ }
+ FROM_SHORT = builder.build();
+ }
+
+ Type(int code) {
+ this.code = Shorts.checkedCast(code);
+ }
+
+ public short code() {
+ return code;
+ }
+
+ public static Type fromCode(short code) {
+ return Preconditions.checkNotNull(FROM_SHORT.get(code), "Unknown chunk type: %s", code);
+ }
+ }
+
+ /**
+ * The byte boundary to pad chunks on.
+ */
+ public static final int PAD_BOUNDARY = 4;
+
+ /**
+ * The number of bytes in every chunk that describes chunk type, header size, and chunk size.
+ */
+ public static final int METADATA_SIZE = 8;
+
+ /**
+ * The offset in bytes, from the start of the chunk, where the chunk size can be found.
+ */
+ private static final int CHUNK_SIZE_OFFSET = 4;
+
+ /**
+ * The parent to this chunk, if any.
+ */
+ @Nullable
+ private final Chunk parent;
+
+ /**
+ * Size of the chunk header in bytes.
+ */
+ protected final int headerSize;
+
+ /**
+ * headerSize + dataSize. The total size of this chunk.
+ */
+ protected final int chunkSize;
+
+ /**
+ * Offset of this chunk from the start of the file.
+ */
+ protected final int offset;
+
+ protected Chunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ this.parent = parent;
+ offset = buffer.position() - 2;
+ headerSize = (buffer.getShort() & 0xFFFF);
+ chunkSize = buffer.getInt();
+ }
+
+ /**
+ * Finishes initialization of a chunk. This should be called immediately after the constructor.
+ * This is separate from the constructor so that the header of a chunk can be fully initialized
+ * before the payload of that chunk is initialized for chunks that require such behavior.
+ *
+ * @param buffer The buffer that the payload will be initialized from.
+ */
+ protected void init(ByteBuffer buffer) {
+ }
+
+ /**
+ * Returns the parent to this chunk, if any. A parent is a chunk whose payload contains this
+ * chunk. If there's no parent, null is returned.
+ */
+ @Nullable
+ public Chunk getParent() {
+ return parent;
+ }
+
+ protected abstract Type getType();
+
+ /**
+ * Returns the size of this chunk's header.
+ */
+ public final int getHeaderSize() {
+ return headerSize;
}
- Type(int code) {
- this.code = Shorts.checkedCast(code);
+ /**
+ * Returns the size of this chunk when it was first read from a buffer. A chunk's size can deviate
+ * from this value when its data is modified (e.g. adding an entry, changing a string).
+ *
+ *
A chunk's current size can be determined from the length of the byte array returned from
+ * {@link #toByteArray}.
+ */
+ public final int getOriginalChunkSize() {
+ return chunkSize;
}
- public short code() {
- return code;
+ /**
+ * Reposition the buffer after this chunk. Use this at the end of a Chunk constructor.
+ *
+ * @param buffer The buffer to be repositioned.
+ */
+ private final void seekToEndOfChunk(ByteBuffer buffer) {
+ buffer.position(offset + chunkSize);
}
- public static Type fromCode(short code) {
- return Preconditions.checkNotNull(FROM_SHORT.get(code), "Unknown chunk type: %s", code);
+ /**
+ * Writes the type and header size. We don't know how big this chunk will be (it could be
+ * different since the last time we checked), so this needs to be passed in.
+ *
+ * @param output The buffer that will be written to.
+ * @param chunkSize The total size of this chunk in bytes, including the header.
+ */
+ protected final void writeHeader(ByteBuffer output, int chunkSize) {
+ int start = output.position();
+ output.putShort(getType().code());
+ output.putShort((short) headerSize);
+ output.putInt(chunkSize);
+ writeHeader(output);
+ int headerBytes = output.position() - start;
+ Preconditions.checkState(headerBytes == getHeaderSize(),
+ "Written header is wrong size. Got %s, want %s", headerBytes, getHeaderSize());
}
- }
-
- /** The byte boundary to pad chunks on. */
- public static final int PAD_BOUNDARY = 4;
-
- /** The number of bytes in every chunk that describes chunk type, header size, and chunk size. */
- public static final int METADATA_SIZE = 8;
-
- /** The offset in bytes, from the start of the chunk, where the chunk size can be found. */
- private static final int CHUNK_SIZE_OFFSET = 4;
-
- /** The parent to this chunk, if any. */
- @Nullable
- private final Chunk parent;
-
- /** Size of the chunk header in bytes. */
- protected final int headerSize;
-
- /** headerSize + dataSize. The total size of this chunk. */
- protected final int chunkSize;
-
- /** Offset of this chunk from the start of the file. */
- protected final int offset;
-
- protected Chunk(ByteBuffer buffer, @Nullable Chunk parent) {
- this.parent = parent;
- offset = buffer.position() - 2;
- headerSize = (buffer.getShort() & 0xFFFF);
- chunkSize = buffer.getInt();
- }
-
- /**
- * Finishes initialization of a chunk. This should be called immediately after the constructor.
- * This is separate from the constructor so that the header of a chunk can be fully initialized
- * before the payload of that chunk is initialized for chunks that require such behavior.
- *
- * @param buffer The buffer that the payload will be initialized from.
- */
- protected void init(ByteBuffer buffer) {}
-
- /**
- * Returns the parent to this chunk, if any. A parent is a chunk whose payload contains this
- * chunk. If there's no parent, null is returned.
- */
- @Nullable
- public Chunk getParent() {
- return parent;
- }
-
- protected abstract Type getType();
-
- /** Returns the size of this chunk's header. */
- public final int getHeaderSize() {
- return headerSize;
- }
-
- /**
- * Returns the size of this chunk when it was first read from a buffer. A chunk's size can deviate
- * from this value when its data is modified (e.g. adding an entry, changing a string).
- *
- *
A chunk's current size can be determined from the length of the byte array returned from
- * {@link #toByteArray}.
- */
- public final int getOriginalChunkSize() {
- return chunkSize;
- }
-
- /**
- * Reposition the buffer after this chunk. Use this at the end of a Chunk constructor.
- * @param buffer The buffer to be repositioned.
- */
- private final void seekToEndOfChunk(ByteBuffer buffer) {
- buffer.position(offset + chunkSize);
- }
-
- /**
- * Writes the type and header size. We don't know how big this chunk will be (it could be
- * different since the last time we checked), so this needs to be passed in.
- *
- * @param output The buffer that will be written to.
- * @param chunkSize The total size of this chunk in bytes, including the header.
- */
- protected final void writeHeader(ByteBuffer output, int chunkSize) {
- int start = output.position();
- output.putShort(getType().code());
- output.putShort((short) headerSize);
- output.putInt(chunkSize);
- writeHeader(output);
- int headerBytes = output.position() - start;
- Preconditions.checkState(headerBytes == getHeaderSize(),
- "Written header is wrong size. Got %s, want %s", headerBytes, getHeaderSize());
- }
-
- /**
- * Writes the remaining header (after the type, {@code headerSize}, and {@code chunkSize}).
- *
- * @param output The buffer that the header will be written to.
- */
- protected void writeHeader(ByteBuffer output) {}
-
- /**
- * Writes the chunk payload. The payload is data in a chunk which is not in
- * the first {@code headerSize} bytes of the chunk.
- *
- * @param output The stream that the payload will be written to.
- * @param header The already-written header. This can be modified to fix payload offsets.
- * @param shrink True if this payload should be optimized for size.
- * @throws IOException Thrown if {@code output} could not be written to (out of memory).
- */
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {}
-
- /**
- * Pads {@code output} until {@code currentLength} is on a 4-byte boundary.
- *
- * @param output The {@link DataOutput} that will be padded.
- * @param currentLength The current length, in bytes, of {@code output}
- * @return The new length of {@code output}
- * @throws IOException Thrown if {@code output} could not be written to.
- */
- protected int writePad(DataOutput output, int currentLength) throws IOException {
- while (currentLength % PAD_BOUNDARY != 0) {
- output.write(0);
- ++currentLength;
+
+ /**
+ * Writes the remaining header (after the type, {@code headerSize}, and {@code chunkSize}).
+ *
+ * @param output The buffer that the header will be written to.
+ */
+ protected void writeHeader(ByteBuffer output) {
+ }
+
+ /**
+ * Writes the chunk payload. The payload is data in a chunk which is not in
+ * the first {@code headerSize} bytes of the chunk.
+ *
+ * @param output The stream that the payload will be written to.
+ * @param header The already-written header. This can be modified to fix payload offsets.
+ * @param shrink True if this payload should be optimized for size.
+ * @throws IOException Thrown if {@code output} could not be written to (out of memory).
+ */
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ }
+
+ /**
+ * Pads {@code output} until {@code currentLength} is on a 4-byte boundary.
+ *
+ * @param output The {@link DataOutput} that will be padded.
+ * @param currentLength The current length, in bytes, of {@code output}
+ * @return The new length of {@code output}
+ * @throws IOException Thrown if {@code output} could not be written to.
+ */
+ protected int writePad(DataOutput output, int currentLength) throws IOException {
+ while (currentLength % PAD_BOUNDARY != 0) {
+ output.write(0);
+ ++currentLength;
+ }
+ return currentLength;
}
- return currentLength;
- }
-
- @Override
- public final byte[] toByteArray() throws IOException {
- return toByteArray(false);
- }
-
- /**
- * Converts this chunk into an array of bytes representation. Normally you will not need to
- * override this method unless your header changes based on the contents / size of the payload.
- */
- @Override
- public final byte[] toByteArray(boolean shrink) throws IOException {
- ByteBuffer header = ByteBuffer.allocate(getHeaderSize()).order(ByteOrder.LITTLE_ENDIAN);
- writeHeader(header, 0); // The chunk size isn't known yet. This will be filled in later.
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
- writePayload(payload, header, shrink);
+
+ @Override
+ public final byte[] toByteArray() throws IOException {
+ return toByteArray(false);
+ }
+
+ /**
+ * Converts this chunk into an array of bytes representation. Normally you will not need to
+ * override this method unless your header changes based on the contents / size of the payload.
+ */
+ @Override
+ public final byte[] toByteArray(boolean shrink) throws IOException {
+ ByteBuffer header = ByteBuffer.allocate(getHeaderSize()).order(ByteOrder.LITTLE_ENDIAN);
+ writeHeader(header, 0); // The chunk size isn't known yet. This will be filled in later.
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+
+ try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
+ writePayload(payload, header, shrink);
+ }
+
+ byte[] payloadBytes = baos.toByteArray();
+ int chunkSize = getHeaderSize() + payloadBytes.length;
+ header.putInt(CHUNK_SIZE_OFFSET, chunkSize);
+
+ // Combine results
+ ByteBuffer result = ByteBuffer.allocate(chunkSize).order(ByteOrder.LITTLE_ENDIAN);
+ result.put(header.array());
+ result.put(payloadBytes);
+ return result.array();
+ }
+
+ /**
+ * Creates a new chunk whose contents start at {@code buffer}'s current position.
+ *
+ * @param buffer A buffer positioned at the start of a chunk.
+ * @return new chunk
+ */
+ public static Chunk newInstance(ByteBuffer buffer) {
+ return newInstance(buffer, null);
}
- byte[] payloadBytes = baos.toByteArray();
- int chunkSize = getHeaderSize() + payloadBytes.length;
- header.putInt(CHUNK_SIZE_OFFSET, chunkSize);
-
- // Combine results
- ByteBuffer result = ByteBuffer.allocate(chunkSize).order(ByteOrder.LITTLE_ENDIAN);
- result.put(header.array());
- result.put(payloadBytes);
- return result.array();
- }
-
- /**
- * Creates a new chunk whose contents start at {@code buffer}'s current position.
- *
- * @param buffer A buffer positioned at the start of a chunk.
- * @return new chunk
- */
- public static Chunk newInstance(ByteBuffer buffer) {
- return newInstance(buffer, null);
- }
-
- /**
- * Creates a new chunk whose contents start at {@code buffer}'s current position.
- *
- * @param buffer A buffer positioned at the start of a chunk.
- * @param parent The parent to this chunk (or null if there's no parent).
- * @return new chunk
- */
- public static Chunk newInstance(ByteBuffer buffer, @Nullable Chunk parent) {
- Chunk result;
- Type type = Type.fromCode(buffer.getShort());
- switch (type) {
- case STRING_POOL:
- result = new StringPoolChunk(buffer, parent);
- break;
- case TABLE:
- result = new ResourceTableChunk(buffer, parent);
- break;
- case XML:
- result = new XmlChunk(buffer, parent);
- break;
- case XML_START_NAMESPACE:
- result = new XmlNamespaceStartChunk(buffer, parent);
- break;
- case XML_END_NAMESPACE:
- result = new XmlNamespaceEndChunk(buffer, parent);
- break;
- case XML_START_ELEMENT:
- result = new XmlStartElementChunk(buffer, parent);
- break;
- case XML_END_ELEMENT:
- result = new XmlEndElementChunk(buffer, parent);
- break;
- case XML_CDATA:
- result = new XmlCdataChunk(buffer, parent);
- break;
- case XML_RESOURCE_MAP:
- result = new XmlResourceMapChunk(buffer, parent);
- break;
- case TABLE_PACKAGE:
- result = new PackageChunk(buffer, parent);
- break;
- case TABLE_TYPE:
- result = new TypeChunk(buffer, parent);
- break;
- case TABLE_TYPE_SPEC:
- result = new TypeSpecChunk(buffer, parent);
- break;
- case TABLE_LIBRARY:
- result = new LibraryChunk(buffer, parent);
- break;
- default:
- result = new UnknownChunk(buffer, parent);
+ /**
+ * Creates a new chunk whose contents start at {@code buffer}'s current position.
+ *
+ * @param buffer A buffer positioned at the start of a chunk.
+ * @param parent The parent to this chunk (or null if there's no parent).
+ * @return new chunk
+ */
+ public static Chunk newInstance(ByteBuffer buffer, @Nullable Chunk parent) {
+ Chunk result;
+ Type type = Type.fromCode(buffer.getShort());
+ switch (type) {
+ case STRING_POOL:
+ result = new StringPoolChunk(buffer, parent);
+ break;
+ case TABLE:
+ result = new ResourceTableChunk(buffer, parent);
+ break;
+ case XML:
+ result = new XmlChunk(buffer, parent);
+ break;
+ case XML_START_NAMESPACE:
+ result = new XmlNamespaceStartChunk(buffer, parent);
+ break;
+ case XML_END_NAMESPACE:
+ result = new XmlNamespaceEndChunk(buffer, parent);
+ break;
+ case XML_START_ELEMENT:
+ result = new XmlStartElementChunk(buffer, parent);
+ break;
+ case XML_END_ELEMENT:
+ result = new XmlEndElementChunk(buffer, parent);
+ break;
+ case XML_CDATA:
+ result = new XmlCdataChunk(buffer, parent);
+ break;
+ case XML_RESOURCE_MAP:
+ result = new XmlResourceMapChunk(buffer, parent);
+ break;
+ case TABLE_PACKAGE:
+ result = new PackageChunk(buffer, parent);
+ break;
+ case TABLE_TYPE:
+ result = new TypeChunk(buffer, parent);
+ break;
+ case TABLE_TYPE_SPEC:
+ result = new TypeSpecChunk(buffer, parent);
+ break;
+ case TABLE_LIBRARY:
+ result = new LibraryChunk(buffer, parent);
+ break;
+ default:
+ result = new UnknownChunk(buffer, parent);
+ }
+ result.init(buffer);
+ result.seekToEndOfChunk(buffer);
+ return result;
}
- result.init(buffer);
- result.seekToEndOfChunk(buffer);
- return result;
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ChunkWithChunks.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ChunkWithChunks.java
index 11ba3a41ff..8a311a2b76 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ChunkWithChunks.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ChunkWithChunks.java
@@ -24,50 +24,52 @@
import java.util.LinkedHashMap;
import java.util.Map;
-/** Represents a chunk whose payload is a list of sub-chunks. */
+/**
+ * Represents a chunk whose payload is a list of sub-chunks.
+ */
public abstract class ChunkWithChunks extends Chunk {
- private final Map chunks = new LinkedHashMap<>();
+ private final Map chunks = new LinkedHashMap<>();
- protected ChunkWithChunks(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- }
+ protected ChunkWithChunks(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ }
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- chunks.clear();
- int start = this.offset + getHeaderSize();
- int offset = start;
- int end = this.offset + getOriginalChunkSize();
- int position = buffer.position();
- buffer.position(start);
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ chunks.clear();
+ int start = this.offset + getHeaderSize();
+ int offset = start;
+ int end = this.offset + getOriginalChunkSize();
+ int position = buffer.position();
+ buffer.position(start);
- while (offset < end) {
- Chunk chunk = Chunk.newInstance(buffer, this);
- chunks.put(offset, chunk);
- offset += chunk.getOriginalChunkSize();
- }
+ while (offset < end) {
+ Chunk chunk = Chunk.newInstance(buffer, this);
+ chunks.put(offset, chunk);
+ offset += chunk.getOriginalChunkSize();
+ }
- buffer.position(position);
- }
+ buffer.position(position);
+ }
- /**
- * Retrieves the @{code chunks} contained in this chunk.
- *
- * @return map of buffer offset -> chunk contained in this chunk.
- */
- public final Map getChunks() {
- return chunks;
- }
+ /**
+ * Retrieves the @{code chunks} contained in this chunk.
+ *
+ * @return map of buffer offset -> chunk contained in this chunk.
+ */
+ public final Map getChunks() {
+ return chunks;
+ }
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- for (Chunk chunk : getChunks().values()) {
- byte[] chunkBytes = chunk.toByteArray(shrink);
- output.write(chunkBytes);
- writePad(output, chunkBytes.length);
+ @Override
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ for (Chunk chunk : getChunks().values()) {
+ byte[] chunkBytes = chunk.toByteArray(shrink);
+ output.write(chunkBytes);
+ writePad(output, chunkBytes.length);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/LibraryChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/LibraryChunk.java
index 38236de8b0..4a31546f4c 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/LibraryChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/LibraryChunk.java
@@ -17,6 +17,7 @@
package com.google.devrel.gmscore.tools.apk.arsc;
import androidx.annotation.Nullable;
+
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -32,105 +33,121 @@
*/
public final class LibraryChunk extends Chunk {
- /** The number of resources of this type at creation time. */
- private final int entryCount;
-
- /** The libraries used in this chunk (package id + name). */
- private final List entries = new ArrayList<>();
-
- protected LibraryChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- entryCount = buffer.getInt();
- }
-
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- entries.addAll(enumerateEntries(buffer));
- }
+ /**
+ * The number of resources of this type at creation time.
+ */
+ private final int entryCount;
- private List enumerateEntries(ByteBuffer buffer) {
- List result = new ArrayList<>(entryCount);
- int offset = this.offset + getHeaderSize();
- int endOffset = offset + Entry.SIZE * entryCount;
+ /**
+ * The libraries used in this chunk (package id + name).
+ */
+ private final List entries = new ArrayList<>();
- while (offset < endOffset) {
- result.add(Entry.create(buffer, offset));
- offset += Entry.SIZE;
+ protected LibraryChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ entryCount = buffer.getInt();
}
- return result;
- }
-
- @Override
- protected Type getType() {
- return Type.TABLE_LIBRARY;
- }
-
- @Override
- protected void writeHeader(ByteBuffer output) {
- super.writeHeader(output);
- output.putInt(entries.size());
- }
-
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- for (Entry entry : entries) {
- output.write(entry.toByteArray(shrink));
- }
- }
-
- /** A shared library package-id to package name entry. */
- protected static class Entry implements SerializableResource {
-
- /** Library entries only contain a package ID (4 bytes) and a package name. */
- private static final int SIZE = 4 + PackageUtils.PACKAGE_NAME_SIZE;
- private final int packageId;
- private final String packageName;
-
- static Entry create(ByteBuffer buffer, int offset) {
- int packageId = buffer.getInt(offset);
- String packageName = PackageUtils.readPackageName(buffer, offset + 4);
- return new Entry(packageId, packageName);
- }
-
- private Entry(int packageId, String packageName) {
- this.packageId = packageId;
- this.packageName = packageName;
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ entries.addAll(enumerateEntries(buffer));
}
- /** The id assigned to the shared library at build time. */
- public int packageId() { return packageId; }
+ private List enumerateEntries(ByteBuffer buffer) {
+ List result = new ArrayList<>(entryCount);
+ int offset = this.offset + getHeaderSize();
+ int endOffset = offset + Entry.SIZE * entryCount;
- /** The package name of the shared library. */
- public String packageName() { return packageName; }
+ while (offset < endOffset) {
+ result.add(Entry.create(buffer, offset));
+ offset += Entry.SIZE;
+ }
+ return result;
+ }
@Override
- public byte[] toByteArray() throws IOException {
- return toByteArray(false);
+ protected Type getType() {
+ return Type.TABLE_LIBRARY;
}
@Override
- public byte[] toByteArray(boolean shrink) throws IOException {
- ByteBuffer buffer = ByteBuffer.allocate(SIZE).order(ByteOrder.LITTLE_ENDIAN);
- buffer.putInt(packageId());
- PackageUtils.writePackageName(buffer, packageName());
- return buffer.array();
+ protected void writeHeader(ByteBuffer output) {
+ super.writeHeader(output);
+ output.putInt(entries.size());
}
@Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Entry entry = (Entry)o;
- return packageId == entry.packageId &&
- Objects.equals(packageName, entry.packageName);
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ for (Entry entry : entries) {
+ output.write(entry.toByteArray(shrink));
+ }
}
- @Override
- public int hashCode() {
- return Objects.hash(packageId, packageName);
+ /**
+ * A shared library package-id to package name entry.
+ */
+ protected static class Entry implements SerializableResource {
+
+ /**
+ * Library entries only contain a package ID (4 bytes) and a package name.
+ */
+ private static final int SIZE = 4 + PackageUtils.PACKAGE_NAME_SIZE;
+
+ private final int packageId;
+ private final String packageName;
+
+ static Entry create(ByteBuffer buffer, int offset) {
+ int packageId = buffer.getInt(offset);
+ String packageName = PackageUtils.readPackageName(buffer, offset + 4);
+ return new Entry(packageId, packageName);
+ }
+
+ private Entry(int packageId, String packageName) {
+ this.packageId = packageId;
+ this.packageName = packageName;
+ }
+
+ /**
+ * The id assigned to the shared library at build time.
+ */
+ public int packageId() {
+ return packageId;
+ }
+
+ /**
+ * The package name of the shared library.
+ */
+ public String packageName() {
+ return packageName;
+ }
+
+ @Override
+ public byte[] toByteArray() throws IOException {
+ return toByteArray(false);
+ }
+
+ @Override
+ public byte[] toByteArray(boolean shrink) throws IOException {
+ ByteBuffer buffer = ByteBuffer.allocate(SIZE).order(ByteOrder.LITTLE_ENDIAN);
+ buffer.putInt(packageId());
+ PackageUtils.writePackageName(buffer, packageName());
+ return buffer.array();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Entry entry = (Entry) o;
+ return packageId == entry.packageId &&
+ Objects.equals(packageName, entry.packageName);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(packageId, packageName);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageChunk.java
index cc8c2e32fc..9ede602663 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageChunk.java
@@ -16,11 +16,11 @@
package com.google.devrel.gmscore.tools.apk.arsc;
+import androidx.annotation.Nullable;
import com.google.common.base.Preconditions;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
-import androidx.annotation.Nullable;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -29,211 +29,251 @@
import java.util.Map;
import java.util.Optional;
-/** A package chunk is a collection of resource data types within a package. */
+/**
+ * A package chunk is a collection of resource data types within a package.
+ */
public final class PackageChunk extends ChunkWithChunks {
- /** Offset in bytes, from the start of the chunk, where {@code typeStringsOffset} can be found. */
- private static final int TYPE_OFFSET_OFFSET = 268;
-
- /** Offset in bytes, from the start of the chunk, where {@code keyStringsOffset} can be found. */
- private static final int KEY_OFFSET_OFFSET = 276;
-
- /** The package id if this is a base package, or 0 if not a base package. */
- private int id;
-
- /** The name of the package. */
- private String packageName;
-
- /** The offset (from {@code offset}) in the original buffer where type strings start. */
- private final int typeStringsOffset;
-
- /** The index into the type string pool of the last public type. */
- private final int lastPublicType;
-
- /** An offset to the string pool that contains the key strings for this package. */
- private final int keyStringsOffset;
-
- /** The index into the key string pool of the last public key. */
- private final int lastPublicKey;
-
- /** An offset to the type ID(s). This is undocumented in the original code. */
- private final int typeIdOffset;
-
- /** Contains a mapping of a type index to its {@link TypeSpecChunk}. */
- private final Map typeSpecs = new HashMap<>();
-
- /** Contains a mapping of a type index to all of the {@link TypeChunk} with that index. */
- private final Multimap types = ArrayListMultimap.create();
-
- /** May contain a library chunk for mapping dynamic references to resolved references. */
- private Optional libraryChunk = Optional.empty();
-
- protected PackageChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- id = buffer.getInt();
- packageName = PackageUtils.readPackageName(buffer, buffer.position());
- typeStringsOffset = buffer.getInt();
- lastPublicType = buffer.getInt();
- keyStringsOffset = buffer.getInt();
- lastPublicKey = buffer.getInt();
- typeIdOffset = buffer.getInt();
- }
-
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- for (Chunk chunk : getChunks().values()) {
- if (chunk instanceof TypeChunk) {
- TypeChunk typeChunk = (TypeChunk) chunk;
- types.put(typeChunk.getId(), typeChunk);
- } else if (chunk instanceof TypeSpecChunk) {
- TypeSpecChunk typeSpecChunk = (TypeSpecChunk) chunk;
- typeSpecs.put(typeSpecChunk.getId(), typeSpecChunk);
- } else if (chunk instanceof LibraryChunk) {
- if (libraryChunk.isPresent()) {
- throw new IllegalStateException(
- "Multiple library chunks present in package chunk.");
- }
- libraryChunk = Optional.of((LibraryChunk) chunk);
- } else if (!(chunk instanceof StringPoolChunk)) {
- throw new IllegalStateException(
- String.format("PackageChunk contains an unexpected chunk: %s", chunk.getClass()));
- }
- }
- }
-
- /** Returns the package id if this is a base package, or 0 if not a base package. */
- public int getId() {
- return id;
- }
-
- /** Sets the package id */
- public void setId(int id) {
- this.id = id;
- }
-
- /**
- * Returns the string pool that contains the names of the resources in this package.
- */
- public StringPoolChunk getKeyStringPool() {
- Chunk chunk = Preconditions.checkNotNull(getChunks().get(keyStringsOffset + offset));
- Preconditions.checkState(chunk instanceof StringPoolChunk, "Key string pool not found.");
- return (StringPoolChunk) chunk;
- }
-
- /**
- * Get the type string for a specific id, e.g., (e.g. string, attr, id).
- *
- * @param id The id to get the type for.
- * @return The type string.
- */
- public String getTypeString(int id) {
- StringPoolChunk typePool = getTypeStringPool();
- Preconditions.checkNotNull(typePool, "Package has no type pool.");
- Preconditions.checkState(typePool.getStyleCount() >= id, "No type for id: " + id);
- return typePool.getString(id - 1); // - 1 here to convert to 0-based index
- }
-
- /**
- * Returns the string pool that contains the type strings for this package, such as "layout",
- * "string", "color".
- */
- public StringPoolChunk getTypeStringPool() {
- Chunk chunk = Preconditions.checkNotNull(getChunks().get(typeStringsOffset + offset));
- Preconditions.checkState(chunk instanceof StringPoolChunk, "Type string pool not found.");
- return (StringPoolChunk) chunk;
- }
-
- /** Returns all {@link TypeChunk} in this package. */
- public Collection getTypeChunks() {
- return types.values();
- }
-
- /**
- * For a given type id, returns the {@link TypeChunk} objects that match that id. The type id is
- * the 1-based index of the type in the type string pool (returned by {@link #getTypeStringPool}).
- *
- * @param id The 1-based type id to return {@link TypeChunk} objects for.
- * @return The matching {@link TypeChunk} objects, or an empty collection if there are none.
- */
- public Collection getTypeChunks(int id) {
- return types.get(id);
- }
-
- /**
- * For a given type, returns the {@link TypeChunk} objects that match that type
- * (e.g. "attr", "id", "string", ...).
- *
- * @param type The type to return {@link TypeChunk} objects for.
- * @return The matching {@link TypeChunk} objects, or an empty collection if there are none.
- */
- public Collection getTypeChunks(String type) {
- StringPoolChunk typeStringPool = Preconditions.checkNotNull(getTypeStringPool());
- return getTypeChunks(typeStringPool.indexOf(type) + 1); // Convert 0-based index to 1-based
- }
-
- /** Returns all {@link TypeSpecChunk} in this package. */
- public Collection getTypeSpecChunks() {
- return typeSpecs.values();
- }
-
- /** For a given (1-based) type id, returns the {@link TypeSpecChunk} matching it. */
- public TypeSpecChunk getTypeSpecChunk(int id) {
- return Preconditions.checkNotNull(typeSpecs.get(id));
- }
-
- /**
- * For a given {@code type}, returns the {@link TypeSpecChunk} that matches it
- * (e.g. "attr", "id", "string", ...).
- */
- public TypeSpecChunk getTypeSpecChunk(String type) {
- StringPoolChunk typeStringPool = Preconditions.checkNotNull(getTypeStringPool());
- return getTypeSpecChunk(typeStringPool.indexOf(type) + 1); // Convert 0-based index to 1-based
- }
-
- /** Returns the name of this package. */
- public String getPackageName() {
- return packageName;
- }
-
- /** Set the package name */
- public void setPackageName(String packageName) {
- this.packageName = packageName;
- }
-
- @Override
- protected Type getType() {
- return Type.TABLE_PACKAGE;
- }
-
- @Override
- protected void writeHeader(ByteBuffer output) {
- output.putInt(id);
- PackageUtils.writePackageName(output, packageName);
- output.putInt(0); // typeStringsOffset. This value can't be computed here.
- output.putInt(lastPublicType);
- output.putInt(0); // keyStringsOffset. This value can't be computed here.
- output.putInt(lastPublicKey);
- output.putInt(typeIdOffset);
- }
-
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- int typeOffset = typeStringsOffset;
- int keyOffset = keyStringsOffset;
- int payloadOffset = 0;
- for (Chunk chunk : getChunks().values()) {
- if (chunk == getTypeStringPool()) {
- typeOffset = payloadOffset + getHeaderSize();
- } else if (chunk == getKeyStringPool()) {
- keyOffset = payloadOffset + getHeaderSize();
- }
- byte[] chunkBytes = chunk.toByteArray(shrink);
- output.write(chunkBytes);
- payloadOffset = writePad(output, chunkBytes.length);
- }
- header.putInt(TYPE_OFFSET_OFFSET, typeOffset);
- header.putInt(KEY_OFFSET_OFFSET, keyOffset);
- }
+ /**
+ * Offset in bytes, from the start of the chunk, where {@code typeStringsOffset} can be found.
+ */
+ private static final int TYPE_OFFSET_OFFSET = 268;
+
+ /**
+ * Offset in bytes, from the start of the chunk, where {@code keyStringsOffset} can be found.
+ */
+ private static final int KEY_OFFSET_OFFSET = 276;
+
+ /**
+ * The package id if this is a base package, or 0 if not a base package.
+ */
+ private int id;
+
+ /**
+ * The name of the package.
+ */
+ private String packageName;
+
+ /**
+ * The offset (from {@code offset}) in the original buffer where type strings start.
+ */
+ private final int typeStringsOffset;
+
+ /**
+ * The index into the type string pool of the last public type.
+ */
+ private final int lastPublicType;
+
+ /**
+ * An offset to the string pool that contains the key strings for this package.
+ */
+ private final int keyStringsOffset;
+
+ /**
+ * The index into the key string pool of the last public key.
+ */
+ private final int lastPublicKey;
+
+ /**
+ * An offset to the type ID(s). This is undocumented in the original code.
+ */
+ private final int typeIdOffset;
+
+ /**
+ * Contains a mapping of a type index to its {@link TypeSpecChunk}.
+ */
+ private final Map typeSpecs = new HashMap<>();
+
+ /**
+ * Contains a mapping of a type index to all of the {@link TypeChunk} with that index.
+ */
+ private final Multimap types = ArrayListMultimap.create();
+
+ /**
+ * May contain a library chunk for mapping dynamic references to resolved references.
+ */
+ private Optional libraryChunk = Optional.empty();
+
+ protected PackageChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ id = buffer.getInt();
+ packageName = PackageUtils.readPackageName(buffer, buffer.position());
+ typeStringsOffset = buffer.getInt();
+ lastPublicType = buffer.getInt();
+ keyStringsOffset = buffer.getInt();
+ lastPublicKey = buffer.getInt();
+ typeIdOffset = buffer.getInt();
+ }
+
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ for (Chunk chunk : getChunks().values()) {
+ if (chunk instanceof TypeChunk) {
+ TypeChunk typeChunk = (TypeChunk) chunk;
+ types.put(typeChunk.getId(), typeChunk);
+ } else if (chunk instanceof TypeSpecChunk) {
+ TypeSpecChunk typeSpecChunk = (TypeSpecChunk) chunk;
+ typeSpecs.put(typeSpecChunk.getId(), typeSpecChunk);
+ } else if (chunk instanceof LibraryChunk) {
+ if (libraryChunk.isPresent()) {
+ throw new IllegalStateException(
+ "Multiple library chunks present in package chunk.");
+ }
+ libraryChunk = Optional.of((LibraryChunk) chunk);
+ } else if (!(chunk instanceof StringPoolChunk)) {
+ throw new IllegalStateException(
+ String.format("PackageChunk contains an unexpected chunk: %s", chunk.getClass()));
+ }
+ }
+ }
+
+ /**
+ * Returns the package id if this is a base package, or 0 if not a base package.
+ */
+ public int getId() {
+ return id;
+ }
+
+ /**
+ * Sets the package id
+ */
+ public void setId(int id) {
+ this.id = id;
+ }
+
+ /**
+ * Returns the string pool that contains the names of the resources in this package.
+ */
+ public StringPoolChunk getKeyStringPool() {
+ Chunk chunk = Preconditions.checkNotNull(getChunks().get(keyStringsOffset + offset));
+ Preconditions.checkState(chunk instanceof StringPoolChunk, "Key string pool not found.");
+ return (StringPoolChunk) chunk;
+ }
+
+ /**
+ * Get the type string for a specific id, e.g., (e.g. string, attr, id).
+ *
+ * @param id The id to get the type for.
+ * @return The type string.
+ */
+ public String getTypeString(int id) {
+ StringPoolChunk typePool = getTypeStringPool();
+ Preconditions.checkNotNull(typePool, "Package has no type pool.");
+ Preconditions.checkState(typePool.getStyleCount() >= id, "No type for id: " + id);
+ return typePool.getString(id - 1); // - 1 here to convert to 0-based index
+ }
+
+ /**
+ * Returns the string pool that contains the type strings for this package, such as "layout",
+ * "string", "color".
+ */
+ public StringPoolChunk getTypeStringPool() {
+ Chunk chunk = Preconditions.checkNotNull(getChunks().get(typeStringsOffset + offset));
+ Preconditions.checkState(chunk instanceof StringPoolChunk, "Type string pool not found.");
+ return (StringPoolChunk) chunk;
+ }
+
+ /**
+ * Returns all {@link TypeChunk} in this package.
+ */
+ public Collection getTypeChunks() {
+ return types.values();
+ }
+
+ /**
+ * For a given type id, returns the {@link TypeChunk} objects that match that id. The type id is
+ * the 1-based index of the type in the type string pool (returned by {@link #getTypeStringPool}).
+ *
+ * @param id The 1-based type id to return {@link TypeChunk} objects for.
+ * @return The matching {@link TypeChunk} objects, or an empty collection if there are none.
+ */
+ public Collection getTypeChunks(int id) {
+ return types.get(id);
+ }
+
+ /**
+ * For a given type, returns the {@link TypeChunk} objects that match that type
+ * (e.g. "attr", "id", "string", ...).
+ *
+ * @param type The type to return {@link TypeChunk} objects for.
+ * @return The matching {@link TypeChunk} objects, or an empty collection if there are none.
+ */
+ public Collection getTypeChunks(String type) {
+ StringPoolChunk typeStringPool = Preconditions.checkNotNull(getTypeStringPool());
+ return getTypeChunks(typeStringPool.indexOf(type) + 1); // Convert 0-based index to 1-based
+ }
+
+ /**
+ * Returns all {@link TypeSpecChunk} in this package.
+ */
+ public Collection getTypeSpecChunks() {
+ return typeSpecs.values();
+ }
+
+ /**
+ * For a given (1-based) type id, returns the {@link TypeSpecChunk} matching it.
+ */
+ public TypeSpecChunk getTypeSpecChunk(int id) {
+ return Preconditions.checkNotNull(typeSpecs.get(id));
+ }
+
+ /**
+ * For a given {@code type}, returns the {@link TypeSpecChunk} that matches it
+ * (e.g. "attr", "id", "string", ...).
+ */
+ public TypeSpecChunk getTypeSpecChunk(String type) {
+ StringPoolChunk typeStringPool = Preconditions.checkNotNull(getTypeStringPool());
+ return getTypeSpecChunk(typeStringPool.indexOf(type) + 1); // Convert 0-based index to 1-based
+ }
+
+ /**
+ * Returns the name of this package.
+ */
+ public String getPackageName() {
+ return packageName;
+ }
+
+ /**
+ * Set the package name
+ */
+ public void setPackageName(String packageName) {
+ this.packageName = packageName;
+ }
+
+ @Override
+ protected Type getType() {
+ return Type.TABLE_PACKAGE;
+ }
+
+ @Override
+ protected void writeHeader(ByteBuffer output) {
+ output.putInt(id);
+ PackageUtils.writePackageName(output, packageName);
+ output.putInt(0); // typeStringsOffset. This value can't be computed here.
+ output.putInt(lastPublicType);
+ output.putInt(0); // keyStringsOffset. This value can't be computed here.
+ output.putInt(lastPublicKey);
+ output.putInt(typeIdOffset);
+ }
+
+ @Override
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ int typeOffset = typeStringsOffset;
+ int keyOffset = keyStringsOffset;
+ int payloadOffset = 0;
+ for (Chunk chunk : getChunks().values()) {
+ if (chunk == getTypeStringPool()) {
+ typeOffset = payloadOffset + getHeaderSize();
+ } else if (chunk == getKeyStringPool()) {
+ keyOffset = payloadOffset + getHeaderSize();
+ }
+ byte[] chunkBytes = chunk.toByteArray(shrink);
+ output.write(chunkBytes);
+ payloadOffset = writePad(output, chunkBytes.length);
+ }
+ header.putInt(TYPE_OFFSET_OFFSET, typeOffset);
+ header.putInt(KEY_OFFSET_OFFSET, keyOffset);
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageUtils.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageUtils.java
index 5077ca6f04..6c5215762f 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageUtils.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/PackageUtils.java
@@ -20,48 +20,53 @@
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
-/** Provides utility methods for package names. */
+/**
+ * Provides utility methods for package names.
+ */
public final class PackageUtils {
- public static final int PACKAGE_NAME_SIZE = 256;
+ public static final int PACKAGE_NAME_SIZE = 256;
- private PackageUtils() {} // Prevent instantiation
+ private PackageUtils() {
+ } // Prevent instantiation
- /**
- * Reads the package name from the buffer and repositions the buffer to point directly after
- * the package name.
- * @param buffer The buffer containing the package name.
- * @param offset The offset in the buffer to read from.
- * @return The package name.
- */
- public static String readPackageName(ByteBuffer buffer, int offset) {
- byte[] data = buffer.array();
- int length = 0;
- // Look for the null terminator for the string instead of using the entire buffer.
- // It's UTF-16 so check 2 bytes at a time to see if its double 0.
- for (int i = offset; i < data.length && i < PACKAGE_NAME_SIZE + offset; i += 2) {
- if (data[i] == 0 && data[i + 1] == 0) {
- length = i - offset;
- break;
- }
+ /**
+ * Reads the package name from the buffer and repositions the buffer to point directly after
+ * the package name.
+ *
+ * @param buffer The buffer containing the package name.
+ * @param offset The offset in the buffer to read from.
+ * @return The package name.
+ */
+ public static String readPackageName(ByteBuffer buffer, int offset) {
+ byte[] data = buffer.array();
+ int length = 0;
+ // Look for the null terminator for the string instead of using the entire buffer.
+ // It's UTF-16 so check 2 bytes at a time to see if its double 0.
+ for (int i = offset; i < data.length && i < PACKAGE_NAME_SIZE + offset; i += 2) {
+ if (data[i] == 0 && data[i + 1] == 0) {
+ length = i - offset;
+ break;
+ }
+ }
+ Charset utf16 = StandardCharsets.UTF_16LE;
+ String str = new String(data, offset, length, utf16);
+ buffer.position(offset + PACKAGE_NAME_SIZE);
+ return str;
}
- Charset utf16 = StandardCharsets.UTF_16LE;
- String str = new String(data, offset, length, utf16);
- buffer.position(offset + PACKAGE_NAME_SIZE);
- return str;
- }
- /**
- * Writes the provided package name to the buffer in UTF-16.
- * @param buffer The buffer that will be written to.
- * @param packageName The package name that will be written to the buffer.
- */
- public static void writePackageName(ByteBuffer buffer, String packageName) {
- byte[] nameBytes = packageName.getBytes(StandardCharsets.UTF_16LE);
- buffer.put(nameBytes, 0, Math.min(nameBytes.length, PACKAGE_NAME_SIZE));
- if (nameBytes.length < PACKAGE_NAME_SIZE) {
- // pad out the remaining space with an empty array.
- buffer.put(new byte[PACKAGE_NAME_SIZE - nameBytes.length]);
+ /**
+ * Writes the provided package name to the buffer in UTF-16.
+ *
+ * @param buffer The buffer that will be written to.
+ * @param packageName The package name that will be written to the buffer.
+ */
+ public static void writePackageName(ByteBuffer buffer, String packageName) {
+ byte[] nameBytes = packageName.getBytes(StandardCharsets.UTF_16LE);
+ buffer.put(nameBytes, 0, Math.min(nameBytes.length, PACKAGE_NAME_SIZE));
+ if (nameBytes.length < PACKAGE_NAME_SIZE) {
+ // pad out the remaining space with an empty array.
+ buffer.put(new byte[PACKAGE_NAME_SIZE - nameBytes.length]);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceEntryStatsCollector.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceEntryStatsCollector.java
index 5891f05675..e7d3895ef6 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceEntryStatsCollector.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceEntryStatsCollector.java
@@ -26,255 +26,276 @@
/**
* Calculates extra information about an {@link ResourceEntry}, such as the total
* APK size the entry is responsible for.
- *
+ *
* This class is not thread-safe.
*/
public class ResourceEntryStatsCollector {
- /** The size in bytes of an offset in a chunk. */
- private static final int OFFSET_SIZE = 4;
-
- /** The size in bytes of overhead for styles, if present, in {@link StringPoolChunk}. */
- private static final int STYLE_OVERHEAD = 8;
-
- /**
- * The number of bytes, in addition to the header, that the {@link PackageChunk} has in overhead
- * excluding the chunks it contains.
- */
- private static final int PACKAGE_CHUNK_OVERHEAD = 8;
-
- private final Map stats = new HashMap<>();
-
- private final ArscBlamer blamer;
-
- private final ResourceTableChunk resourceTable;
-
- /**
- * Creates a new {@link ResourceEntryStatsCollector}.
- *
- * @param blamer The blamer that maps resource entries to what they use.
- * @param resourceTable The resource table that {@code blamer} is blamed on.
- */
- public ResourceEntryStatsCollector(ArscBlamer blamer, ResourceTableChunk resourceTable) {
- this.resourceTable = resourceTable;
- this.blamer = blamer;
- }
-
- public void compute() throws IOException {
- Preconditions.checkState(stats.isEmpty(), "Must only call #compute once.");
- blamer.blame();
- computeStringPoolSizes();
- computePackageSizes();
- }
-
- /** Returns entries for which there are computed stats. Must first call {@link #compute}. */
- public Map getStats() {
- Preconditions.checkState(!stats.isEmpty(), "Must call #compute() first.");
- return Collections.unmodifiableMap(stats);
- }
-
- /** Returns computed stats for a given entry. Must first call {@link #compute}. */
- public ResourceStatistics getStats(ResourceEntry entry) {
- Preconditions.checkState(!stats.isEmpty(), "Must call #compute() first.");
- return stats.containsKey(entry) ? stats.get(entry) : ResourceStatistics.EMPTY;
- }
-
- private void computeStringPoolSizes() throws IOException {
- computePoolSizes(resourceTable.getStringPool(), blamer.getStringToBlamedResources());
- }
-
- private void computePackageSizes() throws IOException {
- computeTypePoolSizes();
- computeKeyPoolSizes();
- computeTypeSpecSizes();
- computeTypeChunkSizes();
- computePackageChunkSizes();
- }
-
- private void computeTypePoolSizes() throws IOException {
- for (Entry[]> entry
- : blamer.getTypeToBlamedResources().entrySet()) {
- computePoolSizes(entry.getKey().getTypeStringPool(), entry.getValue());
+ /**
+ * The size in bytes of an offset in a chunk.
+ */
+ private static final int OFFSET_SIZE = 4;
+
+ /**
+ * The size in bytes of overhead for styles, if present, in {@link StringPoolChunk}.
+ */
+ private static final int STYLE_OVERHEAD = 8;
+
+ /**
+ * The number of bytes, in addition to the header, that the {@link PackageChunk} has in overhead
+ * excluding the chunks it contains.
+ */
+ private static final int PACKAGE_CHUNK_OVERHEAD = 8;
+
+ private final Map stats = new HashMap<>();
+
+ private final ArscBlamer blamer;
+
+ private final ResourceTableChunk resourceTable;
+
+ /**
+ * Creates a new {@link ResourceEntryStatsCollector}.
+ *
+ * @param blamer The blamer that maps resource entries to what they use.
+ * @param resourceTable The resource table that {@code blamer} is blamed on.
+ */
+ public ResourceEntryStatsCollector(ArscBlamer blamer, ResourceTableChunk resourceTable) {
+ this.resourceTable = resourceTable;
+ this.blamer = blamer;
}
- }
- private void computeKeyPoolSizes() throws IOException {
- for (Entry[]> entry
- : blamer.getKeyToBlamedResources().entrySet()) {
- computePoolSizes(entry.getKey().getKeyStringPool(), entry.getValue());
+ public void compute() throws IOException {
+ Preconditions.checkState(stats.isEmpty(), "Must only call #compute once.");
+ blamer.blame();
+ computeStringPoolSizes();
+ computePackageSizes();
}
- }
- private void computeTypeSpecSizes() {
- for (Entry[]> entry
- : blamer.getTypeToBlamedResources().entrySet()) {
- computeTypeSpecSizes(entry.getKey(), entry.getValue());
- }
- }
-
- private void computeTypeChunkSizes() {
- for (Entry> entry
- : blamer.getTypeEntryToBlamedResources().asMap().entrySet()) {
- TypeChunk.Entry chunkEntry = entry.getKey();
- TypeChunk typeChunk = chunkEntry.parent();
- int size = chunkEntry.size() + OFFSET_SIZE;
- int count = typeChunk.getEntries().size();
- int nullEntries = typeChunk.getTotalEntryCount() - typeChunk.getEntries().size();
- int overhead = typeChunk.getHeaderSize() + nullEntries * OFFSET_SIZE;
- addSizes(entry.getValue(), overhead, size, count);
+ /**
+ * Returns entries for which there are computed stats. Must first call {@link #compute}.
+ */
+ public Map getStats() {
+ Preconditions.checkState(!stats.isEmpty(), "Must call #compute() first.");
+ return Collections.unmodifiableMap(stats);
}
- }
- private void computePackageChunkSizes() {
- for (Entry> entry
- : blamer.getPackageToBlamedResources().asMap().entrySet()) {
- int overhead = entry.getKey().getHeaderSize() + PACKAGE_CHUNK_OVERHEAD;
- addSizes(entry.getValue(), overhead, 0, 1);
+ /**
+ * Returns computed stats for a given entry. Must first call {@link #compute}.
+ */
+ public ResourceStatistics getStats(ResourceEntry entry) {
+ Preconditions.checkState(!stats.isEmpty(), "Must call #compute() first.");
+ return stats.containsKey(entry) ? stats.get(entry) : ResourceStatistics.EMPTY;
}
- }
- private void computePoolSizes(StringPoolChunk stringPool,
- List[] usages) throws IOException {
- int overhead = stringPool.getHeaderSize();
- if (stringPool.getStyleCount() > 0) {
- overhead += STYLE_OVERHEAD;
+ private void computeStringPoolSizes() throws IOException {
+ computePoolSizes(resourceTable.getStringPool(), blamer.getStringToBlamedResources());
}
- // We have to iterate over the indices of the string pool, because it is possible that there are
- // indices which have *no* associated resource entry (i.e. references from XML files without an
- // entry in R).
- int count = 0;
- for (int i = 0; i < usages.length; ++i) {
- if (usages[i].isEmpty()) {
- overhead += computeStringAndStyleSize(stringPool, i);
- } else {
- ++count;
- }
+ private void computePackageSizes() throws IOException {
+ computeTypePoolSizes();
+ computeKeyPoolSizes();
+ computeTypeSpecSizes();
+ computeTypeChunkSizes();
+ computePackageChunkSizes();
}
- // Now that we know the number of actual entries, we can compute the size.
- for (int i = 0; i < usages.length; ++i) {
- if (usages[i].isEmpty()) {
- continue;
- }
- int size = computeStringAndStyleSize(stringPool, i);
- addSizes(usages[i], overhead, size, count);
- }
- }
-
- private void computeTypeSpecSizes(PackageChunk packageChunk,
- List[] usages) {
- for (int i = 0; i < usages.length; ++i) {
- // The 1 here is to convert back to a 1-based index.
- TypeSpecChunk typeSpec = packageChunk.getTypeSpecChunk(i + 1);
- // TypeSpecChunk entries share everything equally.
- addSizes(usages[i], typeSpec.getOriginalChunkSize(), 0, 1);
+ private void computeTypePoolSizes() throws IOException {
+ for (Entry[]> entry
+ : blamer.getTypeToBlamedResources().entrySet()) {
+ computePoolSizes(entry.getKey().getTypeStringPool(), entry.getValue());
+ }
}
- }
-
- /**
- * Given an {@code index} into a {@code stringPool}, return string's total size in bytes plus its
- * style, if any.
- *
- * @param stringPool The string pool containing the {@code index}.
- * @param index The (0-based) index of the string and (optional) style.
- * @throws IOException Thrown if the style's length could not be computed.
- */
- private int computeStringAndStyleSize(StringPoolChunk stringPool, int index)
- throws IOException {
- return computeStringSize(stringPool, index) + computeStyleSize(stringPool, index);
- }
-
- /** Given an {@code index} into a {@code stringPool}, return string's total size in bytes. */
- private int computeStringSize(StringPoolChunk stringPool, int index) {
- String string = stringPool.getString(index);
- int result = BinaryResourceString.encodeString(string, stringPool.getStringType()).length;
- result += OFFSET_SIZE;
- return result;
- }
-
- /**
- * Given an {@code index} into a {@code stringPool}, return style's total size in bytes or 0 if
- * there's no style at that index.
- *
- * @throws IOException Thrown if the style's length could not be computed.
- */
- private int computeStyleSize(StringPoolChunk stringPool, int index) throws IOException {
- if (index >= stringPool.getStyleCount()) { // No style at index
- return 0;
- }
- return stringPool.getStyle(index).toByteArray().length + OFFSET_SIZE;
- }
-
- /**
- * Adds to the {@link #stats} of {@code entries} that reference a value in a chunk the bytes it's
- * responsible for. This should only be called once per chunk-value pair.
- *
- * @param entries The resource entries referencing a single value in a chunk.
- * @param overhead The number of bytes of overhead of a chunk. Typically the header size.
- * @param size The size in bytes of a value in a chunk that {@code entries} reference.
- * @param count The total number of values in the chunk.
- */
- private void addSizes(Collection entries, int overhead, int size, int count) {
- int usageCount = entries.size();
- for (ResourceEntry resourceEntry : entries) {
- // TODO(acornwall): Replace with Java 8's #getOrDefault when possible.
- if (!stats.containsKey(resourceEntry)) {
- stats.put(resourceEntry, new ResourceStatistics());
- }
- ResourceStatistics resourceStats = stats.get(resourceEntry);
- if (usageCount == 1) {
- resourceStats.addPrivateSize(size);
- } else {
- resourceStats.addSharedSize(size);
- }
- // Special case: If the chunk only has one relevant value, removing this entry will remove the
- // entire chunk.
- if (usageCount == 1 && count == 1) {
- resourceStats.addPrivateSize(overhead);
- }
- resourceStats.addProportionalSize(size, usageCount);
- resourceStats.addProportionalSize(overhead, usageCount * count);
+
+ private void computeKeyPoolSizes() throws IOException {
+ for (Entry[]> entry
+ : blamer.getKeyToBlamedResources().entrySet()) {
+ computePoolSizes(entry.getKey().getKeyStringPool(), entry.getValue());
+ }
}
- }
- /** Stats for an individual {@link ResourceEntry}. */
- public static class ResourceStatistics {
+ private void computeTypeSpecSizes() {
+ for (Entry[]> entry
+ : blamer.getTypeToBlamedResources().entrySet()) {
+ computeTypeSpecSizes(entry.getKey(), entry.getValue());
+ }
+ }
- /** The empty, immutable instance of ResourceStatistics which contains 0 for all values. */
- public static final ResourceStatistics EMPTY = new ResourceStatistics();
+ private void computeTypeChunkSizes() {
+ for (Entry> entry
+ : blamer.getTypeEntryToBlamedResources().asMap().entrySet()) {
+ TypeChunk.Entry chunkEntry = entry.getKey();
+ TypeChunk typeChunk = chunkEntry.parent();
+ int size = chunkEntry.size() + OFFSET_SIZE;
+ int count = typeChunk.getEntries().size();
+ int nullEntries = typeChunk.getTotalEntryCount() - typeChunk.getEntries().size();
+ int overhead = typeChunk.getHeaderSize() + nullEntries * OFFSET_SIZE;
+ addSizes(entry.getValue(), overhead, size, count);
+ }
+ }
- private int privateSize = 0;
- private int sharedSize = 0;
- private double proportionalSize = 0;
+ private void computePackageChunkSizes() {
+ for (Entry> entry
+ : blamer.getPackageToBlamedResources().asMap().entrySet()) {
+ int overhead = entry.getKey().getHeaderSize() + PACKAGE_CHUNK_OVERHEAD;
+ addSizes(entry.getValue(), overhead, 0, 1);
+ }
+ }
- private ResourceStatistics() {}
+ private void computePoolSizes(StringPoolChunk stringPool,
+ List[] usages) throws IOException {
+ int overhead = stringPool.getHeaderSize();
+ if (stringPool.getStyleCount() > 0) {
+ overhead += STYLE_OVERHEAD;
+ }
+
+ // We have to iterate over the indices of the string pool, because it is possible that there are
+ // indices which have *no* associated resource entry (i.e. references from XML files without an
+ // entry in R).
+ int count = 0;
+ for (int i = 0; i < usages.length; ++i) {
+ if (usages[i].isEmpty()) {
+ overhead += computeStringAndStyleSize(stringPool, i);
+ } else {
+ ++count;
+ }
+ }
+
+ // Now that we know the number of actual entries, we can compute the size.
+ for (int i = 0; i < usages.length; ++i) {
+ if (usages[i].isEmpty()) {
+ continue;
+ }
+ int size = computeStringAndStyleSize(stringPool, i);
+ addSizes(usages[i], overhead, size, count);
+ }
+ }
- /** The number of bytes that would be freed if this resource was removed. */
- public int getPrivateSize() {
- return privateSize;
+ private void computeTypeSpecSizes(PackageChunk packageChunk,
+ List[] usages) {
+ for (int i = 0; i < usages.length; ++i) {
+ // The 1 here is to convert back to a 1-based index.
+ TypeSpecChunk typeSpec = packageChunk.getTypeSpecChunk(i + 1);
+ // TypeSpecChunk entries share everything equally.
+ addSizes(usages[i], typeSpec.getOriginalChunkSize(), 0, 1);
+ }
}
- /** The number of bytes taken up by this resource that are also shared with other resources. */
- public int getSharedSize() {
- return sharedSize;
+ /**
+ * Given an {@code index} into a {@code stringPool}, return string's total size in bytes plus its
+ * style, if any.
+ *
+ * @param stringPool The string pool containing the {@code index}.
+ * @param index The (0-based) index of the string and (optional) style.
+ * @throws IOException Thrown if the style's length could not be computed.
+ */
+ private int computeStringAndStyleSize(StringPoolChunk stringPool, int index)
+ throws IOException {
+ return computeStringSize(stringPool, index) + computeStyleSize(stringPool, index);
}
- /** The total size this resource is responsible for. */
- public double getProportionalSize() {
- return proportionalSize;
+ /**
+ * Given an {@code index} into a {@code stringPool}, return string's total size in bytes.
+ */
+ private int computeStringSize(StringPoolChunk stringPool, int index) {
+ String string = stringPool.getString(index);
+ int result = BinaryResourceString.encodeString(string, stringPool.getStringType()).length;
+ result += OFFSET_SIZE;
+ return result;
}
- private void addPrivateSize(int privateSize) {
- this.privateSize += privateSize;
+ /**
+ * Given an {@code index} into a {@code stringPool}, return style's total size in bytes or 0 if
+ * there's no style at that index.
+ *
+ * @throws IOException Thrown if the style's length could not be computed.
+ */
+ private int computeStyleSize(StringPoolChunk stringPool, int index) throws IOException {
+ if (index >= stringPool.getStyleCount()) { // No style at index
+ return 0;
+ }
+ return stringPool.getStyle(index).toByteArray().length + OFFSET_SIZE;
}
- private void addSharedSize(int sharedSize) {
- this.sharedSize += sharedSize;
+ /**
+ * Adds to the {@link #stats} of {@code entries} that reference a value in a chunk the bytes it's
+ * responsible for. This should only be called once per chunk-value pair.
+ *
+ * @param entries The resource entries referencing a single value in a chunk.
+ * @param overhead The number of bytes of overhead of a chunk. Typically the header size.
+ * @param size The size in bytes of a value in a chunk that {@code entries} reference.
+ * @param count The total number of values in the chunk.
+ */
+ private void addSizes(Collection entries, int overhead, int size, int count) {
+ int usageCount = entries.size();
+ for (ResourceEntry resourceEntry : entries) {
+ // TODO(acornwall): Replace with Java 8's #getOrDefault when possible.
+ if (!stats.containsKey(resourceEntry)) {
+ stats.put(resourceEntry, new ResourceStatistics());
+ }
+ ResourceStatistics resourceStats = stats.get(resourceEntry);
+ if (usageCount == 1) {
+ resourceStats.addPrivateSize(size);
+ } else {
+ resourceStats.addSharedSize(size);
+ }
+ // Special case: If the chunk only has one relevant value, removing this entry will remove the
+ // entire chunk.
+ if (usageCount == 1 && count == 1) {
+ resourceStats.addPrivateSize(overhead);
+ }
+ resourceStats.addProportionalSize(size, usageCount);
+ resourceStats.addProportionalSize(overhead, usageCount * count);
+ }
}
- private void addProportionalSize(int numerator, int denominator) {
- this.proportionalSize += 1.0 * numerator / denominator;
+ /**
+ * Stats for an individual {@link ResourceEntry}.
+ */
+ public static class ResourceStatistics {
+
+ /**
+ * The empty, immutable instance of ResourceStatistics which contains 0 for all values.
+ */
+ public static final ResourceStatistics EMPTY = new ResourceStatistics();
+
+ private int privateSize = 0;
+ private int sharedSize = 0;
+ private double proportionalSize = 0;
+
+ private ResourceStatistics() {
+ }
+
+ /**
+ * The number of bytes that would be freed if this resource was removed.
+ */
+ public int getPrivateSize() {
+ return privateSize;
+ }
+
+ /**
+ * The number of bytes taken up by this resource that are also shared with other resources.
+ */
+ public int getSharedSize() {
+ return sharedSize;
+ }
+
+ /**
+ * The total size this resource is responsible for.
+ */
+ public double getProportionalSize() {
+ return proportionalSize;
+ }
+
+ private void addPrivateSize(int privateSize) {
+ this.privateSize += privateSize;
+ }
+
+ private void addSharedSize(int sharedSize) {
+ this.sharedSize += sharedSize;
+ }
+
+ private void addProportionalSize(int numerator, int denominator) {
+ this.proportionalSize += 1.0 * numerator / denominator;
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceTableChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceTableChunk.java
index 8752b90549..b6dffd6609 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceTableChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/ResourceTableChunk.java
@@ -16,9 +16,9 @@
package com.google.devrel.gmscore.tools.apk.arsc;
+import androidx.annotation.Nullable;
import com.google.common.base.Preconditions;
-import androidx.annotation.Nullable;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.Collections;
@@ -36,57 +36,67 @@
*/
public final class ResourceTableChunk extends ChunkWithChunks {
- /** A string pool containing all string resource values in the entire resource table. */
- private StringPoolChunk stringPool;
+ /**
+ * A string pool containing all string resource values in the entire resource table.
+ */
+ private StringPoolChunk stringPool;
- /** The packages contained in this resource table. */
- private final Map packages = new HashMap<>();
+ /**
+ * The packages contained in this resource table.
+ */
+ private final Map packages = new HashMap<>();
- protected ResourceTableChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- // packageCount. We ignore this, because we already know how many chunks we have.
- Preconditions.checkState(buffer.getInt() >= 1, "ResourceTableChunk package count was < 1.");
- }
+ protected ResourceTableChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ // packageCount. We ignore this, because we already know how many chunks we have.
+ Preconditions.checkState(buffer.getInt() >= 1, "ResourceTableChunk package count was < 1.");
+ }
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- packages.clear();
- for (Chunk chunk : getChunks().values()) {
- if (chunk instanceof PackageChunk) {
- PackageChunk packageChunk = (PackageChunk) chunk;
- packages.put(packageChunk.getPackageName(), packageChunk);
- } else if (chunk instanceof StringPoolChunk) {
- stringPool = (StringPoolChunk) chunk;
- }
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ packages.clear();
+ for (Chunk chunk : getChunks().values()) {
+ if (chunk instanceof PackageChunk) {
+ PackageChunk packageChunk = (PackageChunk) chunk;
+ packages.put(packageChunk.getPackageName(), packageChunk);
+ } else if (chunk instanceof StringPoolChunk) {
+ stringPool = (StringPoolChunk) chunk;
+ }
+ }
+ Preconditions.checkNotNull(stringPool, "ResourceTableChunk must have a string pool.");
}
- Preconditions.checkNotNull(stringPool, "ResourceTableChunk must have a string pool.");
- }
- /** Returns the string pool containing all string resource values in the resource table. */
- public StringPoolChunk getStringPool() {
- return stringPool;
- }
+ /**
+ * Returns the string pool containing all string resource values in the resource table.
+ */
+ public StringPoolChunk getStringPool() {
+ return stringPool;
+ }
- /** Returns the package with the given {@code packageName}. Else, returns null. */
- @Nullable
- public PackageChunk getPackage(String packageName) {
- return packages.get(packageName);
- }
+ /**
+ * Returns the package with the given {@code packageName}. Else, returns null.
+ */
+ @Nullable
+ public PackageChunk getPackage(String packageName) {
+ return packages.get(packageName);
+ }
- /** Returns the packages contained in this resource table. */
- public Collection getPackages() {
- return Collections.unmodifiableCollection(packages.values());
- }
+ /**
+ * Returns the packages contained in this resource table.
+ */
+ public Collection getPackages() {
+ return Collections.unmodifiableCollection(packages.values());
+ }
- @Override
- protected Type getType() {
- return Type.TABLE;
- }
+ @Override
+ protected Type getType() {
+ return Type.TABLE;
+ }
- @Override
- protected void writeHeader(ByteBuffer output) {
- super.writeHeader(output);
- output.putInt(packages.size());
- }
+ @Override
+ protected void writeHeader(ByteBuffer output) {
+ super.writeHeader(output);
+ output.putInt(packages.size());
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/SerializableResource.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/SerializableResource.java
index b9c62a1d5f..d099defe4c 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/SerializableResource.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/SerializableResource.java
@@ -22,20 +22,22 @@
* A resource, typically a @{link Chunk}, that can be converted to an array of bytes.
*/
public interface SerializableResource {
-
- /**
- * Converts this resource into an array of bytes representation.
- * @return An array of bytes representing this resource.
- * @throws IOException
- */
- byte[] toByteArray() throws IOException;
- /**
- * Converts this resource into an array of bytes representation.
- * @param shrink True if, when converting to a byte array, this resource can modify the returned
- * bytes in an effort to reduce the size.
- * @return An array of bytes representing this resource.
- * @throws IOException
- */
- byte[] toByteArray(boolean shrink) throws IOException;
+ /**
+ * Converts this resource into an array of bytes representation.
+ *
+ * @return An array of bytes representing this resource.
+ * @throws IOException
+ */
+ byte[] toByteArray() throws IOException;
+
+ /**
+ * Converts this resource into an array of bytes representation.
+ *
+ * @param shrink True if, when converting to a byte array, this resource can modify the returned
+ * bytes in an effort to reduce the size.
+ * @return An array of bytes representing this resource.
+ * @throws IOException
+ */
+ byte[] toByteArray(boolean shrink) throws IOException;
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/StringPoolChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/StringPoolChunk.java
index c0c73efaa1..28b7f59074 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/StringPoolChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/StringPoolChunk.java
@@ -16,11 +16,11 @@
package com.google.devrel.gmscore.tools.apk.arsc;
+import androidx.annotation.Nullable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.io.LittleEndianDataOutputStream;
-import androidx.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.IOException;
@@ -28,396 +28,416 @@
import java.nio.ByteOrder;
import java.util.*;
-/** Represents a string pool structure. */
+/**
+ * Represents a string pool structure.
+ */
public final class StringPoolChunk extends Chunk {
- // These are the defined flags for the "flags" field of ResourceStringPoolHeader
- private static final int SORTED_FLAG = 1 << 0;
- private static final int UTF8_FLAG = 1 << 8;
-
- /** The offset from the start of the header that the stylesStart field is at. */
- private static final int STYLE_START_OFFSET = 24;
-
- /** Flags. */
- private final int flags;
-
- /** Index from header of the string data. */
- private final int stringsStart;
-
- /** Index from header of the style data. */
- private final int stylesStart;
-
- /**
- * Number of strings in the original buffer. This is not necessarily the number of strings in
- * {@code strings}.
- */
- private final int stringCount;
-
- /**
- * Number of styles in the original buffer. This is not necessarily the number of styles in
- * {@code styles}.
- */
- private final int styleCount;
-
- /**
- * The strings ordered as they appear in the arsc file. e.g. strings.get(1234) gets the 1235th
- * string in the arsc file.
- */
- private final List strings = new ArrayList<>();
-
- /**
- * These styles have a 1:1 relationship with the strings. For example, styles.get(3) refers to
- * the string at location strings.get(3). There are never more styles than strings (though there
- * may be less). Inside of that are all of the styles referenced by that string.
- */
- private final List styles = new ArrayList<>();
-
- /**
- * True if the original {@link StringPoolChunk} shows signs of being deduped. Specifically, this
- * is set to true if there exists a string whose offset is <= the previous offset. This is used to
- * preserve the deduping of strings for pools that have been deduped.
- */
- private boolean isOriginalDeduped = false;
-
- protected StringPoolChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- stringCount = buffer.getInt();
- styleCount = buffer.getInt();
- flags = buffer.getInt();
- stringsStart = buffer.getInt();
- stylesStart = buffer.getInt();
- }
-
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- strings.addAll(readStrings(buffer, offset + stringsStart, stringCount));
- styles.addAll(readStyles(buffer, offset + stylesStart, styleCount));
- }
-
- /**
- * Returns the 0-based index of the first occurrence of the given string, or -1 if the string is
- * not in the pool. This runs in O(n) time.
- *
- * @param string The string to check the pool for.
- * @return Index of the string, or -1 if not found.
- */
- public int indexOf(String string) {
- return strings.indexOf(string);
- }
-
- /**
- * Returns a string at the given (0-based) index.
- *
- * @param index The (0-based) index of the string to return.
- * @throws IndexOutOfBoundsException If the index is out of range (index < 0 || index >= size()).
- */
- public String getString(int index) {
- return strings.get(index);
- }
-
- public void updateString(int index, String value){
- strings.set(index, value);
- }
-
- /** Returns the number of strings in this pool. */
- public int getStringCount() {
- return strings.size();
- }
-
- /**
- * Returns a style at the given (0-based) index.
- *
- * @param index The (0-based) index of the style to return.
- * @throws IndexOutOfBoundsException If the index is out of range (index < 0 || index >= size()).
- */
- public StringPoolStyle getStyle(int index) {
- return styles.get(index);
- }
-
- /** Returns the number of styles in this pool. */
- public int getStyleCount() {
- return styles.size();
- }
-
- /** Returns the type of strings in this pool. */
- public BinaryResourceString.Type getStringType() {
- return isUTF8() ? BinaryResourceString.Type.UTF8 : BinaryResourceString.Type.UTF16;
- }
-
- @Override
- protected Type getType() {
- return Type.STRING_POOL;
- }
-
- /** Returns the number of bytes needed for offsets based on {@code strings} and {@code styles}. */
- private int getOffsetSize() {
- return (strings.size() + styles.size()) * 4;
- }
-
- /**
- * True if this string pool contains strings in UTF-8 format. Otherwise, strings are in UTF-16.
- *
- * @return true if @{code strings} are in UTF-8; false if they're in UTF-16.
- */
- public boolean isUTF8() {
- return (flags & UTF8_FLAG) != 0;
- }
-
- /**
- * True if this string pool contains already-sorted strings.
- *
- * @return true if @{code strings} are sorted.
- */
- public boolean isSorted() {
- return (flags & SORTED_FLAG) != 0;
- }
-
- private List readStrings(ByteBuffer buffer, int offset, int count) {
- List result = new ArrayList<>();
- int previousOffset = -1;
- // After the header, we now have an array of offsets for the strings in this pool.
- for (int i = 0; i < count; ++i) {
- int stringOffset = offset + buffer.getInt();
- result.add(BinaryResourceString.decodeString(buffer, stringOffset, getStringType()));
- if (stringOffset <= previousOffset) {
- isOriginalDeduped = true;
- }
- previousOffset = stringOffset;
- }
- return result;
- }
-
- private List readStyles(ByteBuffer buffer, int offset, int count) {
- List result = new ArrayList<>();
- // After the array of offsets for the strings in the pool, we have an offset for the styles
- // in this pool.
- for (int i = 0; i < count; ++i) {
- int styleOffset = offset + buffer.getInt();
- result.add(StringPoolStyle.create(buffer, styleOffset, this));
+ // These are the defined flags for the "flags" field of ResourceStringPoolHeader
+ private static final int SORTED_FLAG = 1 << 0;
+ private static final int UTF8_FLAG = 1 << 8;
+
+ /**
+ * The offset from the start of the header that the stylesStart field is at.
+ */
+ private static final int STYLE_START_OFFSET = 24;
+
+ /**
+ * Flags.
+ */
+ private final int flags;
+
+ /**
+ * Index from header of the string data.
+ */
+ private final int stringsStart;
+
+ /**
+ * Index from header of the style data.
+ */
+ private final int stylesStart;
+
+ /**
+ * Number of strings in the original buffer. This is not necessarily the number of strings in
+ * {@code strings}.
+ */
+ private final int stringCount;
+
+ /**
+ * Number of styles in the original buffer. This is not necessarily the number of styles in
+ * {@code styles}.
+ */
+ private final int styleCount;
+
+ /**
+ * The strings ordered as they appear in the arsc file. e.g. strings.get(1234) gets the 1235th
+ * string in the arsc file.
+ */
+ private final List strings = new ArrayList<>();
+
+ /**
+ * These styles have a 1:1 relationship with the strings. For example, styles.get(3) refers to
+ * the string at location strings.get(3). There are never more styles than strings (though there
+ * may be less). Inside of that are all of the styles referenced by that string.
+ */
+ private final List styles = new ArrayList<>();
+
+ /**
+ * True if the original {@link StringPoolChunk} shows signs of being deduped. Specifically, this
+ * is set to true if there exists a string whose offset is <= the previous offset. This is used to
+ * preserve the deduping of strings for pools that have been deduped.
+ */
+ private boolean isOriginalDeduped = false;
+
+ protected StringPoolChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ stringCount = buffer.getInt();
+ styleCount = buffer.getInt();
+ flags = buffer.getInt();
+ stringsStart = buffer.getInt();
+ stylesStart = buffer.getInt();
}
- return result;
- }
-
- private int writeStrings(DataOutput payload, ByteBuffer offsets, boolean shrink)
- throws IOException {
- int stringOffset = 0;
- Map used = new HashMap<>(); // Keeps track of strings already written
- for (String string : strings) {
- // Dedupe everything except stylized strings, unless shrink is true (then dedupe everything)
- if (used.containsKey(string) && (shrink || isOriginalDeduped)) {
- Integer offset = used.get(string);
- offsets.putInt(offset == null ? 0 : offset);
- } else {
- byte[] encodedString = BinaryResourceString.encodeString(string, getStringType());
- payload.write(encodedString);
- used.put(string, stringOffset);
- offsets.putInt(stringOffset);
- stringOffset += encodedString.length;
- }
+
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ strings.addAll(readStrings(buffer, offset + stringsStart, stringCount));
+ styles.addAll(readStyles(buffer, offset + stylesStart, styleCount));
}
- // ARSC files pad to a 4-byte boundary. We should do so too.
- stringOffset = writePad(payload, stringOffset);
- return stringOffset;
- }
+ /**
+ * Returns the 0-based index of the first occurrence of the given string, or -1 if the string is
+ * not in the pool. This runs in O(n) time.
+ *
+ * @param string The string to check the pool for.
+ * @return Index of the string, or -1 if not found.
+ */
+ public int indexOf(String string) {
+ return strings.indexOf(string);
+ }
- private int writeStyles(DataOutput payload, ByteBuffer offsets, boolean shrink)
- throws IOException {
- int styleOffset = 0;
- if (!styles.isEmpty()) {
- Map used = new HashMap<>(); // Keeps track of bytes already written
- for (StringPoolStyle style : styles) {
- if (!used.containsKey(style) || !shrink) {
- byte[] encodedStyle = style.toByteArray(shrink);
- payload.write(encodedStyle);
- used.put(style, styleOffset);
- offsets.putInt(styleOffset);
- styleOffset += encodedStyle.length;
- } else { // contains key and shrink is true
- Integer offset = used.get(style);
- offsets.putInt(offset == null ? 0 : offset);
- }
- }
- // The end of the spans are terminated with another sentinel value
- payload.writeInt(StringPoolStyle.RES_STRING_POOL_SPAN_END);
- styleOffset += 4;
- // TODO(acornwall): There appears to be an extra SPAN_END here... why?
- payload.writeInt(StringPoolStyle.RES_STRING_POOL_SPAN_END);
- styleOffset += 4;
-
- styleOffset = writePad(payload, styleOffset);
+ /**
+ * Returns a string at the given (0-based) index.
+ *
+ * @param index The (0-based) index of the string to return.
+ * @throws IndexOutOfBoundsException If the index is out of range (index < 0 || index >= size()).
+ */
+ public String getString(int index) {
+ return strings.get(index);
}
- return styleOffset;
- }
-
- @Override
- protected void writeHeader(ByteBuffer output) {
- int stringsStart = getHeaderSize() + getOffsetSize();
- output.putInt(strings.size());
- output.putInt(styles.size());
- output.putInt(flags);
- output.putInt(strings.isEmpty() ? 0 : stringsStart);
- output.putInt(0); // Placeholder. The styles starting offset cannot be computed at this point.
- }
-
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- int stringOffset = 0;
- ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize());
- offsets.order(ByteOrder.LITTLE_ENDIAN);
-
- // Write to a temporary payload so we can rearrange this and put the offsets first
- try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
- stringOffset = writeStrings(payload, offsets, shrink);
- writeStyles(payload, offsets, shrink);
+
+ public void updateString(int index, String value) {
+ strings.set(index, value);
}
- output.write(offsets.array());
- output.write(baos.toByteArray());
- if (!styles.isEmpty()) {
- header.putInt(STYLE_START_OFFSET, getHeaderSize() + getOffsetSize() + stringOffset);
+ /**
+ * Returns the number of strings in this pool.
+ */
+ public int getStringCount() {
+ return strings.size();
}
- }
-
- /**
- * Represents all of the styles for a particular string. The string is determined by its index
- * in {@link StringPoolChunk}.
- */
- public static class StringPoolStyle implements SerializableResource {
-
- // Styles are a list of integers with 0xFFFFFFFF serving as a sentinel value.
- static final int RES_STRING_POOL_SPAN_END = 0xFFFFFFFF;
- private final List spans;
-
- static StringPoolStyle create(ByteBuffer buffer, int offset, StringPoolChunk parent) {
- Builder spans = ImmutableList.builder();
- int nameIndex = buffer.getInt(offset);
- while (nameIndex != RES_STRING_POOL_SPAN_END) {
- spans.add(StringPoolSpan.create(buffer, offset, parent));
- offset += StringPoolSpan.SPAN_LENGTH;
- nameIndex = buffer.getInt(offset);
- }
- return new StringPoolStyle(spans.build());
+
+ /**
+ * Returns a style at the given (0-based) index.
+ *
+ * @param index The (0-based) index of the style to return.
+ * @throws IndexOutOfBoundsException If the index is out of range (index < 0 || index >= size()).
+ */
+ public StringPoolStyle getStyle(int index) {
+ return styles.get(index);
}
- private StringPoolStyle(List spans) {
- this.spans = spans;
+ /**
+ * Returns the number of styles in this pool.
+ */
+ public int getStyleCount() {
+ return styles.size();
}
- @Override
- public byte[] toByteArray() throws IOException {
- return toByteArray(false);
+ /**
+ * Returns the type of strings in this pool.
+ */
+ public BinaryResourceString.Type getStringType() {
+ return isUTF8() ? BinaryResourceString.Type.UTF8 : BinaryResourceString.Type.UTF16;
}
@Override
- public byte[] toByteArray(boolean shrink) throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
- for (StringPoolSpan span : spans) {
- byte[] encodedSpan = span.toByteArray(shrink);
- if (encodedSpan.length != StringPoolSpan.SPAN_LENGTH) {
- throw new IllegalStateException("Encountered a span of invalid length.");
- }
- payload.write(encodedSpan);
- }
- payload.writeInt(RES_STRING_POOL_SPAN_END);
- }
-
- return baos.toByteArray();
+ protected Type getType() {
+ return Type.STRING_POOL;
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- StringPoolStyle that = (StringPoolStyle)o;
- return Objects.equals(spans, that.spans);
+ /**
+ * Returns the number of bytes needed for offsets based on {@code strings} and {@code styles}.
+ */
+ private int getOffsetSize() {
+ return (strings.size() + styles.size()) * 4;
}
- @Override
- public int hashCode() {
- return Objects.hash(spans);
+ /**
+ * True if this string pool contains strings in UTF-8 format. Otherwise, strings are in UTF-16.
+ *
+ * @return true if @{code strings} are in UTF-8; false if they're in UTF-16.
+ */
+ public boolean isUTF8() {
+ return (flags & UTF8_FLAG) != 0;
}
/**
- * Returns a brief description of the contents of this style. The representation of this
- * information is subject to change, but below is a typical example:
+ * True if this string pool contains already-sorted strings.
*
- *
+ */
+ @Override
+ public String toString() {
+ return String.format("StringPoolStyle{spans=%s}", spans);
+ }
+ }
+
+ /**
+ * Represents a styled span associated with a specific string.
+ */
+ private static class StringPoolSpan implements SerializableResource {
+ static final int SPAN_LENGTH = 12;
+
+ private final int nameIndex;
+ private final int start;
+ private final int stop;
+ private final StringPoolChunk parent;
+
+ static StringPoolSpan create(ByteBuffer buffer, int offset, StringPoolChunk parent) {
+ int nameIndex = buffer.getInt(offset);
+ int start = buffer.getInt(offset + 4);
+ int stop = buffer.getInt(offset + 8);
+ return new StringPoolSpan(nameIndex, start, stop, parent);
+ }
+
+ private StringPoolSpan(int nameIndex, int start, int stop, StringPoolChunk parent) {
+ this.nameIndex = nameIndex;
+ this.start = start;
+ this.stop = stop;
+ this.parent = parent;
+ }
+
+ @Override
+ public final byte[] toByteArray() {
+ return toByteArray(false);
+ }
+
+ @Override
+ public final byte[] toByteArray(boolean shrink) {
+ ByteBuffer buffer = ByteBuffer.allocate(SPAN_LENGTH).order(ByteOrder.LITTLE_ENDIAN);
+ buffer.putInt(nameIndex);
+ buffer.putInt(start);
+ buffer.putInt(stop);
+ return buffer.array();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ StringPoolSpan that = (StringPoolSpan) o;
+ return nameIndex == that.nameIndex &&
+ start == that.start &&
+ stop == that.stop &&
+ Objects.equals(parent, that.parent);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(nameIndex, start, stop, parent);
+ }
+
+ /**
+ * Returns a brief description of this span. The representation of this information is subject
+ * to change, but below is a typical example:
+ *
+ *
"StringPoolSpan{foo, start=0, stop=5}"
+ */
+ @Override
+ public String toString() {
+ return String.format("StringPoolSpan{%s, start=%d, stop=%d}",
+ parent.getString(nameIndex), start, stop);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeChunk.java
index 9a98ed54c8..1030df262b 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeChunk.java
@@ -16,11 +16,11 @@
package com.google.devrel.gmscore.tools.apk.arsc;
+import androidx.annotation.Nullable;
import com.google.common.base.Preconditions;
import com.google.common.io.LittleEndianDataOutputStream;
import com.google.common.primitives.UnsignedBytes;
-import androidx.annotation.Nullable;
import java.io.ByteArrayOutputStream;
import java.io.DataOutput;
import java.io.IOException;
@@ -38,412 +38,485 @@
*/
public final class TypeChunk extends Chunk {
- /** The type identifier of the resource type this chunk is holding. */
- private final int id;
-
- /** The number of resources of this type at creation time. */
- private final int entryCount;
-
- /** The offset (from {@code offset}) in the original buffer where {@code entries} start. */
- private final int entriesStart;
-
- /** The resource configuration that these resource entries correspond to. */
- private BinaryResourceConfiguration configuration;
-
- /** A sparse list of resource entries defined by this chunk. */
- private final Map entries = new TreeMap<>();
-
- protected TypeChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- id = UnsignedBytes.toInt(buffer.get());
- buffer.position(buffer.position() + 3); // Skip 3 bytes for packing
- entryCount = buffer.getInt();
- entriesStart = buffer.getInt();
- configuration = BinaryResourceConfiguration.create(buffer);
- }
-
- @Override
- protected void init(ByteBuffer buffer) {
- int offset = this.offset + entriesStart;
- for (int i = 0; i < entryCount; ++i) {
- Entry entry = Entry.create(buffer, offset, this);
- if (entry != null) {
- entries.put(i, entry);
- }
- }
- }
-
- /** Returns the (1-based) type id of the resource types that this {@link TypeChunk} is holding. */
- public int getId() {
- return id;
- }
-
- /** Returns the name of the type this chunk represents (e.g. string, attr, id). */
- public String getTypeName() {
- PackageChunk packageChunk = getPackageChunk();
- Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
- StringPoolChunk typePool = packageChunk.getTypeStringPool();
- Preconditions.checkNotNull(typePool, "%s's parent package has no type pool.", getClass());
- return typePool.getString(getId() - 1); // - 1 here to convert to 0-based index
- }
-
- /** Returns the resource configuration that these resource entries correspond to. */
- public BinaryResourceConfiguration getConfiguration() {
- return configuration;
- }
-
- /**
- * Sets the resource configuration that this chunk's entries correspond to.
- *
- * @param configuration The new configuration.
- */
- public void setConfiguration(BinaryResourceConfiguration configuration) {
- this.configuration = configuration;
- }
-
- /** Returns the total number of entries for this type + configuration, including null entries. */
- public int getTotalEntryCount() {
- return entryCount;
- }
-
- /** Returns a sparse list of 0-based indices to resource entries defined by this chunk. */
- public Map getEntries() {
- return Collections.unmodifiableMap(entries);
- }
-
- /** Returns true if this chunk contains an entry for {@code resourceId}. */
- public boolean containsResource(BinaryResourceIdentifier resourceId) {
- PackageChunk packageChunk = Preconditions.checkNotNull(getPackageChunk());
- int packageId = packageChunk.getId();
- int typeId = getId();
- return resourceId.packageId() == packageId
- && resourceId.typeId() == typeId
- && entries.containsKey(resourceId.entryId());
- }
-
- /**
- * Overrides the entries in this chunk at the given index:entry pairs in {@code entries}.
- * For example, if the current list of entries is {0: foo, 1: bar, 2: baz}, and {@code entries}
- * is {1: qux, 3: quux}, then the entries will be changed to {0: foo, 1: qux, 2: baz}. If an entry
- * has an index that does not exist in the dense entry list, then it is considered a no-op for
- * that single entry.
- *
- * @param entries A sparse list containing index:entry pairs to override.
- */
- public void overrideEntries(Map entries) {
- for (Map.Entry entry : entries.entrySet()) {
- int index = entry.getKey() != null ? entry.getKey() : -1;
- overrideEntry(index, entry.getValue());
- }
- }
-
- /**
- * Overrides an entry at the given index. Passing null for the {@code entry} will remove that
- * entry from {@code entries}. Indices < 0 or >= {@link #getTotalEntryCount()} are a no-op.
- *
- * @param index The 0-based index for the entry to override.
- * @param entry The entry to override, or null if the entry should be removed at this location.
- */
- public void overrideEntry(int index, @Nullable Entry entry) {
- if (index >= 0 && index < entryCount) {
- if (entry != null) {
- entries.put(index, entry);
- } else {
- entries.remove(index);
- }
- }
- }
-
- protected String getString(int index) {
- ResourceTableChunk resourceTable = getResourceTableChunk();
- Preconditions.checkNotNull(resourceTable, "%s has no resource table.", getClass());
- return resourceTable.getStringPool().getString(index);
- }
-
- protected String getKeyName(int index) {
- PackageChunk packageChunk = getPackageChunk();
- Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
- StringPoolChunk keyPool = packageChunk.getKeyStringPool();
- Preconditions.checkNotNull(keyPool, "%s's parent package has no key pool.", getClass());
- return keyPool.getString(index);
- }
-
- protected void updateKey(int index, String value) {
- PackageChunk packageChunk = getPackageChunk();
- Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
- StringPoolChunk keyPool = packageChunk.getKeyStringPool();
- Preconditions.checkNotNull(keyPool, "%s's parent package has no key pool.", getClass());
- keyPool.updateString(index, value);
- }
-
- @Nullable
- private ResourceTableChunk getResourceTableChunk() {
- Chunk chunk = getParent();
- while (chunk != null && !(chunk instanceof ResourceTableChunk)) {
- chunk = chunk.getParent();
- }
- return chunk != null ? (ResourceTableChunk) chunk : null;
- }
-
- /** Returns the package enclosing this chunk, if any. Else, returns null. */
- @Nullable
- public PackageChunk getPackageChunk() {
- Chunk chunk = getParent();
- while (chunk != null && !(chunk instanceof PackageChunk)) {
- chunk = chunk.getParent();
- }
- return chunk != null ? (PackageChunk) chunk : null;
- }
-
- @Override
- protected Type getType() {
- return Type.TABLE_TYPE;
- }
-
- /** Returns the number of bytes needed for offsets based on {@code entries}. */
- private int getOffsetSize() {
- return entryCount * 4;
- }
-
- private int writeEntries(DataOutput payload, ByteBuffer offsets, boolean shrink)
- throws IOException {
- int entryOffset = 0;
- for (int i = 0; i < entryCount; ++i) {
- Entry entry = entries.get(i);
- if (entry == null) {
- offsets.putInt(Entry.NO_ENTRY);
- } else {
- byte[] encodedEntry = entry.toByteArray(shrink);
- payload.write(encodedEntry);
- offsets.putInt(entryOffset);
- entryOffset += encodedEntry.length;
- }
- }
- entryOffset = writePad(payload, entryOffset);
- return entryOffset;
- }
-
- @Override
- protected void writeHeader(ByteBuffer output) {
- int entriesStart = getHeaderSize() + getOffsetSize();
- output.putInt(id); // Write an unsigned byte with 3 bytes padding
- output.putInt(entryCount);
- output.putInt(entriesStart);
- output.put(configuration.toByteArray(false));
- }
-
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize()).order(ByteOrder.LITTLE_ENDIAN);
- try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
- writeEntries(payload, offsets, shrink);
- }
- output.write(offsets.array());
- output.write(baos.toByteArray());
- }
-
- /** An {@link Entry} in a {@link TypeChunk}. Contains one or more {@link BinaryResourceValue}. */
- public static class Entry implements SerializableResource {
-
- /** An entry offset that indicates that a given resource is not present. */
- public static final int NO_ENTRY = 0xFFFFFFFF;
-
- /** Set if this is a complex resource. Otherwise, it's a simple resource. */
- private static final int FLAG_COMPLEX = 0x0001;
-
- /** Size of a single resource id + value mapping entry. */
- private static final int MAPPING_SIZE = 4 + BinaryResourceValue.SIZE;
-
- private final int headerSize;
- private final int flags;
- private final int keyIndex;
- private BinaryResourceValue value;
- private Map values;
- private final int parentEntry;
- private final TypeChunk parent;
-
- private Entry(int headerSize,
- int flags,
- int keyIndex,
- BinaryResourceValue value,
- Map values,
- int parentEntry,
- TypeChunk parent) {
- this.headerSize = headerSize;
- this.flags = flags;
- this.keyIndex = keyIndex;
- this.value = value;
- this.values = values;
- this.parentEntry = parentEntry;
- this.parent = parent;
- }
-
- /** Number of bytes in the header of the {@link Entry}. */
- public int headerSize() { return headerSize; }
+ /**
+ * The type identifier of the resource type this chunk is holding.
+ */
+ private final int id;
- /** Resource entry flags. */
- public int flags() { return flags; }
+ /**
+ * The number of resources of this type at creation time.
+ */
+ private final int entryCount;
- /** Index into {@link PackageChunk#getKeyStringPool} identifying this entry. */
- public int keyIndex() { return keyIndex; }
+ /**
+ * The offset (from {@code offset}) in the original buffer where {@code entries} start.
+ */
+ private final int entriesStart;
- /** The value of this resource entry, if this is not a complex entry. Else, null. */
- @Nullable
- public BinaryResourceValue value() { return value; }
+ /**
+ * The resource configuration that these resource entries correspond to.
+ */
+ private BinaryResourceConfiguration configuration;
/**
- * Update the value of this resource entry
- * @param value
+ * A sparse list of resource entries defined by this chunk.
*/
- public void updateValue(@Nullable BinaryResourceValue value){
- this.value = value;
+ private final Map entries = new TreeMap<>();
+
+ protected TypeChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ id = UnsignedBytes.toInt(buffer.get());
+ buffer.position(buffer.position() + 3); // Skip 3 bytes for packing
+ entryCount = buffer.getInt();
+ entriesStart = buffer.getInt();
+ configuration = BinaryResourceConfiguration.create(buffer);
}
- /** The extra values in this resource entry if this {@link #isComplex}. */
- public Map values() { return values; }
-
- /** Update the value in this resource entry if this {@link #isComplex}. */
- public void updateValue(int index, BinaryResourceValue value){
- if (index >= 0) {
- if (value != null) {
- values.put(index, value);
- } else {
- values.remove(index);
+ @Override
+ protected void init(ByteBuffer buffer) {
+ int offset = this.offset + entriesStart;
+ for (int i = 0; i < entryCount; ++i) {
+ Entry entry = Entry.create(buffer, offset, this);
+ if (entry != null) {
+ entries.put(i, entry);
+ }
}
- }
}
/**
- * Entry into {@link PackageChunk} that is the parent {@link Entry} to this entry.
- * This value only makes sense when this is complex ({@link #isComplex} returns true).
+ * Returns the (1-based) type id of the resource types that this {@link TypeChunk} is holding.
*/
- public int parentEntry() { return parentEntry; }
+ public int getId() {
+ return id;
+ }
- /** The {@link TypeChunk} that this resource entry belongs to. */
- public TypeChunk parent() { return parent; }
+ /**
+ * Returns the name of the type this chunk represents (e.g. string, attr, id).
+ */
+ public String getTypeName() {
+ PackageChunk packageChunk = getPackageChunk();
+ Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
+ StringPoolChunk typePool = packageChunk.getTypeStringPool();
+ Preconditions.checkNotNull(typePool, "%s's parent package has no type pool.", getClass());
+ return typePool.getString(getId() - 1); // - 1 here to convert to 0-based index
+ }
- /** Returns the name of the type this chunk represents (e.g. string, attr, id). */
- public final String typeName() {
- return parent().getTypeName();
+ /**
+ * Returns the resource configuration that these resource entries correspond to.
+ */
+ public BinaryResourceConfiguration getConfiguration() {
+ return configuration;
}
- /** The total number of bytes that this {@link Entry} takes up. */
- public final int size() {
- return headerSize() + (isComplex() ? values().size() * MAPPING_SIZE : BinaryResourceValue.SIZE);
+ /**
+ * Sets the resource configuration that this chunk's entries correspond to.
+ *
+ * @param configuration The new configuration.
+ */
+ public void setConfiguration(BinaryResourceConfiguration configuration) {
+ this.configuration = configuration;
}
- /** Returns the key name identifying this resource entry. */
- public final String key() {
- return parent().getKeyName(keyIndex());
+ /**
+ * Returns the total number of entries for this type + configuration, including null entries.
+ */
+ public int getTotalEntryCount() {
+ return entryCount;
}
- /** Update the key name in this resource entry. */
- public final void updateKey(int keyIndex, String value) {
- parent().updateKey(keyIndex, value);
+ /**
+ * Returns a sparse list of 0-based indices to resource entries defined by this chunk.
+ */
+ public Map getEntries() {
+ return Collections.unmodifiableMap(entries);
}
- /** Returns true if this is a complex resource. */
- public final boolean isComplex() {
- return (flags() & FLAG_COMPLEX) != 0;
+ /**
+ * Returns true if this chunk contains an entry for {@code resourceId}.
+ */
+ public boolean containsResource(BinaryResourceIdentifier resourceId) {
+ PackageChunk packageChunk = Preconditions.checkNotNull(getPackageChunk());
+ int packageId = packageChunk.getId();
+ int typeId = getId();
+ return resourceId.packageId() == packageId
+ && resourceId.typeId() == typeId
+ && entries.containsKey(resourceId.entryId());
}
/**
- * Creates a new {@link Entry} whose contents start at the 0-based position in
- * {@code buffer} given by a 4-byte value read from {@code buffer} and then added to
- * {@code baseOffset}. If the value read from {@code buffer} is equal to {@link #NO_ENTRY}, then
- * null is returned as there is no resource at that position.
+ * Overrides the entries in this chunk at the given index:entry pairs in {@code entries}.
+ * For example, if the current list of entries is {0: foo, 1: bar, 2: baz}, and {@code entries}
+ * is {1: qux, 3: quux}, then the entries will be changed to {0: foo, 1: qux, 2: baz}. If an entry
+ * has an index that does not exist in the dense entry list, then it is considered a no-op for
+ * that single entry.
*
- *
Otherwise, this position is parsed and returned as an {@link Entry}.
+ * @param entries A sparse list containing index:entry pairs to override.
+ */
+ public void overrideEntries(Map entries) {
+ for (Map.Entry entry : entries.entrySet()) {
+ int index = entry.getKey() != null ? entry.getKey() : -1;
+ overrideEntry(index, entry.getValue());
+ }
+ }
+
+ /**
+ * Overrides an entry at the given index. Passing null for the {@code entry} will remove that
+ * entry from {@code entries}. Indices < 0 or >= {@link #getTotalEntryCount()} are a no-op.
*
- * @param buffer A buffer positioned at an offset to an {@link Entry}.
- * @param baseOffset Offset that must be added to the value at {@code buffer}'s position.
- * @param parent The {@link TypeChunk} that this resource entry belongs to.
- * @return New {@link Entry} or null if there is no resource at this location.
+ * @param index The 0-based index for the entry to override.
+ * @param entry The entry to override, or null if the entry should be removed at this location.
*/
+ public void overrideEntry(int index, @Nullable Entry entry) {
+ if (index >= 0 && index < entryCount) {
+ if (entry != null) {
+ entries.put(index, entry);
+ } else {
+ entries.remove(index);
+ }
+ }
+ }
+
+ protected String getString(int index) {
+ ResourceTableChunk resourceTable = getResourceTableChunk();
+ Preconditions.checkNotNull(resourceTable, "%s has no resource table.", getClass());
+ return resourceTable.getStringPool().getString(index);
+ }
+
+ protected String getKeyName(int index) {
+ PackageChunk packageChunk = getPackageChunk();
+ Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
+ StringPoolChunk keyPool = packageChunk.getKeyStringPool();
+ Preconditions.checkNotNull(keyPool, "%s's parent package has no key pool.", getClass());
+ return keyPool.getString(index);
+ }
+
+ protected void updateKey(int index, String value) {
+ PackageChunk packageChunk = getPackageChunk();
+ Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
+ StringPoolChunk keyPool = packageChunk.getKeyStringPool();
+ Preconditions.checkNotNull(keyPool, "%s's parent package has no key pool.", getClass());
+ keyPool.updateString(index, value);
+ }
+
@Nullable
- public static Entry create(ByteBuffer buffer, int baseOffset, TypeChunk parent) {
- int offset = buffer.getInt();
- if (offset == NO_ENTRY) {
- return null;
- }
- int position = buffer.position();
- buffer.position(baseOffset + offset); // Set buffer position to resource entry start
- Entry result = newInstance(buffer, parent);
- buffer.position(position); // Restore buffer position
- return result;
+ private ResourceTableChunk getResourceTableChunk() {
+ Chunk chunk = getParent();
+ while (chunk != null && !(chunk instanceof ResourceTableChunk)) {
+ chunk = chunk.getParent();
+ }
+ return chunk != null ? (ResourceTableChunk) chunk : null;
}
+ /**
+ * Returns the package enclosing this chunk, if any. Else, returns null.
+ */
@Nullable
- private static Entry newInstance(ByteBuffer buffer, TypeChunk parent) {
- int headerSize = buffer.getShort() & 0xFFFF;
- int flags = buffer.getShort() & 0xFFFF;
- int keyIndex = buffer.getInt();
- BinaryResourceValue value = null;
- Map values = new LinkedHashMap<>();
- int parentEntry = 0;
- if ((flags & FLAG_COMPLEX) != 0) {
- parentEntry = buffer.getInt();
- int valueCount = buffer.getInt();
- for (int i = 0; i < valueCount; ++i) {
- values.put(buffer.getInt(), BinaryResourceValue.create(buffer));
+ public PackageChunk getPackageChunk() {
+ Chunk chunk = getParent();
+ while (chunk != null && !(chunk instanceof PackageChunk)) {
+ chunk = chunk.getParent();
}
- } else {
- value = BinaryResourceValue.create(buffer);
- }
- return new Entry(headerSize, flags, keyIndex, value, values, parentEntry, parent);
+ return chunk != null ? (PackageChunk) chunk : null;
}
@Override
- public final byte[] toByteArray() {
- return toByteArray(false);
+ protected Type getType() {
+ return Type.TABLE_TYPE;
}
- @Override
- public final byte[] toByteArray(boolean shrink) {
- ByteBuffer buffer = ByteBuffer.allocate(size());
- buffer.order(ByteOrder.LITTLE_ENDIAN);
- buffer.putShort((short) headerSize());
- buffer.putShort((short) flags());
- buffer.putInt(keyIndex());
- if (isComplex()) {
- buffer.putInt(parentEntry());
- buffer.putInt(values().size());
- for (Map.Entry entry : values().entrySet()) {
- buffer.putInt(entry.getKey());
- buffer.put(entry.getValue().toByteArray(shrink));
+ /**
+ * Returns the number of bytes needed for offsets based on {@code entries}.
+ */
+ private int getOffsetSize() {
+ return entryCount * 4;
+ }
+
+ private int writeEntries(DataOutput payload, ByteBuffer offsets, boolean shrink)
+ throws IOException {
+ int entryOffset = 0;
+ for (int i = 0; i < entryCount; ++i) {
+ Entry entry = entries.get(i);
+ if (entry == null) {
+ offsets.putInt(Entry.NO_ENTRY);
+ } else {
+ byte[] encodedEntry = entry.toByteArray(shrink);
+ payload.write(encodedEntry);
+ offsets.putInt(entryOffset);
+ entryOffset += encodedEntry.length;
+ }
}
- } else {
- BinaryResourceValue value = value();
- Preconditions.checkNotNull(value, "A non-complex TypeChunk entry must have a value.");
- buffer.put(value.toByteArray());
- }
- return buffer.array();
+ entryOffset = writePad(payload, entryOffset);
+ return entryOffset;
}
@Override
- public final String toString() {
- return String.format("Entry{key=%s}", key());
+ protected void writeHeader(ByteBuffer output) {
+ int entriesStart = getHeaderSize() + getOffsetSize();
+ output.putInt(id); // Write an unsigned byte with 3 bytes padding
+ output.putInt(entryCount);
+ output.putInt(entriesStart);
+ output.put(configuration.toByteArray(false));
}
@Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Entry entry = (Entry)o;
- return headerSize == entry.headerSize &&
- flags == entry.flags &&
- keyIndex == entry.keyIndex &&
- parentEntry == entry.parentEntry &&
- Objects.equals(value, entry.value) &&
- Objects.equals(values, entry.values) &&
- Objects.equals(parent, entry.parent);
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ ByteBuffer offsets = ByteBuffer.allocate(getOffsetSize()).order(ByteOrder.LITTLE_ENDIAN);
+ try (LittleEndianDataOutputStream payload = new LittleEndianDataOutputStream(baos)) {
+ writeEntries(payload, offsets, shrink);
+ }
+ output.write(offsets.array());
+ output.write(baos.toByteArray());
}
- @Override
- public int hashCode() {
- return Objects.hash(headerSize, flags, keyIndex, value, values, parentEntry, parent);
+ /**
+ * An {@link Entry} in a {@link TypeChunk}. Contains one or more {@link BinaryResourceValue}.
+ */
+ public static class Entry implements SerializableResource {
+
+ /**
+ * An entry offset that indicates that a given resource is not present.
+ */
+ public static final int NO_ENTRY = 0xFFFFFFFF;
+
+ /**
+ * Set if this is a complex resource. Otherwise, it's a simple resource.
+ */
+ private static final int FLAG_COMPLEX = 0x0001;
+
+ /**
+ * Size of a single resource id + value mapping entry.
+ */
+ private static final int MAPPING_SIZE = 4 + BinaryResourceValue.SIZE;
+
+ private final int headerSize;
+ private final int flags;
+ private final int keyIndex;
+ private BinaryResourceValue value;
+ private Map values;
+ private final int parentEntry;
+ private final TypeChunk parent;
+
+ private Entry(int headerSize,
+ int flags,
+ int keyIndex,
+ BinaryResourceValue value,
+ Map values,
+ int parentEntry,
+ TypeChunk parent) {
+ this.headerSize = headerSize;
+ this.flags = flags;
+ this.keyIndex = keyIndex;
+ this.value = value;
+ this.values = values;
+ this.parentEntry = parentEntry;
+ this.parent = parent;
+ }
+
+ /**
+ * Number of bytes in the header of the {@link Entry}.
+ */
+ public int headerSize() {
+ return headerSize;
+ }
+
+ /**
+ * Resource entry flags.
+ */
+ public int flags() {
+ return flags;
+ }
+
+ /**
+ * Index into {@link PackageChunk#getKeyStringPool} identifying this entry.
+ */
+ public int keyIndex() {
+ return keyIndex;
+ }
+
+ /**
+ * The value of this resource entry, if this is not a complex entry. Else, null.
+ */
+ @Nullable
+ public BinaryResourceValue value() {
+ return value;
+ }
+
+ /**
+ * Update the value of this resource entry
+ *
+ * @param value
+ */
+ public void updateValue(@Nullable BinaryResourceValue value) {
+ this.value = value;
+ }
+
+ /**
+ * The extra values in this resource entry if this {@link #isComplex}.
+ */
+ public Map values() {
+ return values;
+ }
+
+ /**
+ * Update the value in this resource entry if this {@link #isComplex}.
+ */
+ public void updateValue(int index, BinaryResourceValue value) {
+ if (index >= 0) {
+ if (value != null) {
+ values.put(index, value);
+ } else {
+ values.remove(index);
+ }
+ }
+ }
+
+ /**
+ * Entry into {@link PackageChunk} that is the parent {@link Entry} to this entry.
+ * This value only makes sense when this is complex ({@link #isComplex} returns true).
+ */
+ public int parentEntry() {
+ return parentEntry;
+ }
+
+ /**
+ * The {@link TypeChunk} that this resource entry belongs to.
+ */
+ public TypeChunk parent() {
+ return parent;
+ }
+
+ /**
+ * Returns the name of the type this chunk represents (e.g. string, attr, id).
+ */
+ public final String typeName() {
+ return parent().getTypeName();
+ }
+
+ /**
+ * The total number of bytes that this {@link Entry} takes up.
+ */
+ public final int size() {
+ return headerSize() + (isComplex() ? values().size() * MAPPING_SIZE : BinaryResourceValue.SIZE);
+ }
+
+ /**
+ * Returns the key name identifying this resource entry.
+ */
+ public final String key() {
+ return parent().getKeyName(keyIndex());
+ }
+
+ /**
+ * Update the key name in this resource entry.
+ */
+ public final void updateKey(int keyIndex, String value) {
+ parent().updateKey(keyIndex, value);
+ }
+
+ /**
+ * Returns true if this is a complex resource.
+ */
+ public final boolean isComplex() {
+ return (flags() & FLAG_COMPLEX) != 0;
+ }
+
+ /**
+ * Creates a new {@link Entry} whose contents start at the 0-based position in
+ * {@code buffer} given by a 4-byte value read from {@code buffer} and then added to
+ * {@code baseOffset}. If the value read from {@code buffer} is equal to {@link #NO_ENTRY}, then
+ * null is returned as there is no resource at that position.
+ *
+ *
Otherwise, this position is parsed and returned as an {@link Entry}.
+ *
+ * @param buffer A buffer positioned at an offset to an {@link Entry}.
+ * @param baseOffset Offset that must be added to the value at {@code buffer}'s position.
+ * @param parent The {@link TypeChunk} that this resource entry belongs to.
+ * @return New {@link Entry} or null if there is no resource at this location.
+ */
+ @Nullable
+ public static Entry create(ByteBuffer buffer, int baseOffset, TypeChunk parent) {
+ int offset = buffer.getInt();
+ if (offset == NO_ENTRY) {
+ return null;
+ }
+ int position = buffer.position();
+ buffer.position(baseOffset + offset); // Set buffer position to resource entry start
+ Entry result = newInstance(buffer, parent);
+ buffer.position(position); // Restore buffer position
+ return result;
+ }
+
+ @Nullable
+ private static Entry newInstance(ByteBuffer buffer, TypeChunk parent) {
+ int headerSize = buffer.getShort() & 0xFFFF;
+ int flags = buffer.getShort() & 0xFFFF;
+ int keyIndex = buffer.getInt();
+ BinaryResourceValue value = null;
+ Map values = new LinkedHashMap<>();
+ int parentEntry = 0;
+ if ((flags & FLAG_COMPLEX) != 0) {
+ parentEntry = buffer.getInt();
+ int valueCount = buffer.getInt();
+ for (int i = 0; i < valueCount; ++i) {
+ values.put(buffer.getInt(), BinaryResourceValue.create(buffer));
+ }
+ } else {
+ value = BinaryResourceValue.create(buffer);
+ }
+ return new Entry(headerSize, flags, keyIndex, value, values, parentEntry, parent);
+ }
+
+ @Override
+ public final byte[] toByteArray() {
+ return toByteArray(false);
+ }
+
+ @Override
+ public final byte[] toByteArray(boolean shrink) {
+ ByteBuffer buffer = ByteBuffer.allocate(size());
+ buffer.order(ByteOrder.LITTLE_ENDIAN);
+ buffer.putShort((short) headerSize());
+ buffer.putShort((short) flags());
+ buffer.putInt(keyIndex());
+ if (isComplex()) {
+ buffer.putInt(parentEntry());
+ buffer.putInt(values().size());
+ for (Map.Entry entry : values().entrySet()) {
+ buffer.putInt(entry.getKey());
+ buffer.put(entry.getValue().toByteArray(shrink));
+ }
+ } else {
+ BinaryResourceValue value = value();
+ Preconditions.checkNotNull(value, "A non-complex TypeChunk entry must have a value.");
+ buffer.put(value.toByteArray());
+ }
+ return buffer.array();
+ }
+
+ @Override
+ public final String toString() {
+ return String.format("Entry{key=%s}", key());
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ Entry entry = (Entry) o;
+ return headerSize == entry.headerSize &&
+ flags == entry.flags &&
+ keyIndex == entry.keyIndex &&
+ parentEntry == entry.parentEntry &&
+ Objects.equals(value, entry.value) &&
+ Objects.equals(values, entry.values) &&
+ Objects.equals(parent, entry.parent);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(headerSize, flags, keyIndex, value, values, parentEntry, parent);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeSpecChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeSpecChunk.java
index 691d9a38af..c12922b8d2 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeSpecChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/TypeSpecChunk.java
@@ -16,86 +16,98 @@
package com.google.devrel.gmscore.tools.apk.arsc;
+import androidx.annotation.Nullable;
import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedBytes;
-import androidx.annotation.Nullable;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
-/** A chunk that contains a collection of resource entries for a particular resource data type. */
+/**
+ * A chunk that contains a collection of resource entries for a particular resource data type.
+ */
public final class TypeSpecChunk extends Chunk {
- /** The id of the resource type that this type spec refers to. */
- private final int id;
+ /**
+ * The id of the resource type that this type spec refers to.
+ */
+ private final int id;
- /** Resource configuration masks. */
- private final int[] resources;
+ /**
+ * Resource configuration masks.
+ */
+ private final int[] resources;
- protected TypeSpecChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- id = UnsignedBytes.toInt(buffer.get());
- buffer.position(buffer.position() + 3); // Skip 3 bytes for packing
- int resourceCount = buffer.getInt();
- resources = new int[resourceCount];
+ protected TypeSpecChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ id = UnsignedBytes.toInt(buffer.get());
+ buffer.position(buffer.position() + 3); // Skip 3 bytes for packing
+ int resourceCount = buffer.getInt();
+ resources = new int[resourceCount];
- for (int i = 0; i < resourceCount; ++i) {
- resources[i] = buffer.getInt();
+ for (int i = 0; i < resourceCount; ++i) {
+ resources[i] = buffer.getInt();
+ }
}
- }
- /**
- * Returns the (1-based) type id of the resources that this {@link TypeSpecChunk} has
- * configuration masks for.
- */
- public int getId() {
- return id;
- }
+ /**
+ * Returns the (1-based) type id of the resources that this {@link TypeSpecChunk} has
+ * configuration masks for.
+ */
+ public int getId() {
+ return id;
+ }
- /** Returns the number of resource entries that this chunk has configuration masks for. */
- public int getResourceCount() {
- return resources.length;
- }
+ /**
+ * Returns the number of resource entries that this chunk has configuration masks for.
+ */
+ public int getResourceCount() {
+ return resources.length;
+ }
- @Override
- protected Type getType() {
- return Type.TABLE_TYPE_SPEC;
- }
+ @Override
+ protected Type getType() {
+ return Type.TABLE_TYPE_SPEC;
+ }
- /** Returns the name of the type this chunk represents (e.g. string, attr, id). */
- public String getTypeName() {
- PackageChunk packageChunk = getPackageChunk();
- Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
- StringPoolChunk typePool = packageChunk.getTypeStringPool();
- Preconditions.checkNotNull(typePool, "%s's parent package has no type pool.", getClass());
- return typePool.getString(getId() - 1); // - 1 here to convert to 0-based index
- }
+ /**
+ * Returns the name of the type this chunk represents (e.g. string, attr, id).
+ */
+ public String getTypeName() {
+ PackageChunk packageChunk = getPackageChunk();
+ Preconditions.checkNotNull(packageChunk, "%s has no parent package.", getClass());
+ StringPoolChunk typePool = packageChunk.getTypeStringPool();
+ Preconditions.checkNotNull(typePool, "%s's parent package has no type pool.", getClass());
+ return typePool.getString(getId() - 1); // - 1 here to convert to 0-based index
+ }
- /** Returns the package enclosing this chunk, if any. Else, returns null. */
- @Nullable
- private PackageChunk getPackageChunk() {
- Chunk chunk = getParent();
- while (chunk != null && !(chunk instanceof PackageChunk)) {
- chunk = chunk.getParent();
+ /**
+ * Returns the package enclosing this chunk, if any. Else, returns null.
+ */
+ @Nullable
+ private PackageChunk getPackageChunk() {
+ Chunk chunk = getParent();
+ while (chunk != null && !(chunk instanceof PackageChunk)) {
+ chunk = chunk.getParent();
+ }
+ return chunk != null ? (PackageChunk) chunk : null;
}
- return chunk != null ? (PackageChunk) chunk : null;
- }
- @Override
- protected void writeHeader(ByteBuffer output) {
- super.writeHeader(output);
- // id is an unsigned byte in the range [0-255]. It is guaranteed to be non-negative.
- // Because our output is in little-endian, we are making use of the 4 byte packing here
- output.putInt(id);
- output.putInt(resources.length);
- }
+ @Override
+ protected void writeHeader(ByteBuffer output) {
+ super.writeHeader(output);
+ // id is an unsigned byte in the range [0-255]. It is guaranteed to be non-negative.
+ // Because our output is in little-endian, we are making use of the 4 byte packing here
+ output.putInt(id);
+ output.putInt(resources.length);
+ }
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- for (int resource : resources) {
- output.writeInt(resource);
+ @Override
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ for (int resource : resources) {
+ output.writeInt(resource);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/UnknownChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/UnknownChunk.java
index 7117e63f7b..0d07c31993 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/UnknownChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/UnknownChunk.java
@@ -17,6 +17,7 @@
package com.google.devrel.gmscore.tools.apk.arsc;
import androidx.annotation.Nullable;
+
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -27,35 +28,35 @@
*/
public final class UnknownChunk extends Chunk {
- private final Type type;
+ private final Type type;
- private final byte[] header;
+ private final byte[] header;
- private final byte[] payload;
+ private final byte[] payload;
- protected UnknownChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
+ protected UnknownChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
- type = Type.fromCode(buffer.getShort(offset));
- header = new byte[headerSize - Chunk.METADATA_SIZE];
- payload = new byte[chunkSize - headerSize];
- buffer.get(header);
- buffer.get(payload);
- }
+ type = Type.fromCode(buffer.getShort(offset));
+ header = new byte[headerSize - Chunk.METADATA_SIZE];
+ payload = new byte[chunkSize - headerSize];
+ buffer.get(header);
+ buffer.get(payload);
+ }
- @Override
- protected void writeHeader(ByteBuffer output) {
- output.put(header);
- }
+ @Override
+ protected void writeHeader(ByteBuffer output) {
+ output.put(header);
+ }
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- output.write(payload);
- }
+ @Override
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ output.write(payload);
+ }
- @Override
- protected Type getType() {
- return type;
- }
+ @Override
+ protected Type getType() {
+ return type;
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlAttribute.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlAttribute.java
index 1cb2977b3f..f3d696834e 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlAttribute.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlAttribute.java
@@ -20,129 +20,149 @@
import java.nio.ByteOrder;
import java.util.Objects;
-/** Represents an XML attribute and value. */
+/**
+ * Represents an XML attribute and value.
+ */
public class XmlAttribute implements SerializableResource {
- /** The serialized size in bytes of an {@link XmlAttribute}. */
- public static final int SIZE = 12 + BinaryResourceValue.SIZE;
-
- private final int namespaceIndex;
- private final int nameIndex;
- private final int rawValueIndex;
- private final BinaryResourceValue typedValue;
- private final XmlNodeChunk parent;
-
- /**
- * Creates a new {@link XmlAttribute} based on the bytes at the current {@code buffer} position.
- *
- * @param buffer A buffer whose position is at the start of a {@link XmlAttribute}.
- * @param parent The parent chunk that contains this attribute; used for string lookups.
- */
- public static XmlAttribute create(ByteBuffer buffer, XmlNodeChunk parent) {
- int namespace = buffer.getInt();
- int name = buffer.getInt();
- int rawValue = buffer.getInt();
- BinaryResourceValue typedValue = BinaryResourceValue.create(buffer);
- return new XmlAttribute(namespace, name, rawValue, typedValue, parent);
- }
-
- private XmlAttribute(int namespaceIndex,
- int nameIndex,
- int rawValueIndex,
- BinaryResourceValue typedValue,
- XmlNodeChunk parent) {
- this.namespaceIndex = namespaceIndex;
- this.nameIndex = nameIndex;
- this.rawValueIndex = rawValueIndex;
- this.typedValue = typedValue;
- this.parent = parent;
- }
-
- /** A string reference to the namespace URI, or -1 if not present. */
- public int namespaceIndex() {
- return namespaceIndex;
- }
-
- /** A string reference to the attribute name. */
- public int nameIndex() {
- return nameIndex;
- }
-
- /** A string reference to a string containing the character value. */
- public int rawValueIndex() {
- return rawValueIndex;
- }
-
- /** A {@link BinaryResourceValue} instance containing the parsed value. */
- public BinaryResourceValue typedValue() {
- return typedValue;
- }
-
- /** The parent of this XML attribute; used for dereferencing the namespace and name. */
- public XmlNodeChunk parent() {
- return parent;
- }
-
- /** The namespace URI, or the empty string if not present. */
- public final String namespace() {
- return getString(namespaceIndex());
- }
-
- /** The attribute name, or the empty string if not present. */
- public final String name() {
- return getString(nameIndex());
- }
-
- /** The raw character value. */
- public final String rawValue() {
- return getString(rawValueIndex());
- }
-
- private String getString(int index) {
- return parent().getString(index);
- }
-
- @Override
- public byte[] toByteArray() {
- return toByteArray(false);
- }
-
- @Override
- public byte[] toByteArray(boolean shrink) {
- ByteBuffer buffer = ByteBuffer.allocate(SIZE).order(ByteOrder.LITTLE_ENDIAN);
- buffer.putInt(namespaceIndex());
- buffer.putInt(nameIndex());
- buffer.putInt(rawValueIndex());
- buffer.put(typedValue().toByteArray(shrink));
- return buffer.array();
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- XmlAttribute that = (XmlAttribute)o;
- return namespaceIndex == that.namespaceIndex &&
- nameIndex == that.nameIndex &&
- rawValueIndex == that.rawValueIndex &&
- Objects.equals(typedValue, that.typedValue) &&
- Objects.equals(parent, that.parent);
- }
-
- @Override
- public int hashCode() {
- return Objects.hash(namespaceIndex, nameIndex, rawValueIndex, typedValue, parent);
- }
-
- /**
- * Returns a brief description of this XML attribute. The representation of this information is
- * subject to change, but below is a typical example:
- *
- *
- */
- @Override
- public String toString() {
- return String.format("XmlAttribute{namespace=%s, name=%s, value=%s}",
- namespace(), name(), rawValue());
- }
+ /**
+ * The serialized size in bytes of an {@link XmlAttribute}.
+ */
+ public static final int SIZE = 12 + BinaryResourceValue.SIZE;
+
+ private final int namespaceIndex;
+ private final int nameIndex;
+ private final int rawValueIndex;
+ private final BinaryResourceValue typedValue;
+ private final XmlNodeChunk parent;
+
+ /**
+ * Creates a new {@link XmlAttribute} based on the bytes at the current {@code buffer} position.
+ *
+ * @param buffer A buffer whose position is at the start of a {@link XmlAttribute}.
+ * @param parent The parent chunk that contains this attribute; used for string lookups.
+ */
+ public static XmlAttribute create(ByteBuffer buffer, XmlNodeChunk parent) {
+ int namespace = buffer.getInt();
+ int name = buffer.getInt();
+ int rawValue = buffer.getInt();
+ BinaryResourceValue typedValue = BinaryResourceValue.create(buffer);
+ return new XmlAttribute(namespace, name, rawValue, typedValue, parent);
+ }
+
+ private XmlAttribute(int namespaceIndex,
+ int nameIndex,
+ int rawValueIndex,
+ BinaryResourceValue typedValue,
+ XmlNodeChunk parent) {
+ this.namespaceIndex = namespaceIndex;
+ this.nameIndex = nameIndex;
+ this.rawValueIndex = rawValueIndex;
+ this.typedValue = typedValue;
+ this.parent = parent;
+ }
+
+ /**
+ * A string reference to the namespace URI, or -1 if not present.
+ */
+ public int namespaceIndex() {
+ return namespaceIndex;
+ }
+
+ /**
+ * A string reference to the attribute name.
+ */
+ public int nameIndex() {
+ return nameIndex;
+ }
+
+ /**
+ * A string reference to a string containing the character value.
+ */
+ public int rawValueIndex() {
+ return rawValueIndex;
+ }
+
+ /**
+ * A {@link BinaryResourceValue} instance containing the parsed value.
+ */
+ public BinaryResourceValue typedValue() {
+ return typedValue;
+ }
+
+ /**
+ * The parent of this XML attribute; used for dereferencing the namespace and name.
+ */
+ public XmlNodeChunk parent() {
+ return parent;
+ }
+
+ /**
+ * The namespace URI, or the empty string if not present.
+ */
+ public final String namespace() {
+ return getString(namespaceIndex());
+ }
+
+ /**
+ * The attribute name, or the empty string if not present.
+ */
+ public final String name() {
+ return getString(nameIndex());
+ }
+
+ /**
+ * The raw character value.
+ */
+ public final String rawValue() {
+ return getString(rawValueIndex());
+ }
+
+ private String getString(int index) {
+ return parent().getString(index);
+ }
+
+ @Override
+ public byte[] toByteArray() {
+ return toByteArray(false);
+ }
+
+ @Override
+ public byte[] toByteArray(boolean shrink) {
+ ByteBuffer buffer = ByteBuffer.allocate(SIZE).order(ByteOrder.LITTLE_ENDIAN);
+ buffer.putInt(namespaceIndex());
+ buffer.putInt(nameIndex());
+ buffer.putInt(rawValueIndex());
+ buffer.put(typedValue().toByteArray(shrink));
+ return buffer.array();
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ XmlAttribute that = (XmlAttribute) o;
+ return namespaceIndex == that.namespaceIndex &&
+ nameIndex == that.nameIndex &&
+ rawValueIndex == that.rawValueIndex &&
+ Objects.equals(typedValue, that.typedValue) &&
+ Objects.equals(parent, that.parent);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(namespaceIndex, nameIndex, rawValueIndex, typedValue, parent);
+ }
+
+ /**
+ * Returns a brief description of this XML attribute. The representation of this information is
+ * subject to change, but below is a typical example:
+ *
+ *
- */
- @Override
- public String toString() {
- return String.format("XmlCdataChunk{line=%d, comment=%s, value=%s}",
- getLineNumber(), getComment(), getRawValue());
- }
+ /**
+ * Returns a brief description of this XML node. The representation of this information is
+ * subject to change, but below is a typical example:
+ *
+ *
- */
- @Override
- public String toString() {
- return String.format("XmlEndElementChunk{line=%d, comment=%s, namespace=%s, name=%s}",
- getLineNumber(), getComment(), getNamespace(), getName());
- }
+ /**
+ * Returns a brief description of this XML node. The representation of this information is
+ * subject to change, but below is a typical example:
+ *
+ *
- */
- @Override
- public String toString() {
- return String.format("XmlNamespaceChunk{line=%d, comment=%s, prefix=%s, uri=%s}",
- getLineNumber(), getComment(), getPrefix(), getUri());
- }
+ /**
+ * Returns a brief description of this namespace chunk. The representation of this information is
+ * subject to change, but below is a typical example:
+ *
+ *
+ */
+ @Override
+ public String toString() {
+ return String.format("XmlNamespaceChunk{line=%d, comment=%s, prefix=%s, uri=%s}",
+ getLineNumber(), getComment(), getPrefix(), getUri());
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceEndChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceEndChunk.java
index 6cce6e9fdf..49f70957db 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceEndChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceEndChunk.java
@@ -17,17 +17,20 @@
package com.google.devrel.gmscore.tools.apk.arsc;
import androidx.annotation.Nullable;
+
import java.nio.ByteBuffer;
-/** Represents the ending tag of a namespace in an XML document. */
+/**
+ * Represents the ending tag of a namespace in an XML document.
+ */
public final class XmlNamespaceEndChunk extends XmlNamespaceChunk {
- protected XmlNamespaceEndChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- }
+ protected XmlNamespaceEndChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ }
- @Override
- protected Type getType() {
- return Type.XML_END_NAMESPACE;
- }
+ @Override
+ protected Type getType() {
+ return Type.XML_END_NAMESPACE;
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceStartChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceStartChunk.java
index 3cb3daff9f..1fbd23383c 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceStartChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNamespaceStartChunk.java
@@ -17,17 +17,20 @@
package com.google.devrel.gmscore.tools.apk.arsc;
import androidx.annotation.Nullable;
+
import java.nio.ByteBuffer;
-/** Represents the starting tag of a namespace in an XML document. */
+/**
+ * Represents the starting tag of a namespace in an XML document.
+ */
public final class XmlNamespaceStartChunk extends XmlNamespaceChunk {
- protected XmlNamespaceStartChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- }
+ protected XmlNamespaceStartChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ }
- @Override
- protected Type getType() {
- return Type.XML_START_NAMESPACE;
- }
+ @Override
+ protected Type getType() {
+ return Type.XML_START_NAMESPACE;
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNodeChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNodeChunk.java
index 3f293c0a9d..2dddc35780 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNodeChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlNodeChunk.java
@@ -17,80 +17,93 @@
package com.google.devrel.gmscore.tools.apk.arsc;
import androidx.annotation.Nullable;
+
import java.nio.ByteBuffer;
-/** The common superclass for the various types of XML nodes. */
+/**
+ * The common superclass for the various types of XML nodes.
+ */
public abstract class XmlNodeChunk extends Chunk {
- /** The line number in the original source at which this node appeared. */
- private final int lineNumber;
-
- /** A string reference of this node's comment. If this is -1, then there is no comment. */
- private final int comment;
+ /**
+ * The line number in the original source at which this node appeared.
+ */
+ private final int lineNumber;
- protected XmlNodeChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- lineNumber = buffer.getInt();
- comment = buffer.getInt();
- }
+ /**
+ * A string reference of this node's comment. If this is -1, then there is no comment.
+ */
+ private final int comment;
- /** Returns true if this XML node contains a comment. Else, returns false. */
- public boolean hasComment() {
- return comment != -1;
- }
+ protected XmlNodeChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ lineNumber = buffer.getInt();
+ comment = buffer.getInt();
+ }
- /** Returns the line number in the original source at which this node appeared. */
- public int getLineNumber() {
- return lineNumber;
- }
+ /**
+ * Returns true if this XML node contains a comment. Else, returns false.
+ */
+ public boolean hasComment() {
+ return comment != -1;
+ }
- /** Returns the comment associated with this node, if any. Else, returns the empty string. */
- public String getComment() {
- return getString(comment);
- }
+ /**
+ * Returns the line number in the original source at which this node appeared.
+ */
+ public int getLineNumber() {
+ return lineNumber;
+ }
- /**
- * An {@link XmlNodeChunk} does not know by itself what strings its indices reference. In order
- * to get the actual string, the first {@link XmlChunk} ancestor is found. The
- * {@link XmlChunk} ancestor should have a string pool which {@code index} references.
- *
- * @param index The index of the string.
- * @return String that the given {@code index} references, or empty string if {@code index} is -1.
- */
- protected String getString(int index) {
- if (index == -1) { // Special case. Packed XML files use -1 for "no string entry"
- return "";
+ /**
+ * Returns the comment associated with this node, if any. Else, returns the empty string.
+ */
+ public String getComment() {
+ return getString(comment);
}
- Chunk parent = getParent();
- while (parent != null) {
- if (parent instanceof XmlChunk) {
- return ((XmlChunk) parent).getString(index);
- }
- parent = parent.getParent();
+
+ /**
+ * An {@link XmlNodeChunk} does not know by itself what strings its indices reference. In order
+ * to get the actual string, the first {@link XmlChunk} ancestor is found. The
+ * {@link XmlChunk} ancestor should have a string pool which {@code index} references.
+ *
+ * @param index The index of the string.
+ * @return String that the given {@code index} references, or empty string if {@code index} is -1.
+ */
+ protected String getString(int index) {
+ if (index == -1) { // Special case. Packed XML files use -1 for "no string entry"
+ return "";
+ }
+ Chunk parent = getParent();
+ while (parent != null) {
+ if (parent instanceof XmlChunk) {
+ return ((XmlChunk) parent).getString(index);
+ }
+ parent = parent.getParent();
+ }
+ throw new IllegalStateException("XmlNodeChunk did not have an XmlChunk parent.");
}
- throw new IllegalStateException("XmlNodeChunk did not have an XmlChunk parent.");
- }
- /**
- * An {@link XmlNodeChunk} and anything that is itself an {@link XmlNodeChunk} has a header size
- * of 16. Anything else is, interestingly, considered to be a payload. For that reason, this
- * method is final.
- */
- @Override
- protected final void writeHeader(ByteBuffer output) {
- super.writeHeader(output);
- output.putInt(lineNumber);
- output.putInt(comment);
- }
+ /**
+ * An {@link XmlNodeChunk} and anything that is itself an {@link XmlNodeChunk} has a header size
+ * of 16. Anything else is, interestingly, considered to be a payload. For that reason, this
+ * method is final.
+ */
+ @Override
+ protected final void writeHeader(ByteBuffer output) {
+ super.writeHeader(output);
+ output.putInt(lineNumber);
+ output.putInt(comment);
+ }
- /**
- * Returns a brief description of this XML node. The representation of this information is
- * subject to change, but below is a typical example:
- *
- *
- */
- @Override
- public String toString() {
- return String.format("XmlNodeChunk{line=%d, comment=%s}", getLineNumber(), getComment());
- }
+ /**
+ * Returns a brief description of this XML node. The representation of this information is
+ * subject to change, but below is a typical example:
+ *
+ *
+ */
+ @Override
+ public String toString() {
+ return String.format("XmlNodeChunk{line=%d, comment=%s}", getLineNumber(), getComment());
+ }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlResourceMapChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlResourceMapChunk.java
index 85d389f0ec..4c81d5c595 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlResourceMapChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlResourceMapChunk.java
@@ -17,6 +17,7 @@
package com.google.devrel.gmscore.tools.apk.arsc;
import androidx.annotation.Nullable;
+
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -31,56 +32,60 @@
*/
public class XmlResourceMapChunk extends Chunk {
- /** The size of a resource reference for {@code resources} in bytes. */
- private static final int RESOURCE_SIZE = 4;
+ /**
+ * The size of a resource reference for {@code resources} in bytes.
+ */
+ private static final int RESOURCE_SIZE = 4;
- /**
- * Contains a mapping of attributeID to resourceID. For example, the attributeID 2 refers to the
- * resourceID returned by {@code resources.get(2)}.
- */
- private final List resources = new ArrayList<>();
+ /**
+ * Contains a mapping of attributeID to resourceID. For example, the attributeID 2 refers to the
+ * resourceID returned by {@code resources.get(2)}.
+ */
+ private final List resources = new ArrayList<>();
- protected XmlResourceMapChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- }
+ protected XmlResourceMapChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ }
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- resources.addAll(enumerateResources(buffer));
- }
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ resources.addAll(enumerateResources(buffer));
+ }
- private List enumerateResources(ByteBuffer buffer) {
- int resourceCount = (getOriginalChunkSize() - getHeaderSize()) / RESOURCE_SIZE;
- List result = new ArrayList<>(resourceCount);
- int offset = this.offset + getHeaderSize();
- buffer.mark();
- buffer.position(offset);
+ private List enumerateResources(ByteBuffer buffer) {
+ int resourceCount = (getOriginalChunkSize() - getHeaderSize()) / RESOURCE_SIZE;
+ List result = new ArrayList<>(resourceCount);
+ int offset = this.offset + getHeaderSize();
+ buffer.mark();
+ buffer.position(offset);
- for (int i = 0; i < resourceCount; ++i) {
- result.add(buffer.getInt());
- }
+ for (int i = 0; i < resourceCount; ++i) {
+ result.add(buffer.getInt());
+ }
- buffer.reset();
- return result;
- }
+ buffer.reset();
+ return result;
+ }
- /** Returns the resource ID that this {@code attributeId} maps to. */
- public BinaryResourceIdentifier getResourceId(int attributeId) {
- return BinaryResourceIdentifier.create(resources.get(attributeId));
- }
+ /**
+ * Returns the resource ID that this {@code attributeId} maps to.
+ */
+ public BinaryResourceIdentifier getResourceId(int attributeId) {
+ return BinaryResourceIdentifier.create(resources.get(attributeId));
+ }
- @Override
- protected Type getType() {
- return Type.XML_RESOURCE_MAP;
- }
+ @Override
+ protected Type getType() {
+ return Type.XML_RESOURCE_MAP;
+ }
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- super.writePayload(output, header, shrink);
- for (Integer resource : resources) {
- output.writeInt(resource);
+ @Override
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ super.writePayload(output, header, shrink);
+ for (Integer resource : resources) {
+ output.writeInt(resource);
+ }
}
- }
}
diff --git a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlStartElementChunk.java b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlStartElementChunk.java
index 523dcaf1a4..d219dec230 100644
--- a/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlStartElementChunk.java
+++ b/app/src/main/java/com/google/devrel/gmscore/tools/apk/arsc/XmlStartElementChunk.java
@@ -16,9 +16,9 @@
package com.google.devrel.gmscore.tools.apk.arsc;
+import androidx.annotation.Nullable;
import com.google.common.base.Preconditions;
-import androidx.annotation.Nullable;
import java.io.DataOutput;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -26,120 +26,144 @@
import java.util.Collections;
import java.util.List;
-/** Represents the beginning of an XML node. */
+/**
+ * Represents the beginning of an XML node.
+ */
public final class XmlStartElementChunk extends XmlNodeChunk {
- /** A string reference to the namespace URI, or -1 if not present. */
- private final int namespace;
-
- /** A string reference to the element name that this chunk represents. */
- private final int name;
-
- /** The offset to the start of the attributes payload. */
- private final int attributeStart;
-
- /** The number of attributes in the original buffer. */
- private final int attributeCount;
-
- /** The (0-based) index of the id attribute, or -1 if not present. */
- private final int idIndex;
-
- /** The (0-based) index of the class attribute, or -1 if not present. */
- private final int classIndex;
-
- /** The (0-based) index of the style attribute, or -1 if not present. */
- private final int styleIndex;
-
- /** The XML attributes associated with this element. */
- private final List attributes = new ArrayList<>();
-
- protected XmlStartElementChunk(ByteBuffer buffer, @Nullable Chunk parent) {
- super(buffer, parent);
- namespace = buffer.getInt();
- name = buffer.getInt();
- attributeStart = (buffer.getShort() & 0xFFFF);
- int attributeSize = (buffer.getShort() & 0xFFFF);
- Preconditions.checkState(attributeSize == XmlAttribute.SIZE,
- "attributeSize is wrong size. Got %s, want %s", attributeSize, XmlAttribute.SIZE);
- attributeCount = (buffer.getShort() & 0xFFFF);
-
- // The following indices are 1-based and need to be adjusted.
- idIndex = (buffer.getShort() & 0xFFFF) - 1;
- classIndex = (buffer.getShort() & 0xFFFF) - 1;
- styleIndex = (buffer.getShort() & 0xFFFF) - 1;
- }
-
- @Override
- protected void init(ByteBuffer buffer) {
- super.init(buffer);
- attributes.addAll(enumerateAttributes(buffer));
- }
-
- private List enumerateAttributes(ByteBuffer buffer) {
- List result = new ArrayList<>(attributeCount);
- int offset = this.offset + getHeaderSize() + attributeStart;
- int endOffset = offset + XmlAttribute.SIZE * attributeCount;
- buffer.mark();
- buffer.position(offset);
-
- while (offset < endOffset) {
- result.add(XmlAttribute.create(buffer, this));
- offset += XmlAttribute.SIZE;
+ /**
+ * A string reference to the namespace URI, or -1 if not present.
+ */
+ private final int namespace;
+
+ /**
+ * A string reference to the element name that this chunk represents.
+ */
+ private final int name;
+
+ /**
+ * The offset to the start of the attributes payload.
+ */
+ private final int attributeStart;
+
+ /**
+ * The number of attributes in the original buffer.
+ */
+ private final int attributeCount;
+
+ /**
+ * The (0-based) index of the id attribute, or -1 if not present.
+ */
+ private final int idIndex;
+
+ /**
+ * The (0-based) index of the class attribute, or -1 if not present.
+ */
+ private final int classIndex;
+
+ /**
+ * The (0-based) index of the style attribute, or -1 if not present.
+ */
+ private final int styleIndex;
+
+ /**
+ * The XML attributes associated with this element.
+ */
+ private final List attributes = new ArrayList<>();
+
+ protected XmlStartElementChunk(ByteBuffer buffer, @Nullable Chunk parent) {
+ super(buffer, parent);
+ namespace = buffer.getInt();
+ name = buffer.getInt();
+ attributeStart = (buffer.getShort() & 0xFFFF);
+ int attributeSize = (buffer.getShort() & 0xFFFF);
+ Preconditions.checkState(attributeSize == XmlAttribute.SIZE,
+ "attributeSize is wrong size. Got %s, want %s", attributeSize, XmlAttribute.SIZE);
+ attributeCount = (buffer.getShort() & 0xFFFF);
+
+ // The following indices are 1-based and need to be adjusted.
+ idIndex = (buffer.getShort() & 0xFFFF) - 1;
+ classIndex = (buffer.getShort() & 0xFFFF) - 1;
+ styleIndex = (buffer.getShort() & 0xFFFF) - 1;
+ }
+
+ @Override
+ protected void init(ByteBuffer buffer) {
+ super.init(buffer);
+ attributes.addAll(enumerateAttributes(buffer));
+ }
+
+ private List enumerateAttributes(ByteBuffer buffer) {
+ List result = new ArrayList<>(attributeCount);
+ int offset = this.offset + getHeaderSize() + attributeStart;
+ int endOffset = offset + XmlAttribute.SIZE * attributeCount;
+ buffer.mark();
+ buffer.position(offset);
+
+ while (offset < endOffset) {
+ result.add(XmlAttribute.create(buffer, this));
+ offset += XmlAttribute.SIZE;
+ }
+
+ buffer.reset();
+ return result;
+ }
+
+ /**
+ * Returns the namespace URI, or the empty string if not present.
+ */
+ public String getNamespace() {
+ return getString(namespace);
+ }
+
+ /**
+ * Returns the element name that this chunk represents.
+ */
+ public String getName() {
+ return getString(name);
+ }
+
+ /**
+ * Returns an unmodifiable list of this XML element's attributes.
+ */
+ public List getAttributes() {
+ return Collections.unmodifiableList(attributes);
+ }
+
+ @Override
+ protected Type getType() {
+ return Type.XML_START_ELEMENT;
+ }
+
+ @Override
+ protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
+ throws IOException {
+ super.writePayload(output, header, shrink);
+ output.writeInt(namespace);
+ output.writeInt(name);
+ output.writeShort((short) XmlAttribute.SIZE); // attribute start
+ output.writeShort((short) XmlAttribute.SIZE);
+ output.writeShort((short) attributes.size());
+ output.writeShort((short) (idIndex + 1));
+ output.writeShort((short) (classIndex + 1));
+ output.writeShort((short) (styleIndex + 1));
+ for (XmlAttribute attribute : attributes) {
+ output.write(attribute.toByteArray(shrink));
+ }
}
- buffer.reset();
- return result;
- }
-
- /** Returns the namespace URI, or the empty string if not present. */
- public String getNamespace() {
- return getString(namespace);
- }
-
- /** Returns the element name that this chunk represents. */
- public String getName() {
- return getString(name);
- }
-
- /** Returns an unmodifiable list of this XML element's attributes. */
- public List getAttributes() {
- return Collections.unmodifiableList(attributes);
- }
-
- @Override
- protected Type getType() {
- return Type.XML_START_ELEMENT;
- }
-
- @Override
- protected void writePayload(DataOutput output, ByteBuffer header, boolean shrink)
- throws IOException {
- super.writePayload(output, header, shrink);
- output.writeInt(namespace);
- output.writeInt(name);
- output.writeShort((short) XmlAttribute.SIZE); // attribute start
- output.writeShort((short) XmlAttribute.SIZE);
- output.writeShort((short) attributes.size());
- output.writeShort((short) (idIndex + 1));
- output.writeShort((short) (classIndex + 1));
- output.writeShort((short) (styleIndex + 1));
- for (XmlAttribute attribute : attributes) {
- output.write(attribute.toByteArray(shrink));
+ /**
+ * Returns a brief description of this XML node. The representation of this information is
+ * subject to change, but below is a typical example:
+ *
+ *
+ */
+ @Override
+ public String toString() {
+ return String.format(
+ "XmlStartElementChunk{line=%d, comment=%s, namespace=%s, name=%s, attributes=%s}",
+ getLineNumber(), getComment(), getNamespace(), getName(), attributes.toString());
}
- }
-
- /**
- * Returns a brief description of this XML node. The representation of this information is
- * subject to change, but below is a typical example:
- *
- *