Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
* <p>
* If you use the {@code Builder.setDecompressConcatenated(true)} or {@code GzipCompressorInputStream(in, true)} then {@link #read} will return -1 only after
* the entire input stream has been exhausted; any bytes that follow an encoded GZIP member must constitute a new encoded GZIP member, otherwise an
* {@link IOException} is thrown. The data read from a stream constructed this way will consist of the concatenated data of all of the encoded GZIP members in
* order.
* {@link IOException} is thrown. Trailing zero padding after the last encoded GZIP member is ignored, like GNU gzip does. The data read from a stream
* constructed this way will consist of the concatenated data of all of the encoded GZIP members in order.
* </p>
* <p>
* To build an instance, use {@link Builder}.
Expand Down Expand Up @@ -358,6 +358,23 @@ public GzipParameters getMetaData() {
return parameters;
}

/**
* Consumes the remaining input and tests whether it consists solely of zero bytes.
* <p>
* Trailing zero padding after the last member commonly appears when .gz files are stored on or copied from media that pads them to a block boundary; GNU
* gzip silently accepts such input.
* </p>
*
* @return true if the input ended while only zero bytes were read, false if a non-zero byte was found.
*/
private boolean isTrailingZeroPadding() throws IOException {
int b;
do {
b = in.read();
} while (b == 0);
return b == -1;
}

private boolean init(final boolean isFirstMember) throws IOException {
if (!isFirstMember && !decompressConcatenated) { // at least one must be true
throw new CompressorException("Unexpected: isFirstMember and decompressConcatenated are both false.");
Expand All @@ -370,6 +387,10 @@ private boolean init(final boolean isFirstMember) throws IOException {
return false;
}
if (magic0 != GzipUtils.ID1 || in.read() != GzipUtils.ID2) {
// Like GNU gzip, trailing zero padding after the last member is not an error; anything else is.
if (!isFirstMember && magic0 == 0 && isTrailingZeroPadding()) {
return false;
}
throw new CompressorException(isFirstMember ? "Input is not in the .gz format." : "Unexpected data after a valid .gz stream.");
}
parameters = new GzipParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,63 @@ void testUnencodableFileNameThrowsCompressorException() throws IOException {
assertEquals("name.txt", gis.getMetaData().getFileName());
}
}

/**
* Tests COMPRESS-709: trailing zero padding after a valid member is ignored, like GNU gzip does, instead of failing with
* {@code "Unexpected data after a valid .gz stream."}.
*
* @throws IOException on test failure.
*/
@Test
void testCompress709TrailingZeroPaddingIsIgnored() throws IOException {
final byte[] payload = "Hello1\n".getBytes(StandardCharsets.ISO_8859_1);
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GzipCompressorOutputStream gos = new GzipCompressorOutputStream(bos)) {
gos.write(payload);
}
final byte[] member = bos.toByteArray();
// Padding spanning more than one internal read buffer.
final byte[] zeroPadding = new byte[16384];
final ByteArrayOutputStream padded = new ByteArrayOutputStream();
padded.write(member);
padded.write(zeroPadding);
final byte[] gz = padded.toByteArray();
// Concatenated mode: the padding terminates the stream cleanly.
try (GzipCompressorInputStream gis = GzipCompressorInputStream.builder()
.setInputStream(new ByteArrayInputStream(gz))
.setDecompressConcatenated(true)
.get()) {
assertArrayEquals(payload, IOUtils.toByteArray(gis));
}
// Non-concatenated mode never looked at the tail.
try (GzipCompressorInputStream gis = GzipCompressorInputStream.builder()
.setInputStream(new ByteArrayInputStream(gz))
.setDecompressConcatenated(false)
.get()) {
assertArrayEquals(payload, IOUtils.toByteArray(gis));
}
}

/**
* Tests COMPRESS-709: trailing input that is not zero padding is still rejected in concatenated mode.
*
* @throws IOException on test failure.
*/
@Test
void testCompress709TrailingGarbageIsStillRejected() throws IOException {
final ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (GzipCompressorOutputStream gos = new GzipCompressorOutputStream(bos)) {
gos.write("Hello1\n".getBytes(StandardCharsets.ISO_8859_1));
}
final ByteArrayOutputStream withGarbage = new ByteArrayOutputStream();
withGarbage.write(bos.toByteArray());
withGarbage.write("garbage".getBytes(StandardCharsets.ISO_8859_1));
try (GzipCompressorInputStream gis = GzipCompressorInputStream.builder()
.setInputStream(new ByteArrayInputStream(withGarbage.toByteArray()))
.setDecompressConcatenated(true)
.get()) {
final CompressorException e = assertThrows(CompressorException.class, () -> IOUtils.toByteArray(gis));
assertEquals("Unexpected data after a valid .gz stream.", e.getMessage());
}
}
}