package io.netty.handler.codec.redis;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.haproxy.HAProxyConstants;
import io.netty.util.ByteProcessor;
import io.netty.util.CharsetUtil;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class RedisDecoder extends ByteToMessageDecoder {
    private final boolean decodeInlineCommands;
    private final int maxInlineMessageLength;
    private final RedisMessagePool messagePool;
    private int remainingBulkLength;
    private State state;
    private final ToPositiveLongProcessor toPositiveLongProcessor;
    private RedisMessageType type;

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

        static {
            RedisMessageType.values();
            int[] iArr = new int[6];
            $SwitchMap$io$netty$handler$codec$redis$RedisMessageType = iArr;
            try {
                iArr[RedisMessageType.ARRAY_HEADER.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisMessageType[RedisMessageType.BULK_STRING.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisMessageType[RedisMessageType.INLINE_COMMAND.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisMessageType[RedisMessageType.SIMPLE_STRING.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisMessageType[RedisMessageType.ERROR.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisMessageType[RedisMessageType.INTEGER.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            State.values();
            int[] iArr2 = new int[5];
            $SwitchMap$io$netty$handler$codec$redis$RedisDecoder$State = iArr2;
            try {
                iArr2[State.DECODE_TYPE.ordinal()] = 1;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisDecoder$State[State.DECODE_INLINE.ordinal()] = 2;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisDecoder$State[State.DECODE_LENGTH.ordinal()] = 3;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisDecoder$State[State.DECODE_BULK_STRING_EOL.ordinal()] = 4;
            } catch (NoSuchFieldError unused10) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$redis$RedisDecoder$State[State.DECODE_BULK_STRING_CONTENT.ordinal()] = 5;
            } catch (NoSuchFieldError unused11) {
            }
        }
    }

    public enum State {
        DECODE_TYPE,
        DECODE_INLINE,
        DECODE_LENGTH,
        DECODE_BULK_STRING_EOL,
        DECODE_BULK_STRING_CONTENT
    }

    public static final class ToPositiveLongProcessor implements ByteProcessor {
        private long result;

        private ToPositiveLongProcessor() {
        }

        public /* synthetic */ ToPositiveLongProcessor(AnonymousClass1 anonymousClass1) {
            this();
        }

        public long content() {
            return this.result;
        }

        @Override // io.netty.util.ByteProcessor
        public boolean process(byte b2) {
            if (b2 < 48 || b2 > 57) {
                throw new RedisCodecException(a.l("bad byte in number: ", b2));
            }
            this.result = (this.result * 10) + ((long) (b2 - HAProxyConstants.AF_UNIX_BYTE));
            return true;
        }

        public void reset() {
            this.result = 0L;
        }
    }

    public RedisDecoder() {
        this(false);
    }

    public RedisDecoder(int i2, RedisMessagePool redisMessagePool) {
        this(i2, redisMessagePool, false);
    }

    public RedisDecoder(int i2, RedisMessagePool redisMessagePool, boolean z2) {
        this.toPositiveLongProcessor = new ToPositiveLongProcessor(null);
        this.state = State.DECODE_TYPE;
        if (i2 <= 0 || i2 > 536870912) {
            throw new RedisCodecException(a.p("maxInlineMessageLength: ", i2, " (expected: <= ", RedisConstants.REDIS_MESSAGE_MAX_LENGTH, ")"));
        }
        this.maxInlineMessageLength = i2;
        this.messagePool = redisMessagePool;
        this.decodeInlineCommands = z2;
    }

    public RedisDecoder(boolean z2) {
        this(65536, FixedRedisMessagePool.INSTANCE, z2);
    }

    private boolean decodeBulkString(ByteBuf byteBuf, List<Object> list) {
        int i2 = this.remainingBulkLength;
        if (i2 == -1) {
            list.add(FullBulkStringRedisMessage.NULL_INSTANCE);
            resetDecoder();
            return true;
        }
        if (i2 == 0) {
            this.state = State.DECODE_BULK_STRING_EOL;
            return decodeBulkStringEndOfLine(byteBuf, list);
        }
        list.add(new BulkStringHeaderRedisMessage(i2));
        this.state = State.DECODE_BULK_STRING_CONTENT;
        return decodeBulkStringContent(byteBuf, list);
    }

    private boolean decodeBulkStringContent(ByteBuf byteBuf, List<Object> list) {
        int i2 = byteBuf.readableBytes();
        if (i2 == 0) {
            return false;
        }
        int i3 = this.remainingBulkLength;
        if (i3 == 0 && i2 < 2) {
            return false;
        }
        if (i2 < i3 + 2) {
            int iMin = Math.min(i3, i2);
            this.remainingBulkLength -= iMin;
            list.add(new DefaultBulkStringRedisContent(byteBuf.readSlice(iMin).retain()));
            return true;
        }
        ByteBuf slice = byteBuf.readSlice(i3);
        readEndOfLine(byteBuf);
        list.add(new DefaultLastBulkStringRedisContent(slice.retain()));
        resetDecoder();
        return true;
    }

    private boolean decodeBulkStringEndOfLine(ByteBuf byteBuf, List<Object> list) {
        if (byteBuf.readableBytes() < 2) {
            return false;
        }
        readEndOfLine(byteBuf);
        list.add(FullBulkStringRedisMessage.EMPTY_INSTANCE);
        resetDecoder();
        return true;
    }

    private boolean decodeInline(ByteBuf byteBuf, List<Object> list) {
        ByteBuf line = readLine(byteBuf);
        if (line != null) {
            list.add(newInlineRedisMessage(this.type, line));
            resetDecoder();
            return true;
        }
        if (byteBuf.readableBytes() <= this.maxInlineMessageLength) {
            return false;
        }
        StringBuilder sbF = a.F("length: ");
        sbF.append(byteBuf.readableBytes());
        sbF.append(" (expected: <= ");
        throw new RedisCodecException(a.y(sbF, this.maxInlineMessageLength, ")"));
    }

    private boolean decodeLength(ByteBuf byteBuf, List<Object> list) {
        ByteBuf line = readLine(byteBuf);
        if (line == null) {
            return false;
        }
        long redisNumber = parseRedisNumber(line);
        if (redisNumber < -1) {
            throw new RedisCodecException("length: " + redisNumber + " (expected: >= -1)");
        }
        int iOrdinal = this.type.ordinal();
        if (iOrdinal != 4) {
            if (iOrdinal == 5) {
                list.add(new ArrayHeaderRedisMessage(redisNumber));
                resetDecoder();
                return true;
            }
            StringBuilder sbF = a.F("bad type: ");
            sbF.append(this.type);
            throw new RedisCodecException(sbF.toString());
        }
        if (redisNumber <= 536870912) {
            this.remainingBulkLength = (int) redisNumber;
            return decodeBulkString(byteBuf, list);
        }
        throw new RedisCodecException("length: " + redisNumber + " (expected: <= " + RedisConstants.REDIS_MESSAGE_MAX_LENGTH + ")");
    }

    private boolean decodeType(ByteBuf byteBuf) {
        if (!byteBuf.isReadable()) {
            return false;
        }
        RedisMessageType from = RedisMessageType.readFrom(byteBuf, this.decodeInlineCommands);
        this.type = from;
        this.state = from.isInline() ? State.DECODE_INLINE : State.DECODE_LENGTH;
        return true;
    }

    private RedisMessage newInlineRedisMessage(RedisMessageType redisMessageType, ByteBuf byteBuf) {
        int iOrdinal = redisMessageType.ordinal();
        if (iOrdinal == 0) {
            return new InlineCommandRedisMessage(byteBuf.toString(CharsetUtil.UTF_8));
        }
        if (iOrdinal == 1) {
            SimpleStringRedisMessage simpleString = this.messagePool.getSimpleString(byteBuf);
            return simpleString != null ? simpleString : new SimpleStringRedisMessage(byteBuf.toString(CharsetUtil.UTF_8));
        }
        if (iOrdinal == 2) {
            ErrorRedisMessage error = this.messagePool.getError(byteBuf);
            return error != null ? error : new ErrorRedisMessage(byteBuf.toString(CharsetUtil.UTF_8));
        }
        if (iOrdinal == 3) {
            IntegerRedisMessage integer = this.messagePool.getInteger(byteBuf);
            return integer != null ? integer : new IntegerRedisMessage(parseRedisNumber(byteBuf));
        }
        throw new RedisCodecException("bad type: " + redisMessageType);
    }

    private long parsePositiveNumber(ByteBuf byteBuf) {
        this.toPositiveLongProcessor.reset();
        byteBuf.forEachByte(this.toPositiveLongProcessor);
        return this.toPositiveLongProcessor.content();
    }

    private long parseRedisNumber(ByteBuf byteBuf) {
        int i2 = byteBuf.readableBytes();
        int i3 = (i2 <= 0 || byteBuf.getByte(byteBuf.readerIndex()) != 45) ? 0 : 1;
        if (i2 <= i3) {
            StringBuilder sbF = a.F("no number to parse: ");
            sbF.append(byteBuf.toString(CharsetUtil.US_ASCII));
            throw new RedisCodecException(sbF.toString());
        }
        if (i2 <= i3 + 19) {
            return i3 != 0 ? -parsePositiveNumber(byteBuf.skipBytes(i3)) : parsePositiveNumber(byteBuf);
        }
        StringBuilder sbF2 = a.F("too many characters to be a valid RESP Integer: ");
        sbF2.append(byteBuf.toString(CharsetUtil.US_ASCII));
        throw new RedisCodecException(sbF2.toString());
    }

    private static void readEndOfLine(ByteBuf byteBuf) {
        short s2 = byteBuf.readShort();
        if (RedisConstants.EOL_SHORT == s2) {
            return;
        }
        byte[] bArrShortToBytes = RedisCodecUtil.shortToBytes(s2);
        StringBuilder sbF = a.F("delimiter: [");
        sbF.append((int) bArrShortToBytes[0]);
        sbF.append(",");
        throw new RedisCodecException(a.y(sbF, bArrShortToBytes[1], "] (expected: \\r\\n)"));
    }

    private static ByteBuf readLine(ByteBuf byteBuf) {
        int iForEachByte;
        if (!byteBuf.isReadable(2) || (iForEachByte = byteBuf.forEachByte(ByteProcessor.FIND_LF)) < 0) {
            return null;
        }
        ByteBuf slice = byteBuf.readSlice((iForEachByte - byteBuf.readerIndex()) - 1);
        readEndOfLine(byteBuf);
        return slice;
    }

    private void resetDecoder() {
        this.state = State.DECODE_TYPE;
        this.remainingBulkLength = 0;
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        while (true) {
            try {
                int iOrdinal = this.state.ordinal();
                if (iOrdinal != 0) {
                    if (iOrdinal != 1) {
                        if (iOrdinal != 2) {
                            if (iOrdinal != 3) {
                                if (iOrdinal != 4) {
                                    throw new RedisCodecException("Unknown state: " + this.state);
                                }
                                if (!decodeBulkStringContent(byteBuf, list)) {
                                    return;
                                }
                            } else if (!decodeBulkStringEndOfLine(byteBuf, list)) {
                                return;
                            }
                        } else if (!decodeLength(byteBuf, list)) {
                            return;
                        }
                    } else if (!decodeInline(byteBuf, list)) {
                        return;
                    }
                } else if (!decodeType(byteBuf)) {
                    return;
                }
            } catch (RedisCodecException e2) {
                resetDecoder();
                throw e2;
            } catch (Exception e3) {
                resetDecoder();
                throw new RedisCodecException(e3);
            }
        }
    }
}
