mirror of
https://github.com/DarkStore-3DS/Project_CTR.git
synced 2026-07-03 00:39:14 +00:00
[ctrtool] Implemented --listromfs & --romfsdir to work directly with NCCH. Also implemented plainrgn extraction.
This commit is contained in:
+47
-13
@@ -31,11 +31,47 @@ void ivfc_set_file(ivfc_context* ctx, FILE* file)
|
||||
ctx->file = file;
|
||||
}
|
||||
|
||||
void ivfc_set_encrypted(ivfc_context* ctx, u32 encrypted)
|
||||
{
|
||||
ctx->encrypted = encrypted;
|
||||
}
|
||||
|
||||
void ivfc_set_key(ivfc_context* ctx, u8 key[16])
|
||||
{
|
||||
memcpy(ctx->key, key, 16);
|
||||
}
|
||||
|
||||
void ivfc_set_counter(ivfc_context* ctx, u8 counter[16])
|
||||
{
|
||||
memcpy(ctx->counter, counter, 16);
|
||||
}
|
||||
|
||||
void ivfc_fseek(ivfc_context* ctx, u64 offset)
|
||||
{
|
||||
u64 data_pos = offset - ctx->offset;
|
||||
fseeko64(ctx->file, offset, SEEK_SET);
|
||||
ctr_init_counter(&ctx->aes, ctx->key, ctx->counter);
|
||||
ctr_add_counter(&ctx->aes, data_pos / 0x10);
|
||||
}
|
||||
|
||||
size_t ivfc_fread(ivfc_context* ctx, void* buffer, size_t size, size_t count)
|
||||
{
|
||||
size_t read;
|
||||
if ((read = fread(buffer, size, count, ctx->file)) != count) {
|
||||
//printf("ivfc_fread() fail\n");
|
||||
return read;
|
||||
}
|
||||
if (ctx->encrypted) {
|
||||
ctr_crypt_counter(&ctx->aes, buffer, buffer, size*read);
|
||||
}
|
||||
return read;
|
||||
}
|
||||
|
||||
|
||||
void ivfc_process(ivfc_context* ctx, u32 actions)
|
||||
{
|
||||
fseeko64(ctx->file, ctx->offset, SEEK_SET);
|
||||
fread(&ctx->header, 1, sizeof(ivfc_header), ctx->file);
|
||||
ivfc_fseek(ctx, ctx->offset);
|
||||
ivfc_fread(ctx, &ctx->header, 1, sizeof(ivfc_header));
|
||||
|
||||
if (getle32(ctx->header.magic) != MAGIC_IVFC)
|
||||
{
|
||||
@@ -45,25 +81,23 @@ void ivfc_process(ivfc_context* ctx, u32 actions)
|
||||
|
||||
if (getle32(ctx->header.id) == 0x10000)
|
||||
{
|
||||
fread(&ctx->romfsheader, 1, sizeof(ivfc_header_romfs), ctx->file);
|
||||
|
||||
ctx->levelcount = 3;
|
||||
|
||||
ctx->level[2].hashblocksize = 1 << getle32(ctx->romfsheader.level3.blocksize);
|
||||
ctx->level[1].hashblocksize = 1 << getle32(ctx->romfsheader.level2.blocksize);
|
||||
ctx->level[0].hashblocksize = 1 << getle32(ctx->romfsheader.level1.blocksize);
|
||||
ctx->level[2].hashblocksize = 1 << getle32(ctx->header.level3.blocksize);
|
||||
ctx->level[1].hashblocksize = 1 << getle32(ctx->header.level2.blocksize);
|
||||
ctx->level[0].hashblocksize = 1 << getle32(ctx->header.level1.blocksize);
|
||||
|
||||
ctx->bodyoffset = align64(IVFC_HEADER_SIZE + getle32(ctx->romfsheader.masterhashsize), ctx->level[2].hashblocksize);
|
||||
ctx->bodysize = getle64(ctx->romfsheader.level3.hashdatasize);
|
||||
ctx->bodyoffset = align64(IVFC_HEADER_SIZE + getle32(ctx->header.masterhashsize), ctx->level[2].hashblocksize);
|
||||
ctx->bodysize = getle64(ctx->header.level3.hashdatasize);
|
||||
|
||||
ctx->level[2].dataoffset = ctx->bodyoffset;
|
||||
ctx->level[2].datasize = align64(ctx->bodysize, ctx->level[2].hashblocksize);
|
||||
|
||||
ctx->level[0].dataoffset = ctx->level[2].dataoffset + ctx->level[2].datasize;
|
||||
ctx->level[0].datasize = align64(getle64(ctx->romfsheader.level1.hashdatasize), ctx->level[0].hashblocksize);
|
||||
ctx->level[0].datasize = align64(getle64(ctx->header.level1.hashdatasize), ctx->level[0].hashblocksize);
|
||||
|
||||
ctx->level[1].dataoffset = ctx->level[0].dataoffset + ctx->level[0].datasize;
|
||||
ctx->level[1].datasize = align64(getle64(ctx->romfsheader.level2.hashdatasize), ctx->level[1].hashblocksize);
|
||||
ctx->level[1].datasize = align64(getle64(ctx->header.level2.hashdatasize), ctx->level[1].hashblocksize);
|
||||
|
||||
ctx->level[0].hashoffset = IVFC_HEADER_SIZE;
|
||||
ctx->level[1].hashoffset = ctx->level[0].dataoffset;
|
||||
@@ -125,8 +159,8 @@ void ivfc_read(ivfc_context* ctx, u64 offset, u64 size, u8* buffer)
|
||||
return;
|
||||
}
|
||||
|
||||
fseeko64(ctx->file, ctx->offset + offset, SEEK_SET);
|
||||
if (size != fread(buffer, 1, size, ctx->file))
|
||||
ivfc_fseek(ctx, ctx->offset + offset);
|
||||
if (size != ivfc_fread(ctx, buffer, 1, size))
|
||||
{
|
||||
fprintf(stderr, "Error, IVFC could not read file\n");
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user