//
// sub_BA1AA8 from 0xba1aa8 to 0xba2110 (1640 bytes)
//
// 2 xrefs:
// > sub_BA2110 @ 0xba21f4
// > ??> @ 0x111c910
//
// 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
//
__int64 __fastcall sub_BA1AA8(__int64 *a1, _BYTE *a2)
{
__int64 v4; // x19
__int64 v5; // x24
time_t v6; // x2
time_t v7; // x3
__int64 v8; // x1
time_t v9; // x0
__int64 v10; // x0
__int64 result; // x0
int v12; // w0
_BOOL4 v13; // w0
__int64 v14; // x26
_QWORD *v15; // x21
int v16; // w0
int v17; // w3
__int64 v18; // x2
unsigned __int16 v19; // w1
int *v20; // x0
const char *v21; // x0
int v22; // w25
unsigned int v23; // w24
__int64 v24; // x1
char *v25; // x21
unsigned __int64 v26; // x27
__int64 v27; // d8
unsigned __int64 v28; // x28
_BYTE *v29; // x0
signed __int64 v30; // x25
signed __int64 v31; // x24
char *v32; // x3
size_t v33; // x24
signed __int64 v34; // x25
char *v35; // x0
bool v36; // zf
char *v37; // x0
unsigned __int64 v38; // x24
char *v39; // x24
const char *v40; // x25
size_t v41; // x0
__int64 v42; // x0
int v43; // w5
size_t v44; // x0
__int64 v45; // x21
__int64 v46; // x1
char *v47; // [xsp+88h] [xbp+88h]
time_t timer; // [xsp+90h] [xbp+90h] BYREF
struct sockaddr v49; // [xsp+98h] [xbp+98h] BYREF
v4 = a1[195];
v5 = *a1;
time(&timer);
v6 = *(_QWORD *)(v4 + 48);
v7 = timer;
if ( v6 < timer )
{
*(_DWORD *)(v4 + 8) = -99;
*(_DWORD *)v4 = 3;
*a2 = 0;
goto LABEL_12;
}
if ( timer > *(int *)(v4 + 32) + *(_QWORD *)(v4 + 56) )
{
time((time_t *)(v4 + 56));
v8 = *(_QWORD *)(v4 + 48);
v9 = timer;
*a2 = 0;
if ( v8 - v9 > 0 )
{
v10 = **(_QWORD **)(v4 + 16);
switch ( *(_DWORD *)v4 )
{
case 0:
result = sub_BA1248(v4, 7u);
goto LABEL_15;
case 1:
result = sub_BA06AC(v4, 7);
goto LABEL_15;
case 2:
result = sub_BA0B70(v4, 7);
goto LABEL_15;
case 3:
sub_B7B528(v10, "%s\n", "TFTP finished");
goto LABEL_16;
default:
sub_B7B690(v10, "%s", "Internal state machine error");
result = 71;
break;
}
return result;
}
LABEL_12:
sub_B7B690(v5, "TFTP response timeout");
return 28;
}
*a2 = 0;
if ( v6 - v7 <= 0 )
goto LABEL_12;
v12 = sub_B9FFC8(*(_DWORD *)(v4 + 24), -1, 0xFFFFFFFF, 0);
if ( v12 == -1 )
{
v20 = (int *)__errno();
v21 = (const char *)sub_B9F460((__int64)a1, *v20);
sub_B7B690(v5, "%s", v21);
*(_DWORD *)(v4 + 12) = 5;
return 0;
}
if ( !v12 )
return 0;
v14 = a1[195];
LODWORD(timer) = 128;
v15 = (_QWORD *)*a1;
v16 = recvfrom(*(_DWORD *)(v14 + 24), *(void **)(v14 + 352), *(_DWORD *)(v14 + 340) + 4, 0, &v49, (socklen_t *)&timer);
*(_DWORD *)(v14 + 332) = v16;
v17 = v16;
if ( !*(_DWORD *)(v14 + 328) )
{
v22 = timer;
memcpy((void *)(v14 + 200), &v49, (unsigned int)timer);
*(_DWORD *)(v14 + 328) = v22;
v17 = *(_DWORD *)(v14 + 332);
}
if ( v17 > 3 )
{
v18 = *(_QWORD *)(v14 + 352);
v19 = _byteswap_ushort(*(_WORD *)v18);
*(_DWORD *)(v14 + 12) = v19;
switch ( v19 )
{
case 3u:
if ( v17 == 4
|| (*(unsigned __int8 *)(v18 + 3) | (*(unsigned __int8 *)(v18 + 2) << 8)) != (unsigned __int16)(*(_WORD *)(v14 + 64) + 1) )
{
goto LABEL_26;
}
v23 = sub_B7C1F0((__int64)a1, 1, (char *)(v18 + 4), v17 - 4);
if ( !v23 )
{
v24 = v15[19] + *(_DWORD *)(v14 + 332) - 4;
v15[19] = v24;
sub_B722D8(v15, v24);
goto LABEL_26;
}
sub_BA1758(v14, 5u);
return v23;
case 4u:
goto LABEL_26;
case 5u:
*(_DWORD *)(v14 + 8) = *(unsigned __int8 *)(v18 + 3) | (*(unsigned __int8 *)(v18 + 2) << 8);
sub_B7B528((__int64)v15, "%s\n", (const char *)(v18 + 4));
goto LABEL_26;
case 6u:
v25 = (char *)(v18 + 2);
v26 = v18 + 2 + v17 - 2;
v27 = **(_QWORD **)(v14 + 16);
*(_DWORD *)(v14 + 340) = 512;
break;
default:
sub_B7B690((__int64)v15, "%s", "Internal error: Unexpected packet");
goto LABEL_26;
}
while ( 1 )
{
if ( (unsigned __int64)v25 >= v26 )
{
LABEL_26:
if ( !(unsigned int)sub_B72490(a1) )
goto LABEL_27;
sub_BA1758(v14, 5u);
return 42;
}
v28 = v26 - (_QWORD)v25;
v29 = memchr(v25, 0, v26 - (_QWORD)v25);
if ( v29 )
v30 = v29 - v25;
else
v30 = v26 - (_QWORD)v25;
v31 = v30 + 1;
if ( v28 <= v30 + 1 )
goto LABEL_58;
v32 = &v25[v31];
v33 = v28 - v31;
v47 = v32;
v34 = v30 + 2;
v35 = (char *)memchr(v32, 0, v33);
v36 = v35 == nullptr;
v37 = (char *)(v35 - v47);
if ( !v36 )
v33 = (size_t)v37;
v38 = v34 + v33;
if ( v28 < v38 || (v39 = &v25[v38], v40 = &v25[strlen(v25) + 1], !v39) )
{
LABEL_58:
sub_B7B690(v27, "Malformed ACK packet, rejecting");
return 71;
}
sub_B7B528(v27, "got option=(%s) value=(%s)\n", v25, v40);
v41 = strlen(v25);
if ( (unsigned int)sub_B94858(v25, "blksize", v41) )
{
v42 = strtol(v40, nullptr, 10);
if ( !v42 )
{
sub_B7B690(v27, "invalid blocksize value in OACK packet");
return 71;
}
if ( v42 > 65464 )
{
sub_B7B690(v27, "%s (%d)", "blksize is larger than max supported", 65464);
return 71;
}
if ( v42 <= 7 )
{
sub_B7B690(v27, "%s (%d)", "blksize is smaller than min supported", 8);
return 71;
}
v43 = *(_DWORD *)(v14 + 344);
if ( v42 > v43 )
{
sub_B7B690(v27, "%s (%ld)", "server requested blksize larger than allocated", v42);
return 71;
}
*(_DWORD *)(v14 + 340) = v42;
sub_B7B528(v27, "%s (%d) %s (%d)\n", "blksize parsed from OACK", v42, "requested", v43);
LABEL_52:
v25 = v39;
}
else
{
v44 = strlen(v25);
if ( !(unsigned int)sub_B94858(v25, "tsize", v44) )
goto LABEL_52;
v45 = strtol(v40, nullptr, 10);
sub_B7B528(v27, "%s (%ld)\n", "tsize parsed from OACK", v45);
if ( *(_BYTE *)(v27 + 1569) )
goto LABEL_52;
if ( !v45 )
{
sub_B7B690(v27, "invalid tsize -:%s:- value in OACK packet", v40);
return 71;
}
v46 = v45;
v25 = v39;
sub_B72430(v27, v46);
}
}
}
sub_B7B690((__int64)v15, "Received too short packet");
*(_DWORD *)(v14 + 12) = 7;
LABEL_27:
result = sub_BA1758(v4, *(_DWORD *)(v4 + 12));
LABEL_15:
if ( !(_DWORD)result )
{
LABEL_16:
v13 = *(_DWORD *)v4 == 3;
*a2 = v13;
if ( v13 )
{
sub_B94394((__int64)a1, -1, -1, 0, 0, -1, 0);
return 0;
}
return 0;
}
return result;
}