package io.netty.handler.codec.stomp;

import g.a.a.a.a;
import io.netty.buffer.ByteBuf;
import io.netty.handler.codec.DecoderException;
import io.netty.handler.codec.ReplayingDecoder;
import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.AsciiString;
import io.netty.util.internal.AppendableCharSequence;
import java.util.Locale;

/* JADX INFO: loaded from: classes.dex */
public class StompSubframeDecoder extends ReplayingDecoder<State> {
    private static final int DEFAULT_CHUNK_SIZE = 8132;
    private static final int DEFAULT_MAX_LINE_LENGTH = 1024;
    private int alreadyReadChunkSize;
    private long contentLength;
    private LastStompContentSubframe lastContent;
    private final int maxChunkSize;
    private final int maxLineLength;
    private final boolean validateHeaders;

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

        static {
            State.values();
            int[] iArr = new int[6];
            $SwitchMap$io$netty$handler$codec$stomp$StompSubframeDecoder$State = iArr;
            try {
                iArr[State.SKIP_CONTROL_CHARACTERS.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$stomp$StompSubframeDecoder$State[State.READ_HEADERS.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$stomp$StompSubframeDecoder$State[State.BAD_FRAME.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$stomp$StompSubframeDecoder$State[State.READ_CONTENT.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$stomp$StompSubframeDecoder$State[State.FINALIZE_FRAME_READ.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    public enum State {
        SKIP_CONTROL_CHARACTERS,
        READ_HEADERS,
        READ_CONTENT,
        FINALIZE_FRAME_READ,
        BAD_FRAME,
        INVALID_CHUNK
    }

    public StompSubframeDecoder() {
        this(1024, DEFAULT_CHUNK_SIZE);
    }

    public StompSubframeDecoder(int i2, int i3) {
        this(i2, i3, false);
    }

    public StompSubframeDecoder(int i2, int i3, boolean z2) {
        super(State.SKIP_CONTROL_CHARACTERS);
        this.contentLength = -1L;
        if (i2 <= 0) {
            throw new IllegalArgumentException(a.l("maxLineLength must be a positive integer: ", i2));
        }
        if (i3 <= 0) {
            throw new IllegalArgumentException(a.l("maxChunkSize must be a positive integer: ", i3));
        }
        this.maxChunkSize = i3;
        this.maxLineLength = i2;
        this.validateHeaders = z2;
    }

    public StompSubframeDecoder(boolean z2) {
        this(1024, DEFAULT_CHUNK_SIZE, z2);
    }

    private static long getContentLength(StompHeaders stompHeaders, long j2) {
        AsciiString asciiString = StompHeaders.CONTENT_LENGTH;
        long j3 = stompHeaders.getLong(asciiString, j2);
        if (j3 >= 0) {
            return j3;
        }
        throw new DecoderException(((Object) asciiString) + " must be non-negative");
    }

    private void invalidHeader(String str, String str2) {
        if (str != null) {
            str2 = a.u(str, ":", str2);
        }
        throw new IllegalArgumentException(a.t("a header value or name contains a prohibited character ':', ", str2));
    }

    private void invalidLineLength() {
        throw new TooLongFrameException(a.y(a.F("An STOMP line is larger than "), this.maxLineLength, " bytes."));
    }

    private StompCommand readCommand(ByteBuf byteBuf) {
        StompCommand stompCommandValueOf;
        String line = readLine(byteBuf, 16);
        try {
            stompCommandValueOf = StompCommand.valueOf(line);
        } catch (IllegalArgumentException unused) {
            stompCommandValueOf = null;
        }
        if (stompCommandValueOf == null) {
            try {
                stompCommandValueOf = StompCommand.valueOf(line.toUpperCase(Locale.US));
            } catch (IllegalArgumentException unused2) {
            }
        }
        if (stompCommandValueOf != null) {
            return stompCommandValueOf;
        }
        throw new DecoderException("failed to read command from channel");
    }

    private boolean readHeader(StompHeaders stompHeaders, AppendableCharSequence appendableCharSequence, ByteBuf byteBuf) {
        appendableCharSequence.reset();
        String string = null;
        int i2 = 0;
        boolean z2 = false;
        while (true) {
            byte b2 = byteBuf.readByte();
            if (b2 == 58 && string == null) {
                string = appendableCharSequence.toString();
                appendableCharSequence.reset();
                z2 = true;
            } else if (b2 == 13) {
                continue;
            } else {
                if (b2 == 10) {
                    break;
                }
                if (i2 >= this.maxLineLength) {
                    invalidLineLength();
                }
                if (b2 == 58 && string != null) {
                    z2 = false;
                }
                i2++;
                appendableCharSequence.append((char) b2);
            }
        }
        if (string == null && i2 == 0) {
            return false;
        }
        if (z2) {
            stompHeaders.add(string, appendableCharSequence.toString());
        } else if (this.validateHeaders) {
            invalidHeader(string, appendableCharSequence.toString());
        }
        return true;
    }

    private State readHeaders(ByteBuf byteBuf, StompHeaders stompHeaders) {
        while (readHeader(stompHeaders, new AppendableCharSequence(128), byteBuf)) {
        }
        if (stompHeaders.contains(StompHeaders.CONTENT_LENGTH)) {
            long contentLength = getContentLength(stompHeaders, 0L);
            this.contentLength = contentLength;
            if (contentLength == 0) {
                return State.FINALIZE_FRAME_READ;
            }
        }
        return State.READ_CONTENT;
    }

    private String readLine(ByteBuf byteBuf, int i2) {
        AppendableCharSequence appendableCharSequence = new AppendableCharSequence(i2);
        int i3 = 0;
        while (true) {
            byte b2 = byteBuf.readByte();
            if (b2 != 13) {
                if (b2 == 10) {
                    return appendableCharSequence.toString();
                }
                if (i3 >= this.maxLineLength) {
                    invalidLineLength();
                }
                i3++;
                appendableCharSequence.append((char) b2);
            }
        }
    }

    private void resetDecoder() {
        checkpoint(State.SKIP_CONTROL_CHARACTERS);
        this.contentLength = -1L;
        this.alreadyReadChunkSize = 0;
        this.lastContent = null;
    }

    private static void skipControlCharacters(ByteBuf byteBuf) {
        while (true) {
            byte b2 = byteBuf.readByte();
            if (b2 != 13 && b2 != 10) {
                byteBuf.readerIndex(byteBuf.readerIndex() - 1);
                return;
            }
        }
    }

    private static void skipNullCharacter(ByteBuf byteBuf) {
        byte b2 = byteBuf.readByte();
        if (b2 != 0) {
            throw new IllegalStateException(a.n("unexpected byte in buffer ", b2, " while expecting NULL byte"));
        }
    }

    /* JADX WARN: Can't wrap try/catch for region: R(7:0|2|(3:(8:(9:4|(2:6|(2:9|10))|66|15|(2:17|(2:19|67))(2:20|(1:22)(5:23|(1:25)|26|(4:28|(1:30)|31|(1:33)(2:35|36))(2:37|(1:39)(3:(1:41)(1:42)|43|(1:45)(2:50|51)))|34))|46|(1:48)|49|68)(1:11)|66|15|(0)(0)|46|(0)|49|68)|64|14)|12|62|13|(1:(0))) */
    /* JADX WARN: Code restructure failed: missing block: B:57:0x010e, code lost:
    
        r6 = e;
     */
    /* JADX WARN: Removed duplicated region for block: B:17:0x004a  */
    /* JADX WARN: Removed duplicated region for block: B:20:0x004f A[Catch: Exception -> 0x00f3, TryCatch #2 {Exception -> 0x00f3, blocks: (B:15:0x003d, B:46:0x00d6, B:48:0x00dd, B:49:0x00e1, B:20:0x004f, B:23:0x0056, B:26:0x005b, B:28:0x0063, B:31:0x006b, B:33:0x007f, B:34:0x0088, B:35:0x008c, B:37:0x0095, B:39:0x00a8, B:41:0x00ad, B:43:0x00bd, B:45:0x00cc, B:50:0x00ea, B:42:0x00b4), top: B:66:0x003d }] */
    /* JADX WARN: Removed duplicated region for block: B:48:0x00dd A[Catch: Exception -> 0x00f3, TryCatch #2 {Exception -> 0x00f3, blocks: (B:15:0x003d, B:46:0x00d6, B:48:0x00dd, B:49:0x00e1, B:20:0x004f, B:23:0x0056, B:26:0x005b, B:28:0x0063, B:31:0x006b, B:33:0x007f, B:34:0x0088, B:35:0x008c, B:37:0x0095, B:39:0x00a8, B:41:0x00ad, B:43:0x00bd, B:45:0x00cc, B:50:0x00ea, B:42:0x00b4), top: B:66:0x003d }] */
    @Override // io.netty.handler.codec.ByteToMessageDecoder
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct add '--show-bad-code' argument
    */
    public void decode(io.netty.channel.ChannelHandlerContext r6, io.netty.buffer.ByteBuf r7, java.util.List<java.lang.Object> r8) {
        /*
            Method dump skipped, instruction units count: 294
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.stomp.StompSubframeDecoder.decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List):void");
    }
}
