    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 */
    017package org.apache.commons.compress.archivers.zip;
    018
    019import java.nio.file.attribute.FileTime;
    020import java.util.Date;
    021import java.util.Objects;
    022import java.util.zip.ZipException;
    023
    024import org.apache.commons.compress.utils.TimeUtils;
    025
    026/**
    027 * NTFS extra field that was thought to store various attributes but
    028 * in reality only stores timestamps.
    029 *
    030 * <pre>
    031 *    4.5.5 -NTFS Extra Field (0x000a):
    032 *
    033 *       The following is the layout of the NTFS attributes
    034 *       "extra" block. (Note: At this time the Mtime, Atime
    035 *       and Ctime values MAY be used on any WIN32 system.)
    036 *
    037 *       Note: all fields stored in Intel low-byte/high-byte order.
    038 *
    039 *         Value      Size       Description
    040 *         -----      ----       -----------
    041 * (NTFS)  0x000a     2 bytes    Tag for this "extra" block type
    042 *         TSize      2 bytes    Size of the total "extra" block
    043 *         Reserved   4 bytes    Reserved for future use
    044 *         Tag1       2 bytes    NTFS attribute tag value #1
    045 *         Size1      2 bytes    Size of attribute #1, in bytes
    046 *         (var)      Size1      Attribute #1 data
    047 *          .
    048 *          .
    049 *          .
    050 *          TagN       2 bytes    NTFS attribute tag value #N
    051 *          SizeN      2 bytes    Size of attribute #N, in bytes
    052 *          (var)      SizeN      Attribute #N data
    053 *
    054 *        For NTFS, values for Tag1 through TagN are as follows:
    055 *        (currently only one set of attributes is defined for NTFS)
    056 *
    057 *          Tag        Size       Description
    058 *          -----      ----       -----------
    059 *          0x0001     2 bytes    Tag for attribute #1
    060 *          Size1      2 bytes    Size of attribute #1, in bytes
    061 *          Mtime      8 bytes    File last modification time
    062 *          Atime      8 bytes    File last access time
    063 *          Ctime      8 bytes    File creation time
    064 * </pre>
    065 *
    066 * @since 1.11
    067 * @NotThreadSafe
    068 */
    069public class X000A_NTFS implements ZipExtraField {
    070
    071    /**
    072     * The header ID for this extra field.
    073     *
    074     * @since 1.23
    075     */
    076    public static final ZipShort HEADER_ID = new ZipShort(0x000a);
    077
    078    private static final ZipShort TIME_ATTR_TAG = new ZipShort(0x0001);
    079    private static final ZipShort TIME_ATTR_SIZE = new ZipShort(3 * 8);
    080
    081    private static ZipEightByteInteger dateToZip(final Date d) {
    082        if (d == null) {
    083            return null;
    084        }
    085        return new ZipEightByteInteger(TimeUtils.toNtfsTime(d));
    086    }
    087    private static ZipEightByteInteger fileTimeToZip(final FileTime time) {
    088        if (time == null) {
    089            return null;
    090        }
    091        return new ZipEightByteInteger(TimeUtils.toNtfsTime(time));
    092    }
    093    private static Date zipToDate(final ZipEightByteInteger z) {
    094        if (z == null || ZipEightByteInteger.ZERO.equals(z)) {
    095            return null;
    096        }
    097        return TimeUtils.ntfsTimeToDate(z.getLongValue());
    098    }
    099
    100    private static FileTime zipToFileTime(final ZipEightByteInteger z) {
    101        if (z == null || ZipEightByteInteger.ZERO.equals(z)) {
    102            return null;
    103        }
    104        return TimeUtils.ntfsTimeToFileTime(z.getLongValue());
    105    }
    106
    107    private ZipEightByteInteger modifyTime = ZipEightByteInteger.ZERO;
    108
    109    private ZipEightByteInteger accessTime = ZipEightByteInteger.ZERO;
    110
    111    private ZipEightByteInteger createTime = ZipEightByteInteger.ZERO;
    112
    113    @Override
    114    public boolean equals(final Object o) {
    115        if (o instanceof X000A_NTFS) {
    116            final X000A_NTFS xf = (X000A_NTFS) o;
    117
    118            return Objects.equals(modifyTime, xf.modifyTime) &&
    119                   Objects.equals(accessTime, xf.accessTime) &&
    120                   Objects.equals(createTime, xf.createTime);
    121        }
    122        return false;
    123    }
    124
    125    /**
    126     * Gets the access time as a {@link FileTime}
    127     * of this ZIP entry, or null if no such timestamp exists in the ZIP entry.
    128     *
    129     * @return access time as a {@link FileTime} or null.
    130     * @since 1.23
    131     */
    132    public FileTime getAccessFileTime() {
    133        return zipToFileTime(accessTime);
    134    }
    135
    136    /**
    137     * Gets the access time as a java.util.Date
    138     * of this ZIP entry, or null if no such timestamp exists in the ZIP entry.
    139     *
    140     * @return access time as java.util.Date or null.
    141     */
    142    public Date getAccessJavaTime() {
    143        return zipToDate(accessTime);
    144    }
    145
    146    /**
    147     * Gets the "File last access time" of this ZIP entry as a
    148     * ZipEightByteInteger object, or {@link ZipEightByteInteger#ZERO}
    149     * if no such timestamp exists in the ZIP entry.
    150     *
    151     * @return File last access time
    152     */
    153    public ZipEightByteInteger getAccessTime() { return accessTime; }
    154
    155    /**
    156     * Gets the actual data to put into central directory data - without Header-ID
    157     * or length specifier.
    158     *
    159     * @return the central directory data
    160     */
    161    @Override
    162    public byte[] getCentralDirectoryData() {
    163        return getLocalFileDataData();
    164    }
    165
    166    /**
    167     * Gets the length of the extra field in the local file data - without
    168     * Header-ID or length specifier.
    169     *
    170     * <p>For X5455 the central length is often smaller than the
    171     * local length, because central cannot contain access or create
    172     * timestamps.</p>
    173     *
    174     * @return a {@code ZipShort} for the length of the data of this extra field
    175     */
    176    @Override
    177    public ZipShort getCentralDirectoryLength() {
    178        return getLocalFileDataLength();
    179    }
    180
    181    /**
    182     * Gets the create time as a {@link FileTime}
    183     * of this ZIP entry, or null if no such timestamp exists in the ZIP entry.
    184     *
    185     * @return create time as a {@link FileTime} or null.
    186     * @since 1.23
    187     */
    188    public FileTime getCreateFileTime() {
    189        return zipToFileTime(createTime);
    190    }
    191
    192    /**
    193     * Gets the create time as a java.util.Date of this ZIP
    194     * entry, or null if no such timestamp exists in the ZIP entry.
    195     *
    196     * @return create time as java.util.Date or null.
    197     */
    198    public Date getCreateJavaTime() {
    199        return zipToDate(createTime);
    200    }
    201
    202    /**
    203     * Gets the "File creation time" of this ZIP entry as a
    204     * ZipEightByteInteger object, or {@link ZipEightByteInteger#ZERO}
    205     * if no such timestamp exists in the ZIP entry.
    206     *
    207     * @return File creation time
    208     */
    209    public ZipEightByteInteger getCreateTime() { return createTime; }
    210
    211    /**
    212     * Gets the Header-ID.
    213     *
    214     * @return the value for the header id for this extrafield
    215     */
    216    @Override
    217    public ZipShort getHeaderId() {
    218        return HEADER_ID;
    219    }
    220
    221    /**
    222     * Gets the actual data to put into local file data - without Header-ID
    223     * or length specifier.
    224     *
    225     * @return get the data
    226     */
    227    @Override
    228    public byte[] getLocalFileDataData() {
    229        final byte[] data = new byte[getLocalFileDataLength().getValue()];
    230        int pos = 4;
    231        System.arraycopy(TIME_ATTR_TAG.getBytes(), 0, data, pos, 2);
    232        pos += 2;
    233        System.arraycopy(TIME_ATTR_SIZE.getBytes(), 0, data, pos, 2);
    234        pos += 2;
    235        System.arraycopy(modifyTime.getBytes(), 0, data, pos, 8);
    236        pos += 8;
    237        System.arraycopy(accessTime.getBytes(), 0, data, pos, 8);
    238        pos += 8;
    239        System.arraycopy(createTime.getBytes(), 0, data, pos, 8);
    240        return data;
    241    }
    242
    243    /**
    244     * Gets the length of the extra field in the local file data - without
    245     * Header-ID or length specifier.
    246     *
    247     * @return a {@code ZipShort} for the length of the data of this extra field
    248     */
    249    @Override
    250    public ZipShort getLocalFileDataLength() {
    251        return new ZipShort(4 /* reserved */
    252                            + 2 /* Tag#1 */
    253                            + 2 /* Size#1 */
    254                            + 3 * 8 /* time values */);
    255    }
    256
    257    /**
    258     * Gets the modify time as a {@link FileTime}
    259     * of this ZIP entry, or null if no such timestamp exists in the ZIP entry.
    260     *
    261     * @return modify time as a {@link FileTime} or null.
    262     * @since 1.23
    263     */
    264    public FileTime getModifyFileTime() {
    265        return zipToFileTime(modifyTime);
    266    }
    267
    268    /**
    269     * Gets the modify time as a java.util.Date
    270     * of this ZIP entry, or null if no such timestamp exists in the ZIP entry.
    271     *
    272     * @return modify time as java.util.Date or null.
    273     */
    274    public Date getModifyJavaTime() {
    275        return zipToDate(modifyTime);
    276    }
    277
    278    /**
    279     * Gets the "File last modification time" of this ZIP entry as
    280     * a ZipEightByteInteger object, or {@link
    281     * ZipEightByteInteger#ZERO} if no such timestamp exists in the
    282     * ZIP entry.
    283     *
    284     * @return File last modification time
    285     */
    286    public ZipEightByteInteger getModifyTime() { return modifyTime; }
    287
    288    @Override
    289    public int hashCode() {
    290        int hc = -123;
    291        if (modifyTime != null) {
    292            hc ^= modifyTime.hashCode();
    293        }
    294        if (accessTime != null) {
    295            // Since accessTime is often same as modifyTime,
    296            // this prevents them from XOR negating each other.
    297            hc ^= Integer.rotateLeft(accessTime.hashCode(), 11);
    298        }
    299        if (createTime != null) {
    300            hc ^= Integer.rotateLeft(createTime.hashCode(), 22);
    301        }
    302        return hc;
    303    }
    304
    305    /**
    306     * Doesn't do anything special since this class always uses the
    307     * same parsing logic for both central directory and local file data.
    308     */
    309    @Override
    310    public void parseFromCentralDirectoryData(
    311            final byte[] buffer, final int offset, final int length
    312    ) throws ZipException {
    313        reset();
    314        parseFromLocalFileData(buffer, offset, length);
    315    }
    316
    317    /**
    318     * Populate data from this array as if it was in local file data.
    319     *
    320     * @param data   an array of bytes
    321     * @param offset the start offset
    322     * @param length the number of bytes in the array from offset
    323     * @throws java.util.zip.ZipException on error
    324     */
    325    @Override
    326    public void parseFromLocalFileData(
    327            final byte[] data, int offset, final int length
    328    ) throws ZipException {
    329        final int len = offset + length;
    330
    331        // skip reserved
    332        offset += 4;
    333
    334        while (offset + 4 <= len) {
    335            final ZipShort tag = new ZipShort(data, offset);
    336            offset += 2;
    337            if (tag.equals(TIME_ATTR_TAG)) {
    338                readTimeAttr(data, offset, len - offset);
    339                break;
    340            }
    341            final ZipShort size = new ZipShort(data, offset);
    342            offset += 2 + size.getValue();
    343        }
    344    }
    345
    346    private void readTimeAttr(final byte[] data, int offset, final int length) {
    347        if (length >= 2 + 3 * 8) {
    348            final ZipShort tagValueLength = new ZipShort(data, offset);
    349            if (TIME_ATTR_SIZE.equals(tagValueLength)) {
    350                offset += 2;
    351                modifyTime = new ZipEightByteInteger(data, offset);
    352                offset += 8;
    353                accessTime = new ZipEightByteInteger(data, offset);
    354                offset += 8;
    355                createTime = new ZipEightByteInteger(data, offset);
    356            }
    357        }
    358    }
    359
    360    /**
    361     * Reset state back to newly constructed state.  Helps us make sure
    362     * parse() calls always generate clean results.
    363     */
    364    private void reset() {
    365        this.modifyTime = ZipEightByteInteger.ZERO;
    366        this.accessTime = ZipEightByteInteger.ZERO;
    367        this.createTime = ZipEightByteInteger.ZERO;
    368    }
    369
    370    /**
    371     * Sets the access time.
    372     *
    373     * @param time access time as a {@link FileTime}
    374     * @since 1.23
    375     */
    376    public void setAccessFileTime(final FileTime time) {
    377        setAccessTime(fileTimeToZip(time));
    378    }
    379
    380    /**
    381     * Sets the access time as a java.util.Date
    382     * of this ZIP entry.
    383     *
    384     * @param d access time as java.util.Date
    385     */
    386    public void setAccessJavaTime(final Date d) { setAccessTime(dateToZip(d)); }
    387
    388    /**
    389     * Sets the File last access time of this ZIP entry using a
    390     * ZipEightByteInteger object.
    391     *
    392     * @param t ZipEightByteInteger of the access time
    393     */
    394    public void setAccessTime(final ZipEightByteInteger t) {
    395        accessTime = t == null ? ZipEightByteInteger.ZERO : t;
    396    }
    397
    398    /**
    399     * Sets the create time.
    400     *
    401     * @param time create time as a {@link FileTime}
    402     * @since 1.23
    403     */
    404    public void setCreateFileTime(final FileTime time) {
    405        setCreateTime(fileTimeToZip(time));
    406    }
    407
    408    /**
    409     * <p>
    410     * Sets the create time as a java.util.Date
    411     * of this ZIP entry.  Supplied value is truncated to per-second
    412     * precision (milliseconds zeroed-out).
    413     * </p><p>
    414     * Note: the setters for flags and timestamps are decoupled.
    415     * Even if the timestamp is not-null, it will only be written
    416     * out if the corresponding bit in the flags is also set.
    417     * </p>
    418     *
    419     * @param d create time as java.util.Date
    420     */
    421    public void setCreateJavaTime(final Date d) { setCreateTime(dateToZip(d)); }
    422
    423    /**
    424     * Sets the File creation time of this ZIP entry using a
    425     * ZipEightByteInteger object.
    426     *
    427     * @param t ZipEightByteInteger of the create time
    428     */
    429    public void setCreateTime(final ZipEightByteInteger t) {
    430        createTime = t == null ? ZipEightByteInteger.ZERO : t;
    431    }
    432
    433    /**
    434     * Sets the modify time.
    435     *
    436     * @param time modify time as a {@link FileTime}
    437     * @since 1.23
    438     */
    439    public void setModifyFileTime(final FileTime time) {
    440        setModifyTime(fileTimeToZip(time));
    441    }
    442
    443    /**
    444     * Sets the modify time as a java.util.Date of this ZIP entry.
    445     *
    446     * @param d modify time as java.util.Date
    447     */
    448    public void setModifyJavaTime(final Date d) { setModifyTime(dateToZip(d)); }
    449
    450    /**
    451     * Sets the File last modification time of this ZIP entry using a
    452     * ZipEightByteInteger object.
    453     *
    454     * @param t ZipEightByteInteger of the modify time
    455     */
    456    public void setModifyTime(final ZipEightByteInteger t) {
    457        modifyTime = t == null ? ZipEightByteInteger.ZERO : t;
    458    }
    459
    460    /**
    461     * Returns a String representation of this class useful for
    462     * debugging purposes.
    463     *
    464     * @return A String representation of this class useful for
    465     *         debugging purposes.
    466     */
    467    @Override
    468    public String toString() {
    469        final StringBuilder buf = new StringBuilder();
    470        buf.append("0x000A Zip Extra Field:")
    471            .append(" Modify:[").append(getModifyFileTime()).append("] ")
    472            .append(" Access:[").append(getAccessFileTime()).append("] ")
    473            .append(" Create:[").append(getCreateFileTime()).append("] ");
    474        return buf.toString();
    475    }
    476}