author | ksrini |
Sun, 22 Apr 2012 06:54:38 -0700 | |
changeset 12544 | 5768f2e096de |
parent 10115 | eb08d08c7ef7 |
child 12857 | 0a5f341c2a28 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
12544 | 2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.java.util.jar.pack; |
|
27 |
||
6313 | 28 |
import com.sun.java.util.jar.pack.ConstantPool.ClassEntry; |
29 |
import com.sun.java.util.jar.pack.ConstantPool.DescriptorEntry; |
|
30 |
import com.sun.java.util.jar.pack.ConstantPool.LiteralEntry; |
|
31 |
import com.sun.java.util.jar.pack.ConstantPool.MemberEntry; |
|
32 |
import com.sun.java.util.jar.pack.ConstantPool.SignatureEntry; |
|
33 |
import com.sun.java.util.jar.pack.ConstantPool.Utf8Entry; |
|
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
34 |
import java.io.BufferedInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
35 |
import java.io.BufferedOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
36 |
import java.io.File; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
37 |
import java.io.FilterOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
38 |
import java.io.IOException; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
39 |
import java.io.InputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
40 |
import java.io.OutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
41 |
import java.util.Date; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
42 |
import java.util.Enumeration; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
43 |
import java.util.Map; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
44 |
import java.util.jar.JarEntry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
45 |
import java.util.jar.JarFile; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
46 |
import java.util.jar.JarInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
47 |
import java.util.jar.JarOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6900
diff
changeset
|
48 |
import java.util.zip.ZipEntry; |
4919 | 49 |
import sun.util.logging.PlatformLogger; |
50 |
||
2 | 51 |
class Utils { |
52 |
static final String COM_PREFIX = "com.sun.java.util.jar.pack."; |
|
53 |
static final String METAINF = "META-INF"; |
|
54 |
||
55 |
/* |
|
56 |
* Outputs various diagnostic support information. |
|
57 |
* If >0, print summary comments (e.g., constant pool info). |
|
58 |
* If >1, print unit comments (e.g., processing of classes). |
|
59 |
* If >2, print many comments (e.g., processing of members). |
|
60 |
* If >3, print tons of comments (e.g., processing of references). |
|
61 |
* (installer only) |
|
62 |
*/ |
|
12544 | 63 |
static final String DEBUG_VERBOSE = COM_PREFIX+"verbose"; |
2 | 64 |
|
65 |
/* |
|
66 |
* Disables use of native code, prefers the Java-coded implementation. |
|
67 |
* (installer only) |
|
68 |
*/ |
|
69 |
static final String DEBUG_DISABLE_NATIVE = COM_PREFIX+"disable.native"; |
|
70 |
||
71 |
/* |
|
72 |
* Use the default working TimeZone instead of UTC. |
|
73 |
* Note: This has installer unpacker implications. |
|
74 |
* see: zip.cpp which uses gmtime vs. localtime. |
|
75 |
*/ |
|
76 |
static final String PACK_DEFAULT_TIMEZONE = COM_PREFIX+"default.timezone"; |
|
77 |
||
78 |
/* |
|
79 |
* Property indicating that the unpacker should |
|
80 |
* ignore the transmitted PACK_MODIFICATION_TIME, |
|
81 |
* replacing it by the given value. The value can |
|
82 |
* be a numeric string, representing the number of |
|
83 |
* mSecs since the epoch (UTC), or the special string |
|
84 |
* {@link #NOW}, meaning the current time (UTC). |
|
85 |
* The default value is the special string {@link #KEEP}, |
|
86 |
* which asks the unpacker to preserve all transmitted |
|
87 |
* modification time information. |
|
88 |
* (installer only) |
|
89 |
*/ |
|
90 |
static final String UNPACK_MODIFICATION_TIME = COM_PREFIX+"unpack.modification.time"; |
|
91 |
||
92 |
/* |
|
93 |
* Property indicating that the unpacker strip the |
|
94 |
* Debug Attributes, if they are present, in the pack stream. |
|
95 |
* The default value is false. |
|
96 |
* (installer only) |
|
97 |
*/ |
|
98 |
static final String UNPACK_STRIP_DEBUG = COM_PREFIX+"unpack.strip.debug"; |
|
99 |
||
100 |
/* |
|
101 |
* Remove the input file after unpacking. |
|
102 |
* (installer only) |
|
103 |
*/ |
|
104 |
static final String UNPACK_REMOVE_PACKFILE = COM_PREFIX+"unpack.remove.packfile"; |
|
105 |
||
106 |
/* |
|
107 |
* A possible value for MODIFICATION_TIME |
|
108 |
*/ |
|
109 |
static final String NOW = "now"; |
|
110 |
// Other debug options: |
|
111 |
// com...debug.bands=false add band IDs to pack file, to verify sync |
|
112 |
// com...dump.bands=false dump band contents to local disk |
|
113 |
// com...no.vary.codings=false turn off coding variation heuristics |
|
114 |
// com...no.big.strings=false turn off "big string" feature |
|
115 |
||
116 |
/* |
|
117 |
* If this property is set to {@link #TRUE}, the packer will preserve |
|
118 |
* the ordering of class files of the original jar in the output archive. |
|
119 |
* The ordering is preserved only for class-files; resource files |
|
120 |
* may be reordered. |
|
121 |
* <p> |
|
122 |
* If the packer is allowed to reorder class files, it can marginally |
|
123 |
* decrease the transmitted size of the archive. |
|
124 |
*/ |
|
125 |
static final String PACK_KEEP_CLASS_ORDER = COM_PREFIX+"keep.class.order"; |
|
126 |
/* |
|
127 |
* This string PACK200 is given as a zip comment on all JAR files |
|
128 |
* produced by this utility. |
|
129 |
*/ |
|
130 |
static final String PACK_ZIP_ARCHIVE_MARKER_COMMENT = "PACK200"; |
|
131 |
||
6313 | 132 |
// Keep a TLS point to the global data and environment. |
133 |
// This makes it simpler to supply environmental options |
|
2 | 134 |
// to the engine code, especially the native code. |
6313 | 135 |
static final ThreadLocal<TLGlobals> currentInstance = new ThreadLocal<>(); |
136 |
||
12544 | 137 |
// convenience method to access the TL globals |
6313 | 138 |
static TLGlobals getTLGlobals() { |
139 |
return currentInstance.get(); |
|
140 |
} |
|
141 |
||
2 | 142 |
static PropMap currentPropMap() { |
143 |
Object obj = currentInstance.get(); |
|
144 |
if (obj instanceof PackerImpl) |
|
6313 | 145 |
return ((PackerImpl)obj).props; |
2 | 146 |
if (obj instanceof UnpackerImpl) |
6313 | 147 |
return ((UnpackerImpl)obj).props; |
2 | 148 |
return null; |
149 |
} |
|
150 |
||
151 |
static final boolean nolog |
|
12544 | 152 |
= Boolean.getBoolean(COM_PREFIX+"nolog"); |
153 |
||
154 |
static final boolean SORT_MEMBERS_DESCR_MAJOR |
|
155 |
= Boolean.getBoolean(COM_PREFIX+"sort.members.descr.major"); |
|
2 | 156 |
|
12544 | 157 |
static final boolean SORT_HANDLES_KIND_MAJOR |
158 |
= Boolean.getBoolean(COM_PREFIX+"sort.handles.kind.major"); |
|
159 |
||
160 |
static final boolean SORT_INDY_BSS_MAJOR |
|
161 |
= Boolean.getBoolean(COM_PREFIX+"sort.indy.bss.major"); |
|
162 |
||
163 |
static final boolean SORT_BSS_BSM_MAJOR |
|
164 |
= Boolean.getBoolean(COM_PREFIX+"sort.bss.bsm.major"); |
|
2 | 165 |
|
4919 | 166 |
static class Pack200Logger { |
167 |
private final String name; |
|
168 |
private PlatformLogger log; |
|
169 |
Pack200Logger(String name) { |
|
170 |
this.name = name; |
|
171 |
} |
|
172 |
||
173 |
private synchronized PlatformLogger getLogger() { |
|
174 |
if (log == null) { |
|
175 |
log = PlatformLogger.getLogger(name); |
|
176 |
} |
|
177 |
return log; |
|
178 |
} |
|
179 |
||
180 |
public void warning(String msg, Object param) { |
|
181 |
getLogger().warning(msg, param); |
|
182 |
} |
|
183 |
||
184 |
public void warning(String msg) { |
|
185 |
warning(msg, null); |
|
186 |
} |
|
187 |
||
188 |
public void info(String msg) { |
|
189 |
int verbose = currentPropMap().getInteger(DEBUG_VERBOSE); |
|
190 |
if (verbose > 0) { |
|
191 |
if (nolog) { |
|
192 |
System.out.println(msg); |
|
193 |
} else { |
|
194 |
getLogger().info(msg); |
|
2 | 195 |
} |
196 |
} |
|
4919 | 197 |
} |
2 | 198 |
|
4919 | 199 |
public void fine(String msg) { |
200 |
int verbose = currentPropMap().getInteger(DEBUG_VERBOSE); |
|
201 |
if (verbose > 0) { |
|
202 |
System.out.println(msg); |
|
2 | 203 |
} |
4919 | 204 |
} |
2 | 205 |
} |
206 |
||
4919 | 207 |
static final Pack200Logger log |
208 |
= new Pack200Logger("java.util.jar.Pack200"); |
|
209 |
||
2 | 210 |
// Returns the Max Version String of this implementation |
211 |
static String getVersionString() { |
|
6891
f8a528363fa5
6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents:
6313
diff
changeset
|
212 |
return "Pack200, Vendor: " + |
f8a528363fa5
6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents:
6313
diff
changeset
|
213 |
System.getProperty("java.vendor") + |
f8a528363fa5
6991164: pack source needs vendor rebranding changes (jdk7 only)
ksrini
parents:
6313
diff
changeset
|
214 |
", Version: " + |
2 | 215 |
Constants.JAVA6_PACKAGE_MAJOR_VERSION + "." + |
216 |
Constants.JAVA6_PACKAGE_MINOR_VERSION; |
|
217 |
} |
|
218 |
||
219 |
static void markJarFile(JarOutputStream out) throws IOException { |
|
220 |
out.setComment(PACK_ZIP_ARCHIVE_MARKER_COMMENT); |
|
221 |
} |
|
222 |
||
223 |
// -0 mode helper |
|
224 |
static void copyJarFile(JarInputStream in, JarOutputStream out) throws IOException { |
|
225 |
if (in.getManifest() != null) { |
|
226 |
ZipEntry me = new ZipEntry(JarFile.MANIFEST_NAME); |
|
227 |
out.putNextEntry(me); |
|
228 |
in.getManifest().write(out); |
|
229 |
out.closeEntry(); |
|
230 |
} |
|
231 |
byte[] buffer = new byte[1 << 14]; |
|
232 |
for (JarEntry je; (je = in.getNextJarEntry()) != null; ) { |
|
233 |
out.putNextEntry(je); |
|
234 |
for (int nr; 0 < (nr = in.read(buffer)); ) { |
|
235 |
out.write(buffer, 0, nr); |
|
236 |
} |
|
237 |
} |
|
238 |
in.close(); |
|
239 |
markJarFile(out); // add PACK200 comment |
|
240 |
} |
|
241 |
static void copyJarFile(JarFile in, JarOutputStream out) throws IOException { |
|
242 |
byte[] buffer = new byte[1 << 14]; |
|
10115 | 243 |
for (Enumeration<JarEntry> e = in.entries(); e.hasMoreElements(); ) { |
244 |
JarEntry je = e.nextElement(); |
|
2 | 245 |
out.putNextEntry(je); |
246 |
InputStream ein = in.getInputStream(je); |
|
247 |
for (int nr; 0 < (nr = ein.read(buffer)); ) { |
|
248 |
out.write(buffer, 0, nr); |
|
249 |
} |
|
250 |
} |
|
251 |
in.close(); |
|
252 |
markJarFile(out); // add PACK200 comment |
|
253 |
} |
|
254 |
static void copyJarFile(JarInputStream in, OutputStream out) throws IOException { |
|
255 |
// 4947205 : Peformance is slow when using pack-effort=0 |
|
256 |
out = new BufferedOutputStream(out); |
|
257 |
out = new NonCloser(out); // protect from JarOutputStream.close() |
|
8803
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
258 |
try (JarOutputStream jout = new JarOutputStream(out)) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
259 |
copyJarFile(in, jout); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
260 |
} |
2 | 261 |
} |
262 |
static void copyJarFile(JarFile in, OutputStream out) throws IOException { |
|
263 |
||
264 |
// 4947205 : Peformance is slow when using pack-effort=0 |
|
265 |
out = new BufferedOutputStream(out); |
|
266 |
out = new NonCloser(out); // protect from JarOutputStream.close() |
|
8803
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
267 |
try (JarOutputStream jout = new JarOutputStream(out)) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
268 |
copyJarFile(in, jout); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
269 |
} |
2 | 270 |
} |
271 |
// Wrapper to prevent closing of client-supplied stream. |
|
272 |
static private |
|
273 |
class NonCloser extends FilterOutputStream { |
|
274 |
NonCloser(OutputStream out) { super(out); } |
|
275 |
public void close() throws IOException { flush(); } |
|
276 |
} |
|
277 |
static String getJarEntryName(String name) { |
|
278 |
if (name == null) return null; |
|
279 |
return name.replace(File.separatorChar, '/'); |
|
280 |
} |
|
281 |
||
282 |
static String zeString(ZipEntry ze) { |
|
283 |
int store = (ze.getCompressedSize() > 0) ? |
|
284 |
(int)( (1.0 - ((double)ze.getCompressedSize()/(double)ze.getSize()))*100 ) |
|
285 |
: 0 ; |
|
286 |
// Follow unzip -lv output |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
287 |
return ze.getSize() + "\t" + ze.getMethod() |
2 | 288 |
+ "\t" + ze.getCompressedSize() + "\t" |
289 |
+ store + "%\t" |
|
290 |
+ new Date(ze.getTime()) + "\t" |
|
291 |
+ Long.toHexString(ze.getCrc()) + "\t" |
|
292 |
+ ze.getName() ; |
|
293 |
} |
|
294 |
||
295 |
||
296 |
||
297 |
static byte[] readMagic(BufferedInputStream in) throws IOException { |
|
298 |
in.mark(4); |
|
299 |
byte[] magic = new byte[4]; |
|
300 |
for (int i = 0; i < magic.length; i++) { |
|
301 |
// read 1 byte at a time, so we always get 4 |
|
302 |
if (1 != in.read(magic, i, 1)) |
|
303 |
break; |
|
304 |
} |
|
305 |
in.reset(); |
|
306 |
return magic; |
|
307 |
} |
|
308 |
||
309 |
// magic number recognizers |
|
310 |
static boolean isJarMagic(byte[] magic) { |
|
311 |
return (magic[0] == (byte)'P' && |
|
312 |
magic[1] == (byte)'K' && |
|
313 |
magic[2] >= 1 && |
|
314 |
magic[2] < 8 && |
|
315 |
magic[3] == magic[2] + 1); |
|
316 |
} |
|
317 |
static boolean isPackMagic(byte[] magic) { |
|
318 |
return (magic[0] == (byte)0xCA && |
|
319 |
magic[1] == (byte)0xFE && |
|
320 |
magic[2] == (byte)0xD0 && |
|
321 |
magic[3] == (byte)0x0D); |
|
322 |
} |
|
323 |
static boolean isGZIPMagic(byte[] magic) { |
|
324 |
return (magic[0] == (byte)0x1F && |
|
325 |
magic[1] == (byte)0x8B && |
|
326 |
magic[2] == (byte)0x08); |
|
327 |
// fourth byte is variable "flg" field |
|
328 |
} |
|
329 |
||
330 |
private Utils() { } // do not instantiate |
|
331 |
} |