package io.netty.handler.codec;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import java.nio.ByteOrder;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public class LengthFieldBasedFrameDecoder extends ByteToMessageDecoder {
    private final ByteOrder byteOrder;
    private long bytesToDiscard;
    private boolean discardingTooLongFrame;
    private final boolean failFast;
    private final int initialBytesToStrip;
    private final int lengthAdjustment;
    private final int lengthFieldEndOffset;
    private final int lengthFieldLength;
    private final int lengthFieldOffset;
    private final int maxFrameLength;
    private long tooLongFrameLength;

    public LengthFieldBasedFrameDecoder(int i2, int i3, int i4) {
        this(i2, i3, i4, 0, 0);
    }

    private void fail(long j2) {
        if (j2 <= 0) {
            throw new TooLongFrameException("Adjusted frame length exceeds " + this.maxFrameLength + " - discarding");
        }
        throw new TooLongFrameException("Adjusted frame length exceeds " + this.maxFrameLength + ": " + j2 + " - discarded");
    }

    private void failIfNecessary(boolean z) {
        if (this.bytesToDiscard != 0) {
            if (this.failFast && z) {
                fail(this.tooLongFrameLength);
                return;
            }
            return;
        }
        long j2 = this.tooLongFrameLength;
        this.tooLongFrameLength = 0L;
        this.discardingTooLongFrame = false;
        boolean z2 = this.failFast;
        if (!z2 || (z2 && z)) {
            fail(j2);
        }
    }

    @Override // io.netty.handler.codec.ByteToMessageDecoder
    protected final void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {
        Object objDecode = decode(channelHandlerContext, byteBuf);
        if (objDecode != null) {
            list.add(objDecode);
        }
    }

    protected ByteBuf extractFrame(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, int i2, int i3) {
        return byteBuf.retainedSlice(i2, i3);
    }

    protected long getUnadjustedFrameLength(ByteBuf byteBuf, int i2, int i3, ByteOrder byteOrder) {
        int unsignedByte;
        ByteBuf byteBufOrder = byteBuf.order(byteOrder);
        if (i3 == 1) {
            unsignedByte = byteBufOrder.getUnsignedByte(i2);
        } else if (i3 == 2) {
            unsignedByte = byteBufOrder.getUnsignedShort(i2);
        } else {
            if (i3 != 3) {
                if (i3 == 4) {
                    return byteBufOrder.getUnsignedInt(i2);
                }
                if (i3 == 8) {
                    return byteBufOrder.getLong(i2);
                }
                throw new DecoderException("unsupported lengthFieldLength: " + this.lengthFieldLength + " (expected: 1, 2, 3, 4, or 8)");
            }
            unsignedByte = byteBufOrder.getUnsignedMedium(i2);
        }
        return unsignedByte;
    }

    public LengthFieldBasedFrameDecoder(int i2, int i3, int i4, int i5, int i6) {
        this(i2, i3, i4, i5, i6, true);
    }

    public LengthFieldBasedFrameDecoder(int i2, int i3, int i4, int i5, int i6, boolean z) {
        this(ByteOrder.BIG_ENDIAN, i2, i3, i4, i5, i6, z);
    }

    protected Object decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
        if (this.discardingTooLongFrame) {
            long j2 = this.bytesToDiscard;
            int iMin = (int) Math.min(j2, byteBuf.readableBytes());
            byteBuf.skipBytes(iMin);
            this.bytesToDiscard = j2 - ((long) iMin);
            failIfNecessary(false);
        }
        if (byteBuf.readableBytes() < this.lengthFieldEndOffset) {
            return null;
        }
        long unadjustedFrameLength = getUnadjustedFrameLength(byteBuf, byteBuf.readerIndex() + this.lengthFieldOffset, this.lengthFieldLength, this.byteOrder);
        if (unadjustedFrameLength >= 0) {
            int i2 = this.lengthAdjustment;
            int i3 = this.lengthFieldEndOffset;
            long j3 = unadjustedFrameLength + ((long) (i2 + i3));
            if (j3 < i3) {
                byteBuf.skipBytes(i3);
                throw new CorruptedFrameException("Adjusted frame length (" + j3 + ") is less than lengthFieldEndOffset: " + this.lengthFieldEndOffset);
            }
            if (j3 > this.maxFrameLength) {
                long j4 = j3 - ((long) byteBuf.readableBytes());
                this.tooLongFrameLength = j3;
                if (j4 < 0) {
                    byteBuf.skipBytes((int) j3);
                } else {
                    this.discardingTooLongFrame = true;
                    this.bytesToDiscard = j4;
                    byteBuf.skipBytes(byteBuf.readableBytes());
                }
                failIfNecessary(true);
                return null;
            }
            int i4 = (int) j3;
            if (byteBuf.readableBytes() < i4) {
                return null;
            }
            int i5 = this.initialBytesToStrip;
            if (i5 <= i4) {
                byteBuf.skipBytes(i5);
                int i6 = byteBuf.readerIndex();
                int i7 = i4 - this.initialBytesToStrip;
                ByteBuf byteBufExtractFrame = extractFrame(channelHandlerContext, byteBuf, i6, i7);
                byteBuf.readerIndex(i6 + i7);
                return byteBufExtractFrame;
            }
            byteBuf.skipBytes(i4);
            throw new CorruptedFrameException("Adjusted frame length (" + j3 + ") is less than initialBytesToStrip: " + this.initialBytesToStrip);
        }
        byteBuf.skipBytes(this.lengthFieldEndOffset);
        throw new CorruptedFrameException("negative pre-adjustment length field: " + unadjustedFrameLength);
    }

    public LengthFieldBasedFrameDecoder(ByteOrder byteOrder, int i2, int i3, int i4, int i5, int i6, boolean z) {
        if (byteOrder == null) {
            throw new NullPointerException("byteOrder");
        }
        if (i2 <= 0) {
            throw new IllegalArgumentException("maxFrameLength must be a positive integer: " + i2);
        }
        if (i3 < 0) {
            throw new IllegalArgumentException("lengthFieldOffset must be a non-negative integer: " + i3);
        }
        if (i6 < 0) {
            throw new IllegalArgumentException("initialBytesToStrip must be a non-negative integer: " + i6);
        }
        if (i3 <= i2 - i4) {
            this.byteOrder = byteOrder;
            this.maxFrameLength = i2;
            this.lengthFieldOffset = i3;
            this.lengthFieldLength = i4;
            this.lengthAdjustment = i5;
            this.lengthFieldEndOffset = i3 + i4;
            this.initialBytesToStrip = i6;
            this.failFast = z;
            return;
        }
        throw new IllegalArgumentException("maxFrameLength (" + i2 + ") must be equal to or greater than lengthFieldOffset (" + i3 + ") + lengthFieldLength (" + i4 + ").");
    }
}
