Add a NUSTitleLoaderFST which allows you to create a NUSTitle from a FSTDataProvider. Useful for recursive mounting

This commit is contained in:
Maschell 2019-04-10 20:01:19 +02:00
parent de7909eb34
commit 8e4faa58dc
2 changed files with 113 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/>.
****************************************************************************/
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));
}
}

View File

@ -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<Long> size) throws IOException {
String filename = content.getFilename();
Optional<FSTEntry> contentFileOpt = FSTUtils.getChildOfDirectory(base, filename);
FSTEntry contentFile = contentFileOpt.orElseThrow(() -> new FileNotFoundException(filename + " was not found."));
return fstDataProvider.readFileAsStream(contentFile, offset, size);
}
@Override
public Optional<byte[]> getContentH3Hash(Content content) throws IOException {
return readFileByFilename(base, String.format("%08X%s", content.getID(), Settings.H3_EXTENTION));
}
private Optional<byte[]> readFileByFilename(FSTEntry base, String filename) throws IOException {
Optional<FSTEntry> entryOpt = FSTUtils.getChildOfDirectory(base, filename);
if (entryOpt.isPresent()) {
FSTEntry entry = entryOpt.get();
return Optional.of(fstDataProvider.readFile(entry));
}
return Optional.empty();
}
@Override
public Optional<byte[]> getRawTMD() throws IOException {
return readFileByFilename(base, Settings.TMD_FILENAME);
}
@Override
public Optional<byte[]> getRawTicket() throws IOException {
return readFileByFilename(base, Settings.TICKET_FILENAME);
}
@Override
public Optional<byte[]> getRawCert() throws IOException {
return readFileByFilename(base, Settings.CERT_FILENAME);
}
@Override
public void cleanup() throws IOException {
}
}