1 /*
   2  * Copyright (c) 2013, 2015, 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.file.attribute.FileTime;
  29 import java.time.Instant;
  30 import java.time.LocalDateTime;
  31 import java.time.ZoneId;
  32 import java.util.concurrent.TimeUnit;
  33 
  34 class ZipUtils {
  35 
  36     // used to adjust values between Windows and java epoch
  37     private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
  38 
  39     /**
  40      * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
  41      */
  42     public static final FileTime winTimeToFileTime(long wtime) {
  43         return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
  44                              TimeUnit.MICROSECONDS);
  45     }
  46 
  47     /**
  48      * Converts FileTime to Windows time.
  49      */
  50     public static final long fileTimeToWinTime(FileTime ftime) {
  51         return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
  52     }
  53 
  54     /**
  55      * Converts "standard Unix time"(in seconds, UTC/GMT) to FileTime
  56      */
  57     public static final FileTime unixTimeToFileTime(long utime) {
  58         return FileTime.from(utime, TimeUnit.SECONDS);
  59     }
  60 
  61     /**
  62      * Converts FileTime to "standard Unix time".
  63      */
  64     public static final long fileTimeToUnixTime(FileTime ftime) {
  65         return ftime.to(TimeUnit.SECONDS);
  66     }
  67 
  68     /**
  69      * Converts DOS time to Java time (number of milliseconds since epoch).
  70      */
  71     public static long dosToJavaTime(long dtime) {
  72         LocalDateTime ldt = LocalDateTime.of(
  73                 (int) (((dtime >> 25) & 0x7f) + 1980),
  74                 (int) ((dtime >> 21) & 0x0f),
  75                 (int) ((dtime >> 16) & 0x1f),
  76                 (int) ((dtime >> 11) & 0x1f),
  77                 (int) ((dtime >> 5) & 0x3f),
  78                 (int) ((dtime << 1) & 0x3e));
  79         return ldt.toEpochSecond(
  80                 ZoneId.systemDefault().getRules().getOffset(ldt)) * 1000L;
  81     }
  82 
  83     /**
  84      * Converts extended DOS time to Java time, where up to 1999 milliseconds
  85      * might be encoded into the upper half of the returned long.
  86      *
  87      * @param xdostime the extended DOS time value
  88      * @return milliseconds since epoch
  89      */
  90     public static long extendedDosToJavaTime(long xdostime) {
  91         long time = dosToJavaTime(xdostime);
  92         return time + (xdostime >> 32);
  93     }
  94 
  95     /**
  96      * Converts Java time to DOS time.
  97      */
  98     private static long javaToDosTime(long time) {
  99         Instant instant = Instant.ofEpochMilli(time);
 100         LocalDateTime ldt = LocalDateTime.ofInstant(
 101                 instant, ZoneId.systemDefault());
 102         int year = ldt.getYear() - 1980;
 103         if (year < 0) {
 104             return (1 << 21) | (1 << 16);
 105         }
 106         return (year << 25 |
 107             ldt.getMonthValue() << 21 |
 108             ldt.getDayOfMonth() << 16 |
 109             ldt.getHour() << 11 |
 110             ldt.getMinute() << 5 |
 111             ldt.getSecond() >> 1) & 0xffffffffL;
 112     }
 113 
 114     /**
 115      * Converts Java time to DOS time, encoding any milliseconds lost
 116      * in the conversion into the upper half of the returned long.
 117      *
 118      * @param time milliseconds since epoch
 119      * @return DOS time with 2s remainder encoded into upper half
 120      */
 121     public static long javaToExtendedDosTime(long time) {
 122         if (time < 0) {
 123             return ZipEntry.DOSTIME_BEFORE_1980;
 124         }
 125         long dostime = javaToDosTime(time);
 126         return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
 127                 ? dostime + ((time % 2000) << 32)
 128                 : ZipEntry.DOSTIME_BEFORE_1980;
 129     }
 130 
 131     /**
 132      * Fetches unsigned 16-bit value from byte array at specified offset.
 133      * The bytes are assumed to be in Intel (little-endian) byte order.
 134      */
 135     public static final int get16(byte b[], int off) {
 136         return Byte.toUnsignedInt(b[off]) | (Byte.toUnsignedInt(b[off+1]) << 8);
 137     }
 138 
 139     /**
 140      * Fetches unsigned 32-bit value from byte array at specified offset.
 141      * The bytes are assumed to be in Intel (little-endian) byte order.
 142      */
 143     public static final long get32(byte b[], int off) {
 144         return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
 145     }
 146 
 147     /**
 148      * Fetches signed 64-bit value from byte array at specified offset.
 149      * The bytes are assumed to be in Intel (little-endian) byte order.
 150      */
 151     public static final long get64(byte b[], int off) {
 152         return get32(b, off) | (get32(b, off+4) << 32);
 153     }
 154 
 155     /**
 156      * Fetches signed 32-bit value from byte array at specified offset.
 157      * The bytes are assumed to be in Intel (little-endian) byte order.
 158      *
 159      */
 160     public static final int get32S(byte b[], int off) {
 161         return (get16(b, off) | (get16(b, off+2) << 16));
 162     }
 163 }