package okhttp3.internal.publicsuffix; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.net.IDN; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicBoolean; import net.sf.scuba.smartcards.ISO7816; import o.C14851gaI; import o.C14866gag; import o.C14881gav; import o.C14953gcr; import o.C14957gcv; import o.C14964gdG; import o.C14994gdz; import o.C15017gel; import o.C15157gil; import o.C15160gio; import o.InterfaceC15149gid; import o.gaG; import o.gbF; import okhttp3.internal.Util; import okhttp3.internal.platform.Platform; /* loaded from: classes.dex */ public final class PublicSuffixDatabase { private static final char EXCEPTION_MARKER = '!'; private static final List PREVAILING_RULE; public static final String PUBLIC_SUFFIX_RESOURCE = "publicsuffixes.gz"; private static final PublicSuffixDatabase instance; private byte[] publicSuffixExceptionListBytes; private byte[] publicSuffixListBytes; public static final Companion Companion = new Companion(null); private static final byte[] WILDCARD_LABEL = {ISO7816.INS_PSO}; private final AtomicBoolean listRead = new AtomicBoolean(false); private final CountDownLatch readCompleteLatch = new CountDownLatch(1); public final String getEffectiveTldPlusOne(String str) { int size; int size2; C14957gcv.e(str, ""); String unicode = IDN.toUnicode(str); C14957gcv.c((Object) unicode, ""); List splitDomain = splitDomain(unicode); List findMatchingRule = findMatchingRule(splitDomain); if (splitDomain.size() == findMatchingRule.size() && findMatchingRule.get(0).charAt(0) != '!') { return null; } if (findMatchingRule.get(0).charAt(0) == '!') { size = splitDomain.size(); size2 = findMatchingRule.size(); } else { size = splitDomain.size(); size2 = findMatchingRule.size() + 1; } List splitDomain2 = splitDomain(str); C14957gcv.e(splitDomain2, ""); C14994gdz ieS = new gaG.IeS(splitDomain2); int i = size - size2; C14957gcv.e(ieS, ""); if (i < 0) { StringBuilder sb = new StringBuilder("Requested element count "); sb.append(i); sb.append(" is less than zero."); throw new IllegalArgumentException(sb.toString().toString()); } if (i != 0) { ieS = new C14994gdz(ieS, i); } return C14964gdG.e(ieS, ".", "", "", "..."); } private final List splitDomain(String str) { String str2 = str; char[] cArr = {'.'}; C14957gcv.e(str2, ""); C14957gcv.e(cArr, ""); List c = C15017gel.c((CharSequence) str2, String.valueOf(cArr[0]), false, 0); if (!C14957gcv.b(C14881gav.d((List) c), (Object) "")) { return c; } C14957gcv.e(c, ""); List list = c; int size = c.size() - 1; return C14881gav.c(list, size >= 0 ? size : 0); } private final List findMatchingRule(List list) { C14851gaI c14851gaI; String str; String str2; String str3; C14851gaI c; if (!this.listRead.get() && this.listRead.compareAndSet(false, true)) { readTheListUninterruptibly(); } else { try { this.readCompleteLatch.await(); } catch (InterruptedException unused) { Thread.currentThread().interrupt(); } } if (this.publicSuffixListBytes == null) { throw new IllegalStateException("Unable to load publicsuffixes.gz resource from the classpath.".toString()); } int size = list.size(); byte[][] bArr = new byte[size]; for (int i = 0; i < size; i++) { String str4 = list.get(i); Charset charset = StandardCharsets.UTF_8; C14957gcv.c(charset, ""); byte[] bytes = str4.getBytes(charset); C14957gcv.c(bytes, ""); bArr[i] = bytes; } byte[][] bArr2 = bArr; int length = bArr2.length; int i2 = 0; while (true) { c14851gaI = null; if (i2 >= length) { str = null; break; } Companion companion = Companion; byte[] bArr3 = this.publicSuffixListBytes; if (bArr3 == null) { C14957gcv.a(""); bArr3 = null; } str = companion.binarySearch(bArr3, bArr, i2); if (str != null) { break; } i2++; } if (bArr2.length > 1) { byte[][] bArr4 = (byte[][]) bArr2.clone(); int length2 = bArr4.length; for (int i3 = 0; i3 < length2 - 1; i3++) { bArr4[i3] = WILDCARD_LABEL; Companion companion2 = Companion; byte[] bArr5 = this.publicSuffixListBytes; if (bArr5 == null) { C14957gcv.a(""); bArr5 = null; } str2 = companion2.binarySearch(bArr5, bArr4, i3); if (str2 != null) { break; } } } str2 = null; if (str2 != null) { int length3 = bArr2.length; for (int i4 = 0; i4 < length3 - 1; i4++) { Companion companion3 = Companion; byte[] bArr6 = this.publicSuffixExceptionListBytes; if (bArr6 == null) { C14957gcv.a(""); bArr6 = null; } str3 = companion3.binarySearch(bArr6, bArr, i4); if (str3 != null) { break; } } } str3 = null; if (str3 != null) { String c2 = C14957gcv.c("!", (Object) str3); char[] cArr = {'.'}; C14957gcv.e(c2, ""); C14957gcv.e(cArr, ""); return C15017gel.c((CharSequence) c2, String.valueOf(cArr[0]), false, 0); } if (str == null && str2 == null) { return PREVAILING_RULE; } if (str == null) { c = null; } else { String str5 = str; char[] cArr2 = {'.'}; C14957gcv.e(str5, ""); C14957gcv.e(cArr2, ""); c = C15017gel.c((CharSequence) str5, String.valueOf(cArr2[0]), false, 0); } if (c == null) { c = C14851gaI.c; } if (str2 != null) { String str6 = str2; char[] cArr3 = {'.'}; C14957gcv.e(str6, ""); C14957gcv.e(cArr3, ""); c14851gaI = C15017gel.c((CharSequence) str6, String.valueOf(cArr3[0]), false, 0); } if (c14851gaI == null) { c14851gaI = C14851gaI.c; } return c.size() <= c14851gaI.size() ? c14851gaI : c; } private final void readTheListUninterruptibly() { boolean z = false; while (true) { try { try { readTheList(); break; } catch (InterruptedIOException unused) { Thread.interrupted(); z = true; } catch (IOException e) { Platform.Companion.get().log("Failed to read public suffix list", 5, e); if (z) { Thread.currentThread().interrupt(); return; } return; } } catch (Throwable th) { if (z) { Thread.currentThread().interrupt(); } throw th; } } if (z) { Thread.currentThread().interrupt(); } } private final void readTheList() throws IOException { InputStream resourceAsStream = PublicSuffixDatabase.class.getResourceAsStream(PUBLIC_SUFFIX_RESOURCE); if (resourceAsStream == null) { return; } InterfaceC15149gid c = C15160gio.c(new C15157gil(C15160gio.e(resourceAsStream))); try { InterfaceC15149gid interfaceC15149gid = c; byte[] e = interfaceC15149gid.e(interfaceC15149gid.n()); byte[] e2 = interfaceC15149gid.e(interfaceC15149gid.n()); C14866gag c14866gag = C14866gag.c; gbF.d(c, null); synchronized (this) { C14957gcv.e(e); this.publicSuffixListBytes = e; C14957gcv.e(e2); this.publicSuffixExceptionListBytes = e2; C14866gag c14866gag2 = C14866gag.c; } this.readCompleteLatch.countDown(); } finally { } } public final void setListBytes(byte[] bArr, byte[] bArr2) { C14957gcv.e(bArr, ""); C14957gcv.e(bArr2, ""); this.publicSuffixListBytes = bArr; this.publicSuffixExceptionListBytes = bArr2; this.listRead.set(true); this.readCompleteLatch.countDown(); } /* loaded from: classes.dex */ public static final class Companion { private Companion() { } public final PublicSuffixDatabase get() { return PublicSuffixDatabase.instance; } /* JADX INFO: Access modifiers changed from: private */ public final String binarySearch(byte[] bArr, byte[][] bArr2, int i) { int i2; boolean z; int and; int and2; int length = bArr.length; int i3 = 0; while (i3 < length) { int i4 = (i3 + length) / 2; while (i4 >= 0 && bArr[i4] != 10) { i4--; } int i5 = i4 + 1; int i6 = 1; while (true) { i2 = i5 + i6; if (bArr[i2] == 10) { break; } i6++; } int i7 = i2 - i5; int i8 = i; boolean z2 = false; int i9 = 0; int i10 = 0; while (true) { if (z2) { and = 46; z = false; } else { z = z2; and = Util.and(bArr2[i8][i9], 255); } and2 = and - Util.and(bArr[i5 + i10], 255); if (and2 != 0) { break; } i10++; i9++; if (i10 == i7) { break; } if (bArr2[i8].length != i9) { z2 = z; } else { if (i8 == bArr2.length - 1) { break; } i8++; i9 = -1; z2 = true; } } if (and2 >= 0) { if (and2 <= 0) { int i11 = i7 - i10; int length2 = bArr2[i8].length - i9; int length3 = bArr2.length; for (int i12 = i8 + 1; i12 < length3; i12++) { length2 += bArr2[i12].length; } if (length2 >= i11) { if (length2 <= i11) { Charset charset = StandardCharsets.UTF_8; C14957gcv.c(charset, ""); return new String(bArr, i5, i7, charset); } } } i3 = i2 + 1; } length = i4; } return null; } public /* synthetic */ Companion(C14953gcr c14953gcr) { this(); } } static { List singletonList = Collections.singletonList("*"); C14957gcv.c(singletonList, ""); PREVAILING_RULE = singletonList; instance = new PublicSuffixDatabase(); } }