Skip to content

Commit e5866aa

Browse files
jaikiranLance Andersen
authored and
Lance Andersen
committed
8244706: GZIP "OS" header flag hard-coded to 0 instead of 255 (RFC 1952 non-compliance)
Reviewed-by: lancea, bchristi
1 parent 1086713 commit e5866aa

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

src/java.base/share/classes/java/util/zip/GZIPOutputStream.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -52,6 +52,9 @@ public class GZIPOutputStream extends DeflaterOutputStream {
5252
*/
5353
private static final int TRAILER_SIZE = 8;
5454

55+
// Represents the default "unknown" value for OS header, per RFC-1952
56+
private static final byte OS_UNKNOWN = (byte) 255;
57+
5558
/**
5659
* Creates a new output stream with the specified buffer size.
5760
*
@@ -189,7 +192,7 @@ private void writeHeader() throws IOException {
189192
0, // Modification time MTIME (int)
190193
0, // Modification time MTIME (int)
191194
0, // Extra flags (XFLG)
192-
0 // Operating system (OS)
195+
OS_UNKNOWN // Operating system (OS)
193196
});
194197
}
195198

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import org.testng.Assert;
25+
import org.testng.annotations.Test;
26+
27+
import java.io.ByteArrayInputStream;
28+
import java.io.ByteArrayOutputStream;
29+
import java.nio.charset.StandardCharsets;
30+
import java.util.zip.GZIPInputStream;
31+
import java.util.zip.GZIPOutputStream;
32+
33+
/**
34+
* @test
35+
* @bug 8244706
36+
* @summary Verify that the OS header flag in the stream written out by java.util.zip.GZIPOutputStream
37+
* has the correct expected value
38+
* @run testng GZIPOutputStreamHeaderTest
39+
*/
40+
public class GZIPOutputStreamHeaderTest {
41+
42+
private static final int OS_HEADER_INDEX = 9;
43+
private static final int HEADER_VALUE_OS_UNKNOWN = 255;
44+
45+
/**
46+
* Test that the {@code OS} header field in the GZIP output stream
47+
* has a value of {@code 255} which represents "unknown"
48+
*/
49+
@Test
50+
public void testOSHeader() throws Exception {
51+
final String data = "Hello world!!!";
52+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
53+
try (final GZIPOutputStream gzipOutputStream = new GZIPOutputStream(baos);) {
54+
gzipOutputStream.write(data.getBytes(StandardCharsets.UTF_8));
55+
}
56+
final byte[] compressed = baos.toByteArray();
57+
Assert.assertNotNull(compressed, "Compressed data is null");
58+
Assert.assertEquals(toUnsignedByte(compressed[OS_HEADER_INDEX]), HEADER_VALUE_OS_UNKNOWN,
59+
"Unexpected value for OS header");
60+
// finally verify that the compressed data is readable back to the original
61+
final String uncompressed;
62+
try (final ByteArrayOutputStream os = new ByteArrayOutputStream();
63+
final ByteArrayInputStream bis = new ByteArrayInputStream(compressed);
64+
final GZIPInputStream gzipInputStream = new GZIPInputStream(bis)) {
65+
gzipInputStream.transferTo(os);
66+
uncompressed = new String(os.toByteArray(), StandardCharsets.UTF_8);
67+
}
68+
Assert.assertEquals(uncompressed, data, "Unexpected data read from GZIPInputStream");
69+
}
70+
71+
private static int toUnsignedByte(final byte b) {
72+
return b & 0xff;
73+
}
74+
}

0 commit comments

Comments
 (0)