    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 */
    017package org.apache.commons.compress.archivers.zip;
    018
    019import static org.apache.commons.compress.archivers.zip.ZipConstants.BYTE_MASK;
    020
    021import java.io.Serializable;
    022import java.math.BigInteger;
    023
    024/**
    025 * Utility class that represents an eight byte integer with conversion
    026 * rules for the little endian byte order of ZIP files.
    027 * @Immutable
    028 *
    029 * @since 1.2
    030 */
    031public final class ZipEightByteInteger implements Serializable {
    032    private static final long serialVersionUID = 1L;
    033
    034    private static final int BYTE_1 = 1;
    035    private static final int BYTE_1_MASK = 0xFF00;
    036    private static final int BYTE_1_SHIFT = 8;
    037
    038    private static final int BYTE_2 = 2;
    039    private static final int BYTE_2_MASK = 0xFF0000;
    040    private static final int BYTE_2_SHIFT = 16;
    041
    042    private static final int BYTE_3 = 3;
    043    private static final long BYTE_3_MASK = 0xFF000000L;
    044    private static final int BYTE_3_SHIFT = 24;
    045
    046    private static final int BYTE_4 = 4;
    047    private static final long BYTE_4_MASK = 0xFF00000000L;
    048    private static final int BYTE_4_SHIFT = 32;
    049
    050    private static final int BYTE_5 = 5;
    051    private static final long BYTE_5_MASK = 0xFF0000000000L;
    052    private static final int BYTE_5_SHIFT = 40;
    053
    054    private static final int BYTE_6 = 6;
    055    private static final long BYTE_6_MASK = 0xFF000000000000L;
    056    private static final int BYTE_6_SHIFT = 48;
    057
    058    private static final int BYTE_7 = 7;
    059    private static final long BYTE_7_MASK = 0x7F00000000000000L;
    060    private static final int BYTE_7_SHIFT = 56;
    061
    062    private static final int LEFTMOST_BIT_SHIFT = 63;
    063    private static final byte LEFTMOST_BIT = (byte) 0x80;
    064
    065    public static final ZipEightByteInteger ZERO = new ZipEightByteInteger(0);
    066
    067    /**
    068     * Gets value as eight bytes in big endian byte order.
    069     * @param value the value to convert
    070     * @return value as eight bytes in big endian byte order
    071     */
    072    public static byte[] getBytes(final BigInteger value) {
    073        final byte[] result = new byte[8];
    074        final long val = value.longValue();
    075        result[0] = (byte) (val & BYTE_MASK);
    076        result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT);
    077        result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT);
    078        result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT);
    079        result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT);
    080        result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT);
    081        result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT);
    082        result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT);
    083        if (value.testBit(LEFTMOST_BIT_SHIFT)) {
    084            result[BYTE_7] |= LEFTMOST_BIT;
    085        }
    086        return result;
    087    }
    088
    089    /**
    090     * Gets value as eight bytes in big endian byte order.
    091     * @param value the value to convert
    092     * @return value as eight bytes in big endian byte order
    093     */
    094    public static byte[] getBytes(final long value) {
    095        return getBytes(BigInteger.valueOf(value));
    096    }
    097
    098    /**
    099     * Helper method to get the value as a Java long from an eight-byte array
    100     * @param bytes the array of bytes
    101     * @return the corresponding Java long value
    102     */
    103    public static long getLongValue(final byte[] bytes) {
    104        return getLongValue(bytes, 0);
    105    }
    106
    107    /**
    108     * Helper method to get the value as a Java long from eight bytes
    109     * starting at given array offset
    110     * @param bytes the array of bytes
    111     * @param offset the offset to start
    112     * @return the corresponding Java long value
    113     */
    114    public static long getLongValue(final byte[] bytes, final int offset) {
    115        return getValue(bytes, offset).longValue();
    116    }
    117
    118    /**
    119     * Helper method to get the value as a Java long from an eight-byte array
    120     * @param bytes the array of bytes
    121     * @return the corresponding Java BigInteger value
    122     */
    123    public static BigInteger getValue(final byte[] bytes) {
    124        return getValue(bytes, 0);
    125    }
    126
    127    /**
    128     * Helper method to get the value as a Java BigInteger from eight
    129     * bytes starting at given array offset
    130     * @param bytes the array of bytes
    131     * @param offset the offset to start
    132     * @return the corresponding Java BigInteger value
    133     */
    134    public static BigInteger getValue(final byte[] bytes, final int offset) {
    135        long value = (long) bytes[offset + BYTE_7] << BYTE_7_SHIFT & BYTE_7_MASK;
    136        value += (long) bytes[offset + BYTE_6] << BYTE_6_SHIFT & BYTE_6_MASK;
    137        value += (long) bytes[offset + BYTE_5] << BYTE_5_SHIFT & BYTE_5_MASK;
    138        value += (long) bytes[offset + BYTE_4] << BYTE_4_SHIFT & BYTE_4_MASK;
    139        value += (long) bytes[offset + BYTE_3] << BYTE_3_SHIFT & BYTE_3_MASK;
    140        value += (long) bytes[offset + BYTE_2] << BYTE_2_SHIFT & BYTE_2_MASK;
    141        value += (long) bytes[offset + BYTE_1] << BYTE_1_SHIFT & BYTE_1_MASK;
    142        value += (long) bytes[offset] & BYTE_MASK;
    143        final BigInteger val = BigInteger.valueOf(value);
    144        return (bytes[offset + BYTE_7] & LEFTMOST_BIT) == LEFTMOST_BIT
    145            ? val.setBit(LEFTMOST_BIT_SHIFT) : val;
    146    }
    147
    148    private final BigInteger value;
    149
    150    /**
    151     * Create instance from a number.
    152     * @param value the BigInteger to store as a ZipEightByteInteger
    153     */
    154    public ZipEightByteInteger(final BigInteger value) {
    155        this.value = value;
    156    }
    157
    158    /**
    159     * Create instance from bytes.
    160     * @param bytes the bytes to store as a ZipEightByteInteger
    161     */
    162    public ZipEightByteInteger (final byte[] bytes) {
    163        this(bytes, 0);
    164    }
    165
    166    /**
    167     * Create instance from the eight bytes starting at offset.
    168     * @param bytes the bytes to store as a ZipEightByteInteger
    169     * @param offset the offset to start
    170     */
    171    public ZipEightByteInteger (final byte[] bytes, final int offset) {
    172        value = ZipEightByteInteger.getValue(bytes, offset);
    173    }
    174
    175    /**
    176     * Create instance from a number.
    177     * @param value the long to store as a ZipEightByteInteger
    178     */
    179    public ZipEightByteInteger(final long value) {
    180        this(BigInteger.valueOf(value));
    181    }
    182
    183    /**
    184     * Override to make two instances with same value equal.
    185     * @param o an object to compare
    186     * @return true if the objects are equal
    187     */
    188    @Override
    189    public boolean equals(final Object o) {
    190        if (!(o instanceof ZipEightByteInteger)) {
    191            return false;
    192        }
    193        return value.equals(((ZipEightByteInteger) o).getValue());
    194    }
    195
    196    /**
    197     * Gets value as eight bytes in big endian byte order.
    198     * @return value as eight bytes in big endian order
    199     */
    200    public byte[] getBytes() {
    201        return ZipEightByteInteger.getBytes(value);
    202    }
    203
    204    /**
    205     * Gets value as Java long.
    206     * @return value as a long
    207     */
    208    public long getLongValue() {
    209        return value.longValue();
    210    }
    211
    212    /**
    213     * Gets value as Java BigInteger.
    214     * @return value as a BigInteger
    215     */
    216    public BigInteger getValue() {
    217        return value;
    218    }
    219
    220    /**
    221     * Override to make two instances with same value equal.
    222     * @return the hash code of the value stored in the ZipEightByteInteger
    223     */
    224    @Override
    225    public int hashCode() {
    226        return value.hashCode();
    227    }
    228
    229    @Override
    230    public String toString() {
    231        return "ZipEightByteInteger value: " + value;
    232    }
    233}