diff options
author | Lasse Collin <lasse.collin@tukaani.org> | 2017-06-05 15:41:57 +0300 |
---|---|---|
committer | Lasse Collin <lasse.collin@tukaani.org> | 2017-06-05 15:53:44 +0300 |
commit | fad57f3871c589b0c31d10ac603efc3ca148e721 (patch) | |
tree | 109f65cea2c82331e6da5be993c8917b376676db | |
parent | 37c1e645c469bfcac2424a0f1f03d3aff605d8b5 (diff) | |
download | xz-java-fad57f3871c589b0c31d10ac603efc3ca148e721.tar.gz |
Improve buffer handling in RangeDecoderFromBuffer.
This way errors get detected slightly quicker and we avoid
the need to check if the buffer position is still OK.
-rw-r--r-- | src/org/tukaani/xz/LZMA2InputStream.java | 2 | ||||
-rw-r--r-- | src/org/tukaani/xz/rangecoder/RangeDecoderFromBuffer.java | 20 |
2 files changed, 10 insertions, 12 deletions
diff --git a/src/org/tukaani/xz/LZMA2InputStream.java b/src/org/tukaani/xz/LZMA2InputStream.java index 4e865c6..d176507 100644 --- a/src/org/tukaani/xz/LZMA2InputStream.java +++ b/src/org/tukaani/xz/LZMA2InputStream.java @@ -228,8 +228,6 @@ public class LZMA2InputStream extends InputStream { } else { lz.setLimit(copySizeMax); lzma.decode(); - if (!rc.isInBufferOK()) - throw new CorruptedInputException(); } int copiedSize = lz.flush(buf, off); diff --git a/src/org/tukaani/xz/rangecoder/RangeDecoderFromBuffer.java b/src/org/tukaani/xz/rangecoder/RangeDecoderFromBuffer.java index cac7a7e..58c8b64 100644 --- a/src/org/tukaani/xz/rangecoder/RangeDecoderFromBuffer.java +++ b/src/org/tukaani/xz/rangecoder/RangeDecoderFromBuffer.java @@ -18,11 +18,11 @@ public final class RangeDecoderFromBuffer extends RangeDecoder { private static final int INIT_SIZE = 5; private final byte[] buf; - private int pos = 0; - private int end = 0; + private int pos; public RangeDecoderFromBuffer(int inputSizeMax) { buf = new byte[inputSizeMax - INIT_SIZE]; + pos = buf.length; } public void prepareInputBuffer(DataInputStream in, int len) @@ -36,17 +36,17 @@ public final class RangeDecoderFromBuffer extends RangeDecoder { code = in.readInt(); range = 0xFFFFFFFF; - pos = 0; - end = len - INIT_SIZE; - in.readFully(buf, 0, end); - } - - public boolean isInBufferOK() { - return pos <= end; + // Read the data to the end of the buffer. If the data is corrupt + // and the decoder, reading from buf, tries to read past the end of + // the data, ArrayIndexOutOfBoundsException will be thrown and + // the problem is detected immediately. + len -= INIT_SIZE; + pos = buf.length - len; + in.readFully(buf, pos, len); } public boolean isFinished() { - return pos == end && code == 0; + return pos == buf.length && code == 0; } public void normalize() throws IOException { |