From aaa03d0d8bb03e1adecee6abf4bd60a6a2cfbe32 Mon Sep 17 00:00:00 2001 From: Alexandre Blazart Date: Wed, 25 Aug 2021 20:12:19 +0200 Subject: [PATCH] Fix the implementation of StreamingXXHash32.asChecksum() In the Checksum view, we only keep the last 28 bits of the hash instead of the 32 bits of the full hash. The result is that the 4 first bits are always set to 0 in LZ4Block*Stream. This commit: - fixes the Checksum view in StreamingXXHash32 - keep the current behaviour in LZ4Block*Stream to avoid breaking existing data built with this library --- src/java/net/jpountz/lz4/LZ4BlockInputStream.java | 2 +- src/java/net/jpountz/lz4/LZ4BlockOutputStream.java | 2 +- src/java/net/jpountz/xxhash/StreamingXXHash32.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/java/net/jpountz/lz4/LZ4BlockInputStream.java b/src/java/net/jpountz/lz4/LZ4BlockInputStream.java index 23fd5e3..30e63e3 100644 --- a/src/java/net/jpountz/lz4/LZ4BlockInputStream.java +++ b/src/java/net/jpountz/lz4/LZ4BlockInputStream.java @@ -257,7 +257,7 @@ private void refill() throws IOException { } checksum.reset(); checksum.update(buffer, 0, originalLen); - if ((int) checksum.getValue() != check) { + if ((int) (checksum.getValue() & 0x0FFFFFFF) != check) { throw new IOException("Stream is corrupted"); } o = 0; diff --git a/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java b/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java index 623eb1c..063f922 100644 --- a/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java +++ b/src/java/net/jpountz/lz4/LZ4BlockOutputStream.java @@ -206,7 +206,7 @@ private void flushBufferedData() throws IOException { } checksum.reset(); checksum.update(buffer, 0, o); - final int check = (int) checksum.getValue(); + final int check = (int) (checksum.getValue() & 0x0FFFFFFF); int compressedLength = compressor.compress(buffer, 0, o, compressedBuffer, HEADER_LENGTH); final int compressMethod; if (compressedLength >= o) { diff --git a/src/java/net/jpountz/xxhash/StreamingXXHash32.java b/src/java/net/jpountz/xxhash/StreamingXXHash32.java index 8143433..eb5f608 100644 --- a/src/java/net/jpountz/xxhash/StreamingXXHash32.java +++ b/src/java/net/jpountz/xxhash/StreamingXXHash32.java @@ -103,7 +103,7 @@ public final Checksum asChecksum() { @Override public long getValue() { - return StreamingXXHash32.this.getValue() & 0xFFFFFFFL; + return StreamingXXHash32.this.getValue() & 0xFFFFFFFFL; } @Override