author | ksrini |
Tue, 29 May 2012 14:56:48 -0700 | |
changeset 12857 | 0a5f341c2a28 |
parent 12559 | 9456ceada8b1 |
child 16076 | d7183f4305e5 |
child 14342 | 8435a30053c1 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8803
diff
changeset
|
2 |
* Copyright (c) 2003, 2011, 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 |
||
27 |
package com.sun.java.util.jar.pack; |
|
28 |
||
7192
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
29 |
import java.io.BufferedInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
30 |
import java.io.File; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
31 |
import java.io.FileInputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
32 |
import java.io.IOException; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
33 |
import java.io.InputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
34 |
import java.nio.ByteBuffer; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
35 |
import java.util.jar.JarOutputStream; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
36 |
import java.util.jar.Pack200; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
37 |
import java.util.zip.CRC32; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
38 |
import java.util.zip.Deflater; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
39 |
import java.util.zip.ZipEntry; |
445c518364c4
7003227: (pack200) intermittent failures compiling pack200
ksrini
parents:
6320
diff
changeset
|
40 |
import java.util.zip.ZipOutputStream; |
2 | 41 |
|
42 |
class NativeUnpack { |
|
43 |
// Pointer to the native unpacker obj |
|
44 |
private long unpackerPtr; |
|
45 |
||
46 |
// Input stream. |
|
47 |
private BufferedInputStream in; |
|
48 |
||
49 |
private static synchronized native void initIDs(); |
|
50 |
||
51 |
// Starts processing at the indicated position in the buffer. |
|
52 |
// If the buffer is null, the readInputFn callback is used to get bytes. |
|
53 |
// Returns (s<<32|f), the number of following segments and files. |
|
54 |
private synchronized native long start(ByteBuffer buf, long offset); |
|
55 |
||
56 |
// Returns true if there's another, and fills in the parts. |
|
57 |
private synchronized native boolean getNextFile(Object[] parts); |
|
58 |
||
59 |
private synchronized native ByteBuffer getUnusedInput(); |
|
60 |
||
61 |
// Resets the engine and frees all resources. |
|
62 |
// Returns total number of bytes consumed by the engine. |
|
63 |
private synchronized native long finish(); |
|
64 |
||
65 |
// Setting state in the unpacker. |
|
66 |
protected synchronized native boolean setOption(String opt, String value); |
|
67 |
protected synchronized native String getOption(String opt); |
|
68 |
||
69 |
private int _verbose; |
|
70 |
||
71 |
// State for progress bar: |
|
72 |
private long _byteCount; // bytes read in current segment |
|
73 |
private int _segCount; // number of segs scanned |
|
74 |
private int _fileCount; // number of files written |
|
75 |
private long _estByteLimit; // estimate of eventual total |
|
76 |
private int _estSegLimit; // ditto |
|
77 |
private int _estFileLimit; // ditto |
|
78 |
private int _prevPercent = -1; // for monotonicity |
|
79 |
||
80 |
private final CRC32 _crc32 = new CRC32(); |
|
81 |
private byte[] _buf = new byte[1<<14]; |
|
82 |
||
83 |
private UnpackerImpl _p200; |
|
84 |
private PropMap _props; |
|
85 |
||
86 |
static { |
|
87 |
// If loading from stand alone build uncomment this. |
|
88 |
// System.loadLibrary("unpack"); |
|
89 |
java.security.AccessController.doPrivileged( |
|
12559
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10115
diff
changeset
|
90 |
new java.security.PrivilegedAction<Void>() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10115
diff
changeset
|
91 |
public Void run() { |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10115
diff
changeset
|
92 |
System.loadLibrary("unpack"); |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10115
diff
changeset
|
93 |
return null; |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10115
diff
changeset
|
94 |
} |
9456ceada8b1
7164376: Replace use of sun.security.action.LoadLibraryAction with System.loadLibrary
mchung
parents:
10115
diff
changeset
|
95 |
}); |
2 | 96 |
initIDs(); |
97 |
} |
|
98 |
||
99 |
NativeUnpack(UnpackerImpl p200) { |
|
100 |
super(); |
|
101 |
_p200 = p200; |
|
6313 | 102 |
_props = p200.props; |
2 | 103 |
p200._nunp = this; |
104 |
} |
|
105 |
||
106 |
// for JNI callbacks |
|
107 |
static private Object currentInstance() { |
|
6313 | 108 |
UnpackerImpl p200 = (UnpackerImpl) Utils.getTLGlobals(); |
2 | 109 |
return (p200 == null)? null: p200._nunp; |
110 |
} |
|
111 |
||
112 |
// Callback from the unpacker engine to get more data. |
|
113 |
private long readInputFn(ByteBuffer pbuf, long minlen) throws IOException { |
|
114 |
if (in == null) return 0; // nothing is readable |
|
115 |
long maxlen = pbuf.capacity() - pbuf.position(); |
|
116 |
assert(minlen <= maxlen); // don't talk nonsense |
|
117 |
long numread = 0; |
|
118 |
int steps = 0; |
|
119 |
while (numread < minlen) { |
|
120 |
steps++; |
|
121 |
// read available input, up to buf.length or maxlen |
|
122 |
int readlen = _buf.length; |
|
123 |
if (readlen > (maxlen - numread)) |
|
124 |
readlen = (int)(maxlen - numread); |
|
125 |
int nr = in.read(_buf, 0, readlen); |
|
126 |
if (nr <= 0) break; |
|
127 |
numread += nr; |
|
128 |
assert(numread <= maxlen); |
|
129 |
// %%% get rid of this extra copy by using nio? |
|
130 |
pbuf.put(_buf, 0, nr); |
|
131 |
} |
|
132 |
if (_verbose > 1) |
|
133 |
Utils.log.fine("readInputFn("+minlen+","+maxlen+") => "+numread+" steps="+steps); |
|
134 |
if (maxlen > 100) { |
|
135 |
_estByteLimit = _byteCount + maxlen; |
|
136 |
} else { |
|
137 |
_estByteLimit = (_byteCount + numread) * 20; |
|
138 |
} |
|
139 |
_byteCount += numread; |
|
140 |
updateProgress(); |
|
141 |
return numread; |
|
142 |
} |
|
143 |
||
144 |
private void updateProgress() { |
|
145 |
// Progress is a combination of segment reading and file writing. |
|
146 |
final double READ_WT = 0.33; |
|
147 |
final double WRITE_WT = 0.67; |
|
148 |
double readProgress = _segCount; |
|
149 |
if (_estByteLimit > 0 && _byteCount > 0) |
|
150 |
readProgress += (double)_byteCount / _estByteLimit; |
|
151 |
double writeProgress = _fileCount; |
|
152 |
double scaledProgress |
|
153 |
= READ_WT * readProgress / Math.max(_estSegLimit,1) |
|
154 |
+ WRITE_WT * writeProgress / Math.max(_estFileLimit,1); |
|
155 |
int percent = (int) Math.round(100*scaledProgress); |
|
156 |
if (percent > 100) percent = 100; |
|
157 |
if (percent > _prevPercent) { |
|
158 |
_prevPercent = percent; |
|
159 |
_props.setInteger(Pack200.Unpacker.PROGRESS, percent); |
|
160 |
if (_verbose > 0) |
|
161 |
Utils.log.info("progress = "+percent); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
private void copyInOption(String opt) { |
|
166 |
String val = _props.getProperty(opt); |
|
167 |
if (_verbose > 0) |
|
168 |
Utils.log.info("set "+opt+"="+val); |
|
169 |
if (val != null) { |
|
170 |
boolean set = setOption(opt, val); |
|
171 |
if (!set) |
|
172 |
Utils.log.warning("Invalid option "+opt+"="+val); |
|
173 |
} |
|
174 |
} |
|
175 |
||
176 |
void run(InputStream inRaw, JarOutputStream jstream, |
|
177 |
ByteBuffer presetInput) throws IOException { |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
178 |
BufferedInputStream in0 = new BufferedInputStream(inRaw); |
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
179 |
this.in = in0; // for readInputFn to see |
2 | 180 |
_verbose = _props.getInteger(Utils.DEBUG_VERBOSE); |
181 |
// Fix for BugId: 4902477, -unpack.modification.time = 1059010598000 |
|
182 |
// TODO eliminate and fix in unpack.cpp |
|
183 |
||
184 |
final int modtime = Pack200.Packer.KEEP.equals(_props.getProperty(Utils.UNPACK_MODIFICATION_TIME, "0")) ? |
|
185 |
Constants.NO_MODTIME : _props.getTime(Utils.UNPACK_MODIFICATION_TIME); |
|
186 |
||
187 |
copyInOption(Utils.DEBUG_VERBOSE); |
|
188 |
copyInOption(Pack200.Unpacker.DEFLATE_HINT); |
|
189 |
if (modtime == Constants.NO_MODTIME) // Dont pass KEEP && NOW |
|
190 |
copyInOption(Utils.UNPACK_MODIFICATION_TIME); |
|
191 |
updateProgress(); // reset progress bar |
|
192 |
for (;;) { |
|
193 |
// Read the packed bits. |
|
194 |
long counts = start(presetInput, 0); |
|
195 |
_byteCount = _estByteLimit = 0; // reset partial scan counts |
|
196 |
++_segCount; // just finished scanning a whole segment... |
|
197 |
int nextSeg = (int)( counts >>> 32 ); |
|
198 |
int nextFile = (int)( counts >>> 0 ); |
|
199 |
||
200 |
// Estimate eventual total number of segments and files. |
|
201 |
_estSegLimit = _segCount + nextSeg; |
|
202 |
double filesAfterThisSeg = _fileCount + nextFile; |
|
203 |
_estFileLimit = (int)( (filesAfterThisSeg * |
|
204 |
_estSegLimit) / _segCount ); |
|
205 |
||
206 |
// Write the files. |
|
207 |
int[] intParts = { 0,0, 0, 0 }; |
|
208 |
// intParts = {size.hi/lo, mod, defl} |
|
209 |
Object[] parts = { intParts, null, null, null }; |
|
210 |
// parts = { {intParts}, name, data0/1 } |
|
211 |
while (getNextFile(parts)) { |
|
212 |
//BandStructure.printArrayTo(System.out, intParts, 0, parts.length); |
|
213 |
String name = (String) parts[1]; |
|
214 |
long size = ( (long)intParts[0] << 32) |
|
215 |
+ (((long)intParts[1] << 32) >>> 32); |
|
216 |
||
217 |
long mtime = (modtime != Constants.NO_MODTIME ) ? |
|
218 |
modtime : intParts[2] ; |
|
219 |
boolean deflateHint = (intParts[3] != 0); |
|
220 |
ByteBuffer data0 = (ByteBuffer) parts[2]; |
|
221 |
ByteBuffer data1 = (ByteBuffer) parts[3]; |
|
222 |
writeEntry(jstream, name, mtime, size, deflateHint, |
|
223 |
data0, data1); |
|
224 |
++_fileCount; |
|
225 |
updateProgress(); |
|
226 |
} |
|
6320 | 227 |
presetInput = getUnusedInput(); |
2 | 228 |
long consumed = finish(); |
229 |
if (_verbose > 0) |
|
230 |
Utils.log.info("bytes consumed = "+consumed); |
|
231 |
if (presetInput == null && |
|
7795
98021fc612af
6990106: FindBugs scan - Malicious code vulnerability Warnings in com.sun.java.util.jar.pack.*
ksrini
parents:
7192
diff
changeset
|
232 |
!Utils.isPackMagic(Utils.readMagic(in0))) { |
2 | 233 |
break; |
234 |
} |
|
235 |
if (_verbose > 0 ) { |
|
236 |
if (presetInput != null) |
|
237 |
Utils.log.info("unused input = "+presetInput); |
|
238 |
} |
|
239 |
} |
|
240 |
} |
|
241 |
||
242 |
void run(InputStream in, JarOutputStream jstream) throws IOException { |
|
243 |
run(in, jstream, null); |
|
244 |
} |
|
245 |
||
246 |
void run(File inFile, JarOutputStream jstream) throws IOException { |
|
247 |
// %%% maybe memory-map the file, and pass it straight into unpacker |
|
248 |
ByteBuffer mappedFile = null; |
|
8803
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
249 |
try (FileInputStream fis = new FileInputStream(inFile)) { |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
250 |
run(fis, jstream, mappedFile); |
b3f57bfca459
7022382: convert pack200 library code to use try-with-resources
smarks
parents:
7795
diff
changeset
|
251 |
} |
2 | 252 |
// Note: caller is responsible to finish with jstream. |
253 |
} |
|
254 |
||
255 |
private void writeEntry(JarOutputStream j, String name, |
|
256 |
long mtime, long lsize, boolean deflateHint, |
|
257 |
ByteBuffer data0, ByteBuffer data1) throws IOException { |
|
258 |
int size = (int)lsize; |
|
259 |
if (size != lsize) |
|
260 |
throw new IOException("file too large: "+lsize); |
|
261 |
||
262 |
CRC32 crc32 = _crc32; |
|
263 |
||
264 |
if (_verbose > 1) |
|
265 |
Utils.log.fine("Writing entry: "+name+" size="+size |
|
266 |
+(deflateHint?" deflated":"")); |
|
267 |
||
268 |
if (_buf.length < size) { |
|
269 |
int newSize = size; |
|
270 |
while (newSize < _buf.length) { |
|
271 |
newSize <<= 1; |
|
272 |
if (newSize <= 0) { |
|
273 |
newSize = size; |
|
274 |
break; |
|
275 |
} |
|
276 |
} |
|
277 |
_buf = new byte[newSize]; |
|
278 |
} |
|
279 |
assert(_buf.length >= size); |
|
280 |
||
281 |
int fillp = 0; |
|
282 |
if (data0 != null) { |
|
283 |
int size0 = data0.capacity(); |
|
284 |
data0.get(_buf, fillp, size0); |
|
285 |
fillp += size0; |
|
286 |
} |
|
287 |
if (data1 != null) { |
|
288 |
int size1 = data1.capacity(); |
|
289 |
data1.get(_buf, fillp, size1); |
|
290 |
fillp += size1; |
|
291 |
} |
|
292 |
while (fillp < size) { |
|
293 |
// Fill in rest of data from the stream itself. |
|
294 |
int nr = in.read(_buf, fillp, size - fillp); |
|
295 |
if (nr <= 0) throw new IOException("EOF at end of archive"); |
|
296 |
fillp += nr; |
|
297 |
} |
|
298 |
||
299 |
ZipEntry z = new ZipEntry(name); |
|
10115 | 300 |
z.setTime(mtime * 1000); |
2 | 301 |
|
302 |
if (size == 0) { |
|
303 |
z.setMethod(ZipOutputStream.STORED); |
|
304 |
z.setSize(0); |
|
305 |
z.setCrc(0); |
|
306 |
z.setCompressedSize(0); |
|
307 |
} else if (!deflateHint) { |
|
308 |
z.setMethod(ZipOutputStream.STORED); |
|
309 |
z.setSize(size); |
|
310 |
z.setCompressedSize(size); |
|
311 |
crc32.reset(); |
|
312 |
crc32.update(_buf, 0, size); |
|
313 |
z.setCrc(crc32.getValue()); |
|
314 |
} else { |
|
315 |
z.setMethod(Deflater.DEFLATED); |
|
316 |
z.setSize(size); |
|
317 |
} |
|
318 |
||
319 |
j.putNextEntry(z); |
|
320 |
||
321 |
if (size > 0) |
|
322 |
j.write(_buf, 0, size); |
|
323 |
||
324 |
j.closeEntry(); |
|
325 |
if (_verbose > 0) Utils.log.info("Writing " + Utils.zeString(z)); |
|
326 |
} |
|
327 |
} |