Search in sources :

Example 6 with AndrolibException

use of com.tencent.mm.androlib.AndrolibException in project AndResGuard by shwenzhang.

the class RawARSCDecoder method readConfigFlags.

private void readConfigFlags() throws IOException, AndrolibException {
    int read = 28;
    int size = mIn.readInt();
    if (size < 28) {
        throw new AndrolibException("Config size < 28");
    }
    boolean isInvalid = false;
    short mcc = mIn.readShort();
    short mnc = mIn.readShort();
    char[] language = new char[] { (char) mIn.readByte(), (char) mIn.readByte() };
    char[] country = new char[] { (char) mIn.readByte(), (char) mIn.readByte() };
    byte orientation = mIn.readByte();
    byte touchscreen = mIn.readByte();
    int density = mIn.readUnsignedShort();
    byte keyboard = mIn.readByte();
    byte navigation = mIn.readByte();
    byte inputFlags = mIn.readByte();
    /* inputPad0 */
    mIn.skipBytes(1);
    short screenWidth = mIn.readShort();
    short screenHeight = mIn.readShort();
    short sdkVersion = mIn.readShort();
    /* minorVersion, now must always be 0 */
    mIn.skipBytes(2);
    byte screenLayout = 0;
    byte uiMode = 0;
    short smallestScreenWidthDp = 0;
    if (size >= 32) {
        screenLayout = mIn.readByte();
        uiMode = mIn.readByte();
        smallestScreenWidthDp = mIn.readShort();
        read = 32;
    }
    short screenWidthDp = 0;
    short screenHeightDp = 0;
    if (size >= 36) {
        screenWidthDp = mIn.readShort();
        screenHeightDp = mIn.readShort();
        read = 36;
    }
    char[] localeScript = null;
    char[] localeVariant = null;
    if (size >= 48) {
        localeScript = readScriptOrVariantChar(4).toCharArray();
        localeVariant = readScriptOrVariantChar(8).toCharArray();
        read = 48;
    }
    byte screenLayout2 = 0;
    if (size >= 52) {
        screenLayout2 = mIn.readByte();
        // reserved padding
        mIn.skipBytes(3);
        read = 52;
    }
    if (size >= 56) {
        mIn.skipBytes(4);
        read = 56;
    }
    int exceedingSize = size - KNOWN_CONFIG_BYTES;
    if (exceedingSize > 0) {
        byte[] buf = new byte[exceedingSize];
        mIn.readFully(buf);
        BigInteger exceedingBI = new BigInteger(1, buf);
        if (exceedingBI.equals(BigInteger.ZERO)) {
            LOGGER.fine(String.format("Config flags size > %d, but exceeding bytes are all zero, so it should be ok.", KNOWN_CONFIG_BYTES));
        } else {
            LOGGER.warning(String.format("Config flags size > %d. Exceeding bytes: 0x%X.", KNOWN_CONFIG_BYTES, exceedingBI));
            isInvalid = true;
        }
    }
}
Also used : AndrolibException(com.tencent.mm.androlib.AndrolibException) BigInteger(java.math.BigInteger)

Example 7 with AndrolibException

use of com.tencent.mm.androlib.AndrolibException in project AndResGuard by shwenzhang.

the class StringBlock method writeTableNameStringBlock.

public static int writeTableNameStringBlock(ExtDataInput reader, ExtDataOutput out, Map<Integer, String> tableProguardMap) throws IOException, AndrolibException {
    int type = reader.readInt();
    int chunkSize = reader.readInt();
    int stringCount = reader.readInt();
    int styleOffsetCount = reader.readInt();
    int flags = reader.readInt();
    int stringsOffset = reader.readInt();
    int stylesOffset = reader.readInt();
    StringBlock block = new StringBlock();
    block.m_isUTF8 = (flags & UTF8_FLAG) != 0;
    if (block.m_isUTF8) {
        System.out.printf("resources.arsc Character Encoding: utf-8\n");
    } else {
        System.out.printf("resources.arsc Character Encoding: utf-16\n");
    }
    block.m_stringOffsets = reader.readIntArray(stringCount);
    block.m_stringOwns = new int[stringCount];
    for (int i = 0; i < stringCount; i++) {
        block.m_stringOwns[i] = -1;
    }
    if (styleOffsetCount != 0) {
        block.m_styleOffsets = reader.readIntArray(styleOffsetCount);
    }
    {
        int size = ((stylesOffset == 0) ? chunkSize : stylesOffset) - stringsOffset;
        if ((size % 4) != 0) {
            throw new IOException("String data size is not multiple of 4 (" + size + ").");
        }
        block.m_strings = new byte[size];
        reader.readFully(block.m_strings);
    }
    if (stylesOffset != 0) {
        int size = (chunkSize - stylesOffset);
        if ((size % 4) != 0) {
            throw new IOException("Style data size is not multiple of 4 (" + size + ").");
        }
        block.m_styles = reader.readIntArray(size / 4);
    }
    int totalSize = 0;
    out.writeCheckInt(type, CHUNK_STRINGPOOL_TYPE);
    totalSize += 4;
    totalSize += 6 * 4 + 4 * stringCount + 4 * styleOffsetCount;
    stringsOffset = totalSize;
    byte[] strings = new byte[block.m_strings.length];
    int[] stringOffsets = new int[stringCount];
    System.arraycopy(block.m_stringOffsets, 0, stringOffsets, 0, stringOffsets.length);
    int offset = 0;
    int i;
    for (i = 0; i < stringCount; i++) {
        stringOffsets[i] = offset;
        //如果找不到即没混淆这一项,直接拷贝
        if (tableProguardMap.get(i) == null) {
            //需要区分是否是最后一项
            int copyLen = (i == (stringCount - 1)) ? (block.m_strings.length - block.m_stringOffsets[i]) : (block.m_stringOffsets[i + 1] - block.m_stringOffsets[i]);
            System.arraycopy(block.m_strings, block.m_stringOffsets[i], strings, offset, copyLen);
            offset += copyLen;
            totalSize += copyLen;
        } else {
            String name = tableProguardMap.get(i);
            if (block.m_isUTF8) {
                strings[offset++] = (byte) name.length();
                strings[offset++] = (byte) name.length();
                totalSize += 2;
                byte[] tempByte = name.getBytes(Charset.forName("UTF-8"));
                if (name.length() != tempByte.length) {
                    throw new AndrolibException(String.format("writeTableNameStringBlock lenght is different  name %d, tempByte %d\n", name.length(), tempByte.length));
                }
                System.arraycopy(tempByte, 0, strings, offset, tempByte.length);
                offset += name.length();
                strings[offset++] = NULL;
                totalSize += name.length() + 1;
            } else {
                writeShort(strings, offset, (short) name.length());
                offset += 2;
                totalSize += 2;
                byte[] tempByte = name.getBytes(Charset.forName("UTF-16LE"));
                if ((name.length() * 2) != tempByte.length) {
                    throw new AndrolibException(String.format("writeTableNameStringBlock lenght is different  name %d, tempByte %d\n", name.length(), tempByte.length));
                }
                System.arraycopy(tempByte, 0, strings, offset, tempByte.length);
                offset += tempByte.length;
                strings[offset++] = NULL;
                strings[offset++] = NULL;
                totalSize += tempByte.length + 2;
            }
        }
    }
    //要保证string size 是4的倍数,要补零
    int size = totalSize - stringsOffset;
    if ((size % 4) != 0) {
        int add = 4 - (size % 4);
        for (i = 0; i < add; i++) {
            strings[offset++] = NULL;
            totalSize++;
        }
    }
    //因为是int的,如果之前的不为0
    if (stylesOffset != 0) {
        stylesOffset = totalSize;
        totalSize += block.m_styles.length * 4;
    }
    out.writeInt(totalSize);
    out.writeInt(stringCount);
    out.writeInt(styleOffsetCount);
    out.writeInt(flags);
    out.writeInt(stringsOffset);
    out.writeInt(stylesOffset);
    out.writeIntArray(stringOffsets);
    if (stylesOffset != 0) {
        out.writeIntArray(block.m_styleOffsets);
    }
    out.write(strings, 0, offset);
    if (stylesOffset != 0) {
        out.writeIntArray(block.m_styles);
    }
    return (chunkSize - totalSize);
}
Also used : AndrolibException(com.tencent.mm.androlib.AndrolibException) IOException(java.io.IOException)

Example 8 with AndrolibException

use of com.tencent.mm.androlib.AndrolibException in project AndResGuard by shwenzhang.

the class RawARSCDecoder method decode.

public static ResPackage[] decode(InputStream arscStream) throws AndrolibException {
    try {
        RawARSCDecoder decoder = new RawARSCDecoder(arscStream);
        System.out.printf("parse to get the exist names in the resouces.arsc first\n");
        ResPackage[] pkgs = decoder.readTable();
        return pkgs;
    } catch (IOException ex) {
        throw new AndrolibException("Could not decode arsc file", ex);
    }
}
Also used : AndrolibException(com.tencent.mm.androlib.AndrolibException) IOException(java.io.IOException) ResPackage(com.tencent.mm.androlib.res.data.ResPackage)

Example 9 with AndrolibException

use of com.tencent.mm.androlib.AndrolibException in project AndResGuard by shwenzhang.

the class ARSCDecoder method writeEntry.

private void writeEntry() throws IOException, AndrolibException {
    /* size */
    mOut.writeBytes(mIn, 2);
    short flags = mIn.readShort();
    mOut.writeShort(flags);
    int specNamesId = mIn.readInt();
    ResPackage pkg = mPkgs[mCurPackageID];
    if (pkg.isCanProguard()) {
        specNamesId = mCurSpecNameToPos.get(pkg.getSpecRepplace(mResId));
        if (specNamesId < 0) {
            throw new AndrolibException(String.format("writeEntry new specNamesId < 0 %d", specNamesId));
        }
    }
    mOut.writeInt(specNamesId);
    if ((flags & ENTRY_FLAG_COMPLEX) == 0) {
        writeValue();
    } else {
        writeComplexEntry();
    }
}
Also used : AndrolibException(com.tencent.mm.androlib.AndrolibException) ResPackage(com.tencent.mm.androlib.res.data.ResPackage)

Aggregations

AndrolibException (com.tencent.mm.androlib.AndrolibException)9 IOException (java.io.IOException)4 ResPackage (com.tencent.mm.androlib.res.data.ResPackage)3 BigInteger (java.math.BigInteger)3 Configuration (com.tencent.mm.resourceproguard.Configuration)1 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 Pattern (java.util.regex.Pattern)1