Commit 5e81d5be by Mark Wielaard Committed by Mark Wielaard

ZipFile.java (readLeShort): Take and use DataInput as argument.

	* java/util/zip/ZipFile.java (readLeShort): Take and use DataInput as
	argument.
	(readLeShort): Likewise and use byte[].
	(readLeInt): Likewise.
	(readEntries): Use new versions of methods and use byte[] for reading
	a complete zip entry. Add ZipFile name to exceptions.
	(entries): Add ZipFile name to exceptions.
	(getEntry): Likewise.
	(checkLocalHeader): Use new versions of methods and add ZipFile name
	to exceptions.

From-SVN: r58697
parent bab076f7
2002-10-27 Mark Wielaard <mark@klomp.org>
* java/util/zip/ZipFile.java (readLeShort): Take and use DataInput as
argument.
(readLeShort): Likewise and use byte[].
(readLeInt): Likewise.
(readEntries): Use new versions of methods and use byte[] for reading
a complete zip entry. Add ZipFile name to exceptions.
(entries): Add ZipFile name to exceptions.
(getEntry): Likewise.
(checkLocalHeader): Use new versions of methods and add ZipFile name
to exceptions.
2002-10-31 Mark Anderson <mark@panonet.net> 2002-10-31 Mark Anderson <mark@panonet.net>
* java/awt/GridBagLayout.java (setConstraints): New stubbed method * java/awt/GridBagLayout.java (setConstraints): New stubbed method
......
/* java.util.zip.ZipFile /* java.util.zip.ZipFile
Copyright (C) 2001 Free Software Foundation, Inc. Copyright (C) 2001, 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath. This file is part of GNU Classpath.
...@@ -36,6 +36,10 @@ obligated to do so. If you do not wish to do so, delete this ...@@ -36,6 +36,10 @@ obligated to do so. If you do not wish to do so, delete this
exception statement from your version. */ exception statement from your version. */
package java.util.zip; package java.util.zip;
import java.io.ByteArrayInputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.IOException; import java.io.IOException;
...@@ -102,9 +106,9 @@ public class ZipFile implements ZipConstants ...@@ -102,9 +106,9 @@ public class ZipFile implements ZipConstants
/** /**
* Opens a Zip file reading the given File in the given mode. * Opens a Zip file reading the given File in the given mode.
* *
* If the OPEN_DELETE mode is specified, the zip file will be deleted at some time moment * If the OPEN_DELETE mode is specified, the zip file will be deleted at
* after it is opened. It will be deleted before the zip file is closed or the Virtual Machine * some time moment after it is opened. It will be deleted before the zip
* exits. * file is closed or the Virtual Machine exits.
* *
* The contents of the zip file will be accessible until it is closed. * The contents of the zip file will be accessible until it is closed.
* *
...@@ -121,7 +125,8 @@ public class ZipFile implements ZipConstants ...@@ -121,7 +125,8 @@ public class ZipFile implements ZipConstants
{ {
if ((mode & OPEN_DELETE) != 0) if ((mode & OPEN_DELETE) != 0)
{ {
throw new IllegalArgumentException("OPEN_DELETE mode not supported yet in java.util.zip.ZipFile"); throw new IllegalArgumentException
("OPEN_DELETE mode not supported yet in java.util.zip.ZipFile");
} }
this.raf = new RandomAccessFile(file, "r"); this.raf = new RandomAccessFile(file, "r");
this.name = file.getName(); this.name = file.getName();
...@@ -133,8 +138,11 @@ public class ZipFile implements ZipConstants ...@@ -133,8 +138,11 @@ public class ZipFile implements ZipConstants
* @exception IOException if a i/o error occured. * @exception IOException if a i/o error occured.
* @exception EOFException if the file ends prematurely * @exception EOFException if the file ends prematurely
*/ */
private final int readLeShort() throws IOException { private final int readLeShort(DataInput di) throws IOException
return raf.readUnsignedByte() | raf.readUnsignedByte() << 8; {
byte[] b = new byte[2];
di.readFully(b);
return (b[0] & 0xff) | (b[1] & 0xff) << 8;
} }
/** /**
...@@ -142,8 +150,12 @@ public class ZipFile implements ZipConstants ...@@ -142,8 +150,12 @@ public class ZipFile implements ZipConstants
* @exception IOException if a i/o error occured. * @exception IOException if a i/o error occured.
* @exception EOFException if the file ends prematurely * @exception EOFException if the file ends prematurely
*/ */
private final int readLeInt() throws IOException { private final int readLeInt(DataInput di) throws IOException
return readLeShort() | readLeShort() << 16; {
byte[] b = new byte[4];
di.readFully(b);
return ((b[0] & 0xff) | (b[1] & 0xff) << 8)
| ((b[2] & 0xff) | (b[3] & 0xff) << 8) << 16;
} }
/** /**
...@@ -164,36 +176,43 @@ public class ZipFile implements ZipConstants ...@@ -164,36 +176,43 @@ public class ZipFile implements ZipConstants
{ {
if (pos < 0) if (pos < 0)
throw new ZipException throw new ZipException
("central directory not found, probably not a zip file"); ("central directory not found, probably not a zip file: " + name);
raf.seek(pos--); raf.seek(pos--);
} }
while (readLeInt() != ENDSIG); while (readLeInt(raf) != ENDSIG);
if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD) if (raf.skipBytes(ENDTOT - ENDNRD) != ENDTOT - ENDNRD)
throw new EOFException(); throw new EOFException(name);
int count = readLeShort(); int count = readLeShort(raf);
if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ) if (raf.skipBytes(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ)
throw new EOFException(); throw new EOFException(name);
int centralOffset = readLeInt(); int centralOffset = readLeInt(raf);
entries = new ZipEntry[count]; entries = new ZipEntry[count];
raf.seek(centralOffset); raf.seek(centralOffset);
byte[] ebs = new byte[24];
ByteArrayInputStream ebais = new ByteArrayInputStream(ebs);
DataInputStream edip = new DataInputStream(ebais);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)
{ {
if (readLeInt() != CENSIG) if (readLeInt(raf) != CENSIG)
throw new ZipException("Wrong Central Directory signature"); throw new ZipException("Wrong Central Directory signature: " + name);
if (raf.skipBytes(CENHOW - CENVEM) != CENHOW - CENVEM) if (raf.skipBytes(CENHOW - CENVEM) != CENHOW - CENVEM)
throw new EOFException(); throw new EOFException(name);
int method = readLeShort();
int dostime = readLeInt(); raf.readFully(ebs);
int crc = readLeInt(); ebais.reset();
int csize = readLeInt(); int method = readLeShort(edip);
int size = readLeInt(); int dostime = readLeInt(edip);
int nameLen = readLeShort(); int crc = readLeInt(edip);
int extraLen = readLeShort(); int csize = readLeInt(edip);
int commentLen = readLeShort(); int size = readLeInt(edip);
int nameLen = readLeShort(edip);
int extraLen = readLeShort(edip);
int commentLen = readLeShort(edip);
if (raf.skipBytes(CENOFF - CENDSK) != CENOFF - CENDSK) if (raf.skipBytes(CENOFF - CENDSK) != CENOFF - CENDSK)
throw new EOFException(); throw new EOFException(name);
int offset = readLeInt(); int offset = readLeInt(raf);
byte[] buffer = new byte[Math.max(nameLen, commentLen)]; byte[] buffer = new byte[Math.max(nameLen, commentLen)];
...@@ -244,7 +263,7 @@ public class ZipFile implements ZipConstants ...@@ -244,7 +263,7 @@ public class ZipFile implements ZipConstants
public Enumeration entries() public Enumeration entries()
{ {
if (entries == null) if (entries == null)
throw new IllegalStateException("ZipFile has closed"); throw new IllegalStateException("ZipFile has closed: " + name);
return new ZipEntryEnumeration(entries); return new ZipEntryEnumeration(entries);
} }
...@@ -265,7 +284,7 @@ public class ZipFile implements ZipConstants ...@@ -265,7 +284,7 @@ public class ZipFile implements ZipConstants
public ZipEntry getEntry(String name) public ZipEntry getEntry(String name)
{ {
if (entries == null) if (entries == null)
throw new IllegalStateException("ZipFile has closed"); throw new IllegalStateException("ZipFile has closed: " + name);
int index = getEntryIndex(name); int index = getEntryIndex(name);
return index >= 0 ? (ZipEntry) entries[index].clone() : null; return index >= 0 ? (ZipEntry) entries[index].clone() : null;
} }
...@@ -283,24 +302,24 @@ public class ZipFile implements ZipConstants ...@@ -283,24 +302,24 @@ public class ZipFile implements ZipConstants
synchronized (raf) synchronized (raf)
{ {
raf.seek(entry.offset); raf.seek(entry.offset);
if (readLeInt() != LOCSIG) if (readLeInt(raf) != LOCSIG)
throw new ZipException("Wrong Local header signature"); throw new ZipException("Wrong Local header signature: " + name);
/* skip version and flags */ /* skip version and flags */
if (raf.skipBytes(LOCHOW - LOCVER) != LOCHOW - LOCVER) if (raf.skipBytes(LOCHOW - LOCVER) != LOCHOW - LOCVER)
throw new EOFException(); throw new EOFException(name);
if (entry.getMethod() != readLeShort()) if (entry.getMethod() != readLeShort(raf))
throw new ZipException("Compression method mismatch"); throw new ZipException("Compression method mismatch: " + name);
/* Skip time, crc, size and csize */ /* Skip time, crc, size and csize */
if (raf.skipBytes(LOCNAM - LOCTIM) != LOCNAM - LOCTIM) if (raf.skipBytes(LOCNAM - LOCTIM) != LOCNAM - LOCTIM)
throw new EOFException(); throw new EOFException(name);
if (entry.getName().length() != readLeShort()) if (entry.getName().length() != readLeShort(raf))
throw new ZipException("file name length mismatch"); throw new ZipException("file name length mismatch: " + name);
int extraLen = entry.getName().length() + readLeShort(); int extraLen = entry.getName().length() + readLeShort(raf);
return entry.offset + LOCHDR + extraLen; return entry.offset + LOCHDR + extraLen;
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment