//
// PEM_write_bio from 0x821d04 to 0x821fc8 (708 bytes)
//
// 4 xrefs:
// > PEM_ASN1_write_bio @ 0x822300
// > PEM_write @ 0x822578
// > PEM_X509_INFO_write_bio @ 0x8ba66c
// > ??> @ 0x6534c
//
// 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 PEM_write_bio(int a1, const char *a2, const char *a3, int a4, int a5)
{
int v9; // r6
size_t v10; // r4
__int16 v11; // r7
_BYTE *v12; // r5
int v14; // r10
signed int v15; // r4
int v16; // r11
int v17; // r8
int v18; // r3
size_t v19; // r10
int v20; // r0
int v21; // r2
int v22; // r0
int v23; // r4
int v24; // r0
int v25; // [sp+10h] [bp-14h]
const char *v26; // [sp+14h] [bp-10h]
int v27[2]; // [sp+1Ch] [bp-8h] BYREF
v9 = EVP_ENCODE_CTX_new();
if ( !v9 )
goto LABEL_5;
EVP_EncodeInit();
v10 = strlen(a2);
if ( BIO_write(a1, (int)"-----BEGIN ", 11) != 11
|| (v14 = BIO_write(a1, (int)a2, v10), v14 != v10)
|| BIO_write(a1, (int)"-----\n", 6) != 6
|| (v15 = strlen(a3), v15 > 0) && (BIO_write(a1, (int)a3, v15) != v15 || BIO_write(a1, (int)"\n", 1) != 1) )
{
v11 = 7;
v12 = nullptr;
LABEL_4:
CRYPTO_clear_free(v12, 0x2000u, (int)"crypto/pem/pem_lib.c", 640);
EVP_ENCODE_CTX_free(v9);
ERR_put_error(9, 114, v11, (int)"crypto/pem/pem_lib.c", 642);
return 0;
}
v12 = CRYPTO_malloc(&stru_1FFC.st_value, (int)"crypto/pem/pem_lib.c", 612);
if ( !v12 )
{
LABEL_5:
v11 = 65;
v12 = nullptr;
goto LABEL_4;
}
if ( a5 > 0 )
{
v16 = 0;
v26 = a2;
v25 = v14;
v17 = 0;
v18 = a5;
while ( 1 )
{
v19 = v18 >= 5120 ? 5120 : v18;
v20 = EVP_EncodeUpdate(v9, (int)v12, (int)v27, (void *)(a4 + v17), v19);
v17 += v19;
a5 -= v19;
if ( !v20 )
break;
v21 = v27[0];
if ( v27[0] )
{
v22 = BIO_write(a1, (int)v12, v27[0]);
v21 = v27[0];
if ( v22 != v27[0] )
break;
}
v18 = a5;
v16 += v21;
if ( a5 <= 0 )
{
v14 = v25;
a2 = v26;
v23 = v16;
EVP_EncodeFinal(v9, v12, v27);
goto LABEL_23;
}
}
LABEL_21:
v11 = 7;
goto LABEL_4;
}
v23 = 0;
EVP_EncodeFinal(v9, v12, v27);
LABEL_23:
if ( v27[0] > 0 )
{
v24 = BIO_write(a1, (int)v12, v27[0]);
if ( v24 != v27[0] )
goto LABEL_21;
}
if ( BIO_write(a1, (int)"-----END ", 9) != 9
|| v14 != BIO_write(a1, (int)a2, v14)
|| BIO_write(a1, (int)"-----\n", 6) != 6 )
{
goto LABEL_21;
}
CRYPTO_clear_free(v12, 0x2000u, (int)"crypto/pem/pem_lib.c", 636);
EVP_ENCODE_CTX_free(v9);
return v23 + v27[0];
}