package l2;

import Z0.o;
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 androidx.navigation.C1134i;
import com.revenuecat.purchases.common.Constants;
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;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final C1134i f20268a = new C1134i(6);

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

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

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

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

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

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

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

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

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static final byte[] f20277j = {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(b[] bVarArr, byte[] bArr) throws IOException {
        int i6 = 0;
        int length = 0;
        for (b bVar : bVarArr) {
            length += ((((bVar.f20263g * 2) + 7) & (-8)) / 8) + (bVar.f20261e * 2) + d(bVar.f20257a, bVar.f20258b, bArr).getBytes(StandardCharsets.UTF_8).length + 16 + bVar.f20262f;
        }
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(length);
        if (Arrays.equals(bArr, f20273f)) {
            int length2 = bVarArr.length;
            while (i6 < length2) {
                b bVar2 = bVarArr[i6];
                q(byteArrayOutputStream, bVar2, d(bVar2.f20257a, bVar2.f20258b, bArr));
                p(byteArrayOutputStream, bVar2);
                i6++;
            }
        } else {
            for (b bVar3 : bVarArr) {
                q(byteArrayOutputStream, bVar3, d(bVar3.f20257a, bVar3.f20258b, bArr));
            }
            int length3 = bVarArr.length;
            while (i6 < length3) {
                p(byteArrayOutputStream, bVarArr[i6]);
                i6++;
            }
        }
        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 z6 = true;
        for (File file2 : fileArrListFiles) {
            z6 = c(file2) && z6;
        }
        return z6;
    }

    public static String d(String str, String str2, byte[] bArr) {
        byte[] bArr2 = f20275h;
        boolean zEquals = Arrays.equals(bArr, bArr2);
        byte[] bArr3 = f20274g;
        Object obj = (zEquals || Arrays.equals(bArr, bArr3)) ? Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR : "!";
        if (str.length() <= 0) {
            if ("!".equals(obj)) {
                return str2.replace(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, "!");
            }
            if (Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR.equals(obj)) {
                return str2.replace("!", Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
            }
        } else {
            if (str2.equals("classes.dex")) {
                return str;
            }
            if (str2.contains("!") || str2.contains(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR)) {
                if ("!".equals(obj)) {
                    return str2.replace(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, "!");
                }
                if (Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR.equals(obj)) {
                    return str2.replace("!", Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
                }
            } else if (!str2.endsWith(".apk")) {
                return com.google.android.recaptcha.internal.a.n(o.t(str), (Arrays.equals(bArr, bArr2) || Arrays.equals(bArr, bArr3)) ? Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR : "!", 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 i6) throws IOException {
        byte[] bArr = new byte[i6];
        int i7 = 0;
        while (i7 < i6) {
            int i8 = inputStream.read(bArr, i7, i6 - i7);
            if (i8 < 0) {
                throw new IllegalStateException(o.l("Not enough bytes to read: ", i6));
            }
            i7 += i8;
        }
        return bArr;
    }

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

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

    public static b[] i(FileInputStream fileInputStream, byte[] bArr, byte[] bArr2, b[] bVarArr) throws IOException {
        byte[] bArr3 = f20276i;
        if (!Arrays.equals(bArr, bArr3)) {
            if (!Arrays.equals(bArr, f20277j)) {
                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 {
                b[] bVarArrK = k(byteArrayInputStream, bArr2, iM, bVarArr);
                byteArrayInputStream.close();
                return bVarArrK;
            } catch (Throwable th) {
                try {
                    byteArrayInputStream.close();
                } catch (Throwable th2) {
                    th.addSuppressed(th2);
                }
                throw th;
            }
        }
        if (Arrays.equals(f20271d, 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 {
            b[] bVarArrJ = j(byteArrayInputStream2, iM2, bVarArr);
            byteArrayInputStream2.close();
            return bVarArrJ;
        } catch (Throwable th3) {
            try {
                byteArrayInputStream2.close();
            } catch (Throwable th4) {
                th3.addSuppressed(th4);
            }
            throw th3;
        }
    }

    public static b[] j(ByteArrayInputStream byteArrayInputStream, int i6, b[] bVarArr) {
        if (byteArrayInputStream.available() == 0) {
            return new b[0];
        }
        if (i6 != bVarArr.length) {
            throw new IllegalStateException("Mismatched number of dex files found in metadata");
        }
        String[] strArr = new String[i6];
        int[] iArr = new int[i6];
        for (int i7 = 0; i7 < i6; i7++) {
            int iM = (int) m(byteArrayInputStream, 2);
            iArr[i7] = (int) m(byteArrayInputStream, 2);
            strArr[i7] = new String(f(byteArrayInputStream, iM), StandardCharsets.UTF_8);
        }
        for (int i8 = 0; i8 < i6; i8++) {
            b bVar = bVarArr[i8];
            if (!bVar.f20258b.equals(strArr[i8])) {
                throw new IllegalStateException("Order of dexfiles in metadata did not match baseline");
            }
            int i9 = iArr[i8];
            bVar.f20261e = i9;
            bVar.f20264h = g(byteArrayInputStream, i9);
        }
        return bVarArr;
    }

    public static b[] k(ByteArrayInputStream byteArrayInputStream, byte[] bArr, int i6, b[] bVarArr) throws IOException {
        if (byteArrayInputStream.available() == 0) {
            return new b[0];
        }
        if (i6 != bVarArr.length) {
            throw new IllegalStateException("Mismatched number of dex files found in metadata");
        }
        for (int i7 = 0; i7 < i6; i7++) {
            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);
            b bVar = null;
            if (bVarArr.length > 0) {
                int iIndexOf = str.indexOf("!");
                if (iIndexOf < 0) {
                    iIndexOf = str.indexOf(Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR);
                }
                String strSubstring = iIndexOf > 0 ? str.substring(iIndexOf + 1) : str;
                int i8 = 0;
                while (true) {
                    if (i8 >= bVarArr.length) {
                        break;
                    }
                    if (bVarArr[i8].f20258b.equals(strSubstring)) {
                        bVar = bVarArr[i8];
                        break;
                    }
                    i8++;
                }
            }
            if (bVar == null) {
                throw new IllegalStateException("Missing profile key: ".concat(str));
            }
            bVar.f20260d = jM;
            int[] iArrG = g(byteArrayInputStream, iM);
            if (Arrays.equals(bArr, f20275h)) {
                bVar.f20261e = iM;
                bVar.f20264h = iArrG;
            }
        }
        return bVarArr;
    }

    public static b[] l(FileInputStream fileInputStream, byte[] bArr, String str) throws IOException {
        if (!Arrays.equals(bArr, f20272e)) {
            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 {
            b[] bVarArrN = n(byteArrayInputStream, str, iM);
            byteArrayInputStream.close();
            return bVarArrN;
        } catch (Throwable th) {
            try {
                byteArrayInputStream.close();
            } catch (Throwable th2) {
                th.addSuppressed(th2);
            }
            throw th;
        }
    }

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

    public static b[] n(ByteArrayInputStream byteArrayInputStream, String str, int i6) throws IOException {
        TreeMap treeMap;
        if (byteArrayInputStream.available() == 0) {
            return new b[0];
        }
        b[] bVarArr = new b[i6];
        for (int i7 = 0; i7 < i6; i7++) {
            int iM = (int) m(byteArrayInputStream, 2);
            int iM2 = (int) m(byteArrayInputStream, 2);
            bVarArr[i7] = new b(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 i8 = 0; i8 < i6; i8++) {
            b bVar = bVarArr[i8];
            int iAvailable = byteArrayInputStream.available() - bVar.f20262f;
            int iM3 = 0;
            while (true) {
                int iAvailable2 = byteArrayInputStream.available();
                treeMap = bVar.f20265i;
                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");
            }
            bVar.f20264h = g(byteArrayInputStream, bVar.f20261e);
            int i9 = bVar.f20263g;
            BitSet bitSetValueOf = BitSet.valueOf(f(byteArrayInputStream, (((i9 * 2) + 7) & (-8)) / 8));
            for (int i10 = 0; i10 < i9; i10++) {
                int i11 = bitSetValueOf.get(i10) ? 2 : 0;
                if (bitSetValueOf.get(i10 + i9)) {
                    i11 |= 4;
                }
                if (i11 != 0) {
                    Integer num = (Integer) treeMap.get(Integer.valueOf(i10));
                    if (num == null) {
                        num = 0;
                    }
                    treeMap.put(Integer.valueOf(i10), Integer.valueOf(i11 | num.intValue()));
                }
            }
        }
        return bVarArr;
    }

    /* JADX WARN: Finally extract failed */
    public static boolean o(ByteArrayOutputStream byteArrayOutputStream, byte[] bArr, b[] bVarArr) throws IOException {
        long j4;
        ArrayList arrayList;
        int length;
        byte[] bArr2 = f20271d;
        int i6 = 0;
        if (!Arrays.equals(bArr, bArr2)) {
            byte[] bArr3 = f20272e;
            if (Arrays.equals(bArr, bArr3)) {
                byte[] bArrB = b(bVarArr, bArr3);
                u(byteArrayOutputStream, bVarArr.length, 1);
                u(byteArrayOutputStream, bArrB.length, 4);
                byte[] bArrA = a(bArrB);
                u(byteArrayOutputStream, bArrA.length, 4);
                byteArrayOutputStream.write(bArrA);
                return true;
            }
            byte[] bArr4 = f20274g;
            if (Arrays.equals(bArr, bArr4)) {
                u(byteArrayOutputStream, bVarArr.length, 1);
                for (b bVar : bVarArr) {
                    int size = bVar.f20265i.size() * 4;
                    String strD = d(bVar.f20257a, bVar.f20258b, bArr4);
                    Charset charset = StandardCharsets.UTF_8;
                    v(byteArrayOutputStream, strD.getBytes(charset).length);
                    v(byteArrayOutputStream, bVar.f20264h.length);
                    u(byteArrayOutputStream, size, 4);
                    u(byteArrayOutputStream, bVar.f20259c, 4);
                    byteArrayOutputStream.write(strD.getBytes(charset));
                    Iterator it = bVar.f20265i.keySet().iterator();
                    while (it.hasNext()) {
                        v(byteArrayOutputStream, ((Integer) it.next()).intValue());
                        v(byteArrayOutputStream, 0);
                    }
                    for (int i7 : bVar.f20264h) {
                        v(byteArrayOutputStream, i7);
                    }
                }
                return true;
            }
            byte[] bArr5 = f20273f;
            if (Arrays.equals(bArr, bArr5)) {
                byte[] bArrB2 = b(bVarArr, bArr5);
                u(byteArrayOutputStream, bVarArr.length, 1);
                u(byteArrayOutputStream, bArrB2.length, 4);
                byte[] bArrA2 = a(bArrB2);
                u(byteArrayOutputStream, bArrA2.length, 4);
                byteArrayOutputStream.write(bArrA2);
                return true;
            }
            byte[] bArr6 = f20275h;
            if (!Arrays.equals(bArr, bArr6)) {
                return false;
            }
            v(byteArrayOutputStream, bVarArr.length);
            for (b bVar2 : bVarArr) {
                String strD2 = d(bVar2.f20257a, bVar2.f20258b, bArr6);
                Charset charset2 = StandardCharsets.UTF_8;
                v(byteArrayOutputStream, strD2.getBytes(charset2).length);
                TreeMap treeMap = bVar2.f20265i;
                v(byteArrayOutputStream, treeMap.size());
                v(byteArrayOutputStream, bVar2.f20264h.length);
                u(byteArrayOutputStream, bVar2.f20259c, 4);
                byteArrayOutputStream.write(strD2.getBytes(charset2));
                Iterator it2 = treeMap.keySet().iterator();
                while (it2.hasNext()) {
                    v(byteArrayOutputStream, ((Integer) it2.next()).intValue());
                }
                for (int i8 : bVar2.f20264h) {
                    v(byteArrayOutputStream, i8);
                }
            }
            return true;
        }
        ArrayList arrayList2 = new ArrayList(3);
        ArrayList arrayList3 = new ArrayList(3);
        ByteArrayOutputStream byteArrayOutputStream2 = new ByteArrayOutputStream();
        try {
            v(byteArrayOutputStream2, bVarArr.length);
            int i9 = 2;
            int i10 = 2;
            for (b bVar3 : bVarArr) {
                u(byteArrayOutputStream2, bVar3.f20259c, 4);
                u(byteArrayOutputStream2, bVar3.f20260d, 4);
                u(byteArrayOutputStream2, bVar3.f20263g, 4);
                String strD3 = d(bVar3.f20257a, bVar3.f20258b, bArr2);
                Charset charset3 = StandardCharsets.UTF_8;
                int length2 = strD3.getBytes(charset3).length;
                v(byteArrayOutputStream2, length2);
                i10 = i10 + 14 + length2;
                byteArrayOutputStream2.write(strD3.getBytes(charset3));
            }
            byte[] byteArray = byteArrayOutputStream2.toByteArray();
            if (i10 != byteArray.length) {
                throw new IllegalStateException("Expected size " + i10 + ", does not match actual size " + byteArray.length);
            }
            k kVar = new k(byteArray, 1, false);
            byteArrayOutputStream2.close();
            arrayList2.add(kVar);
            ByteArrayOutputStream byteArrayOutputStream3 = new ByteArrayOutputStream();
            int i11 = 0;
            int i12 = 0;
            while (i11 < bVarArr.length) {
                try {
                    b bVar4 = bVarArr[i11];
                    v(byteArrayOutputStream3, i11);
                    v(byteArrayOutputStream3, bVar4.f20261e);
                    i12 = i12 + 4 + (bVar4.f20261e * i9);
                    int[] iArr = bVar4.f20264h;
                    int length3 = iArr.length;
                    int i13 = i6;
                    int i14 = i9;
                    int i15 = i13;
                    while (i15 < length3) {
                        int i16 = iArr[i15];
                        v(byteArrayOutputStream3, i16 - i13);
                        i15++;
                        i13 = i16;
                    }
                    i11++;
                    i9 = i14;
                    i6 = 0;
                } catch (Throwable th) {
                }
            }
            byte[] byteArray2 = byteArrayOutputStream3.toByteArray();
            if (i12 != byteArray2.length) {
                throw new IllegalStateException("Expected size " + i12 + ", does not match actual size " + byteArray2.length);
            }
            k kVar2 = new k(byteArray2, 3, true);
            byteArrayOutputStream3.close();
            arrayList2.add(kVar2);
            byteArrayOutputStream3 = new ByteArrayOutputStream();
            int i17 = 0;
            int i18 = 0;
            while (i17 < bVarArr.length) {
                try {
                    b bVar5 = bVarArr[i17];
                    Iterator it3 = bVar5.f20265i.entrySet().iterator();
                    int iIntValue = 0;
                    while (it3.hasNext()) {
                        iIntValue |= ((Integer) ((Map.Entry) it3.next()).getValue()).intValue();
                    }
                    ByteArrayOutputStream byteArrayOutputStream4 = new ByteArrayOutputStream();
                    try {
                        r(byteArrayOutputStream4, iIntValue, bVar5);
                        byte[] byteArray3 = byteArrayOutputStream4.toByteArray();
                        byteArrayOutputStream4.close();
                        byteArrayOutputStream4 = new ByteArrayOutputStream();
                        try {
                            s(byteArrayOutputStream4, bVar5);
                            byte[] byteArray4 = byteArrayOutputStream4.toByteArray();
                            byteArrayOutputStream4.close();
                            v(byteArrayOutputStream3, i17);
                            int length4 = byteArray3.length + 2 + byteArray4.length;
                            int i19 = i18 + 6;
                            ArrayList arrayList4 = arrayList3;
                            u(byteArrayOutputStream3, length4, 4);
                            v(byteArrayOutputStream3, iIntValue);
                            byteArrayOutputStream3.write(byteArray3);
                            byteArrayOutputStream3.write(byteArray4);
                            i18 = i19 + length4;
                            i17++;
                            arrayList3 = arrayList4;
                        } finally {
                        }
                    } finally {
                    }
                } finally {
                    try {
                        byteArrayOutputStream3.close();
                        throw th;
                    } catch (Throwable th2) {
                        th.addSuppressed(th2);
                    }
                }
            }
            ArrayList arrayList5 = arrayList3;
            byte[] byteArray5 = byteArrayOutputStream3.toByteArray();
            if (i18 != byteArray5.length) {
                throw new IllegalStateException("Expected size " + i18 + ", does not match actual size " + byteArray5.length);
            }
            k kVar3 = new k(byteArray5, 4, true);
            byteArrayOutputStream3.close();
            arrayList2.add(kVar3);
            long j6 = 4;
            long size2 = j6 + j6 + 4 + ((long) (arrayList2.size() * 16));
            u(byteArrayOutputStream, arrayList2.size(), 4);
            int i20 = 0;
            while (i20 < arrayList2.size()) {
                k kVar4 = (k) arrayList2.get(i20);
                int i21 = kVar4.f20289a;
                if (i21 == 1) {
                    j4 = 0;
                } else if (i21 == 2) {
                    j4 = 1;
                } else if (i21 == 3) {
                    j4 = 2;
                } else if (i21 == 4) {
                    j4 = 3;
                } else {
                    if (i21 != 5) {
                        throw null;
                    }
                    j4 = 4;
                }
                u(byteArrayOutputStream, j4, 4);
                u(byteArrayOutputStream, size2, 4);
                byte[] bArr7 = kVar4.f20290b;
                if (kVar4.f20291c) {
                    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;
                i20++;
                arrayList5 = arrayList;
            }
            ArrayList arrayList6 = arrayList5;
            for (int i22 = 0; i22 < arrayList6.size(); i22++) {
                byteArrayOutputStream.write((byte[]) arrayList6.get(i22));
            }
            return true;
        } catch (Throwable th3) {
            try {
                byteArrayOutputStream2.close();
                throw th3;
            } catch (Throwable th4) {
                th3.addSuppressed(th4);
                throw th3;
            }
        }
    }

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

    public static void q(ByteArrayOutputStream byteArrayOutputStream, b bVar, String str) throws IOException {
        Charset charset = StandardCharsets.UTF_8;
        v(byteArrayOutputStream, str.getBytes(charset).length);
        v(byteArrayOutputStream, bVar.f20261e);
        u(byteArrayOutputStream, bVar.f20262f, 4);
        u(byteArrayOutputStream, bVar.f20259c, 4);
        u(byteArrayOutputStream, bVar.f20263g, 4);
        byteArrayOutputStream.write(str.getBytes(charset));
    }

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

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

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:114:0x01a3  */
    /* JADX WARN: Removed duplicated region for block: B:122:0x01bb  */
    /* JADX WARN: Removed duplicated region for block: B:125:0x01c9  */
    /* JADX WARN: Removed duplicated region for block: B:153:0x0213  */
    /* JADX WARN: Removed duplicated region for block: B:157:0x021f  */
    /* JADX WARN: Removed duplicated region for block: B:158:0x0223  */
    /* JADX WARN: Removed duplicated region for block: B:236:0x02d4  */
    /* JADX WARN: Removed duplicated region for block: B:245:0x02ea A[ADDED_TO_REGION] */
    /* JADX WARN: Removed duplicated region for block: B:247:0x02ee  */
    /* JADX WARN: Removed duplicated region for block: B:266:0x0107 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:27:0x0072  */
    /* JADX WARN: Removed duplicated region for block: B:282:0x0170 A[EXC_TOP_SPLITTER, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:293:0x01d4 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 z6) {
        char c6;
        FileInputStream fileInputStreamE;
        b[] bVarArrL;
        b[] bVarArr;
        b[] bVarArr2;
        byte[] bArr;
        boolean z7;
        Throwable th;
        Throwable th2;
        boolean z8;
        byte[] bArr2;
        ByteArrayOutputStream byteArrayOutputStream;
        int i6;
        C2449a c2449a;
        FileInputStream fileInputStreamE2;
        boolean z9;
        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 (!z6) {
                File file = new File(filesDir, "profileinstaller_profileWrittenFor_lastUpdateTime.dat");
                if (file.exists()) {
                    try {
                        DataInputStream dataInputStream = new DataInputStream(new FileInputStream(file));
                        try {
                            long j4 = dataInputStream.readLong();
                            dataInputStream.close();
                            z9 = j4 == packageInfo.lastUpdateTime;
                            if (z9) {
                                dVar.a(2, null);
                            }
                        } finally {
                        }
                    } catch (IOException unused) {
                        z9 = false;
                    }
                    if (z9) {
                    }
                } else {
                    z9 = false;
                    if (z9) {
                        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");
            C2449a c2449a2 = new C2449a(assets, executor, dVar, name, file2);
            byte[] bArr3 = (byte[]) c2449a2.f20252d;
            if (bArr3 != null) {
                if (file2.exists()) {
                    if (!file2.canWrite()) {
                        c2449a2.f(4, null);
                    }
                    c2449a2.f20249a = true;
                    byte[] bArr4 = f20269b;
                    try {
                        fileInputStreamE = c2449a2.e("dexopt/baseline.prof", assets);
                    } catch (FileNotFoundException e6) {
                        dVar.a(6, e6);
                        fileInputStreamE = null;
                    } catch (IOException e7) {
                        dVar.a(7, e7);
                        fileInputStreamE = null;
                    }
                    c6 = '\b';
                    try {
                        try {
                        } catch (IOException e8) {
                            dVar.a(7, e8);
                        }
                        if (fileInputStreamE != null) {
                            try {
                                try {
                                } catch (IOException e9) {
                                    dVar.a(7, e9);
                                    fileInputStreamE.close();
                                    bVarArrL = null;
                                    c2449a2.f20256h = bVarArrL;
                                    bVarArr = (b[]) c2449a2.f20256h;
                                    if (bVarArr != null) {
                                    }
                                    d dVar2 = (d) c2449a2.f20251c;
                                    bVarArr2 = (b[]) c2449a2.f20256h;
                                    if (bVarArr2 != null) {
                                    }
                                    bArr = (byte[]) c2449a2.f20253e;
                                    if (bArr != null) {
                                    }
                                    if (z7) {
                                    }
                                    z8 = z7;
                                    j.c(context, (z8 || !z6) ? 0 : c6);
                                }
                            } catch (IllegalStateException e10) {
                                dVar.a(8, e10);
                                fileInputStreamE.close();
                                bVarArrL = null;
                                c2449a2.f20256h = bVarArrL;
                                bVarArr = (b[]) c2449a2.f20256h;
                                if (bVarArr != null) {
                                }
                                d dVar22 = (d) c2449a2.f20251c;
                                bVarArr2 = (b[]) c2449a2.f20256h;
                                if (bVarArr2 != null) {
                                }
                                bArr = (byte[]) c2449a2.f20253e;
                                if (bArr != null) {
                                }
                                if (z7) {
                                }
                                z8 = z7;
                                j.c(context, (z8 || !z6) ? 0 : c6);
                            }
                            if (!Arrays.equals(bArr4, f(fileInputStreamE, 4))) {
                                throw new IllegalStateException("Invalid magic");
                            }
                            bVarArrL = l(fileInputStreamE, f(fileInputStreamE, 4), (String) c2449a2.f20255g);
                            try {
                                fileInputStreamE.close();
                            } catch (IOException e11) {
                                dVar.a(7, e11);
                            }
                            c2449a2.f20256h = bVarArrL;
                        }
                        bVarArr = (b[]) c2449a2.f20256h;
                        if (bVarArr != null && ((i6 = Build.VERSION.SDK_INT) >= 31 || i6 == 24 || i6 == 25)) {
                            try {
                                fileInputStreamE2 = c2449a2.e("dexopt/baseline.profm", assets);
                            } catch (FileNotFoundException e12) {
                                dVar.a(9, e12);
                            } catch (IOException e13) {
                                dVar.a(7, e13);
                            } catch (IllegalStateException e14) {
                                c2449a2.f20256h = null;
                                dVar.a(8, e14);
                            }
                            if (fileInputStreamE2 == null) {
                                try {
                                    if (!Arrays.equals(f20270c, f(fileInputStreamE2, 4))) {
                                        throw new IllegalStateException("Invalid magic");
                                    }
                                    c2449a2.f20256h = i(fileInputStreamE2, f(fileInputStreamE2, 4), bArr3, bVarArr);
                                    fileInputStreamE2.close();
                                    c2449a = c2449a2;
                                    if (c2449a != null) {
                                        c2449a2 = c2449a;
                                    }
                                } finally {
                                }
                            } else {
                                if (fileInputStreamE2 != null) {
                                    fileInputStreamE2.close();
                                }
                                c2449a = null;
                                if (c2449a != null) {
                                }
                            }
                        }
                        d dVar222 = (d) c2449a2.f20251c;
                        bVarArr2 = (b[]) c2449a2.f20256h;
                        if (bVarArr2 != null && (bArr2 = (byte[]) c2449a2.f20252d) != null) {
                            if (c2449a2.f20249a) {
                                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 e15) {
                                dVar222.a(7, e15);
                            } catch (IllegalStateException e16) {
                                dVar222.a(8, e16);
                            }
                            if (o(byteArrayOutputStream, bArr2, bVarArr2)) {
                                c2449a2.f20253e = byteArrayOutputStream.toByteArray();
                                byteArrayOutputStream.close();
                                c2449a2.f20256h = null;
                            } else {
                                dVar222.a(5, null);
                                c2449a2.f20256h = null;
                                byteArrayOutputStream.close();
                            }
                        }
                        bArr = (byte[]) c2449a2.f20253e;
                        if (bArr != null) {
                            z7 = false;
                            c6 = 1;
                        } else {
                            try {
                                if (!c2449a2.f20249a) {
                                    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) c2449a2.f20254f);
                                                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 i7 = byteArrayInputStream.read(bArr5);
                                                                                    if (i7 <= 0) {
                                                                                        break;
                                                                                    } else {
                                                                                        fileOutputStream.write(bArr5, 0, i7);
                                                                                    }
                                                                                }
                                                                                c6 = 1;
                                                                                c2449a2.f(1, null);
                                                                                fileLockTryLock.close();
                                                                                channel.close();
                                                                                fileOutputStream.close();
                                                                                byteArrayInputStream.close();
                                                                                c2449a2.f20253e = null;
                                                                                c2449a2.f20256h = null;
                                                                                z7 = 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 e17) {
                                        e = e17;
                                        c2449a2.f(6, e);
                                        z7 = false;
                                        if (z7) {
                                        }
                                        z8 = z7;
                                        j.c(context, (z8 || !z6) ? 0 : c6);
                                    } catch (IOException e18) {
                                        e = e18;
                                        c2449a2.f(7, e);
                                        z7 = false;
                                        if (z7) {
                                        }
                                        z8 = z7;
                                        j.c(context, (z8 || !z6) ? 0 : c6);
                                    }
                                } catch (FileNotFoundException e19) {
                                    e = e19;
                                    c6 = 1;
                                    c2449a2.f(6, e);
                                    z7 = false;
                                    if (z7) {
                                    }
                                    z8 = z7;
                                    j.c(context, (z8 || !z6) ? 0 : c6);
                                } catch (IOException e20) {
                                    e = e20;
                                    c6 = 1;
                                    c2449a2.f(7, e);
                                    z7 = false;
                                    if (z7) {
                                    }
                                    z8 = z7;
                                    j.c(context, (z8 || !z6) ? 0 : c6);
                                }
                            } finally {
                                c2449a2.f20253e = null;
                                c2449a2.f20256h = null;
                            }
                        }
                        if (z7) {
                            e(packageInfo, filesDir);
                        }
                        z8 = z7;
                    } finally {
                    }
                } else {
                    try {
                        if (!file2.createNewFile()) {
                            c2449a2.f(4, null);
                        }
                        c2449a2.f20249a = true;
                        byte[] bArr42 = f20269b;
                        fileInputStreamE = c2449a2.e("dexopt/baseline.prof", assets);
                        c6 = '\b';
                        if (fileInputStreamE != null) {
                        }
                        bVarArr = (b[]) c2449a2.f20256h;
                        if (bVarArr != null) {
                            fileInputStreamE2 = c2449a2.e("dexopt/baseline.profm", assets);
                            if (fileInputStreamE2 == null) {
                            }
                        }
                        d dVar2222 = (d) c2449a2.f20251c;
                        bVarArr2 = (b[]) c2449a2.f20256h;
                        if (bVarArr2 != null) {
                            if (c2449a2.f20249a) {
                            }
                        }
                        bArr = (byte[]) c2449a2.f20253e;
                        if (bArr != null) {
                        }
                        if (z7) {
                        }
                        z8 = z7;
                    } catch (IOException unused2) {
                        c6 = 1;
                        c2449a2.f(4, null);
                    }
                }
                j.c(context, (z8 || !z6) ? 0 : c6);
            }
            c2449a2.f(3, Integer.valueOf(Build.VERSION.SDK_INT));
            c6 = 1;
            z8 = false;
            j.c(context, (z8 || !z6) ? 0 : c6);
        } catch (PackageManager.NameNotFoundException e21) {
            dVar.a(7, e21);
            j.c(context, false);
        }
    }

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

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