package okhttp3.internal.publicsuffix;

import R4.k;
import R4.n;
import S4.p;
import e4.C;
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.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.jvm.internal.o;
import n2.AbstractC2612c;
import okhttp3.internal.Util;
import okhttp3.internal.platform.Platform;
import p5.AbstractC2732b;
import p5.J;
import x4.AbstractC3251q;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class PublicSuffixDatabase {

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public static final Companion f22598e = new Companion(0);

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public static final byte[] f22599f = {42};

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public static final List f22600g = AbstractC2612c.q("*");

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static final PublicSuffixDatabase f22601h = new PublicSuffixDatabase();

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final AtomicBoolean f22602a = new AtomicBoolean(false);

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final CountDownLatch f22603b = new CountDownLatch(1);

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public byte[] f22604c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public byte[] f22605d;

    public static final class Companion {
        public /* synthetic */ Companion(int i6) {
            this();
        }

        public static final String a(Companion companion, byte[] bArr, byte[][] bArr2, int i6) {
            int i7;
            boolean z6;
            int i8;
            int i9;
            int i10 = -1;
            companion.getClass();
            int length = bArr.length;
            int i11 = 0;
            while (i11 < length) {
                int i12 = (i11 + length) / 2;
                while (i12 > i10 && bArr[i12] != 10) {
                    i12 += i10;
                }
                int i13 = i12 + 1;
                int i14 = 1;
                while (true) {
                    i7 = i13 + i14;
                    if (bArr[i7] == 10) {
                        break;
                    }
                    i14++;
                }
                int i15 = i7 - i13;
                int i16 = i6;
                boolean z7 = false;
                int i17 = 0;
                int i18 = 0;
                while (true) {
                    if (z7) {
                        i8 = 46;
                        z6 = false;
                    } else {
                        byte b5 = bArr2[i16][i17];
                        byte[] bArr3 = Util.f22179a;
                        int i19 = b5 & 255;
                        z6 = z7;
                        i8 = i19;
                    }
                    byte b6 = bArr[i13 + i18];
                    byte[] bArr4 = Util.f22179a;
                    i9 = i8 - (b6 & 255);
                    if (i9 != 0) {
                        break;
                    }
                    i18++;
                    i17++;
                    if (i18 == i15) {
                        break;
                    }
                    if (bArr2[i16].length != i17) {
                        z7 = z6;
                    } else {
                        if (i16 == bArr2.length - 1) {
                            break;
                        }
                        i16++;
                        z7 = true;
                        i17 = -1;
                    }
                }
                if (i9 >= 0) {
                    if (i9 <= 0) {
                        int i20 = i15 - i18;
                        int length2 = bArr2[i16].length - i17;
                        int length3 = bArr2.length;
                        for (int i21 = i16 + 1; i21 < length3; i21++) {
                            length2 += bArr2[i21].length;
                        }
                        if (length2 >= i20) {
                            if (length2 <= i20) {
                                Charset UTF_8 = StandardCharsets.UTF_8;
                                o.g(UTF_8, "UTF_8");
                                return new String(bArr, i13, i15, UTF_8);
                            }
                        }
                        length = i12;
                    }
                    i11 = i7 + 1;
                } else {
                    length = i12;
                }
                i10 = -1;
            }
            return null;
        }

        private Companion() {
        }
    }

    public static List c(String str) {
        List listC1 = p.c1(str, new char[]{'.'}, 6);
        return o.c(AbstractC3251q.n0(listC1), "") ? AbstractC3251q.b0(listC1) : listC1;
    }

    public final String a(String str) {
        Companion companion;
        String strA;
        String strA2;
        String strA3;
        List listC1;
        int size;
        int size2;
        int i6 = 0;
        String unicodeDomain = IDN.toUnicode(str);
        o.g(unicodeDomain, "unicodeDomain");
        List listC = c(unicodeDomain);
        AtomicBoolean atomicBoolean = this.f22602a;
        if (atomicBoolean.get() || !atomicBoolean.compareAndSet(false, true)) {
            try {
                this.f22603b.await();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        } else {
            boolean z6 = false;
            while (true) {
                try {
                    try {
                        b();
                        break;
                    } catch (InterruptedIOException unused2) {
                        Thread.interrupted();
                        z6 = true;
                    } catch (IOException e6) {
                        Platform.Companion.get().log("Failed to read public suffix list", 5, e6);
                        if (z6) {
                        }
                    }
                } finally {
                    if (z6) {
                        Thread.currentThread().interrupt();
                    }
                }
            }
        }
        if (this.f22604c == null) {
            throw new IllegalStateException("Unable to load publicsuffixes.gz resource from the classpath.");
        }
        int size3 = listC.size();
        byte[][] bArr = new byte[size3][];
        for (int i7 = 0; i7 < size3; i7++) {
            String str2 = (String) listC.get(i7);
            Charset UTF_8 = StandardCharsets.UTF_8;
            o.g(UTF_8, "UTF_8");
            byte[] bytes = str2.getBytes(UTF_8);
            o.g(bytes, "this as java.lang.String).getBytes(charset)");
            bArr[i7] = bytes;
        }
        int i8 = 0;
        while (true) {
            companion = f22598e;
            if (i8 >= size3) {
                strA = null;
                break;
            }
            byte[] bArr2 = this.f22604c;
            if (bArr2 == null) {
                o.n("publicSuffixListBytes");
                throw null;
            }
            strA = Companion.a(companion, bArr2, bArr, i8);
            if (strA != null) {
                break;
            }
            i8++;
        }
        if (size3 > 1) {
            byte[][] bArr3 = (byte[][]) bArr.clone();
            int length = bArr3.length - 1;
            for (int i9 = 0; i9 < length; i9++) {
                bArr3[i9] = f22599f;
                byte[] bArr4 = this.f22604c;
                if (bArr4 == null) {
                    o.n("publicSuffixListBytes");
                    throw null;
                }
                strA2 = Companion.a(companion, bArr4, bArr3, i9);
                if (strA2 != null) {
                    break;
                }
            }
            strA2 = null;
        } else {
            strA2 = null;
        }
        if (strA2 != null) {
            int i10 = size3 - 1;
            for (int i11 = 0; i11 < i10; i11++) {
                byte[] bArr5 = this.f22605d;
                if (bArr5 == null) {
                    o.n("publicSuffixExceptionListBytes");
                    throw null;
                }
                strA3 = Companion.a(companion, bArr5, bArr, i11);
                if (strA3 != null) {
                    break;
                }
            }
            strA3 = null;
        } else {
            strA3 = null;
        }
        if (strA3 != null) {
            listC1 = p.c1("!".concat(strA3), new char[]{'.'}, 6);
        } else if (strA == null && strA2 == null) {
            listC1 = f22600g;
        } else {
            List listC12 = y.f26065a;
            List listC13 = strA != null ? p.c1(strA, new char[]{'.'}, 6) : listC12;
            if (strA2 != null) {
                listC12 = p.c1(strA2, new char[]{'.'}, 6);
            }
            listC1 = listC13.size() > listC12.size() ? listC13 : listC12;
        }
        if (listC.size() == listC1.size() && ((String) listC1.get(0)).charAt(0) != '!') {
            return null;
        }
        if (((String) listC1.get(0)).charAt(0) == '!') {
            size = listC.size();
            size2 = listC1.size();
        } else {
            size = listC.size();
            size2 = listC1.size() + 1;
        }
        k kVarZ = n.Z(AbstractC3251q.W(c(str)), size - size2);
        StringBuilder sb = new StringBuilder();
        sb.append((CharSequence) "");
        for (Object obj : kVarZ) {
            i6++;
            if (i6 > 1) {
                sb.append((CharSequence) ".");
            }
            C.l(sb, obj, null);
        }
        sb.append((CharSequence) "");
        return sb.toString();
    }

    public final void b() {
        try {
            InputStream resourceAsStream = PublicSuffixDatabase.class.getResourceAsStream("publicsuffixes.gz");
            if (resourceAsStream != null) {
                J jC = AbstractC2732b.c(new p5.y(AbstractC2732b.k(resourceAsStream)));
                try {
                    long jD = jC.d();
                    jC.A(jD);
                    byte[] bArrZ = jC.f23059b.z(jD);
                    long jD2 = jC.d();
                    jC.A(jD2);
                    byte[] bArrZ2 = jC.f23059b.z(jD2);
                    jC.close();
                    synchronized (this) {
                        this.f22604c = bArrZ;
                        this.f22605d = bArrZ2;
                    }
                } finally {
                }
            }
        } finally {
            this.f22603b.countDown();
        }
    }
}
