| 1 25 package net.sf.javaguard; 26 27 import java.io.*; 28 import java.util.jar.Attributes ; 29 import java.util.jar.Manifest ; 30 import java.security.MessageDigest ; 31 import net.sf.javaguard.classfile.ClassConstants; 32 33 34 41 public class ManifestContainer { 42 43 private static final char[] base64 = { 44 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 45 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 46 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 47 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' 48 }; 49 50 private static final char pad = '='; 51 52 private static final String MANIFEST_VERSION_VALUE = "1.0"; 53 54 private static final String MANIFEST_DIGESTALG_TAG = "Digest-Algorithms"; 55 56 private static final String MANIFEST_CREATEDBY_TAG = "Created-By"; 57 58 59 private Manifest manifest = null; 60 61 62 63 64 68 public static String toBase64(byte[] b) { 69 StringBuffer sb = new StringBuffer (); 70 for (int ptr = 0; ptr < b.length; ptr += 3) { 71 sb.append(base64[(b[ptr] >> 2) & 0x3F]); 72 if (ptr + 1 < b.length) { 73 sb.append(base64[((b[ptr] << 4) & 0x30) | ((b[ptr + 1] >> 4) & 0x0F)]); 74 if (ptr + 2 < b.length) { 75 sb.append(base64[((b[ptr + 1] << 2) & 0x3C) | ((b[ptr + 2] >> 6) & 0x03)]); 76 sb.append(base64[b[ptr + 2] & 0x3F]); 77 } 78 else { 79 sb.append(base64[(b[ptr + 1] << 2) & 0x3C]); 80 sb.append(pad); 81 } 82 } else { 83 sb.append(base64[((b[ptr] << 4) & 0x30)]); 84 sb.append(pad); 85 sb.append(pad); 86 } 87 } 88 return sb.toString(); 89 } 90 91 92 93 94 96 public ManifestContainer() { 97 } 98 99 100 104 public ManifestContainer(Manifest mf) { 105 addManifest(manifest); 106 } 107 108 109 110 111 115 public void addManifest(Manifest mf) { 116 if (null != mf) { 117 try { 118 ByteArrayOutputStream os = new ByteArrayOutputStream(); 119 mf.write(os); 120 byte[] bytes = os.toByteArray(); 121 os.close(); 122 ByteArrayInputStream is = new ByteArrayInputStream(bytes); 123 getManifest().read(is); 124 is.close(); 125 } catch (IOException ioex) { 126 System.err.println("Unexpected I/O exception:" + ioex.getMessage()); 128 ioex.printStackTrace(); 129 } 130 } 131 } 132 133 134 135 136 142 public void write(OutputStream os) throws IOException { 143 String vername = Attributes.Name.MANIFEST_VERSION.toString(); 145 String version = getManifest().getMainAttributes().getValue(vername); 146 if (null == version) { 147 version = MANIFEST_VERSION_VALUE; 148 getManifest().getMainAttributes().putValue(vername, version); 149 } 150 getManifest().getMainAttributes().putValue(MANIFEST_CREATEDBY_TAG, Version.getProgramName()); 152 153 getManifest().write(os); 154 } 155 156 157 158 159 164 public void updateManifest(String oldName, String newName, MessageDigest [] digests) { 165 Attributes attrs = (Attributes ) getManifest().getAttributes(oldName); 166 if (null == attrs) { 167 attrs = new Attributes (); 168 } 169 170 if (digests != null && digests.length > 0) { 172 StringBuffer sb = new StringBuffer (); 174 for (int i = 0; i < digests.length; i++) { 175 if (i > 0) { 176 sb.append(" "); 177 } 178 sb.append(digests[i].getAlgorithm()); 179 } 180 attrs.putValue(MANIFEST_DIGESTALG_TAG, sb.toString()); 181 182 for (int i=0; i<digests.length; i++) { 184 attrs.putValue(digests[i].getAlgorithm() + "-Digest", toBase64(digests[i].digest())); 185 } 186 } 187 getManifest().getEntries().remove(oldName); 189 getManifest().getEntries().put(newName, attrs); 190 191 if (oldName.endsWith(ClassConstants.CLASS_EXT)) { 194 Attributes mainAttrs = getManifest().getMainAttributes(); 195 if (null != mainAttrs) { 196 String str = mainAttrs.getValue(Attributes.Name.MAIN_CLASS); 197 int len = ClassConstants.CLASS_EXT.length(); 198 if (null != str && str.equals(oldName.substring(0, oldName.length() - len))) { 199 mainAttrs.put(Attributes.Name.MAIN_CLASS, newName.substring(0, newName.length() - len)); 200 } 201 } 202 } 203 } 204 205 206 207 208 211 private Manifest getManifest() { 212 if (null == manifest) { 213 manifest = new Manifest (); 214 } 215 return manifest; 216 } 217 } 218 | Popular Tags |