From 86aa80c87b32ef8b454937ac07de056f8ac06c2a Mon Sep 17 00:00:00 2001 From: shadowninja108 Date: Sun, 23 Sep 2018 00:24:14 -0700 Subject: [PATCH] Fixed Nso implementation Added more keyset printing --- LibHac/Keyset.cs | 47 ++++++++++++++++++++++++++++++++++++++++------- libhac/Nso.cs | 16 ++++++---------- 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/LibHac/Keyset.cs b/LibHac/Keyset.cs index 68c256bd..0ef37cc3 100644 --- a/LibHac/Keyset.cs +++ b/LibHac/Keyset.cs @@ -269,9 +269,11 @@ namespace LibHac public static class ExternalKeys { private const int TitleKeySize = 0x10; - private static readonly Dictionary CommonKeyDict; - private static readonly Dictionary UniqueKeyDict; - private static readonly Dictionary AllKeyDict; + + public static readonly Dictionary + CommonKeyDict, + UniqueKeyDict, + AllKeyDict; static ExternalKeys() { @@ -393,12 +395,12 @@ namespace LibHac } } - public static string PrintKeys(Keyset keyset) + public static string PrintKeys(Keyset keyset, Dictionary dict) { var sb = new StringBuilder(); - int maxNameLength = CommonKeyDict.Values.Max(x => x.Name.Length); + int maxNameLength = dict.Values.Max(x => x.Name.Length); - foreach (KeyValue keySlot in CommonKeyDict.Values.OrderBy(x => x.Name)) + foreach (KeyValue keySlot in dict.Values.OrderBy(x => x.Name)) { byte[] key = keySlot.GetKey(keyset); if (key.IsEmpty()) continue; @@ -410,6 +412,37 @@ namespace LibHac return sb.ToString(); } + public static string PrintCommonKeys(Keyset keyset) + { + return PrintKeys(keyset, CommonKeyDict); + } + + public static string PrintUniqueKeys(Keyset keyset) + { + return PrintKeys(keyset, UniqueKeyDict); + } + + public static string PrintAllKeys(Keyset keyset) + { + return PrintKeys(keyset, AllKeyDict); + } + + public static string PrintTitleKeys(Keyset keyset) + { + var sb = new StringBuilder(); + int maxNameLength = keyset.TitleKeys.Values.Max(x => x.Length); + + foreach (KeyValuePair kv in keyset.TitleKeys) + { + byte[] key = kv.Key; + byte[] value = kv.Value; + var line = $"{key.ToHexString().PadRight(maxNameLength)} = {value.ToHexString()}"; + sb.AppendLine(line); + } + + return sb.ToString(); + } + private static List CreateCommonKeyList() { var keys = new List @@ -489,7 +522,7 @@ namespace LibHac return keys; } - private class KeyValue + public class KeyValue { public readonly string Name; public readonly int Size; diff --git a/libhac/Nso.cs b/libhac/Nso.cs index 5b412593..d8ea6404 100644 --- a/libhac/Nso.cs +++ b/libhac/Nso.cs @@ -36,11 +36,11 @@ namespace LibHac dataSection.IsCompressed = flags[2]; dataSection.CheckHash = flags[5]; - ReadSegmentHeader(textSection); + ReadSegmentHeader(textSection, reader); reader.ReadUInt32(); // Module offset (TODO) - ReadSegmentHeader(rodataSection); + ReadSegmentHeader(rodataSection, reader); reader.ReadUInt32(); // Module file size - ReadSegmentHeader(dataSection); + ReadSegmentHeader(dataSection, reader); BssSize = reader.ReadUInt32(); reader.Read(BuildID, 0, 0x20); textSection.CompressedSize = reader.ReadUInt32(); @@ -49,7 +49,7 @@ namespace LibHac reader.ReadBytes(0x1C); // Padding RodataRelativeExtents = new RodataRelativeExtent[] { - ReadRodataRelativeExtent(), ReadRodataRelativeExtent(), ReadRodataRelativeExtent() + ReadRodataRelativeExtent(reader), ReadRodataRelativeExtent(reader), ReadRodataRelativeExtent(reader) }; reader.Read(textSection.Hash, 0, 0x20); @@ -60,22 +60,18 @@ namespace LibHac reader.Close(); } - public void ReadSegmentHeader(NsoSection section) + public void ReadSegmentHeader(NsoSection section, BinaryReader reader) { - BinaryReader reader = new BinaryReader(StreamSource.CreateStream()); section.FileOffset = reader.ReadUInt32(); section.MemoryOffset = reader.ReadUInt32(); section.DecompressedSize = reader.ReadUInt32(); - reader.Close(); } - public RodataRelativeExtent ReadRodataRelativeExtent() + public RodataRelativeExtent ReadRodataRelativeExtent(BinaryReader reader) { - BinaryReader reader = new BinaryReader(StreamSource.CreateStream()); RodataRelativeExtent extent = new RodataRelativeExtent(); extent.RegionRodataOffset = reader.ReadUInt32(); extent.RegionSize = reader.ReadUInt32(); - reader.Close(); return extent; }