package com.tencent.mm.plugin.downloader.d;

import com.tencent.matrix.trace.core.AppMethodBeat;
import com.tencent.mm.plugin.downloader.d.b.b;
import com.tencent.mm.sdk.platformtools.ab;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public final class i {
    public static Map<Integer, ByteBuffer> E(ByteBuffer byteBuffer) {
        b bVar;
        AppMethodBeat.i(2326);
        b.D(byteBuffer);
        ByteBuffer d = b.d(byteBuffer, byteBuffer.capacity() - 24);
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        int i = 0;
        while (d.hasRemaining()) {
            i++;
            if (d.remaining() < 8) {
                bVar = new b("Insufficient data to read size of APK Signing Block entry #".concat(String.valueOf(i)));
                AppMethodBeat.o(2326);
                throw bVar;
            }
            long j = d.getLong();
            if (j < 4 || j > 2147483647L) {
                bVar = new b("APK Signing Block entry #" + i + " size out of range: " + j);
                AppMethodBeat.o(2326);
                throw bVar;
            }
            int i2 = (int) j;
            int position = d.position() + i2;
            if (i2 > d.remaining()) {
                b bVar2 = new b("APK Signing Block entry #" + i + " size out of range: " + i2 + ", available: " + d.remaining());
                AppMethodBeat.o(2326);
                throw bVar2;
            }
            int i3 = d.getInt();
            linkedHashMap.put(Integer.valueOf(i3), b.e(d, i2 - 4));
            if (i3 == 1896449818) {
                ab.i("MicroMsg.Channel.V2SchemeUtil", "find V2 signature block Id : 1896449818");
            }
            d.position(position);
        }
        if (linkedHashMap.isEmpty()) {
            bVar = new b("not have Id-Value Pair in APK Signing Block entry #".concat(String.valueOf(i)));
            AppMethodBeat.o(2326);
            throw bVar;
        }
        AppMethodBeat.o(2326);
        return linkedHashMap;
    }

    /* JADX WARNING: Removed duplicated region for block: B:18:0x0048  */
    public static java.nio.ByteBuffer P(java.io.File r6) {
        /*
        r0 = 0;
        r3 = 2327; // 0x917 float:3.261E-42 double:1.1497E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r3);
        if (r6 == 0) goto L_0x0014;
    L_0x0008:
        r1 = r6.exists();
        if (r1 == 0) goto L_0x0014;
    L_0x000e:
        r1 = r6.isFile();
        if (r1 != 0) goto L_0x0018;
    L_0x0014:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r3);
    L_0x0017:
        return r0;
    L_0x0018:
        r2 = new java.io.RandomAccessFile;	 Catch:{ all -> 0x0062 }
        r1 = "r";
        r2.<init>(r6, r1);	 Catch:{ all -> 0x0062 }
        r1 = com.tencent.mm.plugin.downloader.d.b.e(r2);	 Catch:{ all -> 0x0044 }
        r0 = r1.first;	 Catch:{ all -> 0x0044 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0044 }
        r1 = r1.second;	 Catch:{ all -> 0x0044 }
        r1 = (java.lang.Long) r1;	 Catch:{ all -> 0x0044 }
        r4 = r1.longValue();	 Catch:{ all -> 0x0044 }
        r1 = com.tencent.mm.plugin.downloader.d.k.c(r2, r4);	 Catch:{ all -> 0x0044 }
        if (r1 == 0) goto L_0x004f;
    L_0x0036:
        r0 = new com.tencent.mm.plugin.downloader.d.b$b;	 Catch:{ all -> 0x0044 }
        r1 = "ZIP64 APK not supported";
        r0.<init>(r1);	 Catch:{ all -> 0x0044 }
        r1 = 2327; // 0x917 float:3.261E-42 double:1.1497E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r1);	 Catch:{ all -> 0x0044 }
        throw r0;	 Catch:{ all -> 0x0044 }
    L_0x0044:
        r0 = move-exception;
        r1 = r0;
    L_0x0046:
        if (r2 == 0) goto L_0x004b;
    L_0x0048:
        r2.close();
    L_0x004b:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r3);
        throw r1;
    L_0x004f:
        r0 = com.tencent.mm.plugin.downloader.d.b.b(r0, r4);	 Catch:{ all -> 0x0044 }
        r0 = com.tencent.mm.plugin.downloader.d.b.b(r2, r0);	 Catch:{ all -> 0x0044 }
        r0 = r0.first;	 Catch:{ all -> 0x0044 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0044 }
        r2.close();
        com.tencent.matrix.trace.core.AppMethodBeat.o(r3);
        goto L_0x0017;
    L_0x0062:
        r1 = move-exception;
        r2 = r0;
        goto L_0x0046;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.plugin.downloader.d.i.P(java.io.File):java.nio.ByteBuffer");
    }

    /* JADX WARNING: Removed duplicated region for block: B:44:0x0176 A:{Catch:{ all -> 0x0035 }} */
    /* JADX WARNING: Removed duplicated region for block: B:41:0x0155 A:{Catch:{ all -> 0x0035 }} */
    /* JADX WARNING: Removed duplicated region for block: B:11:0x0039  */
    public static com.tencent.mm.plugin.downloader.d.a Q(java.io.File r14) {
        /*
        r3 = 0;
        r12 = 0;
        r10 = 2328; // 0x918 float:3.262E-42 double:1.15E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.i(r10);
        r1 = 0;
        r2 = new java.io.RandomAccessFile;	 Catch:{ all -> 0x01ee }
        r0 = "r";
        r2.<init>(r14, r0);	 Catch:{ all -> 0x01ee }
        r4 = com.tencent.mm.plugin.downloader.d.b.e(r2);	 Catch:{ all -> 0x0035 }
        r0 = r4.first;	 Catch:{ all -> 0x0035 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0035 }
        r1 = r4.second;	 Catch:{ all -> 0x0035 }
        r1 = (java.lang.Long) r1;	 Catch:{ all -> 0x0035 }
        r6 = r1.longValue();	 Catch:{ all -> 0x0035 }
        r1 = com.tencent.mm.plugin.downloader.d.k.c(r2, r6);	 Catch:{ all -> 0x0035 }
        if (r1 == 0) goto L_0x0040;
    L_0x0027:
        r0 = new com.tencent.mm.plugin.downloader.d.b$b;	 Catch:{ all -> 0x0035 }
        r1 = "ZIP64 APK not supported";
        r0.<init>(r1);	 Catch:{ all -> 0x0035 }
        r1 = 2328; // 0x918 float:3.262E-42 double:1.15E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r1);	 Catch:{ all -> 0x0035 }
        throw r0;	 Catch:{ all -> 0x0035 }
    L_0x0035:
        r0 = move-exception;
        r1 = r2;
    L_0x0037:
        if (r1 == 0) goto L_0x003c;
    L_0x0039:
        r1.close();
    L_0x003c:
        com.tencent.matrix.trace.core.AppMethodBeat.o(r10);
        throw r0;
    L_0x0040:
        r0 = com.tencent.mm.plugin.downloader.d.b.b(r0, r6);	 Catch:{ all -> 0x0035 }
        r5 = com.tencent.mm.plugin.downloader.d.b.b(r2, r0);	 Catch:{ all -> 0x0035 }
        r6 = r6 - r0;
        r6 = (int) r6;	 Catch:{ all -> 0x0035 }
        r6 = a(r2, r0, r6);	 Catch:{ all -> 0x0035 }
        r0 = java.lang.Long.valueOf(r0);	 Catch:{ all -> 0x0035 }
        r1 = android.util.Pair.create(r6, r0);	 Catch:{ all -> 0x0035 }
        r6 = new com.tencent.mm.plugin.downloader.d.a;	 Catch:{ all -> 0x0035 }
        r6.<init>();	 Catch:{ all -> 0x0035 }
        r0 = 0;
        r6.lowMemory = r0;	 Catch:{ all -> 0x0035 }
        r8 = r14.length();	 Catch:{ all -> 0x0035 }
        r6.kLT = r8;	 Catch:{ all -> 0x0035 }
        r0 = r5.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r8 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = (int) r8;	 Catch:{ all -> 0x0035 }
        r8 = 0;
        r0 = a(r2, r8, r0);	 Catch:{ all -> 0x0035 }
        r8 = 0;
        r7 = java.lang.Long.valueOf(r8);	 Catch:{ all -> 0x0035 }
        r0 = android.util.Pair.create(r0, r7);	 Catch:{ all -> 0x0035 }
        r6.kLU = r0;	 Catch:{ all -> 0x0035 }
        r6.kLV = r5;	 Catch:{ all -> 0x0035 }
        r6.kLW = r1;	 Catch:{ all -> 0x0035 }
        r6.kLX = r4;	 Catch:{ all -> 0x0035 }
        r0 = r6.lowMemory;	 Catch:{ all -> 0x0035 }
        if (r0 != 0) goto L_0x008d;
    L_0x0089:
        r0 = r6.kLU;	 Catch:{ all -> 0x0035 }
        if (r0 == 0) goto L_0x0099;
    L_0x008d:
        r0 = r6.kLV;	 Catch:{ all -> 0x0035 }
        if (r0 == 0) goto L_0x0099;
    L_0x0091:
        r0 = r6.kLW;	 Catch:{ all -> 0x0035 }
        if (r0 == 0) goto L_0x0099;
    L_0x0095:
        r0 = r6.kLX;	 Catch:{ all -> 0x0035 }
        if (r0 != 0) goto L_0x00b8;
    L_0x0099:
        r0 = new java.lang.RuntimeException;	 Catch:{ all -> 0x0035 }
        r1 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0035 }
        r3 = "ApkSectionInfo paramters is not valid : ";
        r1.<init>(r3);	 Catch:{ all -> 0x0035 }
        r3 = r6.toString();	 Catch:{ all -> 0x0035 }
        r1 = r1.append(r3);	 Catch:{ all -> 0x0035 }
        r1 = r1.toString();	 Catch:{ all -> 0x0035 }
        r0.<init>(r1);	 Catch:{ all -> 0x0035 }
        r1 = 2328; // 0x918 float:3.262E-42 double:1.15E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r1);	 Catch:{ all -> 0x0035 }
        throw r0;	 Catch:{ all -> 0x0035 }
    L_0x00b8:
        r0 = r6.lowMemory;	 Catch:{ all -> 0x0035 }
        if (r0 != 0) goto L_0x00ee;
    L_0x00bc:
        r0 = r6.kLU;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = (r0 > r12 ? 1 : (r0 == r12 ? 0 : -1));
        if (r0 != 0) goto L_0x0174;
    L_0x00ca:
        r0 = r6.kLU;	 Catch:{ all -> 0x0035 }
        r0 = r0.first;	 Catch:{ all -> 0x0035 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.remaining();	 Catch:{ all -> 0x0035 }
        r4 = (long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r6.kLU;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r4 = r4 + r0;
        r0 = r6.kLV;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1));
        if (r0 != 0) goto L_0x0174;
    L_0x00ee:
        r0 = r6.kLV;	 Catch:{ all -> 0x0035 }
        r0 = r0.first;	 Catch:{ all -> 0x0035 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.remaining();	 Catch:{ all -> 0x0035 }
        r4 = (long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r6.kLV;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r4 = r4 + r0;
        r0 = r6.kLW;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1));
        if (r0 != 0) goto L_0x0174;
    L_0x0112:
        r0 = r6.kLW;	 Catch:{ all -> 0x0035 }
        r0 = r0.first;	 Catch:{ all -> 0x0035 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.remaining();	 Catch:{ all -> 0x0035 }
        r4 = (long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r6.kLW;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r4 = r4 + r0;
        r0 = r6.kLX;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1));
        if (r0 != 0) goto L_0x0174;
    L_0x0136:
        r0 = r6.kLX;	 Catch:{ all -> 0x0035 }
        r0 = r0.first;	 Catch:{ all -> 0x0035 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.remaining();	 Catch:{ all -> 0x0035 }
        r4 = (long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r6.kLX;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = r0 + r4;
        r4 = r6.kLT;	 Catch:{ all -> 0x0035 }
        r0 = (r0 > r4 ? 1 : (r0 == r4 ? 0 : -1));
        if (r0 != 0) goto L_0x0174;
    L_0x0152:
        r0 = 1;
    L_0x0153:
        if (r0 != 0) goto L_0x0176;
    L_0x0155:
        r0 = new java.lang.RuntimeException;	 Catch:{ all -> 0x0035 }
        r1 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0035 }
        r3 = "ApkSectionInfo paramters is not valid : ";
        r1.<init>(r3);	 Catch:{ all -> 0x0035 }
        r3 = r6.toString();	 Catch:{ all -> 0x0035 }
        r1 = r1.append(r3);	 Catch:{ all -> 0x0035 }
        r1 = r1.toString();	 Catch:{ all -> 0x0035 }
        r0.<init>(r1);	 Catch:{ all -> 0x0035 }
        r1 = 2328; // 0x918 float:3.262E-42 double:1.15E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r1);	 Catch:{ all -> 0x0035 }
        throw r0;	 Catch:{ all -> 0x0035 }
    L_0x0174:
        r0 = r3;
        goto L_0x0153;
    L_0x0176:
        r0 = r6.kLX;	 Catch:{ all -> 0x0035 }
        r0 = r0.first;	 Catch:{ all -> 0x0035 }
        r0 = (java.nio.ByteBuffer) r0;	 Catch:{ all -> 0x0035 }
        r1 = r6.kLX;	 Catch:{ all -> 0x0035 }
        r1 = r1.second;	 Catch:{ all -> 0x0035 }
        r1 = (java.lang.Long) r1;	 Catch:{ all -> 0x0035 }
        r4 = r1.longValue();	 Catch:{ all -> 0x0035 }
        r4 = com.tencent.mm.plugin.downloader.d.b.b(r0, r4);	 Catch:{ all -> 0x0035 }
        r0 = r6.kLW;	 Catch:{ all -> 0x0035 }
        r0 = r0.second;	 Catch:{ all -> 0x0035 }
        r0 = (java.lang.Long) r0;	 Catch:{ all -> 0x0035 }
        r0 = r0.longValue();	 Catch:{ all -> 0x0035 }
        r0 = (r4 > r0 ? 1 : (r4 == r0 ? 0 : -1));
        if (r0 == 0) goto L_0x01c2;
    L_0x0198:
        r0 = new java.lang.RuntimeException;	 Catch:{ all -> 0x0035 }
        r1 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0035 }
        r3 = "CentralDirOffset mismatch , EocdCentralDirOffset : ";
        r1.<init>(r3);	 Catch:{ all -> 0x0035 }
        r1 = r1.append(r4);	 Catch:{ all -> 0x0035 }
        r3 = ", centralDirOffset : ";
        r1 = r1.append(r3);	 Catch:{ all -> 0x0035 }
        r3 = r6.kLW;	 Catch:{ all -> 0x0035 }
        r3 = r3.second;	 Catch:{ all -> 0x0035 }
        r1 = r1.append(r3);	 Catch:{ all -> 0x0035 }
        r1 = r1.toString();	 Catch:{ all -> 0x0035 }
        r0.<init>(r1);	 Catch:{ all -> 0x0035 }
        r1 = 2328; // 0x918 float:3.262E-42 double:1.15E-320;
        com.tencent.matrix.trace.core.AppMethodBeat.o(r1);	 Catch:{ all -> 0x0035 }
        throw r0;	 Catch:{ all -> 0x0035 }
    L_0x01c2:
        r0 = "MicroMsg.Channel.V2SchemeUtil";
        r1 = new java.lang.StringBuilder;	 Catch:{ all -> 0x0035 }
        r3 = "baseApk : ";
        r1.<init>(r3);	 Catch:{ all -> 0x0035 }
        r3 = r14.getAbsolutePath();	 Catch:{ all -> 0x0035 }
        r1 = r1.append(r3);	 Catch:{ all -> 0x0035 }
        r3 = "\nApkSectionInfo = ";
        r1 = r1.append(r3);	 Catch:{ all -> 0x0035 }
        r1 = r1.append(r6);	 Catch:{ all -> 0x0035 }
        r1 = r1.toString();	 Catch:{ all -> 0x0035 }
        com.tencent.mm.sdk.platformtools.ab.i(r0, r1);	 Catch:{ all -> 0x0035 }
        r2.close();
        com.tencent.matrix.trace.core.AppMethodBeat.o(r10);
        return r6;
    L_0x01ee:
        r0 = move-exception;
        goto L_0x0037;
        */
        throw new UnsupportedOperationException("Method not decompiled: com.tencent.mm.plugin.downloader.d.i.Q(java.io.File):com.tencent.mm.plugin.downloader.d.a");
    }

    private static ByteBuffer a(RandomAccessFile randomAccessFile, long j, int i) {
        AppMethodBeat.i(2329);
        ByteBuffer allocate = ByteBuffer.allocate(i);
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        randomAccessFile.seek(j);
        randomAccessFile.readFully(allocate.array(), allocate.arrayOffset(), allocate.capacity());
        AppMethodBeat.o(2329);
        return allocate;
    }

    public static ByteBuffer G(Map<Integer, ByteBuffer> map) {
        AppMethodBeat.i(2330);
        RuntimeException runtimeException;
        if (map == null || map.isEmpty()) {
            runtimeException = new RuntimeException("getNewApkV2SchemeBlock , id value pair is empty");
            AppMethodBeat.o(2330);
            throw runtimeException;
        }
        long j;
        long j2 = 24;
        Iterator it = map.entrySet().iterator();
        while (true) {
            j = j2;
            if (!it.hasNext()) {
                break;
            }
            j2 = ((long) (((ByteBuffer) ((Entry) it.next()).getValue()).remaining() + 12)) + j;
        }
        ByteBuffer allocate = ByteBuffer.allocate((int) (8 + j));
        allocate.order(ByteOrder.LITTLE_ENDIAN);
        allocate.putLong(j);
        for (Entry entry : map.entrySet()) {
            ByteBuffer byteBuffer = (ByteBuffer) entry.getValue();
            allocate.putLong((long) (byteBuffer.remaining() + 4));
            allocate.putInt(((Integer) entry.getKey()).intValue());
            allocate.put(byteBuffer.array(), byteBuffer.arrayOffset() + byteBuffer.position(), byteBuffer.remaining());
        }
        allocate.putLong(j);
        allocate.putLong(2334950737559900225L);
        allocate.putLong(3617552046287187010L);
        if (allocate.remaining() > 0) {
            runtimeException = new RuntimeException("generateNewApkV2SchemeBlock error");
            AppMethodBeat.o(2330);
            throw runtimeException;
        }
        allocate.flip();
        AppMethodBeat.o(2330);
        return allocate;
    }

    public static boolean R(File file) {
        AppMethodBeat.i(2331);
        try {
            if (E(P(file)).containsKey(Integer.valueOf(1896449818))) {
                AppMethodBeat.o(2331);
                return true;
            }
        } catch (IOException e) {
            ab.e("MicroMsg.Channel.V2SchemeUtil", "containV2Signature, error: %s", new Object[]{e.getMessage()});
        } catch (b e2) {
            ab.e("MicroMsg.Channel.V2SchemeUtil", "APK : " + file.getAbsolutePath() + " not have apk signature block");
        }
        AppMethodBeat.o(2331);
        return false;
    }
}
