//
// sub_C34538 from 0xc34538 to 0xc34bec (1716 bytes)
//
// 2 xrefs:
// > TIFFReadCustomDirectory @ 0xc3bbdc
// > TIFFReadDirectory @ 0xc3c248
//
// 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_C34538(const char **a1, const char *a2, _QWORD *a3, _QWORD *a4)
{
__int64 v6; // x0
int v8; // w0
__int64 v9; // x22
unsigned int v10; // w24
unsigned __int16 v12; // w1
__int64 v13; // x2
int v14; // w22
__int64 v15; // x2
int v16; // w28
char *v17; // x0
char *v18; // x25
signed __int64 v19; // x2
signed __int64 v20; // x1
unsigned __int16 v21; // w1
int v22; // w0
__int64 v23; // x22
__int64 v24; // x1
char *v25; // x0
__int64 v26; // x0
_WORD *v27; // x27
_WORD *v28; // x22
char *v29; // x20
unsigned int v30; // w24
__int64 v31; // x1
int v32; // w0
__int64 *v33; // x28
int v34; // w0
int v35; // w0
int v36; // w0
__int64 v37; // x1
unsigned __int16 v38; // [xsp+66h] [xbp+66h] BYREF
unsigned __int64 v39; // [xsp+68h] [xbp+68h] BYREF
__int64 v40; // [xsp+70h] [xbp+70h]
__int64 v41; // [xsp+78h] [xbp+78h]
v6 = _stack_chk_guard;
a1[3] = a2;
v41 = v6;
if ( a4 )
{
*a4 = 0;
a2 = a1[3];
}
v8 = *((_DWORD *)a1 + 4);
if ( (v8 & 0x800) == 0 )
{
if ( (const char *)((__int64 (__fastcall *)(const char *, const char *, _QWORD))a1[122])(a1[119], a2, 0) != a1[3] )
{
v10 = 0;
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "%s: Seek error accessing TIFF directory", *a1);
return v10;
}
if ( ((_DWORD)a1[2] & 0x80000) != 0 )
{
if ( ((__int64 (__fastcall *)(const char *, unsigned __int64 *, __int64))a1[120])(a1[119], &v39, 8) != 8 )
{
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "%s: Can not read TIFF directory count", *a1);
return 0;
}
if ( ((_DWORD)a1[2] & 0x80) != 0 )
TIFFSwabLong8(&v39);
if ( v39 > 0x1000 )
goto LABEL_79;
v13 = 20;
v12 = v39;
v14 = 20;
v38 = v39;
}
else
{
if ( ((__int64 (__fastcall *)(const char *, unsigned __int16 *, __int64))a1[120])(a1[119], &v38, 2) != 2 )
{
v10 = 0;
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "%s: Can not read TIFF directory count", *a1);
return v10;
}
if ( ((_DWORD)a1[2] & 0x80) != 0 )
TIFFSwabShort(&v38);
v12 = v38;
if ( v38 > 0x1000u )
goto LABEL_69;
v13 = 12;
v14 = 12;
}
v25 = (char *)TIFFCheckMalloc(a1, v12, v13, "to read TIFF directory");
v18 = v25;
if ( v25 )
{
v26 = ((__int64 (__fastcall *)(const char *, char *, _QWORD))a1[120])(a1[119], v25, (v14 * v38) & 0x1FFFFC);
v21 = v38;
if ( v26 != v14 * v38 )
{
v10 = 0;
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "%.100s: Can not read TIFF directory", *a1);
TIFFfree(v18);
return v10;
}
if ( !a4 )
goto LABEL_61;
if ( ((_DWORD)a1[2] & 0x80000) == 0 )
{
if ( ((__int64 (__fastcall *)(const char *, unsigned __int64 *, __int64))a1[120])(a1[119], &v39, 4) != 4 )
LODWORD(v39) = 0;
if ( ((_DWORD)a1[2] & 0x80) == 0 )
goto LABEL_60;
LABEL_59:
TIFFSwabLong(&v39);
LABEL_60:
v21 = v38;
*a4 = (unsigned int)v39;
LABEL_61:
v27 = TIFFCheckMalloc(a1, v21, 24, "to read TIFF directory");
if ( !v27 )
{
v10 = 0;
TIFFfree(v18);
return v10;
}
if ( !v38 )
{
LABEL_77:
TIFFfree(v18);
*a3 = v27;
return v38;
}
v28 = v27;
v29 = v18;
LOWORD(v30) = 0;
while ( 1 )
{
v32 = *((_DWORD *)a1 + 4);
if ( (v32 & 0x80) != 0 )
{
TIFFSwabShort(v29);
v34 = *((_DWORD *)a1 + 4);
*v28 = *(_WORD *)v29;
if ( (v34 & 0x80) != 0 )
{
v33 = (__int64 *)(v29 + 4);
TIFFSwabShort(v29 + 2);
v35 = *((_DWORD *)a1 + 4);
v28[1] = *((_WORD *)v29 + 1);
if ( (v35 & 0x80000) != 0 )
{
if ( (v35 & 0x80) != 0 )
TIFFSwabLong8(v29 + 4);
LABEL_68:
v29 += 20;
v40 = *v33;
*((_QWORD *)v28 + 1) = v40;
*((_QWORD *)v28 + 2) = (unsigned __int8)*(v29 - 8)
| (unsigned __int64)(unsigned __int16)((unsigned __int8)*(v29 - 7) << 8)
| ((unsigned __int64)(unsigned __int8)*(v29 - 6) << 16) & 0xFFFFFF
| ((unsigned __int8)*(v29 - 5) << 24)
| ((unsigned __int64)(unsigned __int8)*(v29 - 4) << 32) & 0xFFFFFFFFFFLL
| ((unsigned __int64)(unsigned __int8)*(v29 - 3) << 40) & 0xFFFFFFFFFFFFLL
| ((unsigned __int64)(unsigned __int8)*(v29 - 2) << 48) & 0xFFFFFFFFFFFFFFLL
| ((unsigned __int64)(unsigned __int8)*(v29 - 1) << 56);
goto LABEL_65;
}
if ( (v35 & 0x80) != 0 )
TIFFSwabLong(v29 + 4);
}
else
{
v33 = (__int64 *)(v29 + 4);
v28[1] = *((_WORD *)v29 + 1);
if ( (v34 & 0x80000) != 0 )
goto LABEL_68;
}
}
else
{
v33 = (__int64 *)(v29 + 4);
*v28 = *(_WORD *)v29;
v28[1] = *((_WORD *)v29 + 1);
if ( (v32 & 0x80000) != 0 )
goto LABEL_68;
}
v31 = *((unsigned int *)v29 + 1);
v29 += 12;
*((_DWORD *)v28 + 4) = *((_DWORD *)v29 - 1);
*((_QWORD *)v28 + 1) = v31;
LABEL_65:
v28 += 12;
v30 = (unsigned __int16)(v30 + 1);
if ( v38 <= v30 )
goto LABEL_77;
}
}
if ( ((__int64 (__fastcall *)(const char *, _QWORD *, __int64))a1[120])(a1[119], a4, 8) != 8 )
*a4 = 0;
v36 = *((_DWORD *)a1 + 4);
goto LABEL_84;
}
return 0;
}
if ( (v8 & 0x80000) != 0 )
{
v9 = (__int64)(a2 + 8);
if ( (__int64)(a2 + 8) <= 7 || v9 < (__int64)a2 || v9 > (__int64)a1[116] )
{
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "Can not read TIFF directory count");
return 0;
}
TIFFmemcpy(&v39, &a2[(_QWORD)a1[115]], 8u);
if ( ((_DWORD)a1[2] & 0x80) != 0 )
TIFFSwabLong8(&v39);
if ( v39 <= 0x1000 )
{
v15 = 20;
v10 = (unsigned __int16)v39;
v16 = 20;
v38 = v39;
LABEL_27:
if ( !v10 )
{
TIFFErrorExt(
a1[119],
"TIFFFetchDirectory",
"Sanity check on directory count failed, zero tag directories not supported");
return v10;
}
v17 = (char *)TIFFCheckMalloc(a1, (unsigned __int16)v10, v15, "to read TIFF directory");
v18 = v17;
if ( v17 )
{
v19 = v16 * (unsigned int)v38;
v20 = v19 + v9;
if ( v9 > v19 + v9 || v19 > v20 || v20 > (__int64)a1[116] )
{
v10 = 0;
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "Can not read TIFF directory");
TIFFfree(v18);
return v10;
}
TIFFmemcpy(v17, &a1[115][v9], v19);
v21 = v38;
if ( !a4 )
goto LABEL_61;
v22 = *((_DWORD *)a1 + 4);
v23 = v16 * (unsigned int)v38 + v9;
if ( (v22 & 0x80000) == 0 )
{
v24 = v23 + 4;
if ( v23 + 4 > 3 && v24 >= v23 && v24 <= (__int64)a1[116] )
{
TIFFmemcpy(&v39, &a1[115][v23], 4u);
v22 = *((_DWORD *)a1 + 4);
}
else
{
LODWORD(v39) = 0;
}
if ( (v22 & 0x80) == 0 )
goto LABEL_60;
goto LABEL_59;
}
v37 = v23 + 8;
if ( v23 + 8 <= 7 || v37 < v23 || v37 > (__int64)a1[116] )
{
*a4 = 0;
if ( (v22 & 0x80) != 0 )
goto LABEL_86;
goto LABEL_85;
}
TIFFmemcpy(a4, &a1[115][v23], 8u);
v36 = *((_DWORD *)a1 + 4);
LABEL_84:
if ( (v36 & 0x80) != 0 )
{
LABEL_86:
TIFFSwabLong8(a4);
v21 = v38;
goto LABEL_61;
}
LABEL_85:
v21 = v38;
goto LABEL_61;
}
return 0;
}
LABEL_79:
TIFFErrorExt(
a1[119],
"TIFFFetchDirectory",
"Sanity check on directory count failed, this is probably not a valid IFD offset");
return 0;
}
v9 = (__int64)(a2 + 2);
if ( (__int64)(a2 + 2) > 1 && v9 >= (__int64)a2 && v9 <= (__int64)a1[116] )
{
TIFFmemcpy(&v38, &a2[(_QWORD)a1[115]], 2u);
if ( ((_DWORD)a1[2] & 0x80) != 0 )
TIFFSwabShort(&v38);
v10 = v38;
if ( v38 <= 0x1000u )
{
v15 = 12;
v16 = 12;
goto LABEL_27;
}
LABEL_69:
v10 = 0;
TIFFErrorExt(
a1[119],
"TIFFFetchDirectory",
"Sanity check on directory count failed, this is probably not a valid IFD offset");
return v10;
}
v10 = 0;
TIFFErrorExt(a1[119], "TIFFFetchDirectory", "Can not read TIFF directory count");
return v10;
}