package jadx.core.xmlgen;

import java.io.IOException;

public class CommonBinaryParser extends ParserConstants {

	protected ParserStream is;

	protected String[] parseStringPool() throws IOException {
		is.checkInt16(RES_STRING_POOL_TYPE, "String pool expected");
		return parseStringPoolNoType();
	}

	protected String[] parseStringPoolNoType() throws IOException {
		long start = is.getPos() - 2;
		is.checkInt16(0x001c, "String pool header size not 0x001c");
		long size = is.readUInt32();

		int stringCount = is.readInt32();
		int styleCount = is.readInt32();
		int flags = is.readInt32();
		long stringsStart = is.readInt32();
		long stylesStart = is.readInt32();

		int[] stringsOffset = is.readInt32Array(stringCount);
		int[] stylesOffset = is.readInt32Array(styleCount);

		is.checkPos(start + stringsStart, "Expected strings start");
		String[] strings = new String[stringCount];
		if ((flags & UTF8_FLAG) != 0) {
			// UTF-8
			for (int i = 0; i < stringCount; i++) {
				// is.checkPos(start + stringsStart + stringsOffset[i], "Expected string start");
				strings[i] = is.readString8();
			}
		} else {
			// UTF-16
			long stringsStartOffset = start + stringsStart;
			for (int i = 0; i < stringCount; i++) {
				// is.checkPos(stringsStartOffset + stringsOffset[i], "Expected string start");
				// TODO: don't trust specified string length, read until \0
				// TODO: stringsOffset can be same for different indexes
				strings[i] = is.readString16();
			}
		}
		if (stylesStart != 0) {
			is.checkPos(start + stylesStart, "Expected styles start");
			if (styleCount != 0) {
				// TODO: implement styles parsing
			}
		}
		// skip padding zeroes
		is.skip(start + size - is.getPos());
		return strings;
	}

	protected void die(String message) throws IOException {
		throw new IOException("Decode error: " + message
				+ ", position: 0x" + Long.toHexString(is.getPos()));
	}

}

@@ -483,24 +483,56 @@
 eadUInt32();
+%0A%09%09long chunkEnd = start + size;
 %0A%0A%09%09int stri
@@ -1169,28 +1169,43 @@
 ong 
-stringsStartOffset =
+end = stylesStart == 0 ? chunkEnd :
  sta
@@ -1207,28 +1207,27 @@
 : start + st
-ring
+yle
 sStart;%0A%09%09%09f
@@ -1229,144 +1229,120 @@
 %0A%09%09%09
-for (int i = 0; i %3C stringCount; i++) %7B%0A%09%09%09%09// is.checkPos(stringsStartOffset + stringsOffset%5Bi%5D, %22Expected string start%22);
+byte%5B%5D strArray = is.readInt8Array((int) (end - is.getPos()));%0A%09%09%09for (int i = 0; i %3C stringCount; i++) %7B
 %0A%09%09%09%09//
- TODO:
  don
@@ -1395,22 +1395,16 @@
 0%0A%09%09%09%09//
- TODO:
  strings
@@ -1457,31 +1457,31 @@
 trings%5Bi%5D = 
-is.read
+extract
 String16();%0A
@@ -1477,16 +1477,42 @@
 tring16(
+strArray, stringsOffset%5Bi%5D
 );%0A%09%09%09%7D%0A
@@ -1715,53 +1715,429 @@
 kip(
-start + size - is.getPos());%0A%09%09return strings
+chunkEnd - is.getPos());%0A%09%09return strings;%0A%09%7D%0A%0A%09private static String extractString16(byte%5B%5D strArray, int offset) %7B%0A%09%09int len = strArray.length;%0A%09%09int start = offset + 2;%0A%09%09int end = start;%0A%09%09while (true) %7B%0A%09%09%09if (end + 1 %3E= len) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%09if (strArray%5Bend%5D == 0 && strArray%5Bend + 1%5D == 0) %7B%0A%09%09%09%09break;%0A%09%09%09%7D%0A%09%09%09end += 2;%0A%09%09%7D%0A%09%09return new String(strArray, start, end - start, ParserStream.STRING_CHARSET_UTF16)
 ;%0A%09%7D

fix strings pool parsing in '.arsc' file