package gr;

import bn.c;
import com.google.firebase.perf.util.Constants;
import dr.h;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.net.IDN;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.jvm.internal.k;
import kotlin.text.d;
import kotlin.text.z;
import kr.f;
import kr.l;
import kr.o;
import rm.n0;
import rp.p;
import sm.c0;
import sm.t;
import sm.u;
import vq.m;

/* loaded from: classes3.dex */
public final class a {

    /* renamed from: e, reason: collision with root package name */
    public static final C0442a f20138e = new C0442a(null);

    /* renamed from: f, reason: collision with root package name */
    public static final String f20139f = a.class.getSimpleName() + ".gz";

    /* renamed from: g, reason: collision with root package name */
    private static final byte[] f20140g = {(byte) 42};

    /* renamed from: h, reason: collision with root package name */
    private static final List f20141h;

    /* renamed from: i, reason: collision with root package name */
    private static final a f20142i;

    /* renamed from: a, reason: collision with root package name */
    private final AtomicBoolean f20143a = new AtomicBoolean(false);

    /* renamed from: b, reason: collision with root package name */
    private final CountDownLatch f20144b = new CountDownLatch(1);

    /* renamed from: c, reason: collision with root package name */
    private byte[] f20145c;

    /* renamed from: d, reason: collision with root package name */
    private byte[] f20146d;

    /* renamed from: gr.a$a, reason: collision with other inner class name */
    /* loaded from: classes3.dex */
    public static final class C0442a {
        private C0442a() {
        }

        public /* synthetic */ C0442a(k kVar) {
            this();
        }

        /* JADX INFO: Access modifiers changed from: private */
        public final String b(byte[] bArr, byte[][] bArr2, int i10) {
            int i11;
            boolean z10;
            int b10;
            int b11;
            int length = bArr.length;
            int i12 = 0;
            while (i12 < length) {
                int i13 = (i12 + length) / 2;
                while (i13 > -1 && bArr[i13] != ((byte) 10)) {
                    i13--;
                }
                int i14 = i13 + 1;
                int i15 = 1;
                while (true) {
                    i11 = i14 + i15;
                    if (bArr[i11] == ((byte) 10)) {
                        break;
                    }
                    i15++;
                }
                int i16 = i11 - i14;
                int i17 = i10;
                boolean z11 = false;
                int i18 = 0;
                int i19 = 0;
                while (true) {
                    if (z11) {
                        b10 = 46;
                        z10 = false;
                    } else {
                        z10 = z11;
                        b10 = m.b(bArr2[i17][i18], Constants.MAX_HOST_LENGTH);
                    }
                    b11 = b10 - m.b(bArr[i14 + i19], Constants.MAX_HOST_LENGTH);
                    if (b11 != 0) {
                        break;
                    }
                    i19++;
                    i18++;
                    if (i19 == i16) {
                        break;
                    }
                    if (bArr2[i17].length != i18) {
                        z11 = z10;
                    } else {
                        if (i17 == bArr2.length - 1) {
                            break;
                        }
                        i17++;
                        i18 = -1;
                        z11 = true;
                    }
                }
                if (b11 >= 0) {
                    if (b11 <= 0) {
                        int i20 = i16 - i19;
                        int length2 = bArr2[i17].length - i18;
                        int length3 = bArr2.length;
                        for (int i21 = i17 + 1; i21 < length3; i21++) {
                            length2 += bArr2[i21].length;
                        }
                        if (length2 >= i20) {
                            if (length2 <= i20) {
                                return new String(bArr, i14, i16, d.f26484b);
                            }
                        }
                    }
                    i12 = i11 + 1;
                }
                length = i14 - 1;
            }
            return null;
        }

        public final a c() {
            return a.f20142i;
        }
    }

    static {
        List e10;
        e10 = t.e("*");
        f20141h = e10;
        f20142i = new a();
    }

    /* JADX WARN: Code restructure failed: missing block: B:57:0x00d9, code lost:
    
        r1 = kotlin.text.z.H0(r8, new char[]{'.'}, false, 0, 6, null);
     */
    /* JADX WARN: Code restructure failed: missing block: B:60:0x00ef, code lost:
    
        r0 = kotlin.text.z.H0(r5, new char[]{'.'}, false, 0, 6, null);
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    private final List b(List list) {
        String str;
        String str2;
        String str3;
        List k10;
        List k11;
        List H0;
        if (this.f20143a.get() || !this.f20143a.compareAndSet(false, true)) {
            try {
                this.f20144b.await();
            } catch (InterruptedException unused) {
                Thread.currentThread().interrupt();
            }
        } else {
            e();
        }
        if (!(this.f20145c != null)) {
            throw new IllegalStateException(("Unable to load " + f20139f + " resource from the classpath.").toString());
        }
        int size = list.size();
        byte[][] bArr = new byte[size];
        for (int i10 = 0; i10 < size; i10++) {
            byte[] bytes = ((String) list.get(i10)).getBytes(d.f26484b);
            kotlin.jvm.internal.t.f(bytes, "this as java.lang.String).getBytes(charset)");
            bArr[i10] = bytes;
        }
        int i11 = 0;
        while (true) {
            str = null;
            if (i11 >= size) {
                str2 = null;
                break;
            }
            C0442a c0442a = f20138e;
            byte[] bArr2 = this.f20145c;
            if (bArr2 == null) {
                kotlin.jvm.internal.t.y("publicSuffixListBytes");
                bArr2 = null;
            }
            str2 = c0442a.b(bArr2, bArr, i11);
            if (str2 != null) {
                break;
            }
            i11++;
        }
        if (size > 1) {
            byte[][] bArr3 = (byte[][]) bArr.clone();
            int length = bArr3.length - 1;
            for (int i12 = 0; i12 < length; i12++) {
                bArr3[i12] = f20140g;
                C0442a c0442a2 = f20138e;
                byte[] bArr4 = this.f20145c;
                if (bArr4 == null) {
                    kotlin.jvm.internal.t.y("publicSuffixListBytes");
                    bArr4 = null;
                }
                String b10 = c0442a2.b(bArr4, bArr3, i12);
                if (b10 != null) {
                    str3 = b10;
                    break;
                }
            }
        }
        str3 = null;
        if (str3 != null) {
            int i13 = size - 1;
            int i14 = 0;
            while (true) {
                if (i14 >= i13) {
                    break;
                }
                C0442a c0442a3 = f20138e;
                byte[] bArr5 = this.f20146d;
                if (bArr5 == null) {
                    kotlin.jvm.internal.t.y("publicSuffixExceptionListBytes");
                    bArr5 = null;
                }
                String b11 = c0442a3.b(bArr5, bArr, i14);
                if (b11 != null) {
                    str = b11;
                    break;
                }
                i14++;
            }
        }
        if (str != null) {
            H0 = z.H0('!' + str, new char[]{'.'}, false, 0, 6, null);
            return H0;
        }
        if (str2 == null && str3 == null) {
            return f20141h;
        }
        if (str2 == null || k10 == null) {
            k10 = u.k();
        }
        if (str3 == null || k11 == null) {
            k11 = u.k();
        }
        return k10.size() > k11.size() ? k10 : k11;
    }

    private final void d() {
        InputStream resourceAsStream = a.class.getResourceAsStream(f20139f);
        if (resourceAsStream == null) {
            return;
        }
        f b10 = o.b(new l(o.e(resourceAsStream)));
        try {
            byte[] v02 = b10.v0(b10.readInt());
            byte[] v03 = b10.v0(b10.readInt());
            n0 n0Var = n0.f34904a;
            c.a(b10, null);
            synchronized (this) {
                kotlin.jvm.internal.t.d(v02);
                this.f20145c = v02;
                kotlin.jvm.internal.t.d(v03);
                this.f20146d = v03;
            }
            this.f20144b.countDown();
        } finally {
        }
    }

    private final void e() {
        boolean z10 = false;
        while (true) {
            try {
                try {
                    d();
                    break;
                } catch (InterruptedIOException unused) {
                    Thread.interrupted();
                    z10 = true;
                } catch (IOException e10) {
                    h.f16598a.g().j("Failed to read public suffix list", 5, e10);
                    if (z10) {
                        Thread.currentThread().interrupt();
                        return;
                    }
                    return;
                }
            } catch (Throwable th2) {
                if (z10) {
                    Thread.currentThread().interrupt();
                }
                throw th2;
            }
        }
        if (z10) {
            Thread.currentThread().interrupt();
        }
    }

    private final List f(String str) {
        List H0;
        Object o02;
        List X;
        H0 = z.H0(str, new char[]{'.'}, false, 0, 6, null);
        o02 = c0.o0(H0);
        if (!kotlin.jvm.internal.t.b(o02, "")) {
            return H0;
        }
        X = c0.X(H0, 1);
        return X;
    }

    public final String c(String domain) {
        int size;
        int size2;
        rp.h S;
        rp.h n10;
        String v10;
        kotlin.jvm.internal.t.g(domain, "domain");
        String unicodeDomain = IDN.toUnicode(domain);
        kotlin.jvm.internal.t.f(unicodeDomain, "unicodeDomain");
        List f10 = f(unicodeDomain);
        List b10 = b(f10);
        if (f10.size() == b10.size() && ((String) b10.get(0)).charAt(0) != '!') {
            return null;
        }
        if (((String) b10.get(0)).charAt(0) == '!') {
            size = f10.size();
            size2 = b10.size();
        } else {
            size = f10.size();
            size2 = b10.size() + 1;
        }
        S = c0.S(f(domain));
        n10 = p.n(S, size - size2);
        v10 = p.v(n10, ".", null, null, 0, null, null, 62, null);
        return v10;
    }
}
