package io.netty.handler.codec.mqtt;

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.util.CharsetUtil;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public final class MqttDecoder extends ReplayingDecoder<DecoderState> {
    private static final int DEFAULT_MAX_BYTES_IN_MESSAGE = 8092;
    private int bytesRemainingInVariablePart;
    private final int maxBytesInMessage;
    private MqttFixedHeader mqttFixedHeader;
    private Object variableHeader;

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

        static {
            MqttMessageType.values();
            int[] iArr = new int[14];
            $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType = iArr;
            try {
                iArr[MqttMessageType.CONNECT.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.CONNACK.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.SUBSCRIBE.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.UNSUBSCRIBE.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.SUBACK.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.UNSUBACK.ordinal()] = 6;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PUBACK.ordinal()] = 7;
            } catch (NoSuchFieldError unused7) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PUBREC.ordinal()] = 8;
            } catch (NoSuchFieldError unused8) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PUBCOMP.ordinal()] = 9;
            } catch (NoSuchFieldError unused9) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PUBREL.ordinal()] = 10;
            } catch (NoSuchFieldError unused10) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PUBLISH.ordinal()] = 11;
            } catch (NoSuchFieldError unused11) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PINGREQ.ordinal()] = 12;
            } catch (NoSuchFieldError unused12) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.PINGRESP.ordinal()] = 13;
            } catch (NoSuchFieldError unused13) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttMessageType[MqttMessageType.DISCONNECT.ordinal()] = 14;
            } catch (NoSuchFieldError unused14) {
            }
            DecoderState.values();
            int[] iArr2 = new int[4];
            $SwitchMap$io$netty$handler$codec$mqtt$MqttDecoder$DecoderState = iArr2;
            try {
                iArr2[DecoderState.READ_FIXED_HEADER.ordinal()] = 1;
            } catch (NoSuchFieldError unused15) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttDecoder$DecoderState[DecoderState.READ_VARIABLE_HEADER.ordinal()] = 2;
            } catch (NoSuchFieldError unused16) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttDecoder$DecoderState[DecoderState.READ_PAYLOAD.ordinal()] = 3;
            } catch (NoSuchFieldError unused17) {
            }
            try {
                $SwitchMap$io$netty$handler$codec$mqtt$MqttDecoder$DecoderState[DecoderState.BAD_MESSAGE.ordinal()] = 4;
            } catch (NoSuchFieldError unused18) {
            }
        }
    }

    public enum DecoderState {
        READ_FIXED_HEADER,
        READ_VARIABLE_HEADER,
        READ_PAYLOAD,
        BAD_MESSAGE
    }

    public static final class Result<T> {
        private final int numberOfBytesConsumed;
        private final T value;

        public Result(T t2, int i2) {
            this.value = t2;
            this.numberOfBytesConsumed = i2;
        }
    }

    public MqttDecoder() {
        this(DEFAULT_MAX_BYTES_IN_MESSAGE);
    }

    public MqttDecoder(int i2) {
        super(DecoderState.READ_FIXED_HEADER);
        this.maxBytesInMessage = i2;
    }

    private static Result<byte[]> decodeByteArray(ByteBuf byteBuf) {
        Result<Integer> resultDecodeMsbLsb = decodeMsbLsb(byteBuf);
        int iIntValue = ((Integer) ((Result) resultDecodeMsbLsb).value).intValue();
        byte[] bArr = new byte[iIntValue];
        byteBuf.readBytes(bArr);
        return new Result<>(bArr, ((Result) resultDecodeMsbLsb).numberOfBytesConsumed + iIntValue);
    }

    private static Result<MqttConnAckVariableHeader> decodeConnAckVariableHeader(ByteBuf byteBuf) {
        return new Result<>(new MqttConnAckVariableHeader(MqttConnectReturnCode.valueOf(byteBuf.readByte()), (byteBuf.readUnsignedByte() & 1) == 1), 2);
    }

    private static Result<MqttConnectPayload> decodeConnectionPayload(ByteBuf byteBuf, MqttConnectVariableHeader mqttConnectVariableHeader) {
        Result<byte[]> result;
        Result<String> result2;
        Result<String> resultDecodeString;
        Result<byte[]> resultDecodeByteArray;
        Result<String> resultDecodeString2 = decodeString(byteBuf);
        String str = (String) ((Result) resultDecodeString2).value;
        if (!MqttCodecUtil.isValidClientId(MqttVersion.fromProtocolNameAndLevel(mqttConnectVariableHeader.name(), (byte) mqttConnectVariableHeader.version()), str)) {
            throw new MqttIdentifierRejectedException(a.t("invalid clientIdentifier: ", str));
        }
        int i2 = ((Result) resultDecodeString2).numberOfBytesConsumed;
        if (mqttConnectVariableHeader.isWillFlag()) {
            Result<String> resultDecodeString3 = decodeString(byteBuf, 0, 32767);
            int i3 = ((Result) resultDecodeString3).numberOfBytesConsumed + i2;
            Result<byte[]> resultDecodeByteArray2 = decodeByteArray(byteBuf);
            result = resultDecodeByteArray2;
            i2 = i3 + ((Result) resultDecodeByteArray2).numberOfBytesConsumed;
            result2 = resultDecodeString3;
        } else {
            result = null;
            result2 = null;
        }
        if (mqttConnectVariableHeader.hasUserName()) {
            resultDecodeString = decodeString(byteBuf);
            i2 += ((Result) resultDecodeString).numberOfBytesConsumed;
        } else {
            resultDecodeString = null;
        }
        if (mqttConnectVariableHeader.hasPassword()) {
            resultDecodeByteArray = decodeByteArray(byteBuf);
            i2 += ((Result) resultDecodeByteArray).numberOfBytesConsumed;
        } else {
            resultDecodeByteArray = null;
        }
        return new Result<>(new MqttConnectPayload((String) ((Result) resultDecodeString2).value, result2 != null ? (String) ((Result) result2).value : null, result != null ? (byte[]) ((Result) result).value : null, resultDecodeString != null ? (String) ((Result) resultDecodeString).value : null, resultDecodeByteArray != null ? (byte[]) ((Result) resultDecodeByteArray).value : null), i2);
    }

    private static Result<MqttConnectVariableHeader> decodeConnectionVariableHeader(ByteBuf byteBuf) {
        Result<String> resultDecodeString = decodeString(byteBuf);
        int i2 = ((Result) resultDecodeString).numberOfBytesConsumed;
        MqttVersion mqttVersionFromProtocolNameAndLevel = MqttVersion.fromProtocolNameAndLevel((String) ((Result) resultDecodeString).value, byteBuf.readByte());
        short unsignedByte = byteBuf.readUnsignedByte();
        Result<Integer> resultDecodeMsbLsb = decodeMsbLsb(byteBuf);
        int i3 = ((Result) resultDecodeMsbLsb).numberOfBytesConsumed + i2 + 1 + 1;
        boolean z2 = (unsignedByte & 128) == 128;
        boolean z3 = (unsignedByte & 64) == 64;
        boolean z4 = (unsignedByte & 32) == 32;
        int i4 = (unsignedByte & 24) >> 3;
        boolean z5 = (unsignedByte & 4) == 4;
        boolean z6 = (unsignedByte & 2) == 2;
        if (mqttVersionFromProtocolNameAndLevel == MqttVersion.MQTT_3_1_1) {
            if (!((unsignedByte & 1) == 0)) {
                throw new DecoderException("non-zero reserved flag");
            }
        }
        return new Result<>(new MqttConnectVariableHeader(mqttVersionFromProtocolNameAndLevel.protocolName(), mqttVersionFromProtocolNameAndLevel.protocolLevel(), z2, z3, z4, i4, z5, z6, ((Integer) ((Result) resultDecodeMsbLsb).value).intValue()), i3);
    }

    private static MqttFixedHeader decodeFixedHeader(ByteBuf byteBuf) {
        int i2;
        int i3;
        short unsignedByte = byteBuf.readUnsignedByte();
        MqttMessageType mqttMessageTypeValueOf = MqttMessageType.valueOf(unsignedByte >> 4);
        int i4 = 0;
        boolean z2 = (unsignedByte & 8) == 8;
        int i5 = (unsignedByte & 6) >> 1;
        boolean z3 = (unsignedByte & 1) != 0;
        int i6 = 0;
        int i7 = 1;
        while (true) {
            short unsignedByte2 = byteBuf.readUnsignedByte();
            i2 = ((unsignedByte2 & 127) * i7) + i4;
            i7 *= 128;
            i6++;
            i3 = unsignedByte2 & 128;
            if (i3 == 0 || i6 >= 4) {
                break;
            }
            i4 = i2;
        }
        if (i6 != 4 || i3 == 0) {
            return MqttCodecUtil.validateFixedHeader(MqttCodecUtil.resetUnusedFields(new MqttFixedHeader(mqttMessageTypeValueOf, z2, MqttQoS.valueOf(i5), z3, i2)));
        }
        throw new DecoderException("remaining length exceeds 4 digits (" + mqttMessageTypeValueOf + ')');
    }

    private static Result<Integer> decodeMessageId(ByteBuf byteBuf) {
        Result<Integer> resultDecodeMsbLsb = decodeMsbLsb(byteBuf);
        if (MqttCodecUtil.isValidMessageId(((Integer) ((Result) resultDecodeMsbLsb).value).intValue())) {
            return resultDecodeMsbLsb;
        }
        StringBuilder sbF = a.F("invalid messageId: ");
        sbF.append(((Result) resultDecodeMsbLsb).value);
        throw new DecoderException(sbF.toString());
    }

    private static Result<MqttMessageIdVariableHeader> decodeMessageIdVariableHeader(ByteBuf byteBuf) {
        Result<Integer> resultDecodeMessageId = decodeMessageId(byteBuf);
        return new Result<>(MqttMessageIdVariableHeader.from(((Integer) ((Result) resultDecodeMessageId).value).intValue()), ((Result) resultDecodeMessageId).numberOfBytesConsumed);
    }

    private static Result<Integer> decodeMsbLsb(ByteBuf byteBuf) {
        return decodeMsbLsb(byteBuf, 0, 65535);
    }

    private static Result<Integer> decodeMsbLsb(ByteBuf byteBuf, int i2, int i3) {
        int unsignedByte = byteBuf.readUnsignedByte() | (byteBuf.readUnsignedByte() << 8);
        if (unsignedByte < i2 || unsignedByte > i3) {
            unsignedByte = -1;
        }
        return new Result<>(Integer.valueOf(unsignedByte), 2);
    }

    private static Result<?> decodePayload(ByteBuf byteBuf, MqttMessageType mqttMessageType, int i2, Object obj) {
        int iOrdinal = mqttMessageType.ordinal();
        return iOrdinal != 0 ? iOrdinal != 2 ? iOrdinal != 7 ? iOrdinal != 8 ? iOrdinal != 9 ? new Result<>(null, 0) : decodeUnsubscribePayload(byteBuf, i2) : decodeSubackPayload(byteBuf, i2) : decodeSubscribePayload(byteBuf, i2) : decodePublishPayload(byteBuf, i2) : decodeConnectionPayload(byteBuf, (MqttConnectVariableHeader) obj);
    }

    private static Result<ByteBuf> decodePublishPayload(ByteBuf byteBuf, int i2) {
        return new Result<>(byteBuf.readRetainedSlice(i2), i2);
    }

    private static Result<MqttPublishVariableHeader> decodePublishVariableHeader(ByteBuf byteBuf, MqttFixedHeader mqttFixedHeader) {
        Result<String> resultDecodeString = decodeString(byteBuf);
        if (!MqttCodecUtil.isValidPublishTopicName((String) ((Result) resultDecodeString).value)) {
            throw new DecoderException(a.A(a.F("invalid publish topic name: "), (String) ((Result) resultDecodeString).value, " (contains wildcards)"));
        }
        int i2 = ((Result) resultDecodeString).numberOfBytesConsumed;
        int iIntValue = -1;
        if (mqttFixedHeader.qosLevel().value() > 0) {
            Result<Integer> resultDecodeMessageId = decodeMessageId(byteBuf);
            iIntValue = ((Integer) ((Result) resultDecodeMessageId).value).intValue();
            i2 += ((Result) resultDecodeMessageId).numberOfBytesConsumed;
        }
        return new Result<>(new MqttPublishVariableHeader((String) ((Result) resultDecodeString).value, iIntValue), i2);
    }

    private static Result<String> decodeString(ByteBuf byteBuf) {
        return decodeString(byteBuf, 0, Integer.MAX_VALUE);
    }

    private static Result<String> decodeString(ByteBuf byteBuf, int i2, int i3) {
        Result<Integer> resultDecodeMsbLsb = decodeMsbLsb(byteBuf);
        int iIntValue = ((Integer) ((Result) resultDecodeMsbLsb).value).intValue();
        int i4 = ((Result) resultDecodeMsbLsb).numberOfBytesConsumed;
        if (iIntValue < i2 || iIntValue > i3) {
            byteBuf.skipBytes(iIntValue);
            return new Result<>(null, i4 + iIntValue);
        }
        String string = byteBuf.toString(byteBuf.readerIndex(), iIntValue, CharsetUtil.UTF_8);
        byteBuf.skipBytes(iIntValue);
        return new Result<>(string, i4 + iIntValue);
    }

    private static Result<MqttSubAckPayload> decodeSubackPayload(ByteBuf byteBuf, int i2) {
        ArrayList arrayList = new ArrayList();
        int i3 = 0;
        while (i3 < i2) {
            int unsignedByte = byteBuf.readUnsignedByte();
            if (unsignedByte != MqttQoS.FAILURE.value()) {
                unsignedByte &= 3;
            }
            i3++;
            arrayList.add(Integer.valueOf(unsignedByte));
        }
        return new Result<>(new MqttSubAckPayload(arrayList), i3);
    }

    private static Result<MqttSubscribePayload> decodeSubscribePayload(ByteBuf byteBuf, int i2) {
        ArrayList arrayList = new ArrayList();
        int i3 = 0;
        while (i3 < i2) {
            Result<String> resultDecodeString = decodeString(byteBuf);
            int i4 = ((Result) resultDecodeString).numberOfBytesConsumed + i3;
            arrayList.add(new MqttTopicSubscription((String) ((Result) resultDecodeString).value, MqttQoS.valueOf(byteBuf.readUnsignedByte() & 3)));
            i3 = i4 + 1;
        }
        return new Result<>(new MqttSubscribePayload(arrayList), i3);
    }

    private static Result<MqttUnsubscribePayload> decodeUnsubscribePayload(ByteBuf byteBuf, int i2) {
        ArrayList arrayList = new ArrayList();
        int i3 = 0;
        while (i3 < i2) {
            Result<String> resultDecodeString = decodeString(byteBuf);
            i3 += ((Result) resultDecodeString).numberOfBytesConsumed;
            arrayList.add(((Result) resultDecodeString).value);
        }
        return new Result<>(new MqttUnsubscribePayload(arrayList), i3);
    }

    private static Result<?> decodeVariableHeader(ByteBuf byteBuf, MqttFixedHeader mqttFixedHeader) {
        switch (mqttFixedHeader.messageType()) {
        }
        return new Result<>(null, 0);
    }

    private MqttMessage invalidMessage(Throwable th) {
        checkpoint(DecoderState.BAD_MESSAGE);
        return MqttMessageFactory.newInvalidMessage(th);
    }

    /* JADX WARN: Removed duplicated region for block: B:19:0x006d A[Catch: Exception -> 0x00b3, TryCatch #0 {Exception -> 0x00b3, blocks: (B:17:0x0054, B:19:0x006d, B:21:0x0087, B:22:0x00b2, B:14:0x0034, B:16:0x003a, B:26:0x00bc, B:27:0x00d9, B:13:0x0023), top: B:28:0x0023 }] */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0087 A[Catch: Exception -> 0x00b3, TryCatch #0 {Exception -> 0x00b3, blocks: (B:17:0x0054, B:19:0x006d, B:21:0x0087, B:22:0x00b2, B:14:0x0034, B:16:0x003a, B:26:0x00bc, B:27:0x00d9, B:13:0x0023), top: B:28:0x0023 }] */
    @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 r3, io.netty.buffer.ByteBuf r4, java.util.List<java.lang.Object> r5) {
        /*
            Method dump skipped, instruction units count: 218
            To view this dump add '--comments-level debug' option
        */
        throw new UnsupportedOperationException("Method not decompiled: io.netty.handler.codec.mqtt.MqttDecoder.decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List):void");
    }
}
