package g2;

import com.android.org.conscrypt.ct.CTConstants;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/* JADX INFO: compiled from: ASN1InputStream.java */
/* JADX INFO: loaded from: classes.dex */
public class j extends FilterInputStream {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    private final int f4709e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    private final boolean f4710f;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    private final byte[][] f4711g;

    public j(InputStream inputStream) {
        this(inputStream, y1.c(inputStream));
    }

    static s b(int i3, r1 r1Var, byte[][] bArr) throws IOException {
        if (i3 == 10) {
            return f.p(d(r1Var, bArr));
        }
        if (i3 == 12) {
            return new d1(r1Var.e());
        }
        if (i3 == 30) {
            return new l0(c(r1Var));
        }
        switch (i3) {
            case 1:
                return c.p(d(r1Var, bArr));
            case 2:
                return new k(r1Var.e(), false);
            case CTConstants.CERTIFICATE_LENGTH_BYTES /* 3 */:
                return b.p(r1Var.c(), r1Var);
            case 4:
                return new v0(r1Var.e());
            case 5:
                return t0.f4744e;
            case 6:
                return n.q(d(r1Var, bArr));
            default:
                switch (i3) {
                    case 18:
                        return new u0(r1Var.e());
                    case 19:
                        return new y0(r1Var.e());
                    case 20:
                        return new b1(r1Var.e());
                    case 21:
                        return new f1(r1Var.e());
                    case 22:
                        return new s0(r1Var.e());
                    case 23:
                        return new y(r1Var.e());
                    case 24:
                        return new i(r1Var.e());
                    case 25:
                        return new r0(r1Var.e());
                    case 26:
                        return new g1(r1Var.e());
                    case 27:
                        return new p0(r1Var.e());
                    case 28:
                        return new e1(r1Var.e());
                    default:
                        throw new IOException("unknown tag " + i3 + " encountered");
                }
        }
    }

    private static char[] c(r1 r1Var) throws IOException {
        int i3;
        int iC = r1Var.c();
        if ((iC & 1) != 0) {
            throw new IOException("malformed BMPString encoding encountered");
        }
        int i4 = iC / 2;
        char[] cArr = new char[i4];
        byte[] bArr = new byte[8];
        int i5 = 0;
        int i6 = 0;
        while (iC >= 8) {
            if (k2.a.d(r1Var, bArr, 0, 8) != 8) {
                throw new EOFException("EOF encountered in middle of BMPString");
            }
            cArr[i6] = (char) ((bArr[0] << 8) | (bArr[1] & 255));
            cArr[i6 + 1] = (char) ((bArr[2] << 8) | (bArr[3] & 255));
            cArr[i6 + 2] = (char) ((bArr[4] << 8) | (bArr[5] & 255));
            cArr[i6 + 3] = (char) ((bArr[6] << 8) | (bArr[7] & 255));
            i6 += 4;
            iC -= 8;
        }
        if (iC > 0) {
            if (k2.a.d(r1Var, bArr, 0, iC) != iC) {
                throw new EOFException("EOF encountered in middle of BMPString");
            }
            while (true) {
                int i7 = i5 + 1;
                int i8 = i7 + 1;
                i3 = i6 + 1;
                cArr[i6] = (char) ((bArr[i5] << 8) | (bArr[i7] & 255));
                if (i8 >= iC) {
                    break;
                }
                i5 = i8;
                i6 = i3;
            }
            i6 = i3;
        }
        if (r1Var.c() == 0 && i4 == i6) {
            return cArr;
        }
        throw new IllegalStateException();
    }

    private static byte[] d(r1 r1Var, byte[][] bArr) throws IOException {
        int iC = r1Var.c();
        if (iC >= bArr.length) {
            return r1Var.e();
        }
        byte[] bArr2 = bArr[iC];
        if (bArr2 == null) {
            bArr2 = new byte[iC];
            bArr[iC] = bArr2;
        }
        r1Var.d(bArr2);
        return bArr2;
    }

    static int g(InputStream inputStream, int i3, boolean z2) throws IOException {
        int i4 = inputStream.read();
        if (i4 < 0) {
            throw new EOFException("EOF found when length expected");
        }
        if (i4 == 128) {
            return -1;
        }
        if (i4 <= 127) {
            return i4;
        }
        int i5 = i4 & 127;
        if (i5 > 4) {
            throw new IOException("DER length more than 4 bytes: " + i5);
        }
        int i6 = 0;
        for (int i7 = 0; i7 < i5; i7++) {
            int i8 = inputStream.read();
            if (i8 < 0) {
                throw new EOFException("EOF found reading length");
            }
            i6 = (i6 << 8) + i8;
        }
        if (i6 < 0) {
            throw new IOException("corrupted stream - negative length found");
        }
        if (i6 < i3 || z2) {
            return i6;
        }
        throw new IOException("corrupted stream - out of bounds length found: " + i6 + " >= " + i3);
    }

    static int i(InputStream inputStream, int i3) throws IOException {
        int i4 = i3 & 31;
        if (i4 != 31) {
            return i4;
        }
        int i5 = 0;
        int i6 = inputStream.read();
        if ((i6 & 127) == 0) {
            throw new IOException("corrupted stream - invalid high tag number found");
        }
        while (i6 >= 0 && (i6 & 128) != 0) {
            i5 = (i5 | (i6 & 127)) << 7;
            i6 = inputStream.read();
        }
        if (i6 >= 0) {
            return i5 | (i6 & 127);
        }
        throw new EOFException("EOF found inside tag value.");
    }

    protected s a(int i3, int i4, int i5) throws IOException {
        boolean z2 = (i3 & 32) != 0;
        r1 r1Var = new r1(this, i5, this.f4709e);
        if ((i3 & 64) != 0) {
            return new h1(z2, i4, r1Var.e());
        }
        if ((i3 & 128) != 0) {
            return new w(r1Var).c(z2, i4);
        }
        if (!z2) {
            return b(i4, r1Var, this.f4711g);
        }
        if (i4 != 4) {
            if (i4 == 8) {
                return new j1(j(r1Var));
            }
            if (i4 == 16) {
                return this.f4710f ? new v1(r1Var.e()) : k1.a(j(r1Var));
            }
            if (i4 == 17) {
                return k1.b(j(r1Var));
            }
            throw new IOException("unknown tag " + i4 + " encountered");
        }
        e eVarJ = j(r1Var);
        int iF = eVarJ.f();
        o[] oVarArr = new o[iF];
        for (int i6 = 0; i6 != iF; i6++) {
            d dVarD = eVarJ.d(i6);
            if (!(dVarD instanceof o)) {
                throw new g("unknown object encountered in constructed OCTET STRING: " + dVarD.getClass());
            }
            oVarArr[i6] = (o) dVarD;
        }
        return new c0(oVarArr);
    }

    int e() {
        return this.f4709e;
    }

    protected int f() throws IOException {
        return g(this, this.f4709e, false);
    }

    public s h() throws IOException {
        int i3 = read();
        if (i3 <= 0) {
            if (i3 != 0) {
                return null;
            }
            throw new IOException("unexpected end-of-contents marker");
        }
        int i4 = i(this, i3);
        boolean z2 = (i3 & 32) != 0;
        int iF = f();
        if (iF >= 0) {
            try {
                return a(i3, i4, iF);
            } catch (IllegalArgumentException e3) {
                throw new g("corrupted stream detected", e3);
            }
        }
        if (!z2) {
            throw new IOException("indefinite-length primitive encoding encountered");
        }
        w wVar = new w(new t1(this, this.f4709e), this.f4709e);
        if ((i3 & 64) != 0) {
            return new a0(i4, wVar).a();
        }
        if ((i3 & 128) != 0) {
            return new j0(true, i4, wVar).a();
        }
        if (i4 == 4) {
            return new d0(wVar).a();
        }
        if (i4 == 8) {
            return new o0(wVar).a();
        }
        if (i4 == 16) {
            return new f0(wVar).a();
        }
        if (i4 == 17) {
            return new h0(wVar).a();
        }
        throw new IOException("unknown BER object encountered");
    }

    e j(r1 r1Var) throws IOException {
        if (r1Var.c() < 1) {
            return new e(0);
        }
        j jVar = new j(r1Var);
        e eVar = new e();
        while (true) {
            s sVarH = jVar.h();
            if (sVarH == null) {
                return eVar;
            }
            eVar.a(sVarH);
        }
    }

    public j(byte[] bArr) {
        this(new ByteArrayInputStream(bArr), bArr.length);
    }

    public j(byte[] bArr, boolean z2) {
        this(new ByteArrayInputStream(bArr), bArr.length, z2);
    }

    public j(InputStream inputStream, int i3) {
        this(inputStream, i3, false);
    }

    public j(InputStream inputStream, int i3, boolean z2) {
        super(inputStream);
        this.f4709e = i3;
        this.f4710f = z2;
        this.f4711g = new byte[11][];
    }
}
