JNUSLib/src/de/mas/wiiu/jnus/implementations/NUSDataProvider.java

129 lines
4.2 KiB
Java
Raw Normal View History

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;
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;
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
*
2016-12-12 21:01:12 +01:00
* @author Maschell
*
*/
public abstract class NUSDataProvider {
@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
*
* @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
*/
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);
}
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
*
* @param content
* The content of which the h3 hashes should be saved
2016-12-12 21:01:12 +01:00
* @param outputFolder
* @throws IOException
*/
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);
if (hash == null || hash.length == 0) {
2016-12-12 21:01:12 +01:00
return;
}
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);
if (output.exists() && output.length() == hash.length) {
log.info(h3Filename + " already exists");
2016-12-12 21:01:12 +01:00
return;
}
log.info("Saving " + h3Filename + " ");
2016-12-12 21:01:12 +01:00
FileUtils.saveByteArrayToFile(output, hash);
}
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.
*
* @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
*/
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);
if (inputStream == null) {
2016-12-12 21:01:12 +01:00
log.info("Couldn't save encrypted content. Input stream was null");
return;
}
2016-12-12 21:01:12 +01:00
File output = new File(outputFolder + File.separator + content.getFilename());
if (output.exists()) {
if (output.length() == content.getEncryptedFileSizeAligned()) {
2016-12-12 21:01:12 +01:00
log.info("Encrypted content alreadys exists, skipped");
return;
} 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");
}
}
FileUtils.saveInputStreamToFile(output, inputStream, content.getEncryptedFileSizeAligned());
2016-12-12 21:01:12 +01:00
}
/**
*
* @param content
* @param offset
* @return
* @throws IOException
*/
public abstract InputStream getInputStreamFromContent(Content content, long offset) throws IOException;
2016-12-12 21:01:12 +01:00
// TODO: JavaDocs
2016-12-12 21:01:12 +01:00
public abstract byte[] getContentH3Hash(Content content) throws IOException;
2016-12-12 21:01:12 +01:00
public abstract byte[] getRawTMD() throws IOException;
2016-12-12 21:01:12 +01:00
public abstract byte[] getRawTicket() throws IOException;
2016-12-12 21:01:12 +01:00
public abstract byte[] getRawCert() throws IOException;
2016-12-12 21:01:12 +01:00
public abstract void cleanup() throws IOException;
2016-12-12 21:01:12 +01:00
}