diff --git a/NandReader/Program.cs b/NandReader/Program.cs index 81214873..48e18905 100644 --- a/NandReader/Program.cs +++ b/NandReader/Program.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.IO; using libhac; using libhac.Nand; +using libhac.Savefile; namespace NandReader { @@ -86,19 +87,37 @@ namespace NandReader var tickets = new List(); var system = nand.OpenSystemPartition(); - logger?.LogMessage("Searching save 80000000000000E1"); - var saveE1 = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read); - tickets.AddRange(Ticket.SearchTickets(saveE1, logger)); + var saveE1File = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read); + tickets.AddRange(ReadTickets(saveE1File)); - logger?.LogMessage("Searching save 80000000000000E2"); var saveE2 = system.OpenFile("save\\80000000000000E2", FileMode.Open, FileAccess.Read); - tickets.AddRange(Ticket.SearchTickets(saveE2, logger)); + tickets.AddRange(ReadTickets(saveE2)); logger?.LogMessage($"Found {tickets.Count} tickets"); return tickets.ToArray(); } + private static List ReadTickets(Stream savefile) + { + var tickets = new List(); + var save = new Savefile(savefile); + var ticketList = new BinaryReader(save.OpenFile("ticket_list.bin")); + var ticketFile = new BinaryReader(save.OpenFile("ticket.bin")); + + var titleId = ticketList.ReadUInt64(); + while (titleId != ulong.MaxValue) + { + ticketList.BaseStream.Position += 0x18; + var start = ticketFile.BaseStream.Position; + tickets.Add(new Ticket(ticketFile)); + ticketFile.BaseStream.Position = start + 0x400; + titleId = ticketList.ReadUInt64(); + } + + return tickets; + } + private static Keyset OpenKeyset() { var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); diff --git a/NandReaderGui/ViewModel/NandViewModel.cs b/NandReaderGui/ViewModel/NandViewModel.cs index 631f5b87..5250af3c 100644 --- a/NandReaderGui/ViewModel/NandViewModel.cs +++ b/NandReaderGui/ViewModel/NandViewModel.cs @@ -7,6 +7,7 @@ using GalaSoft.MvvmLight; using GalaSoft.MvvmLight.Command; using libhac; using libhac.Nand; +using libhac.Savefile; using libhac.XTSSharp; namespace NandReaderGui.ViewModel @@ -69,19 +70,36 @@ namespace NandReaderGui.ViewModel var tickets = new List(); var system = nand.OpenSystemPartition(); - logger?.LogMessage("Searching save 80000000000000E1"); - var saveE1 = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read); - tickets.AddRange(Ticket.SearchTickets(saveE1, logger)); + var saveE1File = system.OpenFile("save\\80000000000000E1", FileMode.Open, FileAccess.Read); + tickets.AddRange(ReadTickets(saveE1File)); - logger?.LogMessage("Searching save 80000000000000E2"); var saveE2 = system.OpenFile("save\\80000000000000E2", FileMode.Open, FileAccess.Read); - tickets.AddRange(Ticket.SearchTickets(saveE2, logger)); + tickets.AddRange(ReadTickets(saveE2)); logger?.LogMessage($"Found {tickets.Count} tickets"); return tickets.ToArray(); } + private static List ReadTickets(Stream savefile) + { + var tickets = new List(); + var save = new Savefile(savefile); + var ticketList = new BinaryReader(save.OpenFile("ticket_list.bin")); + var ticketFile = new BinaryReader(save.OpenFile("ticket.bin")); + + var titleId = ticketList.ReadUInt64(); + while (titleId != ulong.MaxValue) + { + ticketList.BaseStream.Position += 0x18; + var start = ticketFile.BaseStream.Position; + tickets.Add(new Ticket(ticketFile)); + ticketFile.BaseStream.Position = start + 0x400; + titleId = ticketList.ReadUInt64(); + } + + return tickets; + } private static Keyset OpenKeyset() { var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); diff --git a/libhac/Ticket.cs b/libhac/Ticket.cs index 7068ca02..adc54f7b 100644 --- a/libhac/Ticket.cs +++ b/libhac/Ticket.cs @@ -145,45 +145,6 @@ namespace libhac return stream.ToArray(); } - public static Ticket[] SearchTickets(Stream file, IProgressReport logger = null) - { - var reader = new BinaryReader(file, Encoding.Default, true); - file.Position += 0x140; - var tickets = new Dictionary(); - - logger?.SetTotal(file.Length); - - // Ticket starts occur at multiples of 0x400 - while (file.Position + 0x800 < file.Length) - { - var position = file.Position; - logger?.Report(position); - - if (reader.ReadUInt32() != 0x746f6f52) // Root - { - file.Position = position + 0x400; - continue; - } - - file.Position -= 0x144; - var sigType = (TicketSigType)reader.ReadUInt32(); - if (sigType < TicketSigType.Rsa4096Sha1 || sigType > TicketSigType.EcdsaSha256) - { - file.Position = position + 0x400; - continue; - } - - file.Position -= 4; - - var ticket = new Ticket(reader); - tickets[ticket.RightsId.ToHexString()] = ticket; - file.Position = position + 0x400; - } - - logger?.SetTotal(0); - return tickets.Values.ToArray(); - } - public byte[] GetTitleKey(Keyset keyset) { if (TitleKeyType == TitleKeyType.Common)