package io.netty.handler.codec;

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

/* 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);
    }

    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 z2) {
        this(ByteOrder.BIG_ENDIAN, i2, i3, i4, i5, i6, z2);
    }

    public LengthFieldBasedFrameDecoder(ByteOrder byteOrder, int i2, int i3, int i4, int i5, int i6, boolean z2) {
        Objects.requireNonNull(byteOrder, "byteOrder");
        if (i2 <= 0) {
            throw new IllegalArgumentException(a.l("maxFrameLength must be a positive integer: ", i2));
        }
        if (i3 < 0) {
            throw new IllegalArgumentException(a.l("lengthFieldOffset must be a non-negative integer: ", i3));
        }
        if (i6 < 0) {
            throw new IllegalArgumentException(a.l("initialBytesToStrip must be a non-negative integer: ", i6));
        }
        if (i3 > i2 - i4) {
            throw new IllegalArgumentException(a.y(a.I("maxFrameLength (", i2, ") must be equal to or greater than lengthFieldOffset (", i3, ") + lengthFieldLength ("), 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 = z2;
    }

    private void discardingTooLongFrame(ByteBuf byteBuf) {
        long j2 = this.bytesToDiscard;
        int iMin = (int) Math.min(j2, byteBuf.readableBytes());
        byteBuf.skipBytes(iMin);
        this.bytesToDiscard = j2 - ((long) iMin);
        failIfNecessary(false);
    }

    private void exceededFrameLength(ByteBuf byteBuf, long j2) {
        int i2;
        long j3 = j2 - ((long) byteBuf.readableBytes());
        this.tooLongFrameLength = j2;
        if (j3 < 0) {
            i2 = (int) j2;
        } else {
            this.discardingTooLongFrame = true;
            this.bytesToDiscard = j3;
            i2 = byteBuf.readableBytes();
        }
        byteBuf.skipBytes(i2);
        failIfNecessary(true);
    }

    private void fail(long j2) {
        if (j2 <= 0) {
            throw new TooLongFrameException(a.y(a.F("Adjusted frame length exceeds "), this.maxFrameLength, " - discarding"));
        }
        StringBuilder sbF = a.F("Adjusted frame length exceeds ");
        sbF.append(this.maxFrameLength);
        sbF.append(": ");
        sbF.append(j2);
        sbF.append(" - discarded");
        throw new TooLongFrameException(sbF.toString());
    }

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

    private static void failOnFrameLengthLessThanInitialBytesToStrip(ByteBuf byteBuf, long j2, int i2) {
        byteBuf.skipBytes((int) j2);
        throw new CorruptedFrameException("Adjusted frame length (" + j2 + ") is less than initialBytesToStrip: " + i2);
    }

    private static void failOnFrameLengthLessThanLengthFieldEndOffset(ByteBuf byteBuf, long j2, int i2) {
        byteBuf.skipBytes(i2);
        throw new CorruptedFrameException("Adjusted frame length (" + j2 + ") is less than lengthFieldEndOffset: " + i2);
    }

    private static void failOnNegativeLengthField(ByteBuf byteBuf, long j2, int i2) {
        byteBuf.skipBytes(i2);
        throw new CorruptedFrameException(a.q("negative pre-adjustment length field: ", j2));
    }

    public Object decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
        if (this.discardingTooLongFrame) {
            discardingTooLongFrame(byteBuf);
        }
        if (byteBuf.readableBytes() < this.lengthFieldEndOffset) {
            return null;
        }
        long unadjustedFrameLength = getUnadjustedFrameLength(byteBuf, byteBuf.readerIndex() + this.lengthFieldOffset, this.lengthFieldLength, this.byteOrder);
        if (unadjustedFrameLength < 0) {
            failOnNegativeLengthField(byteBuf, unadjustedFrameLength, this.lengthFieldEndOffset);
        }
        int i2 = this.lengthAdjustment;
        int i3 = this.lengthFieldEndOffset;
        long j2 = unadjustedFrameLength + ((long) (i2 + i3));
        if (j2 < i3) {
            failOnFrameLengthLessThanLengthFieldEndOffset(byteBuf, j2, i3);
        }
        if (j2 > this.maxFrameLength) {
            exceededFrameLength(byteBuf, j2);
            return null;
        }
        int i4 = (int) j2;
        if (byteBuf.readableBytes() < i4) {
            return null;
        }
        int i5 = this.initialBytesToStrip;
        if (i5 > i4) {
            failOnFrameLengthLessThanInitialBytesToStrip(byteBuf, j2, i5);
        }
        byteBuf.skipBytes(this.initialBytesToStrip);
        int i6 = byteBuf.readerIndex();
        int i7 = i4 - this.initialBytesToStrip;
        ByteBuf byteBufExtractFrame = extractFrame(channelHandlerContext, byteBuf, i6, i7);
        byteBuf.readerIndex(i6 + i7);
        return byteBufExtractFrame;
    }

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

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

    public 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(a.y(a.F("unsupported lengthFieldLength: "), this.lengthFieldLength, " (expected: 1, 2, 3, 4, or 8)"));
            }
            unsignedByte = byteBufOrder.getUnsignedMedium(i2);
        }
        return unsignedByte;
    }
}
