package org.bouncycastle.asn1;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class ASN1StreamParser {
    private final java.io.InputStream a;
    private final int b;
    private final byte[][] c;

    ASN1StreamParser(java.io.InputStream inputStream, int i, byte[][] bArr) {
        this.a = inputStream;
        this.b = i;
        this.c = bArr;
    }

    private void k(boolean z) {
        java.io.InputStream inputStream = this.a;
        if (inputStream instanceof org.bouncycastle.asn1.IndefiniteLengthInputStream) {
            ((org.bouncycastle.asn1.IndefiniteLengthInputStream) inputStream).i(z);
        }
    }

    org.bouncycastle.asn1.ASN1Encodable a(int i) throws java.io.IOException {
        k(false);
        int iE = org.bouncycastle.asn1.ASN1InputStream.E(this.a, i);
        int iY = org.bouncycastle.asn1.ASN1InputStream.y(this.a, this.b, iE == 3 || iE == 4 || iE == 16 || iE == 17 || iE == 8);
        if (iY < 0) {
            if ((i & 32) == 0) {
                throw new java.io.IOException("indefinite-length primitive encoding encountered");
            }
            org.bouncycastle.asn1.ASN1StreamParser aSN1StreamParser = new org.bouncycastle.asn1.ASN1StreamParser(new org.bouncycastle.asn1.IndefiniteLengthInputStream(this.a, this.b), this.b, this.c);
            int i2 = i & 192;
            return i2 != 0 ? new org.bouncycastle.asn1.BERTaggedObjectParser(i2, iE, aSN1StreamParser) : aSN1StreamParser.e(iE);
        }
        org.bouncycastle.asn1.DefiniteLengthInputStream definiteLengthInputStream = new org.bouncycastle.asn1.DefiniteLengthInputStream(this.a, iY, this.b);
        if ((i & 224) == 0) {
            return g(iE, definiteLengthInputStream);
        }
        org.bouncycastle.asn1.ASN1StreamParser aSN1StreamParser2 = new org.bouncycastle.asn1.ASN1StreamParser(definiteLengthInputStream, definiteLengthInputStream.a(), this.c);
        int i3 = i & 192;
        if (i3 != 0) {
            return new org.bouncycastle.asn1.DLTaggedObjectParser(i3, iE, (i & 32) != 0, aSN1StreamParser2);
        }
        return aSN1StreamParser2.d(iE);
    }

    org.bouncycastle.asn1.ASN1Primitive b(int i, int i2, boolean z) {
        return !z ? org.bouncycastle.asn1.ASN1TaggedObject.C(i, i2, ((org.bouncycastle.asn1.DefiniteLengthInputStream) this.a).k()) : org.bouncycastle.asn1.ASN1TaggedObject.A(i, i2, j());
    }

    org.bouncycastle.asn1.ASN1Primitive c(int i, int i2) {
        return org.bouncycastle.asn1.ASN1TaggedObject.B(i, i2, j());
    }

    org.bouncycastle.asn1.ASN1Encodable d(int i) throws org.bouncycastle.asn1.ASN1Exception {
        if (i == 3) {
            return new org.bouncycastle.asn1.BERBitStringParser(this);
        }
        if (i == 4) {
            return new org.bouncycastle.asn1.BEROctetStringParser(this);
        }
        if (i == 8) {
            return new org.bouncycastle.asn1.DERExternalParser(this);
        }
        if (i == 16) {
            return new org.bouncycastle.asn1.DLSequenceParser(this);
        }
        if (i == 17) {
            return new org.bouncycastle.asn1.DLSetParser(this);
        }
        throw new org.bouncycastle.asn1.ASN1Exception("unknown DL object encountered: 0x" + java.lang.Integer.toHexString(i));
    }

    org.bouncycastle.asn1.ASN1Encodable e(int i) throws org.bouncycastle.asn1.ASN1Exception {
        if (i == 3) {
            return new org.bouncycastle.asn1.BERBitStringParser(this);
        }
        if (i == 4) {
            return new org.bouncycastle.asn1.BEROctetStringParser(this);
        }
        if (i == 8) {
            return new org.bouncycastle.asn1.DERExternalParser(this);
        }
        if (i == 16) {
            return new org.bouncycastle.asn1.BERSequenceParser(this);
        }
        if (i == 17) {
            return new org.bouncycastle.asn1.BERSetParser(this);
        }
        throw new org.bouncycastle.asn1.ASN1Exception("unknown BER object encountered: 0x" + java.lang.Integer.toHexString(i));
    }

    org.bouncycastle.asn1.ASN1Encodable f(int i) {
        return g(i, (org.bouncycastle.asn1.DefiniteLengthInputStream) this.a);
    }

    org.bouncycastle.asn1.ASN1Encodable g(int i, org.bouncycastle.asn1.DefiniteLengthInputStream definiteLengthInputStream) throws org.bouncycastle.asn1.ASN1Exception {
        if (i == 3) {
            return new org.bouncycastle.asn1.DLBitStringParser(definiteLengthInputStream);
        }
        if (i == 4) {
            return new org.bouncycastle.asn1.DEROctetStringParser(definiteLengthInputStream);
        }
        if (i == 8) {
            throw new org.bouncycastle.asn1.ASN1Exception("externals must use constructed encoding (see X.690 8.18)");
        }
        if (i == 16) {
            throw new org.bouncycastle.asn1.ASN1Exception("sets must use constructed encoding (see X.690 8.11.1/8.12.1)");
        }
        if (i == 17) {
            throw new org.bouncycastle.asn1.ASN1Exception("sequences must use constructed encoding (see X.690 8.9.1/8.10.1)");
        }
        try {
            return org.bouncycastle.asn1.ASN1InputStream.i(i, definiteLengthInputStream, this.c);
        } catch (java.lang.IllegalArgumentException e) {
            throw new org.bouncycastle.asn1.ASN1Exception("corrupted stream detected", e);
        }
    }

    org.bouncycastle.asn1.ASN1Encodable h(int i) throws java.io.IOException {
        if (i < 0 || i > 30) {
            throw new java.lang.IllegalArgumentException("invalid universal tag number: " + i);
        }
        int i2 = this.a.read();
        if (i2 < 0) {
            return null;
        }
        if ((i2 & (-33)) == i) {
            return a(i2);
        }
        throw new java.io.IOException("unexpected identifier encountered: " + i2);
    }

    public org.bouncycastle.asn1.ASN1Encodable i() throws java.io.IOException {
        int i = this.a.read();
        if (i < 0) {
            return null;
        }
        return a(i);
    }

    org.bouncycastle.asn1.ASN1EncodableVector j() throws java.io.IOException {
        int i = this.a.read();
        if (i < 0) {
            return new org.bouncycastle.asn1.ASN1EncodableVector(0);
        }
        org.bouncycastle.asn1.ASN1EncodableVector aSN1EncodableVector = new org.bouncycastle.asn1.ASN1EncodableVector();
        do {
            org.bouncycastle.asn1.ASN1Encodable aSN1EncodableA = a(i);
            aSN1EncodableVector.a(aSN1EncodableA instanceof org.bouncycastle.asn1.InMemoryRepresentable ? ((org.bouncycastle.asn1.InMemoryRepresentable) aSN1EncodableA).a() : aSN1EncodableA.j());
            i = this.a.read();
        } while (i >= 0);
        return aSN1EncodableVector;
    }
}
