From ecd16bf6652c30c8eacbb2dabbc4e6dfd12236c7 Mon Sep 17 00:00:00 2001 From: Tom Tromey Date: Wed, 22 Sep 2004 20:16:17 +0000 Subject: [PATCH] re PR java/14446 (GZIPInputStream: corrupted gzip file - crc mismatch) PR libgcj/14446: * java/util/zip/GZIPInputStream.java (read): Avoid sign extension when comparing CRCs. * java/util/zip/InflaterInputStream.java (onebytebuffer): New field. (read()): New overload. From-SVN: r87882 --- libjava/ChangeLog | 9 +++++++++ libjava/java/util/zip/GZIPInputStream.java | 4 +++- libjava/java/util/zip/InflaterInputStream.java | 16 ++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libjava/ChangeLog b/libjava/ChangeLog index bb05c53795b..df804df9c91 100644 --- a/libjava/ChangeLog +++ b/libjava/ChangeLog @@ -1,3 +1,12 @@ +2004-09-22 Tom Tromey + + PR libgcj/14446: + * java/util/zip/GZIPInputStream.java (read): Avoid sign extension + when comparing CRCs. + * java/util/zip/InflaterInputStream.java (onebytebuffer): New + field. + (read()): New overload. + 2004-09-21 Tom Tromey * java/lang/reflect/natMethod.cc (_Jv_CallAnyMethodA): diff --git a/libjava/java/util/zip/GZIPInputStream.java b/libjava/java/util/zip/GZIPInputStream.java index 9eef73eb945..56401639e0c 100644 --- a/libjava/java/util/zip/GZIPInputStream.java +++ b/libjava/java/util/zip/GZIPInputStream.java @@ -230,7 +230,9 @@ public class GZIPInputStream tmp[i] = (byte) eof_read(); } - int header_crc = read4(tmp, 0); + // Be careful to avoid sign extension here; CRC32.getValue() + // returns a long. + long header_crc = read4(tmp, 0) & 0xffffffffL; if (crc.getValue() != header_crc) throw new ZipException("corrupted gzip file - crc mismatch"); int isize = read4(tmp, 4); diff --git a/libjava/java/util/zip/InflaterInputStream.java b/libjava/java/util/zip/InflaterInputStream.java index 27c29ff41df..3676a2cdb5d 100644 --- a/libjava/java/util/zip/InflaterInputStream.java +++ b/libjava/java/util/zip/InflaterInputStream.java @@ -70,6 +70,9 @@ public class InflaterInputStream extends FilterInputStream */ protected int len; + // We just use this if we are decoding one byte at a time with the + // read() call. + private byte[] onebytebuffer = new byte[1]; /** * Create an InflaterInputStream with the default decompresseor @@ -155,6 +158,19 @@ public class InflaterInputStream extends FilterInputStream inf.setInput(buf, 0, len); } + /** + * Reads one byte of decompressed data. + * + * The byte is in the lower 8 bits of the int. + */ + public int read() throws IOException + { + int nread = read(onebytebuffer, 0, 1); + if (nread > 0) + return onebytebuffer[0] & 0xff; + return -1; + } + /** * Decompresses data into the byte array * -- 2.30.2