package org.bouncycastle.bcpg;

import java.io.BufferedInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import org.bouncycastle.util.io.Streams;

/* JADX INFO: loaded from: classes.dex */
public class BCPGInputStream extends InputStream implements PacketTags {
    InputStream in;
    int mNextB;
    int nextB;
    boolean next = false;
    boolean mNext = false;

    private static class PartialInputStream extends InputStream {
        private int dataLength;
        private final InputStream in;
        private boolean partial;

        PartialInputStream(InputStream inputStream, boolean z, int i) {
            this.in = inputStream;
            this.partial = z;
            this.dataLength = i;
        }

        private int loadDataLength() throws IOException {
            boolean[] zArr = new boolean[3];
            this.dataLength = StreamUtil.readBodyLen(this.in, zArr);
            if (zArr[StreamUtil.flag_eof]) {
                return -1;
            }
            this.partial = zArr[StreamUtil.flag_partial];
            return this.dataLength;
        }

        @Override // java.io.InputStream
        public int available() throws IOException {
            int iAvailable = this.in.available();
            if (iAvailable <= this.dataLength || this.dataLength < 0) {
                return iAvailable;
            }
            if (this.partial && this.dataLength == 0) {
                return 1;
            }
            return this.dataLength;
        }

        @Override // java.io.InputStream
        public int read() throws IOException {
            while (this.dataLength == 0) {
                if (!this.partial || loadDataLength() < 0) {
                    return -1;
                }
            }
            int i = this.in.read();
            if (i < 0) {
                throw new EOFException("premature end of stream in PartialInputStream");
            }
            this.dataLength--;
            if (this.partial && this.dataLength == 0) {
                loadDataLength();
            }
            return i;
        }

        @Override // java.io.InputStream
        public int read(byte[] bArr, int i, int i2) throws IOException {
            while (this.dataLength == 0) {
                if (!this.partial || loadDataLength() < 0) {
                    return -1;
                }
            }
            if (this.dataLength <= i2 && this.dataLength >= 0) {
                i2 = this.dataLength;
            }
            int i3 = this.in.read(bArr, i, i2);
            if (i3 < 0) {
                throw new EOFException("premature end of stream in PartialInputStream");
            }
            this.dataLength -= i3;
            if (this.partial && this.dataLength == 0) {
                loadDataLength();
            }
            return i3;
        }
    }

    public BCPGInputStream(InputStream inputStream) {
        this.in = inputStream;
    }

    public static BCPGInputStream wrap(InputStream inputStream) {
        return inputStream instanceof BCPGInputStream ? (BCPGInputStream) inputStream : new BCPGInputStream(inputStream);
    }

    @Override // java.io.InputStream
    public int available() throws IOException {
        return this.in.available();
    }

    @Override // java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws IOException {
        this.in.close();
    }

    @Override // java.io.InputStream
    public synchronized void mark(int i) {
        this.mNext = this.next;
        this.mNextB = this.nextB;
        this.in.mark(i);
    }

    @Override // java.io.InputStream
    public boolean markSupported() {
        return this.in.markSupported();
    }

    public int nextPacketTag() throws IOException {
        if (!this.next) {
            try {
                this.nextB = read();
            } catch (EOFException e) {
                this.nextB = -1;
            }
            this.next = true;
        }
        int i = this.nextB;
        int i2 = this.nextB;
        if (i < 0) {
            return i2;
        }
        int i3 = i2 & 63;
        return (this.nextB & 64) == 0 ? i3 >> 2 : i3;
    }

    @Override // java.io.InputStream
    public int read() throws IOException {
        if (!this.next) {
            return this.in.read();
        }
        this.next = false;
        return this.nextB;
    }

    @Override // java.io.InputStream
    public int read(byte[] bArr, int i, int i2) throws IOException {
        if (i2 == 0) {
            return 0;
        }
        if (!this.next) {
            return this.in.read(bArr, i, i2);
        }
        if (this.nextB < 0) {
            return -1;
        }
        bArr[i] = (byte) this.nextB;
        this.next = false;
        return 1;
    }

    public byte[] readAll() throws IOException {
        return Streams.readAll(this);
    }

    public void readFully(byte[] bArr) throws IOException {
        readFully(bArr, 0, bArr.length);
    }

    public void readFully(byte[] bArr, int i, int i2) throws IOException {
        if (Streams.readFully(this, bArr, i, i2) < i2) {
            throw new EOFException();
        }
    }

    public Packet readPacket() throws IOException {
        int i;
        int i2;
        int i3 = read();
        if (i3 < 0) {
            return null;
        }
        if ((i3 & 128) == 0) {
            throw new IOException("invalid header encountered");
        }
        boolean z = true;
        int bodyLen = 0;
        boolean z2 = (i3 & 64) != 0;
        if (z2) {
            i = i3 & 63;
            boolean[] zArr = new boolean[3];
            bodyLen = StreamUtil.readBodyLen(this, zArr);
            z = zArr[StreamUtil.flag_partial];
        } else {
            int i4 = i3 & 3;
            i = (i3 & 63) >> 2;
            switch (i4) {
                case 0:
                    i2 = read();
                    break;
                case 1:
                    i2 = StreamUtil.read2OctetLength(this);
                    break;
                case 2:
                    i2 = StreamUtil.read4OctetLength(this);
                    break;
                case 3:
                    break;
                default:
                    throw new IOException("unknown length type encountered");
            }
            bodyLen = i2;
            z = false;
        }
        BCPGInputStream bCPGInputStream = (bodyLen == 0 && z) ? this : new BCPGInputStream(new BufferedInputStream(new PartialInputStream(this.in, z, bodyLen)));
        switch (i) {
            case 0:
                return new ReservedPacket(bCPGInputStream, z2);
            case 1:
                return new PublicKeyEncSessionPacket(bCPGInputStream, z2);
            case 2:
                return new SignaturePacket(bCPGInputStream, z2);
            case 3:
                return new SymmetricKeyEncSessionPacket(bCPGInputStream, z2);
            case 4:
                return new OnePassSignaturePacket(bCPGInputStream, z2);
            case 5:
                return new SecretKeyPacket(bCPGInputStream, z2);
            case 6:
                return new PublicKeyPacket(bCPGInputStream, z2);
            case 7:
                return new SecretSubkeyPacket(bCPGInputStream, z2);
            case 8:
                return new CompressedDataPacket(bCPGInputStream, z2);
            case 9:
                return new SymmetricEncDataPacket(bCPGInputStream, z2);
            case 10:
                return new MarkerPacket(bCPGInputStream, z2);
            case 11:
                return new LiteralDataPacket(bCPGInputStream, z2);
            case 12:
                return new TrustPacket(bCPGInputStream, z2);
            case 13:
                return new UserIDPacket(bCPGInputStream, z2);
            case 14:
                return new PublicSubkeyPacket(bCPGInputStream, z2);
            case 17:
                return new UserAttributePacket(bCPGInputStream, z2);
            case 18:
                return new SymmetricEncIntegrityPacket(bCPGInputStream, z2);
            case 19:
                return new ModDetectionCodePacket(bCPGInputStream, z2);
            case 20:
                return new AEADEncDataPacket(bCPGInputStream, z2);
            case 21:
                return new PaddingPacket(bCPGInputStream, z2);
            case 60:
            case 61:
            case PacketTags.EXPERIMENTAL_3 /* 62 */:
            case 63:
                return new ExperimentalPacket(i, bCPGInputStream, z2);
            default:
                return new UnknownPacket(i, bCPGInputStream, z2);
        }
    }

    @Override // java.io.InputStream
    public synchronized void reset() throws IOException {
        this.next = this.mNext;
        this.nextB = this.mNextB;
        this.in.reset();
    }

    public int skipMarkerAndPaddingPackets() throws IOException {
        while (true) {
            int iNextPacketTag = nextPacketTag();
            if (iNextPacketTag != 10 && iNextPacketTag != 21) {
                return iNextPacketTag;
            }
            readPacket();
        }
    }

    public int skipMarkerPackets() throws IOException {
        return skipMarkerAndPaddingPackets();
    }
}
