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(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 (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 (byte[] bytes, 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(long value) {
136        byte[] result = new byte[WORD];
137        result[0] = (byte) ((value & BYTE_MASK));
138        result[BYTE_1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
139        result[BYTE_2] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT);
140        result[BYTE_3] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT);
141        return result;
142    }
143
144    /**
145     * Helper method to get the value as a Java long from four bytes starting at given array offset
146     * @param bytes the array of bytes
147     * @param offset the offset to start
148     * @return the corresponding Java long value
149     */
150    public static long getValue(byte[] bytes, int offset) {
151        long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
152        value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
153        value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
154        value += (bytes[offset] & BYTE_MASK);
155        return value;
156    }
157
158    /**
159     * Helper method to get the value as a Java long from a four-byte array
160     * @param bytes the array of bytes
161     * @return the corresponding Java long value
162     */
163    public static long getValue(byte[] bytes) {
164        return getValue(bytes, 0);
165    }
166
167    /**
168     * Override to make two instances with same value equal.
169     * @param o an object to compare
170     * @return true if the objects are equal
171     */
172    @Override
173    public boolean equals(Object o) {
174        if (o == null || !(o instanceof ZipLong)) {
175            return false;
176        }
177        return value == ((ZipLong) o).getValue();
178    }
179
180    /**
181     * Override to make two instances with same value equal.
182     * @return the value stored in the ZipLong
183     */
184    @Override
185    public int hashCode() {
186        return (int) value;
187    }
188
189    @Override
190    public Object clone() {
191        try {
192            return super.clone();
193        } catch (CloneNotSupportedException cnfe) {
194            // impossible
195            throw new RuntimeException(cnfe);
196        }
197    }
198
199    @Override
200    public String toString() {
201        return "ZipLong value: " + value;
202    }
203}