    001    /*
    002     * Licensed to the Apache Software Foundation (ASF) under one
    003     * or more contributor license agreements.  See the NOTICE file
    004     * distributed with this work for additional information
    005     * regarding copyright ownership.  The ASF licenses this file
    006     * to you under the Apache License, Version 2.0 (the
    007     * "License"); you may not use this file except in compliance
    008     * with the License.  You may obtain a copy of the License at
    009     *
    010     * http://www.apache.org/licenses/LICENSE-2.0
    011     *
    012     * Unless required by applicable law or agreed to in writing,
    013     * software distributed under the License is distributed on an
    014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    015     * KIND, either express or implied.  See the License for the
    016     * specific language governing permissions and limitations
    017     * under the License.
    018     */
    019    package org.apache.commons.compress.archivers.zip;
    020
    021    import java.util.zip.CRC32;
    022    import java.util.zip.ZipException;
    023
    024    /**
    025     * Adds Unix file permission and UID/GID fields as well as symbolic
    026     * link handling.
    027     *
    028     * <p>This class uses the ASi extra field in the format:
    029     * <pre>
    030     *         Value         Size            Description
    031     *         -----         ----            -----------
    032     * (Unix3) 0x756e        Short           tag for this extra block type
    033     *         TSize         Short           total data size for this block
    034     *         CRC           Long            CRC-32 of the remaining data
    035     *         Mode          Short           file permissions
    036     *         SizDev        Long            symlink'd size OR major/minor dev num
    037     *         UID           Short           user ID
    038     *         GID           Short           group ID
    039     *         (var.)        variable        symbolic link filename
    040     * </pre>
    041     * taken from appnote.iz (Info-ZIP note, 981119) found at <a
    042     * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p>
    043
    044     *
    045     * <p>Short is two bytes and Long is four bytes in big endian byte and
    046     * word order, device numbers are currently not supported.</p>
    047     * @NotThreadSafe
    048     *
    049     * <p>Since the documentation this class is based upon doesn't mention
    050     * the character encoding of the file name at all, it is assumed that
    051     * it uses the current platform's default encoding.</p>
    052     */
    053    public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
    054
    055        private static final ZipShort HEADER_ID = new ZipShort(0x756E);
    056        private static final int      WORD = 4;
    057        /**
    058         * Standard Unix stat(2) file mode.
    059         */
    060        private int mode = 0;
    061        /**
    062         * User ID.
    063         */
    064        private int uid = 0;
    065        /**
    066         * Group ID.
    067         */
    068        private int gid = 0;
    069        /**
    070         * File this entry points to, if it is a symbolic link.
    071         *
    072         * <p>empty string - if entry is not a symbolic link.</p>
    073         */
    074        private String link = "";
    075        /**
    076         * Is this an entry for a directory?
    077         */
    078        private boolean dirFlag = false;
    079
    080        /**
    081         * Instance used to calculate checksums.
    082         */
    083        private CRC32 crc = new CRC32();
    084
    085        /** Constructor for AsiExtraField. */
    086        public AsiExtraField() {
    087        }
    088
    089        /**
    090         * The Header-ID.
    091         * @return the value for the header id for this extrafield
    092         */
    093        public ZipShort getHeaderId() {
    094            return HEADER_ID;
    095        }
    096
    097        /**
    098         * Length of the extra field in the local file data - without
    099         * Header-ID or length specifier.
    100         * @return a <code>ZipShort</code> for the length of the data of this extra field
    101         */
    102        public ZipShort getLocalFileDataLength() {
    103            return new ZipShort(WORD         // CRC
    104                              + 2         // Mode
    105                              + WORD         // SizDev
    106                              + 2         // UID
    107                              + 2         // GID
    108                              + getLinkedFile().getBytes().length);
    109                              // Uses default charset - see class Javadoc
    110        }
    111
    112        /**
    113         * Delegate to local file data.
    114         * @return the centralDirectory length
    115         */
    116        public ZipShort getCentralDirectoryLength() {
    117            return getLocalFileDataLength();
    118        }
    119
    120        /**
    121         * The actual data to put into local file data - without Header-ID
    122         * or length specifier.
    123         * @return get the data
    124         */
    125        public byte[] getLocalFileDataData() {
    126            // CRC will be added later
    127            byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
    128            System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
    129
    130            byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc
    131            // CheckStyle:MagicNumber OFF
    132            System.arraycopy(ZipLong.getBytes(linkArray.length),
    133                             0, data, 2, WORD);
    134
    135            System.arraycopy(ZipShort.getBytes(getUserId()),
    136                             0, data, 6, 2);
    137            System.arraycopy(ZipShort.getBytes(getGroupId()),
    138                             0, data, 8, 2);
    139
    140            System.arraycopy(linkArray, 0, data, 10, linkArray.length);
    141            // CheckStyle:MagicNumber ON
    142
    143            crc.reset();
    144            crc.update(data);
    145            long checksum = crc.getValue();
    146
    147            byte[] result = new byte[data.length + WORD];
    148            System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
    149            System.arraycopy(data, 0, result, WORD, data.length);
    150            return result;
    151        }
    152
    153        /**
    154         * Delegate to local file data.
    155         * @return the local file data
    156         */
    157        public byte[] getCentralDirectoryData() {
    158            return getLocalFileDataData();
    159        }
    160
    161        /**
    162         * Set the user id.
    163         * @param uid the user id
    164         */
    165        public void setUserId(int uid) {
    166            this.uid = uid;
    167        }
    168
    169        /**
    170         * Get the user id.
    171         * @return the user id
    172         */
    173        public int getUserId() {
    174            return uid;
    175        }
    176
    177        /**
    178         * Set the group id.
    179         * @param gid the group id
    180         */
    181        public void setGroupId(int gid) {
    182            this.gid = gid;
    183        }
    184
    185        /**
    186         * Get the group id.
    187         * @return the group id
    188         */
    189        public int getGroupId() {
    190            return gid;
    191        }
    192
    193        /**
    194         * Indicate that this entry is a symbolic link to the given filename.
    195         *
    196         * @param name Name of the file this entry links to, empty String
    197         *             if it is not a symbolic link.
    198         */
    199        public void setLinkedFile(String name) {
    200            link = name;
    201            mode = getMode(mode);
    202        }
    203
    204        /**
    205         * Name of linked file
    206         *
    207         * @return name of the file this entry links to if it is a
    208         *         symbolic link, the empty string otherwise.
    209         */
    210        public String getLinkedFile() {
    211            return link;
    212        }
    213
    214        /**
    215         * Is this entry a symbolic link?
    216         * @return true if this is a symbolic link
    217         */
    218        public boolean isLink() {
    219            return getLinkedFile().length() != 0;
    220        }
    221
    222        /**
    223         * File mode of this file.
    224         * @param mode the file mode
    225         */
    226        public void setMode(int mode) {
    227            this.mode = getMode(mode);
    228        }
    229
    230        /**
    231         * File mode of this file.
    232         * @return the file mode
    233         */
    234        public int getMode() {
    235            return mode;
    236        }
    237
    238        /**
    239         * Indicate whether this entry is a directory.
    240         * @param dirFlag if true, this entry is a directory
    241         */
    242        public void setDirectory(boolean dirFlag) {
    243            this.dirFlag = dirFlag;
    244            mode = getMode(mode);
    245        }
    246
    247        /**
    248         * Is this entry a directory?
    249         * @return true if this entry is a directory
    250         */
    251        public boolean isDirectory() {
    252            return dirFlag && !isLink();
    253        }
    254
    255        /**
    256         * Populate data from this array as if it was in local file data.
    257         * @param data an array of bytes
    258         * @param offset the start offset
    259         * @param length the number of bytes in the array from offset
    260         * @throws ZipException on error
    261         */
    262        public void parseFromLocalFileData(byte[] data, int offset, int length)
    263            throws ZipException {
    264
    265            long givenChecksum = ZipLong.getValue(data, offset);
    266            byte[] tmp = new byte[length - WORD];
    267            System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
    268            crc.reset();
    269            crc.update(tmp);
    270            long realChecksum = crc.getValue();
    271            if (givenChecksum != realChecksum) {
    272                throw new ZipException("bad CRC checksum "
    273                                       + Long.toHexString(givenChecksum)
    274                                       + " instead of "
    275                                       + Long.toHexString(realChecksum));
    276            }
    277
    278            int newMode = ZipShort.getValue(tmp, 0);
    279            // CheckStyle:MagicNumber OFF
    280            byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
    281            uid = ZipShort.getValue(tmp, 6);
    282            gid = ZipShort.getValue(tmp, 8);
    283
    284            if (linkArray.length == 0) {
    285                link = "";
    286            } else {
    287                System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
    288                link = new String(linkArray); // Uses default charset - see class Javadoc
    289            }
    290            // CheckStyle:MagicNumber ON
    291            setDirectory((newMode & DIR_FLAG) != 0);
    292            setMode(newMode);
    293        }
    294
    295        /**
    296         * Doesn't do anything special since this class always uses the
    297         * same data in central directory and local file data.
    298         */
    299        public void parseFromCentralDirectoryData(byte[] buffer, int offset,
    300                                                  int length)
    301            throws ZipException {
    302            parseFromLocalFileData(buffer, offset, length);
    303        }
    304
    305        /**
    306         * Get the file mode for given permissions with the correct file type.
    307         * @param mode the mode
    308         * @return the type with the mode
    309         */
    310        protected int getMode(int mode) {
    311            int type = FILE_FLAG;
    312            if (isLink()) {
    313                type = LINK_FLAG;
    314            } else if (isDirectory()) {
    315                type = DIR_FLAG;
    316            }
    317            return type | (mode & PERM_MASK);
    318        }
    319
    320        @Override
    321        public Object clone() {
    322            try {
    323                AsiExtraField cloned = (AsiExtraField) super.clone();
    324                cloned.crc = new CRC32();
    325                return cloned;
    326            } catch (CloneNotSupportedException cnfe) {
    327                // impossible
    328                throw new RuntimeException(cnfe);
    329            }
    330        }
    331    }