package org.bouncycastle.apache.bzip2;

import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.Integers;

/* JADX INFO: loaded from: classes3.dex */
public class CBZip2InputStream extends InputStream implements BZip2Constants {
    private static final int NO_RAND_PART_B_STATE = 3;
    private static final int NO_RAND_PART_C_STATE = 4;
    private static final int RAND_PART_B_STATE = 1;
    private static final int RAND_PART_C_STATE = 2;
    private int blockSize100k;
    private InputStream bsStream;
    int ch2;
    int chPrev;
    int count;
    private int expectedBlockCRC;
    private int expectedStreamCRC;
    int i;
    int i2;
    int j2;
    private int last;
    private byte[] ll8;
    private int nInUse;
    private int origPtr;
    private int streamCRC;
    int tPos;
    private int[] tt;
    int z;
    private final CRC blockCRC = new CRC();
    private byte[] seqToUnseq = new byte[256];
    private byte[] selectors = new byte[BZip2Constants.MAX_SELECTORS];
    private int[] unzftab = new int[256];
    private int[][] limit = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, 6, 21);
    private int[][] base = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, 6, 21);
    private int[][] perm = (int[][]) Array.newInstance((Class<?>) Integer.TYPE, 6, BZip2Constants.MAX_ALPHA_SIZE);
    private int[] minLens = new int[6];
    private boolean streamEnd = false;
    private int currentByte = -1;
    private int currentState = 0;
    int rNToGo = 0;
    int rTPos = 0;
    private int bsLive = 0;
    private int bsBuff = 0;

    public CBZip2InputStream(InputStream inputStream) throws IOException {
        this.ll8 = null;
        this.tt = null;
        this.bsStream = inputStream;
        int i = this.bsStream.read();
        int i2 = this.bsStream.read();
        int i3 = this.bsStream.read();
        int i4 = this.bsStream.read();
        if (i4 < 0) {
            throw new EOFException();
        }
        if (((i != 66) | (i2 != 90) | (i3 != 104) | (i4 < 49)) || (i4 > 57)) {
            throw new IOException("Invalid stream header");
        }
        this.blockSize100k = i4 - 48;
        int i5 = this.blockSize100k * BZip2Constants.baseBlockSize;
        this.ll8 = new byte[i5];
        this.tt = new int[i5];
        this.streamCRC = 0;
        beginBlock();
    }

    private void beginBlock() throws IOException {
        long jBsGetLong48 = bsGetLong48();
        if (jBsGetLong48 != 54156738319193L) {
            if (jBsGetLong48 != 25779555029136L) {
                throw new IOException("Block header error");
            }
            this.expectedStreamCRC = bsGetInt32();
            if (this.expectedStreamCRC != this.streamCRC) {
                throw new IOException("Stream CRC error");
            }
            this.streamEnd = true;
            return;
        }
        this.expectedBlockCRC = bsGetInt32();
        boolean z = bsGetBit() == 1;
        getAndMoveToFrontDecode();
        this.blockCRC.initialise();
        int[] iArr = new int[257];
        iArr[0] = 0;
        this.i = 0;
        int i = 0;
        while (this.i < 256) {
            i += this.unzftab[this.i];
            iArr[this.i + 1] = i;
            this.i++;
        }
        if (i != this.last + 1) {
            throw new IllegalStateException();
        }
        this.i = 0;
        while (this.i <= this.last) {
            int i2 = this.ll8[this.i] & 255;
            int[] iArr2 = this.tt;
            int i3 = iArr[i2];
            iArr[i2] = i3 + 1;
            iArr2[i3] = this.i;
            this.i++;
        }
        this.tPos = this.tt[this.origPtr];
        this.count = 0;
        this.i2 = 0;
        this.ch2 = 256;
        if (!z) {
            setupNoRandPartA();
            return;
        }
        this.rNToGo = 0;
        this.rTPos = 0;
        setupRandPartA();
    }

    private int bsGetBit() throws IOException {
        if (this.bsLive != 0) {
            this.bsLive--;
            return (this.bsBuff >>> this.bsLive) & 1;
        }
        this.bsBuff = requireByte();
        this.bsLive = 7;
        return this.bsBuff >>> 7;
    }

    private int bsGetBits(int i) throws IOException {
        while (this.bsLive < i) {
            this.bsBuff = (this.bsBuff << 8) | requireByte();
            this.bsLive += 8;
        }
        this.bsLive -= i;
        return ((1 << i) - 1) & (this.bsBuff >>> this.bsLive);
    }

    private int bsGetBitsSmall(int i) throws IOException {
        if (this.bsLive < i) {
            this.bsBuff = (this.bsBuff << 8) | requireByte();
            this.bsLive += 8;
        }
        this.bsLive -= i;
        return ((1 << i) - 1) & (this.bsBuff >>> this.bsLive);
    }

    private int bsGetInt32() throws IOException {
        return bsGetBits(16) | (bsGetBits(16) << 16);
    }

    private long bsGetLong48() throws IOException {
        return (((long) bsGetBits(24)) << 24) | ((long) bsGetBits(24));
    }

    private void endBlock() throws IOException {
        int i = this.blockCRC.getFinal();
        if (this.expectedBlockCRC != i) {
            throw new IOException("Block CRC error");
        }
        this.streamCRC = i ^ Integers.rotateLeft(this.streamCRC, 1);
    }

    private void getAndMoveToFrontDecode() throws IOException {
        int i;
        int i2 = this.blockSize100k * BZip2Constants.baseBlockSize;
        this.origPtr = bsGetBits(24);
        if (this.origPtr > i2 + 10) {
            throw new IllegalStateException();
        }
        int iRecvDecodingTables = recvDecodingTables();
        int i3 = this.nInUse + 2;
        int i4 = 1;
        int i5 = this.nInUse + 1;
        int i6 = 0;
        for (int i7 = 0; i7 <= 255; i7++) {
            this.unzftab[i7] = 0;
        }
        byte[] bArr = new byte[this.nInUse];
        for (int i8 = 0; i8 < this.nInUse; i8++) {
            bArr[i8] = this.seqToUnseq[i8];
        }
        this.last = -1;
        int i9 = this.selectors[0] & 255;
        int i10 = this.minLens[i9];
        int[] iArr = this.limit[i9];
        int[] iArr2 = this.perm[i9];
        int[] iArr3 = this.base[i9];
        int iBsGetBits = bsGetBits(i10);
        int i11 = i10;
        while (true) {
            int i12 = i6;
            if (iBsGetBits < iArr[i11]) {
                int i13 = iBsGetBits - iArr3[i11];
                if (i13 >= i3) {
                    throw new IllegalStateException();
                }
                int i14 = iArr2[i13];
                int i15 = 49;
                int i16 = i12;
                while (i14 != i5) {
                    if (i14 <= i4) {
                        int i17 = i12;
                        while (true) {
                            if (i4 > 1048576) {
                                throw new IllegalStateException();
                            }
                            i17 += i4 << i14;
                            i4 <<= 1;
                            if (i15 == 0) {
                                i16++;
                                if (i16 >= iRecvDecodingTables) {
                                    throw new IllegalStateException();
                                }
                                int i18 = this.selectors[i16] & 255;
                                int i19 = this.minLens[i18];
                                int[] iArr4 = this.limit[i18];
                                iArr = iArr4;
                                iArr2 = this.perm[i18];
                                i15 = 50;
                                iArr3 = this.base[i18];
                                i10 = i19;
                            }
                            i15--;
                            int iBsGetBits2 = bsGetBits(i10);
                            int i20 = i5;
                            int i21 = i10;
                            while (iBsGetBits2 >= iArr[i21]) {
                                i21++;
                                if (i21 > 20) {
                                    throw new IllegalStateException();
                                }
                                iBsGetBits2 = bsGetBit() | (iBsGetBits2 << 1);
                            }
                            int i22 = iBsGetBits2 - iArr3[i21];
                            if (i22 >= i3) {
                                throw new IllegalStateException();
                            }
                            i14 = iArr2[i22];
                            if (i14 > 1) {
                                byte b = bArr[i12];
                                int[] iArr5 = this.unzftab;
                                int i23 = b & 255;
                                iArr5[i23] = iArr5[i23] + i17;
                                if (this.last >= i2 - i17) {
                                    throw new IllegalStateException("Block overrun");
                                }
                                while (true) {
                                    i17--;
                                    if (i17 < 0) {
                                        break;
                                    }
                                    byte[] bArr2 = this.ll8;
                                    int i24 = this.last + 1;
                                    this.last = i24;
                                    bArr2[i24] = b;
                                }
                                i5 = i20;
                                i4 = 1;
                                break;
                            }
                            i5 = i20;
                        }
                    } else {
                        int i25 = i5;
                        int i26 = this.last + 1;
                        this.last = i26;
                        if (i26 >= i2) {
                            throw new IllegalStateException("Block overrun");
                        }
                        int i27 = i14 - 1;
                        byte b2 = bArr[i27];
                        int[] iArr6 = this.unzftab;
                        int i28 = b2 & 255;
                        iArr6[i28] = iArr6[i28] + 1;
                        this.ll8[this.last] = b2;
                        if (i14 <= 16) {
                            while (i27 > 0) {
                                bArr[i27] = bArr[i27 - 1];
                                i27--;
                            }
                            i = i12;
                        } else {
                            i = i12;
                            System.arraycopy(bArr, i, bArr, 1, i27);
                        }
                        bArr[i] = b2;
                        if (i15 == 0) {
                            i16++;
                            if (i16 >= iRecvDecodingTables) {
                                throw new IllegalStateException();
                            }
                            int i29 = this.selectors[i16] & 255;
                            int i30 = this.minLens[i29];
                            int[] iArr7 = this.limit[i29];
                            int[] iArr8 = this.perm[i29];
                            iArr3 = this.base[i29];
                            i10 = i30;
                            iArr = iArr7;
                            iArr2 = iArr8;
                            i15 = 50;
                        }
                        i15--;
                        int iBsGetBits3 = bsGetBits(i10);
                        int i31 = i10;
                        while (iBsGetBits3 >= iArr[i31]) {
                            i31++;
                            if (i31 > 20) {
                                throw new IllegalStateException();
                            }
                            iBsGetBits3 = (iBsGetBits3 << 1) | bsGetBit();
                        }
                        int i32 = iBsGetBits3 - iArr3[i31];
                        if (i32 >= i3) {
                            throw new IllegalStateException();
                        }
                        i14 = iArr2[i32];
                        i12 = i;
                        i5 = i25;
                        i4 = 1;
                    }
                }
                int i33 = i12;
                if (this.origPtr > this.last) {
                    throw new IllegalStateException();
                }
                int i34 = this.last + 1;
                int i35 = i33;
                while (i33 <= 255) {
                    int i36 = this.unzftab[i33];
                    i35 = i35 | i36 | (i34 - i36);
                    i33++;
                }
                if (i35 < 0) {
                    throw new IllegalStateException();
                }
                return;
            }
            i11++;
            if (i11 > 20) {
                throw new IllegalStateException();
            }
            iBsGetBits = (iBsGetBits << 1) | bsGetBit();
            i6 = i12;
        }
    }

    private void hbCreateDecodeTables(int[] iArr, int[] iArr2, int[] iArr3, byte[] bArr, int i, int i2, int i3) {
        Arrays.fill(iArr2, 0);
        Arrays.fill(iArr, 0);
        int i4 = 0;
        int i5 = 0;
        while (i <= i2) {
            for (int i6 = 0; i6 < i3; i6++) {
                if ((bArr[i6] & 255) == i) {
                    iArr3[i4] = i6;
                    i4++;
                }
            }
            iArr2[i] = i5;
            int i7 = i5 + i4;
            iArr[i] = i7;
            i5 += i7;
            i++;
        }
    }

    private void implClose(boolean z) throws IOException {
        if (this.bsStream != null) {
            if (z) {
                this.bsStream.close();
            }
            this.bsStream = null;
        }
    }

    private int recvDecodingTables() throws IOException {
        int i;
        int i2;
        this.nInUse = 0;
        int iBsGetBits = bsGetBits(16);
        for (int i3 = 0; i3 < 16; i3++) {
            if (((32768 >>> i3) & iBsGetBits) != 0) {
                int iBsGetBits2 = bsGetBits(16);
                int i4 = i3 * 16;
                for (int i5 = 0; i5 < 16; i5++) {
                    if (((32768 >>> i5) & iBsGetBits2) != 0) {
                        byte[] bArr = this.seqToUnseq;
                        int i6 = this.nInUse;
                        this.nInUse = i6 + 1;
                        bArr[i6] = (byte) (i4 + i5);
                    }
                }
            }
        }
        if (this.nInUse < 1) {
            throw new IllegalStateException();
        }
        int i7 = this.nInUse + 2;
        int iBsGetBitsSmall = bsGetBitsSmall(3);
        if (iBsGetBitsSmall < 2 || iBsGetBitsSmall > 6) {
            throw new IllegalStateException();
        }
        int iBsGetBits3 = bsGetBits(15);
        if (iBsGetBits3 < 1) {
            throw new IllegalStateException();
        }
        int i8 = 5517840;
        for (int i9 = 0; i9 < iBsGetBits3; i9++) {
            int i10 = 0;
            while (bsGetBit() == 1) {
                i10++;
                if (i10 >= iBsGetBitsSmall) {
                    throw new IllegalStateException();
                }
            }
            if (i9 < 18002) {
                switch (i10) {
                    case 0:
                        this.selectors[i9] = (byte) (i8 & 15);
                        continue;
                    case 1:
                        i = ((i8 >>> 4) & 15) | ((i8 << 4) & 240);
                        i2 = 16776960;
                        break;
                    case 2:
                        i = ((i8 >>> 8) & 15) | ((i8 << 4) & 4080);
                        i2 = 16773120;
                        break;
                    case 3:
                        i = ((i8 >>> 12) & 15) | ((i8 << 4) & 65520);
                        i2 = 16711680;
                        break;
                    case 4:
                        i = ((i8 >>> 16) & 15) | ((i8 << 4) & 1048560);
                        i2 = 15728640;
                        break;
                    case 5:
                        i = (i8 >>> 20) & 15;
                        i8 <<= 4;
                        i2 = 16777200;
                        break;
                    default:
                        throw new IllegalStateException();
                }
                i8 = (i8 & i2) | i;
                this.selectors[i9] = (byte) (i8 & 15);
                continue;
            }
        }
        byte[] bArr2 = new byte[i7];
        for (int i11 = 0; i11 < iBsGetBitsSmall; i11++) {
            int iBsGetBitsSmall2 = bsGetBitsSmall(5);
            if ((iBsGetBitsSmall2 < 1) || (iBsGetBitsSmall2 > 20)) {
                throw new IllegalStateException();
            }
            int iMin = 32;
            int iMax = 0;
            for (int i12 = 0; i12 < i7; i12++) {
                int iBsGetBit = bsGetBit();
                while (iBsGetBit != 0) {
                    int iBsGetBitsSmall3 = bsGetBitsSmall(2);
                    iBsGetBitsSmall2 += 1 - (iBsGetBitsSmall3 & 2);
                    if ((iBsGetBitsSmall2 < 1) || (iBsGetBitsSmall2 > 20)) {
                        throw new IllegalStateException();
                    }
                    iBsGetBit = iBsGetBitsSmall3 & 1;
                }
                bArr2[i12] = (byte) iBsGetBitsSmall2;
                iMax = Math.max(iMax, iBsGetBitsSmall2);
                iMin = Math.min(iMin, iBsGetBitsSmall2);
            }
            hbCreateDecodeTables(this.limit[i11], this.base[i11], this.perm[i11], bArr2, iMin, iMax, i7);
            this.minLens[i11] = iMin;
        }
        return iBsGetBits3;
    }

    private int requireByte() throws IOException {
        int i = this.bsStream.read();
        if (i >= 0) {
            return i & 255;
        }
        throw new EOFException();
    }

    private void setupNoRandPartA() throws IOException {
        if (this.i2 > this.last) {
            endBlock();
            beginBlock();
            return;
        }
        this.chPrev = this.ch2;
        this.ch2 = this.ll8[this.tPos] & 255;
        this.tPos = this.tt[this.tPos];
        this.i2++;
        this.currentByte = this.ch2;
        this.currentState = 3;
        this.blockCRC.update(this.ch2);
    }

    private void setupNoRandPartB() throws IOException {
        if (this.ch2 != this.chPrev) {
            this.count = 1;
        } else {
            int i = this.count + 1;
            this.count = i;
            if (i >= 4) {
                this.z = this.ll8[this.tPos] & 255;
                this.tPos = this.tt[this.tPos];
                this.currentState = 4;
                this.j2 = 0;
                setupNoRandPartC();
                return;
            }
        }
        setupNoRandPartA();
    }

    private void setupNoRandPartC() throws IOException {
        if (this.j2 < this.z) {
            this.currentByte = this.ch2;
            this.blockCRC.update(this.ch2);
            this.j2++;
        } else {
            this.i2++;
            this.count = 0;
            setupNoRandPartA();
        }
    }

    private void setupRandPartA() throws IOException {
        if (this.i2 > this.last) {
            endBlock();
            beginBlock();
            return;
        }
        this.chPrev = this.ch2;
        this.ch2 = this.ll8[this.tPos] & 255;
        this.tPos = this.tt[this.tPos];
        if (this.rNToGo == 0) {
            short[] sArr = CBZip2OutputStream.R_NUMS;
            int i = this.rTPos;
            this.rTPos = i + 1;
            this.rNToGo = sArr[i];
            this.rTPos &= 511;
        }
        this.rNToGo--;
        this.ch2 ^= this.rNToGo == 1 ? 1 : 0;
        this.i2++;
        this.currentByte = this.ch2;
        this.currentState = 1;
        this.blockCRC.update(this.ch2);
    }

    private void setupRandPartB() throws IOException {
        if (this.ch2 != this.chPrev) {
            this.count = 1;
        } else {
            int i = this.count + 1;
            this.count = i;
            if (i >= 4) {
                this.z = this.ll8[this.tPos] & 255;
                this.tPos = this.tt[this.tPos];
                if (this.rNToGo == 0) {
                    short[] sArr = CBZip2OutputStream.R_NUMS;
                    int i2 = this.rTPos;
                    this.rTPos = i2 + 1;
                    this.rNToGo = sArr[i2];
                    this.rTPos &= 511;
                }
                this.rNToGo--;
                this.z ^= this.rNToGo != 1 ? 0 : 1;
                this.j2 = 0;
                this.currentState = 2;
                setupRandPartC();
                return;
            }
        }
        setupRandPartA();
    }

    private void setupRandPartC() throws IOException {
        if (this.j2 < this.z) {
            this.currentByte = this.ch2;
            this.blockCRC.update(this.ch2);
            this.j2++;
        } else {
            this.i2++;
            this.count = 0;
            setupRandPartA();
        }
    }

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

    @Override // java.io.InputStream
    public int read() throws IOException {
        if (this.streamEnd) {
            return -1;
        }
        int i = this.currentByte;
        switch (this.currentState) {
            case 1:
                setupRandPartB();
                return i;
            case 2:
                setupRandPartC();
                return i;
            case 3:
                setupNoRandPartB();
                return i;
            case 4:
                setupNoRandPartC();
                return i;
            default:
                throw new IllegalStateException();
        }
    }
}
