what-the-bank/sources/org/bouncycastle/util/IPAddress.java

111 lines
3.4 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.util;
/* loaded from: classes6.dex */
public class IPAddress {
public static boolean isValidWithNetMask(String str) {
return isValidIPv4WithNetmask(str) || isValidIPv6WithNetmask(str);
}
public static boolean isValidIPv6WithNetmask(String str) {
int indexOf = str.indexOf("/");
String substring = str.substring(indexOf + 1);
if (indexOf <= 0 || !isValidIPv6(str.substring(0, indexOf))) {
return false;
}
return isValidIPv6(substring) || isMaskValue(substring, 128);
}
public static boolean isValidIPv6(String str) {
int indexOf;
if (str.length() == 0) {
return false;
}
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.append(":");
String obj = sb.toString();
int i = 0;
int i2 = 0;
boolean z = false;
while (i < obj.length() && (indexOf = obj.indexOf(58, i)) >= i) {
if (i2 == 8) {
return false;
}
if (i != indexOf) {
String substring = obj.substring(i, indexOf);
if (indexOf != obj.length() - 1 || substring.indexOf(46) <= 0) {
try {
int parseInt = Integer.parseInt(obj.substring(i, indexOf), 16);
if (parseInt >= 0 && parseInt <= 65535) {
}
} catch (NumberFormatException unused) {
}
return false;
}
if (!isValidIPv4(substring)) {
return false;
}
i2++;
} else {
if (indexOf != 1 && indexOf != obj.length() - 1 && z) {
return false;
}
z = true;
}
i = indexOf + 1;
i2++;
}
return i2 == 8 || z;
}
public static boolean isValidIPv4WithNetmask(String str) {
int indexOf = str.indexOf("/");
String substring = str.substring(indexOf + 1);
if (indexOf <= 0 || !isValidIPv4(str.substring(0, indexOf))) {
return false;
}
return isValidIPv4(substring) || isMaskValue(substring, 32);
}
public static boolean isValidIPv4(String str) {
int indexOf;
if (str.length() == 0) {
return false;
}
StringBuilder sb = new StringBuilder();
sb.append(str);
sb.append(".");
String obj = sb.toString();
int i = 0;
int i2 = 0;
while (i < obj.length() && (indexOf = obj.indexOf(46, i)) > i) {
if (i2 == 4) {
return false;
}
try {
int parseInt = Integer.parseInt(obj.substring(i, indexOf));
if (parseInt >= 0 && parseInt <= 255) {
i = indexOf + 1;
i2++;
}
} catch (NumberFormatException unused) {
}
return false;
}
return i2 == 4;
}
public static boolean isValid(String str) {
return isValidIPv4(str) || isValidIPv6(str);
}
private static boolean isMaskValue(String str, int i) {
try {
int parseInt = Integer.parseInt(str);
return parseInt >= 0 && parseInt <= i;
} catch (NumberFormatException unused) {
return false;
}
}
}