diff --git a/src/de/mas/wiiu/jnus/NUSTitleLoaderFST.java b/src/de/mas/wiiu/jnus/NUSTitleLoaderFST.java new file mode 100644 index 0000000..57d63de --- /dev/null +++ b/src/de/mas/wiiu/jnus/NUSTitleLoaderFST.java @@ -0,0 +1,42 @@ +/**************************************************************************** + * Copyright (C) 2016-2018 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 java.io.IOException; +import java.text.ParseException; + +import de.mas.wiiu.jnus.entities.fst.FSTEntry; +import de.mas.wiiu.jnus.implementations.NUSDataProviderFST; +import de.mas.wiiu.jnus.interfaces.FSTDataProvider; + +public final class NUSTitleLoaderFST { + + private NUSTitleLoaderFST() { + } + + public static NUSTitle loadNUSTitle(FSTDataProvider dataProvider, byte[] commonKey) throws IOException, ParseException { + return loadNUSTitle(dataProvider, dataProvider.getRoot(), commonKey); + } + + public static NUSTitle loadNUSTitle(FSTDataProvider dataProvider, FSTEntry base, byte[] commonKey) throws IOException, ParseException { + NUSTitleConfig config = new NUSTitleConfig(); + config.setCommonKey(commonKey); + + return NUSTitleLoader.loadNusTitle(config, () -> new NUSDataProviderFST(dataProvider, base)); + } + +} diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java new file mode 100644 index 0000000..3f9f6bb --- /dev/null +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java @@ -0,0 +1,71 @@ +package de.mas.wiiu.jnus.implementations; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; + +import de.mas.wiiu.jnus.Settings; +import de.mas.wiiu.jnus.entities.content.Content; +import de.mas.wiiu.jnus.entities.fst.FSTEntry; +import de.mas.wiiu.jnus.interfaces.FSTDataProvider; +import de.mas.wiiu.jnus.interfaces.NUSDataProvider; +import de.mas.wiiu.jnus.utils.FSTUtils; + +public class NUSDataProviderFST implements NUSDataProvider { + private final FSTDataProvider fstDataProvider; + private final FSTEntry base; + + public NUSDataProviderFST(FSTDataProvider fstDataProvider, FSTEntry base) { + this.base = base; + this.fstDataProvider = fstDataProvider; + } + + public NUSDataProviderFST(FSTDataProvider fstDataProvider) { + this(fstDataProvider, fstDataProvider.getRoot()); + } + + @Override + public InputStream getInputStreamFromContent(Content content, long offset, Optional size) throws IOException { + String filename = content.getFilename(); + Optional contentFileOpt = FSTUtils.getChildOfDirectory(base, filename); + FSTEntry contentFile = contentFileOpt.orElseThrow(() -> new FileNotFoundException(filename + " was not found.")); + return fstDataProvider.readFileAsStream(contentFile, offset, size); + } + + @Override + public Optional getContentH3Hash(Content content) throws IOException { + return readFileByFilename(base, String.format("%08X%s", content.getID(), Settings.H3_EXTENTION)); + } + + private Optional readFileByFilename(FSTEntry base, String filename) throws IOException { + Optional entryOpt = FSTUtils.getChildOfDirectory(base, filename); + if (entryOpt.isPresent()) { + + FSTEntry entry = entryOpt.get(); + return Optional.of(fstDataProvider.readFile(entry)); + } + return Optional.empty(); + } + + @Override + public Optional getRawTMD() throws IOException { + return readFileByFilename(base, Settings.TMD_FILENAME); + } + + @Override + public Optional getRawTicket() throws IOException { + return readFileByFilename(base, Settings.TICKET_FILENAME); + } + + @Override + public Optional getRawCert() throws IOException { + return readFileByFilename(base, Settings.CERT_FILENAME); + } + + @Override + public void cleanup() throws IOException { + + } + +}