2018-03-10 12:22:10 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* 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/>.
|
|
|
|
****************************************************************************/
|
2017-05-17 19:20:42 +02:00
|
|
|
package de.mas.wiiu.jnus;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import de.mas.wiiu.jnus.entities.TMD;
|
|
|
|
import de.mas.wiiu.jnus.entities.Ticket;
|
|
|
|
import de.mas.wiiu.jnus.entities.content.Content;
|
|
|
|
import de.mas.wiiu.jnus.entities.fst.FST;
|
|
|
|
import de.mas.wiiu.jnus.implementations.NUSDataProvider;
|
|
|
|
import de.mas.wiiu.jnus.utils.StreamUtils;
|
|
|
|
import de.mas.wiiu.jnus.utils.cryptography.AESDecryption;
|
|
|
|
import lombok.extern.java.Log;
|
|
|
|
|
|
|
|
@Log
|
|
|
|
abstract class NUSTitleLoader {
|
|
|
|
protected NUSTitleLoader() {
|
|
|
|
// should be empty
|
|
|
|
}
|
|
|
|
|
|
|
|
public NUSTitle loadNusTitle(NUSTitleConfig config) throws Exception {
|
|
|
|
NUSTitle result = new NUSTitle();
|
|
|
|
|
|
|
|
NUSDataProvider dataProvider = getDataProvider(result, config);
|
|
|
|
result.setDataProvider(dataProvider);
|
|
|
|
|
2019-04-10 18:43:44 +02:00
|
|
|
byte[] tmdData = dataProvider.getRawTMD().orElseThrow(() -> new ParseException("No TMD data found", 0));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2019-04-10 18:43:44 +02:00
|
|
|
TMD tmd = TMD.parseTMD(tmdData);
|
|
|
|
result.setTMD(tmd);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 15:57:36 +01:00
|
|
|
if (config.isNoDecryption()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
Ticket ticket = config.getTicket();
|
|
|
|
if (ticket == null) {
|
2019-04-10 18:43:44 +02:00
|
|
|
Optional<byte[]> ticketOpt = dataProvider.getRawTicket();
|
|
|
|
if (ticketOpt.isPresent()) {
|
|
|
|
ticket = Ticket.parseTicket(ticketOpt.get(), config.getCommonKey());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ticket == null) {
|
|
|
|
new ParseException("Failed to get ticket data",0);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
result.setTicket(ticket);
|
|
|
|
|
|
|
|
Content fstContent = tmd.getContentByIndex(0);
|
|
|
|
|
2019-04-10 18:43:44 +02:00
|
|
|
InputStream fstContentEncryptedStream = dataProvider.getInputStreamFromContent(fstContent, 0, Optional.of(fstContent.getEncryptedFileSize()));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
byte[] fstBytes = StreamUtils.getBytesFromStream(fstContentEncryptedStream, (int) fstContent.getEncryptedFileSize());
|
|
|
|
|
|
|
|
if (fstContent.isEncrypted()) {
|
|
|
|
AESDecryption aesDecryption = new AESDecryption(ticket.getDecryptedKey(), new byte[0x10]);
|
|
|
|
fstBytes = aesDecryption.decrypt(fstBytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
Map<Integer, Content> contents = tmd.getAllContents();
|
|
|
|
|
|
|
|
FST fst = FST.parseFST(fstBytes, contents);
|
|
|
|
result.setFST(fst);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract NUSDataProvider getDataProvider(NUSTitle title, NUSTitleConfig config);
|
|
|
|
}
|