author | jboes |
Tue, 24 Sep 2019 09:43:43 +0100 | |
changeset 58288 | 48e480e56aad |
parent 58242 | 94bb65cb37d3 |
child 58679 | 9c3209ff7550 |
child 59201 | b24f4caa1411 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57670
cffcc4c5a5ba
8226530: ZipFile reads wrong entry size from ZIP64 entries
lancea
parents:
50238
diff
changeset
|
2 |
* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.util.zip; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.io.IOException; |
|
30 |
import java.io.EOFException; |
|
31 |
import java.io.PushbackInputStream; |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
32 |
import java.nio.charset.Charset; |
9676
5663e62f8d7e
7041612: Rename StandardCharset to StandardCharsets
mduigou
parents:
9526
diff
changeset
|
33 |
import java.nio.charset.StandardCharsets; |
2438 | 34 |
import static java.util.zip.ZipConstants64.*; |
17910
82d10099a8a6
4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
11828
diff
changeset
|
35 |
import static java.util.zip.ZipUtils.*; |
2 | 36 |
|
37 |
/** |
|
38 |
* This class implements an input stream filter for reading files in the |
|
39 |
* ZIP file format. Includes support for both compressed and uncompressed |
|
40 |
* entries. |
|
41 |
* |
|
42 |
* @author David Connelly |
|
45434
4582657c7260
8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents:
29226
diff
changeset
|
43 |
* @since 1.1 |
2 | 44 |
*/ |
45 |
public |
|
46 |
class ZipInputStream extends InflaterInputStream implements ZipConstants { |
|
47 |
private ZipEntry entry; |
|
48 |
private int flag; |
|
49 |
private CRC32 crc = new CRC32(); |
|
50 |
private long remaining; |
|
51 |
private byte[] tmpbuf = new byte[512]; |
|
52 |
||
53 |
private static final int STORED = ZipEntry.STORED; |
|
54 |
private static final int DEFLATED = ZipEntry.DEFLATED; |
|
55 |
||
56 |
private boolean closed = false; |
|
57 |
// this flag is set to true after EOF has reached for |
|
58 |
// one entry |
|
59 |
private boolean entryEOF = false; |
|
60 |
||
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
61 |
private ZipCoder zc; |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
62 |
|
2 | 63 |
/** |
64 |
* Check to make sure that this stream has not been closed |
|
65 |
*/ |
|
66 |
private void ensureOpen() throws IOException { |
|
67 |
if (closed) { |
|
68 |
throw new IOException("Stream closed"); |
|
69 |
} |
|
70 |
} |
|
71 |
||
72 |
/** |
|
73 |
* Creates a new ZIP input stream. |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
74 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
75 |
* <p>The UTF-8 {@link java.nio.charset.Charset charset} is used to |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
76 |
* decode the entry names. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
77 |
* |
2 | 78 |
* @param in the actual input stream |
79 |
*/ |
|
80 |
public ZipInputStream(InputStream in) { |
|
9676
5663e62f8d7e
7041612: Rename StandardCharset to StandardCharsets
mduigou
parents:
9526
diff
changeset
|
81 |
this(in, StandardCharsets.UTF_8); |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
82 |
} |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
83 |
|
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
84 |
/** |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
85 |
* Creates a new ZIP input stream. |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
86 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
87 |
* @param in the actual input stream |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
88 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
89 |
* @param charset |
2704
a92617170304
6836489: Incorrect @link usage in java.util.zip API doc
sherman
parents:
2592
diff
changeset
|
90 |
* The {@linkplain java.nio.charset.Charset charset} to be |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
91 |
* used to decode the ZIP entry name (ignored if the |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
92 |
* <a href="package-summary.html#lang_encoding"> language |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
93 |
* encoding bit</a> of the ZIP entry's general purpose bit |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
94 |
* flag is set). |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
95 |
* |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
96 |
* @since 1.7 |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
97 |
*/ |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
98 |
public ZipInputStream(InputStream in, Charset charset) { |
2 | 99 |
super(new PushbackInputStream(in, 512), new Inflater(true), 512); |
100 |
usesDefaultInflater = true; |
|
50238
a9307f400f5a
8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents:
47216
diff
changeset
|
101 |
if (in == null) { |
2 | 102 |
throw new NullPointerException("in is null"); |
103 |
} |
|
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
104 |
if (charset == null) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
105 |
throw new NullPointerException("charset is null"); |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
106 |
this.zc = ZipCoder.get(charset); |
2 | 107 |
} |
108 |
||
109 |
/** |
|
110 |
* Reads the next ZIP file entry and positions the stream at the |
|
111 |
* beginning of the entry data. |
|
112 |
* @return the next ZIP file entry, or null if there are no more entries |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
113 |
* @throws ZipException if a ZIP file error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
114 |
* @throws IOException if an I/O error has occurred |
2 | 115 |
*/ |
116 |
public ZipEntry getNextEntry() throws IOException { |
|
117 |
ensureOpen(); |
|
118 |
if (entry != null) { |
|
119 |
closeEntry(); |
|
120 |
} |
|
121 |
crc.reset(); |
|
122 |
inf.reset(); |
|
123 |
if ((entry = readLOC()) == null) { |
|
124 |
return null; |
|
125 |
} |
|
126 |
if (entry.method == STORED) { |
|
127 |
remaining = entry.size; |
|
128 |
} |
|
129 |
entryEOF = false; |
|
130 |
return entry; |
|
131 |
} |
|
132 |
||
133 |
/** |
|
134 |
* Closes the current ZIP entry and positions the stream for reading the |
|
135 |
* next entry. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
136 |
* @throws ZipException if a ZIP file error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
137 |
* @throws IOException if an I/O error has occurred |
2 | 138 |
*/ |
139 |
public void closeEntry() throws IOException { |
|
140 |
ensureOpen(); |
|
141 |
while (read(tmpbuf, 0, tmpbuf.length) != -1) ; |
|
142 |
entryEOF = true; |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Returns 0 after EOF has reached for the current entry data, |
|
147 |
* otherwise always return 1. |
|
148 |
* <p> |
|
149 |
* Programs should not count on this method to return the actual number |
|
150 |
* of bytes that could be read without blocking. |
|
151 |
* |
|
152 |
* @return 1 before EOF and 0 after EOF has reached for current entry. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
153 |
* @throws IOException if an I/O error occurs. |
2 | 154 |
* |
155 |
*/ |
|
156 |
public int available() throws IOException { |
|
157 |
ensureOpen(); |
|
158 |
if (entryEOF) { |
|
159 |
return 0; |
|
160 |
} else { |
|
161 |
return 1; |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Reads from the current ZIP entry into an array of bytes. |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
167 |
* If {@code len} is not zero, the method |
2 | 168 |
* blocks until some input is available; otherwise, no |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
169 |
* bytes are read and {@code 0} is returned. |
2 | 170 |
* @param b the buffer into which the data is read |
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
171 |
* @param off the start offset in the destination array {@code b} |
2 | 172 |
* @param len the maximum number of bytes read |
173 |
* @return the actual number of bytes read, or -1 if the end of the |
|
174 |
* entry is reached |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
175 |
* @throws NullPointerException if {@code b} is {@code null}. |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
176 |
* @throws IndexOutOfBoundsException if {@code off} is negative, |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
177 |
* {@code len} is negative, or {@code len} is greater than |
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
178 |
* {@code b.length - off} |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
179 |
* @throws ZipException if a ZIP file error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
180 |
* @throws IOException if an I/O error has occurred |
2 | 181 |
*/ |
182 |
public int read(byte[] b, int off, int len) throws IOException { |
|
183 |
ensureOpen(); |
|
184 |
if (off < 0 || len < 0 || off > b.length - len) { |
|
185 |
throw new IndexOutOfBoundsException(); |
|
186 |
} else if (len == 0) { |
|
187 |
return 0; |
|
188 |
} |
|
189 |
||
190 |
if (entry == null) { |
|
191 |
return -1; |
|
192 |
} |
|
193 |
switch (entry.method) { |
|
194 |
case DEFLATED: |
|
195 |
len = super.read(b, off, len); |
|
196 |
if (len == -1) { |
|
197 |
readEnd(entry); |
|
198 |
entryEOF = true; |
|
199 |
entry = null; |
|
200 |
} else { |
|
201 |
crc.update(b, off, len); |
|
202 |
} |
|
203 |
return len; |
|
204 |
case STORED: |
|
205 |
if (remaining <= 0) { |
|
206 |
entryEOF = true; |
|
207 |
entry = null; |
|
208 |
return -1; |
|
209 |
} |
|
210 |
if (len > remaining) { |
|
211 |
len = (int)remaining; |
|
212 |
} |
|
213 |
len = in.read(b, off, len); |
|
214 |
if (len == -1) { |
|
215 |
throw new ZipException("unexpected EOF"); |
|
216 |
} |
|
217 |
crc.update(b, off, len); |
|
218 |
remaining -= len; |
|
219 |
if (remaining == 0 && entry.crc != crc.getValue()) { |
|
220 |
throw new ZipException( |
|
221 |
"invalid entry CRC (expected 0x" + Long.toHexString(entry.crc) + |
|
222 |
" but got 0x" + Long.toHexString(crc.getValue()) + ")"); |
|
223 |
} |
|
224 |
return len; |
|
225 |
default: |
|
226 |
throw new ZipException("invalid compression method"); |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
/** |
|
231 |
* Skips specified number of bytes in the current ZIP entry. |
|
232 |
* @param n the number of bytes to skip |
|
233 |
* @return the actual number of bytes skipped |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
234 |
* @throws ZipException if a ZIP file error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
235 |
* @throws IOException if an I/O error has occurred |
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
236 |
* @throws IllegalArgumentException if {@code n < 0} |
2 | 237 |
*/ |
238 |
public long skip(long n) throws IOException { |
|
239 |
if (n < 0) { |
|
240 |
throw new IllegalArgumentException("negative skip length"); |
|
241 |
} |
|
242 |
ensureOpen(); |
|
243 |
int max = (int)Math.min(n, Integer.MAX_VALUE); |
|
244 |
int total = 0; |
|
245 |
while (total < max) { |
|
246 |
int len = max - total; |
|
247 |
if (len > tmpbuf.length) { |
|
248 |
len = tmpbuf.length; |
|
249 |
} |
|
250 |
len = read(tmpbuf, 0, len); |
|
251 |
if (len == -1) { |
|
252 |
entryEOF = true; |
|
253 |
break; |
|
254 |
} |
|
255 |
total += len; |
|
256 |
} |
|
257 |
return total; |
|
258 |
} |
|
259 |
||
260 |
/** |
|
261 |
* Closes this input stream and releases any system resources associated |
|
262 |
* with the stream. |
|
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57670
diff
changeset
|
263 |
* @throws IOException if an I/O error has occurred |
2 | 264 |
*/ |
265 |
public void close() throws IOException { |
|
266 |
if (!closed) { |
|
267 |
super.close(); |
|
268 |
closed = true; |
|
269 |
} |
|
270 |
} |
|
271 |
||
272 |
private byte[] b = new byte[256]; |
|
273 |
||
274 |
/* |
|
275 |
* Reads local file (LOC) header for next entry. |
|
276 |
*/ |
|
277 |
private ZipEntry readLOC() throws IOException { |
|
278 |
try { |
|
279 |
readFully(tmpbuf, 0, LOCHDR); |
|
280 |
} catch (EOFException e) { |
|
281 |
return null; |
|
282 |
} |
|
283 |
if (get32(tmpbuf, 0) != LOCSIG) { |
|
284 |
return null; |
|
285 |
} |
|
50238
a9307f400f5a
8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents:
47216
diff
changeset
|
286 |
// get flag first, we need check USE_UTF8. |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
287 |
flag = get16(tmpbuf, LOCFLG); |
2 | 288 |
// get the entry name and create the ZipEntry first |
289 |
int len = get16(tmpbuf, LOCNAM); |
|
290 |
int blen = b.length; |
|
291 |
if (len > blen) { |
|
19374
6773349693eb
8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents:
18156
diff
changeset
|
292 |
do { |
2 | 293 |
blen = blen * 2; |
19374
6773349693eb
8015666: test/tools/pack200/TimeStamp.java failing
sherman
parents:
18156
diff
changeset
|
294 |
} while (len > blen); |
2 | 295 |
b = new byte[blen]; |
296 |
} |
|
297 |
readFully(b, 0, len); |
|
50238
a9307f400f5a
8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents:
47216
diff
changeset
|
298 |
// Force to use UTF-8 if the USE_UTF8 bit is ON |
a9307f400f5a
8203328: Rename EFS in java.util.zip internals to something meaningful
martin
parents:
47216
diff
changeset
|
299 |
ZipEntry e = createZipEntry(((flag & USE_UTF8) != 0) |
2592
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
300 |
? zc.toStringUTF8(b, len) |
ef26f663a2ba
4244499: ZipEntry() does not convert filenames from Unicode to platform
sherman
parents:
2438
diff
changeset
|
301 |
: zc.toString(b, len)); |
2 | 302 |
// now get the remaining fields for the entry |
303 |
if ((flag & 1) == 1) { |
|
304 |
throw new ZipException("encrypted ZIP entry not supported"); |
|
305 |
} |
|
306 |
e.method = get16(tmpbuf, LOCHOW); |
|
29226 | 307 |
e.xdostime = get32(tmpbuf, LOCTIM); |
2 | 308 |
if ((flag & 8) == 8) { |
309 |
/* "Data Descriptor" present */ |
|
310 |
if (e.method != DEFLATED) { |
|
311 |
throw new ZipException( |
|
312 |
"only DEFLATED entries can have EXT descriptor"); |
|
313 |
} |
|
314 |
} else { |
|
315 |
e.crc = get32(tmpbuf, LOCCRC); |
|
316 |
e.csize = get32(tmpbuf, LOCSIZ); |
|
317 |
e.size = get32(tmpbuf, LOCLEN); |
|
318 |
} |
|
319 |
len = get16(tmpbuf, LOCEXT); |
|
320 |
if (len > 0) { |
|
17910
82d10099a8a6
4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
11828
diff
changeset
|
321 |
byte[] extra = new byte[len]; |
82d10099a8a6
4759491: method ZipEntry.setTime(long) works incorrectly
sherman
parents:
11828
diff
changeset
|
322 |
readFully(extra, 0, len); |
24711
796059d86950
8044727: Problem reading the contents of some zip files
sherman
parents:
19374
diff
changeset
|
323 |
e.setExtra0(extra, |
57670
cffcc4c5a5ba
8226530: ZipFile reads wrong entry size from ZIP64 entries
lancea
parents:
50238
diff
changeset
|
324 |
e.csize == ZIP64_MAGICVAL || e.size == ZIP64_MAGICVAL, true); |
2 | 325 |
} |
326 |
return e; |
|
327 |
} |
|
328 |
||
329 |
/** |
|
58288
48e480e56aad
8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents:
58242
diff
changeset
|
330 |
* Creates a new {@code ZipEntry} object for the specified |
2 | 331 |
* entry name. |
332 |
* |
|
333 |
* @param name the ZIP file entry name |
|
334 |
* @return the ZipEntry just created |
|
335 |
*/ |
|
336 |
protected ZipEntry createZipEntry(String name) { |
|
337 |
return new ZipEntry(name); |
|
338 |
} |
|
339 |
||
26615
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
340 |
/** |
2 | 341 |
* Reads end of deflated entry as well as EXT descriptor if present. |
26615
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
342 |
* |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
343 |
* Local headers for DEFLATED entries may optionally be followed by a |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
344 |
* data descriptor, and that data descriptor may optionally contain a |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
345 |
* leading signature (EXTSIG). |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
346 |
* |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
347 |
* From the zip spec http://www.pkware.com/documents/casestudies/APPNOTE.TXT |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
348 |
* |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
349 |
* """Although not originally assigned a signature, the value 0x08074b50 |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
350 |
* has commonly been adopted as a signature value for the data descriptor |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
351 |
* record. Implementers should be aware that ZIP files may be |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
352 |
* encountered with or without this signature marking data descriptors |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
353 |
* and should account for either case when reading ZIP files to ensure |
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
354 |
* compatibility.""" |
2 | 355 |
*/ |
356 |
private void readEnd(ZipEntry e) throws IOException { |
|
357 |
int n = inf.getRemaining(); |
|
358 |
if (n > 0) { |
|
359 |
((PushbackInputStream)in).unread(buf, len - n, n); |
|
360 |
} |
|
361 |
if ((flag & 8) == 8) { |
|
362 |
/* "Data Descriptor" present */ |
|
2438 | 363 |
if (inf.getBytesWritten() > ZIP64_MAGICVAL || |
364 |
inf.getBytesRead() > ZIP64_MAGICVAL) { |
|
365 |
// ZIP64 format |
|
366 |
readFully(tmpbuf, 0, ZIP64_EXTHDR); |
|
367 |
long sig = get32(tmpbuf, 0); |
|
368 |
if (sig != EXTSIG) { // no EXTSIG present |
|
369 |
e.crc = sig; |
|
370 |
e.csize = get64(tmpbuf, ZIP64_EXTSIZ - ZIP64_EXTCRC); |
|
371 |
e.size = get64(tmpbuf, ZIP64_EXTLEN - ZIP64_EXTCRC); |
|
372 |
((PushbackInputStream)in).unread( |
|
26615
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
373 |
tmpbuf, ZIP64_EXTHDR - ZIP64_EXTCRC, ZIP64_EXTCRC); |
2438 | 374 |
} else { |
375 |
e.crc = get32(tmpbuf, ZIP64_EXTCRC); |
|
376 |
e.csize = get64(tmpbuf, ZIP64_EXTSIZ); |
|
377 |
e.size = get64(tmpbuf, ZIP64_EXTLEN); |
|
378 |
} |
|
2 | 379 |
} else { |
2438 | 380 |
readFully(tmpbuf, 0, EXTHDR); |
381 |
long sig = get32(tmpbuf, 0); |
|
382 |
if (sig != EXTSIG) { // no EXTSIG present |
|
383 |
e.crc = sig; |
|
384 |
e.csize = get32(tmpbuf, EXTSIZ - EXTCRC); |
|
385 |
e.size = get32(tmpbuf, EXTLEN - EXTCRC); |
|
386 |
((PushbackInputStream)in).unread( |
|
26615
faabac3002f1
8056934: ZipInputStream does not correctly handle local header data descriptors with the optional signature missing
martin
parents:
25859
diff
changeset
|
387 |
tmpbuf, EXTHDR - EXTCRC, EXTCRC); |
2438 | 388 |
} else { |
389 |
e.crc = get32(tmpbuf, EXTCRC); |
|
390 |
e.csize = get32(tmpbuf, EXTSIZ); |
|
391 |
e.size = get32(tmpbuf, EXTLEN); |
|
392 |
} |
|
2 | 393 |
} |
394 |
} |
|
395 |
if (e.size != inf.getBytesWritten()) { |
|
396 |
throw new ZipException( |
|
397 |
"invalid entry size (expected " + e.size + |
|
398 |
" but got " + inf.getBytesWritten() + " bytes)"); |
|
399 |
} |
|
400 |
if (e.csize != inf.getBytesRead()) { |
|
401 |
throw new ZipException( |
|
402 |
"invalid entry compressed size (expected " + e.csize + |
|
403 |
" but got " + inf.getBytesRead() + " bytes)"); |
|
404 |
} |
|
405 |
if (e.crc != crc.getValue()) { |
|
406 |
throw new ZipException( |
|
407 |
"invalid entry CRC (expected 0x" + Long.toHexString(e.crc) + |
|
408 |
" but got 0x" + Long.toHexString(crc.getValue()) + ")"); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
/* |
|
413 |
* Reads bytes, blocking until all bytes are read. |
|
414 |
*/ |
|
415 |
private void readFully(byte[] b, int off, int len) throws IOException { |
|
416 |
while (len > 0) { |
|
417 |
int n = in.read(b, off, len); |
|
418 |
if (n == -1) { |
|
419 |
throw new EOFException(); |
|
420 |
} |
|
421 |
off += n; |
|
422 |
len -= n; |
|
423 |
} |
|
424 |
} |
|
425 |
||
426 |
} |