package okhttp3.internal.publicsuffix; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.net.IDN; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import okhttp3.internal.Util; import okhttp3.internal.platform.Platform; import okio.BufferedSource; import okio.GzipSource; import okio.Okio; /* loaded from: classes2.dex */ public final class PublicSuffixDatabase { private static final byte EXCEPTION_MARKER = 33; public static final String PUBLIC_SUFFIX_RESOURCE = "publicsuffixes.gz"; private byte[] publicSuffixExceptionListBytes; private byte[] publicSuffixListBytes; private static final byte[] WILDCARD_LABEL = {42}; private static final String[] EMPTY_RULE = new String[0]; private static final String[] PREVAILING_RULE = {"*"}; private static final PublicSuffixDatabase instance = new PublicSuffixDatabase(); private final AtomicBoolean listRead = new AtomicBoolean(false); private final CountDownLatch readCompleteLatch = new CountDownLatch(1); private static String binarySearchBytes(byte[] bArr, byte[][] bArr2, int i) { int i2; int i3; int i4; int length = bArr.length; int i5 = 0; while (i5 < length) { int i6 = (i5 + length) / 2; while (i6 > -1 && bArr[i6] != 10) { i6--; } int i7 = i6 + 1; int i8 = 1; while (true) { i2 = i7 + i8; if (bArr[i2] == 10) { break; } i8++; } int i9 = i2 - i7; int i10 = i; boolean z = false; int i11 = 0; int i12 = 0; while (true) { if (z) { z = false; i3 = 46; } else { i3 = bArr2[i10][i11] & 255; } i4 = i3 - (bArr[i7 + i12] & 255); if (i4 == 0) { i12++; i11++; if (i12 == i9) { break; } if (bArr2[i10].length == i11) { if (i10 == bArr2.length - 1) { break; } i10++; z = true; i11 = -1; } } else { break; } } if (i4 >= 0) { if (i4 <= 0) { int i13 = i9 - i12; int length2 = bArr2[i10].length - i11; while (true) { i10++; if (i10 >= bArr2.length) { break; } length2 += bArr2[i10].length; } if (length2 >= i13) { if (length2 <= i13) { return new String(bArr, i7, i9, Util.UTF_8); } } } i5 = i2 + 1; } length = i7 - 1; } return null; } private String[] findMatchingRule(String[] strArr) { String str; String str2; String str3; if (this.listRead.get() || !this.listRead.compareAndSet(false, true)) { try { this.readCompleteLatch.await(); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } } else { readTheListUninterruptibly(); } synchronized (this) { if (this.publicSuffixListBytes == null) { throw new IllegalStateException("Unable to load publicsuffixes.gz resource from the classpath."); } } byte[][] bArr = new byte[strArr.length][]; for (int i = 0; i < strArr.length; i++) { bArr[i] = strArr[i].getBytes(Util.UTF_8); } int i2 = 0; while (true) { if (i2 >= bArr.length) { str = null; break; } str = binarySearchBytes(this.publicSuffixListBytes, bArr, i2); if (str != null) { break; } i2++; } if (bArr.length > 1) { byte[][] bArr2 = (byte[][]) bArr.clone(); for (int i3 = 0; i3 < bArr2.length - 1; i3++) { bArr2[i3] = WILDCARD_LABEL; str2 = binarySearchBytes(this.publicSuffixListBytes, bArr2, i3); if (str2 != null) { break; } } } str2 = null; if (str2 != null) { for (int i4 = 0; i4 < bArr.length - 1; i4++) { str3 = binarySearchBytes(this.publicSuffixExceptionListBytes, bArr, i4); if (str3 != null) { break; } } } str3 = null; if (str3 != null) { return ("!" + str3).split("\\."); } if (str == null && str2 == null) { return PREVAILING_RULE; } String[] split = str != null ? str.split("\\.") : EMPTY_RULE; String[] split2 = str2 != null ? str2.split("\\.") : EMPTY_RULE; return split.length > split2.length ? split : split2; } public static PublicSuffixDatabase get() { return instance; } private void readTheList() throws IOException { InputStream resourceAsStream = PublicSuffixDatabase.class.getResourceAsStream(PUBLIC_SUFFIX_RESOURCE); if (resourceAsStream == null) { return; } BufferedSource buffer = Okio.buffer(new GzipSource(Okio.source(resourceAsStream))); try { byte[] bArr = new byte[buffer.readInt()]; buffer.readFully(bArr); byte[] bArr2 = new byte[buffer.readInt()]; buffer.readFully(bArr2); synchronized (this) { this.publicSuffixListBytes = bArr; this.publicSuffixExceptionListBytes = bArr2; } this.readCompleteLatch.countDown(); } finally { Util.closeQuietly(buffer); } } private void readTheListUninterruptibly() { boolean z = false; while (true) { try { try { readTheList(); break; } catch (InterruptedIOException unused) { z = true; } catch (IOException e) { Platform.get().log(5, "Failed to read public suffix list", e); if (z) { Thread.currentThread().interrupt(); return; } return; } } catch (Throwable th) { if (z) { Thread.currentThread().interrupt(); } throw th; } } if (z) { Thread.currentThread().interrupt(); } } public String getEffectiveTldPlusOne(String str) { int length; int length2; if (str == null) { throw new NullPointerException("domain == null"); } String[] split = IDN.toUnicode(str).split("\\."); String[] findMatchingRule = findMatchingRule(split); if (split.length == findMatchingRule.length && findMatchingRule[0].charAt(0) != '!') { return null; } if (findMatchingRule[0].charAt(0) == '!') { length = split.length; length2 = findMatchingRule.length; } else { length = split.length; length2 = findMatchingRule.length + 1; } StringBuilder sb = new StringBuilder(); String[] split2 = str.split("\\."); for (int i = length - length2; i < split2.length; i++) { sb.append(split2[i]); sb.append('.'); } sb.deleteCharAt(sb.length() - 1); return sb.toString(); } void setListBytes(byte[] bArr, byte[] bArr2) { this.publicSuffixListBytes = bArr; this.publicSuffixExceptionListBytes = bArr2; this.listRead.set(true); this.readCompleteLatch.countDown(); } }