From 502acbbf680640bfbafe24870c58ef763741e042 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 6 Apr 2019 16:55:57 +0200 Subject: [PATCH] Add "getChunkFromFIle" function to the DecryptionService to get portions of a file --- src/de/mas/wiiu/jnus/DecryptionService.java | 36 +++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/de/mas/wiiu/jnus/DecryptionService.java b/src/de/mas/wiiu/jnus/DecryptionService.java index 52acd3b..0b3a7ac 100644 --- a/src/de/mas/wiiu/jnus/DecryptionService.java +++ b/src/de/mas/wiiu/jnus/DecryptionService.java @@ -16,6 +16,7 @@ ****************************************************************************/ package de.mas.wiiu.jnus; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; @@ -25,9 +26,7 @@ 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; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.ExecutionException; @@ -149,6 +148,17 @@ public final class DecryptionService { } public void decryptFSTEntryToStream(FSTEntry entry, OutputStream outputStream) throws IOException, CheckSumWrongException { + + long fileSize = entry.getFileSize(); + long fileOffset = entry.getFileOffset(); + long fileOffsetBlock = entry.getFileOffsetBlock(); + + decryptFSTEntryToStream(entry, outputStream, fileSize, fileOffset, fileOffsetBlock); + + } + + public void decryptFSTEntryToStream(FSTEntry entry, OutputStream outputStream, long fileSize, long fileOffset, long fileOffsetBlock) + throws IOException, CheckSumWrongException { if (entry.isNotInPackage() || entry.getContent() == null) { outputStream.close(); return; @@ -156,10 +166,6 @@ public final class DecryptionService { 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); @@ -278,6 +284,24 @@ public final class DecryptionService { decryptContentFromStream(inputStream, outputStream, content); } + public byte[] getChunkFromFile(FSTEntry entry, long offset, long size) throws IOException, CheckSumWrongException { + if(!entry.getContent().isHashed()) { + throw new IOException("Only files from hashed contents are currently supported"); + } + ByteArrayOutputStream out = new ByteArrayOutputStream(); + + long fileOffset = entry.getFileOffset() + offset; + long fileOffsetBlock = fileOffset; + + if (entry.getContent().isHashed()) { + fileOffsetBlock = (fileOffset / 0xFC00) * 0x10000; + } + + decryptFSTEntryToStream(entry, out, size, fileOffset, fileOffsetBlock); + + return out.toByteArray(); + + } public InputStreamWithException getDecryptedOutputAsInputStream(FSTEntry fstEntry) throws IOException { PipedInputStreamWithException in = new PipedInputStreamWithException(); PipedOutputStream out = new PipedOutputStream(in);