1 | /* GZIPInputStream.java - Input filter for reading gzip file
|
---|
2 | Copyright (C) 1999, 2000, 2002 Free Software Foundation, Inc.
|
---|
3 |
|
---|
4 | This file is part of GNU Classpath.
|
---|
5 |
|
---|
6 | GNU Classpath is free software; you can redistribute it and/or modify
|
---|
7 | it under the terms of the GNU General Public License as published by
|
---|
8 | the Free Software Foundation; either version 2, or (at your option)
|
---|
9 | any later version.
|
---|
10 |
|
---|
11 | GNU Classpath is distributed in the hope that it will be useful, but
|
---|
12 | WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | General Public License for more details.
|
---|
15 |
|
---|
16 | You should have received a copy of the GNU General Public License
|
---|
17 | along with GNU Classpath; see the file COPYING. If not, write to the
|
---|
18 | Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
19 | 02111-1307 USA.
|
---|
20 |
|
---|
21 | Linking this library statically or dynamically with other modules is
|
---|
22 | making a combined work based on this library. Thus, the terms and
|
---|
23 | conditions of the GNU General Public License cover the whole
|
---|
24 | combination.
|
---|
25 |
|
---|
26 | As a special exception, the copyright holders of this library give you
|
---|
27 | permission to link this library with independent modules to produce an
|
---|
28 | executable, regardless of the license terms of these independent
|
---|
29 | modules, and to copy and distribute the resulting executable under
|
---|
30 | terms of your choice, provided that you also meet, for each linked
|
---|
31 | independent module, the terms and conditions of the license of that
|
---|
32 | module. An independent module is a module which is not derived from
|
---|
33 | or based on this library. If you modify this library, you may extend
|
---|
34 | this exception to your version of the library, but you are not
|
---|
35 | obligated to do so. If you do not wish to do so, delete this
|
---|
36 | exception statement from your version. */
|
---|
37 |
|
---|
38 | package java.util.zip;
|
---|
39 |
|
---|
40 | import java.io.InputStream;
|
---|
41 | import java.io.IOException;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * @author Tom Tromey
|
---|
45 | * @date May 17, 1999
|
---|
46 | */
|
---|
47 |
|
---|
48 | /* Written using on-line Java Platform 1.2 API Specification
|
---|
49 | * and JCL book.
|
---|
50 | * Believed complete and correct.
|
---|
51 | */
|
---|
52 |
|
---|
53 | public class GZIPInputStream extends InflaterInputStream
|
---|
54 | {
|
---|
55 | public static final int GZIP_MAGIC = 0x8b1f;
|
---|
56 |
|
---|
57 | public void close () throws IOException
|
---|
58 | {
|
---|
59 | // Nothing to do here.
|
---|
60 | super.close();
|
---|
61 | }
|
---|
62 |
|
---|
63 | public GZIPInputStream (InputStream istream) throws IOException
|
---|
64 | {
|
---|
65 | this (istream, 512);
|
---|
66 | }
|
---|
67 |
|
---|
68 | private final int eof_read () throws IOException
|
---|
69 | {
|
---|
70 | int r = in.read();
|
---|
71 | if (r == -1)
|
---|
72 | throw new ZipException ("gzip header corrupted");
|
---|
73 | return r & 0xff;
|
---|
74 | }
|
---|
75 |
|
---|
76 | public GZIPInputStream (InputStream istream, int readsize)
|
---|
77 | throws IOException
|
---|
78 | {
|
---|
79 | super (istream, new Inflater (true), readsize);
|
---|
80 |
|
---|
81 | // NOTE: header reading code taken from zlib's gzio.c.
|
---|
82 |
|
---|
83 | // Read the magic number.
|
---|
84 | int magic = eof_read () | (eof_read () << 8);
|
---|
85 | if (magic != GZIP_MAGIC)
|
---|
86 | throw new ZipException ("gzip header corrupted");
|
---|
87 |
|
---|
88 | int method = eof_read ();
|
---|
89 | int flags = eof_read ();
|
---|
90 | // Test from zlib.
|
---|
91 | if (method != Z_DEFLATED || (flags & RESERVED) != 0)
|
---|
92 | throw new ZipException ("gzip header corrupted");
|
---|
93 |
|
---|
94 | // Discard time, xflags, OS code.
|
---|
95 | for (int i = 0; i < 6; ++i)
|
---|
96 | eof_read ();
|
---|
97 |
|
---|
98 | // Skip the extra field.
|
---|
99 | if ((flags & EXTRA_FIELD) != 0)
|
---|
100 | {
|
---|
101 | int len = eof_read () | (eof_read () << 8);
|
---|
102 | while (len-- != 0)
|
---|
103 | eof_read ();
|
---|
104 | }
|
---|
105 |
|
---|
106 | if ((flags & ORIG_NAME) != 0)
|
---|
107 | {
|
---|
108 | while (true)
|
---|
109 | {
|
---|
110 | int c = eof_read ();
|
---|
111 | if (c == 0)
|
---|
112 | break;
|
---|
113 | }
|
---|
114 | }
|
---|
115 |
|
---|
116 | if ((flags & COMMENT) != 0)
|
---|
117 | {
|
---|
118 | while (true)
|
---|
119 | {
|
---|
120 | int c = eof_read ();
|
---|
121 | if (c == 0)
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | if ((flags & HEAD_CRC) != 0)
|
---|
127 | {
|
---|
128 | // FIXME: consider checking CRC of the header.
|
---|
129 | eof_read ();
|
---|
130 | eof_read ();
|
---|
131 | }
|
---|
132 |
|
---|
133 | crc = new CRC32 ();
|
---|
134 | }
|
---|
135 |
|
---|
136 | public int read (byte[] buf, int off, int len) throws IOException
|
---|
137 | {
|
---|
138 | if (eos)
|
---|
139 | return -1;
|
---|
140 | int r = super.read(buf, off, len);
|
---|
141 | if (r == -1)
|
---|
142 | {
|
---|
143 | eos = true;
|
---|
144 |
|
---|
145 | byte[] tmp = new byte[8];
|
---|
146 | // First copy remaining bytes from inflater input buffer.
|
---|
147 | int avail = inf.getRemaining ();
|
---|
148 | System.arraycopy (this.buf, this.len - avail, tmp, 0, avail);
|
---|
149 |
|
---|
150 | // Now read remaining bytes from wrapped input stream.
|
---|
151 | for (int i = avail; i < 8; ++i)
|
---|
152 | {
|
---|
153 | tmp[i] = (byte) eof_read ();
|
---|
154 | }
|
---|
155 |
|
---|
156 | int header_crc = read4 (tmp, 0);
|
---|
157 | if (crc.getValue() != header_crc)
|
---|
158 | throw new ZipException ("corrupted gzip file - crc mismatch");
|
---|
159 | int isize = read4 (tmp, 4);
|
---|
160 | if (inf.getTotalOut() != isize)
|
---|
161 | throw new ZipException ("corrupted gzip file - size mismatch");
|
---|
162 | return -1;
|
---|
163 | }
|
---|
164 | crc.update(buf, off, r);
|
---|
165 | return r;
|
---|
166 | }
|
---|
167 |
|
---|
168 | private final int read4 (byte[] buf, int offset) throws IOException
|
---|
169 | {
|
---|
170 | return (((buf[offset + 3] & 0xFF) << 24) + ((buf[offset + 2] & 0xFF) << 16)
|
---|
171 | + ((buf[offset + 1] & 0xFF) << 8) + (buf[offset] & 0xFF));
|
---|
172 | }
|
---|
173 |
|
---|
174 | // Checksum used by this input stream.
|
---|
175 | protected CRC32 crc;
|
---|
176 |
|
---|
177 | // Indicates whether end-of-stream has been reached.
|
---|
178 | protected boolean eos;
|
---|
179 |
|
---|
180 | // Some constants from zlib.
|
---|
181 | static final int Z_DEFLATED = 8;
|
---|
182 | static final int HEAD_CRC = 0x02;
|
---|
183 | static final int EXTRA_FIELD = 0x04;
|
---|
184 | static final int ORIG_NAME = 0x08;
|
---|
185 | static final int COMMENT = 0x10;
|
---|
186 | static final int RESERVED = 0xe0;
|
---|
187 | }
|
---|