1 /*
2 * Copyright (c) 2013, 2018, 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 java.util.zip;
27
28 import java.nio.ByteBuffer;
29 import java.nio.file.attribute.FileTime;
30 import java.security.AccessController;
31 import java.security.PrivilegedAction;
32 import java.time.DateTimeException;
33 import java.time.Instant;
34 import java.time.LocalDateTime;
35 import java.time.ZoneId;
36 import java.util.Date;
37 import java.util.concurrent.TimeUnit;
38
39 import static java.util.zip.ZipConstants.ENDHDR;
40
41 import jdk.internal.misc.Unsafe;
42
43 class ZipUtils {
44
45 // used to adjust values between Windows and java epoch
46 private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
47
48 // used to indicate the corresponding windows time is not available
49 public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
50
51 // static final ByteBuffer defaultBuf = ByteBuffer.allocateDirect(0);
261 static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
262 static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
263 static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
264 static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
265 static final int CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
266 static final int CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
267 static final int CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
268 static final int CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
269 static final int CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
270 static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
271 static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
272
273 // The END header is followed by a variable length comment of size < 64k.
274 static final long END_MAXLEN = 0xFFFF + ENDHDR;
275 static final int READBLOCKSZ = 128;
276
277 /**
278 * Loads zip native library, if not already laoded
279 */
280 static void loadLibrary() {
281 SecurityManager sm = System.getSecurityManager();
282 if (sm == null) {
283 System.loadLibrary("zip");
284 } else {
285 PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
286 AccessController.doPrivileged(pa);
287 }
288 }
289
290 private static final Unsafe unsafe = Unsafe.getUnsafe();
291
292 private static final long byteBufferArrayOffset = unsafe.objectFieldOffset(ByteBuffer.class, "hb");
293 private static final long byteBufferOffsetOffset = unsafe.objectFieldOffset(ByteBuffer.class, "offset");
294
295 static byte[] getBufferArray(ByteBuffer byteBuffer) {
296 return (byte[]) unsafe.getReference(byteBuffer, byteBufferArrayOffset);
297 }
298
299 static int getBufferOffset(ByteBuffer byteBuffer) {
300 return unsafe.getInt(byteBuffer, byteBufferOffsetOffset);
301 }
302 }
|
1 /*
2 * Copyright (c) 2013, 2019, 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 java.util.zip;
27
28 import java.nio.ByteBuffer;
29 import java.nio.file.attribute.FileTime;
30 import java.time.DateTimeException;
31 import java.time.Instant;
32 import java.time.LocalDateTime;
33 import java.time.ZoneId;
34 import java.util.Date;
35 import java.util.concurrent.TimeUnit;
36
37 import static java.util.zip.ZipConstants.ENDHDR;
38
39 import jdk.internal.misc.Unsafe;
40
41 class ZipUtils {
42
43 // used to adjust values between Windows and java epoch
44 private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
45
46 // used to indicate the corresponding windows time is not available
47 public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
48
49 // static final ByteBuffer defaultBuf = ByteBuffer.allocateDirect(0);
259 static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
260 static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
261 static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
262 static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
263 static final int CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
264 static final int CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
265 static final int CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
266 static final int CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
267 static final int CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
268 static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
269 static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
270
271 // The END header is followed by a variable length comment of size < 64k.
272 static final long END_MAXLEN = 0xFFFF + ENDHDR;
273 static final int READBLOCKSZ = 128;
274
275 /**
276 * Loads zip native library, if not already laoded
277 */
278 static void loadLibrary() {
279 jdk.internal.access.SharedSecrets.getJavaLangAccess().loadLibrary("zip");
280 }
281
282 private static final Unsafe unsafe = Unsafe.getUnsafe();
283
284 private static final long byteBufferArrayOffset = unsafe.objectFieldOffset(ByteBuffer.class, "hb");
285 private static final long byteBufferOffsetOffset = unsafe.objectFieldOffset(ByteBuffer.class, "offset");
286
287 static byte[] getBufferArray(ByteBuffer byteBuffer) {
288 return (byte[]) unsafe.getReference(byteBuffer, byteBufferArrayOffset);
289 }
290
291 static int getBufferOffset(ByteBuffer byteBuffer) {
292 return unsafe.getInt(byteBuffer, byteBufferOffsetOffset);
293 }
294 }
|