1 /* 2 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package sun.tools.jar; 27 28 import java.io.*; 29 import java.util.*; 30 import java.security.*; 31 32 import sun.net.www.MessageHeader; 33 import java.util.Base64; 34 35 /** 36 * This is OBSOLETE. DO NOT USE THIS. Use java.util.jar.Manifest 37 * instead. It has to stay here because some apps (namely HJ and HJV) 38 * call directly into it. 39 * 40 * @author David Brown 41 * @author Benjamin Renaud 42 */ 43 44 public class Manifest { 45 46 /* list of headers that all pertain to a particular 47 * file in the archive 48 */ 49 private Vector<MessageHeader> entries = new Vector<>(); 50 private byte[] tmpbuf = new byte[512]; 51 /* a hashtable of entries, for fast lookup */ 52 private Hashtable<String, MessageHeader> tableEntries = new Hashtable<>(); 53 54 static final String[] hashes = {"SHA"}; 55 56 static final boolean debug = false; 57 static final String VERSION = "1.0"; 58 static final void debug(String s) { 59 if (debug) 60 System.out.println("man> " + s); 61 } 62 63 public Manifest() {} 64 65 public Manifest(byte[] bytes) throws IOException { 66 this(new ByteArrayInputStream(bytes), false); 67 } 68 69 public Manifest(InputStream is) throws IOException { 70 this(is, true); 71 } 72 73 /** 74 * Parse a manifest from a stream, optionally computing hashes 75 * for the files. 76 */ 77 public Manifest(InputStream is, boolean compute) throws IOException { 78 if (!is.markSupported()) { 79 is = new BufferedInputStream(is); 80 } 81 /* do not rely on available() here! */ 82 while (true) { 83 is.mark(1); 84 if (is.read() == -1) { // EOF 85 break; 86 } 87 is.reset(); 88 MessageHeader m = new MessageHeader(is); 89 if (compute) { 90 doHashes(m); 91 } 92 addEntry(m); 93 } 94 } 95 96 /* recursively generate manifests from directory tree */ 97 public Manifest(String[] files) throws IOException { 98 MessageHeader globals = new MessageHeader(); 99 globals.add("Manifest-Version", VERSION); 100 String jdkVersion = System.getProperty("java.version"); 101 globals.add("Created-By", "Manifest JDK "+jdkVersion); 102 addEntry(globals); 103 addFiles(null, files); 104 } 105 106 public void addEntry(MessageHeader entry) { 107 entries.addElement(entry); 108 String name = entry.findValue("Name"); 109 debug("addEntry for name: "+name); 110 if (name != null) { 111 tableEntries.put(name, entry); 112 } 113 } 114 115 public MessageHeader getEntry(String name) { 116 return tableEntries.get(name); 117 } 118 119 public MessageHeader entryAt(int i) { 120 return entries.elementAt(i); 121 } 122 123 public Enumeration<MessageHeader> entries() { 124 return entries.elements(); 125 } 126 127 public void addFiles(File dir, String[] files) throws IOException { 128 if (files == null) 129 return; 130 for (int i = 0; i < files.length; i++) { 131 File file; 132 if (dir == null) { 133 file = new File(files[i]); 134 } else { 135 file = new File(dir, files[i]); 136 } 137 if (file.isDirectory()) { 138 addFiles(file, file.list()); 139 } else { 140 addFile(file); 141 } 142 } 143 } 144 145 /** 146 * File names are represented internally using "/"; 147 * they are converted to the local format for anything else 148 */ 149 150 private final String stdToLocal(String name) { 151 return name.replace('/', java.io.File.separatorChar); 152 } 153 154 private final String localToStd(String name) { 155 name = name.replace(java.io.File.separatorChar, '/'); 156 if (name.startsWith("./")) 157 name = name.substring(2); 158 else if (name.startsWith("/")) 159 name = name.substring(1); 160 return name; 161 } 162 163 public void addFile(File f) throws IOException { 164 String stdName = localToStd(f.getPath()); 165 if (tableEntries.get(stdName) == null) { 166 MessageHeader mh = new MessageHeader(); 167 mh.add("Name", stdName); 168 addEntry(mh); 169 } 170 } 171 172 public void doHashes(MessageHeader mh) throws IOException { 173 // If unnamed or is a directory return immediately 174 String name = mh.findValue("Name"); 175 if (name == null || name.endsWith("/")) { 176 return; 177 } 178 179 180 /* compute hashes, write over any other "Hash-Algorithms" (?) */ 181 for (int j = 0; j < hashes.length; ++j) { 182 InputStream is = new FileInputStream(stdToLocal(name)); 183 try { 184 MessageDigest dig = MessageDigest.getInstance(hashes[j]); 185 186 int len; 187 while ((len = is.read(tmpbuf, 0, tmpbuf.length)) != -1) { 188 dig.update(tmpbuf, 0, len); 189 } 190 mh.set(hashes[j] + "-Digest", Base64.getMimeEncoder().encodeToString(dig.digest())); 191 } catch (NoSuchAlgorithmException e) { 192 throw new JarException("Digest algorithm " + hashes[j] + 193 " not available."); 194 } finally { 195 is.close(); 196 } 197 } 198 } 199 200 /* Add a manifest file at current position in a stream 201 */ 202 public void stream(OutputStream os) throws IOException { 203 204 PrintStream ps; 205 if (os instanceof PrintStream) { 206 ps = (PrintStream) os; 207 } else { 208 ps = new PrintStream(os); 209 } 210 211 /* the first header in the file should be the global one. 212 * It should say "Manifest-Version: x.x"; if not add it 213 */ 214 MessageHeader globals = entries.elementAt(0); 215 216 if (globals.findValue("Manifest-Version") == null) { 217 /* Assume this is a user-defined manifest. If it has a Name: <..> 218 * field, then it is not global, in which case we just add our own 219 * global Manifest-version: <version> 220 * If the first MessageHeader has no Name: <..>, we assume it 221 * is a global header and so prepend Manifest to it. 222 */ 223 String jdkVersion = System.getProperty("java.version"); 224 225 if (globals.findValue("Name") == null) { 226 globals.prepend("Manifest-Version", VERSION); 227 globals.add("Created-By", "Manifest JDK "+jdkVersion); 228 } else { 229 ps.print("Manifest-Version: "+VERSION+"\r\n"+ 230 "Created-By: "+jdkVersion+"\r\n\r\n"); 231 } 232 ps.flush(); 233 } 234 235 globals.print(ps); 236 237 for (int i = 1; i < entries.size(); ++i) { 238 MessageHeader mh = entries.elementAt(i); 239 mh.print(ps); 240 } 241 } 242 243 public static boolean isManifestName(String name) { 244 245 // remove leading / 246 if (name.charAt(0) == '/') { 247 name = name.substring(1, name.length()); 248 } 249 // case insensitive 250 name = name.toUpperCase(); 251 252 if (name.equals("META-INF/MANIFEST.MF")) { 253 return true; 254 } 255 return false; 256 } 257 }