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;

/* JADX INFO: loaded from: classes.dex */
public class Socks5InitialRequestDecoder extends ReplayingDecoder<State> {

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

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

    public enum State {
        INIT,
        SUCCESS,
        FAILURE
    }

    public Socks5InitialRequestDecoder() {
        super(State.INIT);
    }

    private void fail(List<Object> list, Exception exc) {
        if (!(exc instanceof DecoderException)) {
            exc = new DecoderException(exc);
        }
        checkpoint(State.FAILURE);
        DefaultSocks5InitialRequest defaultSocks5InitialRequest = new DefaultSocks5InitialRequest(Socks5AuthMethod.NO_AUTH);
        defaultSocks5InitialRequest.setDecoderResult(DecoderResult.failure(exc));
        list.add(defaultSocks5InitialRequest);
    }

    @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()) + ')');
                }
                int unsignedByte = byteBuf.readUnsignedByte();
                if (actualReadableBytes() < unsignedByte) {
                    return;
                }
                Socks5AuthMethod[] socks5AuthMethodArr = new Socks5AuthMethod[unsignedByte];
                for (int i2 = 0; i2 < unsignedByte; i2++) {
                    socks5AuthMethodArr[i2] = Socks5AuthMethod.valueOf(byteBuf.readByte());
                }
                list.add(new DefaultSocks5InitialRequest(socks5AuthMethodArr));
                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);
        }
    }
}
