package defpackage;

import com.hierynomus.protocol.transport.TransportException;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.Serializable;
import java.net.Socket;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
import javax.net.SocketFactory;

/* JADX INFO: renamed from: ᵹᵶᵗ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes3.dex */
public final class C5096 {

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public int f19198;

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public Object f19199;

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public Object f19200;

    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    public Serializable f19201;

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public final /* synthetic */ int f19202;

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public Object f19203;

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public Object f19204;

    /* JADX INFO: renamed from: ᵻʽᵯ, reason: contains not printable characters */
    public Object f19205;

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public Object f19206;

    public C5096(int i) {
        this.f19202 = i;
        switch (i) {
            case 2:
                this.f19204 = "";
                this.f19201 = "";
                this.f19198 = -1;
                this.f19203 = AbstractC0892.m3129("");
                break;
        }
    }

    public C5096(SocketFactory socketFactory, int i, C2994 c2994) {
        this.f19202 = 0;
        this.f19200 = AbstractC2626.m6386(C5096.class);
        this.f19201 = new ReentrantLock();
        new C2735();
        this.f19198 = i;
        this.f19206 = socketFactory;
        this.f19204 = c2994;
    }

    /* JADX INFO: renamed from: ᵻˈᵦʼ, reason: contains not printable characters */
    public static ArrayList m10578(String str) {
        ArrayList arrayList = new ArrayList();
        int i = 0;
        while (i <= str.length()) {
            int iM8497 = AbstractC3801.m8497(str, '&', i, 4);
            if (iM8497 == -1) {
                iM8497 = str.length();
            }
            int iM84972 = AbstractC3801.m8497(str, '=', i, 4);
            if (iM84972 == -1 || iM84972 > iM8497) {
                arrayList.add(str.substring(i, iM8497));
                arrayList.add(null);
            } else {
                arrayList.add(str.substring(i, iM84972));
                arrayList.add(str.substring(iM84972 + 1, iM8497));
            }
            i = iM8497 + 1;
        }
        return arrayList;
    }

    /* JADX WARN: Removed duplicated region for block: B:38:0x00ab  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public String toString() {
        switch (this.f19202) {
            case 1:
                StringBuilder sb = new StringBuilder("ConnectionInfo{\n  serverGuid=");
                sb.append((UUID) this.f19204);
                sb.append(",\n  serverName='");
                sb.append((String) this.f19201);
                sb.append("',\n  negotiatedProtocol=");
                sb.append((C4349) this.f19206);
                sb.append(",\n  clientGuid=");
                sb.append((UUID) this.f19203);
                sb.append(",\n  clientCapabilities=");
                sb.append((EnumSet) this.f19199);
                sb.append(",\n  serverCapabilities=");
                sb.append((EnumSet) this.f19205);
                sb.append(",\n  clientSecurityMode=0,\n  serverSecurityMode=");
                return AbstractC3589.m8110(sb, this.f19198, ",\n  server='null'\n}");
            case 2:
                StringBuilder sb2 = new StringBuilder();
                String str = (String) this.f19200;
                if (str != null) {
                    sb2.append(str);
                    sb2.append("://");
                } else {
                    sb2.append("//");
                }
                if (((String) this.f19204).length() > 0 || ((String) this.f19201).length() > 0) {
                    sb2.append((String) this.f19204);
                    if (((String) this.f19201).length() > 0) {
                        sb2.append(':');
                        sb2.append((String) this.f19201);
                    }
                    sb2.append('@');
                }
                String str2 = (String) this.f19206;
                if (str2 != null) {
                    if (AbstractC3801.m8467(str2, ':')) {
                        sb2.append('[');
                        sb2.append((String) this.f19206);
                        sb2.append(']');
                    } else {
                        sb2.append((String) this.f19206);
                    }
                }
                int i = -1;
                if (this.f19198 != -1 || ((String) this.f19200) != null) {
                    int iM10581 = m10581();
                    String str3 = (String) this.f19200;
                    if (str3 == null) {
                        sb2.append(':');
                        sb2.append(iM10581);
                    } else {
                        if (str3.equals("http")) {
                            i = 80;
                        } else if (str3.equals("https")) {
                            i = 443;
                        }
                        if (iM10581 != i) {
                        }
                    }
                }
                ArrayList arrayList = (ArrayList) this.f19203;
                int size = arrayList.size();
                for (int i2 = 0; i2 < size; i2++) {
                    sb2.append('/');
                    sb2.append((String) arrayList.get(i2));
                }
                if (((ArrayList) this.f19199) != null) {
                    sb2.append('?');
                    ArrayList arrayList2 = (ArrayList) this.f19199;
                    C5485 c5485 = ᵗʓ.ˑˊᵲ(ᵗʓ.ᵠˏˏ(0, arrayList2.size()), 2);
                    int i3 = c5485.f20741;
                    int i4 = c5485.f20743;
                    int i5 = c5485.f20742;
                    if ((i5 > 0 && i3 <= i4) || (i5 < 0 && i4 <= i3)) {
                        while (true) {
                            String str4 = (String) arrayList2.get(i3);
                            String str5 = (String) arrayList2.get(i3 + 1);
                            if (i3 > 0) {
                                sb2.append('&');
                            }
                            sb2.append(str4);
                            if (str5 != null) {
                                sb2.append('=');
                                sb2.append(str5);
                            }
                            if (i3 != i4) {
                                i3 += i5;
                            }
                        }
                    }
                }
                if (((String) this.f19205) != null) {
                    sb2.append('#');
                    sb2.append((String) this.f19205);
                }
                return sb2.toString();
            default:
                return super.toString();
        }
    }

    /* JADX INFO: renamed from: ʽˈˋ, reason: contains not printable characters */
    public void m10579() {
        ReentrantLock reentrantLock = (ReentrantLock) this.f19201;
        reentrantLock.lock();
        try {
            if (!m10585()) {
                reentrantLock.unlock();
                return;
            }
            RunnableC4781 runnableC4781 = (RunnableC4781) this.f19205;
            runnableC4781.getClass();
            RunnableC4781.f18188.mo9495("Stopping PacketReader...");
            runnableC4781.f18190.set(true);
            runnableC4781.f18192.interrupt();
            if (((Socket) this.f19203).getInputStream() != null) {
                ((Socket) this.f19203).getInputStream().close();
            }
            BufferedOutputStream bufferedOutputStream = (BufferedOutputStream) this.f19199;
            if (bufferedOutputStream != null) {
                bufferedOutputStream.close();
                this.f19199 = null;
            }
            Socket socket = (Socket) this.f19203;
            if (socket != null) {
                socket.close();
                this.f19203 = null;
            }
            reentrantLock.unlock();
        } catch (Throwable th) {
            reentrantLock.unlock();
            throw th;
        }
    }

    /* JADX INFO: renamed from: ˊᵫᵗ, reason: contains not printable characters */
    public void m10580(int i) throws IOException {
        ((BufferedOutputStream) this.f19199).write(0);
        ((BufferedOutputStream) this.f19199).write((byte) (i >> 16));
        ((BufferedOutputStream) this.f19199).write((byte) (i >> 8));
        ((BufferedOutputStream) this.f19199).write((byte) (i & 255));
    }

    /* JADX INFO: renamed from: ːˏᵟᵯ, reason: contains not printable characters */
    public int m10581() {
        int i = this.f19198;
        if (i != -1) {
            return i;
        }
        String str = (String) this.f19200;
        if (str.equals("http")) {
            return 80;
        }
        return str.equals("https") ? 443 : -1;
    }

    /* JADX WARN: Removed duplicated region for block: B:4:0x002a  */
    /* JADX INFO: renamed from: ᵨᵣ, reason: contains not printable characters */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public void m10582(C1695 c1695, String str) {
        int i;
        Object obj;
        Object obj2;
        int i2;
        int i3;
        int iM4656;
        int i4;
        char cCharAt;
        String str2 = str;
        ArrayList arrayList = (ArrayList) this.f19203;
        byte[] bArr = AbstractC1701.f6716;
        int iM4662 = AbstractC1701.m4662(0, str2.length(), str2);
        int iM4654 = AbstractC1701.m4654(iM4662, str2.length(), str2);
        byte b = -1;
        if (iM4654 - iM4662 < 2) {
            i = -1;
        } else {
            char cCharAt2 = str2.charAt(iM4662);
            char c = 'a';
            if ((ᵥˈᵛ.ˊᵙᵾᵲ(cCharAt2, 97) >= 0 && ᵥˈᵛ.ˊᵙᵾᵲ(cCharAt2, 122) <= 0) || (ᵥˈᵛ.ˊᵙᵾᵲ(cCharAt2, 65) >= 0 && ᵥˈᵛ.ˊᵙᵾᵲ(cCharAt2, 90) <= 0)) {
                i = iM4662 + 1;
                while (true) {
                    if (i >= iM4654) {
                        break;
                    }
                    char cCharAt3 = str2.charAt(i);
                    if ((c <= cCharAt3 && cCharAt3 < '{') || (('A' <= cCharAt3 && cCharAt3 < '[') || (('0' <= cCharAt3 && cCharAt3 < ':') || cCharAt3 == '+' || cCharAt3 == '-' || cCharAt3 == '.'))) {
                        i++;
                        c = 'a';
                    } else if (cCharAt3 != ':') {
                        break;
                    }
                }
                i = -1;
            }
        }
        if (i != -1) {
            int i5 = i;
            i2 = 1;
            obj2 = "https";
            obj = "http";
            if (str2.regionMatches(true, iM4662, "https:", 0, 6)) {
                this.f19200 = obj2;
                iM4662 += 6;
                str2 = str;
            } else {
                str2 = str;
                if (!str2.regionMatches(true, iM4662, "http:", 0, 5)) {
                    throw new IllegalArgumentException("Expected URL scheme 'http' or 'https' but was '" + str2.substring(0, i5) + '\'');
                }
                this.f19200 = obj;
                iM4662 += 5;
            }
        } else {
            obj = "http";
            obj2 = "https";
            i2 = 1;
            if (c1695 == null) {
                ᵕᵩʽᵲ.ᵮᵣᵗˈ("Expected URL scheme 'http' or 'https' but no scheme was found for ".concat(str2.length() > 6 ? AbstractC3801.m8488(6, str2).concat("...") : str2));
                return;
            }
            this.f19200 = c1695.f6698;
        }
        int i6 = iM4662;
        int i7 = 0;
        while (true) {
            i3 = i2;
            if (i6 >= iM4654 || !((cCharAt = str2.charAt(i6)) == '/' || cCharAt == '\\')) {
                break;
            }
            i7++;
            i6++;
            i2 = i3;
        }
        byte b2 = 35;
        if (i7 >= 2 || c1695 == null || !ᵥˈᵛ.ˊᵫᵗ(c1695.f6698, (String) this.f19200)) {
            int i8 = iM4662 + i7;
            int i9 = 0;
            int i10 = 0;
            while (true) {
                iM4656 = AbstractC1701.m4656(i8, iM4654, str2, "@/\\?#");
                byte bCharAt = iM4656 != iM4654 ? str2.charAt(iM4656) : b;
                if (bCharAt == b || bCharAt == b2 || bCharAt == 47 || bCharAt == 92 || bCharAt == 63) {
                    break;
                }
                if (bCharAt == 64) {
                    if (i9 == 0) {
                        int iM4664 = AbstractC1701.m4664(str2, ':', i8, iM4656);
                        String strM2864 = ᵺᵘ.ᵨᵣ(str2, i8, iM4664, " \"':;<=>@[]^`{}|/\\?#", 112);
                        if (i10 != 0) {
                            strM2864 = AbstractC0793.m2864(new StringBuilder(), (String) this.f19204, "%40", strM2864);
                        }
                        this.f19204 = strM2864;
                        if (iM4664 != iM4656) {
                            this.f19201 = ᵺᵘ.ᵨᵣ(str2, iM4664 + 1, iM4656, " \"':;<=>@[]^`{}|/\\?#", 112);
                            i9 = i3;
                        }
                        i10 = i3;
                    } else {
                        this.f19201 = ((String) this.f19201) + "%40" + ᵺᵘ.ᵨᵣ(str2, i8, iM4656, " \"':;<=>@[]^`{}|/\\?#", 112);
                    }
                    i8 = iM4656 + 1;
                    b2 = 35;
                    b = -1;
                }
            }
            int i11 = i8;
            while (true) {
                if (i11 < iM4656) {
                    char cCharAt4 = str2.charAt(i11);
                    if (cCharAt4 == ':') {
                        break;
                    }
                    if (cCharAt4 == '[') {
                        do {
                            i11++;
                            if (i11 < iM4656) {
                            }
                        } while (str2.charAt(i11) != ']');
                    }
                    i11++;
                } else {
                    i11 = iM4656;
                    break;
                }
            }
            int i12 = i11 + 1;
            if (i12 < iM4656) {
                this.f19206 = AbstractC3713.m8354(ᵺᵘ.ᵠˏˏ(i8, i11, 4, str2));
                try {
                    i4 = Integer.parseInt(ᵺᵘ.ᵨᵣ(str2, i12, iM4656, "", 120));
                } catch (NumberFormatException unused) {
                }
                if (i3 > i4 || i4 >= 65536) {
                    i4 = -1;
                }
                this.f19198 = i4;
                if (i4 == -1) {
                    throw new IllegalArgumentException(("Invalid URL port: \"" + str2.substring(i12, iM4656) + '\"').toString());
                }
            } else {
                int i13 = -1;
                this.f19206 = AbstractC3713.m8354(ᵺᵘ.ᵠˏˏ(i8, i11, 4, str2));
                String str3 = (String) this.f19200;
                if (str3.equals(obj)) {
                    i13 = 80;
                } else if (str3.equals(obj2)) {
                    i13 = 443;
                }
                this.f19198 = i13;
            }
            if (((String) this.f19206) == null) {
                throw new IllegalArgumentException(("Invalid URL host: \"" + str2.substring(i8, i11) + '\"').toString());
            }
            iM4662 = iM4656;
        } else {
            this.f19204 = c1695.m4638();
            this.f19201 = c1695.m4639();
            this.f19206 = c1695.f6700;
            this.f19198 = c1695.f6697;
            arrayList.clear();
            arrayList.addAll(c1695.m4637());
            if (iM4662 == iM4654 || str2.charAt(iM4662) == '#') {
                String strM4641 = c1695.m4641();
                this.f19199 = strM4641 != null ? m10578(ᵺᵘ.ᵨᵣ(strM4641, 0, 0, " \"'<>#", 83)) : null;
            }
        }
        int iM46562 = AbstractC1701.m4656(iM4662, iM4654, str2, "?#");
        if (iM4662 != iM46562) {
            char cCharAt5 = str2.charAt(iM4662);
            if (cCharAt5 == '/' || cCharAt5 == '\\') {
                arrayList.clear();
                arrayList.add("");
                iM4662++;
            } else {
                arrayList.set(arrayList.size() - 1, "");
            }
            while (iM4662 < iM46562) {
                int iM46563 = AbstractC1701.m4656(iM4662, iM46562, str2, "/\\");
                boolean z = iM46563 < iM46562;
                String str4 = ᵺᵘ.ᵨᵣ(str2, iM4662, iM46563, " \"<>^`{}|/\\?#", 112);
                if (!str4.equals(".") && !str4.equalsIgnoreCase("%2e")) {
                    if (!str4.equals("..") && !str4.equalsIgnoreCase("%2e.") && !str4.equalsIgnoreCase(".%2e") && !str4.equalsIgnoreCase("%2e%2e")) {
                        if (((CharSequence) AbstractC3653.m8235(1, arrayList)).length() == 0) {
                            arrayList.set(arrayList.size() - 1, str4);
                        } else {
                            arrayList.add(str4);
                        }
                        if (z) {
                            arrayList.add("");
                        }
                    } else if (((String) arrayList.remove(arrayList.size() - 1)).length() != 0 || arrayList.isEmpty()) {
                        arrayList.add("");
                    } else {
                        arrayList.set(arrayList.size() - 1, "");
                    }
                }
                iM4662 = z ? iM46563 + 1 : iM46563;
            }
        }
        if (iM46562 < iM4654 && str2.charAt(iM46562) == '?') {
            int iM46642 = AbstractC1701.m4664(str2, '#', iM46562, iM4654);
            this.f19199 = m10578(ᵺᵘ.ᵨᵣ(str2, iM46562 + 1, iM46642, " \"'<>#", 80));
            iM46562 = iM46642;
        }
        if (iM46562 >= iM4654 || str2.charAt(iM46562) != '#') {
            return;
        }
        this.f19205 = ᵺᵘ.ᵨᵣ(str2, iM46562 + 1, iM4654, "", 48);
    }

    /* JADX INFO: renamed from: ᵲᵶᵳˈ, reason: contains not printable characters */
    public C1695 m10583() {
        ArrayList arrayList;
        String str = (String) this.f19200;
        if (str == null) {
            ᵕᵩʽᵲ.ᵪᵹᵥᵻ("scheme == null");
            return null;
        }
        String str2 = ᵺᵘ.ᵠˏˏ(0, 0, 7, (String) this.f19204);
        String str3 = ᵺᵘ.ᵠˏˏ(0, 0, 7, (String) this.f19201);
        String str4 = (String) this.f19206;
        if (str4 == null) {
            ᵕᵩʽᵲ.ᵪᵹᵥᵻ("host == null");
            return null;
        }
        int iM10581 = m10581();
        ArrayList arrayList2 = (ArrayList) this.f19203;
        ArrayList arrayList3 = new ArrayList(AbstractC5492.m11122(arrayList2, 10));
        int size = arrayList2.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayList2.get(i);
            i++;
            arrayList3.add(ᵺᵘ.ᵠˏˏ(0, 0, 7, (String) obj));
        }
        ArrayList arrayList4 = (ArrayList) this.f19199;
        if (arrayList4 != null) {
            ArrayList arrayList5 = new ArrayList(AbstractC5492.m11122(arrayList4, 10));
            int size2 = arrayList4.size();
            int i2 = 0;
            while (i2 < size2) {
                Object obj2 = arrayList4.get(i2);
                i2++;
                String str5 = (String) obj2;
                arrayList5.add(str5 != null ? ᵺᵘ.ᵠˏˏ(0, 0, 3, str5) : null);
            }
            arrayList = arrayList5;
        } else {
            arrayList = null;
        }
        String str6 = (String) this.f19205;
        return new C1695(str, str2, str3, str4, iM10581, arrayList3, arrayList, str6 != null ? ᵺᵘ.ᵠˏˏ(0, 0, 7, str6) : null, toString());
    }

    /* JADX INFO: renamed from: ᵵᵽ, reason: contains not printable characters */
    public void m10584(ᵜᵪʾ r7) {
        InterfaceC5025 interfaceC5025 = (InterfaceC5025) this.f19200;
        interfaceC5025.mo9497(r7, "Acquiring write lock to send packet << {} >>");
        ReentrantLock reentrantLock = (ReentrantLock) this.f19201;
        reentrantLock.lock();
        try {
            if (!m10585()) {
                throw new TransportException(String.format("Cannot write %s as transport is disconnected", r7));
            }
            try {
                interfaceC5025.mo9486(r7, "Writing packet {}");
                Object obj = ((C2994) this.f19204).f11535;
                C1973 c1973 = new C1973();
                r7.m6815(c1973);
                m10580(c1973.m2476());
                ((BufferedOutputStream) this.f19199).write(c1973.f3142, c1973.f3141, c1973.m2476());
                ((BufferedOutputStream) this.f19199).flush();
                interfaceC5025.mo9497(r7, "Packet {} sent, lock released.");
            } catch (IOException e) {
                throw new TransportException(e);
            }
        } finally {
            reentrantLock.unlock();
        }
    }

    /* JADX INFO: renamed from: ᵷᵺʾᵖ, reason: contains not printable characters */
    public boolean m10585() {
        Socket socket = (Socket) this.f19203;
        return (socket == null || !socket.isConnected() || ((Socket) this.f19203).isClosed()) ? false : true;
    }
}
