package io.netty.handler.codec.socksx.v5;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.socksx.SocksVersion;
import java.util.List;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class Socks5CommandResponseDecoder extends ReplayingDecoder<State> {
    private final Socks5AddressDecoder addressDecoder;

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

        static {
            State.values();
            int[] iArr = new int[3];
            $SwitchMap$io$netty$handler$codec$socksx$v5$Socks5CommandResponseDecoder$State = iArr;
            try {
                iArr[State.INIT.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$v5$Socks5CommandResponseDecoder$State[State.SUCCESS.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$socksx$v5$Socks5CommandResponseDecoder$State[State.FAILURE.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
        }
    }

    public enum State {
        INIT,
        SUCCESS,
        FAILURE
    }

    public Socks5CommandResponseDecoder() {
        this(Socks5AddressDecoder.DEFAULT);
    }

    public Socks5CommandResponseDecoder(Socks5AddressDecoder socks5AddressDecoder) {
        super(State.INIT);
        Objects.requireNonNull(socks5AddressDecoder, "addressDecoder");
        this.addressDecoder = socks5AddressDecoder;
    }

    private void fail(List<Object> list, Exception exc) {
        if (!(exc instanceof DecoderException)) {
            exc = new DecoderException(exc);
        }
        checkpoint(State.FAILURE);
        DefaultSocks5CommandResponse defaultSocks5CommandResponse = new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, Socks5AddressType.IPv4, null, 0);
        defaultSocks5CommandResponse.setDecoderResult(DecoderResult.failure(exc));
        list.add(defaultSocks5CommandResponse);
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    public void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) {
        try {
            int iOrdinal = state().ordinal();
            if (iOrdinal == 0) {
                byte b2 = byteBuf.readByte();
                SocksVersion socksVersion = SocksVersion.SOCKS5;
                if (b2 != socksVersion.byteValue()) {
                    throw new DecoderException("unsupported version: " + ((int) b2) + " (expected: " + ((int) socksVersion.byteValue()) + ')');
                }
                Socks5CommandStatus socks5CommandStatusValueOf = Socks5CommandStatus.valueOf(byteBuf.readByte());
                byteBuf.skipBytes(1);
                Socks5AddressType socks5AddressTypeValueOf = Socks5AddressType.valueOf(byteBuf.readByte());
                list.add(new DefaultSocks5CommandResponse(socks5CommandStatusValueOf, socks5AddressTypeValueOf, this.addressDecoder.decodeAddress(socks5AddressTypeValueOf, byteBuf), byteBuf.readUnsignedShort()));
                checkpoint(State.SUCCESS);
            } else if (iOrdinal != 1) {
                if (iOrdinal != 2) {
                    return;
                }
                byteBuf.skipBytes(actualReadableBytes());
                return;
            }
            int iActualReadableBytes = actualReadableBytes();
            if (iActualReadableBytes > 0) {
                list.add(byteBuf.readRetainedSlice(iActualReadableBytes));
            }
        } catch (Exception e2) {
            fail(list, e2);
        }
    }
}
