From f53af3cd46ffc23deee7d27310c6d1f8ad1c434c Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Mon, 16 Jan 2023 09:45:46 +0300 Subject: [PATCH] Fix read-from-stream flow errors --- misc/logo.svg | 136 ++++++++++++++---- .../java/libKonogonka/Tools/ExportAble.java | 2 +- .../Tools/other/System2/ini1/KIP1Raw.java | 29 ++-- .../ctraes/AesCtrBufferedInputStream.java | 10 +- .../ctraes/InFileStreamProducer.java | 4 +- .../AesCtrClassicBufferedInputStream.java | 2 +- .../InFileStreamClassicProducer.java | 3 - 7 files changed, 143 insertions(+), 43 deletions(-) diff --git a/misc/logo.svg b/misc/logo.svg index 88466c0..ecc756a 100644 --- a/misc/logo.svg +++ b/misc/logo.svg @@ -9,7 +9,7 @@ id="svg50454" xml:space="preserve" sodipodi:docname="logo.svg" - inkscape:version="1.2.1 (9c6d41e410, 2022-07-14)" + inkscape:version="1.2.2 (b0a8486541, 2022-12-01)" xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd" xmlns:xlink="http://www.w3.org/1999/xlink" @@ -25,15 +25,15 @@ inkscape:deskcolor="#505050" inkscape:document-units="mm" showgrid="false" - inkscape:zoom="8" - inkscape:cx="102.375" - inkscape:cy="89.25" - inkscape:window-width="3754" - inkscape:window-height="2127" - inkscape:window-x="1166" + inkscape:zoom="1.9483566" + inkscape:cx="371.59522" + inkscape:cy="203.50484" + inkscape:window-width="2421" + inkscape:window-height="1641" + inkscape:window-x="99" inkscape:window-y="0" inkscape:window-maximized="1" - inkscape:current-layer="g934" + inkscape:current-layer="layer1-6" showguides="false"> size) { diff --git a/src/main/java/libKonogonka/Tools/other/System2/ini1/KIP1Raw.java b/src/main/java/libKonogonka/Tools/other/System2/ini1/KIP1Raw.java index 258aca9..b58e0ab 100644 --- a/src/main/java/libKonogonka/Tools/other/System2/ini1/KIP1Raw.java +++ b/src/main/java/libKonogonka/Tools/other/System2/ini1/KIP1Raw.java @@ -18,21 +18,23 @@ */ package libKonogonka.Tools.other.System2.ini1; +import java.nio.ByteBuffer; + public class KIP1Raw { private KIP1Header headerObject; private final byte[] header; private final byte[] _textDecompressedSection; - private final byte[] _rodataDecompressedSection; - private final byte[] _dataDecompressedSection; + private final byte[] _roDataDecompressedSection; + private final byte[] _rwDataDecompressedSection; KIP1Raw(byte[] header, byte[] _textDecompressedSection, - byte[] _rodataDecompressedSection, - byte[] _dataDecompressedSection){ + byte[] _roDataDecompressedSection, + byte[] _rwDataDecompressedSection){ this.header = header; this._textDecompressedSection = _textDecompressedSection; - this. _rodataDecompressedSection = _rodataDecompressedSection; - this._dataDecompressedSection = _dataDecompressedSection; + this._roDataDecompressedSection = _roDataDecompressedSection; + this._rwDataDecompressedSection = _rwDataDecompressedSection; try { this.headerObject = new KIP1Header(header); } @@ -42,6 +44,17 @@ public class KIP1Raw { public KIP1Header getHeader() { return headerObject; } public byte[] getHeaderRaw() {return header;} public byte[] getTextRaw() {return _textDecompressedSection;} - public byte[] getRodataRaw() {return _rodataDecompressedSection;} - public byte[] getDataRaw() {return _dataDecompressedSection;} + public byte[] getRoDataRaw() {return _roDataDecompressedSection;} + public byte[] getRwDataRaw() {return _rwDataDecompressedSection;} + public byte[] getRaw(){ + ByteBuffer entireKip1 = ByteBuffer.allocate(header.length + + _textDecompressedSection.length + + _roDataDecompressedSection.length + + _rwDataDecompressedSection.length); + entireKip1.put(header) + .put(_textDecompressedSection) + .put(_roDataDecompressedSection) + .put(_rwDataDecompressedSection); + return entireKip1.array(); + } } diff --git a/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java b/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java index 757f31c..732cb5f 100644 --- a/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java +++ b/src/main/java/libKonogonka/ctraes/AesCtrBufferedInputStream.java @@ -38,7 +38,7 @@ public class AesCtrBufferedInputStream extends BufferedInputStream { long mediaEndOffset, InputStream inputStream, long fileSize){ - super(inputStream); + super(inputStream, 0x200); this.decryptor = decryptor; this.mediaOffsetPositionStart = ncaOffsetPosition + (mediaStartOffset * 0x200); this.mediaOffsetPositionEnd = ncaOffsetPosition + (mediaEndOffset * 0x200); @@ -150,15 +150,17 @@ public class AesCtrBufferedInputStream extends BufferedInputStream { byte[] chunkBytes = new byte[bytes]; long actuallyRead = super.read(chunkBytes, 0, bytes); if (actuallyRead != bytes) - throw new IOException("Can't read: " + actuallyRead + "/"+ bytes); + throw new IOException("Can't read. " + actuallyRead + "/"+ bytes); return chunkBytes; } private boolean isPointerInsideEncryptedSection(){ - return (pseudoPos-pointerInsideDecryptedSection >= mediaOffsetPositionStart) && (pseudoPos-pointerInsideDecryptedSection < mediaOffsetPositionEnd); + return (pseudoPos-pointerInsideDecryptedSection >= mediaOffsetPositionStart) && + (pseudoPos-pointerInsideDecryptedSection < mediaOffsetPositionEnd); } private boolean isEndPositionInsideEncryptedSection(long requestedBytesCount){ - return ((pseudoPos-pointerInsideDecryptedSection + requestedBytesCount) >= mediaOffsetPositionStart) && ((pseudoPos-pointerInsideDecryptedSection + requestedBytesCount) < mediaOffsetPositionEnd); + return ((pseudoPos-pointerInsideDecryptedSection + requestedBytesCount) >= mediaOffsetPositionStart) && + ((pseudoPos-pointerInsideDecryptedSection + requestedBytesCount) < mediaOffsetPositionEnd); } @Override diff --git a/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java b/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java index 74646d9..a45a03e 100644 --- a/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java +++ b/src/main/java/libKonogonka/ctraes/InFileStreamProducer.java @@ -29,7 +29,7 @@ public class InFileStreamProducer implements IProducer { private final File file; private final long initialOffset; - private long subOffset; + private final long subOffset; private AesCtrDecryptForMediaBlocks decryptor; private long mediaStartOffset; private long mediaEndOffset; @@ -73,7 +73,7 @@ public class InFileStreamProducer implements IProducer { mediaStartOffset, mediaEndOffset, Files.newInputStream(file.toPath()), - Files.size(file.toPath())); + Files.size(file.toPath())); // Files.size(file.toPath())-initialOffset); ? skipBytesTillBeginning(stream, subOffset); return stream; } diff --git a/src/main/java/libKonogonka/ctraesclassic/AesCtrClassicBufferedInputStream.java b/src/main/java/libKonogonka/ctraesclassic/AesCtrClassicBufferedInputStream.java index 43d4091..c0a9293 100644 --- a/src/main/java/libKonogonka/ctraesclassic/AesCtrClassicBufferedInputStream.java +++ b/src/main/java/libKonogonka/ctraesclassic/AesCtrClassicBufferedInputStream.java @@ -41,7 +41,7 @@ public class AesCtrClassicBufferedInputStream extends BufferedInputStream { long encryptedEndOffset, InputStream inputStream, long fileSize){ - super(inputStream); + super(inputStream, 0x200); this.decryptor = decryptor; this.encryptedStartOffset = encryptedStartOffset; this.encryptedEndOffset = encryptedEndOffset; diff --git a/src/main/java/libKonogonka/ctraesclassic/InFileStreamClassicProducer.java b/src/main/java/libKonogonka/ctraesclassic/InFileStreamClassicProducer.java index d697190..0839ff6 100644 --- a/src/main/java/libKonogonka/ctraesclassic/InFileStreamClassicProducer.java +++ b/src/main/java/libKonogonka/ctraesclassic/InFileStreamClassicProducer.java @@ -19,16 +19,13 @@ package libKonogonka.ctraesclassic; import libKonogonka.IProducer; -import libKonogonka.RainbowDump; import libKonogonka.ctraes.InFileStreamProducer; import java.io.BufferedInputStream; import java.io.File; -import java.io.FilterInputStream; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; public class InFileStreamClassicProducer implements IProducer { private boolean encrypted;