harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ndbe...@apache.org
Subject svn commit: r446478 [2/11] - in /incubator/harmony/enhanced/classlib/trunk/modules/archive: ./ .settings/ META-INF/ make/ src/main/java/java/util/jar/ src/main/java/java/util/zip/ src/main/java/org/apache/harmony/archive/internal/pack200/ src/main/nati...
Date Fri, 15 Sep 2006 02:40:36 GMT
Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java?view=diff&rev=446478&r1=446477&r2=446478
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java Thu Sep 14 19:40:33 2006
@@ -1,382 +1,382 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.util.jar;
-
-import java.io.ByteArrayInputStream;
-import java.io.File;
-import java.io.FilterInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.security.MessageDigest;
-import java.util.Enumeration;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipFile;
-
-/**
- * JarFile is used to read jar entries and their associated data from jar files.
- * 
- * @see JarInputStream
- * @see JarEntry
- */
-public class JarFile extends ZipFile {
-
-    public static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";
-
-    static final String META_DIR = "META-INF/";
-
-    private Manifest manifest;
-
-    private ZipEntry manifestEntry;
-
-    JarVerifier verifier;
-
-    static final class JarFileInputStream extends FilterInputStream {
-        private long count;
-
-        private ZipEntry zipEntry;
-
-        private JarVerifier verifier;
-
-        private JarVerifier.VerifierEntry entry;
-
-        private MessageDigest digest;
-
-        JarFileInputStream(InputStream is, ZipEntry ze, JarVerifier ver) {
-            super(is);
-            if (ver != null) {
-                zipEntry = ze;
-                verifier = ver;
-                count = zipEntry.getSize();
-                entry = verifier.initEntry(ze.getName());
-                if (entry != null) {
-                    digest = entry.digest;
-                }
-            }
-        }
-
-        public int read() throws IOException {
-            int r = super.read();
-            if (entry != null) {
-                if (r != -1) {
-                    digest.update((byte) r);
-                    count--;
-                }
-                if (r == -1 || count <= 0) {
-                    JarVerifier.VerifierEntry temp = entry;
-                    entry = null;
-                    verifier.verifySignatures(temp, zipEntry);
-                }
-            }
-            return r;
-        }
-
-        public int read(byte[] buf, int off, int nbytes) throws IOException {
-            int r = super.read(buf, off, nbytes);
-            if (entry != null) {
-                if (r != -1) {
-                    int size = r;
-                    if (count < size) {
-                        size = (int) count;
-                    }
-                    digest.update(buf, off, size);
-                    count -= r;
-                }
-                if (r == -1 || count <= 0) {
-                    JarVerifier.VerifierEntry temp = entry;
-                    entry = null;
-                    verifier.verifySignatures(temp, zipEntry);
-                }
-            }
-            return r;
-        }
-
-        public long skip(long nbytes) throws IOException {
-            long cnt = 0, rem = 0;
-            byte[] buf = new byte[4096];
-            while (cnt < nbytes) {
-                int x = read(buf, 0,
-                        (rem = nbytes - cnt) > buf.length ? buf.length
-                                : (int) rem);
-                if (x == -1) {
-                    return cnt;
-                }
-                cnt += x;
-            }
-            return cnt;
-        }
-    }
-
-    /**
-     * Create a new JarFile using the contents of file.
-     * 
-     * @param file
-     *            java.io.File
-     * @exception java.io.IOException
-     *                If the file cannot be read.
-     */
-    public JarFile(File file) throws IOException {
-        this(file, true);
-    }
-
-    /**
-     * Create a new JarFile using the contents of file.
-     * 
-     * @param file
-     *            java.io.File
-     * @param verify
-     *            verify a signed jar file
-     * @exception java.io.IOException
-     *                If the file cannot be read.
-     */
-    public JarFile(File file, boolean verify) throws IOException {
-        super(file);
-        if (verify) {
-            verifier = new JarVerifier(file.getPath());
-        }
-        readMetaEntries();
-    }
-
-    /**
-     * Create a new JarFile using the contents of file.
-     * 
-     * @param file
-     *            java.io.File
-     * @param verify
-     *            verify a signed jar file
-     * @param mode
-     *            the mode to use, either OPEN_READ or OPEN_READ | OPEN_DELETE
-     * @exception java.io.IOException
-     *                If the file cannot be read.
-     */
-    public JarFile(File file, boolean verify, int mode) throws IOException {
-        super(file, mode);
-        if (verify) {
-            verifier = new JarVerifier(file.getPath());
-        }
-        readMetaEntries();
-    }
-
-    /**
-     * Create a new JarFile from the contents of the file specified by filename.
-     * 
-     * @param filename
-     *            java.lang.String
-     * @exception java.io.IOException
-     *                If fileName cannot be opened for reading.
-     */
-    public JarFile(String filename) throws IOException {
-        this(filename, true);
-
-    }
-
-    /**
-     * Create a new JarFile from the contents of the file specified by filename.
-     * 
-     * @param filename
-     *            java.lang.String
-     * @param verify
-     *            verify a signed jar file
-     * @exception java.io.IOException
-     *                If fileName cannot be opened for reading.
-     */
-    public JarFile(String filename, boolean verify) throws IOException {
-        super(filename);
-        if (verify) {
-            verifier = new JarVerifier(filename);
-        }
-        readMetaEntries();
-    }
-
-    /**
-     * Return an enumeration containing the JarEntrys contained in this JarFile.
-     * 
-     * @return java.util.Enumeration
-     * @exception java.lang.IllegalStateException
-     *                If this JarFile has been closed.
-     */
-    public Enumeration<JarEntry> entries() {
-        class JarFileEnumerator implements Enumeration<JarEntry> {
-            Enumeration<? extends ZipEntry> ze;
-
-            JarFile jf;
-
-            JarFileEnumerator(Enumeration<? extends ZipEntry> zenum, JarFile jf) {
-                ze = zenum;
-                this.jf = jf;
-            }
-
-            public boolean hasMoreElements() {
-                return ze.hasMoreElements();
-            }
-
-            public JarEntry nextElement() {
-                JarEntry je = new JarEntry(ze.nextElement());
-                je.parentJar = jf;
-                if (verifier != null) {
-                    je.certificates = verifier.getCertificates(je.getName());
-                }
-                return je;
-            }
-        }
-        return new JarFileEnumerator(super.entries(), this);
-    }
-
-    /**
-     * Return the JarEntry specified by name or null if no such entry exists.
-     * 
-     * @param name
-     *            the name of the entry in the jar file
-     * @return java.util.jar.JarEntry
-     */
-    public JarEntry getJarEntry(String name) {
-        return (JarEntry) getEntry(name);
-    }
-
-    /**
-     * Returns the Manifest object associated with this JarFile or null if no
-     * manifest entry exists.
-     * 
-     * @return java.util.jar.Manifest
-     */
-    public Manifest getManifest() throws IOException {
-        if (manifest != null) {
-            return manifest;
-        }
-		try {
-            ByteArrayInputStream is = (ByteArrayInputStream) super
-                    .getInputStream(manifestEntry);
-            if (verifier != null) {
-                byte[] buf = new byte[is.available()];
-                is.mark(buf.length);
-                is.read(buf, 0, buf.length);
-                is.reset();
-                verifier.addMetaEntry(manifestEntry.getName(), buf);
-            }
-            try {
-                manifest = new Manifest(is, verifier != null);
-            } finally {
-                is.close();
-            }
-	        manifestEntry = null;
-		} catch(NullPointerException e) {
-			manifestEntry = null;
-		}
-        return manifest;
-    }
-
-    private void readMetaEntries() throws IOException {
-        ZipEntry[] metaEntries = this.getMetaEntriesImpl(null);
-        int dirLength = META_DIR.length();
-
-        boolean signed = false;
-        if (null != metaEntries) {
-            for (ZipEntry entry : metaEntries) {
-                String entryName = entry.getName();
-                if (manifestEntry == null
-                        && manifest == null
-                        && entryName.regionMatches(true, dirLength,
-                                MANIFEST_NAME, dirLength, MANIFEST_NAME
-                                        .length()
-                                        - dirLength)) {
-                    manifestEntry = entry;
-                    if (verifier == null) {
-                        break;
-                    }
-                } else if (verifier != null
-                        && entryName.length() > dirLength
-                        && (entryName.regionMatches(true,
-                                entryName.length() - 3, ".SF", 0, 3)
-                                || entryName.regionMatches(true, entryName
-                                        .length() - 4, ".DSA", 0, 4) || entryName
-                                .regionMatches(true, entryName.length() - 4,
-                                        ".RSA", 0, 4))) {
-                    signed = true;
-                    InputStream is = super.getInputStream(entry);
-                    byte[] buf = new byte[is.available()];
-                    try {
-                        is.read(buf, 0, buf.length);
-                    } finally {
-                        is.close();
-                    }
-                    verifier.addMetaEntry(entryName, buf);
-                }
-            }
-        }
-        if (!signed) {
-            verifier = null;
-        }
-    }
-
-    /**
-     * Return an InputStream for reading the decompressed contents of ze.
-     * 
-     * @param ze
-     *            the ZipEntry to read from
-     * @return java.io.InputStream
-     * @exception java.io.IOException
-     *                If an error occured while creating the InputStream.
-     */
-    public InputStream getInputStream(ZipEntry ze) throws IOException {
-        if (manifestEntry != null) {
-            getManifest();
-        }
-        if (verifier != null) {
-            verifier.setManifest(getManifest());
-            if (manifest != null) {
-                verifier.mainAttributesChunk = manifest
-                        .getMainAttributesChunk();
-            }
-            if (verifier.readCertificates()) {
-                verifier.removeMetaEntries();
-                if (manifest != null) {
-                    manifest.removeChunks();
-                }
-                if (!verifier.isSignedJar()) {
-                    verifier = null;
-                }
-            }
-        }
-        InputStream in = super.getInputStream(ze);
-        if (in == null) {
-            return null;
-        }
-        return new JarFileInputStream(in, ze, ze.getSize() >= 0 ? verifier
-                : null);
-    }
-
-    /**
-     * Return the JarEntry specified by name or null if no such entry exists
-     * 
-     * @param name
-     *            the name of the entry in the jar file
-     * @return java.util.jar.JarEntry
-     */
-    public ZipEntry getEntry(String name) {
-        ZipEntry ze = super.getEntry(name);
-        if (ze == null) {
-            return ze;
-        }
-        JarEntry je = new JarEntry(ze);
-        je.parentJar = this;
-        if (verifier != null) {
-            je.certificates = verifier.getCertificates(je.getName());
-        }
-        return je;
-    }
-
-    private native ZipEntry[] getMetaEntriesImpl(byte[] buf);
-
-}
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util.jar;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FilterInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.security.MessageDigest;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+/**
+ * JarFile is used to read jar entries and their associated data from jar files.
+ * 
+ * @see JarInputStream
+ * @see JarEntry
+ */
+public class JarFile extends ZipFile {
+
+    public static final String MANIFEST_NAME = "META-INF/MANIFEST.MF";
+
+    static final String META_DIR = "META-INF/";
+
+    private Manifest manifest;
+
+    private ZipEntry manifestEntry;
+
+    JarVerifier verifier;
+
+    static final class JarFileInputStream extends FilterInputStream {
+        private long count;
+
+        private ZipEntry zipEntry;
+
+        private JarVerifier verifier;
+
+        private JarVerifier.VerifierEntry entry;
+
+        private MessageDigest digest;
+
+        JarFileInputStream(InputStream is, ZipEntry ze, JarVerifier ver) {
+            super(is);
+            if (ver != null) {
+                zipEntry = ze;
+                verifier = ver;
+                count = zipEntry.getSize();
+                entry = verifier.initEntry(ze.getName());
+                if (entry != null) {
+                    digest = entry.digest;
+                }
+            }
+        }
+
+        public int read() throws IOException {
+            int r = super.read();
+            if (entry != null) {
+                if (r != -1) {
+                    digest.update((byte) r);
+                    count--;
+                }
+                if (r == -1 || count <= 0) {
+                    JarVerifier.VerifierEntry temp = entry;
+                    entry = null;
+                    verifier.verifySignatures(temp, zipEntry);
+                }
+            }
+            return r;
+        }
+
+        public int read(byte[] buf, int off, int nbytes) throws IOException {
+            int r = super.read(buf, off, nbytes);
+            if (entry != null) {
+                if (r != -1) {
+                    int size = r;
+                    if (count < size) {
+                        size = (int) count;
+                    }
+                    digest.update(buf, off, size);
+                    count -= r;
+                }
+                if (r == -1 || count <= 0) {
+                    JarVerifier.VerifierEntry temp = entry;
+                    entry = null;
+                    verifier.verifySignatures(temp, zipEntry);
+                }
+            }
+            return r;
+        }
+
+        public long skip(long nbytes) throws IOException {
+            long cnt = 0, rem = 0;
+            byte[] buf = new byte[4096];
+            while (cnt < nbytes) {
+                int x = read(buf, 0,
+                        (rem = nbytes - cnt) > buf.length ? buf.length
+                                : (int) rem);
+                if (x == -1) {
+                    return cnt;
+                }
+                cnt += x;
+            }
+            return cnt;
+        }
+    }
+
+    /**
+     * Create a new JarFile using the contents of file.
+     * 
+     * @param file
+     *            java.io.File
+     * @exception java.io.IOException
+     *                If the file cannot be read.
+     */
+    public JarFile(File file) throws IOException {
+        this(file, true);
+    }
+
+    /**
+     * Create a new JarFile using the contents of file.
+     * 
+     * @param file
+     *            java.io.File
+     * @param verify
+     *            verify a signed jar file
+     * @exception java.io.IOException
+     *                If the file cannot be read.
+     */
+    public JarFile(File file, boolean verify) throws IOException {
+        super(file);
+        if (verify) {
+            verifier = new JarVerifier(file.getPath());
+        }
+        readMetaEntries();
+    }
+
+    /**
+     * Create a new JarFile using the contents of file.
+     * 
+     * @param file
+     *            java.io.File
+     * @param verify
+     *            verify a signed jar file
+     * @param mode
+     *            the mode to use, either OPEN_READ or OPEN_READ | OPEN_DELETE
+     * @exception java.io.IOException
+     *                If the file cannot be read.
+     */
+    public JarFile(File file, boolean verify, int mode) throws IOException {
+        super(file, mode);
+        if (verify) {
+            verifier = new JarVerifier(file.getPath());
+        }
+        readMetaEntries();
+    }
+
+    /**
+     * Create a new JarFile from the contents of the file specified by filename.
+     * 
+     * @param filename
+     *            java.lang.String
+     * @exception java.io.IOException
+     *                If fileName cannot be opened for reading.
+     */
+    public JarFile(String filename) throws IOException {
+        this(filename, true);
+
+    }
+
+    /**
+     * Create a new JarFile from the contents of the file specified by filename.
+     * 
+     * @param filename
+     *            java.lang.String
+     * @param verify
+     *            verify a signed jar file
+     * @exception java.io.IOException
+     *                If fileName cannot be opened for reading.
+     */
+    public JarFile(String filename, boolean verify) throws IOException {
+        super(filename);
+        if (verify) {
+            verifier = new JarVerifier(filename);
+        }
+        readMetaEntries();
+    }
+
+    /**
+     * Return an enumeration containing the JarEntrys contained in this JarFile.
+     * 
+     * @return java.util.Enumeration
+     * @exception java.lang.IllegalStateException
+     *                If this JarFile has been closed.
+     */
+    public Enumeration<JarEntry> entries() {
+        class JarFileEnumerator implements Enumeration<JarEntry> {
+            Enumeration<? extends ZipEntry> ze;
+
+            JarFile jf;
+
+            JarFileEnumerator(Enumeration<? extends ZipEntry> zenum, JarFile jf) {
+                ze = zenum;
+                this.jf = jf;
+            }
+
+            public boolean hasMoreElements() {
+                return ze.hasMoreElements();
+            }
+
+            public JarEntry nextElement() {
+                JarEntry je = new JarEntry(ze.nextElement());
+                je.parentJar = jf;
+                if (verifier != null) {
+                    je.certificates = verifier.getCertificates(je.getName());
+                }
+                return je;
+            }
+        }
+        return new JarFileEnumerator(super.entries(), this);
+    }
+
+    /**
+     * Return the JarEntry specified by name or null if no such entry exists.
+     * 
+     * @param name
+     *            the name of the entry in the jar file
+     * @return java.util.jar.JarEntry
+     */
+    public JarEntry getJarEntry(String name) {
+        return (JarEntry) getEntry(name);
+    }
+
+    /**
+     * Returns the Manifest object associated with this JarFile or null if no
+     * manifest entry exists.
+     * 
+     * @return java.util.jar.Manifest
+     */
+    public Manifest getManifest() throws IOException {
+        if (manifest != null) {
+            return manifest;
+        }
+		try {
+            ByteArrayInputStream is = (ByteArrayInputStream) super
+                    .getInputStream(manifestEntry);
+            if (verifier != null) {
+                byte[] buf = new byte[is.available()];
+                is.mark(buf.length);
+                is.read(buf, 0, buf.length);
+                is.reset();
+                verifier.addMetaEntry(manifestEntry.getName(), buf);
+            }
+            try {
+                manifest = new Manifest(is, verifier != null);
+            } finally {
+                is.close();
+            }
+	        manifestEntry = null;
+		} catch(NullPointerException e) {
+			manifestEntry = null;
+		}
+        return manifest;
+    }
+
+    private void readMetaEntries() throws IOException {
+        ZipEntry[] metaEntries = this.getMetaEntriesImpl(null);
+        int dirLength = META_DIR.length();
+
+        boolean signed = false;
+        if (null != metaEntries) {
+            for (ZipEntry entry : metaEntries) {
+                String entryName = entry.getName();
+                if (manifestEntry == null
+                        && manifest == null
+                        && entryName.regionMatches(true, dirLength,
+                                MANIFEST_NAME, dirLength, MANIFEST_NAME
+                                        .length()
+                                        - dirLength)) {
+                    manifestEntry = entry;
+                    if (verifier == null) {
+                        break;
+                    }
+                } else if (verifier != null
+                        && entryName.length() > dirLength
+                        && (entryName.regionMatches(true,
+                                entryName.length() - 3, ".SF", 0, 3)
+                                || entryName.regionMatches(true, entryName
+                                        .length() - 4, ".DSA", 0, 4) || entryName
+                                .regionMatches(true, entryName.length() - 4,
+                                        ".RSA", 0, 4))) {
+                    signed = true;
+                    InputStream is = super.getInputStream(entry);
+                    byte[] buf = new byte[is.available()];
+                    try {
+                        is.read(buf, 0, buf.length);
+                    } finally {
+                        is.close();
+                    }
+                    verifier.addMetaEntry(entryName, buf);
+                }
+            }
+        }
+        if (!signed) {
+            verifier = null;
+        }
+    }
+
+    /**
+     * Return an InputStream for reading the decompressed contents of ze.
+     * 
+     * @param ze
+     *            the ZipEntry to read from
+     * @return java.io.InputStream
+     * @exception java.io.IOException
+     *                If an error occured while creating the InputStream.
+     */
+    public InputStream getInputStream(ZipEntry ze) throws IOException {
+        if (manifestEntry != null) {
+            getManifest();
+        }
+        if (verifier != null) {
+            verifier.setManifest(getManifest());
+            if (manifest != null) {
+                verifier.mainAttributesChunk = manifest
+                        .getMainAttributesChunk();
+            }
+            if (verifier.readCertificates()) {
+                verifier.removeMetaEntries();
+                if (manifest != null) {
+                    manifest.removeChunks();
+                }
+                if (!verifier.isSignedJar()) {
+                    verifier = null;
+                }
+            }
+        }
+        InputStream in = super.getInputStream(ze);
+        if (in == null) {
+            return null;
+        }
+        return new JarFileInputStream(in, ze, ze.getSize() >= 0 ? verifier
+                : null);
+    }
+
+    /**
+     * Return the JarEntry specified by name or null if no such entry exists
+     * 
+     * @param name
+     *            the name of the entry in the jar file
+     * @return java.util.jar.JarEntry
+     */
+    public ZipEntry getEntry(String name) {
+        ZipEntry ze = super.getEntry(name);
+        if (ze == null) {
+            return ze;
+        }
+        JarEntry je = new JarEntry(ze);
+        je.parentJar = this;
+        if (verifier != null) {
+            je.certificates = verifier.getCertificates(je.getName());
+        }
+        return je;
+    }
+
+    private native ZipEntry[] getMetaEntriesImpl(byte[] buf);
+
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarFile.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarInputStream.java?view=diff&rev=446478&r1=446477&r2=446478
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarInputStream.java Thu Sep 14 19:40:33 2006
@@ -1,181 +1,181 @@
-/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.util.jar;
-
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipInputStream;
-
-public class JarInputStream extends ZipInputStream {
-
-    private Manifest manifest;
-
-    private boolean eos = false;
-
-    private JarEntry mEntry;
-
-    private JarEntry jarEntry;
-
-    private boolean isMeta;
-
-    private JarVerifier verifier;
-
-    private OutputStream verStream;
-
-    /**
-     * Constructs a new JarInputStream from stream
-     */
-    public JarInputStream(InputStream stream, boolean verify)
-            throws IOException {
-        super(stream);
-        if (verify) {
-            verifier = new JarVerifier("JarInputStream");
-        }
-        if ((mEntry = getNextJarEntry()) == null) {
-            return;
-        }
-        String name = mEntry.getName().toUpperCase();
-        if (name.equals(JarFile.META_DIR)) {
-            mEntry = null; // modifies behavior of getNextJarEntry()
-            closeEntry();
-            mEntry = getNextJarEntry();
-            name = mEntry.getName().toUpperCase();
-        }
-        if (name.equals(JarFile.MANIFEST_NAME)) {
-            mEntry = null;
-            manifest = new Manifest(this, verify);
-            closeEntry();
-            if (verify) {
-                verifier.setManifest(manifest);
-                if (manifest != null) {
-                    verifier.mainAttributesChunk = manifest
-                            .getMainAttributesChunk();
-                }
-            }
-
-        } else {
-            Attributes temp = new Attributes(3);
-            temp.map.put("hidden", null);
-            mEntry.setAttributes(temp);
-            /*
-             * if not from the first entry, we will not get enough
-             * information,so no verify will be taken out.
-             */
-            verifier = null;
-        }
-    }
-
-    public JarInputStream(InputStream stream) throws IOException {
-        this(stream, true);
-    }
-
-    /**
-     * Returns the Manifest object associated with this JarInputStream or null
-     * if no manifest entry exists.
-     * 
-     * @return java.util.jar.Manifest
-     */
-    public Manifest getManifest() {
-        return manifest;
-    }
-
-    /**
-     * Returns the next JarEntry contained in this stream or null if no more
-     * entries are present.
-     * 
-     * @return java.util.jar.JarEntry
-     * @exception java.io.IOException
-     *                If an error occurs while reading the entry
-     */
-    public JarEntry getNextJarEntry() throws IOException {
-        return (JarEntry) getNextEntry();
-    }
-
-    public int read(byte[] buffer, int offset, int length) throws IOException {
-        if (mEntry != null) {
-            return -1;
-        }
-        int r = super.read(buffer, offset, length);
-        if (verStream != null && !eos) {
-            if (r == -1) {
-                eos = true;
-                if (verifier != null) {
-                    if (isMeta) {
-                        verifier.addMetaEntry(jarEntry.getName(),
-                                ((ByteArrayOutputStream) verStream)
-                                        .toByteArray());
-                        try {
-                            verifier.readCertificates();
-                        } catch (SecurityException e) {
-                            verifier = null;
-                            throw e;
-                        }
-                    } else {
-                        verifier.verifySignatures(
-                                (JarVerifier.VerifierEntry) this.verStream,
-                                jarEntry);
-                    }
-                }
-            } else {
-                verStream.write(buffer, offset, r);
-            }
-        }
-        return r;
-    }
-
-    /**
-     * Returns the next ZipEntry contained in this stream or null if no more
-     * entries are present.
-     * 
-     * @return java.util.zip.ZipEntry
-     * @exception java.io.IOException
-     *                If an error occurs while reading the entry
-     */
-    public ZipEntry getNextEntry() throws IOException {
-        if (mEntry != null) {
-            jarEntry = mEntry;
-            mEntry = null;
-            jarEntry.setAttributes(null);
-        } else {
-            jarEntry = (JarEntry) super.getNextEntry();
-            if (jarEntry == null) {
-                return null;
-            }
-            if (verifier != null) {
-                isMeta = jarEntry.getName().toUpperCase().startsWith(
-                        JarFile.META_DIR);
-                if (isMeta) {
-                    verStream = new ByteArrayOutputStream();
-                } else {
-                    verStream = verifier.initEntry(jarEntry.getName());
-                }
-            }
-        }
-        eos = false;
-        return jarEntry;
-    }
-
-    protected ZipEntry createZipEntry(String name) {
-        JarEntry entry = new JarEntry(name);
-        if (manifest != null) {
-            entry.setAttributes(manifest.getAttributes(name));
-        }
-        return entry;
-    }
-}
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util.jar;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+
+public class JarInputStream extends ZipInputStream {
+
+    private Manifest manifest;
+
+    private boolean eos = false;
+
+    private JarEntry mEntry;
+
+    private JarEntry jarEntry;
+
+    private boolean isMeta;
+
+    private JarVerifier verifier;
+
+    private OutputStream verStream;
+
+    /**
+     * Constructs a new JarInputStream from stream
+     */
+    public JarInputStream(InputStream stream, boolean verify)
+            throws IOException {
+        super(stream);
+        if (verify) {
+            verifier = new JarVerifier("JarInputStream");
+        }
+        if ((mEntry = getNextJarEntry()) == null) {
+            return;
+        }
+        String name = mEntry.getName().toUpperCase();
+        if (name.equals(JarFile.META_DIR)) {
+            mEntry = null; // modifies behavior of getNextJarEntry()
+            closeEntry();
+            mEntry = getNextJarEntry();
+            name = mEntry.getName().toUpperCase();
+        }
+        if (name.equals(JarFile.MANIFEST_NAME)) {
+            mEntry = null;
+            manifest = new Manifest(this, verify);
+            closeEntry();
+            if (verify) {
+                verifier.setManifest(manifest);
+                if (manifest != null) {
+                    verifier.mainAttributesChunk = manifest
+                            .getMainAttributesChunk();
+                }
+            }
+
+        } else {
+            Attributes temp = new Attributes(3);
+            temp.map.put("hidden", null);
+            mEntry.setAttributes(temp);
+            /*
+             * if not from the first entry, we will not get enough
+             * information,so no verify will be taken out.
+             */
+            verifier = null;
+        }
+    }
+
+    public JarInputStream(InputStream stream) throws IOException {
+        this(stream, true);
+    }
+
+    /**
+     * Returns the Manifest object associated with this JarInputStream or null
+     * if no manifest entry exists.
+     * 
+     * @return java.util.jar.Manifest
+     */
+    public Manifest getManifest() {
+        return manifest;
+    }
+
+    /**
+     * Returns the next JarEntry contained in this stream or null if no more
+     * entries are present.
+     * 
+     * @return java.util.jar.JarEntry
+     * @exception java.io.IOException
+     *                If an error occurs while reading the entry
+     */
+    public JarEntry getNextJarEntry() throws IOException {
+        return (JarEntry) getNextEntry();
+    }
+
+    public int read(byte[] buffer, int offset, int length) throws IOException {
+        if (mEntry != null) {
+            return -1;
+        }
+        int r = super.read(buffer, offset, length);
+        if (verStream != null && !eos) {
+            if (r == -1) {
+                eos = true;
+                if (verifier != null) {
+                    if (isMeta) {
+                        verifier.addMetaEntry(jarEntry.getName(),
+                                ((ByteArrayOutputStream) verStream)
+                                        .toByteArray());
+                        try {
+                            verifier.readCertificates();
+                        } catch (SecurityException e) {
+                            verifier = null;
+                            throw e;
+                        }
+                    } else {
+                        verifier.verifySignatures(
+                                (JarVerifier.VerifierEntry) this.verStream,
+                                jarEntry);
+                    }
+                }
+            } else {
+                verStream.write(buffer, offset, r);
+            }
+        }
+        return r;
+    }
+
+    /**
+     * Returns the next ZipEntry contained in this stream or null if no more
+     * entries are present.
+     * 
+     * @return java.util.zip.ZipEntry
+     * @exception java.io.IOException
+     *                If an error occurs while reading the entry
+     */
+    public ZipEntry getNextEntry() throws IOException {
+        if (mEntry != null) {
+            jarEntry = mEntry;
+            mEntry = null;
+            jarEntry.setAttributes(null);
+        } else {
+            jarEntry = (JarEntry) super.getNextEntry();
+            if (jarEntry == null) {
+                return null;
+            }
+            if (verifier != null) {
+                isMeta = jarEntry.getName().toUpperCase().startsWith(
+                        JarFile.META_DIR);
+                if (isMeta) {
+                    verStream = new ByteArrayOutputStream();
+                } else {
+                    verStream = verifier.initEntry(jarEntry.getName());
+                }
+            }
+        }
+        eos = false;
+        return jarEntry;
+    }
+
+    protected ZipEntry createZipEntry(String name) {
+        JarEntry entry = new JarEntry(name);
+        if (manifest != null) {
+            entry.setAttributes(manifest.getAttributes(name));
+        }
+        return entry;
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarInputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarOutputStream.java?view=diff&rev=446478&r1=446477&r2=446478
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarOutputStream.java Thu Sep 14 19:40:33 2006
@@ -1,79 +1,79 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.util.jar;
-
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.zip.ZipEntry;
-import java.util.zip.ZipOutputStream;
-
-/**
- * The JarOutputStream is used to output data in JarFile format.
- */
-public class JarOutputStream extends ZipOutputStream {
-
-    private Manifest manifest;
-
-    /**
-     * Contructs a new JarOuputStream using os as the underlying stream.
-     * Manifest information for the JarFile to be written is obtained from the
-     * parameter Manifest, mf.
-     * 
-     * @param os
-     *            The OutputStream to write to
-     * @param mf
-     *            The Manifest to output for this Jar.
-     * @exception IOException
-     *                If an error occurs creating the JarOutputStream
-     */
-    public JarOutputStream(OutputStream os, Manifest mf) throws IOException {
-        super(os);
-        if (mf == null) {
-            throw new NullPointerException();
-        }
-        manifest = mf;
-        ZipEntry ze = new ZipEntry(JarFile.MANIFEST_NAME);
-        putNextEntry(ze);
-        manifest.write(this);
-        closeEntry();
-    }
-
-    /**
-     * Contructs a new JarOuputStream using os as the underlying stream.
-     * 
-     * @param os
-     *            The OutputStream to write to
-     * @exception IOException
-     *                If an error occurs creating the JarOutputStream
-     */
-    public JarOutputStream(OutputStream os) throws IOException {
-        super(os);
-    }
-
-    /**
-     * Writes the specified entry to the underlying stream. The previous entry
-     * is closed if it is still open.
-     * 
-     * 
-     * @param ze
-     *            The ZipEntry to write
-     * @exception IOException
-     *                If an error occurs writing the entry
-     */
-    public void putNextEntry(ZipEntry ze) throws IOException {
-        super.putNextEntry(ze);
-    }
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util.jar;
+
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * The JarOutputStream is used to output data in JarFile format.
+ */
+public class JarOutputStream extends ZipOutputStream {
+
+    private Manifest manifest;
+
+    /**
+     * Contructs a new JarOuputStream using os as the underlying stream.
+     * Manifest information for the JarFile to be written is obtained from the
+     * parameter Manifest, mf.
+     * 
+     * @param os
+     *            The OutputStream to write to
+     * @param mf
+     *            The Manifest to output for this Jar.
+     * @exception IOException
+     *                If an error occurs creating the JarOutputStream
+     */
+    public JarOutputStream(OutputStream os, Manifest mf) throws IOException {
+        super(os);
+        if (mf == null) {
+            throw new NullPointerException();
+        }
+        manifest = mf;
+        ZipEntry ze = new ZipEntry(JarFile.MANIFEST_NAME);
+        putNextEntry(ze);
+        manifest.write(this);
+        closeEntry();
+    }
+
+    /**
+     * Contructs a new JarOuputStream using os as the underlying stream.
+     * 
+     * @param os
+     *            The OutputStream to write to
+     * @exception IOException
+     *                If an error occurs creating the JarOutputStream
+     */
+    public JarOutputStream(OutputStream os) throws IOException {
+        super(os);
+    }
+
+    /**
+     * Writes the specified entry to the underlying stream. The previous entry
+     * is closed if it is still open.
+     * 
+     * 
+     * @param ze
+     *            The ZipEntry to write
+     * @exception IOException
+     *                If an error occurs writing the entry
+     */
+    public void putNextEntry(ZipEntry ze) throws IOException {
+        super.putNextEntry(ze);
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarOutputStream.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java?view=diff&rev=446478&r1=446477&r2=446478
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java Thu Sep 14 19:40:33 2006
@@ -1,487 +1,487 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- * 
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * 
- *     http://www.apache.org/licenses/LICENSE-2.0
- * 
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.util.jar;
-
-import java.io.ByteArrayInputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.security.GeneralSecurityException;
-import java.security.MessageDigest;
-import java.security.NoSuchAlgorithmException;
-import java.security.cert.Certificate;
-import java.util.HashMap;
-import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.StringTokenizer;
-import java.util.Vector;
-import java.util.zip.ZipEntry;
-
-import org.apache.harmony.luni.util.Base64;
-import org.apache.harmony.luni.util.Msg;
-import org.apache.harmony.security.utils.JarUtils;
-
-/**
- * Non-public class used by {@link JarFile} and
- * {@link JarInputStream} to manage the verification of signed
- * jars. <code>JarFile</code> and <code>JarInputStream</code> objects will
- * be expected to have a <code>JarVerifier</code> instance member which can be
- * used to carry out the tasks associated with verifying a signed jar. These
- * tasks would typically include:
- * <ul>
- * <li>verification of all signed signature files
- * <li>confirmation that all signed data was signed only by the party or
- * parties specified in the signature block data
- * <li>verification that the contents of all signature files (i.e.
- * <code>.SF</code> files) agree with the jar entries information found in the
- * jar manifest.
- * </ul>
- */
-class JarVerifier {
-
-    private String jarName;
-
-    private Manifest man;
-
-    private HashMap<String, byte[]> metaEntries = new HashMap<String, byte[]>(5);
-
-    private Hashtable<String, HashMap<String, Attributes>> signatures =
-        new Hashtable<String, HashMap<String, Attributes>>(5);
-
-    private Hashtable<String, Certificate[]> certificates =
-        new Hashtable<String, Certificate[]>(5);
-
-    private Hashtable<String, Certificate[]> verifiedEntries =
-        new Hashtable<String, Certificate[]>();
-
-    byte[] mainAttributesChunk;
-
-    /**
-     * TODO Type description
-     */
-    static class VerifierEntry extends OutputStream {
-
-        MessageDigest digest;
-
-        byte[] hash;
-
-        Certificate[] certificates;
-
-        VerifierEntry(MessageDigest digest, byte[] hash,
-                Certificate[] certificates) {
-            this.digest = digest;
-            this.hash = hash;
-            this.certificates = certificates;
-        }
-
-        /*
-         * (non-Javadoc)
-         * 
-         * @see java.io.OutputStream#write(int)
-         */
-        public void write(int value) {
-            digest.update((byte) value);
-        }
-
-        /*
-         * (non-Javadoc)
-         * 
-         * @see java.io.OutputStream#write(byte[], int, int)
-         */
-        public void write(byte[] buf, int off, int nbytes) {
-            digest.update(buf, off, nbytes);
-        }
-    }
-
-    /**
-     * Constructs and answers with a new instance of JarVerifier.
-     * 
-     * @param name
-     *            the name of the jar file being verified.
-     */
-    JarVerifier(String name) {
-        jarName = name;
-    }
-
-    /**
-     * Called for each new jar entry read in from the input stream. This method
-     * constructs and returns a new {@link VerifierEntry} which contains the
-     * certificates used to sign the entry and its hash value as specified in
-     * the jar manifest.
-     * 
-     * @param name
-     *            the name of an entry in a jar file which is <b>not</b> in the
-     *            <code>META-INF</code> directory.
-     * @return a new instance of {@link VerifierEntry} which can be used by
-     *         callers as an {@link OutputStream}.
-     */
-    VerifierEntry initEntry(String name) {
-        // If no manifest is present by the time an entry is found,
-        // verification cannot occur. If no signature files have
-        // been found, do not verify.
-        if (man == null || signatures.size() == 0) {
-            return null;
-        }
-
-        Attributes attributes = man.getAttributes(name);
-        // entry has no digest
-        if (attributes == null) {
-            return null;
-        }
-
-        Vector<Certificate> certs = new Vector<Certificate>();
-        Iterator<Map.Entry<String, HashMap<String, Attributes>>> it =
-            signatures.entrySet().iterator();
-        while (it.hasNext()) {
-            Map.Entry<String, HashMap<String, Attributes>> entry = it.next();
-            HashMap<String, Attributes> hm = entry.getValue();
-            if (hm.get(name) != null) {
-                // Found an entry for entry name in .SF file
-                String signatureFile = entry.getKey();
-
-                Vector<Certificate> newCerts = getSignerCertificates(
-                        signatureFile, certificates);
-                Iterator<Certificate> iter = newCerts.iterator();
-                while (iter.hasNext()) {
-                    certs.add(iter.next());
-                }
-            }
-        }
-
-        // entry is not signed
-        if (certs.size() == 0) {
-            return null;
-        }
-        Certificate[] certificatesArray = new Certificate[certs.size()];
-        certs.toArray(certificatesArray);
-
-        String algorithms = attributes.getValue("Digest-Algorithms");
-        if (algorithms == null) {
-            algorithms = "SHA SHA1";
-        }
-        StringTokenizer tokens = new StringTokenizer(algorithms);
-        while (tokens.hasMoreTokens()) {
-            String algorithm = tokens.nextToken();
-            String hash = attributes.getValue(algorithm + "-Digest");
-            if (hash == null) {
-                continue;
-            }
-            byte[] hashBytes;
-            try {
-                hashBytes = hash.getBytes("ISO8859_1");
-            } catch (UnsupportedEncodingException e) {
-                throw new RuntimeException(e.toString());
-            }
-
-            try {
-                return new VerifierEntry(MessageDigest.getInstance(algorithm),
-                        hashBytes, certificatesArray);
-            } catch (NoSuchAlgorithmException e) {
-                // Ignored
-            }
-        }
-        return null;
-    }
-
-    /**
-     * Add a new meta entry to the internal collection of data held on each jar
-     * entry in the <code>META-INF</code> directory including the manifest
-     * file itself. Files associated with the signing of a jar would also be
-     * added to this collection.
-     * 
-     * @param name
-     *            the name of the file located in the <code>META-INF</code>
-     *            directory.
-     * @param buf
-     *            the file bytes for the file called <code>name</code>.
-     * @see #removeMetaEntries()
-     */
-    void addMetaEntry(String name, byte[] buf) {
-        metaEntries.put(name.toUpperCase(), buf);
-    }
-
-    /**
-     * If the associated jar file is signed, check on the validity of all of the
-     * known signatures.
-     * 
-     * @return <code>true</code> if the associated jar is signed and an
-     *         internal check verifies the validity of the signature(s).
-     *         <code>false</code> if the associated jar file has no entries at
-     *         all in its <code>META-INF</code> directory. This situation is
-     *         indicative of an invalid jar file.
-     *         <p>
-     *         Will also return true if the jar file is <i>not</i> signed.
-     *         </p>
-     * @throws SecurityException
-     *             if the jar file is signed and it is determined that a
-     *             signature block file contains an invalid signature for the
-     *             corresponding signature file.
-     */
-    synchronized boolean readCertificates() {
-        if (metaEntries == null) {
-            return false;
-        }
-        Iterator<String> it = metaEntries.keySet().iterator();
-        while (it.hasNext()) {
-            String key = it.next();
-            if (key.endsWith(".DSA") || key.endsWith(".RSA")) {
-                verifyCertificate(key);
-                // Check for recursive class load
-                if (metaEntries == null) {
-                    return false;
-                }
-                it.remove();
-            }
-        }
-        return true;
-    }
-
-    /**
-     * @param certFile
-     */
-    private void verifyCertificate(String certFile) {
-        // Found Digital Sig, .SF should already have been read
-        String signatureFile = certFile.substring(0, certFile.lastIndexOf('.'))
-                + ".SF";
-        byte[] sfBytes = metaEntries.get(signatureFile);
-        if (sfBytes == null) {
-            return;
-        }
-
-        byte[] sBlockBytes = metaEntries.get(certFile);
-        try {
-            Certificate[] signerCertChain = JarUtils.verifySignature(
-                    new ByteArrayInputStream(sfBytes),
-                    new ByteArrayInputStream(sBlockBytes));
-            if (signerCertChain != null) {
-                this.certificates.put(signatureFile, signerCertChain);
-            }
-        } catch (IOException e) {
-            return;
-        } catch (GeneralSecurityException e) {
-            /* [MSG "K00eb", "{0} failed verification of {1}"] */
-            throw new SecurityException(
-                    Msg.getString("K00eb", jarName, signatureFile));
-        }
-
-        // Verify manifest hash in .sf file
-        Attributes attributes = new Attributes();
-        HashMap<String, Attributes> hm = new HashMap<String, Attributes>();
-        try {
-            new InitManifest(new ByteArrayInputStream(sfBytes), attributes, hm,
-                    null, "Signature-Version");
-        } catch (IOException e) {
-            return;
-        }
-
-        boolean createdBySigntool = false;
-        String createdByValue = attributes.getValue("Created-By");
-        if (createdByValue != null) {
-            createdBySigntool = createdByValue.indexOf("signtool") != -1;
-        }
-
-        // Use .SF to verify the mainAttributes of the manifest
-        // If there is no -Digest-Manifest-Main-Attributes entry in .SF
-        // file, such as those created before java 1.5, then we ignore
-        // such verification.
-        // FIXME: The meaning of createdBySigntool
-        if (this.mainAttributesChunk != null && !createdBySigntool) {
-            String digestAttribute = "-Digest-Manifest-Main-Attributes";
-            if (!verify(attributes, digestAttribute, this.mainAttributesChunk,
-                    false, true)) {
-                /* [MSG "K00eb", "{0} failed verification of {1}"] */
-                throw new SecurityException(
-                    Msg.getString("K00eb", jarName, signatureFile));
-            }
-        }
-
-        byte[] manifest = metaEntries.get(JarFile.MANIFEST_NAME);
-        if (manifest == null) {
-            return;
-        }
-        // Use .SF to verify the whole manifest
-        String digestAttribute = createdBySigntool ? "-Digest"
-                : "-Digest-Manifest";
-        if (!verify(attributes, digestAttribute, manifest, false, false)) {
-            Iterator<Map.Entry<String, Attributes>> it = hm.entrySet()
-                    .iterator();
-            while (it.hasNext()) {
-                Map.Entry<String, Attributes> entry = it.next();
-                byte[] chunk = man.getChunk(entry.getKey());
-                if (chunk == null) {
-                    return;
-                }
-                if (!verify(entry.getValue(), "-Digest", chunk,
-                        createdBySigntool, false)) {
-                    /* [MSG "K00ec", "{0} has invalid digest for {1} in {2}"] */
-                    throw new SecurityException(
-                        Msg.getString("K00ec",
-                            new Object[] { signatureFile, entry.getKey(), jarName }));
-                }
-            }
-        }
-        metaEntries.put(signatureFile, null);
-        signatures.put(signatureFile, hm);
-    }
-
-    /**
-     * Associate this verifier with the specified {@link Manifest} object.
-     * 
-     * @param mf
-     *            a <code>java.util.jar.Manifest</code> object.
-     */
-    void setManifest(Manifest mf) {
-        man = mf;
-    }
-
-    /**
-     * Verifies that the digests stored in the manifest match the decrypted
-     * digests from the .SF file. This indicates the validity of the signing,
-     * not the integrity of the file, as it's digest must be calculated and
-     * verified when its contents are read.
-     * 
-     * @param entry
-     *            the {@link VerifierEntry} associated with the specified
-     *            <code>zipEntry</code>.
-     * @param zipEntry
-     *            an entry in the jar file
-     * @throws SecurityException
-     *             if the digest value stored in the manifest does <i>not</i>
-     *             agree with the decrypted digest as recovered from the
-     *             <code>.SF</code> file.
-     * @see #initEntry(String)
-     */
-    void verifySignatures(VerifierEntry entry, ZipEntry zipEntry) {
-        byte[] digest = entry.digest.digest();
-        if (!MessageDigest.isEqual(digest, Base64.decode(entry.hash))) {
-            /* [MSG "K00ec", "{0} has invalid digest for {1} in {2}"] */
-            throw new SecurityException(Msg.getString("K00ec", new Object[] {
-                    JarFile.MANIFEST_NAME, zipEntry.getName(), jarName }));
-        }
-        verifiedEntries.put(zipEntry.getName(), entry.certificates);
-        if (zipEntry instanceof JarEntry) {
-            ((JarEntry) zipEntry).certificates = entry.certificates.clone();
-        }
-    }
-
-    /**
-     * Returns a <code>boolean</code> indication of whether or not the
-     * associated jar file is signed.
-     * 
-     * @return <code>true</code> if the jar is signed, <code>false</code>
-     *         otherwise.
-     */
-    boolean isSignedJar() {
-        return certificates.size() > 0;
-    }
-
-    private boolean verify(Attributes attributes, String entry, byte[] data,
-            boolean ignoreSecondEndline, boolean ignorable) {
-        String algorithms = attributes.getValue("Digest-Algorithms");
-        if (algorithms == null) {
-            algorithms = "SHA SHA1";
-        }
-        StringTokenizer tokens = new StringTokenizer(algorithms);
-        while (tokens.hasMoreTokens()) {
-            String algorithm = tokens.nextToken();
-            String hash = attributes.getValue(algorithm + entry);
-            if (hash == null) {
-                continue;
-            }
-
-            MessageDigest md;
-            try {
-                md = MessageDigest.getInstance(algorithm);
-            } catch (NoSuchAlgorithmException e) {
-                continue;
-            }
-            if (ignoreSecondEndline && data[data.length - 1] == '\n'
-                    && data[data.length - 2] == '\n') {
-                md.update(data, 0, data.length - 1);
-            } else {
-                md.update(data, 0, data.length);
-            }
-            byte[] b = md.digest();
-            byte[] hashBytes;
-            try {
-                hashBytes = hash.getBytes("ISO8859_1");
-            } catch (UnsupportedEncodingException e) {
-                throw new RuntimeException(e.toString());
-            }
-            return MessageDigest.isEqual(b, Base64.decode(hashBytes));
-        }
-        return ignorable;
-    }
-
-    /**
-     * Returns all of the {@link java.security.cert.Certificate} instances that
-     * were used to verify the signature on the jar entry called
-     * <code>name</code>.
-     * 
-     * @param name
-     *            the name of a jar entry.
-     * @return an array of {@link java.security.cert.Certificate}.
-     */
-    Certificate[] getCertificates(String name) {
-        Certificate[] verifiedCerts = verifiedEntries.get(name);
-        if (verifiedCerts == null) {
-            return null;
-        }
-        return verifiedCerts.clone();
-    }
-
-    /**
-     * Remove all entries from the internal collection of data held about each
-     * jar entry in the <code>META-INF</code> directory.
-     * 
-     * @see #addMetaEntry(String, byte[])
-     */
-    void removeMetaEntries() {
-        metaEntries = null;
-    }
-
-    /**
-     * Returns a <code>Vector</code> of all of the
-     * {@link java.security.cert.Certificate}s that are associated with the
-     * signing of the named signature file.
-     * 
-     * @param signatureFileName
-     *            the name of a signature file
-     * @param certificates
-     *            a <code>Map</code> of all of the certificate chains
-     *            discovered so far while attempting to verify the jar that
-     *            contains the signature file <code>signatureFileName</code>.
-     *            This object will have been previously set in the course of one
-     *            or more calls to
-     *            {@link #verifyJarSignatureFile(String, String, String, Map, Map)}
-     *            where it was passed in as the last argument.
-     * @return all of the <code>Certificate</code> entries for the signer of
-     *         the jar whose actions led to the creation of the named signature
-     *         file.
-     */
-    public static Vector<Certificate> getSignerCertificates(
-            String signatureFileName, Map<String, Certificate[]> certificates) {
-        Vector<Certificate> result = new Vector<Certificate>();
-        Certificate[] certChain = certificates.get(signatureFileName);
-        if (certChain != null) {
-            for (Certificate element : certChain) {
-                result.add(element);
-            }
-        }
-        return result;
-    }
-}
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.util.jar;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.GeneralSecurityException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.Certificate;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.Vector;
+import java.util.zip.ZipEntry;
+
+import org.apache.harmony.luni.util.Base64;
+import org.apache.harmony.luni.util.Msg;
+import org.apache.harmony.security.utils.JarUtils;
+
+/**
+ * Non-public class used by {@link JarFile} and
+ * {@link JarInputStream} to manage the verification of signed
+ * jars. <code>JarFile</code> and <code>JarInputStream</code> objects will
+ * be expected to have a <code>JarVerifier</code> instance member which can be
+ * used to carry out the tasks associated with verifying a signed jar. These
+ * tasks would typically include:
+ * <ul>
+ * <li>verification of all signed signature files
+ * <li>confirmation that all signed data was signed only by the party or
+ * parties specified in the signature block data
+ * <li>verification that the contents of all signature files (i.e.
+ * <code>.SF</code> files) agree with the jar entries information found in the
+ * jar manifest.
+ * </ul>
+ */
+class JarVerifier {
+
+    private String jarName;
+
+    private Manifest man;
+
+    private HashMap<String, byte[]> metaEntries = new HashMap<String, byte[]>(5);
+
+    private Hashtable<String, HashMap<String, Attributes>> signatures =
+        new Hashtable<String, HashMap<String, Attributes>>(5);
+
+    private Hashtable<String, Certificate[]> certificates =
+        new Hashtable<String, Certificate[]>(5);
+
+    private Hashtable<String, Certificate[]> verifiedEntries =
+        new Hashtable<String, Certificate[]>();
+
+    byte[] mainAttributesChunk;
+
+    /**
+     * TODO Type description
+     */
+    static class VerifierEntry extends OutputStream {
+
+        MessageDigest digest;
+
+        byte[] hash;
+
+        Certificate[] certificates;
+
+        VerifierEntry(MessageDigest digest, byte[] hash,
+                Certificate[] certificates) {
+            this.digest = digest;
+            this.hash = hash;
+            this.certificates = certificates;
+        }
+
+        /*
+         * (non-Javadoc)
+         * 
+         * @see java.io.OutputStream#write(int)
+         */
+        public void write(int value) {
+            digest.update((byte) value);
+        }
+
+        /*
+         * (non-Javadoc)
+         * 
+         * @see java.io.OutputStream#write(byte[], int, int)
+         */
+        public void write(byte[] buf, int off, int nbytes) {
+            digest.update(buf, off, nbytes);
+        }
+    }
+
+    /**
+     * Constructs and answers with a new instance of JarVerifier.
+     * 
+     * @param name
+     *            the name of the jar file being verified.
+     */
+    JarVerifier(String name) {
+        jarName = name;
+    }
+
+    /**
+     * Called for each new jar entry read in from the input stream. This method
+     * constructs and returns a new {@link VerifierEntry} which contains the
+     * certificates used to sign the entry and its hash value as specified in
+     * the jar manifest.
+     * 
+     * @param name
+     *            the name of an entry in a jar file which is <b>not</b> in the
+     *            <code>META-INF</code> directory.
+     * @return a new instance of {@link VerifierEntry} which can be used by
+     *         callers as an {@link OutputStream}.
+     */
+    VerifierEntry initEntry(String name) {
+        // If no manifest is present by the time an entry is found,
+        // verification cannot occur. If no signature files have
+        // been found, do not verify.
+        if (man == null || signatures.size() == 0) {
+            return null;
+        }
+
+        Attributes attributes = man.getAttributes(name);
+        // entry has no digest
+        if (attributes == null) {
+            return null;
+        }
+
+        Vector<Certificate> certs = new Vector<Certificate>();
+        Iterator<Map.Entry<String, HashMap<String, Attributes>>> it =
+            signatures.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry<String, HashMap<String, Attributes>> entry = it.next();
+            HashMap<String, Attributes> hm = entry.getValue();
+            if (hm.get(name) != null) {
+                // Found an entry for entry name in .SF file
+                String signatureFile = entry.getKey();
+
+                Vector<Certificate> newCerts = getSignerCertificates(
+                        signatureFile, certificates);
+                Iterator<Certificate> iter = newCerts.iterator();
+                while (iter.hasNext()) {
+                    certs.add(iter.next());
+                }
+            }
+        }
+
+        // entry is not signed
+        if (certs.size() == 0) {
+            return null;
+        }
+        Certificate[] certificatesArray = new Certificate[certs.size()];
+        certs.toArray(certificatesArray);
+
+        String algorithms = attributes.getValue("Digest-Algorithms");
+        if (algorithms == null) {
+            algorithms = "SHA SHA1";
+        }
+        StringTokenizer tokens = new StringTokenizer(algorithms);
+        while (tokens.hasMoreTokens()) {
+            String algorithm = tokens.nextToken();
+            String hash = attributes.getValue(algorithm + "-Digest");
+            if (hash == null) {
+                continue;
+            }
+            byte[] hashBytes;
+            try {
+                hashBytes = hash.getBytes("ISO8859_1");
+            } catch (UnsupportedEncodingException e) {
+                throw new RuntimeException(e.toString());
+            }
+
+            try {
+                return new VerifierEntry(MessageDigest.getInstance(algorithm),
+                        hashBytes, certificatesArray);
+            } catch (NoSuchAlgorithmException e) {
+                // Ignored
+            }
+        }
+        return null;
+    }
+
+    /**
+     * Add a new meta entry to the internal collection of data held on each jar
+     * entry in the <code>META-INF</code> directory including the manifest
+     * file itself. Files associated with the signing of a jar would also be
+     * added to this collection.
+     * 
+     * @param name
+     *            the name of the file located in the <code>META-INF</code>
+     *            directory.
+     * @param buf
+     *            the file bytes for the file called <code>name</code>.
+     * @see #removeMetaEntries()
+     */
+    void addMetaEntry(String name, byte[] buf) {
+        metaEntries.put(name.toUpperCase(), buf);
+    }
+
+    /**
+     * If the associated jar file is signed, check on the validity of all of the
+     * known signatures.
+     * 
+     * @return <code>true</code> if the associated jar is signed and an
+     *         internal check verifies the validity of the signature(s).
+     *         <code>false</code> if the associated jar file has no entries at
+     *         all in its <code>META-INF</code> directory. This situation is
+     *         indicative of an invalid jar file.
+     *         <p>
+     *         Will also return true if the jar file is <i>not</i> signed.
+     *         </p>
+     * @throws SecurityException
+     *             if the jar file is signed and it is determined that a
+     *             signature block file contains an invalid signature for the
+     *             corresponding signature file.
+     */
+    synchronized boolean readCertificates() {
+        if (metaEntries == null) {
+            return false;
+        }
+        Iterator<String> it = metaEntries.keySet().iterator();
+        while (it.hasNext()) {
+            String key = it.next();
+            if (key.endsWith(".DSA") || key.endsWith(".RSA")) {
+                verifyCertificate(key);
+                // Check for recursive class load
+                if (metaEntries == null) {
+                    return false;
+                }
+                it.remove();
+            }
+        }
+        return true;
+    }
+
+    /**
+     * @param certFile
+     */
+    private void verifyCertificate(String certFile) {
+        // Found Digital Sig, .SF should already have been read
+        String signatureFile = certFile.substring(0, certFile.lastIndexOf('.'))
+                + ".SF";
+        byte[] sfBytes = metaEntries.get(signatureFile);
+        if (sfBytes == null) {
+            return;
+        }
+
+        byte[] sBlockBytes = metaEntries.get(certFile);
+        try {
+            Certificate[] signerCertChain = JarUtils.verifySignature(
+                    new ByteArrayInputStream(sfBytes),
+                    new ByteArrayInputStream(sBlockBytes));
+            if (signerCertChain != null) {
+                this.certificates.put(signatureFile, signerCertChain);
+            }
+        } catch (IOException e) {
+            return;
+        } catch (GeneralSecurityException e) {
+            /* [MSG "K00eb", "{0} failed verification of {1}"] */
+            throw new SecurityException(
+                    Msg.getString("K00eb", jarName, signatureFile));
+        }
+
+        // Verify manifest hash in .sf file
+        Attributes attributes = new Attributes();
+        HashMap<String, Attributes> hm = new HashMap<String, Attributes>();
+        try {
+            new InitManifest(new ByteArrayInputStream(sfBytes), attributes, hm,
+                    null, "Signature-Version");
+        } catch (IOException e) {
+            return;
+        }
+
+        boolean createdBySigntool = false;
+        String createdByValue = attributes.getValue("Created-By");
+        if (createdByValue != null) {
+            createdBySigntool = createdByValue.indexOf("signtool") != -1;
+        }
+
+        // Use .SF to verify the mainAttributes of the manifest
+        // If there is no -Digest-Manifest-Main-Attributes entry in .SF
+        // file, such as those created before java 1.5, then we ignore
+        // such verification.
+        // FIXME: The meaning of createdBySigntool
+        if (this.mainAttributesChunk != null && !createdBySigntool) {
+            String digestAttribute = "-Digest-Manifest-Main-Attributes";
+            if (!verify(attributes, digestAttribute, this.mainAttributesChunk,
+                    false, true)) {
+                /* [MSG "K00eb", "{0} failed verification of {1}"] */
+                throw new SecurityException(
+                    Msg.getString("K00eb", jarName, signatureFile));
+            }
+        }
+
+        byte[] manifest = metaEntries.get(JarFile.MANIFEST_NAME);
+        if (manifest == null) {
+            return;
+        }
+        // Use .SF to verify the whole manifest
+        String digestAttribute = createdBySigntool ? "-Digest"
+                : "-Digest-Manifest";
+        if (!verify(attributes, digestAttribute, manifest, false, false)) {
+            Iterator<Map.Entry<String, Attributes>> it = hm.entrySet()
+                    .iterator();
+            while (it.hasNext()) {
+                Map.Entry<String, Attributes> entry = it.next();
+                byte[] chunk = man.getChunk(entry.getKey());
+                if (chunk == null) {
+                    return;
+                }
+                if (!verify(entry.getValue(), "-Digest", chunk,
+                        createdBySigntool, false)) {
+                    /* [MSG "K00ec", "{0} has invalid digest for {1} in {2}"] */
+                    throw new SecurityException(
+                        Msg.getString("K00ec",
+                            new Object[] { signatureFile, entry.getKey(), jarName }));
+                }
+            }
+        }
+        metaEntries.put(signatureFile, null);
+        signatures.put(signatureFile, hm);
+    }
+
+    /**
+     * Associate this verifier with the specified {@link Manifest} object.
+     * 
+     * @param mf
+     *            a <code>java.util.jar.Manifest</code> object.
+     */
+    void setManifest(Manifest mf) {
+        man = mf;
+    }
+
+    /**
+     * Verifies that the digests stored in the manifest match the decrypted
+     * digests from the .SF file. This indicates the validity of the signing,
+     * not the integrity of the file, as it's digest must be calculated and
+     * verified when its contents are read.
+     * 
+     * @param entry
+     *            the {@link VerifierEntry} associated with the specified
+     *            <code>zipEntry</code>.
+     * @param zipEntry
+     *            an entry in the jar file
+     * @throws SecurityException
+     *             if the digest value stored in the manifest does <i>not</i>
+     *             agree with the decrypted digest as recovered from the
+     *             <code>.SF</code> file.
+     * @see #initEntry(String)
+     */
+    void verifySignatures(VerifierEntry entry, ZipEntry zipEntry) {
+        byte[] digest = entry.digest.digest();
+        if (!MessageDigest.isEqual(digest, Base64.decode(entry.hash))) {
+            /* [MSG "K00ec", "{0} has invalid digest for {1} in {2}"] */
+            throw new SecurityException(Msg.getString("K00ec", new Object[] {
+                    JarFile.MANIFEST_NAME, zipEntry.getName(), jarName }));
+        }
+        verifiedEntries.put(zipEntry.getName(), entry.certificates);
+        if (zipEntry instanceof JarEntry) {
+            ((JarEntry) zipEntry).certificates = entry.certificates.clone();
+        }
+    }
+
+    /**
+     * Returns a <code>boolean</code> indication of whether or not the
+     * associated jar file is signed.
+     * 
+     * @return <code>true</code> if the jar is signed, <code>false</code>
+     *         otherwise.
+     */
+    boolean isSignedJar() {
+        return certificates.size() > 0;
+    }
+
+    private boolean verify(Attributes attributes, String entry, byte[] data,
+            boolean ignoreSecondEndline, boolean ignorable) {
+        String algorithms = attributes.getValue("Digest-Algorithms");
+        if (algorithms == null) {
+            algorithms = "SHA SHA1";
+        }
+        StringTokenizer tokens = new StringTokenizer(algorithms);
+        while (tokens.hasMoreTokens()) {
+            String algorithm = tokens.nextToken();
+            String hash = attributes.getValue(algorithm + entry);
+            if (hash == null) {
+                continue;
+            }
+
+            MessageDigest md;
+            try {
+                md = MessageDigest.getInstance(algorithm);
+            } catch (NoSuchAlgorithmException e) {
+                continue;
+            }
+            if (ignoreSecondEndline && data[data.length - 1] == '\n'
+                    && data[data.length - 2] == '\n') {
+                md.update(data, 0, data.length - 1);
+            } else {
+                md.update(data, 0, data.length);
+            }
+            byte[] b = md.digest();
+            byte[] hashBytes;
+            try {
+                hashBytes = hash.getBytes("ISO8859_1");
+            } catch (UnsupportedEncodingException e) {
+                throw new RuntimeException(e.toString());
+            }
+            return MessageDigest.isEqual(b, Base64.decode(hashBytes));
+        }
+        return ignorable;
+    }
+
+    /**
+     * Returns all of the {@link java.security.cert.Certificate} instances that
+     * were used to verify the signature on the jar entry called
+     * <code>name</code>.
+     * 
+     * @param name
+     *            the name of a jar entry.
+     * @return an array of {@link java.security.cert.Certificate}.
+     */
+    Certificate[] getCertificates(String name) {
+        Certificate[] verifiedCerts = verifiedEntries.get(name);
+        if (verifiedCerts == null) {
+            return null;
+        }
+        return verifiedCerts.clone();
+    }
+
+    /**
+     * Remove all entries from the internal collection of data held about each
+     * jar entry in the <code>META-INF</code> directory.
+     * 
+     * @see #addMetaEntry(String, byte[])
+     */
+    void removeMetaEntries() {
+        metaEntries = null;
+    }
+
+    /**
+     * Returns a <code>Vector</code> of all of the
+     * {@link java.security.cert.Certificate}s that are associated with the
+     * signing of the named signature file.
+     * 
+     * @param signatureFileName
+     *            the name of a signature file
+     * @param certificates
+     *            a <code>Map</code> of all of the certificate chains
+     *            discovered so far while attempting to verify the jar that
+     *            contains the signature file <code>signatureFileName</code>.
+     *            This object will have been previously set in the course of one
+     *            or more calls to
+     *            {@link #verifyJarSignatureFile(String, String, String, Map, Map)}
+     *            where it was passed in as the last argument.
+     * @return all of the <code>Certificate</code> entries for the signer of
+     *         the jar whose actions led to the creation of the named signature
+     *         file.
+     */
+    public static Vector<Certificate> getSignerCertificates(
+            String signatureFileName, Map<String, Certificate[]> certificates) {
+        Vector<Certificate> result = new Vector<Certificate>();
+        Certificate[] certChain = certificates.get(signatureFileName);
+        if (certChain != null) {
+            for (Certificate element : certChain) {
+                result.add(element);
+            }
+        }
+        return result;
+    }
+}

Propchange: incubator/harmony/enhanced/classlib/trunk/modules/archive/src/main/java/java/util/jar/JarVerifier.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message