From ac1d08645d369bcbf848710101b1894f6f78224d Mon Sep 17 00:00:00 2001 From: Maschell Date: Sun, 7 Apr 2019 14:56:32 +0200 Subject: [PATCH] Add support for the "LocalBackup" format --- .../wiiu/jnus/NUSTitleLoaderLocalBackup.java | 59 ++++++++++++ .../NUSDataProviderLocalBackup.java | 94 +++++++++++++++++++ 2 files changed, 153 insertions(+) create mode 100644 src/de/mas/wiiu/jnus/NUSTitleLoaderLocalBackup.java create mode 100644 src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java diff --git a/src/de/mas/wiiu/jnus/NUSTitleLoaderLocalBackup.java b/src/de/mas/wiiu/jnus/NUSTitleLoaderLocalBackup.java new file mode 100644 index 0000000..e7cc624 --- /dev/null +++ b/src/de/mas/wiiu/jnus/NUSTitleLoaderLocalBackup.java @@ -0,0 +1,59 @@ +/**************************************************************************** + * Copyright (C) 2016-2019 Maschell + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + ****************************************************************************/ + +package de.mas.wiiu.jnus; + +import de.mas.wiiu.jnus.entities.Ticket; +import de.mas.wiiu.jnus.implementations.NUSDataProvider; +import de.mas.wiiu.jnus.implementations.NUSDataProviderLocalBackup; + +public final class NUSTitleLoaderLocalBackup extends NUSTitleLoader { + + private NUSTitleLoaderLocalBackup() { + super(); + } + + public static NUSTitle loadNUSTitle(String inputPath) throws Exception { + return loadNUSTitle(inputPath, (short) Settings.LATEST_TMD_VERSION); + } + + public static NUSTitle loadNUSTitle(String inputPath, short titleVersion) throws Exception { + return loadNUSTitle(inputPath, titleVersion, null); + } + + public static NUSTitle loadNUSTitle(String inputPath, short titleVersion, Ticket ticket) throws Exception { + NUSTitleLoader loader = new NUSTitleLoaderLocalBackup(); + NUSTitleConfig config = new NUSTitleConfig(); + + if (ticket != null) { + config.setTicket(ticket); + } else { + config.setNoDecryption(true); + } + + config.setVersion(titleVersion); + config.setInputPath(inputPath); + + return loader.loadNusTitle(config); + } + + @Override + protected NUSDataProvider getDataProvider(NUSTitle title, NUSTitleConfig config) { + return new NUSDataProviderLocalBackup(title, config.getInputPath(), (short) config.getVersion()); + } + +} diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java new file mode 100644 index 0000000..e391a23 --- /dev/null +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java @@ -0,0 +1,94 @@ +package de.mas.wiiu.jnus.implementations; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.util.Optional; + +import de.mas.wiiu.jnus.NUSTitle; +import de.mas.wiiu.jnus.Settings; +import de.mas.wiiu.jnus.entities.content.Content; +import de.mas.wiiu.jnus.implementations.NUSDataProvider; +import de.mas.wiiu.jnus.utils.StreamUtils; +import lombok.Getter; + +public class NUSDataProviderLocalBackup extends NUSDataProvider { + @Getter private final String localPath; + private final short titleVersion; + + public NUSDataProviderLocalBackup(NUSTitle nustitle, String localPath) { + this(nustitle, localPath, (short) Settings.LATEST_TMD_VERSION); + } + + public NUSDataProviderLocalBackup(NUSTitle nustitle, String localPath, short version) { + super(nustitle); + this.localPath = localPath; + this.titleVersion = version; + } + + private String getFilePathOnDisk(Content c) { + return getLocalPath() + File.separator + c.getFilename(); + } + + @Override + public InputStream getInputStreamFromContent(Content content, long offset, Optional size) throws IOException { + File filepath = new File(getFilePathOnDisk(content)); + if (!filepath.exists()) { + return null; + } + InputStream in = new FileInputStream(filepath); + StreamUtils.skipExactly(in, offset); + return in; + } + + @Override + public byte[] getContentH3Hash(Content content) throws IOException { + String h3Path = getLocalPath() + File.separator + String.format("%08X.h3", content.getID()); + File h3File = new File(h3Path); + if (!h3File.exists()) { + return new byte[0]; + } + return Files.readAllBytes(h3File.toPath()); + } + + @Override + public byte[] getRawTMD() throws IOException { + String inputPath = getLocalPath(); + String tmdPath = inputPath + File.separator + Settings.TMD_FILENAME; + if (titleVersion != Settings.LATEST_TMD_VERSION) { + tmdPath = inputPath + File.separator + "v" + titleVersion + File.separator + Settings.TMD_FILENAME; + } + File tmdFile = new File(tmdPath); + return Files.readAllBytes(tmdFile.toPath()); + } + + @Override + public byte[] getRawTicket() throws IOException { + String inputPath = getLocalPath(); + String ticketPath = inputPath + File.separator + Settings.TICKET_FILENAME; + File ticketFile = new File(ticketPath); + return Files.readAllBytes(ticketFile.toPath()); + } + + @Override + public byte[] getRawCert() throws IOException { + String inputPath = getLocalPath(); + String certPath = inputPath + File.separator + Settings.CERT_FILENAME; + File certFile = new File(certPath); + return Files.readAllBytes(certFile.toPath()); + } + + @Override + public void cleanup() throws IOException { + // We don't need this + } + + @Override + public String toString() { + String titleVersionString = titleVersion == Settings.LATEST_TMD_VERSION ? "latest" : Short.toString(titleVersion); + return "NUSDataProviderLocalBackup [localPath=" + localPath + ", titleVersion=" + titleVersionString + "]"; + } + +}