[kaffe] CVS kaffe (robilad): Resynced with GNU Classpath: Mark's JarFile fixes

Kaffe CVS cvs-commits at kaffe.org
Fri Nov 12 17:00:33 PST 2004


PatchSet 5450 
Date: 2004/11/13 00:56:23
Author: robilad
Branch: HEAD
Tag: (none) 
Log:
Resynced with GNU Classpath: Mark's JarFile fixes

2004-11-12  Dalibor Topic  <robilad at kaffe.org>

        * libraries/javalib/java/util/jar/JarFile.java:
        Resynced with GNU Classpath.

        2004-11-11  Mark Wielaard  <mark at klomp.org>

        * java/util/jar/JarFile.java (verify): Make package private.
        (signaturesRead): Likewise.
        (verified): Likewise.
        (entryCerts): Likewise.
        (DEBUG): Likewise.
        (debug): Likewise.
        (entries): Construct new JarEnumeration with reference to this.
        (JarEnumeration): Make static.
        (JarEnumeration.jarfile): New field.
        (JarEnumeration.nextElement): Use and synchronize on jarfile.
        Compare verified value to Boolean.TRUE or Boolean.False only
        when verify is true.
        (getEntry): Make synchronized. Compare value of verified to
        Boolean.TRUE.
        (getInputStream): Construct EntryInputStream with reference to this.
        (getManifest): Make synchronized.
        (EntryInputStream): Make static.
        (EntryInputStream.jarfile): New field.
        (EntryInputStream.EntryInputStream): Check if manifest exists,
        before getting attributes.
        (eof): Synchronize on jarfile.

Members: 
	ChangeLog:1.2996->1.2997 
	libraries/javalib/java/util/jar/JarFile.java:1.7->1.8 

Index: kaffe/ChangeLog
diff -u kaffe/ChangeLog:1.2996 kaffe/ChangeLog:1.2997
--- kaffe/ChangeLog:1.2996	Fri Nov 12 23:35:54 2004
+++ kaffe/ChangeLog	Sat Nov 13 00:56:23 2004
@@ -1,3 +1,32 @@
+2004-11-12  Dalibor Topic  <robilad at kaffe.org>
+
+	* libraries/javalib/java/util/jar/JarFile.java:
+	Resynced with GNU Classpath.
+
+	2004-11-11  Mark Wielaard  <mark at klomp.org>
+
+        * java/util/jar/JarFile.java (verify): Make package private.
+        (signaturesRead): Likewise.
+        (verified): Likewise.
+        (entryCerts): Likewise.
+        (DEBUG): Likewise.
+        (debug): Likewise.
+        (entries): Construct new JarEnumeration with reference to this.
+        (JarEnumeration): Make static.
+        (JarEnumeration.jarfile): New field.
+        (JarEnumeration.nextElement): Use and synchronize on jarfile.
+        Compare verified value to Boolean.TRUE or Boolean.False only
+        when verify is true.
+        (getEntry): Make synchronized. Compare value of verified to
+        Boolean.TRUE.
+        (getInputStream): Construct EntryInputStream with reference to this.
+        (getManifest): Make synchronized.
+        (EntryInputStream): Make static.
+        (EntryInputStream.jarfile): New field.
+        (EntryInputStream.EntryInputStream): Check if manifest exists,
+        before getting attributes.
+        (eof): Synchronize on jarfile.
+
 2004-11-12  Guilhem Lavaux  <guilhem at kaffe.org>
 
 	* kaffe/kaffevm/systems/unix-pthreads/thread-impl.c
Index: kaffe/libraries/javalib/java/util/jar/JarFile.java
diff -u kaffe/libraries/javalib/java/util/jar/JarFile.java:1.7 kaffe/libraries/javalib/java/util/jar/JarFile.java:1.8
--- kaffe/libraries/javalib/java/util/jar/JarFile.java:1.7	Thu Nov 11 02:58:10 2004
+++ kaffe/libraries/javalib/java/util/jar/JarFile.java	Sat Nov 13 00:56:25 2004
@@ -119,26 +119,31 @@
    * The manifest of this file, if any, otherwise null.
    * Read when first needed.
    */
-  Manifest manifest;
+  private Manifest manifest;
 
   /** Whether to verify the manifest and all entries. */
-  private boolean verify;
+  boolean verify;
 
   /** Whether the has already been loaded. */
   private boolean manifestRead = false;
 
   /** Whether the signature files have been loaded. */
-  private boolean signaturesRead = false;
+  boolean signaturesRead = false;
 
-  /** A map between entry names and booleans, signaling whether or
-      not that entry has been verified. */
+  /**
+   * A map between entry names and booleans, signaling whether or
+   * not that entry has been verified.
+   * Only be accessed with lock on this JarFile*/
   HashMap verified = new HashMap();
 
-  /** A mapping from entry name to certificates, if any. */
-  private HashMap entryCerts;
+  /**
+   * A mapping from entry name to certificates, if any.
+   * Only accessed with lock on this JarFile.
+   */
+  HashMap entryCerts;
 
-  private static boolean DEBUG = false;
-  private static void debug(Object msg)
+  static boolean DEBUG = false;
+  static void debug(Object msg)
   {
     System.err.print(JarFile.class.getName());
     System.err.print(" >>> ");
@@ -303,21 +308,23 @@
    */
   public Enumeration entries() throws IllegalStateException
   {
-    return new JarEnumeration(super.entries());
+    return new JarEnumeration(super.entries(), this);
   }
 
   /**
    * Wraps a given Zip Entries Enumeration. For every zip entry a
    * JarEntry is created and the corresponding Attributes are looked up.
    */
-  private class JarEnumeration implements Enumeration
+  private static class JarEnumeration implements Enumeration
   {
 
     private final Enumeration entries;
+    private final JarFile jarfile;
 
-    JarEnumeration(Enumeration e)
+    JarEnumeration(Enumeration e, JarFile f)
     {
       entries = e;
+      jarfile = f;
     }
 
     public boolean hasMoreElements()
@@ -332,7 +339,7 @@
       Manifest manifest;
       try
 	{
-	  manifest = getManifest();
+	  manifest = jarfile.getManifest();
 	}
       catch (IOException ioe)
 	{
@@ -344,32 +351,35 @@
 	  jar.attr = manifest.getAttributes(jar.getName());
 	}
 
-      if (!signaturesRead)
-        try
-          {
-            readSignatures();
-          }
-        catch (IOException ioe)
-          {
-            if (DEBUG)
-              {
-                debug(ioe);
-                ioe.printStackTrace();
-              }
-            signaturesRead = true; // fudge it.
-          }
-
-      // Include the certificates only if we have asserted that the
-      // signatures are valid. This means the certificates will not be
-      // available if the entry hasn't been read yet.
-      if (entryCerts != null && verified.containsKey(zip.getName())
-          && ((Boolean) verified.get(zip.getName())).booleanValue())
-        {
-          Set certs = (Set) entryCerts.get(jar.getName());
-          if (certs != null)
-            jar.certs = (Certificate[])
-              certs.toArray(new Certificate[certs.size()]);
-        }
+      synchronized(jarfile)
+	{
+	  if (!jarfile.signaturesRead)
+	    try
+	      {
+		jarfile.readSignatures();
+	      }
+	    catch (IOException ioe)
+	      {
+		if (JarFile.DEBUG)
+		  {
+		    JarFile.debug(ioe);
+		    ioe.printStackTrace();
+		  }
+		jarfile.signaturesRead = true; // fudge it.
+	      }
+
+	  // Include the certificates only if we have asserted that the
+	  // signatures are valid. This means the certificates will not be
+	  // available if the entry hasn't been read yet.
+	  if (jarfile.entryCerts != null
+	      && jarfile.verified.get(zip.getName()) == Boolean.TRUE)
+	    {
+	      Set certs = (Set) jarfile.entryCerts.get(jar.getName());
+	      if (certs != null)
+		jar.certs = (Certificate[])
+		  certs.toArray(new Certificate[certs.size()]);
+	    }
+	}
       return jar;
     }
   }
@@ -379,7 +389,7 @@
    * It actually returns a JarEntry not a zipEntry
    * @param name XXX
    */
-  public ZipEntry getEntry(String name)
+  public synchronized ZipEntry getEntry(String name)
   {
     ZipEntry entry = super.getEntry(name);
     if (entry != null)
@@ -400,32 +410,31 @@
 	    jarEntry.attr = manifest.getAttributes(name);
           }
 
-        if (!signaturesRead)
-          try
-            {
-              readSignatures();
-            }
-          catch (IOException ioe)
-            {
-              if (DEBUG)
-                {
-                  debug(ioe);
-                  ioe.printStackTrace();
-                }
-              signaturesRead = true;
-            }
-        // See the comments in the JarEnumeration for why we do this
-        // check.
-        if (DEBUG)
-          debug("entryCerts=" + entryCerts + " verified " + name
-                + " ? " + verified.get(name));
-        if (entryCerts != null && verified.containsKey(name)
-            && ((Boolean) verified.get(name)).booleanValue())
-          {
-            Set certs = (Set) entryCerts.get(name);
-            if (certs != null)
-              jarEntry.certs = (Certificate[])
-                certs.toArray(new Certificate[certs.size()]);
+	if (!signaturesRead)
+	  try
+	    {
+	      readSignatures();
+	    }
+	  catch (IOException ioe)
+	    {
+	      if (DEBUG)
+		{
+		  debug(ioe);
+		  ioe.printStackTrace();
+		}
+	      signaturesRead = true;
+	    }
+	// See the comments in the JarEnumeration for why we do this
+	// check.
+	if (DEBUG)
+	  debug("entryCerts=" + entryCerts + " verified " + name
+		+ " ? " + verified.get(name));
+	if (entryCerts != null && verified.get(name) == Boolean.TRUE)
+	  {
+	    Set certs = (Set) entryCerts.get(name);
+	    if (certs != null)
+	      jarEntry.certs = (Certificate[])
+		certs.toArray(new Certificate[certs.size()]);
 	  }
 	return jarEntry;
       }
@@ -449,13 +458,13 @@
       {
         if (DEBUG)
           debug("reading and verifying " + entry);
-        return new EntryInputStream(entry, super.getInputStream(entry));
+        return new EntryInputStream(entry, super.getInputStream(entry), this);
       }
     else
       {
         if (DEBUG)
           debug("reading already verified entry " + entry);
-        if (!((Boolean) verified.get(entry.getName())).booleanValue())
+        if (verify && verified.get(entry.getName()) == Boolean.FALSE)
           throw new ZipException("digest for " + entry + " is invalid");
         return super.getInputStream(entry);
       }
@@ -479,7 +488,7 @@
    * Returns the manifest for this JarFile or null when the JarFile does not
    * contain a manifest file.
    */
-  public Manifest getManifest() throws IOException
+  public synchronized Manifest getManifest() throws IOException
   {
     if (!manifestRead)
       manifest = readManifest();
@@ -487,6 +496,7 @@
     return manifest;
   }
 
+  // Only called with lock on this JarFile.
   private void readSignatures() throws IOException
   {
     Map pkcs7Dsa = new HashMap();
@@ -872,8 +882,9 @@
   /**
    * A utility class that verifies jar entries as they are read.
    */
-  private class EntryInputStream extends FilterInputStream
+  private static class EntryInputStream extends FilterInputStream
   {
+    private final JarFile jarfile;
     private final long length;
     private long pos;
     private final ZipEntry entry;
@@ -881,16 +892,25 @@
     private final MessageDigest[] md;
     private boolean checked;
 
-    EntryInputStream(final ZipEntry entry, final InputStream in) throws IOException
+    EntryInputStream(final ZipEntry entry,
+		     final InputStream in,
+		     final JarFile jar)
+      throws IOException
     {
       super(in);
       this.entry = entry;
+      this.jarfile = jar;
 
       length = entry.getSize();
       pos = 0;
       checked = false;
 
-      Attributes attr = manifest.getAttributes(entry.getName());
+      Attributes attr;
+      Manifest manifest = jarfile.getManifest();
+      if (manifest != null)
+	attr = manifest.getAttributes(entry.getName());
+      else
+	attr = null;
       if (DEBUG)
         debug("verifying entry " + entry + " attr=" + attr);
       if (attr == null)
@@ -1010,17 +1030,24 @@
                   + " comp=" + new java.math.BigInteger(hash).toString(16));
           if (!Arrays.equals(hash, hashes[i]))
             {
-              if (DEBUG)
-                debug(entry + " could NOT be verified");
-              verified.put(entry.getName(), Boolean.FALSE);
-              return;
-              // XXX ??? what do we do here?
-              // throw new ZipException("message digest mismatch");
+	      synchronized(jarfile)
+		{
+		  if (DEBUG)
+		    debug(entry + " could NOT be verified");
+		  jarfile.verified.put(entry.getName(), Boolean.FALSE);
+		}
+	      return;
+	      // XXX ??? what do we do here?
+	      // throw new ZipException("message digest mismatch");
             }
         }
-      if (DEBUG)
-        debug(entry + " has been VERIFIED");
-      verified.put(entry.getName(), Boolean.TRUE);
+
+      synchronized(jarfile)
+	{
+	  if (DEBUG)
+	    debug(entry + " has been VERIFIED");
+	  jarfile.verified.put(entry.getName(), Boolean.TRUE);
+	}
     }
   }
 }




More information about the kaffe mailing list