package io.netty.handler.codec.spdy;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.handler.codec.memcache.binary.DefaultBinaryMemcacheRequest;
import java.util.Objects;

/* JADX INFO: loaded from: classes.dex */
public class SpdyFrameDecoder {
    private final SpdyFrameDecoderDelegate delegate;
    private byte flags;
    private int length;
    private final int maxChunkSize;
    private int numSettings;
    private final int spdyVersion;
    private State state;
    private int streamId;

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

        static {
            State.values();
            int[] iArr = new int[14];
            $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State = iArr;
            try {
                iArr[State.READ_COMMON_HEADER.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_DATA_FRAME.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_SYN_STREAM_FRAME.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_SYN_REPLY_FRAME.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_RST_STREAM_FRAME.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_SETTINGS_FRAME.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_SETTING.ordinal()] = 7;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_PING_FRAME.ordinal()] = 8;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_GOAWAY_FRAME.ordinal()] = 9;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_HEADERS_FRAME.ordinal()] = 10;
            } catch (NoSuchFieldError unused10) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_WINDOW_UPDATE_FRAME.ordinal()] = 11;
            } catch (NoSuchFieldError unused11) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.READ_HEADER_BLOCK.ordinal()] = 12;
            } catch (NoSuchFieldError unused12) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.DISCARD_FRAME.ordinal()] = 13;
            } catch (NoSuchFieldError unused13) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$spdy$SpdyFrameDecoder$State[State.FRAME_ERROR.ordinal()] = 14;
            } catch (NoSuchFieldError unused14) {
            }
        }
    }

    public enum State {
        READ_COMMON_HEADER,
        READ_DATA_FRAME,
        READ_SYN_STREAM_FRAME,
        READ_SYN_REPLY_FRAME,
        READ_RST_STREAM_FRAME,
        READ_SETTINGS_FRAME,
        READ_SETTING,
        READ_PING_FRAME,
        READ_GOAWAY_FRAME,
        READ_HEADERS_FRAME,
        READ_WINDOW_UPDATE_FRAME,
        READ_HEADER_BLOCK,
        DISCARD_FRAME,
        FRAME_ERROR
    }

    public SpdyFrameDecoder(SpdyVersion spdyVersion, SpdyFrameDecoderDelegate spdyFrameDecoderDelegate) {
        this(spdyVersion, spdyFrameDecoderDelegate, 8192);
    }

    public SpdyFrameDecoder(SpdyVersion spdyVersion, SpdyFrameDecoderDelegate spdyFrameDecoderDelegate, int i2) {
        Objects.requireNonNull(spdyVersion, "spdyVersion");
        Objects.requireNonNull(spdyFrameDecoderDelegate, "delegate");
        if (i2 <= 0) {
            throw new IllegalArgumentException(a.l("maxChunkSize must be a positive integer: ", i2));
        }
        this.spdyVersion = spdyVersion.getVersion();
        this.delegate = spdyFrameDecoderDelegate;
        this.maxChunkSize = i2;
        this.state = State.READ_COMMON_HEADER;
    }

    private static State getNextState(int i2, int i3) {
        switch (i2) {
            case 0:
                return State.READ_DATA_FRAME;
            case 1:
                return State.READ_SYN_STREAM_FRAME;
            case 2:
                return State.READ_SYN_REPLY_FRAME;
            case 3:
                return State.READ_RST_STREAM_FRAME;
            case 4:
                return State.READ_SETTINGS_FRAME;
            case 5:
            default:
                return i3 != 0 ? State.DISCARD_FRAME : State.READ_COMMON_HEADER;
            case 6:
                return State.READ_PING_FRAME;
            case 7:
                return State.READ_GOAWAY_FRAME;
            case 8:
                return State.READ_HEADERS_FRAME;
            case 9:
                return State.READ_WINDOW_UPDATE_FRAME;
        }
    }

    private static boolean hasFlag(byte b2, byte b3) {
        return (b2 & b3) != 0;
    }

    private static boolean isValidFrameHeader(int i2, int i3, byte b2, int i4) {
        switch (i3) {
            case 0:
                if (i2 != 0) {
                }
                break;
            case 1:
                if (i4 >= 10) {
                }
                break;
            case 2:
                if (i4 >= 4) {
                }
                break;
            case 3:
                if (b2 == 0 && i4 == 8) {
                    break;
                }
                break;
            case 4:
                if (i4 >= 4) {
                }
                break;
            case 6:
                if (i4 == 4) {
                }
                break;
            case 7:
                if (i4 == 8) {
                }
                break;
            case 8:
                if (i4 >= 4) {
                }
                break;
            case 9:
                if (i4 == 8) {
                }
                break;
        }
        return true;
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$PrimitiveArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    public void decode(ByteBuf byteBuf) {
        int unsignedShort;
        SpdyFrameDecoderDelegate spdyFrameDecoderDelegate;
        String str;
        State nextState;
        while (true) {
            int i2 = 0;
            z = false;
            boolean z2 = false;
            switch (this.state) {
                case READ_COMMON_HEADER:
                    if (byteBuf.readableBytes() < 8) {
                        return;
                    }
                    int i3 = byteBuf.readerIndex();
                    int i4 = i3 + 4;
                    int i5 = i3 + 5;
                    byteBuf.skipBytes(8);
                    if ((byteBuf.getByte(i3) & DefaultBinaryMemcacheRequest.REQUEST_MAGIC_BYTE) != 0) {
                        unsignedShort = SpdyCodecUtil.getUnsignedShort(byteBuf, i3) & 32767;
                        int unsignedShort2 = SpdyCodecUtil.getUnsignedShort(byteBuf, i3 + 2);
                        this.streamId = 0;
                        i2 = unsignedShort2;
                    } else {
                        unsignedShort = this.spdyVersion;
                        this.streamId = SpdyCodecUtil.getUnsignedInt(byteBuf, i3);
                    }
                    this.flags = byteBuf.getByte(i4);
                    int unsignedMedium = SpdyCodecUtil.getUnsignedMedium(byteBuf, i5);
                    this.length = unsignedMedium;
                    if (unsignedShort != this.spdyVersion) {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid SPDY Version";
                    } else if (isValidFrameHeader(this.streamId, i2, this.flags, unsignedMedium)) {
                        nextState = getNextState(i2, this.length);
                        this.state = nextState;
                    } else {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid Frame Error";
                    }
                    spdyFrameDecoderDelegate.readFrameError(str);
                    break;
                case READ_DATA_FRAME:
                    int i6 = this.length;
                    if (i6 == 0) {
                        this.state = State.READ_COMMON_HEADER;
                        this.delegate.readDataFrame(this.streamId, hasFlag(this.flags, (byte) 1), Unpooled.buffer(0));
                    } else {
                        int iMin = Math.min(this.maxChunkSize, i6);
                        if (byteBuf.readableBytes() < iMin) {
                            return;
                        }
                        ByteBuf byteBufBuffer = byteBuf.alloc().buffer(iMin);
                        byteBufBuffer.writeBytes(byteBuf, iMin);
                        int i7 = this.length - iMin;
                        this.length = i7;
                        if (i7 == 0) {
                            this.state = State.READ_COMMON_HEADER;
                        }
                        if (i7 == 0 && hasFlag(this.flags, (byte) 1)) {
                            z2 = true;
                        }
                        this.delegate.readDataFrame(this.streamId, z2, byteBufBuffer);
                    }
                    break;
                case READ_SYN_STREAM_FRAME:
                    if (byteBuf.readableBytes() < 10) {
                        return;
                    }
                    int i8 = byteBuf.readerIndex();
                    this.streamId = SpdyCodecUtil.getUnsignedInt(byteBuf, i8);
                    int unsignedInt = SpdyCodecUtil.getUnsignedInt(byteBuf, i8 + 4);
                    byte b2 = (byte) ((byteBuf.getByte(i8 + 8) >> 5) & 7);
                    boolean zHasFlag = hasFlag(this.flags, (byte) 1);
                    boolean zHasFlag2 = hasFlag(this.flags, (byte) 2);
                    byteBuf.skipBytes(10);
                    this.length -= 10;
                    int i9 = this.streamId;
                    if (i9 != 0) {
                        this.state = State.READ_HEADER_BLOCK;
                        this.delegate.readSynStreamFrame(i9, unsignedInt, b2, zHasFlag, zHasFlag2);
                    } else {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid SYN_STREAM Frame";
                        spdyFrameDecoderDelegate.readFrameError(str);
                    }
                    break;
                case READ_SYN_REPLY_FRAME:
                    if (byteBuf.readableBytes() < 4) {
                        return;
                    }
                    this.streamId = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex());
                    boolean zHasFlag3 = hasFlag(this.flags, (byte) 1);
                    byteBuf.skipBytes(4);
                    this.length -= 4;
                    int i10 = this.streamId;
                    if (i10 != 0) {
                        this.state = State.READ_HEADER_BLOCK;
                        this.delegate.readSynReplyFrame(i10, zHasFlag3);
                    } else {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid SYN_REPLY Frame";
                        spdyFrameDecoderDelegate.readFrameError(str);
                    }
                    break;
                case READ_RST_STREAM_FRAME:
                    if (byteBuf.readableBytes() < 8) {
                        return;
                    }
                    this.streamId = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex());
                    int signedInt = SpdyCodecUtil.getSignedInt(byteBuf, byteBuf.readerIndex() + 4);
                    byteBuf.skipBytes(8);
                    int i11 = this.streamId;
                    if (i11 == 0 || signedInt == 0) {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid RST_STREAM Frame";
                        spdyFrameDecoderDelegate.readFrameError(str);
                    } else {
                        this.state = State.READ_COMMON_HEADER;
                        this.delegate.readRstStreamFrame(i11, signedInt);
                    }
                    break;
                case READ_SETTINGS_FRAME:
                    if (byteBuf.readableBytes() < 4) {
                        return;
                    }
                    boolean zHasFlag4 = hasFlag(this.flags, (byte) 1);
                    this.numSettings = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex());
                    byteBuf.skipBytes(4);
                    int i12 = this.length - 4;
                    this.length = i12;
                    if ((i12 & 7) == 0 && (i12 >> 3) == this.numSettings) {
                        this.state = State.READ_SETTING;
                        this.delegate.readSettingsFrame(zHasFlag4);
                    } else {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid SETTINGS Frame";
                        spdyFrameDecoderDelegate.readFrameError(str);
                    }
                    break;
                case READ_SETTING:
                    if (this.numSettings == 0) {
                        this.state = State.READ_COMMON_HEADER;
                        this.delegate.readSettingsEnd();
                    } else {
                        if (byteBuf.readableBytes() < 8) {
                            return;
                        }
                        byte b3 = byteBuf.getByte(byteBuf.readerIndex());
                        int unsignedMedium2 = SpdyCodecUtil.getUnsignedMedium(byteBuf, byteBuf.readerIndex() + 1);
                        int signedInt2 = SpdyCodecUtil.getSignedInt(byteBuf, byteBuf.readerIndex() + 4);
                        boolean zHasFlag5 = hasFlag(b3, (byte) 1);
                        boolean zHasFlag6 = hasFlag(b3, (byte) 2);
                        byteBuf.skipBytes(8);
                        this.numSettings--;
                        this.delegate.readSetting(unsignedMedium2, signedInt2, zHasFlag5, zHasFlag6);
                    }
                    break;
                case READ_PING_FRAME:
                    if (byteBuf.readableBytes() < 4) {
                        return;
                    }
                    int signedInt3 = SpdyCodecUtil.getSignedInt(byteBuf, byteBuf.readerIndex());
                    byteBuf.skipBytes(4);
                    this.state = State.READ_COMMON_HEADER;
                    this.delegate.readPingFrame(signedInt3);
                    break;
                case READ_GOAWAY_FRAME:
                    if (byteBuf.readableBytes() < 8) {
                        return;
                    }
                    int unsignedInt2 = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex());
                    int signedInt4 = SpdyCodecUtil.getSignedInt(byteBuf, byteBuf.readerIndex() + 4);
                    byteBuf.skipBytes(8);
                    this.state = State.READ_COMMON_HEADER;
                    this.delegate.readGoAwayFrame(unsignedInt2, signedInt4);
                    break;
                case READ_HEADERS_FRAME:
                    if (byteBuf.readableBytes() < 4) {
                        return;
                    }
                    this.streamId = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex());
                    boolean zHasFlag7 = hasFlag(this.flags, (byte) 1);
                    byteBuf.skipBytes(4);
                    this.length -= 4;
                    int i13 = this.streamId;
                    if (i13 != 0) {
                        this.state = State.READ_HEADER_BLOCK;
                        this.delegate.readHeadersFrame(i13, zHasFlag7);
                    } else {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid HEADERS Frame";
                        spdyFrameDecoderDelegate.readFrameError(str);
                    }
                    break;
                case READ_WINDOW_UPDATE_FRAME:
                    if (byteBuf.readableBytes() < 8) {
                        return;
                    }
                    this.streamId = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex());
                    int unsignedInt3 = SpdyCodecUtil.getUnsignedInt(byteBuf, byteBuf.readerIndex() + 4);
                    byteBuf.skipBytes(8);
                    if (unsignedInt3 != 0) {
                        this.state = State.READ_COMMON_HEADER;
                        this.delegate.readWindowUpdateFrame(this.streamId, unsignedInt3);
                    } else {
                        this.state = State.FRAME_ERROR;
                        spdyFrameDecoderDelegate = this.delegate;
                        str = "Invalid WINDOW_UPDATE Frame";
                        spdyFrameDecoderDelegate.readFrameError(str);
                    }
                    break;
                case READ_HEADER_BLOCK:
                    if (this.length == 0) {
                        this.state = State.READ_COMMON_HEADER;
                        this.delegate.readHeaderBlockEnd();
                    } else {
                        if (!byteBuf.isReadable()) {
                            return;
                        }
                        int iMin2 = Math.min(byteBuf.readableBytes(), this.length);
                        ByteBuf byteBufBuffer2 = byteBuf.alloc().buffer(iMin2);
                        byteBufBuffer2.writeBytes(byteBuf, iMin2);
                        this.length -= iMin2;
                        this.delegate.readHeaderBlock(byteBufBuffer2);
                    }
                    break;
                case DISCARD_FRAME:
                    int iMin3 = Math.min(byteBuf.readableBytes(), this.length);
                    byteBuf.skipBytes(iMin3);
                    int i14 = this.length - iMin3;
                    this.length = i14;
                    if (i14 != 0) {
                        return;
                    }
                    nextState = State.READ_COMMON_HEADER;
                    this.state = nextState;
                    break;
                case FRAME_ERROR:
                    byteBuf.skipBytes(byteBuf.readableBytes());
                    return;
                default:
                    throw new Error("Shouldn't reach here.");
            }
        }
    }
}
