package okhttp3.internal;

import A0.a;
import S4.p;
import S4.w;
import com.revenuecat.purchases.common.Constants;
import java.net.IDN;
import java.net.InetAddress;
import java.util.Locale;
import kotlin.jvm.internal.o;
import p5.C2740j;

/* JADX INFO: loaded from: classes3.dex */
public final class HostnamesKt {
    /* JADX WARN: Code restructure failed: missing block: B:64:0x00cb, code lost:
    
        if (r7 == 16) goto L69;
     */
    /* JADX WARN: Code restructure failed: missing block: B:65:0x00cd, code lost:
    
        if (r8 != (-1)) goto L68;
     */
    /* JADX WARN: Code restructure failed: missing block: B:68:0x00d1, code lost:
    
        r0 = r7 - r8;
        java.lang.System.arraycopy(r3, r8, r3, 16 - r0, r0);
        java.util.Arrays.fill(r3, r8, (16 - r7) + r8, (byte) 0);
     */
    /* JADX WARN: Code restructure failed: missing block: B:70:0x00e1, code lost:
    
        return java.net.InetAddress.getByAddress(r3);
     */
    /* JADX WARN: Code restructure failed: missing block: B:90:?, code lost:
    
        return null;
     */
    /* JADX WARN: Removed duplicated region for block: B:55:0x00a2  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static final InetAddress a(int i6, int i7, String str) {
        int i8;
        byte[] bArr = new byte[16];
        int i9 = i6;
        int i10 = 0;
        int i11 = -1;
        int i12 = -1;
        while (true) {
            if (i9 >= i7) {
                break;
            }
            if (i10 == 16) {
                return null;
            }
            int i13 = i9 + 2;
            if (i13 <= i7 && w.z0(str, "::", i9, false)) {
                if (i11 != -1) {
                    return null;
                }
                i10 += 2;
                i11 = i10;
                if (i13 == i7) {
                    break;
                }
                i12 = i13;
                int i14 = 0;
                i9 = i12;
                while (i9 < i7) {
                }
                i8 = i9 - i12;
                return i8 == 0 ? null : null;
            }
            if (i10 != 0) {
                if (w.z0(str, Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, i9, false)) {
                    i9++;
                } else {
                    if (!w.z0(str, ".", i9, false)) {
                        return null;
                    }
                    int i15 = i10 - 2;
                    int i16 = i15;
                    while (i12 < i7) {
                        if (i16 == 16) {
                            return null;
                        }
                        if (i16 != i15) {
                            if (str.charAt(i12) != '.') {
                                return null;
                            }
                            i12++;
                        }
                        int i17 = 0;
                        int i18 = i12;
                        while (i18 < i7) {
                            char cCharAt = str.charAt(i18);
                            if (o.i(cCharAt, 48) < 0 || o.i(cCharAt, 57) > 0) {
                                break;
                            }
                            if ((i17 == 0 && i12 != i18) || ((i17 * 10) + cCharAt) - 48 > 255) {
                                return null;
                            }
                            i18++;
                        }
                        if (i18 - i12 == 0) {
                            return null;
                        }
                        bArr[i16] = (byte) i17;
                        i16++;
                        i12 = i18;
                    }
                    if (i16 != i10 + 2) {
                        return null;
                    }
                    i10 += 2;
                }
            }
            i12 = i9;
            int i142 = 0;
            i9 = i12;
            while (i9 < i7) {
                int iQ = Util.q(str.charAt(i9));
                if (iQ == -1) {
                    break;
                }
                i142 = (i142 << 4) + iQ;
                i9++;
            }
            i8 = i9 - i12;
            if (i8 == 0 && i8 <= 4) {
                int i19 = i10 + 1;
                bArr[i10] = (byte) (255 & (i142 >>> 8));
                i10 += 2;
                bArr[i19] = (byte) (i142 & 255);
            }
        }
    }

    public static final String b(String str) {
        o.h(str, "<this>");
        int i6 = 0;
        int i7 = -1;
        if (!p.D0(str, Constants.SUBS_ID_BASE_PLAN_ID_SEPARATOR, false)) {
            try {
                String ascii = IDN.toASCII(str);
                o.g(ascii, "toASCII(host)");
                Locale US = Locale.US;
                o.g(US, "US");
                String lowerCase = ascii.toLowerCase(US);
                o.g(lowerCase, "this as java.lang.String).toLowerCase(locale)");
                if (lowerCase.length() == 0) {
                    return null;
                }
                int length = lowerCase.length();
                for (int i8 = 0; i8 < length; i8++) {
                    char cCharAt = lowerCase.charAt(i8);
                    if (o.i(cCharAt, 31) <= 0 || o.i(cCharAt, 127) >= 0 || p.N0(" #%/:?@[\\]", cCharAt, 0, 6) != -1) {
                        return null;
                    }
                }
                return lowerCase;
            } catch (IllegalArgumentException unused) {
                return null;
            }
        }
        InetAddress inetAddressA = (w.A0(str, "[", false) && w.s0(str, "]", false)) ? a(1, str.length() - 1, str) : a(0, str.length(), str);
        if (inetAddressA == null) {
            return null;
        }
        byte[] address = inetAddressA.getAddress();
        if (address.length != 16) {
            if (address.length == 4) {
                return inetAddressA.getHostAddress();
            }
            throw new AssertionError(a.k('\'', "Invalid IPv6 address: '", str));
        }
        int i9 = 0;
        int i10 = 0;
        while (i9 < address.length) {
            int i11 = i9;
            while (i11 < 16 && address[i11] == 0 && address[i11 + 1] == 0) {
                i11 += 2;
            }
            int i12 = i11 - i9;
            if (i12 > i10 && i12 >= 4) {
                i7 = i9;
                i10 = i12;
            }
            i9 = i11 + 2;
        }
        C2740j c2740j = new C2740j();
        while (i6 < address.length) {
            if (i6 == i7) {
                c2740j.Q(58);
                i6 += i10;
                if (i6 == 16) {
                    c2740j.Q(58);
                }
            } else {
                if (i6 > 0) {
                    c2740j.Q(58);
                }
                byte b5 = address[i6];
                byte[] bArr = Util.f22179a;
                c2740j.S(((b5 & 255) << 8) | (address[i6 + 1] & 255));
                i6 += 2;
            }
        }
        return c2740j.H();
    }
}
