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.io.Serializable;
021
022import static org.apache.commons.compress.archivers.zip.ZipConstants.BYTE_MASK;
023import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD;
024
025/**
026 * Utility class that represents a four byte integer with conversion
027 * rules for the big endian byte order of ZIP files.
028 * @Immutable
029 */
030public final class ZipLong implements Cloneable, Serializable {
031    private static final long serialVersionUID = 1L;
032
033    //private static final int BYTE_BIT_SIZE = 8;
034
035    private static final int BYTE_1 = 1;
036    private static final int BYTE_1_MASK = 0xFF00;
037    private static final int BYTE_1_SHIFT = 8;
038
039    private static final int BYTE_2 = 2;
040    private static final int BYTE_2_MASK = 0xFF0000;
041    private static final int BYTE_2_SHIFT = 16;
042
043    private static final int BYTE_3 = 3;
044    private static final long BYTE_3_MASK = 0xFF000000L;
045    private static final int BYTE_3_SHIFT = 24;
046
047    private final long value;
048
049    /** Central File Header Signature */
050    public static final ZipLong CFH_SIG = new ZipLong(0X02014B50L);
051
052    /** Local File Header Signature */
053    public static final ZipLong LFH_SIG = new ZipLong(0X04034B50L);
054
055    /**
056     * Data Descriptor signature.
057     *
058     * <p>Actually, PKWARE uses this as marker for split/spanned
059     * archives and other archivers have started to use it as Data
060     * Descriptor signature (as well).</p>
061     * @since 1.1
062     */
063    public static final ZipLong DD_SIG = new ZipLong(0X08074B50L);
064
065    /**
066     * Value stored in size and similar fields if ZIP64 extensions are
067     * used.
068     * @since 1.3
069     */
070    static final ZipLong ZIP64_MAGIC = new ZipLong(ZipConstants.ZIP64_MAGIC);
071
072    /**
073     * Marks ZIP archives that were supposed to be split or spanned
074     * but only needed a single segment in then end (so are actually
075     * neither split nor spanned).
076     *
077     * <p>This is the "PK00" prefix found in some archives.</p>
078     * @since 1.5
079     */
080    public static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER =
081        new ZipLong(0X30304B50L);
082
083    /**
084     * Archive extra data record signature.
085     * @since 1.5
086     */
087    public static final ZipLong AED_SIG = new ZipLong(0X08064B50L);
088
089    /**
090     * Create instance from a number.
091     * @param value the long to store as a ZipLong
092     */
093    public ZipLong(final long value) {
094        this.value = value;
095    }
096
097    /**
098     * Create instance from bytes.
099     * @param bytes the bytes to store as a ZipLong
100     */
101    public ZipLong (final byte[] bytes) {
102        this(bytes, 0);
103    }
104
105    /**
106     * Create instance from the four bytes starting at offset.
107     * @param bytes the bytes to store as a ZipLong
108     * @param offset the offset to start
109     */
110    public ZipLong (final byte[] bytes, final int offset) {
111        value = ZipLong.getValue(bytes, offset);
112    }
113
114    /**
115     * Get value as four bytes in big endian byte order.
116     * @return value as four bytes in big endian order
117     */
118    public byte[] getBytes() {
119        return ZipLong.getBytes(value);
120    }
121
122    /**
123     * Get value as Java long.
124     * @return value as a long
125     */
126    public long getValue() {
127        return value;
128    }
129
130    /**
131     * Get value as four bytes in big endian byte order.
132     * @param value the value to convert
133     * @return value as four bytes in big endian byte order
134     */
135    public static byte[] getBytes(final long value) {
136        final byte[] result = new byte[WORD];
137        putLong(value, result, 0);
138        return result;
139    }
140
141    /**
142     * put the value as four bytes in big endian byte order.
143     * @param value the Java long to convert to bytes
144     * @param buf the output buffer
145     * @param  offset
146     *         The offset within the output buffer of the first byte to be written.
147     *         must be non-negative and no larger than <tt>buf.length-4</tt>
148     */
149
150    public static void putLong(final long value, final byte[] buf, int offset) {
151        buf[offset++] = (byte) ((value & BYTE_MASK));
152        buf[offset++] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
153        buf[offset++] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT);
154        buf[offset] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT);
155    }
156
157    public void putLong(final byte[] buf, final int offset) {
158        putLong(value, buf, offset);
159    }
160
161    /**
162     * Helper method to get the value as a Java long from four bytes starting at given array offset
163     * @param bytes the array of bytes
164     * @param offset the offset to start
165     * @return the corresponding Java long value
166     */
167    public static long getValue(final byte[] bytes, final int offset) {
168        long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
169        value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
170        value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
171        value += (bytes[offset] & BYTE_MASK);
172        return value;
173    }
174
175    /**
176     * Helper method to get the value as a Java long from a four-byte array
177     * @param bytes the array of bytes
178     * @return the corresponding Java long value
179     */
180    public static long getValue(final byte[] bytes) {
181        return getValue(bytes, 0);
182    }
183
184    /**
185     * Override to make two instances with same value equal.
186     * @param o an object to compare
187     * @return true if the objects are equal
188     */
189    @Override
190    public boolean equals(final Object o) {
191        if (o == null || !(o instanceof ZipLong)) {
192            return false;
193        }
194        return value == ((ZipLong) o).getValue();
195    }
196
197    /**
198     * Override to make two instances with same value equal.
199     * @return the value stored in the ZipLong
200     */
201    @Override
202    public int hashCode() {
203        return (int) value;
204    }
205
206    @Override
207    public Object clone() {
208        try {
209            return super.clone();
210        } catch (final CloneNotSupportedException cnfe) {
211            // impossible
212            throw new RuntimeException(cnfe); //NOSONAR
213        }
214    }
215
216    @Override
217    public String toString() {
218        return "ZipLong value: " + value;
219    }
220}