//
// png_handle_iTXt from 0x65e440 to 0x65e694 (596 bytes)
//
// 3 xrefs:
// > png_push_read_chunk @ 0x652be0
// > png_read_info @ 0x65590c
// > png_read_end @ 0x657440
//
// 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 png_handle_iTXt(_DWORD *a1, int a2, unsigned int a3)
{
int v4; // r3
int v7; // r3
int v8; // r3
_BYTE *v9; // r8
unsigned int v10; // r4
int result; // r0
unsigned int v12; // r4
_BYTE *v13; // r2
unsigned int v15; // r3
int v16; // r2
const char *v17; // r1
_BYTE *v18; // r0
unsigned int v19; // r10
unsigned int v20; // r4
_BYTE *v21; // r3
unsigned int v22; // r9
unsigned int v24; // r9
_BYTE *v25; // r2
unsigned int v26; // r3
size_t v28; // r11
unsigned int v29; // r5
_BYTE *v30; // r11
int v31; // lr
int v32; // r3
unsigned int v33; // [sp+0h] [bp-24h] BYREF
_DWORD v34[7]; // [sp+4h] [bp-20h] BYREF
v4 = a1[214];
if ( v4 )
{
if ( v4 == 1 )
return png_crc_finish(a1, a3);
v7 = v4 - 1;
a1[214] = v7;
if ( v7 == 1 )
{
png_crc_finish(a1, a3);
return png_chunk_benign_error((int)a1, "no space in chunk cache");
}
}
v8 = a1[77];
if ( (v8 & 1) == 0 )
png_chunk_error((int)a1, "missing IHDR");
v9 = (_BYTE *)a1[222];
if ( (v8 & 4) != 0 )
v8 |= 8u;
v10 = a3 + 1;
if ( (a1[77] & 4) != 0 )
a1[77] = v8;
if ( v9 )
{
if ( v10 <= a1[223] )
goto LABEL_11;
a1[222] = 0;
a1[223] = 0;
png_free((int)a1, v9);
}
v18 = png_malloc_base((int)a1, a3 + 1);
v9 = v18;
if ( !v18 )
{
png_chunk_warning((int)a1, "insufficient memory to read chunk");
png_crc_finish(a1, a3);
return png_chunk_benign_error((int)a1, "out of memory");
}
a1[222] = v18;
a1[223] = v10;
LABEL_11:
png_crc_read(a1, (int)v9, a3);
result = png_crc_finish(a1, 0);
if ( result )
return result;
if ( !a3 )
{
v17 = "bad keyword";
return png_chunk_benign_error((int)a1, v17);
}
if ( !*v9 )
{
v17 = "bad keyword";
return png_chunk_benign_error((int)a1, v17);
}
v12 = 0;
v13 = v9;
while ( 1 )
{
v15 = v12 + 1;
if ( v12 + 1 == a3 )
break;
if ( !*++v13 )
goto LABEL_19;
++v12;
}
v15 = a3;
LABEL_19:
if ( v12 > 0x4E )
{
v17 = "bad keyword";
return png_chunk_benign_error((int)a1, v17);
}
if ( a3 < v15 + 5 )
{
v17 = "truncated";
return png_chunk_benign_error((int)a1, v17);
}
v16 = (unsigned __int8)v9[v15 + 1];
if ( v9[v15 + 1] )
{
if ( v16 != 1 )
{
v17 = "bad compression info";
return png_chunk_benign_error((int)a1, v17);
}
if ( v9[v15 + 2] )
{
v17 = "bad compression info";
return png_chunk_benign_error((int)a1, v17);
}
}
v19 = (unsigned __int8)v9[v15 + 1];
v20 = v15 + 3;
v33 = 0;
if ( v16 )
v19 = 1;
if ( a3 <= v20 )
{
v22 = v15 + 3;
}
else
{
v21 = &v9[v20];
v22 = v20;
if ( v9[v20] )
{
do
{
if ( a3 <= ++v22 )
break;
}
while ( *++v21 );
}
}
v24 = v22 + 1;
if ( a3 > v24 && (v25 = &v9[v24], v9[v24]) )
{
v26 = v24;
do
{
if ( a3 <= ++v26 )
break;
}
while ( *++v25 );
}
else
{
v26 = v24;
}
v28 = v26 + 1;
if ( (v26 + 1 <= a3) > v19 )
{
v29 = a3 - v28;
v33 = v29;
LABEL_47:
v30 = &v9[v28];
if ( v19 )
v31 = 2;
else
v31 = 1;
v30[v29] = 0;
goto LABEL_51;
}
if ( v28 < a3 )
v32 = v19 & 1;
else
v32 = 0;
if ( !v32 )
{
v17 = "truncated";
return png_chunk_benign_error((int)a1, v17);
}
v33 = -1;
if ( sub_65C398((int)a1, a3, v28, &v33) == 1 )
{
v9 = (_BYTE *)a1[222];
v29 = v33;
goto LABEL_47;
}
v17 = (const char *)a1[87];
if ( v17 )
return png_chunk_benign_error((int)a1, v17);
v30 = &v9[v28];
v31 = 2;
v30[v33] = 0;
LABEL_51:
v34[5] = &v9[v20];
v34[6] = &v9[v24];
v34[0] = v31;
v34[1] = v9;
v34[2] = v30;
v34[4] = v33;
v34[3] = 0;
result = png_set_text_2(a1, a2, v34, 1);
if ( result )
{
v17 = "insufficient memory";
return png_chunk_benign_error((int)a1, v17);
}
return result;
}