2017-05-17 19:20:42 +02:00
|
|
|
package de.mas.wiiu.jnus.implementations;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
import de.mas.wiiu.jnus.NUSTitle;
|
|
|
|
import de.mas.wiiu.jnus.Settings;
|
|
|
|
import de.mas.wiiu.jnus.entities.content.Content;
|
|
|
|
import de.mas.wiiu.jnus.utils.FileUtils;
|
|
|
|
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.NonNull;
|
2016-12-12 21:01:12 +01:00
|
|
|
import lombok.extern.java.Log;
|
|
|
|
|
|
|
|
@Log
|
|
|
|
/**
|
|
|
|
* Service Methods for loading NUS/Content data from
|
|
|
|
* different sources
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
2016-12-12 21:01:12 +01:00
|
|
|
* @author Maschell
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public abstract class NUSDataProvider {
|
2017-05-17 19:20:42 +02:00
|
|
|
@Getter private final NUSTitle NUSTitle;
|
|
|
|
|
|
|
|
public NUSDataProvider(NUSTitle title) {
|
|
|
|
this.NUSTitle = title;
|
|
|
|
}
|
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
|
|
|
* Saves the given content encrypted with his .h3 file in the given directory.
|
|
|
|
* The Target directory will be created if it's missing.
|
|
|
|
* If the content is not hashed, no .h3 will be saved
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* Content that should be saved
|
|
|
|
* @param outputFolder
|
|
|
|
* Target directory where the files will be stored in.
|
2016-12-12 21:01:12 +01:00
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public void saveEncryptedContentWithH3Hash(@NonNull Content content, @NonNull String outputFolder) throws IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
saveContentH3Hash(content, outputFolder);
|
|
|
|
saveEncryptedContent(content, outputFolder);
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
|
|
|
* Saves the .h3 file of the given content into the given directory.
|
|
|
|
* The Target directory will be created if it's missing.
|
|
|
|
* If the content is not hashed, no .h3 will be saved
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* The content of which the h3 hashes should be saved
|
2016-12-12 21:01:12 +01:00
|
|
|
* @param outputFolder
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public void saveContentH3Hash(@NonNull Content content, @NonNull String outputFolder) throws IOException {
|
|
|
|
if (!content.isHashed()) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
byte[] hash = getContentH3Hash(content);
|
2017-05-17 19:20:42 +02:00
|
|
|
if (hash == null || hash.length == 0) {
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
String h3Filename = String.format("%08X%s", content.getID(), Settings.H3_EXTENTION);
|
2016-12-12 21:01:12 +01:00
|
|
|
File output = new File(outputFolder + File.separator + h3Filename);
|
2017-05-17 19:20:42 +02:00
|
|
|
if (output.exists() && output.length() == hash.length) {
|
|
|
|
log.info(h3Filename + " already exists");
|
2016-12-12 21:01:12 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
log.info("Saving " + h3Filename + " ");
|
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
FileUtils.saveByteArrayToFile(output, hash);
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
/**
|
|
|
|
* Saves the given content encrypted in the given directory.
|
|
|
|
* The Target directory will be created if it's missing.
|
|
|
|
* If the content is not encrypted at all, it will be just saved anyway.
|
2017-05-17 19:20:42 +02:00
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* Content that should be saved
|
|
|
|
* @param outputFolder
|
|
|
|
* Target directory where the files will be stored in.
|
2016-12-12 21:01:12 +01:00
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public void saveEncryptedContent(@NonNull Content content, @NonNull String outputFolder) throws IOException {
|
2016-12-12 21:01:12 +01:00
|
|
|
Utils.createDir(outputFolder);
|
|
|
|
InputStream inputStream = getInputStreamFromContent(content, 0);
|
2017-05-17 19:20:42 +02:00
|
|
|
if (inputStream == null) {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("Couldn't save encrypted content. Input stream was null");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
File output = new File(outputFolder + File.separator + content.getFilename());
|
2017-05-17 19:20:42 +02:00
|
|
|
if (output.exists()) {
|
|
|
|
if (output.length() == content.getEncryptedFileSizeAligned()) {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("Encrypted content alreadys exists, skipped");
|
|
|
|
return;
|
2017-05-17 19:20:42 +02:00
|
|
|
} else {
|
2016-12-12 21:01:12 +01:00
|
|
|
log.info("Encrypted content alreadys exists, but the length is not as expected. Saving it again");
|
|
|
|
}
|
|
|
|
}
|
2017-05-17 19:20:42 +02:00
|
|
|
FileUtils.saveInputStreamToFile(output, inputStream, content.getEncryptedFileSizeAligned());
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param content
|
|
|
|
* @param offset
|
|
|
|
* @return
|
|
|
|
* @throws IOException
|
|
|
|
*/
|
2017-05-17 19:20:42 +02:00
|
|
|
public abstract InputStream getInputStreamFromContent(Content content, long offset) throws IOException;
|
2016-12-12 21:01:12 +01:00
|
|
|
|
2017-05-17 19:20:42 +02:00
|
|
|
// TODO: JavaDocs
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getContentH3Hash(Content content) throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getRawTMD() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getRawTicket() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract byte[] getRawCert() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
public abstract void cleanup() throws IOException;
|
2017-05-17 19:20:42 +02:00
|
|
|
|
2016-12-12 21:01:12 +01:00
|
|
|
}
|