package org.bouncycastle.asn1; import com.google.common.base.Ascii; import com.google.common.primitives.UnsignedBytes; import java.io.IOException; import org.bouncycastle.util.Arrays; /* loaded from: classes6.dex */ public abstract class ASN1ApplicationSpecific extends ASN1Primitive { protected final boolean isConstructed; protected final byte[] octets; protected final int tag; @Override // org.bouncycastle.asn1.ASN1Primitive public boolean isConstructed() { return this.isConstructed; } @Override // org.bouncycastle.asn1.ASN1Primitive, org.bouncycastle.asn1.ASN1Object public int hashCode() { boolean z = this.isConstructed; return ((z ? 1 : 0) ^ this.tag) ^ Arrays.hashCode(this.octets); } public ASN1Primitive getObject(int i) throws IOException { if (i >= 31) { throw new IOException("unsupported tag number"); } byte[] encoded = getEncoded(); byte[] replaceTagNumber = replaceTagNumber(i, encoded); if ((encoded[0] & 32) != 0) { replaceTagNumber[0] = (byte) (replaceTagNumber[0] | 32); } return ASN1Primitive.fromByteArray(replaceTagNumber); } public ASN1Primitive getObject() throws IOException { return ASN1Primitive.fromByteArray(getContents()); } public byte[] getContents() { return Arrays.clone(this.octets); } public int getApplicationTag() { return this.tag; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // org.bouncycastle.asn1.ASN1Primitive public int encodedLength() throws IOException { return StreamUtil.calculateTagLength(this.tag) + StreamUtil.calculateBodyLength(this.octets.length) + this.octets.length; } /* JADX INFO: Access modifiers changed from: package-private */ @Override // org.bouncycastle.asn1.ASN1Primitive public void encode(ASN1OutputStream aSN1OutputStream) throws IOException { aSN1OutputStream.writeEncoded(this.isConstructed ? 96 : 64, this.tag, this.octets); } @Override // org.bouncycastle.asn1.ASN1Primitive boolean asn1Equals(ASN1Primitive aSN1Primitive) { if (!(aSN1Primitive instanceof ASN1ApplicationSpecific)) { return false; } ASN1ApplicationSpecific aSN1ApplicationSpecific = (ASN1ApplicationSpecific) aSN1Primitive; return this.isConstructed == aSN1ApplicationSpecific.isConstructed && this.tag == aSN1ApplicationSpecific.tag && Arrays.areEqual(this.octets, aSN1ApplicationSpecific.octets); } private byte[] replaceTagNumber(int i, byte[] bArr) throws IOException { int i2; if ((bArr[0] & Ascii.US) == 31) { byte b = bArr[1]; int i3 = b & UnsignedBytes.MAX_VALUE; if ((b & Ascii.DEL) == 0) { throw new ASN1ParsingException("corrupted stream - invalid high tag number found"); } i2 = 2; while (i3 >= 0 && (i3 & 128) != 0) { i3 = bArr[i2] & UnsignedBytes.MAX_VALUE; i2++; } } else { i2 = 1; } int length = bArr.length - i2; byte[] bArr2 = new byte[length + 1]; System.arraycopy(bArr, i2, bArr2, 1, length); bArr2[0] = (byte) i; return bArr2; } /* JADX INFO: Access modifiers changed from: protected */ public static int getLengthOfHeader(byte[] bArr) { byte b = bArr[1]; int i = b & UnsignedBytes.MAX_VALUE; if (i == 128 || i <= 127) { return 2; } int i2 = b & Ascii.DEL; if (i2 <= 4) { return i2 + 2; } throw new IllegalStateException("DER length more than 4 bytes: ".concat(String.valueOf(i2))); } public static ASN1ApplicationSpecific getInstance(Object obj) { if (obj == null || (obj instanceof ASN1ApplicationSpecific)) { return (ASN1ApplicationSpecific) obj; } if (!(obj instanceof byte[])) { StringBuilder sb = new StringBuilder("unknown object in getInstance: "); sb.append(obj.getClass().getName()); throw new IllegalArgumentException(sb.toString()); } try { return getInstance(ASN1Primitive.fromByteArray((byte[]) obj)); } catch (IOException e) { StringBuilder sb2 = new StringBuilder("Failed to construct object from byte[]: "); sb2.append(e.getMessage()); throw new IllegalArgumentException(sb2.toString()); } } /* JADX INFO: Access modifiers changed from: package-private */ public ASN1ApplicationSpecific(boolean z, int i, byte[] bArr) { this.isConstructed = z; this.tag = i; this.octets = Arrays.clone(bArr); } }