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.DefaultBinaryMemcacheRequest;
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 int SNAPPY_IDENTIFIER_LEN = 6;
    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 */
    public static /* synthetic */ class AnonymousClass1 {
        public static final /* synthetic */ int[] $SwitchMap$io$netty$handler$codec$compression$SnappyFrameDecoder$ChunkType;

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

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

    public SnappyFrameDecoder() {
        this(false);
    }

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

    private static void checkByte(byte b2, byte b3) {
        if (b2 != b3) {
            throw new DecompressionException("Unexpected stream identifier contents. Mismatched snappy protocol version?");
        }
    }

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

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        if (this.corrupted) {
            byteBuf.skipBytes(byteBuf.readableBytes());
            return;
        }
        try {
            int i2 = byteBuf.readerIndex();
            int i3 = byteBuf.readableBytes();
            if (i3 < 4) {
                return;
            }
            short unsignedByte = byteBuf.getUnsignedByte(i2);
            ChunkType chunkTypeMapChunkType = mapChunkType((byte) unsignedByte);
            int unsignedMediumLE = byteBuf.getUnsignedMediumLE(i2 + 1);
            int iOrdinal = chunkTypeMapChunkType.ordinal();
            if (iOrdinal == 0) {
                if (unsignedMediumLE != 6) {
                    throw new DecompressionException("Unexpected length of stream identifier: " + unsignedMediumLE);
                }
                if (i3 < 10) {
                    return;
                }
                byteBuf.skipBytes(4);
                int i4 = byteBuf.readerIndex();
                byteBuf.skipBytes(6);
                int i5 = i4 + 1;
                checkByte(byteBuf.getByte(i4), (byte) 115);
                int i6 = i5 + 1;
                checkByte(byteBuf.getByte(i5), (byte) 78);
                int i7 = i6 + 1;
                checkByte(byteBuf.getByte(i6), (byte) 97);
                int i8 = i7 + 1;
                checkByte(byteBuf.getByte(i7), (byte) 80);
                checkByte(byteBuf.getByte(i8), (byte) 112);
                checkByte(byteBuf.getByte(i8 + 1), (byte) 89);
                this.started = true;
                return;
            }
            if (iOrdinal == 1) {
                if (!this.started) {
                    throw new DecompressionException("Received COMPRESSED_DATA tag before STREAM_IDENTIFIER");
                }
                if (i3 < unsignedMediumLE + 4) {
                    return;
                }
                byteBuf.skipBytes(4);
                int intLE = byteBuf.readIntLE();
                ByteBuf byteBufBuffer = channelHandlerContext.alloc().buffer();
                try {
                    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();
                    return;
                } catch (Throwable th2) {
                    if (byteBufBuffer != null) {
                        byteBufBuffer.release();
                    }
                    throw th2;
                }
            }
            if (iOrdinal == 2) {
                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 (i3 < 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 (iOrdinal == 3) {
                throw new DecompressionException("Found reserved unskippable chunk type: 0x" + Integer.toHexString(unsignedByte));
            }
            if (iOrdinal != 4) {
                return;
            }
            if (!this.started) {
                throw new DecompressionException("Received RESERVED_SKIPPABLE tag before STREAM_IDENTIFIER");
            }
            int i9 = unsignedMediumLE + 4;
            if (i3 < i9) {
                return;
            }
            byteBuf.skipBytes(i9);
        } catch (Exception e2) {
            this.corrupted = true;
            throw e2;
        }
    }
}
