From 4d703d631a9db8ec1d171918c2571146a8ad0f6e Mon Sep 17 00:00:00 2001 From: Maschell Date: Wed, 10 Apr 2019 20:01:52 +0200 Subject: [PATCH] Add a FSTDataProvider implementation using a WUDDataPartition. --- .../FSTDataProviderWUDDataPartition.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java diff --git a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java new file mode 100644 index 0000000..42180bd --- /dev/null +++ b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java @@ -0,0 +1,58 @@ +package de.mas.wiiu.jnus.implementations; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Optional; + +import de.mas.wiiu.jnus.entities.content.ContentFSTInfo; +import de.mas.wiiu.jnus.entities.fst.FSTEntry; +import de.mas.wiiu.jnus.implementations.wud.parser.WUDDataPartition; +import de.mas.wiiu.jnus.implementations.wud.reader.WUDDiscReader; +import de.mas.wiiu.jnus.interfaces.FSTDataProvider; + +public class FSTDataProviderWUDDataPartition implements FSTDataProvider { + private final WUDDataPartition partition; + private final WUDDiscReader discReader; + private final byte[] titleKey; + + public FSTDataProviderWUDDataPartition(WUDDataPartition partition, WUDDiscReader discReader, byte[] titleKey) { + this.partition = partition; + this.discReader = discReader; + this.titleKey = titleKey; + } + + @Override + public String getName() { + return partition.getPartitionName(); + } + + @Override + public FSTEntry getRoot() { + return partition.getFST().getRoot(); + } + + @Override + public byte[] readFile(FSTEntry entry, long offset, long size) throws IOException { + ContentFSTInfo info = partition.getFST().getContentFSTInfos().get((int) entry.getContentFSTID()); + return getChunkOfData(info.getOffset(), entry.getFileOffset() + offset, size, discReader, titleKey); + } + + @Override + public InputStream readFileAsStream(FSTEntry entry, long offset, Optional size) throws IOException { + ContentFSTInfo info = partition.getFST().getContentFSTInfos().get((int) entry.getContentFSTID()); + long fileSize = size.orElse(entry.getFileSize()); + if (titleKey == null) { + return discReader.readEncryptedToInputStream(partition.getAbsolutePartitionOffset() + info.getOffset() + entry.getFileOffset() + offset, fileSize); + } + return discReader.readDecryptedToInputStream(partition.getAbsolutePartitionOffset() + info.getOffset(), entry.getFileOffset() + offset, fileSize, + titleKey, null, false); + } + + public byte[] getChunkOfData(long contentOffset, long fileoffset, long size, WUDDiscReader discReader, byte[] titleKey) throws IOException { + if (titleKey == null) { + return discReader.readEncryptedToByteArray(partition.getAbsolutePartitionOffset() + contentOffset, fileoffset, (int) size); + } + return discReader.readDecryptedToByteArray(partition.getAbsolutePartitionOffset() + contentOffset, fileoffset, (int) size, titleKey, null, false); + } + +}