1/*
2 * Copyright (c) 2013, 2016, 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
26package java.util.zip;
27
28import java.nio.file.attribute.FileTime;
29import java.time.Instant;
30import java.time.LocalDateTime;
31import java.time.ZoneId;
32import java.util.concurrent.TimeUnit;
33
34import static java.util.zip.ZipConstants.ENDHDR;
35
36class ZipUtils {
37
38    // used to adjust values between Windows and java epoch
39    private static final long WINDOWS_EPOCH_IN_MICROSECONDS = -11644473600000000L;
40
41    // used to indicate the corresponding windows time is not available
42    public static final long WINDOWS_TIME_NOT_AVAILABLE = Long.MIN_VALUE;
43
44    /**
45     * Converts Windows time (in microseconds, UTC/GMT) time to FileTime.
46     */
47    public static final FileTime winTimeToFileTime(long wtime) {
48        return FileTime.from(wtime / 10 + WINDOWS_EPOCH_IN_MICROSECONDS,
49                             TimeUnit.MICROSECONDS);
50    }
51
52    /**
53     * Converts FileTime to Windows time.
54     */
55    public static final long fileTimeToWinTime(FileTime ftime) {
56        return (ftime.to(TimeUnit.MICROSECONDS) - WINDOWS_EPOCH_IN_MICROSECONDS) * 10;
57    }
58
59    /**
60     * The upper bound of the 32-bit unix time, the "year 2038 problem".
61     */
62    public static final long UPPER_UNIXTIME_BOUND = 0x7fffffff;
63
64    /**
65     * Converts "standard Unix time"(in seconds, UTC/GMT) to FileTime
66     */
67    public static final FileTime unixTimeToFileTime(long utime) {
68        return FileTime.from(utime, TimeUnit.SECONDS);
69    }
70
71    /**
72     * Converts FileTime to "standard Unix time".
73     */
74    public static final long fileTimeToUnixTime(FileTime ftime) {
75        return ftime.to(TimeUnit.SECONDS);
76    }
77
78    /**
79     * Converts DOS time to Java time (number of milliseconds since epoch).
80     */
81    public static long dosToJavaTime(long dtime) {
82        LocalDateTime ldt = LocalDateTime.of(
83                (int) (((dtime >> 25) & 0x7f) + 1980),
84                (int) ((dtime >> 21) & 0x0f),
85                (int) ((dtime >> 16) & 0x1f),
86                (int) ((dtime >> 11) & 0x1f),
87                (int) ((dtime >> 5) & 0x3f),
88                (int) ((dtime << 1) & 0x3e));
89        return TimeUnit.MILLISECONDS.convert(ldt.toEpochSecond(
90                ZoneId.systemDefault().getRules().getOffset(ldt)), TimeUnit.SECONDS);
91    }
92
93    /**
94     * Converts extended DOS time to Java time, where up to 1999 milliseconds
95     * might be encoded into the upper half of the returned long.
96     *
97     * @param xdostime the extended DOS time value
98     * @return milliseconds since epoch
99     */
100    public static long extendedDosToJavaTime(long xdostime) {
101        long time = dosToJavaTime(xdostime);
102        return time + (xdostime >> 32);
103    }
104
105    /**
106     * Converts Java time to DOS time.
107     */
108    private static long javaToDosTime(long time) {
109        Instant instant = Instant.ofEpochMilli(time);
110        LocalDateTime ldt = LocalDateTime.ofInstant(
111                instant, ZoneId.systemDefault());
112        int year = ldt.getYear() - 1980;
113        if (year < 0) {
114            return (1 << 21) | (1 << 16);
115        }
116        return (year << 25 |
117            ldt.getMonthValue() << 21 |
118            ldt.getDayOfMonth() << 16 |
119            ldt.getHour() << 11 |
120            ldt.getMinute() << 5 |
121            ldt.getSecond() >> 1) & 0xffffffffL;
122    }
123
124    /**
125     * Converts Java time to DOS time, encoding any milliseconds lost
126     * in the conversion into the upper half of the returned long.
127     *
128     * @param time milliseconds since epoch
129     * @return DOS time with 2s remainder encoded into upper half
130     */
131    public static long javaToExtendedDosTime(long time) {
132        if (time < 0) {
133            return ZipEntry.DOSTIME_BEFORE_1980;
134        }
135        long dostime = javaToDosTime(time);
136        return (dostime != ZipEntry.DOSTIME_BEFORE_1980)
137                ? dostime + ((time % 2000) << 32)
138                : ZipEntry.DOSTIME_BEFORE_1980;
139    }
140
141    /**
142     * Fetches unsigned 16-bit value from byte array at specified offset.
143     * The bytes are assumed to be in Intel (little-endian) byte order.
144     */
145    public static final int get16(byte b[], int off) {
146        return (b[off] & 0xff) | ((b[off + 1] & 0xff) << 8);
147    }
148
149    /**
150     * Fetches unsigned 32-bit value from byte array at specified offset.
151     * The bytes are assumed to be in Intel (little-endian) byte order.
152     */
153    public static final long get32(byte b[], int off) {
154        return (get16(b, off) | ((long)get16(b, off+2) << 16)) & 0xffffffffL;
155    }
156
157    /**
158     * Fetches signed 64-bit value from byte array at specified offset.
159     * The bytes are assumed to be in Intel (little-endian) byte order.
160     */
161    public static final long get64(byte b[], int off) {
162        return get32(b, off) | (get32(b, off+4) << 32);
163    }
164
165    /**
166     * Fetches signed 32-bit value from byte array at specified offset.
167     * The bytes are assumed to be in Intel (little-endian) byte order.
168     *
169     */
170    public static final int get32S(byte b[], int off) {
171        return (get16(b, off) | (get16(b, off+2) << 16));
172    }
173
174    // fields access methods
175    static final int CH(byte[] b, int n) {
176        return b[n] & 0xff ;
177    }
178
179    static final int SH(byte[] b, int n) {
180        return (b[n] & 0xff) | ((b[n + 1] & 0xff) << 8);
181    }
182
183    static final long LG(byte[] b, int n) {
184        return ((SH(b, n)) | (SH(b, n + 2) << 16)) & 0xffffffffL;
185    }
186
187    static final long LL(byte[] b, int n) {
188        return (LG(b, n)) | (LG(b, n + 4) << 32);
189    }
190
191    static final long GETSIG(byte[] b) {
192        return LG(b, 0);
193    }
194
195    // local file (LOC) header fields
196    static final long LOCSIG(byte[] b) { return LG(b, 0); } // signature
197    static final int  LOCVER(byte[] b) { return SH(b, 4); } // version needed to extract
198    static final int  LOCFLG(byte[] b) { return SH(b, 6); } // general purpose bit flags
199    static final int  LOCHOW(byte[] b) { return SH(b, 8); } // compression method
200    static final long LOCTIM(byte[] b) { return LG(b, 10);} // modification time
201    static final long LOCCRC(byte[] b) { return LG(b, 14);} // crc of uncompressed data
202    static final long LOCSIZ(byte[] b) { return LG(b, 18);} // compressed data size
203    static final long LOCLEN(byte[] b) { return LG(b, 22);} // uncompressed data size
204    static final int  LOCNAM(byte[] b) { return SH(b, 26);} // filename length
205    static final int  LOCEXT(byte[] b) { return SH(b, 28);} // extra field length
206
207    // extra local (EXT) header fields
208    static final long EXTCRC(byte[] b) { return LG(b, 4);}  // crc of uncompressed data
209    static final long EXTSIZ(byte[] b) { return LG(b, 8);}  // compressed size
210    static final long EXTLEN(byte[] b) { return LG(b, 12);} // uncompressed size
211
212    // end of central directory header (END) fields
213    static final int  ENDSUB(byte[] b) { return SH(b, 8); }  // number of entries on this disk
214    static final int  ENDTOT(byte[] b) { return SH(b, 10);}  // total number of entries
215    static final long ENDSIZ(byte[] b) { return LG(b, 12);}  // central directory size
216    static final long ENDOFF(byte[] b) { return LG(b, 16);}  // central directory offset
217    static final int  ENDCOM(byte[] b) { return SH(b, 20);}  // size of zip file comment
218    static final int  ENDCOM(byte[] b, int off) { return SH(b, off + 20);}
219
220    // zip64 end of central directory recoder fields
221    static final long ZIP64_ENDTOD(byte[] b) { return LL(b, 24);}  // total number of entries on disk
222    static final long ZIP64_ENDTOT(byte[] b) { return LL(b, 32);}  // total number of entries
223    static final long ZIP64_ENDSIZ(byte[] b) { return LL(b, 40);}  // central directory size
224    static final long ZIP64_ENDOFF(byte[] b) { return LL(b, 48);}  // central directory offset
225    static final long ZIP64_LOCOFF(byte[] b) { return LL(b, 8);}   // zip64 end offset
226
227    // central directory header (CEN) fields
228    static final long CENSIG(byte[] b, int pos) { return LG(b, pos + 0); }
229    static final int  CENVEM(byte[] b, int pos) { return SH(b, pos + 4); }
230    static final int  CENVER(byte[] b, int pos) { return SH(b, pos + 6); }
231    static final int  CENFLG(byte[] b, int pos) { return SH(b, pos + 8); }
232    static final int  CENHOW(byte[] b, int pos) { return SH(b, pos + 10);}
233    static final long CENTIM(byte[] b, int pos) { return LG(b, pos + 12);}
234    static final long CENCRC(byte[] b, int pos) { return LG(b, pos + 16);}
235    static final long CENSIZ(byte[] b, int pos) { return LG(b, pos + 20);}
236    static final long CENLEN(byte[] b, int pos) { return LG(b, pos + 24);}
237    static final int  CENNAM(byte[] b, int pos) { return SH(b, pos + 28);}
238    static final int  CENEXT(byte[] b, int pos) { return SH(b, pos + 30);}
239    static final int  CENCOM(byte[] b, int pos) { return SH(b, pos + 32);}
240    static final int  CENDSK(byte[] b, int pos) { return SH(b, pos + 34);}
241    static final int  CENATT(byte[] b, int pos) { return SH(b, pos + 36);}
242    static final long CENATX(byte[] b, int pos) { return LG(b, pos + 38);}
243    static final long CENOFF(byte[] b, int pos) { return LG(b, pos + 42);}
244
245    // The END header is followed by a variable length comment of size < 64k.
246    static final long END_MAXLEN = 0xFFFF + ENDHDR;
247    static final int READBLOCKSZ = 128;
248}
249