001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 *
017 */
018package org.apache.commons.compress.archivers.zip;
019
020import java.util.Date;
021import java.util.zip.ZipException;
022
023/**
024 * NTFS extra field that was thought to store various attributes but
025 * in reality only stores timestamps.
026 *
027 * <pre>
028 *    4.5.5 -NTFS Extra Field (0x000a):
029 *
030 *       The following is the layout of the NTFS attributes
031 *       "extra" block. (Note: At this time the Mtime, Atime
032 *       and Ctime values MAY be used on any WIN32 system.)
033 *
034 *       Note: all fields stored in Intel low-byte/high-byte order.
035 *
036 *         Value      Size       Description
037 *         -----      ----       -----------
038 * (NTFS)  0x000a     2 bytes    Tag for this "extra" block type
039 *         TSize      2 bytes    Size of the total "extra" block
040 *         Reserved   4 bytes    Reserved for future use
041 *         Tag1       2 bytes    NTFS attribute tag value #1
042 *         Size1      2 bytes    Size of attribute #1, in bytes
043 *         (var)      Size1      Attribute #1 data
044 *          .
045 *          .
046 *          .
047 *          TagN       2 bytes    NTFS attribute tag value #N
048 *          SizeN      2 bytes    Size of attribute #N, in bytes
049 *          (var)      SizeN      Attribute #N data
050 *
051 *        For NTFS, values for Tag1 through TagN are as follows:
052 *        (currently only one set of attributes is defined for NTFS)
053 *
054 *          Tag        Size       Description
055 *          -----      ----       -----------
056 *          0x0001     2 bytes    Tag for attribute #1
057 *          Size1      2 bytes    Size of attribute #1, in bytes
058 *          Mtime      8 bytes    File last modification time
059 *          Atime      8 bytes    File last access time
060 *          Ctime      8 bytes    File creation time
061 * </pre>
062 *
063 * @since 1.11
064 * @NotThreadSafe
065 */
066public class X000A_NTFS implements ZipExtraField {
067    private static final ZipShort HEADER_ID = new ZipShort(0x000a);
068    private static final ZipShort TIME_ATTR_TAG = new ZipShort(0x0001);
069    private static final ZipShort TIME_ATTR_SIZE = new ZipShort(3 * 8);
070
071    private ZipEightByteInteger modifyTime = ZipEightByteInteger.ZERO;
072    private ZipEightByteInteger accessTime = ZipEightByteInteger.ZERO;
073    private ZipEightByteInteger createTime = ZipEightByteInteger.ZERO;
074
075    /**
076     * The Header-ID.
077     *
078     * @return the value for the header id for this extrafield
079     */
080    public ZipShort getHeaderId() {
081        return HEADER_ID;
082    }
083
084    /**
085     * Length of the extra field in the local file data - without
086     * Header-ID or length specifier.
087     *
088     * @return a <code>ZipShort</code> for the length of the data of this extra field
089     */
090    public ZipShort getLocalFileDataLength() {
091        return new ZipShort(4 /* reserved */
092                            + 2 /* Tag#1 */
093                            + 2 /* Size#1 */
094                            + 3 * 8 /* time values */);
095    }
096
097    /**
098     * Length of the extra field in the local file data - without
099     * Header-ID or length specifier.
100     *
101     * <p>For X5455 the central length is often smaller than the
102     * local length, because central cannot contain access or create
103     * timestamps.</p>
104     *
105     * @return a <code>ZipShort</code> for the length of the data of this extra field
106     */
107    public ZipShort getCentralDirectoryLength() {
108        return getLocalFileDataLength();
109    }
110
111    /**
112     * The actual data to put into local file data - without Header-ID
113     * or length specifier.
114     *
115     * @return get the data
116     */
117    public byte[] getLocalFileDataData() {
118        byte[] data = new byte[getLocalFileDataLength().getValue()];
119        int pos = 4;
120        System.arraycopy(TIME_ATTR_TAG.getBytes(), 0, data, pos, 2);
121        pos += 2;
122        System.arraycopy(TIME_ATTR_SIZE.getBytes(), 0, data, pos, 2);
123        pos += 2;
124        System.arraycopy(modifyTime.getBytes(), 0, data, pos, 8);
125        pos += 8;
126        System.arraycopy(accessTime.getBytes(), 0, data, pos, 8);
127        pos += 8;
128        System.arraycopy(createTime.getBytes(), 0, data, pos, 8);
129        return data;
130    }
131
132    /**
133     * The actual data to put into central directory data - without Header-ID
134     * or length specifier.
135     *
136     * @return the central directory data
137     */
138    public byte[] getCentralDirectoryData() {
139        return getLocalFileDataData();
140    }
141
142    /**
143     * Populate data from this array as if it was in local file data.
144     *
145     * @param data   an array of bytes
146     * @param offset the start offset
147     * @param length the number of bytes in the array from offset
148     * @throws java.util.zip.ZipException on error
149     */
150    public void parseFromLocalFileData(
151            byte[] data, int offset, int length
152    ) throws ZipException {
153        final int len = offset + length;
154
155        // skip reserved
156        offset += 4;
157
158        while (offset + 4 <= len) {
159            ZipShort tag = new ZipShort(data, offset);
160            offset += 2;
161            if (tag.equals(TIME_ATTR_TAG)) {
162                readTimeAttr(data, offset, len - offset);
163                break;
164            }
165            ZipShort size = new ZipShort(data, offset);
166            offset += 2 + size.getValue();
167        }
168    }
169
170    /**
171     * Doesn't do anything special since this class always uses the
172     * same parsing logic for both central directory and local file data.
173     */
174    public void parseFromCentralDirectoryData(
175            byte[] buffer, int offset, int length
176    ) throws ZipException {
177        reset();
178        parseFromLocalFileData(buffer, offset, length);
179    }
180
181    /**
182     * Returns the "File last modification time" of this zip entry as
183     * a ZipEightByteInteger object, or {@link
184     * ZipEightByteInteger#ZERO} if no such timestamp exists in the
185     * zip entry.
186     *
187     * @return File last modification time
188     */
189    public ZipEightByteInteger getModifyTime() { return modifyTime; }
190
191    /**
192     * Returns the "File last access time" of this zip entry as a
193     * ZipEightByteInteger object, or {@link ZipEightByteInteger#ZERO}
194     * if no such timestamp exists in the zip entry.
195     *
196     * @return File last access time
197     */
198    public ZipEightByteInteger getAccessTime() { return accessTime; }
199
200    /**
201     * Returns the "File creation time" of this zip entry as a
202     * ZipEightByteInteger object, or {@link ZipEightByteInteger#ZERO}
203     * if no such timestamp exists in the zip entry.
204     *
205     * @return File creation time
206     */
207    public ZipEightByteInteger getCreateTime() { return createTime; }
208
209    /**
210     * Returns the modify time as a java.util.Date
211     * of this zip entry, or null if no such timestamp exists in the zip entry.
212     *
213     * @return modify time as java.util.Date or null.
214     */
215    public Date getModifyJavaTime() {
216        return zipToDate(modifyTime);
217    }
218
219    /**
220     * Returns the access time as a java.util.Date
221     * of this zip entry, or null if no such timestamp exists in the zip entry.
222     *
223     * @return access time as java.util.Date or null.
224     */
225    public Date getAccessJavaTime() {
226        return zipToDate(accessTime);
227    }
228
229    /**
230     * Returns the create time as a a java.util.Date of this zip
231     * entry, or null if no such timestamp exists in the zip entry.
232     *
233     * @return create time as java.util.Date or null.
234     */
235    public Date getCreateJavaTime() {
236        return zipToDate(createTime);
237    }
238
239    /**
240     * Sets the File last modification time of this zip entry using a
241     * ZipEightByteInteger object.
242     *
243     * @param t ZipEightByteInteger of the modify time
244     */
245    public void setModifyTime(ZipEightByteInteger t) {
246        modifyTime = t == null ? ZipEightByteInteger.ZERO : t;
247    }
248
249    /**
250     * Sets the File last access time of this zip entry using a
251     * ZipEightByteInteger object.
252     *
253     * @param t ZipEightByteInteger of the access time
254     */
255    public void setAccessTime(ZipEightByteInteger t) {
256        accessTime = t == null ? ZipEightByteInteger.ZERO : t;
257    }
258
259    /**
260     * Sets the File creation time of this zip entry using a
261     * ZipEightByteInteger object.
262     *
263     * @param t ZipEightByteInteger of the create time
264     */
265    public void setCreateTime(ZipEightByteInteger t) {
266        createTime = t == null ? ZipEightByteInteger.ZERO : t;
267    }
268
269    /**
270     * Sets the modify time as a java.util.Date of this zip entry.
271     *
272     * @param d modify time as java.util.Date
273     */
274    public void setModifyJavaTime(Date d) { setModifyTime(dateToZip(d)); }
275
276    /**
277     * Sets the access time as a java.util.Date
278     * of this zip entry.
279     *
280     * @param d access time as java.util.Date
281     */
282    public void setAccessJavaTime(Date d) { setAccessTime(dateToZip(d)); }
283
284    /**
285     * <p>
286     * Sets the create time as a java.util.Date
287     * of this zip entry.  Supplied value is truncated to per-second
288     * precision (milliseconds zeroed-out).
289     * </p><p>
290     * Note: the setters for flags and timestamps are decoupled.
291     * Even if the timestamp is not-null, it will only be written
292     * out if the corresponding bit in the flags is also set.
293     * </p>
294     *
295     * @param d create time as java.util.Date
296     */
297    public void setCreateJavaTime(Date d) { setCreateTime(dateToZip(d)); }
298
299    /**
300     * Returns a String representation of this class useful for
301     * debugging purposes.
302     *
303     * @return A String representation of this class useful for
304     *         debugging purposes.
305     */
306    @Override
307    public String toString() {
308        StringBuilder buf = new StringBuilder();
309        buf.append("0x000A Zip Extra Field:")
310            .append(" Modify:[").append(getModifyJavaTime()).append("] ")
311            .append(" Access:[").append(getAccessJavaTime()).append("] ")
312            .append(" Create:[").append(getCreateJavaTime()).append("] ");
313        return buf.toString();
314    }
315
316    @Override
317    public boolean equals(Object o) {
318        if (o instanceof X000A_NTFS) {
319            X000A_NTFS xf = (X000A_NTFS) o;
320
321            return (modifyTime == xf.modifyTime || (modifyTime != null && modifyTime.equals(xf.modifyTime))) &&
322                    (accessTime == xf.accessTime || (accessTime != null && accessTime.equals(xf.accessTime))) &&
323                    (createTime == xf.createTime || (createTime != null && createTime.equals(xf.createTime)));
324        } else {
325            return false;
326        }
327    }
328
329    @Override
330    public int hashCode() {
331        int hc = -123;
332        if (modifyTime != null) {
333            hc ^= modifyTime.hashCode();
334        }
335        if (accessTime != null) {
336            // Since accessTime is often same as modifyTime,
337            // this prevents them from XOR negating each other.
338            hc ^= Integer.rotateLeft(accessTime.hashCode(), 11);
339        }
340        if (createTime != null) {
341            hc ^= Integer.rotateLeft(createTime.hashCode(), 22);
342        }
343        return hc;
344    }
345
346    /**
347     * Reset state back to newly constructed state.  Helps us make sure
348     * parse() calls always generate clean results.
349     */
350    private void reset() {
351        this.modifyTime = ZipEightByteInteger.ZERO;
352        this.accessTime = ZipEightByteInteger.ZERO;
353        this.createTime = ZipEightByteInteger.ZERO;
354    }
355
356    private void readTimeAttr(byte[] data, int offset, int length) {
357        if (length >= 2 + 3 * 8) {
358            ZipShort tagValueLength = new ZipShort(data, offset);
359            if (TIME_ATTR_SIZE.equals(tagValueLength)) {
360                offset += 2;
361                modifyTime = new ZipEightByteInteger(data, offset);
362                offset += 8;
363                accessTime = new ZipEightByteInteger(data, offset);
364                offset += 8;
365                createTime = new ZipEightByteInteger(data, offset);
366            }
367        }
368    }
369
370    // https://msdn.microsoft.com/en-us/library/windows/desktop/ms724290%28v=vs.85%29.aspx
371    // A file time is a 64-bit value that represents the number of
372    // 100-nanosecond intervals that have elapsed since 12:00
373    // A.M. January 1, 1601 Coordinated Universal Time (UTC).
374    // this is the offset of Windows time 0 to Unix epoch in 100-nanosecond intervals
375    private static final long EPOCH_OFFSET = -116444736000000000L;
376
377    private static ZipEightByteInteger dateToZip(final Date d) {
378        if (d == null) { return null; }
379        return new ZipEightByteInteger((d.getTime() * 10000l) - EPOCH_OFFSET);
380    }
381
382    private static Date zipToDate(ZipEightByteInteger z) {
383        if (z == null || ZipEightByteInteger.ZERO.equals(z)) { return null; }
384        long l = (z.getLongValue() + EPOCH_OFFSET) / 10000l;
385        return new Date(l);
386    }
387
388}