8203328: Rename EFS in java.util.zip internals to something meaningful
Reviewed-by: sherman

   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.Buffer;
  29 import java.nio.ByteBuffer;
  30 import java.nio.file.attribute.FileTime;
  31 import java.security.AccessController;
  32 import java.security.PrivilegedAction;
  33 import java.time.DateTimeException;
  34 import java.time.Instant;
  35 import java.time.LocalDateTime;
  36 import java.time.ZoneId;
  37 import java.util.Date;
  38 import java.util.concurrent.TimeUnit;
  39 
  40 import static java.util.zip.ZipConstants.ENDHDR;
  41 
  42 import jdk.internal.misc.Unsafe;
  43 import sun.nio.ch.DirectBuffer;
  44 
  45 class ZipUtils {
  46 
  47     // used to adjust values between Windows and java epoch
  48     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  49 
  50     // used to indicate the corresponding windows time is not available
  51     public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
  52 
  53     // static final ByteBuffer defaultBuf = ByteBuffer.allocateDirect(0);
  54     static final ByteBuffer defaultBuf = ByteBuffer.allocate(0);
  55 
  56     /**
  57      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  58      */
  59     public static final FileTime winTimeToFileTime(long wtime) {
  60         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  61                              TimeUnit.MICROSECONDS);
  62     }
  63 
  64     /**
  65      * Converts FileTime to Windows time.
  66      */
  67     public static final long fileTimeToWinTime(FileTime ftime) {
  68         return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
  69     }
  70 
  71     /**
  72      * The upper bound of the 32-bit unix time, the "year 2038 problem".
  73      */
  74     public static final long UPPER_UNIXTIME_BOUND = 0x7fffffff;
  75 
  76     /**
  77      * Converts "standard Unix time"(in seconds, UTC/GMT) to FileTime
  78      */
  79     public static final FileTime unixTimeToFileTime(long utime) {
  80         return FileTime.from(utime, TimeUnit.SECONDS);
  81     }
  82 
  83     /**
  84      * Converts FileTime to "standard Unix time".
  85      */
  86     public static final long fileTimeToUnixTime(FileTime ftime) {
  87         return ftime.to(TimeUnit.SECONDS);
  88     }
  89 
  90     /**

  91      * Converts DOS time to Java time (number of milliseconds since epoch).
  92      */
  93     public static long dosToJavaTime(long dtime) {
  94         int year = (int) (((dtime >> 25) & 0x7f) + 1980);
  95         int month = (int) ((dtime >> 21) & 0x0f);
  96         int day = (int) ((dtime >> 16) & 0x1f);
  97         int hour = (int) ((dtime >> 11) & 0x1f);
  98         int minute = (int) ((dtime >> 5) & 0x3f);
  99         int second = (int) ((dtime << 1) & 0x3e);
 100 
 101         if (month > 0 && month < 13 && day > 0 && hour < 24 && minute < 60 && second < 60) {
 102             try {
 103                 LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, minute, second);
 104                 return TimeUnit.MILLISECONDS.convert(ldt.toEpochSecond(
 105                         ZoneId.systemDefault().getRules().getOffset(ldt)), TimeUnit.SECONDS);
 106             } catch (DateTimeException dte) {
 107                 // ignore
 108             }
 109         }
 110         return overflowDosToJavaTime(year, month, day, hour, minute, second);
 111     }
 112 
 113     /*
 114      * Deal with corner cases where an arguably mal-formed DOS time is used
 115      */
 116     @SuppressWarnings("deprecation") // Use of Date constructor
 117     private static long overflowDosToJavaTime(int year, int month, int day,
 118                                               int hour, int minute, int second) {
 119         return new Date(year - 1900, month - 1, day, hour, minute, second).getTime();
 120     }
 121 
 122 
 123     /**
 124      * Converts extended DOS time to Java time, where up to 1999 milliseconds
 125      * might be encoded into the upper half of the returned long.
 126      *
 127      * @param xdostime the extended DOS time value
 128      * @return milliseconds since epoch
 129      */
 130     public static long extendedDosToJavaTime(long xdostime) {
 131         long time = dosToJavaTime(xdostime);
 132         return time + (xdostime >> 32);
 133     }
 134 
 135     /**
 136      * Converts Java time to DOS time.
 137      */
 138     private static long javaToDosTime(long time) {
 139         Instant instant = Instant.ofEpochMilli(time);
 140         LocalDateTime ldt = LocalDateTime.ofInstant(
 141                 instant, ZoneId.systemDefault());
 142         int year = ldt.getYear() - 1980;
 143         if (year < 0) {
 144             return (1 << 21) | (1 << 16);
 145         }
 146         return (year << 25 |
 147             ldt.getMonthValue() << 21 |
 148             ldt.getDayOfMonth() << 16 |
 149             ldt.getHour() << 11 |
 150             ldt.getMinute() << 5 |
 151             ldt.getSecond() >> 1) & 0xffffffffL;
 152     }
 153 
 154     /**
 155      * Converts Java time to DOS time, encoding any milliseconds lost
 156      * in the conversion into the upper half of the returned long.
 157      *
 158      * @param time milliseconds since epoch
 159      * @return DOS time with 2s remainder encoded into upper half
 160      */
 161     public static long javaToExtendedDosTime(long time) {
 162         if (time < 0) {
 163             return ZipEntry.DOSTIME_BEFORE_1980;
 164         }
 165         long dostime = javaToDosTime(time);
 166         return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
 167                 ? dostime + ((time % 2000) << 32)
 168                 : ZipEntry.DOSTIME_BEFORE_1980;
 169     }
 170 
 171     /**
 172      * Fetches unsigned 16-bit value from byte array at specified offset.
 173      * The bytes are assumed to be in Intel (little-endian) byte order.
 174      */
 175     public static final int get16(byte b[], int off) {
 176         return (b[off] & 0xff) | ((b[off + 1] & 0xff) << 8);
 177     }
 178 
 179     /**
 180      * Fetches unsigned 32-bit value from byte array at specified offset.
 181      * The bytes are assumed to be in Intel (little-endian) byte order.
 182      */
 183     public static final long get32(byte b[], int off) {
 184         return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
 185     }
 186 
 187     /**
 188      * Fetches signed 64-bit value from byte array at specified offset.
 189      * The bytes are assumed to be in Intel (little-endian) byte order.
 190      */
 191     public static final long get64(byte b[], int off) {
 192         return get32(b, off) | (get32(b, off+4) << 32);
 193     }
 194 
 195     /**
 196      * Fetches signed 32-bit value from byte array at specified offset.
 197      * The bytes are assumed to be in Intel (little-endian) byte order.
 198      *
 199      */
 200     public static final int get32S(byte b[], int off) {
 201         return (get16(b, off) | (get16(b, off+2) << 16));
 202     }
 203 
 204     // fields access methods
 205     static final int CH(byte[] b, int n) {
 206         return b[n] & 0xff ;
 207     }
 208 
 209     static final int SH(byte[] b, int n) {
 210         return (b[n] & 0xff) | ((b[n + 1] & 0xff) << 8);
 211     }
 212 
 213     static final long LG(byte[] b, int n) {
 214         return ((SH(b, n)) | (SH(b, n + 2) << 16)) & 0xffffffffL;
 215     }
 216 
 217     static final long LL(byte[] b, int n) {
 218         return (LG(b, n)) | (LG(b, n + 4) << 32);
 219     }
 220 
 221     static final long GETSIG(byte[] b) {
 222         return LG(b, 0);
 223     }
 224 
 225     // local file (LOC) header fields
 226     static final long LOCSIG(byte[] b) { return LG(b, 0); } // signature
 227     static final int  LOCVER(byte[] b) { return SH(b, 4); } // version needed to extract
 228     static final int  LOCFLG(byte[] b) { return SH(b, 6); } // general purpose bit flags
 229     static final int  LOCHOW(byte[] b) { return SH(b, 8); } // compression method
 230     static final long LOCTIM(byte[] b) { return LG(b, 10);} // modification time
 231     static final long LOCCRC(byte[] b) { return LG(b, 14);} // crc of uncompressed data
 232     static final long LOCSIZ(byte[] b) { return LG(b, 18);} // compressed data size
 233     static final long LOCLEN(byte[] b) { return LG(b, 22);} // uncompressed data size
 234     static final int  LOCNAM(byte[] b) { return SH(b, 26);} // filename length
 235     static final int  LOCEXT(byte[] b) { return SH(b, 28);} // extra field length
 236 
 237     // extra local (EXT) header fields
 238     static final long EXTCRC(byte[] b) { return LG(b, 4);}  // crc of uncompressed data
 239     static final long EXTSIZ(byte[] b) { return LG(b, 8);}  // compressed size
 240     static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size
 241 
 242     // end of central directory header (END) fields
 243     static final int  ENDSUB(byte[] b) { return SH(b, 8); }  // number of entries on this disk
 244     static final int  ENDTOT(byte[] b) { return SH(b, 10);}  // total number of entries
 245     static final long ENDSIZ(byte[] b) { return LG(b, 12);}  // central directory size
 246     static final long ENDOFF(byte[] b) { return LG(b, 16);}  // central directory offset
 247     static final int  ENDCOM(byte[] b) { return SH(b, 20);}  // size of zip file comment
 248     static final int  ENDCOM(byte[] b, int off) { return SH(b, off + 20);}
 249 
 250     // zip64 end of central directory recoder fields
 251     static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);}  // total number of entries on disk
 252     static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);}  // total number of entries
 253     static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);}  // central directory size
 254     static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);}  // central directory offset
 255     static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);}   // zip64 end offset
 256 
 257     // central directory header (CEN) fields
 258     static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
 259     static final int  CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
 260     static final int  CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
 261     static final int  CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
 262     static final int  CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
 263     static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
 264     static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
 265     static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
 266     static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
 267     static final int  CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
 268     static final int  CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
 269     static final int  CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
 270     static final int  CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
 271     static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
 272     static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
 273     static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
 274 
 275     // The END header is followed by a variable length comment of size < 64k.
 276     static final long END_MAXLEN = 0xFFFF + ENDHDR;
 277     static final int READBLOCKSZ = 128;
 278 
 279     /**
 280      * Loads zip native library, if not already laoded
 281      */
 282     static void loadLibrary() {
 283         SecurityManager sm = System.getSecurityManager();
 284         if (sm == null) {
 285             System.loadLibrary("zip");
 286         } else {
 287             PrivilegedAction<Void> pa = () -> { System.loadLibrary("zip"); return null; };
 288             AccessController.doPrivileged(pa);
 289         }
 290     }
 291 
 292     private static final Unsafe unsafe = Unsafe.getUnsafe();
 293 
 294     private static final long byteBufferArrayOffset = unsafe.objectFieldOffset(ByteBuffer.class, "hb");
 295     private static final long byteBufferOffsetOffset = unsafe.objectFieldOffset(ByteBuffer.class, "offset");
 296 
 297     static byte[] getBufferArray(ByteBuffer byteBuffer) {
 298         return (byte[]) unsafe.getObject(byteBuffer, byteBufferArrayOffset);
 299     }
 300 
 301     static int getBufferOffset(ByteBuffer byteBuffer) {
 302         return unsafe.getInt(byteBuffer, byteBufferOffsetOffset);
 303     }
 304 }
--- EOF ---