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.File;
|
2018-12-06 17:23:09 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2017-05-17 19:20:42 +02:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.io.PipedOutputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
2018-12-06 17:23:09 +01:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
|
import java.util.concurrent.CompletionException;
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
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.FSTEntry;
|
|
|
|
import de.mas.wiiu.jnus.implementations.NUSDataProvider;
|
|
|
|
import de.mas.wiiu.jnus.utils.CheckSumWrongException;
|
2018-12-06 17:23:09 +01:00
|
|
|
import de.mas.wiiu.jnus.utils.FileUtils;
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.utils.HashUtil;
|
2018-12-06 17:23:09 +01:00
|
|
|
import de.mas.wiiu.jnus.utils.Parallelizable;
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.utils.StreamUtils;
|
|
|
|
import de.mas.wiiu.jnus.utils.Utils;
|
|
|
|
import de.mas.wiiu.jnus.utils.cryptography.NUSDecryption;
|
|
|
|
import lombok.Getter;
|
2018-12-06 17:23:09 +01:00
|
|
|
import lombok.val;
|
2017-05-17 19:20:42 +02:00
|
|
|
import lombok.extern.java.Log;
|
|
|
|
|
|
|
|
@Log
|
|
|
|
public final class DecryptionService {
|
|
|
|
@Getter private final NUSTitle NUSTitle;
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
private boolean parallelizable = false;
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
public static DecryptionService getInstance(NUSTitle nustitle) {
|
2019-02-28 21:00:40 +01:00
|
|
|
return new DecryptionService(nustitle);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private DecryptionService(NUSTitle nustitle) {
|
2018-12-06 17:23:09 +01:00
|
|
|
if (nustitle.getDataProvider() instanceof Parallelizable) {
|
|
|
|
parallelizable = true;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
this.NUSTitle = nustitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Ticket getTicket() {
|
|
|
|
return getNUSTitle().getTicket();
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:41:17 +01:00
|
|
|
public void decryptFSTEntryTo(boolean useFullPath, FSTEntry entry, String outputPath, boolean skipExistingFile) {
|
2018-12-06 17:23:09 +01:00
|
|
|
try {
|
|
|
|
decryptFSTEntryToAsync(useFullPath, entry, outputPath, skipExistingFile).get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
throw new RuntimeException(e);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2018-12-06 17:23:09 +01:00
|
|
|
}
|
2018-03-10 12:22:10 +01:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
public CompletableFuture<Void> decryptFSTEntryToAsync(boolean useFullPath, FSTEntry entry, String outputPath, boolean skipExistingFile) {
|
|
|
|
return CompletableFuture.runAsync(() -> {
|
|
|
|
try {
|
|
|
|
if (entry.isNotInPackage() || entry.getContent() == null) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:40:11 +01:00
|
|
|
log.info("Decrypting " + entry.getFilename());
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
String targetFilePath = new StringBuilder().append(outputPath).append("/").append(entry.getFilename()).toString();
|
|
|
|
String fullPath = new StringBuilder().append(outputPath).toString();
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
if (useFullPath) {
|
|
|
|
targetFilePath = new StringBuilder().append(outputPath).append(entry.getFullPath()).toString();
|
|
|
|
fullPath = new StringBuilder().append(outputPath).append(entry.getPath()).toString();
|
|
|
|
if (entry.isDir()) { // If the entry is a directory. Create it and return.
|
|
|
|
Utils.createDir(targetFilePath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (entry.isDir()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
if (!Utils.createDir(fullPath)) {
|
2017-05-17 19:20:42 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-12-06 17:23:09 +01:00
|
|
|
|
|
|
|
File target = new File(targetFilePath);
|
|
|
|
|
|
|
|
if (skipExistingFile) {
|
|
|
|
File targetFile = new File(targetFilePath);
|
|
|
|
if (targetFile.exists()) {
|
|
|
|
if (entry.isDir()) {
|
2017-05-17 19:20:42 +02:00
|
|
|
return;
|
2018-12-06 17:23:09 +01:00
|
|
|
}
|
|
|
|
if (targetFile.length() == entry.getFileSize()) {
|
|
|
|
Content c = entry.getContent();
|
|
|
|
if (c.isHashed()) {
|
|
|
|
log.info("File already exists: " + entry.getFilename());
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if (Arrays.equals(HashUtil.hashSHA1(target, (int) c.getDecryptedFileSize()), c.getSHA2Hash())) {
|
|
|
|
log.info("File already exists: " + entry.getFilename());
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
log.info("File already exists with the same filesize, but the hash doesn't match: " + entry.getFilename());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
} else {
|
2018-12-06 17:23:09 +01:00
|
|
|
log.info("File already exists but the filesize doesn't match: " + entry.getFilename());
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
// to avoid having fragmented files.
|
|
|
|
FileUtils.FileAsOutputStreamWrapper(new File(targetFilePath), entry.getFileSize(), newOutputStream -> {
|
|
|
|
try {
|
|
|
|
decryptFSTEntryToStream(entry, newOutputStream);
|
|
|
|
} catch (CheckSumWrongException e) {
|
|
|
|
log.info("Hash doesn't match!");
|
|
|
|
// Wrapp it into a IOException
|
|
|
|
throw new IOException(e);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new CompletionException(ex);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2018-12-06 17:23:09 +01:00
|
|
|
});
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryToStream(FSTEntry entry, OutputStream outputStream) throws IOException, CheckSumWrongException {
|
|
|
|
if (entry.isNotInPackage() || entry.getContent() == null) {
|
2019-02-24 09:33:02 +01:00
|
|
|
outputStream.close();
|
2017-05-17 19:20:42 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Content c = entry.getContent();
|
|
|
|
|
|
|
|
long fileSize = entry.getFileSize();
|
|
|
|
long fileOffset = entry.getFileOffset();
|
|
|
|
long fileOffsetBlock = entry.getFileOffsetBlock();
|
|
|
|
|
|
|
|
NUSDataProvider dataProvider = getNUSTitle().getDataProvider();
|
|
|
|
|
|
|
|
InputStream in = dataProvider.getInputStreamFromContent(c, fileOffsetBlock);
|
2018-12-16 15:03:46 +01:00
|
|
|
if (in == null) {
|
|
|
|
String errormsg = "Failed to open the content " + c.getFilename() + " as input stream.";
|
|
|
|
log.warning(errormsg);
|
|
|
|
throw new FileNotFoundException(errormsg);
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
decryptFSTEntryFromStreams(in, outputStream, fileSize, fileOffset, c);
|
|
|
|
} catch (CheckSumWrongException e) {
|
2018-12-06 17:23:09 +01:00
|
|
|
if (entry.getContent().isUNKNWNFlag1Set()) {
|
|
|
|
log.info("Hash doesn't match. But file is optional. Don't worry.");
|
2017-05-17 19:20:42 +02:00
|
|
|
} else {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
2018-12-06 17:23:09 +01:00
|
|
|
sb.append("Hash doesn't match").append(System.lineSeparator());
|
2017-05-17 19:20:42 +02:00
|
|
|
sb.append("Detailed info:").append(System.lineSeparator());
|
|
|
|
sb.append(entry).append(System.lineSeparator());
|
|
|
|
sb.append(entry.getContent()).append(System.lineSeparator());
|
|
|
|
sb.append(String.format("%016x", this.NUSTitle.getTMD().getTitleID()));
|
|
|
|
sb.append(e.getMessage() + " Calculated Hash: " + Utils.ByteArrayToString(e.getGivenHash()) + ", expected hash: "
|
|
|
|
+ Utils.ByteArrayToString(e.getExpectedHash()));
|
|
|
|
log.info(sb.toString());
|
|
|
|
throw e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void decryptFSTEntryFromStreams(InputStream inputStream, OutputStream outputStream, long filesize, long fileoffset, Content content)
|
|
|
|
throws IOException, CheckSumWrongException {
|
|
|
|
decryptStreams(inputStream, outputStream, filesize, fileoffset, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void decryptContentFromStream(InputStream inputStream, OutputStream outputStream, Content content) throws IOException, CheckSumWrongException {
|
|
|
|
long filesize = content.getDecryptedFileSize();
|
|
|
|
log.info("Decrypting Content " + String.format("%08X", content.getID()));
|
|
|
|
decryptStreams(inputStream, outputStream, filesize, 0L, content);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void decryptStreams(InputStream inputStream, OutputStream outputStream, long size, long offset, Content content)
|
|
|
|
throws IOException, CheckSumWrongException {
|
|
|
|
NUSDecryption nusdecryption = new NUSDecryption(getTicket());
|
|
|
|
short contentIndex = (short) content.getIndex();
|
|
|
|
|
|
|
|
long encryptedFileSize = content.getEncryptedFileSize();
|
|
|
|
|
|
|
|
if (content.isEncrypted()) {
|
|
|
|
if (content.isHashed()) {
|
|
|
|
NUSDataProvider dataProvider = getNUSTitle().getDataProvider();
|
|
|
|
byte[] h3 = dataProvider.getContentH3Hash(content);
|
2018-12-16 15:03:46 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
nusdecryption.decryptFileStreamHashed(inputStream, outputStream, size, offset, (short) contentIndex, h3);
|
|
|
|
} else {
|
|
|
|
nusdecryption.decryptFileStream(inputStream, outputStream, size, (short) contentIndex, content.getSHA2Hash(), encryptedFileSize);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
StreamUtils.saveInputStreamToOutputStreamWithHash(inputStream, outputStream, size, content.getSHA2Hash(), encryptedFileSize);
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
synchronized (inputStream) {
|
|
|
|
inputStream.close();
|
|
|
|
}
|
|
|
|
synchronized (outputStream) {
|
|
|
|
outputStream.close();
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
public void decryptContentToSync(Content content, String outPath, boolean skipExistingFile) {
|
|
|
|
try {
|
|
|
|
decryptContentToAsync(content, outPath, skipExistingFile).get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
throw new RuntimeException(e);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2018-12-06 17:23:09 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
public CompletableFuture<Void> decryptContentToAsync(Content content, String outPath, boolean skipExistingFile) {
|
|
|
|
return CompletableFuture.runAsync(() -> {
|
|
|
|
try {
|
|
|
|
String targetFilePath = outPath + File.separator + content.getFilenameDecrypted();
|
|
|
|
if (skipExistingFile) {
|
|
|
|
File targetFile = new File(targetFilePath);
|
|
|
|
if (targetFile.exists()) {
|
|
|
|
if (targetFile.length() == content.getDecryptedFileSize()) {
|
|
|
|
log.info("File already exists : " + content.getFilenameDecrypted());
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
log.info("File already exists but the filesize doesn't match: " + content.getFilenameDecrypted());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
if (!Utils.createDir(outPath)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
log.info("Decrypting Content " + String.format("%08X", content.getID()));
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
FileOutputStream outputStream = new FileOutputStream(new File(targetFilePath));
|
|
|
|
|
|
|
|
decryptContentToStream(content, outputStream);
|
|
|
|
} catch (Exception ex) {
|
|
|
|
throw new CompletionException(ex);
|
|
|
|
}
|
|
|
|
});
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptContentToStream(Content content, OutputStream outputStream) throws IOException, CheckSumWrongException {
|
|
|
|
if (content == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NUSDataProvider dataProvider = getNUSTitle().getDataProvider();
|
|
|
|
InputStream inputStream = dataProvider.getInputStreamFromContent(content, 0);
|
|
|
|
|
|
|
|
decryptContentFromStream(inputStream, outputStream, content);
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
public InputStreamWithException getDecryptedOutputAsInputStream(FSTEntry fstEntry) throws IOException {
|
2017-05-17 19:20:42 +02:00
|
|
|
PipedInputStreamWithException in = new PipedInputStreamWithException();
|
|
|
|
PipedOutputStream out = new PipedOutputStream(in);
|
|
|
|
|
|
|
|
new Thread(() -> {
|
2018-12-06 17:23:09 +01:00
|
|
|
try {
|
2017-05-17 19:20:42 +02:00
|
|
|
decryptFSTEntryToStream(fstEntry, out);
|
|
|
|
in.throwException(null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
in.throwException(e);
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PipedInputStreamWithException getDecryptedContentAsInputStream(Content content) throws IOException, CheckSumWrongException {
|
|
|
|
PipedInputStreamWithException in = new PipedInputStreamWithException();
|
|
|
|
PipedOutputStream out = new PipedOutputStream(in);
|
|
|
|
|
|
|
|
new Thread(() -> {
|
2018-12-06 17:23:09 +01:00
|
|
|
try {// Throwing it in both cases is EXTREMLY important. Otherwise it'll end in a
|
|
|
|
// deadlock
|
2017-05-17 19:20:42 +02:00
|
|
|
decryptContentToStream(content, out);
|
|
|
|
in.throwException(null);
|
|
|
|
} catch (Exception e) {
|
|
|
|
in.throwException(e);
|
|
|
|
}
|
|
|
|
}).start();
|
|
|
|
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
// Decrypt FSTEntry to OutputStream
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
public void decryptFSTEntryTo(String entryFullPath, OutputStream outputStream) throws IOException, CheckSumWrongException {
|
|
|
|
FSTEntry entry = getNUSTitle().getFSTEntryByFullPath(entryFullPath);
|
|
|
|
if (entry == null) {
|
|
|
|
log.info("File not found");
|
2018-12-06 17:23:09 +01:00
|
|
|
throw new FileNotFoundException("File not found");
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
decryptFSTEntryToStream(entry, outputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
// Decrypt single FSTEntry to File
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
public void decryptFSTEntryTo(String entryFullPath, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntryTo(false, entryFullPath, outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryTo(boolean fullPath, String entryFullPath, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntryTo(fullPath, entryFullPath, outputFolder, getNUSTitle().isSkipExistingFiles());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryTo(String entryFullPath, String outputFolder, boolean skipExistingFiles) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntryTo(false, entryFullPath, outputFolder, getNUSTitle().isSkipExistingFiles());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryTo(boolean fullPath, String entryFullPath, String outputFolder, boolean skipExistingFiles)
|
|
|
|
throws IOException, CheckSumWrongException {
|
2018-12-06 17:23:09 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
FSTEntry entry = getNUSTitle().getFSTEntryByFullPath(entryFullPath);
|
|
|
|
if (entry == null) {
|
|
|
|
log.info("File not found");
|
2018-12-06 17:23:09 +01:00
|
|
|
CompletableFuture.completedFuture(null);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:41:17 +01:00
|
|
|
decryptFSTEntryTo(fullPath, entry, outputFolder, skipExistingFiles);
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryTo(FSTEntry entry, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntryTo(false, entry, outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryTo(FSTEntry entry, String outputFolder, boolean skipExistingFiles) throws IOException, CheckSumWrongException {
|
2018-12-06 17:41:17 +01:00
|
|
|
decryptFSTEntryTo(false, entry, outputFolder, getNUSTitle().isSkipExistingFiles());
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
public void decryptFSTEntryTo(boolean fullPath, FSTEntry entry, String outputFolder) throws IOException, CheckSumWrongException {
|
2018-12-06 17:41:17 +01:00
|
|
|
decryptFSTEntryTo(fullPath, entry, outputFolder, getNUSTitle().isSkipExistingFiles());
|
2018-12-06 17:23:09 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
// Decrypt list of FSTEntry to Files
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
public void decryptAllFSTEntriesTo(String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
Utils.createDir(outputFolder + File.separator + "code");
|
|
|
|
Utils.createDir(outputFolder + File.separator + "content");
|
|
|
|
Utils.createDir(outputFolder + File.separator + "meta");
|
|
|
|
decryptFSTEntriesTo(true, ".*", outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntriesTo(String regEx, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntriesTo(true, regEx, outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntriesTo(boolean fullPath, String regEx, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntryListTo(fullPath, getNUSTitle().getFSTEntriesByRegEx(regEx), outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptFSTEntryListTo(List<FSTEntry> list, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptFSTEntryListTo(true, list, outputFolder);
|
|
|
|
}
|
|
|
|
|
2018-12-06 17:23:09 +01:00
|
|
|
public CompletableFuture<Void> decryptFSTEntryListToAsync(boolean fullPath, List<FSTEntry> list, String outputFolder)
|
|
|
|
throws IOException, CheckSumWrongException {
|
|
|
|
return CompletableFuture.allOf(list.stream().map(entry -> decryptFSTEntryToAsync(fullPath, entry, outputFolder, getNUSTitle().isSkipExistingFiles()))
|
|
|
|
.toArray(CompletableFuture[]::new));
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
public void decryptFSTEntryListTo(boolean fullPath, List<FSTEntry> list, String outputFolder) throws IOException, CheckSumWrongException {
|
2018-12-06 17:23:09 +01:00
|
|
|
if (parallelizable && Settings.ALLOW_PARALLELISATION) {
|
|
|
|
try {
|
|
|
|
decryptFSTEntryListToAsync(fullPath, list, outputFolder).get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (val entry : list) {
|
2018-12-06 17:41:17 +01:00
|
|
|
decryptFSTEntryTo(fullPath, entry, outputFolder, getNUSTitle().isSkipExistingFiles());
|
2018-12-06 17:23:09 +01:00
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2018-12-06 17:23:09 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
// Save decrypted contents
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
public void decryptPlainContentByID(int ID, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptPlainContent(getTMDFromNUSTitle().getContentByID(ID), outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptPlainContentByIndex(int index, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptPlainContent(getTMDFromNUSTitle().getContentByIndex(index), outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptPlainContent(Content c, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptPlainContents(new ArrayList<Content>(Arrays.asList(c)), outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptPlainContents(List<Content> list, String outputFolder) throws IOException, CheckSumWrongException {
|
2018-12-06 17:23:09 +01:00
|
|
|
|
|
|
|
if (parallelizable && Settings.ALLOW_PARALLELISATION) {
|
|
|
|
try {
|
|
|
|
decryptPlainContentsAsync(list, outputFolder).get();
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
// wrap it.
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (val c : list) {
|
|
|
|
decryptContentToSync(c, outputFolder, getNUSTitle().isSkipExistingFiles());
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
2018-12-06 17:23:09 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public CompletableFuture<Void> decryptPlainContentsAsync(List<Content> list, String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
return CompletableFuture
|
|
|
|
.allOf(list.stream().map(c -> decryptContentToAsync(c, outputFolder, getNUSTitle().isSkipExistingFiles())).toArray(CompletableFuture[]::new));
|
2017-05-17 19:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void decryptAllPlainContents(String outputFolder) throws IOException, CheckSumWrongException {
|
|
|
|
decryptPlainContents(new ArrayList<Content>(getTMDFromNUSTitle().getAllContents().values()), outputFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
// Other
|
|
|
|
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!
|
|
|
|
private TMD getTMDFromNUSTitle() {
|
|
|
|
return getNUSTitle().getTMD();
|
|
|
|
}
|
|
|
|
}
|