package io.netty.handler.codec.compression;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.memcache.binary.BinaryMemcacheOpcodes;
import java.util.List;
import java.util.Objects;
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 ByteBufChecksum 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 */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$compression$Lz4FrameDecoder$State;

        static {
            State.values();
            int[] iArr = new int[4];
            $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) {
            }
        }
    }

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

    public Lz4FrameDecoder() {
        this(false);
    }

    public Lz4FrameDecoder(LZ4Factory lZ4Factory, Checksum checksum) {
        this.currentState = State.INIT_BLOCK;
        Objects.requireNonNull(lZ4Factory, "factory");
        this.decompressor = lZ4Factory.fastDecompressor();
        this.checksum = checksum == null ? null : ByteBufChecksum.wrapChecksum(checksum);
    }

    public Lz4FrameDecoder(LZ4Factory lZ4Factory, boolean z2) {
        this(lZ4Factory, z2 ? XXHashFactory.fastestInstance().newStreamingHash32(Lz4Constants.DEFAULT_SEED).asChecksum() : null);
    }

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

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        ByteBuf byteBufRetainedSlice;
        try {
            int iOrdinal = this.currentState.ordinal();
            ByteBuf byteBuf2 = null;
            if (iOrdinal != 0) {
                if (iOrdinal != 1) {
                    if (iOrdinal != 2 && iOrdinal != 3) {
                        throw new IllegalStateException();
                    }
                    byteBuf.skipBytes(byteBuf.readableBytes());
                    return;
                }
            } else {
                if (byteBuf.readableBytes() < 21) {
                    return;
                }
                if (byteBuf.readLong() != Lz4Constants.MAGIC_NUMBER) {
                    throw new DecompressionException("unexpected block identifier");
                }
                byte b2 = byteBuf.readByte();
                int i2 = (b2 & BinaryMemcacheOpcodes.PREPEND) + 10;
                int i3 = b2 & 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), Integer.valueOf(Lz4Constants.MAX_BLOCK_SIZE)));
                }
                int iReverseBytes2 = Integer.reverseBytes(byteBuf.readInt());
                int i4 = 1 << i2;
                if (iReverseBytes2 < 0 || iReverseBytes2 > i4) {
                    throw new DecompressionException(String.format("invalid decompressedLength: %d (expected: 0-%d)", Integer.valueOf(iReverseBytes2), Integer.valueOf(i4)));
                }
                if ((iReverseBytes2 == 0 && iReverseBytes != 0) || ((iReverseBytes2 != 0 && iReverseBytes == 0) || (i3 == 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 = i3;
                this.compressedLength = iReverseBytes;
                this.decompressedLength = iReverseBytes2;
                this.currentChecksum = iReverseBytes3;
                this.currentState = State.DECOMPRESS_DATA;
            }
            int i5 = this.blockType;
            int i6 = this.compressedLength;
            int i7 = this.decompressedLength;
            int i8 = this.currentChecksum;
            if (byteBuf.readableBytes() < i6) {
                return;
            }
            ByteBufChecksum byteBufChecksum = this.checksum;
            try {
                try {
                    if (i5 == 16) {
                        byteBufRetainedSlice = byteBuf.retainedSlice(byteBuf.readerIndex(), i7);
                    } else {
                        if (i5 != 32) {
                            throw new DecompressionException(String.format("unexpected blockType: %d (expected: %d or %d)", Integer.valueOf(i5), 16, 32));
                        }
                        byteBufRetainedSlice = channelHandlerContext.alloc().buffer(i7, i7);
                        try {
                            this.decompressor.decompress(CompressionUtil.safeNioBuffer(byteBuf), byteBufRetainedSlice.internalNioBuffer(byteBufRetainedSlice.writerIndex(), i7));
                            byteBufRetainedSlice.writerIndex(byteBufRetainedSlice.writerIndex() + i7);
                        } catch (LZ4Exception e2) {
                            e = e2;
                            throw new DecompressionException((Throwable) e);
                        } catch (Throwable th) {
                            th = th;
                            byteBuf2 = byteBufRetainedSlice;
                            if (byteBuf2 != null) {
                                byteBuf2.release();
                            }
                            throw th;
                        }
                    }
                    byteBuf.skipBytes(i6);
                    if (byteBufChecksum != null) {
                        CompressionUtil.checkChecksum(byteBufChecksum, byteBufRetainedSlice, i8);
                    }
                    list.add(byteBufRetainedSlice);
                    this.currentState = State.INIT_BLOCK;
                } catch (LZ4Exception e3) {
                    e = e3;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (Exception e4) {
            this.currentState = State.CORRUPTED;
            throw e4;
        }
    }

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