package la;

import android.content.Context;
import android.preference.PreferenceManager;
import androidx.annotation.Nullable;
import androidx.room.x;
import com.google.crypto.tink.internal.TinkBugException;
import com.google.crypto.tink.proto.KeyStatusType;
import com.google.crypto.tink.shaded.protobuf.q;
import da.g;
import da.h;
import da.l;
import java.io.ByteArrayInputStream;
import java.io.CharConversionException;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyStoreException;
import java.security.ProviderException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import ma.j0;
import ma.m;
import ma.p0;
import ra.a1;
import ra.d1;
import xyz.stream.utils.e0;

/* JADX INFO: loaded from: classes2.dex */
public final class a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public Context f30186a = null;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public String f30187b = null;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public String f30188c = null;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public String f30189d = null;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public da.a f30190e = null;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public e0 f30191f = null;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public nb.c f30192g;

    public static nb.c c(byte[] bArr) throws IOException {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bArr);
        try {
            d1 d1VarK = d1.K(byteArrayInputStream, q.a());
            byteArrayInputStream.close();
            return new nb.c((a1) h.b(d1VarK).d().B(), 21);
        } catch (Throwable th) {
            byteArrayInputStream.close();
            throw th;
        }
    }

    @Nullable
    private static byte[] readKeysetFromPrefs(Context context, String str, String str2) throws IOException {
        if (str == null) {
            throw new IllegalArgumentException("keysetName cannot be null");
        }
        Context applicationContext = context.getApplicationContext();
        try {
            String string = (str2 == null ? PreferenceManager.getDefaultSharedPreferences(applicationContext) : applicationContext.getSharedPreferences(str2, 0)).getString(str, null);
            if (string == null) {
                return null;
            }
            return com.bumptech.glide.f.d(string);
        } catch (ClassCastException | IllegalArgumentException unused) {
            throw new CharConversionException(String.format("can't read keyset; the pref value %s is not a valid hex string", str));
        }
    }

    @Nullable
    private da.a readOrGenerateNewMasterKey() throws GeneralSecurityException {
        Object obj = b.f30193b;
        f fVar = new f();
        try {
            boolean zA = f.a(this.f30189d);
            try {
                return fVar.b(this.f30189d);
            } catch (GeneralSecurityException | ProviderException e10) {
                if (!zA) {
                    throw new KeyStoreException(String.format("the master key %s exists but is unusable", this.f30189d), e10);
                }
                Object obj2 = b.f30193b;
                return null;
            }
        } catch (GeneralSecurityException | ProviderException unused) {
            Object obj3 = b.f30193b;
            return null;
        }
    }

    public final synchronized b a() {
        b bVar;
        try {
            if (this.f30187b == null) {
                throw new IllegalArgumentException("keysetName cannot be null");
            }
            synchronized (b.f30193b) {
                try {
                    byte[] keysetFromPrefs = readKeysetFromPrefs(this.f30186a, this.f30187b, this.f30188c);
                    if (keysetFromPrefs == null) {
                        if (this.f30189d != null) {
                            this.f30190e = readOrGenerateNewMasterKey();
                        }
                        this.f30192g = b();
                    } else if (this.f30189d != null) {
                        this.f30192g = d(keysetFromPrefs);
                    } else {
                        this.f30192g = c(keysetFromPrefs);
                    }
                    bVar = new b();
                    new xyz.stream.utils.f(this.f30186a, this.f30187b, this.f30188c);
                    bVar.f30194a = this.f30192g;
                } finally {
                }
            }
        } catch (Throwable th) {
            throw th;
        }
        return bVar;
    }

    public final nb.c b() throws GeneralSecurityException {
        int i10;
        KeyStatusType keyStatusType;
        e0 e0Var = this.f30191f;
        if (e0Var == null) {
            throw new GeneralSecurityException("cannot read or generate keyset");
        }
        l lVarM = (l) e0Var.f36129c;
        if (lVarM == null) {
            try {
                lVarM = com.bumptech.glide.f.m(e0Var.o().e());
            } catch (GeneralSecurityException e10) {
                throw new TinkBugException("Parsing parameters failed in getProto(). You probably want to call some Tink register function for " + ((l) e0Var.f36129c), e10);
            }
        }
        x xVar = new x();
        da.d dVar = new da.d(lVarM);
        da.e eVar = da.e.f25072a;
        dVar.f25070c = eVar;
        x xVar2 = dVar.f25071d;
        if (xVar2 != null) {
            Iterator it = ((List) xVar2.f4197c).iterator();
            while (it.hasNext()) {
                ((da.d) it.next()).f25068a = false;
            }
        }
        char c6 = 1;
        dVar.f25068a = true;
        if (dVar.f25071d != null) {
            throw new IllegalStateException("Entry has already been added to a KeysetHandle.Builder");
        }
        Object obj = xVar.f4197c;
        Iterator it2 = ((List) obj).iterator();
        while (it2.hasNext()) {
            ((da.d) it2.next()).f25068a = false;
        }
        dVar.f25071d = xVar;
        List<da.d> list = (List) obj;
        list.add(dVar);
        if (((GeneralSecurityException) xVar.f4198d) != null) {
            throw new GeneralSecurityException("Cannot build keyset due to error in original", (GeneralSecurityException) xVar.f4198d);
        }
        if (xVar.f4196b) {
            throw new GeneralSecurityException("KeysetHandle.Builder#build must only be called once");
        }
        xVar.f4196b = true;
        ArrayList<g> arrayList = new ArrayList(list.size());
        for (int i11 = 0; i11 < list.size() - 1; i11++) {
            if (((da.d) list.get(i11)).f25070c == eVar && ((da.d) list.get(i11 + 1)).f25070c != eVar) {
                throw new GeneralSecurityException("Entries with 'withRandomId()' may only be followed by other entries with 'withRandomId()'.");
            }
        }
        HashSet hashSet = new HashSet();
        Integer numValueOf = null;
        for (da.d dVar2 : list) {
            dVar2.getClass();
            da.e eVar2 = dVar2.f25070c;
            if (eVar2 == null) {
                throw new GeneralSecurityException("No ID was set (with withFixedId or withRandomId)");
            }
            if (eVar2 == eVar) {
                i10 = 0;
                while (true) {
                    if (i10 != 0 && !hashSet.contains(Integer.valueOf(i10))) {
                        break;
                    }
                    int i12 = p0.f30547a;
                    i10 = 0;
                    while (i10 == 0) {
                        byte[] bArrA = j0.a(4);
                        i10 = (bArrA[3] & 255) | ((bArrA[0] & 255) << 24) | ((bArrA[c6] & 255) << 16) | ((bArrA[2] & 255) << 8);
                    }
                }
            } else {
                i10 = 0;
            }
            if (hashSet.contains(Integer.valueOf(i10))) {
                throw new GeneralSecurityException(defpackage.e.e("Id ", i10, " is used twice in the keyset"));
            }
            hashSet.add(Integer.valueOf(i10));
            l lVar = dVar2.f25069b;
            com.bumptech.glide.g gVarB = m.f30537b.b(lVar, lVar.a() ? Integer.valueOf(i10) : null);
            Object obj2 = da.c.f25064b;
            if (obj2.equals(obj2)) {
                keyStatusType = KeyStatusType.ENABLED;
            } else if (da.c.f25065c.equals(obj2)) {
                keyStatusType = KeyStatusType.DISABLED;
            } else {
                if (!da.c.f25066d.equals(obj2)) {
                    throw new IllegalStateException("Unknown key status");
                }
                keyStatusType = KeyStatusType.DESTROYED;
            }
            g gVar = new g(gVarB, keyStatusType, i10, dVar2.f25068a, false, g.f25074h);
            if (dVar2.f25068a) {
                if (numValueOf != null) {
                    throw new GeneralSecurityException("Two primaries were set");
                }
                numValueOf = Integer.valueOf(i10);
            }
            arrayList.add(gVar);
            c6 = 1;
        }
        if (numValueOf == null) {
            throw new GeneralSecurityException("No primary was set");
        }
        ma.l lVar2 = (ma.l) xVar.f4195a;
        h hVar = new h(arrayList, lVar2);
        if (!lVar2.f30534a.isEmpty()) {
            androidx.media3.exoplayer.analytics.g gVar2 = new androidx.media3.exoplayer.analytics.g(hVar, lVar2, 14);
            ArrayList arrayList2 = new ArrayList(arrayList.size());
            for (g gVar3 : arrayList) {
                arrayList2.add(new g(gVar3.f25075a, gVar3.f25076b, gVar3.f25078d, gVar3.f25079e, gVar3.f25080f, gVar2));
            }
            hVar = new h(arrayList2, lVar2, hVar);
        }
        xyz.stream.utils.f fVar = new xyz.stream.utils.f(this.f30186a, this.f30187b, this.f30188c);
        da.a aVar = this.f30190e;
        Object obj3 = b.f30193b;
        try {
            if (aVar != null) {
                com.bumptech.glide.f.r(hVar, fVar, aVar, new byte[0]);
            } else {
                com.bumptech.glide.f.s(hVar, fVar);
            }
            return new nb.c((a1) hVar.d().B(), 21);
        } catch (IOException e11) {
            throw new GeneralSecurityException(e11);
        }
    }

    public final nb.c d(byte[] bArr) {
        try {
            this.f30190e = new f().b(this.f30189d);
            try {
                return new nb.c((a1) com.bumptech.glide.f.n(new a5.d(new ByteArrayInputStream(bArr)), this.f30190e, new byte[0]).d().B(), 21);
            } catch (IOException | GeneralSecurityException e10) {
                try {
                    return c(bArr);
                } catch (IOException unused) {
                    throw e10;
                }
            }
        } catch (GeneralSecurityException | ProviderException e11) {
            try {
                nb.c cVarC = c(bArr);
                Object obj = b.f30193b;
                return cVarC;
            } catch (IOException unused2) {
                throw e11;
            }
        }
    }
}
