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 */
019package org.apache.commons.compress.archivers.zip;
020
021import java.io.Serializable;
022import java.math.BigInteger;
023import java.util.zip.ZipException;
024
025import static org.apache.commons.compress.archivers.zip.ZipUtil.reverse;
026import static org.apache.commons.compress.archivers.zip.ZipUtil.signedByteToUnsignedInt;
027import static org.apache.commons.compress.archivers.zip.ZipUtil.unsignedIntToSignedByte;
028
029/**
030 * An extra field that stores UNIX UID/GID data (owner & group ownership) for a given
031 * zip entry.  We're using the field definition given in Info-Zip's source archive:
032 * zip-3.0.tar.gz/proginfo/extrafld.txt
033 *
034 * <pre>
035 * Local-header version:
036 *
037 * Value         Size        Description
038 * -----         ----        -----------
039 * 0x7875        Short       tag for this extra block type ("ux")
040 * TSize         Short       total data size for this block
041 * Version       1 byte      version of this extra field, currently 1
042 * UIDSize       1 byte      Size of UID field
043 * UID           Variable    UID for this entry (little endian)
044 * GIDSize       1 byte      Size of GID field
045 * GID           Variable    GID for this entry (little endian)
046 *
047 * Central-header version:
048 *
049 * Value         Size        Description
050 * -----         ----        -----------
051 * 0x7855        Short       tag for this extra block type ("Ux")
052 * TSize         Short       total data size for this block (0)
053 * </pre>
054 * @since 1.5
055 */
056public class X7875_NewUnix implements ZipExtraField, Cloneable, Serializable {
057    private static final ZipShort HEADER_ID = new ZipShort(0x7875);
058    private static final ZipShort ZERO = new ZipShort(0);
059    private static final BigInteger ONE_THOUSAND = BigInteger.valueOf(1000);
060    private static final long serialVersionUID = 1L;
061
062    private int version = 1; // always '1' according to current info-zip spec.
063
064    // BigInteger helps us with little-endian / big-endian conversions.
065    // (thanks to BigInteger.toByteArray() and a reverse() method we created).
066    // Also, the spec theoretically allows UID/GID up to 255 bytes long!
067    //
068    // NOTE:  equals() and hashCode() currently assume these can never be null.
069    private BigInteger uid;
070    private BigInteger gid;
071
072    /**
073     * Constructor for X7875_NewUnix.
074     */
075    public X7875_NewUnix() {
076        reset();
077    }
078
079    /**
080     * The Header-ID.
081     *
082     * @return the value for the header id for this extrafield
083     */
084    public ZipShort getHeaderId() {
085        return HEADER_ID;
086    }
087
088    /**
089     * Gets the UID as a long.  UID is typically a 32 bit unsigned
090     * value on most UNIX systems, so we return a long to avoid
091     * integer overflow into the negatives in case values above
092     * and including 2^31 are being used.
093     *
094     * @return the UID value.
095     */
096    public long getUID() { return ZipUtil.bigToLong(uid); }
097
098    /**
099     * Gets the GID as a long.  GID is typically a 32 bit unsigned
100     * value on most UNIX systems, so we return a long to avoid
101     * integer overflow into the negatives in case values above
102     * and including 2^31 are being used.
103     *
104     * @return the GID value.
105     */
106    public long getGID() { return ZipUtil.bigToLong(gid); }
107
108    /**
109     * Sets the UID.
110     *
111     * @param l UID value to set on this extra field.
112     */
113    public void setUID(long l) {
114        this.uid = ZipUtil.longToBig(l);
115    }
116
117    /**
118     * Sets the GID.
119     *
120     * @param l GID value to set on this extra field.
121     */
122    public void setGID(long l) {
123        this.gid = ZipUtil.longToBig(l);
124    }
125
126    /**
127     * Length of the extra field in the local file data - without
128     * Header-ID or length specifier.
129     *
130     * @return a <code>ZipShort</code> for the length of the data of this extra field
131     */
132    public ZipShort getLocalFileDataLength() {
133        int uidSize = trimLeadingZeroesForceMinLength(uid.toByteArray()).length;
134        int gidSize = trimLeadingZeroesForceMinLength(gid.toByteArray()).length;
135
136        // The 3 comes from:  version=1 + uidsize=1 + gidsize=1
137        return new ZipShort(3 + uidSize + gidSize);
138    }
139
140    /**
141     * Length of the extra field in the central directory data - without
142     * Header-ID or length specifier.
143     *
144     * @return a <code>ZipShort</code> for the length of the data of this extra field
145     */
146    public ZipShort getCentralDirectoryLength() {
147        return ZERO;
148    }
149
150    /**
151     * The actual data to put into local file data - without Header-ID
152     * or length specifier.
153     *
154     * @return get the data
155     */
156    public byte[] getLocalFileDataData() {
157        byte[] uidBytes = uid.toByteArray();
158        byte[] gidBytes = gid.toByteArray();
159
160        // BigInteger might prepend a leading-zero to force a positive representation
161        // (e.g., so that the sign-bit is set to zero).  We need to remove that
162        // before sending the number over the wire.
163        uidBytes = trimLeadingZeroesForceMinLength(uidBytes);
164        gidBytes = trimLeadingZeroesForceMinLength(gidBytes);
165
166        // Couldn't bring myself to just call getLocalFileDataLength() when we've
167        // already got the arrays right here.  Yeah, yeah, I know, premature
168        // optimization is the root of all...
169        //
170        // The 3 comes from:  version=1 + uidsize=1 + gidsize=1
171        byte[] data = new byte[3 + uidBytes.length + gidBytes.length];
172
173        // reverse() switches byte array from big-endian to little-endian.
174        reverse(uidBytes);
175        reverse(gidBytes);
176
177        int pos = 0;
178        data[pos++] = unsignedIntToSignedByte(version);
179        data[pos++] = unsignedIntToSignedByte(uidBytes.length);
180        System.arraycopy(uidBytes, 0, data, pos, uidBytes.length);
181        pos += uidBytes.length;
182        data[pos++] = unsignedIntToSignedByte(gidBytes.length);
183        System.arraycopy(gidBytes, 0, data, pos, gidBytes.length);
184        return data;
185    }
186
187    /**
188     * The actual data to put into central directory data - without Header-ID
189     * or length specifier.
190     *
191     * @return get the data
192     */
193    public byte[] getCentralDirectoryData() {
194        return new byte[0];
195    }
196
197    /**
198     * Populate data from this array as if it was in local file data.
199     *
200     * @param data   an array of bytes
201     * @param offset the start offset
202     * @param length the number of bytes in the array from offset
203     * @throws java.util.zip.ZipException on error
204     */
205    public void parseFromLocalFileData(
206            byte[] data, int offset, int length
207    ) throws ZipException {
208        reset();
209        this.version = signedByteToUnsignedInt(data[offset++]);
210        int uidSize = signedByteToUnsignedInt(data[offset++]);
211        byte[] uidBytes = new byte[uidSize];
212        System.arraycopy(data, offset, uidBytes, 0, uidSize);
213        offset += uidSize;
214        this.uid = new BigInteger(1, reverse(uidBytes)); // sign-bit forced positive
215
216        int gidSize = signedByteToUnsignedInt(data[offset++]);
217        byte[] gidBytes = new byte[gidSize];
218        System.arraycopy(data, offset, gidBytes, 0, gidSize);
219        this.gid = new BigInteger(1, reverse(gidBytes)); // sign-bit forced positive
220    }
221
222    /**
223     * Doesn't do anything since this class doesn't store anything
224     * inside the central directory.
225     */
226    public void parseFromCentralDirectoryData(
227            byte[] buffer, int offset, int length
228    ) throws ZipException {
229    }
230
231    /**
232     * Reset state back to newly constructed state.  Helps us make sure
233     * parse() calls always generate clean results.
234     */
235    private void reset() {
236        // Typical UID/GID of the first non-root user created on a unix system.
237        uid = ONE_THOUSAND;
238        gid = ONE_THOUSAND;
239    }
240
241    /**
242     * Returns a String representation of this class useful for
243     * debugging purposes.
244     *
245     * @return A String representation of this class useful for
246     *         debugging purposes.
247     */
248    @Override
249    public String toString() {
250        return "0x7875 Zip Extra Field: UID=" + uid + " GID=" + gid;
251    }
252
253    @Override
254    public Object clone() throws CloneNotSupportedException {
255        return super.clone();
256    }
257
258    @Override
259    public boolean equals(Object o) {
260        if (o instanceof X7875_NewUnix) {
261            X7875_NewUnix xf = (X7875_NewUnix) o;
262            // We assume uid and gid can never be null.
263            return version == xf.version && uid.equals(xf.uid) && gid.equals(xf.gid);
264        }
265        return false;
266    }
267
268    @Override
269    public int hashCode() {
270        int hc = -1234567 * version;
271        // Since most UID's and GID's are below 65,536, this is (hopefully!)
272        // a nice way to make sure typical UID and GID values impact the hash
273        // as much as possible.
274        hc ^= Integer.rotateLeft(uid.hashCode(), 16);
275        hc ^= gid.hashCode();
276        return hc;
277    }
278
279    /**
280     * Not really for external usage, but marked "package" visibility
281     * to help us JUnit it.   Trims a byte array of leading zeroes while
282     * also enforcing a minimum length, and thus it really trims AND pads
283     * at the same time.
284     *
285     * @param array byte[] array to trim & pad.
286     * @return trimmed & padded byte[] array.
287     */
288    static byte[] trimLeadingZeroesForceMinLength(byte[] array) {
289        if (array == null) {
290            return array;
291        }
292
293        int pos = 0;
294        for (byte b : array) {
295            if (b == 0) {
296                pos++;
297            } else {
298                break;
299            }
300        }
301
302        /*
303
304        I agonized over my choice of MIN_LENGTH=1.  Here's the situation:
305        InfoZip (the tool I am using to test interop) always sets these
306        to length=4.  And so a UID of 0 (typically root) for example is
307        encoded as {4,0,0,0,0} (len=4, 32 bits of zero), when it could just
308        as easily be encoded as {1,0} (len=1, 8 bits of zero) according to
309        the spec.
310
311        In the end I decided on MIN_LENGTH=1 for four reasons:
312
313        1.)  We are adhering to the spec as far as I can tell, and so
314             a consumer that cannot parse this is broken.
315
316        2.)  Fundamentally, zip files are about shrinking things, so
317             let's save a few bytes per entry while we can.
318
319        3.)  Of all the people creating zip files using commons-
320             compress, how many care about UNIX UID/GID attributes
321             of the files they store?   (e.g., I am probably thinking
322             way too hard about this and no one cares!)
323
324        4.)  InfoZip's tool, even though it carefully stores every UID/GID
325             for every file zipped on a unix machine (by default) currently
326             appears unable to ever restore UID/GID.
327             unzip -X has no effect on my machine, even when run as root!!!!
328
329        And thus it is decided:  MIN_LENGTH=1.
330
331        If anyone runs into interop problems from this, feel free to set
332        it to MIN_LENGTH=4 at some future time, and then we will behave
333        exactly like InfoZip (requires changes to unit tests, though).
334
335        And I am sorry that the time you spent reading this comment is now
336        gone and you can never have it back.
337
338        */
339        final int MIN_LENGTH = 1;
340
341        byte[] trimmedArray = new byte[Math.max(MIN_LENGTH, array.length - pos)];
342        int startPos = trimmedArray.length - (array.length - pos);
343        System.arraycopy(array, pos, trimmedArray, startPos, trimmedArray.length - startPos);
344        return trimmedArray;
345    }
346}