package io.netty.handler.codec.compression;

import com.seewo.libmcuservice.constant.IMcuConstants;
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.Arrays;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class SnappyFrameDecoder extends ByteToMessageDecoder {
    private static final int MAX_UNCOMPRESSED_DATA_SIZE = 65540;
    private static final byte[] SNAPPY = {115, SDKKeyboardCode.FUNCTION_KEY_BOARD_PAGE_DOWN, 97, SDKKeyboardCode.FUNCTION_KEY_BOARD_LEFT, 112, IMcuConstants.MCU_COMMAND_FT_THIRD};
    private boolean corrupted;
    private final Snappy snappy;
    private boolean started;
    private final boolean validateChecksums;

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

        static {
            int[] iArr = new int[ChunkType.values().length];
            $SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType = iArr;
            try {
                iArr[ChunkType.STREAM_IDENTIFIER.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType[ChunkType.RESERVED_SKIPPABLE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType[ChunkType.RESERVED_UNSKIPPABLE.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType[ChunkType.UNCOMPRESSED_DATA.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType[ChunkType.COMPRESSED_DATA.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    private enum ChunkType {
        STREAM_IDENTIFIER,
        COMPRESSED_DATA,
        UNCOMPRESSED_DATA,
        RESERVED_UNSKIPPABLE,
        RESERVED_SKIPPABLE
    }

    public SnappyFrameDecoder() {
        this(false);
    }

    private static ChunkType mapChunkType(byte b2) {
        return b2 == 0 ? ChunkType.COMPRESSED_DATA : b2 == 1 ? ChunkType.UNCOMPRESSED_DATA : b2 == -1 ? ChunkType.STREAM_IDENTIFIER : (b2 & 128) == 128 ? ChunkType.RESERVED_SKIPPABLE : ChunkType.RESERVED_UNSKIPPABLE;
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        if (this.corrupted) {
            byteBuf.skipBytes(byteBuf.readableBytes());
            return;
        }
        try {
            int i = byteBuf.readerIndex();
            int i2 = byteBuf.readableBytes();
            if (i2 < 4) {
                return;
            }
            short unsignedByte = byteBuf.getUnsignedByte(i);
            ChunkType chunkTypeMapChunkType = mapChunkType((byte) unsignedByte);
            int unsignedMediumLE = byteBuf.getUnsignedMediumLE(i + 1);
            int i3 = AnonymousClass1.$SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType[chunkTypeMapChunkType.ordinal()];
            if (i3 == 1) {
                byte[] bArr = SNAPPY;
                if (unsignedMediumLE != bArr.length) {
                    throw new DecompressionException("Unexpected length of stream identifier: " + unsignedMediumLE);
                }
                if (i2 < bArr.length + 4) {
                    return;
                }
                byte[] bArr2 = new byte[unsignedMediumLE];
                byteBuf.skipBytes(4).readBytes(bArr2);
                if (!Arrays.equals(bArr2, bArr)) {
                    throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy protocol version?");
                }
                this.started = true;
                return;
            }
            if (i3 == 2) {
                if (!this.started) {
                    throw new DecompressionException("Received RESERVED_SKIPPABLE tag before STREAM_IDENTIFIER");
                }
                int i4 = unsignedMediumLE + 4;
                if (i2 < i4) {
                    return;
                }
                byteBuf.skipBytes(i4);
                return;
            }
            if (i3 == 3) {
                throw new DecompressionException("Found reserved unskippable chunk type: 0x" + Integer.toHexString(unsignedByte));
            }
            if (i3 == 4) {
                if (!this.started) {
                    throw new DecompressionException("Received UNCOMPRESSED_DATA tag before STREAM_IDENTIFIER");
                }
                if (unsignedMediumLE > MAX_UNCOMPRESSED_DATA_SIZE) {
                    throw new DecompressionException("Received UNCOMPRESSED_DATA larger than 65540 bytes");
                }
                if (i2 < unsignedMediumLE + 4) {
                    return;
                }
                byteBuf.skipBytes(4);
                if (this.validateChecksums) {
                    Snappy.validateChecksum(byteBuf.readIntLE(), byteBuf, byteBuf.readerIndex(), unsignedMediumLE - 4);
                } else {
                    byteBuf.skipBytes(4);
                }
                list.add(byteBuf.readRetainedSlice(unsignedMediumLE - 4));
                return;
            }
            if (i3 != 5) {
                return;
            }
            if (!this.started) {
                throw new DecompressionException("Received COMPRESSED_DATA tag before STREAM_IDENTIFIER");
            }
            if (i2 < unsignedMediumLE + 4) {
                return;
            }
            byteBuf.skipBytes(4);
            int intLE = byteBuf.readIntLE();
            ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer(0);
            if (this.validateChecksums) {
                int iWriterIndex = byteBuf.writerIndex();
                try {
                    byteBuf.writerIndex((byteBuf.readerIndex() + unsignedMediumLE) - 4);
                    this.snappy.decode(byteBuf, byteBufBuffer);
                    byteBuf.writerIndex(iWriterIndex);
                    Snappy.validateChecksum(intLE, byteBufBuffer, 0, byteBufBuffer.writerIndex());
                } catch (Throwable th) {
                    byteBuf.writerIndex(iWriterIndex);
                    throw th;
                }
            } else {
                this.snappy.decode(byteBuf.readSlice(unsignedMediumLE - 4), byteBufBuffer);
            }
            list.add(byteBufBuffer);
            this.snappy.reset();
        } catch (Exception e2) {
            this.corrupted = true;
            throw e2;
        }
    }

    public SnappyFrameDecoder(boolean z) {
        this.snappy = new Snappy();
        this.validateChecksums = z;
    }
}
