//
// _TIFFRewriteField from 0xc41e7c to 0xc426e0 (2148 bytes)
//
// 5 xrefs:
// > TIFFFlush @ 0xc49340
// > TIFFFlush @ 0xc4935c
// > TIFFFlush @ 0xc493b8
// > TIFFFlush @ 0xc493d4
// > ??> @ 0x71f58
//
// 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 TIFFRewriteField(const char **a1, unsigned __int16 a2, unsigned int a3, __int64 a4, __int64 *a5)
{
int v6; // w22
const char *v10; // x1
__int64 v11; // x21
int v12; // w0
__int64 v13; // x21
__int64 v14; // x28
int v15; // w0
int v16; // w1
__int64 result; // x0
int v18; // w0
unsigned int v19; // w1
unsigned int v20; // w0
int v21; // w0
_DWORD *v22; // x22
unsigned int v23; // w23
int v24; // w0
bool v25; // cc
int v26; // w0
unsigned __int16 v27; // w25
int v28; // w0
int v29; // w0
bool v30; // zf
int v31; // w0
int v32; // w1
int v33; // w0
int v34; // w0
int v35; // w0
const char *v36; // x25
const char *v37; // x26
int v38; // w0
__int64 v39; // x25
__int64 v40; // x24
void *v41; // x0
int v42; // w0
__int64 v43; // x1
__int64 v44; // x0
__int64 v45; // x1
int v46; // w0
__int64 v47; // x0
int v48; // w0
__int64 v49; // x1
__int64 v50; // x0
__int64 v51; // x1
const char *v52; // x21
const char *v53; // x25
int v54; // w0
__int64 v55; // x21
__int64 v56; // x24
unsigned __int16 v57; // [xsp+62h] [xbp+62h] BYREF
unsigned __int16 v58; // [xsp+64h] [xbp+64h] BYREF
unsigned __int16 v59; // [xsp+66h] [xbp+66h] BYREF
__int64 v60; // [xsp+68h] [xbp+68h] BYREF
__int64 v61; // [xsp+70h] [xbp+70h] BYREF
unsigned int v62; // [xsp+78h] [xbp+78h] BYREF
unsigned __int16 v63; // [xsp+80h] [xbp+80h] BYREF
unsigned __int16 v64; // [xsp+82h] [xbp+82h] BYREF
__int64 v65; // [xsp+84h] [xbp+84h] BYREF
__int64 v66; // [xsp+8Ch] [xbp+8Ch] BYREF
v6 = a2;
v58 = 0;
v59 = 0;
v60 = 0;
v61 = 0;
TIFFFindField(a1, a2, 0);
if ( ((_DWORD)a1[2] & 0x800) != 0 )
{
TIFFErrorExt(a1[119], "TIFFResetField", "Memory mapped files not currently supported for this operation.");
return 0;
}
v10 = a1[3];
if ( !v10 )
{
TIFFErrorExt(a1[119], "TIFFResetField", "Attempt to reset field on directory not already on disk.");
return 0;
}
v11 = ((__int64 (__fastcall *)(const char *, const char *, _QWORD))a1[122])(a1[119], v10, 0);
if ( (const char *)v11 != a1[3] )
{
LABEL_18:
TIFFErrorExt(a1[119], "TIFFResetField", "%s: Seek error accessing TIFF directory", *a1);
return 0;
}
if ( ((_DWORD)a1[2] & 0x80000) != 0 )
{
if ( ((__int64 (__fastcall *)(const char *, unsigned int *, __int64))a1[120])(a1[119], &v62, 8) == 8 )
{
if ( ((_DWORD)a1[2] & 0x80) != 0 )
TIFFSwabLong8(&v62);
v12 = (unsigned __int16)v62;
v13 = v11 + 8;
v57 = v62;
v14 = 20;
goto LABEL_9;
}
LABEL_24:
TIFFErrorExt(a1[119], "TIFFResetField", "%s: Can not read TIFF directory count", *a1);
return 0;
}
if ( ((__int64 (__fastcall *)(const char *, unsigned __int16 *, __int64))a1[120])(a1[119], &v57, 2) != 2 )
goto LABEL_24;
if ( ((_DWORD)a1[2] & 0x80) != 0 )
TIFFSwabShort(&v57);
v12 = v57;
v13 = v11 + 2;
v14 = 12;
LABEL_9:
if ( v12 )
{
while ( ((__int64 (__fastcall *)(const char *, unsigned __int16 *, __int64))a1[120])(a1[119], &v63, v14) == v14 )
{
v15 = v63;
v16 = *((_DWORD *)a1 + 4);
v58 = v63;
if ( (v16 & 0x80) != 0 )
{
TIFFSwabShort(&v58);
v15 = v58;
}
if ( v6 == v15 )
goto LABEL_26;
v13 += v14;
if ( !v57 )
goto LABEL_54;
}
TIFFErrorExt(a1[119], "TIFFResetField", "%s: Can not read TIFF directory entry.", *a1);
return 0;
}
if ( v58 != v6 )
{
LABEL_54:
TIFFErrorExt(a1[119], "TIFFResetField", "%s: Could not find tag %d.", *a1, v6);
return 0;
}
LABEL_26:
v18 = *((_DWORD *)a1 + 4);
v59 = v64;
if ( (v18 & 0x80) == 0 )
{
if ( (v18 & 0x80000) != 0 )
{
v60 = v65;
v61 = v66;
goto LABEL_31;
}
v19 = v65;
goto LABEL_29;
}
TIFFSwabShort(&v59);
v31 = *((_DWORD *)a1 + 4);
if ( (v31 & 0x80000) == 0 )
{
v19 = v65;
v62 = v65;
if ( (v31 & 0x80) != 0 )
{
TIFFSwabLong(&v62);
v20 = HIDWORD(v65);
v32 = *((_DWORD *)a1 + 4);
v60 = v62;
v62 = HIDWORD(v65);
if ( (v32 & 0x80) != 0 )
{
TIFFSwabLong(&v62);
v20 = v62;
}
goto LABEL_30;
}
LABEL_29:
v20 = HIDWORD(v65);
v60 = v19;
LABEL_30:
v61 = v20;
goto LABEL_31;
}
v60 = v65;
if ( (v31 & 0x80) != 0 )
{
TIFFSwabLong8(&v60);
v33 = *((_DWORD *)a1 + 4);
v61 = v66;
if ( (v33 & 0x80) != 0 )
TIFFSwabLong8(&v61);
}
else
{
v61 = v66;
}
LABEL_31:
if ( (unsigned int)TIFFDataWidth(a3) != 8 || ((_DWORD)a1[2] & 0x80000) != 0 )
goto LABEL_32;
if ( a3 != 16 )
{
if ( a3 == 17 )
{
v48 = TIFFDataWidth(9u);
v22 = TIFFCheckMalloc(a1, a4, v48, "for field buffer.");
if ( !v22 )
return 0;
if ( a4 > 0 )
{
v49 = *a5;
v50 = 0;
*v22 = *a5;
if ( v49 != (int)v49 )
goto LABEL_83;
while ( ++v50 != a4 )
{
v51 = a5[v50];
v22[v50] = v51;
if ( v51 != (int)v51 )
goto LABEL_83;
}
}
v23 = 9;
goto LABEL_34;
}
if ( a3 == 18 )
{
v23 = 13;
goto LABEL_78;
}
LABEL_32:
v21 = TIFFDataWidth(a3);
v22 = TIFFCheckMalloc(a1, a4, v21, "for field buffer.");
if ( !v22 )
return 0;
v23 = a3;
v24 = TIFFDataWidth(a3);
memcpy(v22, a5, v24 * a4);
goto LABEL_34;
}
v23 = 4;
LABEL_78:
v42 = TIFFDataWidth(v23);
v22 = TIFFCheckMalloc(a1, a4, v42, "for field buffer.");
if ( !v22 )
return 0;
if ( a4 > 0 )
{
v43 = *a5;
v44 = 0;
*v22 = *a5;
if ( v43 == (unsigned int)v43 )
{
while ( ++v44 != a4 )
{
v45 = a5[v44];
v22[v44] = v45;
if ( v45 != (unsigned int)v45 )
goto LABEL_83;
}
goto LABEL_34;
}
LABEL_83:
TIFFfree(v22);
TIFFErrorExt(a1[119], "TIFFResetField", "Value exceeds 32bit range of output type.");
return 0;
}
LABEL_34:
v25 = (int)TIFFDataWidth(v23) <= 1;
v26 = *((_DWORD *)a1 + 4);
if ( !v25 && (v26 & 0x80) != 0 )
{
if ( (unsigned int)TIFFDataWidth(v23) == 2 )
{
TIFFSwabArrayOfShort(v22, a4);
v26 = *((_DWORD *)a1 + 4);
}
else if ( (unsigned int)TIFFDataWidth(v23) == 4 )
{
TIFFSwabArrayOfLong(v22, a4);
v26 = *((_DWORD *)a1 + 4);
}
else
{
if ( (unsigned int)TIFFDataWidth(v23) == 8 )
TIFFSwabArrayOfLong8(v22, a4);
v26 = *((_DWORD *)a1 + 4);
}
}
if ( (v26 & 0x80000) != 0 )
{
if ( (int)TIFFDataWidth(v23) * a4 <= 8 )
{
v61 = v13 + 12;
LABEL_39:
v27 = v23;
if ( a4 == v60 )
{
v27 = v23;
if ( v59 == (unsigned __int16)v23 )
goto LABEL_90;
}
v28 = TIFFDataWidth(v23);
memcpy(&v61, v22, v28 * a4);
goto LABEL_41;
}
}
else if ( (int)TIFFDataWidth(v23) * a4 <= 4 )
{
v61 = v13 + 8;
goto LABEL_39;
}
if ( a4 == v60 && v59 == (unsigned __int16)v23 )
{
LABEL_90:
v47 = ((__int64 (__fastcall *)(const char *, __int64, _QWORD))a1[122])(a1[119], v61, 0);
if ( v47 != v61 )
{
TIFFfree(v22);
TIFFErrorExt(a1[119], "TIFFResetField", "%s: Seek error accessing TIFF directory", *a1);
return 0;
}
v52 = a1[121];
v53 = a1[119];
v54 = TIFFDataWidth(v23);
v55 = ((__int64 (__fastcall *)(const char *, _DWORD *, __int64))v52)(v53, v22, v54 * a4);
v56 = (int)TIFFDataWidth(v23) * a4;
v41 = v22;
if ( v55 == v56 )
{
TIFFfree(v22);
return 1;
}
LABEL_70:
TIFFfree(v41);
TIFFErrorExt(a1[119], "TIFFResetField", "Error writing directory link");
return 0;
}
v61 = ((__int64 (__fastcall *)(const char *, _QWORD, __int64))a1[122])(a1[119], 0, 2);
v36 = a1[121];
v37 = a1[119];
v38 = TIFFDataWidth(v23);
v39 = ((__int64 (__fastcall *)(const char *, _DWORD *, __int64))v36)(v37, v22, v38 * a4);
v40 = (int)TIFFDataWidth(v23) * a4;
v41 = v22;
if ( v39 != v40 )
goto LABEL_70;
v27 = v23;
TIFFfree(v22);
LABEL_41:
v29 = *((_DWORD *)a1 + 4);
v59 = v27;
v64 = v27;
if ( (v29 & 0x80) != 0 )
{
TIFFSwabShort(&v64);
v34 = *((_DWORD *)a1 + 4);
if ( (v34 & 0x80000) != 0 )
{
v65 = v60;
if ( (v34 & 0x80) != 0 )
{
TIFFSwabLong8(&v65);
v46 = *((_DWORD *)a1 + 4);
v66 = v61;
if ( (v46 & 0x80) != 0 )
TIFFSwabLong8(&v66);
}
else
{
v66 = v61;
}
}
else
{
LODWORD(v65) = v60;
if ( (v34 & 0x80) == 0 )
goto LABEL_44;
TIFFSwabLong(&v65);
v35 = *((_DWORD *)a1 + 4);
HIDWORD(v65) = v61;
if ( (v35 & 0x80) != 0 )
TIFFSwabLong((char *)&v65 + 4);
}
}
else
{
if ( (v29 & 0x80000) == 0 )
{
LODWORD(v65) = v60;
LABEL_44:
HIDWORD(v65) = v61;
goto LABEL_45;
}
v65 = v60;
v66 = v61;
}
LABEL_45:
if ( ((__int64 (__fastcall *)(const char *, __int64, _QWORD))a1[122])(a1[119], v13, 0) != v13 )
goto LABEL_18;
v30 = v14 == ((__int64 (__fastcall *)(const char *, unsigned __int16 *, __int64))a1[121])(a1[119], &v63, v14);
result = 1;
if ( !v30 )
{
TIFFErrorExt(a1[119], "TIFFResetField", "%s: Can not write TIFF directory entry.", *a1);
return 0;
}
return result;
}