//
// sub_61D5E4 from 0x61d5e4 to 0x61d9a4 (960 bytes)
//
// 4 xrefs:
// > sub_62160C @ 0x6219b4
// > sub_62160C @ 0x6219d4
// > sub_62160C @ 0x622004
// > sub_62160C @ 0x622336
//
// This function is hookable on this platform!
//
// Using BromaIDA 8.0.0 @ https://github.com/Stazzical/BromaIDA
// Using bindings at commit ba9f177b at Thu Jul 16 22:52:06 2026 from https://github.com/geode-sdk/bindings
//
int __fastcall sub_61D5E4(size_t a1, int a2, char *a3, int a4)
{
_BOOL4 v7; // r6
char *v8; // r5
char *v9; // r0
char *v10; // r4
char *v11; // r4
int result; // r0
char *v13; // r11
char *v14; // r0
const char *v15; // r4
unsigned int v16; // r0
int v17; // r11
int v18; // r2
_BOOL4 v19; // r3
int v20; // r3
int v21; // r4
void *v22; // r3
int v23; // r0
char *v24; // r1
char *v25; // r0
char *v26; // r0
char *v27; // r0
int v28; // r4
char *v29; // r3
bool v30; // zf
int v31; // r2
int v32; // r5
int v33; // t1
bool v34; // zf
int v35; // r1
char *v36; // r2
int v37; // r0
int v38; // r2
char *v39; // r12
int v40; // r1
int v42; // t1
char *v43; // [sp+8h] [bp-1Ch]
int v44; // [sp+8h] [bp-1Ch]
char *v45; // [sp+Ch] [bp-18h]
void *p; // [sp+14h] [bp-10h] BYREF
char *v47; // [sp+18h] [bp-Ch] BYREF
char *v48; // [sp+1Ch] [bp-8h] BYREF
v7 = 0;
v8 = a3;
p = nullptr;
v47 = nullptr;
v9 = strstr(a3, "://");
if ( v9 )
{
v10 = v9 + 3;
if ( sub_62AC7C("https", v8, 5) )
{
a4 = 2;
LABEL_4:
v8 = v10;
goto LABEL_5;
}
if ( sub_62AC7C("socks5h", v8, 7) )
{
a4 = 7;
LABEL_36:
v8 = v10;
v7 = 1;
goto LABEL_5;
}
if ( sub_62AC7C("socks5", v8, 6) )
{
a4 = 5;
goto LABEL_36;
}
if ( sub_62AC7C("socks4a", v8, 7) )
{
v7 = 1;
a4 = 6;
goto LABEL_4;
}
if ( sub_62AC7C("socks4", v8, 6) || sub_62AC7C("socks", v8, 5) )
{
v7 = 1;
a4 = 4;
goto LABEL_4;
}
if ( !sub_62AC7C("http:", v8, 5) )
{
sub_6180F8(a1, "Unsupported proxy scheme for '%s'", v8);
return 7;
}
v8 = v10;
}
if ( (a4 & 0xFFFFFFFD) != 5 )
{
v10 = v8;
v7 = (a4 & 0xFFFFFFFD) == 4;
goto LABEL_4;
}
v7 = 1;
LABEL_5:
v11 = strchr(v8, 64);
if ( v11 )
{
result = sub_61D130(v8, v11 - v8, &p, (void **)&v47, nullptr);
if ( result )
return result;
v8 = v11 + 1;
}
if ( *v8 != 91 )
{
v13 = v8;
goto LABEL_11;
}
v28 = (unsigned __int8)v8[1];
v29 = v8 + 1;
if ( !v8[1] )
{
v13 = v8 + 1;
goto LABEL_76;
}
v30 = v28 == 58;
if ( v28 != 58 )
v30 = v28 == 46;
v13 = v8 + 1;
v31 = v30;
if ( (*(_BYTE *)(ctype_ + v28 + 1) & 0x44) != 0 )
v31 |= 1u;
if ( v31 )
{
do
{
v33 = (unsigned __int8)*++v13;
v28 = v33;
v34 = v33 == 58;
if ( v33 != 58 )
v34 = v28 == 46;
v32 = v34;
if ( !v28 )
goto LABEL_76;
if ( (*(_BYTE *)(ctype_ + v28 + 1) & 0x44) != 0 )
v32 |= 1u;
}
while ( v32 );
}
if ( v28 != 37 )
{
v35 = (unsigned __int8)*v13;
v36 = v13;
goto LABEL_102;
}
v45 = v29;
v37 = strncmp("%25", v13, 3u);
v29 = v45;
if ( v37 )
{
sub_61807C(a1, "Please URL encode %% as %%25, see RFC 6874.\n");
v29 = v45;
}
v38 = (unsigned __int8)v13[1];
v39 = v13 + 1;
if ( !v13[1] )
{
LABEL_96:
v13 = v39;
LABEL_76:
v8 = v29;
sub_61807C(a1, "Invalid IPv6 address format\n");
goto LABEL_11;
}
while ( 1 )
{
if ( (*(_BYTE *)(ctype_ + v38 + 1) & 3) == 0 )
{
v40 = (*(_BYTE *)(ctype_ + v38 + 1) & 0x44) != 0;
if ( (unsigned int)(v38 - 45) <= 1 )
v40 = 1;
if ( v38 == 95 )
v40 |= 1u;
if ( !(v38 == 126 ? v40 | 1 : v40) )
break;
}
v42 = (unsigned __int8)*++v39;
v38 = v42;
if ( !v42 )
goto LABEL_96;
}
v35 = (unsigned __int8)*v39;
v36 = v39;
LABEL_102:
if ( v35 != 93 )
{
v13 = v36;
goto LABEL_76;
}
v8 = v29;
v13 = v36 + 1;
*v36 = 0;
LABEL_11:
v14 = strchr(v13, 58);
if ( v14 )
{
*v14 = 0;
v15 = v14 + 1;
v48 = nullptr;
v16 = strtol(v14 + 1, &v48, 10);
v17 = v16;
if ( v48 && ((v18 = (unsigned __int8)*v48, v18 != 47) ? (v19 = (v18 & 0xDF) != 0) : (v19 = 0), v19)
|| v16 >= 0x10000 )
{
sub_61807C(a1, "No valid port number in proxy string (%s)\n", v15);
}
else
{
*(_DWORD *)(a2 + 260) = v16;
}
v20 = (unsigned __int8)*v8;
}
else
{
v44 = (unsigned __int8)*v8;
if ( v44 == 47 )
return 5;
v27 = strchr(v8, 47);
v20 = v44;
if ( v27 )
{
*v27 = 0;
v20 = (unsigned __int8)*v8;
}
v17 = *(_DWORD *)(a1 + 348);
if ( !v17 )
{
v17 = 443;
if ( a4 != 2 )
v17 = 1080;
if ( !v20 )
goto LABEL_33;
goto LABEL_20;
}
}
if ( !v20 )
{
LABEL_33:
off_ABAEB0(p);
p = nullptr;
off_ABAEB0(v47);
return 0;
}
LABEL_20:
v21 = a2 + 196;
if ( !v7 )
v21 = a2 + 228;
v22 = p;
*(_DWORD *)(v21 + 20) = a4;
if ( !v22 )
{
LABEL_29:
if ( v17 >= 0 )
{
*(_DWORD *)(v21 + 16) = v17;
if ( v7 | (*(_DWORD *)(a2 + 260) >> 31) || !*(_DWORD *)(a2 + 196) )
*(_DWORD *)(a2 + 260) = v17;
}
off_ABAEB0(*(void **)v21);
*(_DWORD *)v21 = 0;
v26 = off_ABAEB8(v8);
*(_DWORD *)v21 = v26;
*(_DWORD *)(v21 + 8) = v26;
if ( v26 )
goto LABEL_33;
return 27;
}
off_ABAEB0(*(void **)(v21 + 24));
*(_DWORD *)(v21 + 24) = 0;
v23 = sub_623F2C(a1, p, 0);
*(_DWORD *)(v21 + 24) = v23;
if ( v23 )
{
off_ABAEB0(*(void **)(v21 + 28));
v24 = v47;
*(_DWORD *)(v21 + 28) = 0;
if ( v24 && (v43 = v24, strlen(v24) <= 0xFF) )
{
v25 = (char *)sub_623F2C(a1, v43, 0);
*(_DWORD *)(v21 + 28) = v25;
}
else
{
v25 = off_ABAEB8(&byte_9B1912);
*(_DWORD *)(v21 + 28) = v25;
}
if ( v25 )
{
*(_BYTE *)(a2 + 621) = 1;
goto LABEL_29;
}
}
return 27;
}