//
// png_handle_iCCP from 0x65d088 to 0x65d45e (982 bytes)
//
// 3 xrefs:
// > png_push_read_chunk @ 0x652a6e
// > png_read_info @ 0x6558fa
// > png_read_end @ 0x657416
//
// 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_iCCP(int a1, _DWORD *a2, unsigned int a3)
{
int v3; // r3
unsigned int v5; // r11
__int16 v6; // r3
unsigned int v8; // r1
const char *v9; // r7
int result; // r0
_BYTE *v11; // r2
unsigned int v12; // r3
int v13; // t1
unsigned int v14; // r10
_BOOL4 v15; // r0
size_t v16; // r8
int v17; // r2
int v18; // r3
char *v19; // r10
int v20; // r11
int v21; // r2
int v22; // r3
int v23; // r1
int v24; // r2
char *v25; // r0
void *v26; // r0
int v27; // r3
int v28; // r2
size_t byte_count; // [sp+8h] [bp-4FCh]
int v30; // [sp+Ch] [bp-4F8h]
int v31; // [sp+14h] [bp-4F0h]
unsigned int v32[2]; // [sp+1Ch] [bp-4E8h] BYREF
size_t v33; // [sp+24h] [bp-4E0h] BYREF
_BYTE v34[84]; // [sp+28h] [bp-4DCh] BYREF
unsigned __int8 v35[132]; // [sp+7Ch] [bp-488h] BYREF
_BYTE v36[1028]; // [sp+100h] [bp-404h] BYREF
v3 = *(_DWORD *)(a1 + 308);
v32[0] = a3;
if ( (v3 & 1) == 0 )
png_chunk_error(a1, "missing IHDR");
if ( (v3 & 6) != 0 )
{
png_crc_finish((_DWORD *)a1, a3);
return png_chunk_benign_error(a1, "out of place");
}
v5 = a3;
if ( a3 <= 8 )
{
png_crc_finish((_DWORD *)a1, a3);
return png_chunk_benign_error(a1, "too short");
}
v6 = *(_WORD *)(a1 + 998);
if ( v6 < 0 )
return png_crc_finish((_DWORD *)a1, a3);
if ( (v6 & 4) != 0 )
{
v8 = a3;
v9 = "too many profiles";
LABEL_7:
png_crc_finish((_DWORD *)a1, v8);
goto LABEL_8;
}
if ( a3 <= 0x50 )
{
png_crc_read((_DWORD *)a1, (int)v34, a3);
v8 = v32[0] - v5;
v32[0] -= v5;
}
else
{
v5 = 81;
png_crc_read((_DWORD *)a1, (int)v34, 81);
v8 = v32[0] - 81;
v32[0] -= 81;
}
if ( !v34[0] )
{
v9 = "bad keyword";
LABEL_30:
v8 = v32[0];
goto LABEL_7;
}
v11 = v34;
v12 = 0;
while ( 1 )
{
v14 = v12 + 1;
v15 = v5 > v12 + 1;
if ( v12 + 1 > 0x4F )
v15 = 0;
if ( !v15 )
break;
v13 = (unsigned __int8)*++v11;
++v12;
if ( !v13 )
goto LABEL_24;
}
if ( v12 > 0x4E )
{
v9 = "bad keyword";
goto LABEL_7;
}
LABEL_24:
v16 = v14 + 1;
if ( v14 + 1 >= v5 )
{
v9 = "bad compression method";
goto LABEL_7;
}
if ( v34[v16] )
{
v9 = "bad compression method";
goto LABEL_7;
}
if ( sub_65C1DC(a1, 1766015824) )
{
v8 = v32[0];
v9 = *(const char **)(a1 + 348);
goto LABEL_7;
}
v17 = *(_DWORD *)(a1 + 320);
*(_DWORD *)(a1 + 328) = v5 - 2 - v14;
v18 = *(_DWORD *)(a1 + 476);
*(_DWORD *)(a1 + 324) = &v34[v14 + 2];
v33 = 132;
if ( v17 != v18 )
{
*(_DWORD *)(a1 + 348) = "zstream unclaimed";
v9 = "zstream unclaimed";
LABEL_34:
*(_DWORD *)(a1 + 320) = 0;
goto LABEL_30;
}
*(_DWORD *)(a1 + 340) = 0;
*(_DWORD *)(a1 + 336) = v35;
sub_65C6A4((_DWORD *)a1, (int)v36, v32, (int *)&v33, 0);
if ( v33 )
{
LABEL_45:
v9 = *(const char **)(a1 + 348);
goto LABEL_34;
}
v30 = a1 + 924;
byte_count = (v35[1] << 16) + (v35[0] << 24) + v35[3] + (v35[2] << 8);
if ( !png_icc_check_length(a1, a1 + 924, (int)v34, byte_count)
|| !png_icc_check_header(a1, v30, (int)v34, byte_count, v35, *(_BYTE *)(a1 + 535)) )
{
LABEL_38:
v9 = nullptr;
goto LABEL_34;
}
v19 = *(char **)(a1 + 888);
v31 = (v35[129] << 16) + (v35[128] << 24) + v35[131] + (v35[130] << 8);
if ( v19 )
{
if ( byte_count <= *(_DWORD *)(a1 + 892) )
goto LABEL_42;
*(_DWORD *)(a1 + 888) = 0;
*(_DWORD *)(a1 + 892) = 0;
png_free(a1, v19);
}
v25 = (char *)png_malloc_base(a1, byte_count);
v19 = v25;
if ( !v25 )
{
v9 = "out of memory";
goto LABEL_34;
}
*(_DWORD *)(a1 + 888) = v25;
*(_DWORD *)(a1 + 892) = byte_count;
LABEL_42:
memcpy(v19, v35, 0x84u);
v20 = 12 * v31;
v21 = *(_DWORD *)(a1 + 320);
v22 = *(_DWORD *)(a1 + 476);
v33 = 12 * v31;
if ( v21 == v22 )
{
*(_DWORD *)(a1 + 336) = v19 + 132;
*(_DWORD *)(a1 + 340) = 0;
sub_65C6A4((_DWORD *)a1, (int)v36, v32, (int *)&v33, 0);
}
else
{
*(_DWORD *)(a1 + 348) = "zstream unclaimed";
}
if ( v33 )
goto LABEL_45;
if ( !png_icc_check_tag_table(a1, v30, (int)v34, byte_count, (unsigned __int8 *)v19) )
goto LABEL_38;
v23 = *(_DWORD *)(a1 + 320);
v24 = *(_DWORD *)(a1 + 476);
v33 = byte_count - 132 - v20;
if ( v23 == v24 )
{
*(_DWORD *)(a1 + 336) = &v19[v20 + 132];
*(_DWORD *)(a1 + 340) = 0;
sub_65C6A4((_DWORD *)a1, (int)v36, v32, (int *)&v33, 1);
}
else
{
*(_DWORD *)(a1 + 348) = "zstream unclaimed";
}
if ( v32[0] )
{
if ( (*(_DWORD *)(a1 + 312) & 0x100000) == 0 )
{
v9 = "extra compressed data";
goto LABEL_34;
}
if ( v33 )
{
v9 = "truncated";
goto LABEL_34;
}
png_chunk_warning(a1, "extra compressed data");
}
else if ( v33 )
{
v9 = "truncated";
goto LABEL_34;
}
png_crc_finish((_DWORD *)a1, v32[0]);
result = png_icc_set_sRGB(a1, v30, (unsigned __int8 *)v19, *(_DWORD *)(a1 + 372));
if ( !a2 )
{
LABEL_63:
*(_DWORD *)(a1 + 320) = 0;
return result;
}
png_free_data(a1, (int)a2, 16, 0);
v26 = png_malloc_base(a1, v16);
a2[29] = v26;
if ( v26 )
{
memcpy(v26, v34, v16);
v27 = a2[2];
v28 = a2[58] | 0x10;
a2[31] = byte_count;
a2[58] = v28;
a2[30] = v19;
a2[2] = v27 | 0x1000;
*(_DWORD *)(a1 + 888) = 0;
result = png_colorspace_sync(a1, (int)a2);
goto LABEL_63;
}
v9 = "out of memory";
*(_WORD *)(a1 + 998) = ~((unsigned int)~(*(unsigned __int16 *)(a1 + 998) << 17) >> 17);
png_colorspace_sync(a1, (int)a2);
*(_DWORD *)(a1 + 320) = 0;
LABEL_8:
*(_WORD *)(a1 + 998) = ~((unsigned int)~(*(unsigned __int16 *)(a1 + 998) << 17) >> 17);
result = png_colorspace_sync(a1, (int)a2);
if ( v9 )
return png_chunk_benign_error(a1, v9);
return result;
}