package org.eclipse.paho.mqttv5.common.util;

import java.io.UnsupportedEncodingException;

/* JADX INFO: loaded from: classes2.dex */
public class MqttTopicValidator {
    private static final int MAX_TOPIC_LEN = 65535;
    private static final int MIN_TOPIC_LEN = 1;
    public static final String MULTI_LEVEL_WILDCARD = "#";
    public static final String MULTI_LEVEL_WILDCARD_PATTERN = "/#";
    private static final char NUL = 0;
    public static final String SINGLE_LEVEL_WILDCARD = "+";
    public static final String TOPIC_LEVEL_SEPARATOR = "/";
    public static final String TOPIC_WILDCARDS = "#+";

    public static void validate(String str, boolean z, boolean z2) throws IllegalArgumentException {
        try {
            int length = str.getBytes("UTF-8").length;
            if (length < 1 || length > 65535) {
                throw new IllegalArgumentException(String.format("Invalid topic length, should be in range[%d, %d]!", 1, 65535));
            }
            if (!z) {
                if (!z2 && str.startsWith("$share/")) {
                    throw new IllegalArgumentException("Shared Subscriptions are not allowed.");
                }
                if (Strings.containsAny(str, TOPIC_WILDCARDS)) {
                    throw new IllegalArgumentException("The topic name MUST NOT contain any wildcard characters (#+)");
                }
                return;
            }
            if (Strings.equalsAny(str, new String[]{MULTI_LEVEL_WILDCARD, SINGLE_LEVEL_WILDCARD})) {
                return;
            }
            if (Strings.countMatches(str, MULTI_LEVEL_WILDCARD) > 1 || (str.contains(MULTI_LEVEL_WILDCARD) && !str.endsWith(MULTI_LEVEL_WILDCARD_PATTERN))) {
                throw new IllegalArgumentException("Invalid usage of multi-level wildcard in topic string: " + str);
            }
            validateSingleLevelWildcard(str);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e.getMessage());
        }
    }

    private static void validateSingleLevelWildcard(String str) {
        char cCharAt = SINGLE_LEVEL_WILDCARD.charAt(0);
        char cCharAt2 = "/".charAt(0);
        char[] charArray = str.toCharArray();
        int length = charArray.length;
        int i = 0;
        while (i < length) {
            int i2 = i - 1;
            char c = i2 >= 0 ? charArray[i2] : (char) 0;
            int i3 = i + 1;
            char c2 = i3 < length ? charArray[i3] : (char) 0;
            if (charArray[i] == cCharAt && ((c != cCharAt2 && c != 0) || (c2 != cCharAt2 && c2 != 0))) {
                throw new IllegalArgumentException(String.format("Invalid usage of single-level wildcard in topic string '%s'!", str));
            }
            i = i3;
        }
    }

    public static boolean isMatched(String str, String str2) throws IllegalArgumentException {
        int length = str2.length();
        int length2 = str.length();
        validate(str, true, true);
        validate(str2, false, true);
        if (str.equals(str2)) {
            return true;
        }
        int i = 0;
        int i2 = 0;
        while (true) {
            if (i < length2 && i2 < length) {
                if (str.charAt(i) != '#') {
                    if ((str2.charAt(i2) == '/' && str.charAt(i) != '/') || (str.charAt(i) != '+' && str.charAt(i) != '#' && str.charAt(i) != str2.charAt(i2))) {
                        break;
                    }
                    if (str.charAt(i) == '+') {
                        int i3 = i2 + 1;
                        while (i3 < length && str2.charAt(i3) != '/') {
                            i3 = i2 + 2;
                            i2++;
                        }
                    } else if (str.charAt(i) == '#') {
                        i2 = length - 1;
                    }
                    i++;
                    i2++;
                } else {
                    i2 = length;
                    i = length2;
                    break;
                }
            } else {
                break;
            }
        }
        if (i2 == length && i == length2) {
            return true;
        }
        if (str.length() - i > 0 && i2 == length) {
            if (str2.charAt(i2 - 1) == '/' && str.charAt(i) == '#') {
                return true;
            }
            if (str.length() - i > 1 && str.substring(i, i + 2).equals(MULTI_LEVEL_WILDCARD_PATTERN) && str.length() - str2.length() == 2 && str.substring(str.length() - 2, str.length()).equals(MULTI_LEVEL_WILDCARD_PATTERN)) {
                return true;
            }
        }
        return false;
    }
}
