package net.sf.scuba.tlv; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; import java.io.InputStream; import o.ghV; /* loaded from: classes6.dex */ public class TLVInputStream extends InputStream { private DataInputStream a; private final InputStream b; public ghV c; private ghV d; private int e; public TLVInputStream(InputStream inputStream) { this.e = 0; if ((inputStream instanceof BufferedInputStream) || (inputStream instanceof ByteArrayInputStream)) { try { this.e = inputStream.available(); } catch (IOException unused) { } } this.b = inputStream; this.a = inputStream instanceof DataInputStream ? (DataInputStream) inputStream : new DataInputStream(inputStream); this.c = new ghV(); this.d = null; } @Override // java.io.InputStream public int available() throws IOException { return this.a.available(); } @Override // java.io.InputStream public int read() throws IOException { int read = this.a.read(); if (read < 0) { return -1; } this.c.a(1); return read; } @Override // java.io.InputStream public long skip(long j) throws IOException { if (j <= 0) { return 0L; } long skip = this.a.skip(j); this.c.a((int) skip); return skip; } @Override // java.io.InputStream public void mark(int i) { synchronized (this) { this.a.mark(i); this.d = (ghV) this.c.clone(); } } @Override // java.io.InputStream public boolean markSupported() { return this.a.markSupported(); } @Override // java.io.InputStream public void reset() throws IOException { synchronized (this) { if (!markSupported()) { throw new IOException("mark/reset not supported"); } this.a.reset(); this.c = this.d; this.d = null; } } @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable public void close() throws IOException { this.a.close(); } public String toString() { return this.c.toString(); } public final long a() throws IOException { if (this.c.b || this.c.a) { return 0L; } ghV ghv = this.c; if (ghv.d.isEmpty()) { throw new IllegalStateException("Length of value is unknown."); } ghV.Sts peek = ghv.d.peek(); return skip(peek.d - peek.c); } public final int c() throws IOException { int i; if (!this.c.a) { throw new IllegalStateException("Not at start of length"); } int readUnsignedByte = this.a.readUnsignedByte(); if ((readUnsignedByte & 128) == 0) { i = 1; } else { int i2 = 0; i = 1; for (int i3 = 0; i3 < (readUnsignedByte & 127); i3++) { i++; i2 = (i2 << 8) | this.a.readUnsignedByte(); } readUnsignedByte = i2; } ghV ghv = this.c; if (readUnsignedByte < 0) { StringBuilder sb = new StringBuilder("Cannot set negative length (length = "); sb.append(readUnsignedByte); sb.append(", 0x"); sb.append(Integer.toHexString(readUnsignedByte)); sb.append(" for tag "); sb.append(Integer.toHexString(ghv.e())); sb.append(")."); throw new IllegalArgumentException(sb.toString()); } ghV.Sts pop = ghv.d.pop(); if (!ghv.d.isEmpty()) { ghv.d.peek().c += i; } pop.d = readUnsignedByte; ghv.d.push(pop); ghv.b = false; ghv.a = false; ghv.c = true; return readUnsignedByte; } public final int b() throws IOException { int readUnsignedByte; if (!this.c.b && !this.c.c) { throw new IllegalStateException("Not at start of tag"); } int readUnsignedByte2 = this.a.readUnsignedByte(); int i = 1; while (true) { if (readUnsignedByte2 != 0 && readUnsignedByte2 != 255) { break; } readUnsignedByte2 = this.a.readUnsignedByte(); i++; } if ((readUnsignedByte2 & 31) == 31) { while (true) { readUnsignedByte = this.a.readUnsignedByte(); i++; if ((readUnsignedByte & 128) != 128) { break; } readUnsignedByte2 = (readUnsignedByte2 << 8) | (readUnsignedByte & 127); } readUnsignedByte2 = (readUnsignedByte2 << 8) | (readUnsignedByte & 127); } ghV ghv = this.c; ghV.Sts sts = new ghV.Sts(ghv, readUnsignedByte2); if (!ghv.d.isEmpty()) { ghv.d.peek().c += i; } ghv.d.push(sts); ghv.b = false; ghv.a = true; ghv.c = false; return readUnsignedByte2; } public final byte[] d() throws IOException { if (this.c.c) { ghV ghv = this.c; if (ghv.d.isEmpty()) { throw new IllegalStateException("Length not yet known."); } int i = ghv.d.peek().d; byte[] bArr = new byte[i]; this.a.readFully(bArr); this.c.a(i); return bArr; } throw new IllegalStateException("Not yet processing value!"); } }