From 69e0c8b36a5bea656e457fb30d5618b84fc40ff1 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 26 Apr 2019 13:44:24 +0200 Subject: [PATCH] The size in the FSTDataProvider and NUSDataProvider functions are not Optional anymore --- src/de/mas/wiiu/jnus/NUSTitleLoader.java | 2 +- .../FSTDataProviderNUSTitle.java | 21 +++++++++---------- .../FSTDataProviderWUDDataPartition.java | 19 +++++++---------- .../implementations/NUSDataProviderFST.java | 2 +- .../implementations/NUSDataProviderLocal.java | 2 +- .../NUSDataProviderLocalBackup.java | 2 +- .../NUSDataProviderRemote.java | 2 +- .../implementations/NUSDataProviderWUD.java | 9 +++----- .../implementations/NUSDataProviderWoomy.java | 2 +- .../wiiu/jnus/interfaces/FSTDataProvider.java | 11 +++++----- .../wiiu/jnus/interfaces/NUSDataProvider.java | 17 +++++++-------- .../wiiu/jnus/utils/DataProviderUtils.java | 2 +- .../utils/download/NUSDownloadService.java | 10 +++------ 13 files changed, 43 insertions(+), 58 deletions(-) diff --git a/src/de/mas/wiiu/jnus/NUSTitleLoader.java b/src/de/mas/wiiu/jnus/NUSTitleLoader.java index 3787b91..30cdb00 100644 --- a/src/de/mas/wiiu/jnus/NUSTitleLoader.java +++ b/src/de/mas/wiiu/jnus/NUSTitleLoader.java @@ -71,7 +71,7 @@ public class NUSTitleLoader { // If we have more than one content, the index 0 is the FST. Content fstContent = result.getTMD().getContentByIndex(0); - InputStream fstContentEncryptedStream = dataProvider.getInputStreamFromContent(fstContent, 0, Optional.of(fstContent.getEncryptedFileSize())); + InputStream fstContentEncryptedStream = dataProvider.getInputStreamFromContent(fstContent); byte[] fstBytes = StreamUtils.getBytesFromStream(fstContentEncryptedStream, (int) fstContent.getEncryptedFileSize()); diff --git a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java index 3b98f50..2fa7d57 100644 --- a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java +++ b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java @@ -65,17 +65,15 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle { } @Override - public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset, Optional size) throws IOException { - long usedSize = size.orElse(entry.getFileSize()); - + public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset, long size) throws IOException { try { - return decryptFSTEntryToStream(entry, out, offset, usedSize); + return decryptFSTEntryToStream(entry, out, offset, size); } catch (CheckSumWrongException | NoSuchAlgorithmException e) { throw new IOException(e); } } - private boolean decryptFSTEntryToStream(FSTEntry entry, OutputStream outputStream, long offset, long fileSize) + private boolean decryptFSTEntryToStream(FSTEntry entry, OutputStream outputStream, long offset, long size) throws IOException, CheckSumWrongException, NoSuchAlgorithmException { if (entry.isNotInPackage() || !title.getTicket().isPresent()) { if (!title.getTicket().isPresent()) { @@ -93,15 +91,15 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle { long streamOffset = payloadOffset; - Optional readFilesize = Optional.empty(); + long streamFilesize = size; if (c.isHashed()) { streamOffset = (payloadOffset / 0xFC00) * 0x10000; long offsetInBlock = payloadOffset - ((streamOffset / 0x10000) * 0xFC00); - if (offsetInBlock + fileSize < 0xFC00) { - readFilesize = Optional.of(0x10000L); + if (offsetInBlock + size < 0xFC00) { + streamFilesize = 0x10000L; } else { long curVal = 0x10000; - long missing = (fileSize - (0xFC00 - offsetInBlock)); + long missing = (size - (0xFC00 - offsetInBlock)); curVal += (missing / 0xFC00) * 0x10000; @@ -109,7 +107,7 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle { curVal += 0x10000; } - readFilesize = Optional.of(curVal); + streamFilesize = curVal; } } else { streamOffset = (payloadOffset / 0x8000) * 0x8000; @@ -117,11 +115,12 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle { if (payloadOffset >= 0x8000 && payloadOffset % 0x8000 == 0) { streamOffset -= 16; } + streamFilesize = c.getEncryptedFileSize(); } NUSDataProvider dataProvider = title.getDataProvider(); - InputStream in = dataProvider.getInputStreamFromContent(c, streamOffset, readFilesize); + InputStream in = dataProvider.getInputStreamFromContent(c, streamOffset, streamFilesize); try { NUSDecryption nusdecryption = new NUSDecryption(title.getTicket().get()); diff --git a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java index c1fb850..fce0660 100644 --- a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java +++ b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderWUDDataPartition.java @@ -1,5 +1,3 @@ -package de.mas.wiiu.jnus.implementations; - /**************************************************************************** * Copyright (C) 2016-2019 Maschell * @@ -16,10 +14,11 @@ package de.mas.wiiu.jnus.implementations; * You should have received a copy of the GNU General Public License * along with this program. If not, see . ****************************************************************************/ +package de.mas.wiiu.jnus.implementations; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.util.Optional; import de.mas.wiiu.jnus.entities.content.ContentFSTInfo; import de.mas.wiiu.jnus.entities.fst.FSTEntry; @@ -64,24 +63,22 @@ public class FSTDataProviderWUDDataPartition implements FSTDataProvider { } @Override - public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset, Optional size) throws IOException { + public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset, long size) throws IOException { ContentFSTInfo info = FSTUtils.getFSTInfoForContent(partition.getFST(), entry.getContentIndex()) .orElseThrow(() -> new IOException("Failed to find FSTInfo")); - long usedSize = size.orElse(entry.getFileSize()); if (titleKey == null) { - return discReader.readEncryptedToStream(out, partition.getPartitionOffset() + info.getOffset() + entry.getFileOffset() + offset, usedSize); + return discReader.readEncryptedToStream(out, partition.getPartitionOffset() + info.getOffset() + entry.getFileOffset() + offset, size); } - return discReader.readDecryptedToOutputStream(out, partition.getPartitionOffset() + info.getOffset(), entry.getFileOffset() + offset, usedSize, - titleKey, null, false); + return discReader.readDecryptedToOutputStream(out, partition.getPartitionOffset() + info.getOffset(), entry.getFileOffset() + offset, size, titleKey, + null, false); } @Override - public InputStream readFileAsStream(FSTEntry entry, long offset, Optional size) throws IOException { + public InputStream readFileAsStream(FSTEntry entry, long offset, long size) throws IOException { if (titleKey == null) { ContentFSTInfo info = FSTUtils.getFSTInfoForContent(partition.getFST(), entry.getContentIndex()) .orElseThrow(() -> new IOException("Failed to find FSTInfo")); - long usedSize = size.orElse(entry.getFileSize()); - return discReader.readEncryptedToStream(partition.getPartitionOffset() + info.getOffset() + entry.getFileOffset() + offset, usedSize); + return discReader.readEncryptedToStream(partition.getPartitionOffset() + info.getOffset() + entry.getFileOffset() + offset, size); } return FSTDataProvider.super.readFileAsStream(entry, offset, size); diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java index 609da37..2367962 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderFST.java @@ -44,7 +44,7 @@ public class NUSDataProviderFST implements NUSDataProvider { } @Override - public InputStream getInputStreamFromContent(Content content, long offset, Optional size) throws IOException { + public InputStream getInputStreamFromContent(Content content, long offset, long size) throws IOException { String filename = content.getFilename(); Optional contentFileOpt = FSTUtils.getChildOfDirectory(base, filename); FSTEntry contentFile = contentFileOpt.orElseThrow(() -> new FileNotFoundException(filename + " was not found.")); diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java index 9897e9b..76ff8ec 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocal.java @@ -45,7 +45,7 @@ public final class NUSDataProviderLocal implements NUSDataProvider { } @Override - public InputStream getInputStreamFromContent(Content content, long offset, Optional size) throws IOException { + public InputStream getInputStreamFromContent(Content content, long offset, long size) throws IOException { File filepath = FileUtils.getFileIgnoringFilenameCases(getLocalPath(), content.getFilename()); if (filepath == null || !filepath.exists()) { String errormsg = "Couldn't open \"" + getLocalPath() + File.separator + content.getFilename() + "\", file does not exist"; diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java index a9b00f3..7c8e105 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderLocalBackup.java @@ -48,7 +48,7 @@ public class NUSDataProviderLocalBackup implements NUSDataProvider { } @Override - public InputStream getInputStreamFromContent(Content content, long offset, Optional size) throws IOException { + public InputStream getInputStreamFromContent(Content content, long offset, long size) throws IOException { File filepath = new File(getFilePathOnDisk(content)); if (!filepath.exists()) { throw new FileNotFoundException(filepath.getAbsolutePath() + " was not found."); diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java index e34e300..fc9a725 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderRemote.java @@ -37,7 +37,7 @@ public class NUSDataProviderRemote implements NUSDataProvider, Parallelizable { } @Override - public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, Optional size) throws IOException { + public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, long size) throws IOException { NUSDownloadService downloadService = NUSDownloadService.getDefaultInstance(); return downloadService.getInputStreamForURL(getRemoteURL(content), fileOffsetBlock, size); } diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java index f54b3ad..78412a6 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWUD.java @@ -61,14 +61,11 @@ public class NUSDataProviderWUD implements NUSDataProvider { } @Override - public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, Optional size) throws IOException { + public InputStream getInputStreamFromContent(Content content, long fileOffsetBlock, long size) throws IOException { WUDDiscReader discReader = getDiscReader(); long offset = getOffsetInWUD(content) + fileOffsetBlock; - long usedSize = content.getEncryptedFileSize() - fileOffsetBlock; - if (size.isPresent()) { - usedSize = size.get(); - } - return discReader.readEncryptedToStream(offset, usedSize); + + return discReader.readEncryptedToStream(offset, size); } @Override diff --git a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java index ab5e2fd..1bf9987 100644 --- a/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java +++ b/src/de/mas/wiiu/jnus/implementations/NUSDataProviderWoomy.java @@ -44,7 +44,7 @@ public class NUSDataProviderWoomy implements NUSDataProvider { } @Override - public InputStream getInputStreamFromContent(@NonNull Content content, long fileOffsetBlock, Optional size) throws IOException { + public InputStream getInputStreamFromContent(@NonNull Content content, long fileOffsetBlock, long size) throws IOException { WoomyZipFile zipFile = getSharedWoomyZipFile(); ZipEntry entry = getWoomyInfo().getContentFiles().get(content.getFilename().toLowerCase()); if (entry == null) { diff --git a/src/de/mas/wiiu/jnus/interfaces/FSTDataProvider.java b/src/de/mas/wiiu/jnus/interfaces/FSTDataProvider.java index 6278d2b..42446da 100644 --- a/src/de/mas/wiiu/jnus/interfaces/FSTDataProvider.java +++ b/src/de/mas/wiiu/jnus/interfaces/FSTDataProvider.java @@ -21,7 +21,6 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.PipedOutputStream; -import java.util.Optional; import de.mas.wiiu.jnus.entities.fst.FSTEntry; import de.mas.wiiu.jnus.utils.PipedInputStreamWithException; @@ -38,16 +37,16 @@ public interface FSTDataProvider { default public byte[] readFile(FSTEntry entry, long offset, long size) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); - readFileToStream(out, entry, offset, Optional.of(size)); + readFileToStream(out, entry, offset, size); return out.toByteArray(); } default public InputStream readFileAsStream(FSTEntry entry) throws IOException { - return readFileAsStream(entry, 0, Optional.empty()); + return readFileAsStream(entry, 0, entry.getFileSize()); } - default public InputStream readFileAsStream(FSTEntry entry, long offset, Optional size) throws IOException { + default public InputStream readFileAsStream(FSTEntry entry, long offset, long size) throws IOException { PipedOutputStream out = new PipedOutputStream(); PipedInputStreamWithException in = new PipedInputStreamWithException(out, 0x10000); @@ -64,9 +63,9 @@ public interface FSTDataProvider { } default public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset) throws IOException { - return readFileToStream(out, entry, offset, Optional.empty()); + return readFileToStream(out, entry, offset, entry.getFileSize()); } - public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset, Optional size) throws IOException; + public boolean readFileToStream(OutputStream out, FSTEntry entry, long offset, long size) throws IOException; } diff --git a/src/de/mas/wiiu/jnus/interfaces/NUSDataProvider.java b/src/de/mas/wiiu/jnus/interfaces/NUSDataProvider.java index bea6a32..3f2fcc1 100644 --- a/src/de/mas/wiiu/jnus/interfaces/NUSDataProvider.java +++ b/src/de/mas/wiiu/jnus/interfaces/NUSDataProvider.java @@ -28,22 +28,19 @@ import de.mas.wiiu.jnus.utils.StreamUtils; public interface NUSDataProvider { default public byte[] getChunkFromContent(Content content, long offset, int size) throws IOException { - return StreamUtils.getBytesFromStream(getInputStreamFromContent(content, offset, Optional.of((long) size)), size); + return StreamUtils.getBytesFromStream(getInputStreamFromContent(content, offset, size), size); } - /** - * - * @param content - * @param offset - * @return - * @throws IOException - */ - public InputStream getInputStreamFromContent(Content content, long offset, Optional size) throws IOException; + default public InputStream getInputStreamFromContent(Content content) throws IOException { + return getInputStreamFromContent(content, 0); + } default public InputStream getInputStreamFromContent(Content content, long offset) throws IOException { - return getInputStreamFromContent(content, offset, Optional.empty()); + return getInputStreamFromContent(content, offset, content.getEncryptedFileSize() - offset); } + public InputStream getInputStreamFromContent(Content content, long offset, long size) throws IOException; + public Optional getContentH3Hash(Content content) throws IOException; public Optional getRawTMD() throws IOException; diff --git a/src/de/mas/wiiu/jnus/utils/DataProviderUtils.java b/src/de/mas/wiiu/jnus/utils/DataProviderUtils.java index 5818afa..b45fe9a 100644 --- a/src/de/mas/wiiu/jnus/utils/DataProviderUtils.java +++ b/src/de/mas/wiiu/jnus/utils/DataProviderUtils.java @@ -133,7 +133,7 @@ public class DataProviderUtils { } Utils.createDir(outputFolder); - InputStream inputStream = dataProvider.getInputStreamFromContent(content, 0); + InputStream inputStream = dataProvider.getInputStreamFromContent(content); if (inputStream == null) { log.warning(content.getFilename() + " Couldn't save encrypted content. Input stream was null"); return; diff --git a/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java b/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java index f083b38..0f27fd1 100644 --- a/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java +++ b/src/de/mas/wiiu/jnus/utils/download/NUSDownloadService.java @@ -83,15 +83,11 @@ public final class NUSDownloadService extends Downloader { return downloadFileToByteArray(URL); } - public InputStream getInputStream(String URL, long offset, Optional size) throws IOException { + public InputStream getInputStream(String URL, long offset, long size) throws IOException { URL url_obj = new URL(URL); HttpURLConnection connection = (HttpURLConnection) url_obj.openConnection(); connection.setRequestProperty("User-Agent", Settings.USER_AGENT); - String sizeString = ""; - if (size.isPresent()) { - sizeString = Long.toString(size.get()); - } - connection.setRequestProperty("Range", "bytes=" + offset + "-" + sizeString); + connection.setRequestProperty("Range", "bytes=" + offset + "-" + Long.toString(size)); try { connection.connect(); } catch (Exception e) { @@ -101,7 +97,7 @@ public final class NUSDownloadService extends Downloader { return connection.getInputStream(); } - public InputStream getInputStreamForURL(String url, long offset, Optional size) throws IOException { + public InputStream getInputStreamForURL(String url, long offset, Long size) throws IOException { String URL = URL_BASE + "/" + url; return getInputStream(URL, offset, size);