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;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2018-12-06 16:48:36 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.CompletionException;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.entities.content.Content;
|
|
|
|
import de.mas.wiiu.jnus.implementations.NUSDataProvider;
|
|
|
|
import de.mas.wiiu.jnus.utils.FileUtils;
|
2018-12-06 16:48:36 +01:00
|
|
|
import de.mas.wiiu.jnus.utils.Parallelizable;
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.utils.Utils;
|
2016-12-12 21:01:12 +01:00
|
|
|
import lombok.Getter;
|
2017-05-17 19:20:42 +02:00
|
|
|
import lombok.extern.java.Log;
|
|
|
|
|
|
|
|
@Log
|
|
|
|
public final class ExtractionService {
|
|
|
|
private static Map<NUSTitle, ExtractionService> instances = new HashMap<>();
|
|
|
|
|
|
|
|
@Getter private final NUSTitle NUSTitle;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
2018-12-06 16:48:36 +01:00
|
|
|
private boolean parallelizable = false;
|
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public static ExtractionService getInstance(NUSTitle nustitle) {
|
2017-05-17 19:20:42 +02:00
|
|
|
if (!instances.containsKey(nustitle)) {
|
2016-12-12 21:01:12 +01:00
|
|
|
instances.put(nustitle, new ExtractionService(nustitle));
|
|
|
|
}
|
|
|
|
return instances.get(nustitle);
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
private ExtractionService(NUSTitle nustitle) {
|
2018-12-06 16:48:36 +01:00
|
|
|
if (nustitle.getDataProvider() instanceof Parallelizable) {
|
|
|
|
parallelizable = true;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
this.NUSTitle = nustitle;
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
private NUSDataProvider getDataProvider() {
|
2016-12-12 21:01:12 +01:00
|
|
|
return getNUSTitle().getDataProvider();
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public void extractAllEncrpytedContentFileHashes(String outputFolder) throws IOException {
|
2017-05-17 19:20:42 +02:00
|
|
|
extractEncryptedContentHashesTo(new ArrayList<Content>(getNUSTitle().getTMD().getAllContents().values()), outputFolder);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public void extractEncryptedContentHashesTo(List<Content> list, String outputFolder) throws IOException {
|
|
|
|
Utils.createDir(outputFolder);
|
|
|
|
NUSDataProvider dataProvider = getDataProvider();
|
2017-05-17 19:20:42 +02:00
|
|
|
for (Content c : list) {
|
2016-12-12 21:01:12 +01:00
|
|
|
dataProvider.saveContentH3Hash(c, outputFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void extractAllEncryptedContentFiles(String outputFolder) throws IOException {
|
|
|
|
extractAllEncryptedContentFilesWithHashesTo(outputFolder);
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public void extractAllEncryptedContentFilesWithoutHashesTo(String outputFolder) throws IOException {
|
2017-05-17 19:20:42 +02:00
|
|
|
extractEncryptedContentFilesTo(new ArrayList<Content>(getNUSTitle().getTMD().getAllContents().values()), outputFolder, false);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public void extractAllEncryptedContentFilesWithHashesTo(String outputFolder) throws IOException {
|
2017-05-17 19:20:42 +02:00
|
|
|
extractEncryptedContentFilesTo(new ArrayList<Content>(getNUSTitle().getTMD().getAllContents().values()), outputFolder, true);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 16:48:36 +01:00
|
|
|
public void extractEncryptedContentTo(Content content, String outputFolder, boolean withHashes) throws IOException {
|
|
|
|
NUSDataProvider dataProvider = getDataProvider();
|
|
|
|
if (withHashes) {
|
|
|
|
dataProvider.saveEncryptedContentWithH3Hash(content, outputFolder);
|
|
|
|
} else {
|
|
|
|
dataProvider.saveEncryptedContent(content, outputFolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
public void extractEncryptedContentFilesTo(List<Content> list, String outputFolder, boolean withHashes) throws IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
Utils.createDir(outputFolder);
|
2018-12-06 16:48:36 +01:00
|
|
|
if (parallelizable) {
|
|
|
|
try {
|
|
|
|
CompletableFuture.allOf(list.stream().map((Content c) -> CompletableFuture.runAsync(() -> {
|
|
|
|
try {
|
|
|
|
extractEncryptedContentTo(c, outputFolder, withHashes);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new CompletionException(e);
|
|
|
|
}
|
|
|
|
})).toArray(CompletableFuture[]::new)).get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (Content c : list) {
|
|
|
|
extractEncryptedContentTo(c, outputFolder, withHashes);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void extractTMDTo(String output) throws IOException {
|
|
|
|
Utils.createDir(output);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
byte[] rawTMD = getDataProvider().getRawTMD();
|
|
|
|
|
|
|
|
if (rawTMD == null || rawTMD.length == 0) {
|
|
|
|
log.info("Couldn't write TMD: No TMD loaded");
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String tmd_path = output + File.separator + Settings.TMD_FILENAME;
|
2017-05-17 19:20:42 +02:00
|
|
|
log.info("Extracting TMD to: " + tmd_path);
|
|
|
|
FileUtils.saveByteArrayToFile(tmd_path, rawTMD);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
2018-12-06 16:48:36 +01:00
|
|
|
public boolean extractTicketTo(String output) throws IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
Utils.createDir(output);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
byte[] rawTicket = getDataProvider().getRawTicket();
|
|
|
|
|
|
|
|
if (rawTicket == null || rawTicket.length == 0) {
|
|
|
|
log.info("Couldn't write Ticket: No Ticket loaded");
|
2018-12-06 16:48:36 +01:00
|
|
|
return false;
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
String ticket_path = output + File.separator + Settings.TICKET_FILENAME;
|
2017-05-17 19:20:42 +02:00
|
|
|
log.info("Extracting Ticket to: " + ticket_path);
|
2018-12-06 16:48:36 +01:00
|
|
|
return FileUtils.saveByteArrayToFile(ticket_path, rawTicket);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public void extractCertTo(String output) throws IOException {
|
|
|
|
Utils.createDir(output);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
byte[] rawCert = getDataProvider().getRawCert();
|
|
|
|
|
|
|
|
if (rawCert == null || rawCert.length == 0) {
|
|
|
|
log.info("Couldn't write Cert: No Cert loaded");
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
String cert_path = output + File.separator + Settings.CERT_FILENAME;
|
2017-05-17 19:20:42 +02:00
|
|
|
log.info("Extracting Cert to: " + cert_path);
|
|
|
|
FileUtils.saveByteArrayToFile(cert_path, rawCert);
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public void extractAll(String outputFolder) throws IOException {
|
|
|
|
Utils.createDir(outputFolder);
|
2018-12-06 16:48:36 +01:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
extractAllEncryptedContentFilesWithHashesTo(outputFolder);
|
|
|
|
extractCertTo(outputFolder);
|
|
|
|
extractTMDTo(outputFolder);
|
|
|
|
extractTicketTo(outputFolder);
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|