package org.bouncycastle.crypto.io;

/* JADX INFO: loaded from: E:\ios\vf_dex\classes4.dex */
public class CipherInputStream extends java.io.FilterInputStream {
    private org.bouncycastle.crypto.SkippingCipher a;
    private byte[] b;
    private org.bouncycastle.crypto.BufferedBlockCipher c;
    private org.bouncycastle.crypto.StreamCipher d;
    private org.bouncycastle.crypto.modes.AEADBlockCipher e;
    private byte[] f;
    private byte[] g;
    private int h;
    private int i;
    private boolean j;
    private long k;
    private int l;

    private void a(int i, boolean z) {
        if (z) {
            org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher = this.c;
            if (bufferedBlockCipher != null) {
                i = bufferedBlockCipher.c(i);
            } else {
                org.bouncycastle.crypto.modes.AEADBlockCipher aEADBlockCipher = this.e;
                if (aEADBlockCipher != null) {
                    i = aEADBlockCipher.getOutputSize(i);
                }
            }
        } else {
            org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher2 = this.c;
            if (bufferedBlockCipher2 != null) {
                i = bufferedBlockCipher2.e(i);
            } else {
                org.bouncycastle.crypto.modes.AEADBlockCipher aEADBlockCipher2 = this.e;
                if (aEADBlockCipher2 != null) {
                    i = aEADBlockCipher2.getUpdateOutputSize(i);
                }
            }
        }
        byte[] bArr = this.f;
        if (bArr == null || bArr.length < i) {
            this.f = new byte[i];
        }
    }

    private void d() throws java.io.IOException {
        int iDoFinal;
        try {
            this.j = true;
            a(0, true);
            org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher = this.c;
            if (bufferedBlockCipher != null) {
                iDoFinal = bufferedBlockCipher.a(this.f, 0);
            } else {
                org.bouncycastle.crypto.modes.AEADBlockCipher aEADBlockCipher = this.e;
                if (aEADBlockCipher == null) {
                    this.i = 0;
                    return;
                }
                iDoFinal = aEADBlockCipher.doFinal(this.f, 0);
            }
            this.i = iDoFinal;
        } catch (org.bouncycastle.crypto.InvalidCipherTextException e) {
            throw new org.bouncycastle.crypto.io.InvalidCipherTextIOException("Error finalising cipher", e);
        } catch (java.lang.Exception e2) {
            throw new java.io.IOException("Error finalising cipher " + e2);
        }
    }

    private int e() throws java.io.IOException {
        int iH;
        if (this.j) {
            return -1;
        }
        this.h = 0;
        this.i = 0;
        while (true) {
            int i = this.i;
            if (i != 0) {
                return i;
            }
            int i2 = ((java.io.FilterInputStream) this).in.read(this.b);
            if (i2 == -1) {
                d();
                int i3 = this.i;
                if (i3 == 0) {
                    return -1;
                }
                return i3;
            }
            try {
                a(i2, false);
                org.bouncycastle.crypto.BufferedBlockCipher bufferedBlockCipher = this.c;
                if (bufferedBlockCipher != null) {
                    iH = bufferedBlockCipher.h(this.b, 0, i2, this.f, 0);
                } else {
                    org.bouncycastle.crypto.modes.AEADBlockCipher aEADBlockCipher = this.e;
                    if (aEADBlockCipher != null) {
                        iH = aEADBlockCipher.processBytes(this.b, 0, i2, this.f, 0);
                    } else {
                        this.d.processBytes(this.b, 0, i2, this.f, 0);
                        this.i = i2;
                    }
                }
                this.i = iH;
            } catch (java.lang.Exception e) {
                throw new org.bouncycastle.crypto.io.CipherIOException("Error processing stream ", e);
            }
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int available() {
        return this.i - this.h;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream, java.io.Closeable, java.lang.AutoCloseable
    public void close() throws java.io.IOException {
        try {
            ((java.io.FilterInputStream) this).in.close();
            if (!this.j) {
                d();
            }
            this.h = 0;
            this.i = 0;
            this.l = 0;
            this.k = 0L;
            byte[] bArr = this.g;
            if (bArr != null) {
                org.bouncycastle.util.Arrays.G(bArr, (byte) 0);
                this.g = null;
            }
            byte[] bArr2 = this.f;
            if (bArr2 != null) {
                org.bouncycastle.util.Arrays.G(bArr2, (byte) 0);
                this.f = null;
            }
            org.bouncycastle.util.Arrays.G(this.b, (byte) 0);
        } catch (java.lang.Throwable th) {
            if (!this.j) {
                d();
            }
            throw th;
        }
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public void mark(int i) {
        ((java.io.FilterInputStream) this).in.mark(i);
        org.bouncycastle.crypto.SkippingCipher skippingCipher = this.a;
        if (skippingCipher != null) {
            this.k = skippingCipher.getPosition();
        }
        byte[] bArr = this.f;
        if (bArr != null) {
            byte[] bArr2 = new byte[bArr.length];
            this.g = bArr2;
            java.lang.System.arraycopy(bArr, 0, bArr2, 0, bArr.length);
        }
        this.l = this.h;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public boolean markSupported() {
        if (this.a != null) {
            return ((java.io.FilterInputStream) this).in.markSupported();
        }
        return false;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read() {
        if (this.h >= this.i && e() < 0) {
            return -1;
        }
        byte[] bArr = this.f;
        int i = this.h;
        this.h = i + 1;
        return bArr[i] & 255;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public void reset() throws java.io.IOException {
        if (this.a == null) {
            throw new java.io.IOException("cipher must implement SkippingCipher to be used with reset()");
        }
        ((java.io.FilterInputStream) this).in.reset();
        this.a.seekTo(this.k);
        byte[] bArr = this.g;
        if (bArr != null) {
            this.f = bArr;
        }
        this.h = this.l;
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public long skip(long j) throws java.io.IOException {
        if (j <= 0) {
            return 0L;
        }
        if (this.a == null) {
            int iMin = (int) java.lang.Math.min(j, available());
            this.h += iMin;
            return iMin;
        }
        long jAvailable = available();
        if (j <= jAvailable) {
            this.h = (int) (((long) this.h) + j);
            return j;
        }
        this.h = this.i;
        long jSkip = ((java.io.FilterInputStream) this).in.skip(j - jAvailable);
        if (jSkip == this.a.skip(jSkip)) {
            return jSkip + jAvailable;
        }
        throw new java.io.IOException("Unable to skip cipher " + jSkip + " bytes.");
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read(byte[] bArr) {
        return read(bArr, 0, bArr.length);
    }

    @Override // java.io.FilterInputStream, java.io.InputStream
    public int read(byte[] bArr, int i, int i2) {
        if (this.h >= this.i && e() < 0) {
            return -1;
        }
        int iMin = java.lang.Math.min(i2, available());
        java.lang.System.arraycopy(this.f, this.h, bArr, i, iMin);
        this.h += iMin;
        return iMin;
    }
}
