src/nca.c
// Read and decrypt part of section into a buff

Read and decrypt part of section into a buff
Read and decrypt part of section into a buff
size_t nca_section_fread(nca_section_ctx_t *ctx, void *buffer, size_t count)
{
    size_t read = 0; 
    char block_buf[0x10];
    if (ctx->sector_ofs)
    {
        if ((read = fread(block_buf, 1, 0x10, ctx->file)) != 0x10)
        {
            return 0;
        }
        aes_setiv(ctx->aes, ctx->ctr, 0x10);
        aes_decrypt(ctx->aes, block_buf, block_buf, 0x10);
        if (count + ctx->sector_ofs < 0x10)
        {
            memcpy(buffer, block_buf + ctx->sector_ofs, count);
            ctx->sector_ofs += count;
            nca_section_fseek(ctx, ctx->cur_seek - ctx->offset);
            return count;
        }
        memcpy(buffer, block_buf + ctx->sector_ofs, 0x10 - ctx->sector_ofs);
        uint32_t read_in_block = 0x10 - ctx->sector_ofs;
        nca_section_fseek(ctx, ctx->cur_seek - ctx->offset + 0x10);
        return read_in_block + nca_section_fread(ctx, (char *)buffer + read_in_block, count - read_in_block);
    }
    if ((read = fread(buffer, 1, count, ctx->file)) != count)
    {
        return 0;
    }
    aes_setiv(ctx->aes, ctx->ctr, 16);
    aes_decrypt(ctx->aes, buffer, buffer, count);
    nca_section_fseek(ctx, ctx->cur_seek - ctx->offset + count);
    return read;
}
size_t nca_section_fread ( nca_section_ctx_t * ctx , void * buffer , size_t count ) { size_t read = 0 ; char block_buf [ 0x10 ] ; if ( ctx -> sector_ofs ) { if ( ( read = fread ( block_buf , 1 , 0x10 , ctx -> file ) ) != 0x10 ) { return 0 ; } aes_setiv ( ctx -> aes , ctx -> ctr , 0x10 ) ; aes_decrypt ( ctx -> aes , block_buf , block_buf , 0x10 ) ; if ( count + ctx -> sector_ofs < 0x10 ) { memcpy ( buffer , block_buf + ctx -> sector_ofs , count ) ; ctx -> sector_ofs += count ; nca_section_fseek ( ctx , ctx -> cur_seek - ctx -> offset ) ; return count ; } memcpy ( buffer , block_buf + ctx -> sector_ofs , 0x10 - ctx -> sector_ofs ) ; uint32_t read_in_block = 0x10 - ctx -> sector_ofs ; nca_section_fseek ( ctx , ctx -> cur_seek - ctx -> offset + 0x10 ) ; return read_in_block + nca_section_fread ( ctx , ( char * ) buffer + read_in_block , count - read_in_block ) ; } if ( ( read = fread ( buffer , 1 , count , ctx -> file ) ) != count ) { return 0 ; } aes_setiv ( ctx -> aes , ctx -> ctr , 16 ) ; aes_decrypt ( ctx -> aes , buffer , buffer , count ) ; nca_section_fseek ( ctx , ctx -> cur_seek - ctx -> offset + count ) ; return read ; }
Read and decrypt part of section into a buff
Read and decrypt part of section into a buff
/* XXX */