public NcaFsHeader(BinaryReader reader) { long start = reader.BaseStream.Position; Field0 = reader.ReadByte(); Field1 = reader.ReadByte(); PartitionType = (SectionPartitionType)reader.ReadByte(); FsType = (SectionFsType)reader.ReadByte(); CryptType = (SectionCryptType)reader.ReadByte(); reader.BaseStream.Position += 3; if (PartitionType == SectionPartitionType.Pfs0 && FsType == SectionFsType.Pfs0) { Type = SectionType.Pfs0; Pfs = new PfsSuperblock(reader); } else if (PartitionType == SectionPartitionType.Romfs && FsType == SectionFsType.Romfs) { if (CryptType == SectionCryptType.BKTR) { Type = SectionType.Bktr; Bktr = new BktrSuperblock(reader); } else { Type = SectionType.Romfs; Romfs = new RomfsSuperblock(reader); } } Ctr = reader.ReadBytes(8).Reverse().ToArray(); reader.BaseStream.Position = start + 512; }
public BktrCryptoStream(Stream baseStream, byte[] key, long offset, long length, long counterOffset, byte[] ctrHi, BktrSuperblock bktr) : base(baseStream, key, offset, length, counterOffset, ctrHi) { BktrHeader header = bktr.SubsectionHeader; byte[] subsectionBytes; using (var streamDec = new RandomAccessSectorStream(new Aes128CtrStream(baseStream, key, offset, length, counterOffset, ctrHi))) { streamDec.Position = header.Offset; subsectionBytes = new byte[header.Size]; streamDec.Read(subsectionBytes, 0, subsectionBytes.Length); } using (var reader = new BinaryReader(new MemoryStream(subsectionBytes))) { SubsectionBlock = new SubsectionBlock(reader); } foreach (SubsectionBucket bucket in SubsectionBlock.Buckets) { SubsectionEntries.AddRange(bucket.Entries); } // Add a subsection for the BKTR headers to make things easier var headerSubsection = new SubsectionEntry { Offset = bktr.RelocationHeader.Offset, Counter = (uint)(ctrHi[4] << 24 | ctrHi[5] << 16 | ctrHi[6] << 8 | ctrHi[7]), OffsetEnd = long.MaxValue }; SubsectionEntries.Add(headerSubsection); for (int i = 0; i < SubsectionEntries.Count - 1; i++) { SubsectionEntries[i].Next = SubsectionEntries[i + 1]; SubsectionEntries[i].OffsetEnd = SubsectionEntries[i + 1].Offset; } SubsectionOffsets = SubsectionEntries.Select(x => x.Offset).ToList(); CurrentEntry = GetSubsectionEntry(0); UpdateCounterSubsection(CurrentEntry.Counter); baseStream.Position = offset; }