package com.pgl.ssdk;

import android.text.TextUtils;
import androidx.media3.exoplayer.rtsp.SessionDescription;
import androidx.media3.exoplayer.source.ProgressiveMediaSource;
import com.pgl.ssdk.ces.out.DungeonFlag;
import com.pgl.ssdk.g;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.Locale;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import org.json.JSONException;
import org.json.JSONObject;

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

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    private static String f24783a = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    private static String f24784b = null;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    private static Boolean f24785c = null;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    private static String f24786d = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private static String f24787e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private static String f24788f = null;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private static int f24789g = -1;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    private static long f24790h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    private static long f24791i;

    /* JADX WARN: Removed duplicated region for block: B:29:0x0026 A[EXC_TOP_SPLITTER, PHI: r1
      0x0026: PHI (r1v3 java.io.RandomAccessFile) = (r1v2 java.io.RandomAccessFile), (r1v4 java.io.RandomAccessFile) binds: [B:8:0x001f, B:11:0x0023] A[DONT_GENERATE, DONT_INLINE], SYNTHETIC] */
    @DungeonFlag
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private static e a(File file) throws Throwable {
        RandomAccessFile randomAccessFile;
        List<e> listA;
        try {
            randomAccessFile = new RandomAccessFile(file, "r");
            try {
                q qVarA = r.a(randomAccessFile, 0L, randomAccessFile.length());
                listA = l.a(qVarA, c.a(qVarA));
                try {
                    randomAccessFile.close();
                } catch (IOException unused) {
                }
            } catch (g.a unused2) {
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException unused3) {
                    }
                }
                listA = null;
            } catch (Throwable unused4) {
                if (randomAccessFile != null) {
                }
                listA = null;
            }
        } catch (g.a unused5) {
            randomAccessFile = null;
        } catch (Throwable unused6) {
            randomAccessFile = null;
        }
        if (listA == null || listA.isEmpty()) {
            listA = k.a(file);
        }
        if (listA == null || listA.isEmpty()) {
            return null;
        }
        return listA.get(0);
    }

    public static long b(File file) throws Throwable {
        String str;
        ZipFile zipFile = null;
        try {
            ZipFile zipFile2 = new ZipFile(file);
            int i10 = 1;
            long size = 0;
            while (true) {
                if (i10 == 1) {
                    str = "classes.dex";
                } else {
                    str = String.format(Locale.getDefault(), "classes%d.dex", Integer.valueOf(i10));
                }
                ZipEntry entry = zipFile2.getEntry(str);
                if (entry != null) {
                    try {
                        size += entry.getSize();
                        i10++;
                    } catch (IOException unused) {
                        zipFile = zipFile2;
                        if (zipFile != null) {
                            try {
                                zipFile.close();
                            } catch (IOException unused2) {
                            }
                        }
                        return 0L;
                    } catch (Throwable th) {
                        th = th;
                        zipFile = zipFile2;
                        if (zipFile != null) {
                            try {
                                zipFile.close();
                            } catch (IOException unused3) {
                            }
                        }
                        throw th;
                    }
                } else {
                    try {
                        break;
                    } catch (IOException unused4) {
                    }
                }
            }
            zipFile2.close();
            return size;
        } catch (IOException unused5) {
        } catch (Throwable th2) {
            th = th2;
        }
    }

    public static String c() throws Throwable {
        if (TextUtils.isEmpty(f24787e) || TextUtils.isEmpty(f24783a) || TextUtils.isEmpty(f24788f) || TextUtils.isEmpty(f24784b)) {
            d();
        }
        return b();
    }

    public static void d() throws Throwable {
        if (TextUtils.isEmpty(f24783a) || TextUtils.isEmpty(f24784b)) {
            f();
        }
        String strE = e();
        if (TextUtils.isEmpty(strE)) {
            return;
        }
        File file = new File(strE);
        if (file.exists() && file.canRead()) {
            f24790h = file.length();
            f24791i = b(file);
            f24787e = c(file);
            if (f24789g == -1 || TextUtils.isEmpty(f24788f)) {
                Object[] objArr = (Object[]) com.pgl.ssdk.ces.a.meta(158, z.a(), strE);
                Integer num = (Integer) objArr[0];
                String str = (String) objArr[1];
                if (str != null) {
                    f24788f = str;
                }
                if (num != null) {
                    f24789g = num.intValue();
                }
            }
        }
    }

    public static String e() {
        if (z.a() == null) {
            return null;
        }
        String packageCodePath = z.a().getPackageCodePath();
        if (TextUtils.isEmpty(packageCodePath)) {
            return null;
        }
        File file = new File(packageCodePath);
        if (file.exists() && file.canRead()) {
            return packageCodePath;
        }
        return null;
    }

    public static void f() throws Throwable {
        String strE = e();
        if (TextUtils.isEmpty(strE)) {
            return;
        }
        File file = new File(strE);
        if (file.exists() && file.canRead()) {
            e eVarA = a(file);
            if (eVarA != null) {
                f24783a = a(eVarA.a());
                f24786d = eVarA.b();
            }
            f24784b = g();
        }
    }

    public static String g() {
        try {
            return a(z.a().getPackageManager().getPackageInfo(z.a().getPackageName(), 64).signatures[0].toByteArray());
        } catch (Throwable unused) {
            return null;
        }
    }

    public static boolean h() {
        if (TextUtils.isEmpty(f24783a) || TextUtils.isEmpty(f24784b)) {
            f();
        }
        return a();
    }

    public static String a(byte[] bArr) {
        try {
            byte[] bArrDigest = MessageDigest.getInstance("SHA1").digest(bArr);
            StringBuilder sb = new StringBuilder();
            for (byte b10 : bArrDigest) {
                sb.append(Integer.toHexString((b10 & 255) | 256).substring(1, 3).toUpperCase());
                sb.append(":");
            }
            return sb.substring(0, sb.length() - 1);
        } catch (NoSuchAlgorithmException unused) {
            return null;
        }
    }

    private static String b() {
        JSONObject jSONObject = new JSONObject();
        try {
            if (!TextUtils.isEmpty(f24783a)) {
                jSONObject.put("sign", f24783a);
            }
            if (!TextUtils.isEmpty(f24786d)) {
                jSONObject.put("subject", f24786d);
            }
            if (!TextUtils.isEmpty(f24787e)) {
                jSONObject.put("md5", f24787e);
            }
            if (!TextUtils.isEmpty(f24788f)) {
                jSONObject.put("path", f24788f);
            }
            long j10 = f24790h;
            if (j10 != 0) {
                jSONObject.put("apkSize", j10);
            }
            long j11 = f24791i;
            if (j11 != 0) {
                jSONObject.put("dexSize", j11);
            }
            int i10 = f24789g;
            if (i10 != -1) {
                jSONObject.put("code", i10);
            }
            if (!TextUtils.isEmpty(f24784b)) {
                jSONObject.put("signpm", f24784b);
            }
            jSONObject.put("rebud", a());
            return jSONObject.toString();
        } catch (JSONException unused) {
            return null;
        }
    }

    public static String c(File file) throws Throwable {
        RandomAccessFile randomAccessFile = null;
        try {
            RandomAccessFile randomAccessFile2 = new RandomAccessFile(file, "r");
            try {
                MessageDigest messageDigest = MessageDigest.getInstance("MD5");
                byte[] bArr = new byte[ProgressiveMediaSource.DEFAULT_LOADING_CHECK_INTERVAL_BYTES];
                while (true) {
                    int i10 = randomAccessFile2.read(bArr);
                    if (i10 == -1) {
                        break;
                    }
                    messageDigest.update(bArr, 0, i10);
                }
                String string = new BigInteger(1, messageDigest.digest()).toString(16);
                while (string.length() < 32) {
                    string = SessionDescription.SUPPORTED_SDP_VERSION.concat(string);
                }
                try {
                    randomAccessFile2.close();
                } catch (IOException unused) {
                }
                return string;
            } catch (IOException | NoSuchAlgorithmException unused2) {
                randomAccessFile = randomAccessFile2;
                if (randomAccessFile == null) {
                    return "";
                }
                try {
                    randomAccessFile.close();
                    return "";
                } catch (IOException unused3) {
                    return "";
                }
            } catch (Throwable th) {
                th = th;
                randomAccessFile = randomAccessFile2;
                if (randomAccessFile != null) {
                    try {
                        randomAccessFile.close();
                    } catch (IOException unused4) {
                    }
                }
                throw th;
            }
        } catch (IOException | NoSuchAlgorithmException unused5) {
        } catch (Throwable th2) {
            th = th2;
        }
    }

    private static boolean a() {
        Boolean bool = f24785c;
        if (bool != null) {
            return bool.booleanValue();
        }
        if (TextUtils.isEmpty(f24784b) || TextUtils.isEmpty(f24783a)) {
            return false;
        }
        Boolean boolValueOf = Boolean.valueOf(!f24784b.equals(f24783a));
        f24785c = boolValueOf;
        return boolValueOf.booleanValue();
    }
}
