package L1;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.util.Log;
import d5.AbstractC0844a;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.Executor;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import u.AbstractC1563h;

/* JADX INFO: loaded from: classes.dex */
public abstract class e {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final C4.f f3954a = new C4.f(8);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public static final byte[] f3955b = {112, 114, 111, 0};

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public static final byte[] f3956c = {112, 114, 109, 0};

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final byte[] f3957d = {48, 49, 53, 0};

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final byte[] f3958e = {48, 49, 48, 0};

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final byte[] f3959f = {48, 48, 57, 0};

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final byte[] f3960g = {48, 48, 53, 0};

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final byte[] f3961h = {48, 48, 49, 0};
    public static final byte[] i = {48, 48, 49, 0};

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final byte[] f3962j = {48, 48, 50, 0};

    public static byte[] a(byte[] bArr) {
        Deflater deflater = new Deflater(1);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        try {
            DeflaterOutputStream deflaterOutputStream = new DeflaterOutputStream(byteArrayOutputStream, deflater);
            try {
                deflaterOutputStream.write(bArr);
                deflaterOutputStream.close();
                deflater.end();
                return byteArrayOutputStream.toByteArray();
            } finally {
            }
        } catch (Throwable th) {
            deflater.end();
            throw th;
        }
    }

    public static byte[] b(c[] cVarArr, byte[] bArr) throws IOException {
        int i5 = 0;
        int length = 0;
        for (c cVar : cVarArr) {
            length += ((((cVar.f3952g * 2) + 7) & (-8)) / 8) + (cVar.f3950e * 2) + d(cVar.f3946a, cVar.f3947b, bArr).getBytes(StandardCharsets.UTF_8).length + 16 + cVar.f3951f;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length);
        if (Arrays.equals(bArr, f3959f)) {
            int length2 = cVarArr.length;
            while (i5 < length2) {
                c cVar2 = cVarArr[i5];
                q(byteArrayOutputStream, cVar2, d(cVar2.f3946a, cVar2.f3947b, bArr));
                p(byteArrayOutputStream, cVar2);
                i5++;
            }
        } else {
            for (c cVar3 : cVarArr) {
                q(byteArrayOutputStream, cVar3, d(cVar3.f3946a, cVar3.f3947b, bArr));
            }
            int length3 = cVarArr.length;
            while (i5 < length3) {
                p(byteArrayOutputStream, cVarArr[i5]);
                i5++;
            }
        }
        if (byteArrayOutputStream.size() == length) {
            return byteArrayOutputStream.toByteArray();
        }
        throw new IllegalStateException("The bytes saved do not match expectation. actual=" + byteArrayOutputStream.size() + " expected=" + length);
    }

    public static boolean c(File file) {
        if (!file.isDirectory()) {
            file.delete();
            return true;
        }
        File[] fileArrListFiles = file.listFiles();
        if (fileArrListFiles == null) {
            return false;
        }
        boolean z8 = true;
        for (File file2 : fileArrListFiles) {
            z8 = c(file2) && z8;
        }
        return z8;
    }

    public static String d(String str, String str2, byte[] bArr) {
        byte[] bArr2 = f3961h;
        boolean zEquals = Arrays.equals(bArr, bArr2);
        byte[] bArr3 = f3960g;
        String str3 = (zEquals || Arrays.equals(bArr, bArr3)) ? ":" : "!";
        if (str.length() <= 0) {
            if ("!".equals(str3)) {
                return str2.replace(":", "!");
            }
            if (":".equals(str3)) {
                return str2.replace("!", ":");
            }
        } else {
            if (str2.equals("classes.dex")) {
                return str;
            }
            if (str2.contains("!") || str2.contains(":")) {
                if ("!".equals(str3)) {
                    return str2.replace(":", "!");
                }
                if (":".equals(str3)) {
                    return str2.replace("!", ":");
                }
            } else if (!str2.endsWith(".apk")) {
                return I1.a.r(AbstractC1563h.b(str), (Arrays.equals(bArr, bArr2) || Arrays.equals(bArr, bArr3)) ? ":" : "!", str2);
            }
        }
        return str2;
    }

    public static void e(PackageInfo packageInfo, File file) {
        try {
            DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream(new File(file, "profileinstaller_profileWrittenFor_lastUpdateTime.dat")));
            try {
                dataOutputStream.writeLong(packageInfo.lastUpdateTime);
                dataOutputStream.close();
            } finally {
            }
        } catch (IOException unused) {
        }
    }

    public static byte[] f(InputStream inputStream, int i5) throws IOException {
        byte[] bArr = new byte[i5];
        int i8 = 0;
        while (i8 < i5) {
            int i9 = inputStream.read(bArr, i8, i5 - i8);
            if (i9 < 0) {
                throw new IllegalStateException(AbstractC0844a.j("Not enough bytes to read: ", i5));
            }
            i8 += i9;
        }
        return bArr;
    }

    public static int[] g(ByteArrayInputStream byteArrayInputStream, int i5) {
        int[] iArr = new int[i5];
        int iM = 0;
        for (int i8 = 0; i8 < i5; i8++) {
            iM += (int) m(byteArrayInputStream, 2);
            iArr[i8] = iM;
        }
        return iArr;
    }

    public static byte[] h(FileInputStream fileInputStream, int i5, int i8) {
        Inflater inflater = new Inflater();
        try {
            byte[] bArr = new byte[i8];
            byte[] bArr2 = new byte[2048];
            int i9 = 0;
            int iInflate = 0;
            while (!inflater.finished() && !inflater.needsDictionary() && i9 < i5) {
                int i10 = fileInputStream.read(bArr2);
                if (i10 < 0) {
                    throw new IllegalStateException("Invalid zip data. Stream ended after $totalBytesRead bytes. Expected " + i5 + " bytes");
                }
                inflater.setInput(bArr2, 0, i10);
                try {
                    iInflate += inflater.inflate(bArr, iInflate, i8 - iInflate);
                    i9 += i10;
                } catch (DataFormatException e8) {
                    throw new IllegalStateException(e8.getMessage());
                }
            }
            if (i9 == i5) {
                if (inflater.finished()) {
                    return bArr;
                }
                throw new IllegalStateException("Inflater did not finish");
            }
            throw new IllegalStateException("Didn't read enough bytes during decompression. expected=" + i5 + " actual=" + i9);
        } finally {
            inflater.end();
        }
    }

    public static c[] i(FileInputStream fileInputStream, byte[] bArr, byte[] bArr2, c[] cVarArr) throws IOException {
        byte[] bArr3 = i;
        if (!Arrays.equals(bArr, bArr3)) {
            if (!Arrays.equals(bArr, f3962j)) {
                throw new IllegalStateException("Unsupported meta version");
            }
            int iM = (int) m(fileInputStream, 2);
            byte[] bArrH = h(fileInputStream, (int) m(fileInputStream, 4), (int) m(fileInputStream, 4));
            if (fileInputStream.read() > 0) {
                throw new IllegalStateException("Content found after the end of file");
            }
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArrH);
            try {
                c[] cVarArrK = k(byteArrayInputStream, bArr2, iM, cVarArr);
                byteArrayInputStream.close();
                return cVarArrK;
            } catch (Throwable th) {
                try {
                    byteArrayInputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
        if (Arrays.equals(f3957d, bArr2)) {
            throw new IllegalStateException("Requires new Baseline Profile Metadata. Please rebuild the APK with Android Gradle Plugin 7.2 Canary 7 or higher");
        }
        if (!Arrays.equals(bArr, bArr3)) {
            throw new IllegalStateException("Unsupported meta version");
        }
        int iM2 = (int) m(fileInputStream, 1);
        byte[] bArrH2 = h(fileInputStream, (int) m(fileInputStream, 4), (int) m(fileInputStream, 4));
        if (fileInputStream.read() > 0) {
            throw new IllegalStateException("Content found after the end of file");
        }
        ByteArrayInputStream byteArrayInputStream2 = new ByteArrayInputStream(bArrH2);
        try {
            c[] cVarArrJ = j(byteArrayInputStream2, iM2, cVarArr);
            byteArrayInputStream2.close();
            return cVarArrJ;
        } catch (Throwable th3) {
            try {
                byteArrayInputStream2.close();
            } catch (Throwable th4) {
                th3.addSuppressed(th4);
            }
            throw th3;
        }
    }

    public static c[] j(ByteArrayInputStream byteArrayInputStream, int i5, c[] cVarArr) {
        if (byteArrayInputStream.available() == 0) {
            return new c[0];
        }
        if (i5 != cVarArr.length) {
            throw new IllegalStateException("Mismatched number of dex files found in metadata");
        }
        String[] strArr = new String[i5];
        int[] iArr = new int[i5];
        for (int i8 = 0; i8 < i5; i8++) {
            int iM = (int) m(byteArrayInputStream, 2);
            iArr[i8] = (int) m(byteArrayInputStream, 2);
            strArr[i8] = new String(f(byteArrayInputStream, iM), StandardCharsets.UTF_8);
        }
        for (int i9 = 0; i9 < i5; i9++) {
            c cVar = cVarArr[i9];
            if (!cVar.f3947b.equals(strArr[i9])) {
                throw new IllegalStateException("Order of dexfiles in metadata did not match baseline");
            }
            int i10 = iArr[i9];
            cVar.f3950e = i10;
            cVar.f3953h = g(byteArrayInputStream, i10);
        }
        return cVarArr;
    }

    public static c[] k(ByteArrayInputStream byteArrayInputStream, byte[] bArr, int i5, c[] cVarArr) throws IOException {
        if (byteArrayInputStream.available() == 0) {
            return new c[0];
        }
        if (i5 != cVarArr.length) {
            throw new IllegalStateException("Mismatched number of dex files found in metadata");
        }
        for (int i8 = 0; i8 < i5; i8++) {
            m(byteArrayInputStream, 2);
            String str = new String(f(byteArrayInputStream, (int) m(byteArrayInputStream, 2)), StandardCharsets.UTF_8);
            long jM = m(byteArrayInputStream, 4);
            int iM = (int) m(byteArrayInputStream, 2);
            c cVar = null;
            if (cVarArr.length > 0) {
                int iIndexOf = str.indexOf("!");
                if (iIndexOf < 0) {
                    iIndexOf = str.indexOf(":");
                }
                String strSubstring = iIndexOf > 0 ? str.substring(iIndexOf + 1) : str;
                int i9 = 0;
                while (true) {
                    if (i9 >= cVarArr.length) {
                        break;
                    }
                    if (cVarArr[i9].f3947b.equals(strSubstring)) {
                        cVar = cVarArr[i9];
                        break;
                    }
                    i9++;
                }
            }
            if (cVar == null) {
                throw new IllegalStateException("Missing profile key: ".concat(str));
            }
            cVar.f3949d = jM;
            int[] iArrG = g(byteArrayInputStream, iM);
            if (Arrays.equals(bArr, f3961h)) {
                cVar.f3950e = iM;
                cVar.f3953h = iArrG;
            }
        }
        return cVarArr;
    }

    public static c[] l(FileInputStream fileInputStream, byte[] bArr, String str) throws IOException {
        if (!Arrays.equals(bArr, f3958e)) {
            throw new IllegalStateException("Unsupported version");
        }
        int iM = (int) m(fileInputStream, 1);
        byte[] bArrH = h(fileInputStream, (int) m(fileInputStream, 4), (int) m(fileInputStream, 4));
        if (fileInputStream.read() > 0) {
            throw new IllegalStateException("Content found after the end of file");
        }
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArrH);
        try {
            c[] cVarArrN = n(byteArrayInputStream, str, iM);
            byteArrayInputStream.close();
            return cVarArrN;
        } catch (Throwable th) {
            try {
                byteArrayInputStream.close();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

    public static long m(InputStream inputStream, int i5) throws IOException {
        byte[] bArrF = f(inputStream, i5);
        long j8 = 0;
        for (int i8 = 0; i8 < i5; i8++) {
            j8 += ((long) (bArrF[i8] & 255)) << (i8 * 8);
        }
        return j8;
    }

    public static c[] n(ByteArrayInputStream byteArrayInputStream, String str, int i5) throws IOException {
        TreeMap treeMap;
        if (byteArrayInputStream.available() == 0) {
            return new c[0];
        }
        c[] cVarArr = new c[i5];
        for (int i8 = 0; i8 < i5; i8++) {
            int iM = (int) m(byteArrayInputStream, 2);
            int iM2 = (int) m(byteArrayInputStream, 2);
            cVarArr[i8] = new c(str, new String(f(byteArrayInputStream, iM), StandardCharsets.UTF_8), m(byteArrayInputStream, 4), iM2, (int) m(byteArrayInputStream, 4), (int) m(byteArrayInputStream, 4), new int[iM2], new TreeMap());
        }
        for (int i9 = 0; i9 < i5; i9++) {
            c cVar = cVarArr[i9];
            int iAvailable = byteArrayInputStream.available() - cVar.f3951f;
            int iM3 = 0;
            while (true) {
                int iAvailable2 = byteArrayInputStream.available();
                treeMap = cVar.i;
                if (iAvailable2 <= iAvailable) {
                    break;
                }
                iM3 += (int) m(byteArrayInputStream, 2);
                treeMap.put(Integer.valueOf(iM3), 1);
                for (int iM4 = (int) m(byteArrayInputStream, 2); iM4 > 0; iM4--) {
                    m(byteArrayInputStream, 2);
                    int iM5 = (int) m(byteArrayInputStream, 1);
                    if (iM5 != 6 && iM5 != 7) {
                        while (iM5 > 0) {
                            m(byteArrayInputStream, 1);
                            for (int iM6 = (int) m(byteArrayInputStream, 1); iM6 > 0; iM6--) {
                                m(byteArrayInputStream, 2);
                            }
                            iM5--;
                        }
                    }
                }
            }
            if (byteArrayInputStream.available() != iAvailable) {
                throw new IllegalStateException("Read too much data during profile line parse");
            }
            cVar.f3953h = g(byteArrayInputStream, cVar.f3950e);
            int i10 = cVar.f3952g;
            BitSet bitSetValueOf = BitSet.valueOf(f(byteArrayInputStream, (((i10 * 2) + 7) & (-8)) / 8));
            for (int i11 = 0; i11 < i10; i11++) {
                int i12 = bitSetValueOf.get(i11) ? 2 : 0;
                if (bitSetValueOf.get(i11 + i10)) {
                    i12 |= 4;
                }
                if (i12 != 0) {
                    Integer num = (Integer) treeMap.get(Integer.valueOf(i11));
                    if (num == null) {
                        num = 0;
                    }
                    treeMap.put(Integer.valueOf(i11), Integer.valueOf(i12 | num.intValue()));
                }
            }
        }
        return cVarArr;
    }

    /* JADX WARN: Finally extract failed */
    public static boolean o(ByteArrayOutputStream byteArrayOutputStream, byte[] bArr, c[] cVarArr) throws IOException {
        long j8;
        ArrayList arrayList;
        int length;
        byte[] bArr2 = f3957d;
        int i5 = 0;
        if (!Arrays.equals(bArr, bArr2)) {
            byte[] bArr3 = f3958e;
            if (Arrays.equals(bArr, bArr3)) {
                byte[] bArrB = b(cVarArr, bArr3);
                u(byteArrayOutputStream, cVarArr.length, 1);
                u(byteArrayOutputStream, bArrB.length, 4);
                byte[] bArrA = a(bArrB);
                u(byteArrayOutputStream, bArrA.length, 4);
                byteArrayOutputStream.write(bArrA);
                return true;
            }
            byte[] bArr4 = f3960g;
            if (Arrays.equals(bArr, bArr4)) {
                u(byteArrayOutputStream, cVarArr.length, 1);
                for (c cVar : cVarArr) {
                    int size = cVar.i.size() * 4;
                    String strD = d(cVar.f3946a, cVar.f3947b, bArr4);
                    Charset charset = StandardCharsets.UTF_8;
                    v(byteArrayOutputStream, strD.getBytes(charset).length);
                    v(byteArrayOutputStream, cVar.f3953h.length);
                    u(byteArrayOutputStream, size, 4);
                    u(byteArrayOutputStream, cVar.f3948c, 4);
                    byteArrayOutputStream.write(strD.getBytes(charset));
                    Iterator it = cVar.i.keySet().iterator();
                    while (it.hasNext()) {
                        v(byteArrayOutputStream, ((Integer) it.next()).intValue());
                        v(byteArrayOutputStream, 0);
                    }
                    for (int i8 : cVar.f3953h) {
                        v(byteArrayOutputStream, i8);
                    }
                }
                return true;
            }
            byte[] bArr5 = f3959f;
            if (Arrays.equals(bArr, bArr5)) {
                byte[] bArrB2 = b(cVarArr, bArr5);
                u(byteArrayOutputStream, cVarArr.length, 1);
                u(byteArrayOutputStream, bArrB2.length, 4);
                byte[] bArrA2 = a(bArrB2);
                u(byteArrayOutputStream, bArrA2.length, 4);
                byteArrayOutputStream.write(bArrA2);
                return true;
            }
            byte[] bArr6 = f3961h;
            if (!Arrays.equals(bArr, bArr6)) {
                return false;
            }
            v(byteArrayOutputStream, cVarArr.length);
            for (c cVar2 : cVarArr) {
                String strD2 = d(cVar2.f3946a, cVar2.f3947b, bArr6);
                Charset charset2 = StandardCharsets.UTF_8;
                v(byteArrayOutputStream, strD2.getBytes(charset2).length);
                TreeMap treeMap = cVar2.i;
                v(byteArrayOutputStream, treeMap.size());
                v(byteArrayOutputStream, cVar2.f3953h.length);
                u(byteArrayOutputStream, cVar2.f3948c, 4);
                byteArrayOutputStream.write(strD2.getBytes(charset2));
                Iterator it2 = treeMap.keySet().iterator();
                while (it2.hasNext()) {
                    v(byteArrayOutputStream, ((Integer) it2.next()).intValue());
                }
                for (int i9 : cVar2.f3953h) {
                    v(byteArrayOutputStream, i9);
                }
            }
            return true;
        }
        ArrayList arrayList2 = new ArrayList(3);
        ArrayList arrayList3 = new ArrayList(3);
        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
        try {
            v(byteArrayOutputStream2, cVarArr.length);
            int i10 = 2;
            int i11 = 2;
            for (c cVar3 : cVarArr) {
                u(byteArrayOutputStream2, cVar3.f3948c, 4);
                u(byteArrayOutputStream2, cVar3.f3949d, 4);
                u(byteArrayOutputStream2, cVar3.f3952g, 4);
                String strD3 = d(cVar3.f3946a, cVar3.f3947b, bArr2);
                Charset charset3 = StandardCharsets.UTF_8;
                int length2 = strD3.getBytes(charset3).length;
                v(byteArrayOutputStream2, length2);
                i11 = i11 + 14 + length2;
                byteArrayOutputStream2.write(strD3.getBytes(charset3));
            }
            byte[] byteArray = byteArrayOutputStream2.toByteArray();
            if (i11 != byteArray.length) {
                throw new IllegalStateException("Expected size " + i11 + ", does not match actual size " + byteArray.length);
            }
            k kVar = new k(1, false, byteArray);
            byteArrayOutputStream2.close();
            arrayList2.add(kVar);
            ByteArrayOutputStream byteArrayOutputStream3 = new ByteArrayOutputStream();
            int i12 = 0;
            int i13 = 0;
            while (i12 < cVarArr.length) {
                try {
                    c cVar4 = cVarArr[i12];
                    v(byteArrayOutputStream3, i12);
                    v(byteArrayOutputStream3, cVar4.f3950e);
                    i13 = i13 + 4 + (cVar4.f3950e * i10);
                    int[] iArr = cVar4.f3953h;
                    int length3 = iArr.length;
                    int i14 = i5;
                    int i15 = i10;
                    int i16 = i14;
                    while (i16 < length3) {
                        int i17 = iArr[i16];
                        v(byteArrayOutputStream3, i17 - i14);
                        i16++;
                        i14 = i17;
                    }
                    i12++;
                    i10 = i15;
                    i5 = 0;
                } catch (Throwable th) {
                }
            }
            byte[] byteArray2 = byteArrayOutputStream3.toByteArray();
            if (i13 != byteArray2.length) {
                throw new IllegalStateException("Expected size " + i13 + ", does not match actual size " + byteArray2.length);
            }
            k kVar2 = new k(3, true, byteArray2);
            byteArrayOutputStream3.close();
            arrayList2.add(kVar2);
            byteArrayOutputStream3 = new ByteArrayOutputStream();
            int i18 = 0;
            int i19 = 0;
            while (i18 < cVarArr.length) {
                try {
                    c cVar5 = cVarArr[i18];
                    Iterator it3 = cVar5.i.entrySet().iterator();
                    int iIntValue = 0;
                    while (it3.hasNext()) {
                        iIntValue |= ((Integer) ((Map.Entry) it3.next()).getValue()).intValue();
                    }
                    ByteArrayOutputStream byteArrayOutputStream4 = new ByteArrayOutputStream();
                    try {
                        r(byteArrayOutputStream4, iIntValue, cVar5);
                        byte[] byteArray3 = byteArrayOutputStream4.toByteArray();
                        byteArrayOutputStream4.close();
                        byteArrayOutputStream4 = new ByteArrayOutputStream();
                        try {
                            s(byteArrayOutputStream4, cVar5);
                            byte[] byteArray4 = byteArrayOutputStream4.toByteArray();
                            byteArrayOutputStream4.close();
                            v(byteArrayOutputStream3, i18);
                            int length4 = byteArray3.length + 2 + byteArray4.length;
                            int i20 = i19 + 6;
                            ArrayList arrayList4 = arrayList3;
                            u(byteArrayOutputStream3, length4, 4);
                            v(byteArrayOutputStream3, iIntValue);
                            byteArrayOutputStream3.write(byteArray3);
                            byteArrayOutputStream3.write(byteArray4);
                            i19 = i20 + length4;
                            i18++;
                            arrayList3 = arrayList4;
                        } finally {
                        }
                    } finally {
                    }
                } finally {
                    try {
                        byteArrayOutputStream3.close();
                        throw th;
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
            }
            ArrayList arrayList5 = arrayList3;
            byte[] byteArray5 = byteArrayOutputStream3.toByteArray();
            if (i19 != byteArray5.length) {
                throw new IllegalStateException("Expected size " + i19 + ", does not match actual size " + byteArray5.length);
            }
            k kVar3 = new k(4, true, byteArray5);
            byteArrayOutputStream3.close();
            arrayList2.add(kVar3);
            long j9 = 4;
            long size2 = j9 + j9 + 4 + ((long) (arrayList2.size() * 16));
            u(byteArrayOutputStream, arrayList2.size(), 4);
            int i21 = 0;
            while (i21 < arrayList2.size()) {
                k kVar4 = (k) arrayList2.get(i21);
                int i22 = kVar4.f3974a;
                if (i22 == 1) {
                    j8 = 0;
                } else if (i22 == 2) {
                    j8 = 1;
                } else if (i22 == 3) {
                    j8 = 2;
                } else if (i22 == 4) {
                    j8 = 3;
                } else {
                    if (i22 != 5) {
                        throw null;
                    }
                    j8 = 4;
                }
                u(byteArrayOutputStream, j8, 4);
                u(byteArrayOutputStream, size2, 4);
                byte[] bArr7 = kVar4.f3975b;
                if (kVar4.f3976c) {
                    long length5 = bArr7.length;
                    byte[] bArrA3 = a(bArr7);
                    arrayList = arrayList5;
                    arrayList.add(bArrA3);
                    u(byteArrayOutputStream, bArrA3.length, 4);
                    u(byteArrayOutputStream, length5, 4);
                    length = bArrA3.length;
                } else {
                    arrayList = arrayList5;
                    arrayList.add(bArr7);
                    u(byteArrayOutputStream, bArr7.length, 4);
                    u(byteArrayOutputStream, 0L, 4);
                    length = bArr7.length;
                }
                size2 += (long) length;
                i21++;
                arrayList5 = arrayList;
            }
            ArrayList arrayList6 = arrayList5;
            for (int i23 = 0; i23 < arrayList6.size(); i23++) {
                byteArrayOutputStream.write((byte[]) arrayList6.get(i23));
            }
            return true;
        } catch (Throwable th3) {
            try {
                byteArrayOutputStream2.close();
                throw th3;
            } catch (Throwable th4) {
                th3.addSuppressed(th4);
                throw th3;
            }
        }
    }

    public static void p(ByteArrayOutputStream byteArrayOutputStream, c cVar) throws IOException {
        s(byteArrayOutputStream, cVar);
        int[] iArr = cVar.f3953h;
        int length = iArr.length;
        int i5 = 0;
        int i8 = 0;
        while (i5 < length) {
            int i9 = iArr[i5];
            v(byteArrayOutputStream, i9 - i8);
            i5++;
            i8 = i9;
        }
        int i10 = cVar.f3952g;
        byte[] bArr = new byte[(((i10 * 2) + 7) & (-8)) / 8];
        for (Map.Entry entry : cVar.i.entrySet()) {
            int iIntValue = ((Integer) entry.getKey()).intValue();
            int iIntValue2 = ((Integer) entry.getValue()).intValue();
            if ((iIntValue2 & 2) != 0) {
                int i11 = iIntValue / 8;
                bArr[i11] = (byte) (bArr[i11] | (1 << (iIntValue % 8)));
            }
            if ((iIntValue2 & 4) != 0) {
                int i12 = iIntValue + i10;
                int i13 = i12 / 8;
                bArr[i13] = (byte) ((1 << (i12 % 8)) | bArr[i13]);
            }
        }
        byteArrayOutputStream.write(bArr);
    }

    public static void q(ByteArrayOutputStream byteArrayOutputStream, c cVar, String str) throws IOException {
        Charset charset = StandardCharsets.UTF_8;
        v(byteArrayOutputStream, str.getBytes(charset).length);
        v(byteArrayOutputStream, cVar.f3950e);
        u(byteArrayOutputStream, cVar.f3951f, 4);
        u(byteArrayOutputStream, cVar.f3948c, 4);
        u(byteArrayOutputStream, cVar.f3952g, 4);
        byteArrayOutputStream.write(str.getBytes(charset));
    }

    public static void r(ByteArrayOutputStream byteArrayOutputStream, int i5, c cVar) throws IOException {
        int iBitCount = Integer.bitCount(i5 & (-2));
        int i8 = cVar.f3952g;
        byte[] bArr = new byte[(((iBitCount * i8) + 7) & (-8)) / 8];
        for (Map.Entry entry : cVar.i.entrySet()) {
            int iIntValue = ((Integer) entry.getKey()).intValue();
            int iIntValue2 = ((Integer) entry.getValue()).intValue();
            int i9 = 0;
            for (int i10 = 1; i10 <= 4; i10 <<= 1) {
                if (i10 != 1 && (i10 & i5) != 0) {
                    if ((i10 & iIntValue2) == i10) {
                        int i11 = (i9 * i8) + iIntValue;
                        int i12 = i11 / 8;
                        bArr[i12] = (byte) ((1 << (i11 % 8)) | bArr[i12]);
                    }
                    i9++;
                }
            }
        }
        byteArrayOutputStream.write(bArr);
    }

    public static void s(ByteArrayOutputStream byteArrayOutputStream, c cVar) throws IOException {
        int i5 = 0;
        for (Map.Entry entry : cVar.i.entrySet()) {
            int iIntValue = ((Integer) entry.getKey()).intValue();
            if ((((Integer) entry.getValue()).intValue() & 1) != 0) {
                v(byteArrayOutputStream, iIntValue - i5);
                v(byteArrayOutputStream, 0);
                i5 = iIntValue;
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:116:0x01a8  */
    /* JADX WARN: Removed duplicated region for block: B:124:0x01c0  */
    /* JADX WARN: Removed duplicated region for block: B:127:0x01ce  */
    /* JADX WARN: Removed duplicated region for block: B:155:0x0219  */
    /* JADX WARN: Removed duplicated region for block: B:159:0x0225  */
    /* JADX WARN: Removed duplicated region for block: B:160:0x0229  */
    /* JADX WARN: Removed duplicated region for block: B:238:0x02db  */
    /* JADX WARN: Removed duplicated region for block: B:247:0x02f1 A[ADDED_TO_REGION] */
    /* JADX WARN: Removed duplicated region for block: B:249:0x02f5  */
    /* JADX WARN: Removed duplicated region for block: B:269:0x0107 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0072  */
    /* JADX WARN: Removed duplicated region for block: B:290:0x0174 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:297:0x01d9 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:84:0x0158  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static void t(Context context, Executor executor, d dVar, boolean z8) {
        char c3;
        FileInputStream fileInputStreamD;
        c[] cVarArrL;
        c[] cVarArr;
        c[] cVarArr2;
        byte[] bArr;
        boolean z9;
        Throwable th;
        Throwable th2;
        boolean z10;
        byte[] bArr2;
        ByteArrayOutputStream byteArrayOutputStream;
        int i5;
        b bVar;
        FileInputStream fileInputStreamD2;
        boolean z11;
        Context applicationContext = context.getApplicationContext();
        String packageName = applicationContext.getPackageName();
        ApplicationInfo applicationInfo = applicationContext.getApplicationInfo();
        AssetManager assets = applicationContext.getAssets();
        String name = new File(applicationInfo.sourceDir).getName();
        try {
            PackageInfo packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
            File filesDir = context.getFilesDir();
            if (!z8) {
                File file = new File(filesDir, "profileinstaller_profileWrittenFor_lastUpdateTime.dat");
                if (file.exists()) {
                    try {
                        DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
                        try {
                            long j8 = dataInputStream.readLong();
                            dataInputStream.close();
                            z11 = j8 == packageInfo.lastUpdateTime;
                            if (z11) {
                                dVar.f(2, null);
                            }
                        } finally {
                        }
                    } catch (IOException unused) {
                        z11 = false;
                    }
                    if (z11) {
                    }
                } else {
                    z11 = false;
                    if (z11) {
                        Log.d("ProfileInstaller", "Skipping profile installation for " + context.getPackageName());
                        j.c(context, false);
                        return;
                    }
                }
            }
            Log.d("ProfileInstaller", "Installing profile for " + context.getPackageName());
            File file2 = new File(new File("/data/misc/profiles/cur/0", packageName), "primary.prof");
            b bVar2 = new b(assets, executor, dVar, name, file2);
            byte[] bArr3 = (byte[]) bVar2.f3941d;
            if (bArr3 != null) {
                if (file2.exists()) {
                    if (!file2.canWrite()) {
                        bVar2.f(4, null);
                    }
                    bVar2.f3938a = true;
                    byte[] bArr4 = f3955b;
                    try {
                        fileInputStreamD = bVar2.d(assets, "dexopt/baseline.prof");
                    } catch (FileNotFoundException e8) {
                        dVar.f(6, e8);
                        fileInputStreamD = null;
                    } catch (IOException e9) {
                        dVar.f(7, e9);
                        fileInputStreamD = null;
                    }
                    c3 = '\b';
                    try {
                        try {
                        } catch (IOException e10) {
                            dVar.f(7, e10);
                        }
                        if (fileInputStreamD != null) {
                            try {
                                try {
                                } catch (IOException e11) {
                                    dVar.f(7, e11);
                                    fileInputStreamD.close();
                                    cVarArrL = null;
                                    bVar2.f3945h = cVarArrL;
                                    cVarArr = (c[]) bVar2.f3945h;
                                    if (cVarArr != null) {
                                    }
                                    d dVar2 = (d) bVar2.f3940c;
                                    cVarArr2 = (c[]) bVar2.f3945h;
                                    if (cVarArr2 != null) {
                                    }
                                    bArr = (byte[]) bVar2.f3942e;
                                    if (bArr != null) {
                                    }
                                    if (z9) {
                                    }
                                    z10 = z9;
                                    j.c(context, (z10 || !z8) ? 0 : c3);
                                }
                            } catch (IllegalStateException e12) {
                                dVar.f(8, e12);
                                fileInputStreamD.close();
                                cVarArrL = null;
                                bVar2.f3945h = cVarArrL;
                                cVarArr = (c[]) bVar2.f3945h;
                                if (cVarArr != null) {
                                }
                                d dVar22 = (d) bVar2.f3940c;
                                cVarArr2 = (c[]) bVar2.f3945h;
                                if (cVarArr2 != null) {
                                }
                                bArr = (byte[]) bVar2.f3942e;
                                if (bArr != null) {
                                }
                                if (z9) {
                                }
                                z10 = z9;
                                j.c(context, (z10 || !z8) ? 0 : c3);
                            }
                            if (!Arrays.equals(bArr4, f(fileInputStreamD, 4))) {
                                throw new IllegalStateException("Invalid magic");
                            }
                            cVarArrL = l(fileInputStreamD, f(fileInputStreamD, 4), (String) bVar2.f3944g);
                            try {
                                fileInputStreamD.close();
                            } catch (IOException e13) {
                                dVar.f(7, e13);
                            }
                            bVar2.f3945h = cVarArrL;
                        }
                        cVarArr = (c[]) bVar2.f3945h;
                        if (cVarArr != null && (i5 = Build.VERSION.SDK_INT) >= 24 && (i5 >= 31 || i5 == 24 || i5 == 25)) {
                            try {
                                fileInputStreamD2 = bVar2.d(assets, "dexopt/baseline.profm");
                            } catch (FileNotFoundException e14) {
                                dVar.f(9, e14);
                            } catch (IOException e15) {
                                dVar.f(7, e15);
                            } catch (IllegalStateException e16) {
                                bVar2.f3945h = null;
                                dVar.f(8, e16);
                            }
                            if (fileInputStreamD2 == null) {
                                try {
                                    if (!Arrays.equals(f3956c, f(fileInputStreamD2, 4))) {
                                        throw new IllegalStateException("Invalid magic");
                                    }
                                    bVar2.f3945h = i(fileInputStreamD2, f(fileInputStreamD2, 4), bArr3, cVarArr);
                                    fileInputStreamD2.close();
                                    bVar = bVar2;
                                    if (bVar != null) {
                                        bVar2 = bVar;
                                    }
                                } finally {
                                }
                            } else {
                                if (fileInputStreamD2 != null) {
                                    fileInputStreamD2.close();
                                }
                                bVar = null;
                                if (bVar != null) {
                                }
                            }
                        }
                        d dVar222 = (d) bVar2.f3940c;
                        cVarArr2 = (c[]) bVar2.f3945h;
                        if (cVarArr2 != null && (bArr2 = (byte[]) bVar2.f3941d) != null) {
                            if (bVar2.f3938a) {
                                throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
                            }
                            try {
                                byteArrayOutputStream = new ByteArrayOutputStream();
                                try {
                                    byteArrayOutputStream.write(bArr4);
                                    byteArrayOutputStream.write(bArr2);
                                } finally {
                                }
                            } catch (IOException e17) {
                                dVar222.f(7, e17);
                            } catch (IllegalStateException e18) {
                                dVar222.f(8, e18);
                            }
                            if (o(byteArrayOutputStream, bArr2, cVarArr2)) {
                                bVar2.f3942e = byteArrayOutputStream.toByteArray();
                                byteArrayOutputStream.close();
                                bVar2.f3945h = null;
                            } else {
                                dVar222.f(5, null);
                                bVar2.f3945h = null;
                                byteArrayOutputStream.close();
                            }
                        }
                        bArr = (byte[]) bVar2.f3942e;
                        if (bArr != null) {
                            z9 = false;
                            c3 = 1;
                        } else {
                            try {
                                if (!bVar2.f3938a) {
                                    throw new IllegalStateException("This device doesn't support aot. Did you call deviceSupportsAotProfile()?");
                                }
                                try {
                                    try {
                                        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
                                        try {
                                            try {
                                                FileOutputStream fileOutputStream = new FileOutputStream((File) bVar2.f3943f);
                                                try {
                                                    try {
                                                        FileChannel channel = fileOutputStream.getChannel();
                                                        try {
                                                            FileLock fileLockTryLock = channel.tryLock();
                                                            try {
                                                                try {
                                                                    if (fileLockTryLock != null) {
                                                                        try {
                                                                            if (fileLockTryLock.isValid()) {
                                                                                byte[] bArr5 = new byte[512];
                                                                                while (true) {
                                                                                    int i8 = byteArrayInputStream.read(bArr5);
                                                                                    if (i8 <= 0) {
                                                                                        break;
                                                                                    } else {
                                                                                        fileOutputStream.write(bArr5, 0, i8);
                                                                                    }
                                                                                }
                                                                                c3 = 1;
                                                                                bVar2.f(1, null);
                                                                                fileLockTryLock.close();
                                                                                channel.close();
                                                                                fileOutputStream.close();
                                                                                byteArrayInputStream.close();
                                                                                bVar2.f3942e = null;
                                                                                bVar2.f3945h = null;
                                                                                z9 = true;
                                                                            }
                                                                        } catch (Throwable th3) {
                                                                            th = th3;
                                                                            Throwable th4 = th;
                                                                            if (fileLockTryLock == null) {
                                                                                throw th4;
                                                                            }
                                                                            try {
                                                                                fileLockTryLock.close();
                                                                                throw th4;
                                                                            } catch (Throwable th5) {
                                                                                th4.addSuppressed(th5);
                                                                                throw th4;
                                                                            }
                                                                        }
                                                                    }
                                                                    throw new IOException("Unable to acquire a lock on the underlying file channel.");
                                                                } catch (Throwable th6) {
                                                                    th = th6;
                                                                    Throwable th7 = th;
                                                                    if (channel == null) {
                                                                        throw th7;
                                                                    }
                                                                    try {
                                                                        channel.close();
                                                                        throw th7;
                                                                    } catch (Throwable th8) {
                                                                        th7.addSuppressed(th8);
                                                                        throw th7;
                                                                    }
                                                                }
                                                            } catch (Throwable th9) {
                                                                th = th9;
                                                            }
                                                        } catch (Throwable th10) {
                                                            th = th10;
                                                        }
                                                    } catch (Throwable th11) {
                                                        th = th11;
                                                        th2 = th;
                                                        try {
                                                            fileOutputStream.close();
                                                            throw th2;
                                                        } catch (Throwable th12) {
                                                            th2.addSuppressed(th12);
                                                            throw th2;
                                                        }
                                                    }
                                                } catch (Throwable th13) {
                                                    th = th13;
                                                    th2 = th;
                                                    fileOutputStream.close();
                                                    throw th2;
                                                }
                                            } catch (Throwable th14) {
                                                th = th14;
                                                th = th;
                                                try {
                                                    byteArrayInputStream.close();
                                                    throw th;
                                                } catch (Throwable th15) {
                                                    th.addSuppressed(th15);
                                                    throw th;
                                                }
                                            }
                                        } catch (Throwable th16) {
                                            th = th16;
                                            th = th;
                                            byteArrayInputStream.close();
                                            throw th;
                                        }
                                    } catch (FileNotFoundException e19) {
                                        e = e19;
                                        bVar2.f(6, e);
                                        z9 = false;
                                        if (z9) {
                                        }
                                        z10 = z9;
                                        j.c(context, (z10 || !z8) ? 0 : c3);
                                    } catch (IOException e20) {
                                        e = e20;
                                        bVar2.f(7, e);
                                        z9 = false;
                                        if (z9) {
                                        }
                                        z10 = z9;
                                        j.c(context, (z10 || !z8) ? 0 : c3);
                                    }
                                } catch (FileNotFoundException e21) {
                                    e = e21;
                                    c3 = 1;
                                    bVar2.f(6, e);
                                    z9 = false;
                                    if (z9) {
                                    }
                                    z10 = z9;
                                    j.c(context, (z10 || !z8) ? 0 : c3);
                                } catch (IOException e22) {
                                    e = e22;
                                    c3 = 1;
                                    bVar2.f(7, e);
                                    z9 = false;
                                    if (z9) {
                                    }
                                    z10 = z9;
                                    j.c(context, (z10 || !z8) ? 0 : c3);
                                }
                            } finally {
                                bVar2.f3942e = null;
                                bVar2.f3945h = null;
                            }
                        }
                        if (z9) {
                            e(packageInfo, filesDir);
                        }
                        z10 = z9;
                    } finally {
                    }
                } else {
                    try {
                        if (!file2.createNewFile()) {
                            bVar2.f(4, null);
                        }
                        bVar2.f3938a = true;
                        byte[] bArr42 = f3955b;
                        fileInputStreamD = bVar2.d(assets, "dexopt/baseline.prof");
                        c3 = '\b';
                        if (fileInputStreamD != null) {
                        }
                        cVarArr = (c[]) bVar2.f3945h;
                        if (cVarArr != null) {
                            fileInputStreamD2 = bVar2.d(assets, "dexopt/baseline.profm");
                            if (fileInputStreamD2 == null) {
                            }
                        }
                        d dVar2222 = (d) bVar2.f3940c;
                        cVarArr2 = (c[]) bVar2.f3945h;
                        if (cVarArr2 != null) {
                            if (bVar2.f3938a) {
                            }
                        }
                        bArr = (byte[]) bVar2.f3942e;
                        if (bArr != null) {
                        }
                        if (z9) {
                        }
                        z10 = z9;
                    } catch (IOException unused2) {
                        c3 = 1;
                        bVar2.f(4, null);
                    }
                }
                j.c(context, (z10 || !z8) ? 0 : c3);
            }
            bVar2.f(3, Integer.valueOf(Build.VERSION.SDK_INT));
            c3 = 1;
            z10 = false;
            j.c(context, (z10 || !z8) ? 0 : c3);
        } catch (PackageManager.NameNotFoundException e23) {
            dVar.f(7, e23);
            j.c(context, false);
        }
    }

    public static void u(ByteArrayOutputStream byteArrayOutputStream, long j8, int i5) throws IOException {
        byte[] bArr = new byte[i5];
        for (int i8 = 0; i8 < i5; i8++) {
            bArr[i8] = (byte) ((j8 >> (i8 * 8)) & 255);
        }
        byteArrayOutputStream.write(bArr);
    }

    public static void v(ByteArrayOutputStream byteArrayOutputStream, int i5) throws IOException {
        u(byteArrayOutputStream, i5, 2);
    }
}
