package okhttp3.internal.publicsuffix; import androidx.webkit.ProxyConfig; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.net.IDN; import java.nio.charset.StandardCharsets; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import okhttp3.internal.platform.Platform; import okio.BufferedSource; import okio.GzipSource; import okio.Okio; /* loaded from: classes5.dex */ public final class PublicSuffixDatabase { public byte[] publicSuffixExceptionListBytes; public byte[] publicSuffixListBytes; public static final byte[] WILDCARD_LABEL = {42}; public static final String[] EMPTY_RULE = new String[0]; public static final String[] PREVAILING_RULE = {ProxyConfig.MATCH_ALL_SCHEMES}; public static final PublicSuffixDatabase instance = new PublicSuffixDatabase(); public final AtomicBoolean listRead = new AtomicBoolean(false); public final CountDownLatch readCompleteLatch = new CountDownLatch(1); public static PublicSuffixDatabase get() { return instance; } 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(); } public final String[] findMatchingRule(String[] strArr) { String str; String str2; String str3; int i = 0; if (!this.listRead.get() && this.listRead.compareAndSet(false, true)) { readTheListUninterruptibly(); } else { try { this.readCompleteLatch.await(); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } } synchronized (this) { if (this.publicSuffixListBytes == null) { throw new IllegalStateException("Unable to load publicsuffixes.gz resource from the classpath."); } } int length = strArr.length; byte[][] bArr = new byte[length][]; for (int i2 = 0; i2 < strArr.length; i2++) { bArr[i2] = strArr[i2].getBytes(StandardCharsets.UTF_8); } int i3 = 0; while (true) { str = null; if (i3 >= length) { str2 = null; break; } str2 = binarySearchBytes(this.publicSuffixListBytes, bArr, i3); if (str2 != null) { break; } i3++; } if (length > 1) { byte[][] bArr2 = (byte[][]) bArr.clone(); for (int i4 = 0; i4 < bArr2.length - 1; i4++) { bArr2[i4] = WILDCARD_LABEL; str3 = binarySearchBytes(this.publicSuffixListBytes, bArr2, i4); if (str3 != null) { break; } } } str3 = null; if (str3 != null) { while (true) { if (i >= length - 1) { break; } String binarySearchBytes = binarySearchBytes(this.publicSuffixExceptionListBytes, bArr, i); if (binarySearchBytes != null) { str = binarySearchBytes; break; } i++; } } if (str != null) { return ("!" + str).split("\\."); } if (str2 == null && str3 == null) { return PREVAILING_RULE; } String[] split = str2 != null ? str2.split("\\.") : EMPTY_RULE; String[] split2 = str3 != null ? str3.split("\\.") : EMPTY_RULE; return split.length > split2.length ? split : split2; } public static String binarySearchBytes(byte[] bArr, byte[][] bArr2, int i) { int i2; boolean z; 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 z2 = false; int i11 = 0; int i12 = 0; while (true) { if (z2) { i3 = 46; z = false; } else { z = z2; 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) { z2 = z; } else { if (i10 == bArr2.length - 1) { break; } i10++; i11 = -1; z2 = true; } } 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, StandardCharsets.UTF_8); } } } i5 = i2 + 1; } length = i6; } return null; } public final void readTheListUninterruptibly() { boolean z = false; while (true) { try { try { readTheList(); break; } catch (InterruptedIOException unused) { Thread.interrupted(); 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 final void readTheList() { InputStream resourceAsStream = PublicSuffixDatabase.class.getResourceAsStream(com.mbridge.msdk.thrid.okhttp.internal.publicsuffix.PublicSuffixDatabase.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); buffer.close(); synchronized (this) { this.publicSuffixListBytes = bArr; this.publicSuffixExceptionListBytes = bArr2; } this.readCompleteLatch.countDown(); } catch (Throwable th) { try { throw th; } catch (Throwable th2) { if (buffer != null) { try { buffer.close(); } catch (Throwable th3) { th.addSuppressed(th3); } } throw th2; } } } }