From 9f70eae8be545662f15fcd5c1e97df77fdd2f41e Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Tue, 28 Sep 2021 21:48:58 -0700 Subject: [PATCH] Fix reading NCCH data in CIA header --- NDecrypt/N3DS/Headers/CIAHeader.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/NDecrypt/N3DS/Headers/CIAHeader.cs b/NDecrypt/N3DS/Headers/CIAHeader.cs index ff6e33b..0cb3933 100644 --- a/NDecrypt/N3DS/Headers/CIAHeader.cs +++ b/NDecrypt/N3DS/Headers/CIAHeader.cs @@ -1,4 +1,4 @@ -using System; +using System.Collections.Generic; using System.IO; namespace NDecrypt.N3DS.Headers @@ -74,7 +74,7 @@ namespace NDecrypt.N3DS.Headers /// /// Content file data /// - public NCCHHeader ContentFileData { get; set; } + public NCCHHeader[] Partitions { get; set; } /// /// Meta file data (Not a necessary component) @@ -122,13 +122,20 @@ namespace NDecrypt.N3DS.Headers if (reader.BaseStream.Position % 64 != 0) reader.BaseStream.Seek(64 - (reader.BaseStream.Position % 64), SeekOrigin.Current); - header.ContentFileData = NCCHHeader.Read(reader, readSignature: true); - if (header.ContentFileData == null) + long startingPosition = reader.BaseStream.Position; + List headers = new List(); + while (reader.BaseStream.Position < startingPosition + header.ContentSize) { - Console.WriteLine($"CIA content file data error: Unable to read NCCH header"); - return null; + long initPosition = reader.BaseStream.Position; + NCCHHeader ncchHeader = NCCHHeader.Read(reader, readSignature: true); + if (ncchHeader == null) + break; + + headers.Add(ncchHeader); + reader.BaseStream.Seek(initPosition + ncchHeader.ContentSizeInMediaUnits * 0x200, SeekOrigin.Begin); } + header.Partitions = headers.ToArray(); if (header.MetaSize > 0) header.MetaFileData = MetaFile.Read(reader);