From fc3a604cd4c3711bb8e0e78fa1da21723f0d5ed4 Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 30 Apr 2019 14:25:29 +0200 Subject: [PATCH] Optimize the random access of the non hashed files --- .../implementations/FSTDataProviderNUSTitle.java | 13 ++++--------- .../jnus/utils/cryptography/NUSDecryption.java | 16 +--------------- 2 files changed, 5 insertions(+), 24 deletions(-) diff --git a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java index 2be617b..d434e23 100644 --- a/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java +++ b/src/de/mas/wiiu/jnus/implementations/FSTDataProviderNUSTitle.java @@ -91,7 +91,7 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle { long streamOffset = payloadOffset; - long streamFilesize = size; + long streamFilesize = c.getEncryptedFileSize(); if (c.isHashed()) { streamOffset = (payloadOffset / 0xFC00) * 0x10000; long offsetInBlock = payloadOffset - ((streamOffset / 0x10000) * 0xFC00); @@ -110,16 +110,11 @@ public class FSTDataProviderNUSTitle implements FSTDataProvider, HasNUSTitle { streamFilesize = curVal; } } else { - if (size != entry.getFileSize()) { - streamOffset = (payloadOffset / 0x8000) * 0x8000; - - // We need the missing bytes of the previous blocks + the size we want to read. - streamFilesize = size; - long offsetInBlock = offset - streamOffset; - streamFilesize += offsetInBlock; + if (size != entry.getFileSize()) { + streamFilesize = size; // We need the previous IV if we don't start at the first block. - if (payloadOffset >= 0x8000 && payloadOffset % 0x8000 == 0) { + if (payloadOffset >= 16) { streamOffset -= 16; streamFilesize += 16; } diff --git a/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java b/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java index 00ad9b0..722c2b8 100644 --- a/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java +++ b/src/de/mas/wiiu/jnus/utils/cryptography/NUSDecryption.java @@ -91,13 +91,8 @@ public class NUSDecryption extends AESDecryption { int skipoffset = (int) (fileOffset % 0x8000); try { - - // If we are at the beginning of a block, but it's not the first one, - // we need to get the IV from the last 16 bytes of the previous block. - // while beeing paranoid to exactly read 16 bytes but not more. Reading more - // would destroy our input stream. // The input stream has been prepared to start 16 bytes earlier on this case. - if (fileOffset >= 0x8000 && fileOffset % 0x8000 == 0) { + if (fileOffset >= 16) { int toRead = 16; byte[] data = new byte[toRead]; int readTotal = 0; @@ -119,15 +114,6 @@ public class NUSDecryption extends AESDecryption { long toRead = Utils.align(filesize + 15, 16); do { - // In case we start on the middle of a block we need to consume the "garbage" and save the - // current IV. - if (skipoffset > 0) { - int skippedBytes = StreamUtils.getChunkFromStream(inputStream, blockBuffer, overflow, skipoffset); - if (skippedBytes >= 16) { - IV = Arrays.copyOfRange(blockBuffer, skippedBytes - 16, skippedBytes); - } - skipoffset = 0; - } int curReadSize = BLOCKSIZE; if (toRead < BLOCKSIZE) {