Fixed Nso implementation

Added more keyset printing
This commit is contained in:
shadowninja108 2018-09-23 00:24:14 -07:00
parent 0efc550fa6
commit 86aa80c87b
2 changed files with 46 additions and 17 deletions

View file

@ -269,9 +269,11 @@ namespace LibHac
public static class ExternalKeys
{
private const int TitleKeySize = 0x10;
private static readonly Dictionary<string, KeyValue> CommonKeyDict;
private static readonly Dictionary<string, KeyValue> UniqueKeyDict;
private static readonly Dictionary<string, KeyValue> AllKeyDict;
public static readonly Dictionary<string, KeyValue>
CommonKeyDict,
UniqueKeyDict,
AllKeyDict;
static ExternalKeys()
{
@ -393,12 +395,12 @@ namespace LibHac
}
}
public static string PrintKeys(Keyset keyset)
public static string PrintKeys(Keyset keyset, Dictionary<string, KeyValue> 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<byte[], byte[]> 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<KeyValue> CreateCommonKeyList()
{
var keys = new List<KeyValue>
@ -489,7 +522,7 @@ namespace LibHac
return keys;
}
private class KeyValue
public class KeyValue
{
public readonly string Name;
public readonly int Size;

View file

@ -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;
}