src/nca.c
// Get a buff, encrypt it and write it in section

Get a buff, encrypt it and write it in section
Get a buff encrypt it and write it in section
size_t nca_section_fwrite(nca_section_ctx_t *ctx, void *buffer, size_t count, uint64_t offset)
{
    nca_section_fseek(ctx, offset);
    uint8_t sector_ofs = ctx->sector_ofs;
    uint64_t temp_buff_size = sector_ofs + count;
    unsigned char *temp_buff = (unsigned char *)malloc(temp_buff_size);
    nca_section_fseek(ctx, ctx->cur_seek - ctx->offset);
    nca_section_fread(ctx, temp_buff, sector_ofs);
    nca_section_fseek(ctx, ctx->cur_seek - ctx->offset);
    memcpy(temp_buff + sector_ofs, buffer, count);
    aes_setiv(ctx->aes, ctx->ctr, 16);
    aes_encrypt(ctx->aes, temp_buff, temp_buff, temp_buff_size);
    if (!fwrite(temp_buff, 1, temp_buff_size, ctx->file))
    {
        fprintf(stderr, "Unable to modify NCA");
        return 0;
    }
    nca_section_fseek(ctx, ctx->cur_seek - ctx->offset + count);
    return count;
}
size_t nca_section_fwrite ( nca_section_ctx_t * ctx , void * buffer , size_t count , uint64_t offset ) { nca_section_fseek ( ctx , offset ) ; uint8_t sector_ofs = ctx -> sector_ofs ; uint64_t temp_buff_size = sector_ofs + count ; unsigned char * temp_buff = ( unsigned char * ) malloc ( temp_buff_size ) ; nca_section_fseek ( ctx , ctx -> cur_seek - ctx -> offset ) ; nca_section_fread ( ctx , temp_buff , sector_ofs ) ; nca_section_fseek ( ctx , ctx -> cur_seek - ctx -> offset ) ; memcpy ( temp_buff + sector_ofs , buffer , count ) ; aes_setiv ( ctx -> aes , ctx -> ctr , 16 ) ; aes_encrypt ( ctx -> aes , temp_buff , temp_buff , temp_buff_size ) ; if ( ! fwrite ( temp_buff , 1 , temp_buff_size , ctx -> file ) ) { fprintf ( stderr , " " ) ; return 0 ; } nca_section_fseek ( ctx , ctx -> cur_seek - ctx -> offset + count ) ; return count ; }
Get a buff, encrypt it and write it in section
Get a buff encrypt it and write it in section