package io.netty.handler.codec.compression;

import com.seewo.sdk.model.SDKKeyboardCode;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import java.util.List;
import java.util.zip.Checksum;
import net.jpountz.lz4.LZ4Exception;
import net.jpountz.lz4.LZ4Factory;
import net.jpountz.lz4.LZ4FastDecompressor;
import net.jpountz.xxhash.XXHashFactory;

/* JADX INFO: loaded from: classes.dex */
public class Lz4FrameDecoder extends ByteToMessageDecoder {
    private int blockType;
    private Checksum checksum;
    private int compressedLength;
    private int currentChecksum;
    private State currentState;
    private int decompressedLength;
    private LZ4FastDecompressor decompressor;

    /* JADX INFO: renamed from: io.netty.handler.codec.compression.Lz4FrameDecoder$1, reason: invalid class name */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State;

        static {
            int[] iArr = new int[State.values().length];
            $SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State = iArr;
            try {
                iArr[State.INIT_BLOCK.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State[State.DECOMPRESS_DATA.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State[State.FINISHED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State[State.CORRUPTED.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
        }
    }

    private enum State {
        INIT_BLOCK,
        DECOMPRESS_DATA,
        FINISHED,
        CORRUPTED
    }

    public Lz4FrameDecoder() {
        this(false);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference failed for: r11v1, types: [int] */
    /* JADX WARN: Type inference failed for: r11v2, types: [io.netty.util.ReferenceCounted] */
    /* JADX WARN: Type inference failed for: r11v5 */
    /* JADX WARN: Type inference failed for: r11v6 */
    /* JADX WARN: Type inference failed for: r2v17 */
    /* JADX WARN: Type inference failed for: r2v18, types: [int] */
    /* JADX WARN: Type inference failed for: r2v29 */
    /* JADX WARN: Type inference failed for: r2v40 */
    /* JADX WARN: Type inference failed for: r2v41 */
    /* JADX WARN: Type inference failed for: r4v7, types: [java.util.zip.Checksum] */
    @Override // io.netty.handler.codec.ByteToMessageDecoder
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        byte[] bArr;
        ByteBuf byteBuf2;
        int i2;
        ?? r2;
        byte[] bArr2;
        int iArrayOffset;
        ByteBuf byteBuf3;
        try {
            int i3 = AnonymousClass1.$SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State[this.currentState.ordinal()];
            if (i3 != 1) {
                if (i3 != 2) {
                    if (i3 != 3 && i3 != 4) {
                        throw new IllegalStateException();
                    }
                    byteBuf.skipBytes(byteBuf.readableBytes());
                    return;
                }
            } else {
                if (byteBuf.readableBytes() < 21) {
                    return;
                }
                if (byteBuf.readLong() != 5501767354678207339L) {
                    throw new DecompressionException("unexpected block identifier");
                }
                byte b = byteBuf.readByte();
                int i4 = (b & SDKKeyboardCode.FUNCTION_KEY_BOARD_L) + 10;
                int i5 = b & 240;
                int iReverseBytes = Integer.reverseBytes(byteBuf.readInt());
                if (iReverseBytes < 0 || iReverseBytes > 33554432) {
                    throw new DecompressionException(String.format("invalid compressedLength: %d (expected: 0-%d)", Integer.valueOf(iReverseBytes), 33554432));
                }
                int iReverseBytes2 = Integer.reverseBytes(byteBuf.readInt());
                int i6 = 1 << i4;
                if (iReverseBytes2 < 0 || iReverseBytes2 > i6) {
                    throw new DecompressionException(String.format("invalid decompressedLength: %d (expected: 0-%d)", Integer.valueOf(iReverseBytes2), Integer.valueOf(i6)));
                }
                if ((iReverseBytes2 == 0 && iReverseBytes != 0) || ((iReverseBytes2 != 0 && iReverseBytes == 0) || (i5 == 16 && iReverseBytes2 != iReverseBytes))) {
                    throw new DecompressionException(String.format("stream corrupted: compressedLength(%d) and decompressedLength(%d) mismatch", Integer.valueOf(iReverseBytes), Integer.valueOf(iReverseBytes2)));
                }
                int iReverseBytes3 = Integer.reverseBytes(byteBuf.readInt());
                if (iReverseBytes2 == 0 && iReverseBytes == 0) {
                    if (iReverseBytes3 != 0) {
                        throw new DecompressionException("stream corrupted: checksum error");
                    }
                    this.currentState = State.FINISHED;
                    this.decompressor = null;
                    this.checksum = null;
                    return;
                }
                this.blockType = i5;
                this.compressedLength = iReverseBytes;
                this.decompressedLength = iReverseBytes2;
                this.currentChecksum = iReverseBytes3;
                this.currentState = State.DECOMPRESS_DATA;
            }
            int i7 = this.blockType;
            int i8 = this.compressedLength;
            int i9 = this.decompressedLength;
            int i10 = this.currentChecksum;
            if (byteBuf.readableBytes() < i8) {
                return;
            }
            int i11 = byteBuf.readerIndex();
            ByteBuf byteBufHeapBuffer = channelHandlerContext.alloc().heapBuffer(i9, i9);
            byte[] bArrArray = byteBufHeapBuffer.array();
            ?? WriterIndex = byteBufHeapBuffer.writerIndex() + byteBufHeapBuffer.arrayOffset();
            try {
            } catch (Throwable th) {
                th = th;
            }
            if (i7 != 16) {
                if (i7 != 32) {
                    throw new DecompressionException(String.format("unexpected blockType: %d (expected: %d or %d)", Integer.valueOf(i7), 16, 32));
                }
                try {
                    if (byteBuf.hasArray()) {
                        byte[] bArrArray2 = byteBuf.array();
                        iArrayOffset = byteBuf.arrayOffset() + i11;
                        bArr2 = bArrArray2;
                    } else {
                        byte[] bArr3 = new byte[i8];
                        byteBuf.getBytes(i11, bArr3);
                        bArr2 = bArr3;
                        iArrayOffset = 0;
                    }
                    try {
                        ?? r22 = WriterIndex;
                        int i12 = iArrayOffset;
                        bArr = bArrArray;
                        byteBuf3 = byteBufHeapBuffer;
                        i2 = i10;
                        try {
                            try {
                                int iDecompress = this.decompressor.decompress(bArr2, i12, bArrArray, r22 == true ? 1 : 0, i9);
                                if (i8 != iDecompress) {
                                    throw new DecompressionException(String.format("stream corrupted: compressedLength(%d) and actual length(%d) mismatch", Integer.valueOf(i8), Integer.valueOf(iDecompress)));
                                }
                                byteBuf2 = byteBuf3;
                                r2 = r22;
                            } catch (LZ4Exception e2) {
                                e = e2;
                                throw new DecompressionException((Throwable) e);
                            }
                        } catch (Throwable th2) {
                            th = th2;
                            WriterIndex = byteBuf3;
                        }
                        th = th2;
                        WriterIndex = byteBuf3;
                    } catch (LZ4Exception e3) {
                        e = e3;
                        byteBuf3 = byteBufHeapBuffer;
                    }
                } catch (Throwable th3) {
                    th = th3;
                    WriterIndex = byteBufHeapBuffer;
                }
                WriterIndex.release();
                throw th;
            }
            ?? r23 = WriterIndex;
            bArr = bArrArray;
            byteBuf2 = byteBufHeapBuffer;
            i2 = i10;
            byteBuf.getBytes(i11, bArr, r23 == true ? 1 : 0, i9);
            r2 = r23;
            ?? r4 = this.checksum;
            if (r4 != 0) {
                r4.reset();
                r4.update(bArr, r2, i9);
                int value = (int) r4.getValue();
                int i13 = i2;
                if (value != i13) {
                    throw new DecompressionException(String.format("stream corrupted: mismatching checksum: %d (expected: %d)", Integer.valueOf(value), Integer.valueOf(i13)));
                }
            }
            byteBuf2.writerIndex(byteBuf2.writerIndex() + i9);
            list.add(byteBuf2);
            byteBuf.skipBytes(i8);
            this.currentState = State.INIT_BLOCK;
        } catch (Exception e4) {
            this.currentState = State.CORRUPTED;
            throw e4;
        }
    }

    public boolean isClosed() {
        return this.currentState == State.FINISHED;
    }

    public Lz4FrameDecoder(boolean z) {
        this(LZ4Factory.fastestInstance(), z);
    }

    public Lz4FrameDecoder(LZ4Factory lZ4Factory, boolean z) {
        this(lZ4Factory, z ? XXHashFactory.fastestInstance().newStreamingHash32(-1756908916).asChecksum() : null);
    }

    public Lz4FrameDecoder(LZ4Factory lZ4Factory, Checksum checksum) {
        this.currentState = State.INIT_BLOCK;
        if (lZ4Factory != null) {
            this.decompressor = lZ4Factory.fastDecompressor();
            this.checksum = checksum;
            return;
        }
        throw new NullPointerException("factory");
    }
}
