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