Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 1 | /* |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 8 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 10 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 17 | */ |
| 18 | package org.apache.commons.compress.archivers.zip; |
| 19 | |
Julius Davies | ccc7441 | 2013-01-08 00:01:55 +0000 | [diff] [blame] | 20 | import java.io.Serializable; |
| 21 | |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 22 | import static org.apache.commons.compress.archivers.zip.ZipConstants.BYTE_MASK; |
| 23 | import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD; |
| 24 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 25 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 26 | * Utility class that represents a four byte integer with conversion |
| 27 | * rules for the big endian byte order of ZIP files. |
Sebastian Bazley | 44dbd93 | 2009-03-28 00:03:11 +0000 | [diff] [blame] | 28 | * @Immutable |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 29 | */ |
Julius Davies | ccc7441 | 2013-01-08 00:01:55 +0000 | [diff] [blame] | 30 | public final class ZipLong implements Cloneable, Serializable { |
| 31 | private static final long serialVersionUID = 1L; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 32 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 33 | //private static final int BYTE_BIT_SIZE = 8; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 34 | |
| 35 | private static final int BYTE_1 = 1; |
| 36 | private static final int BYTE_1_MASK = 0xFF00; |
| 37 | private static final int BYTE_1_SHIFT = 8; |
| 38 | |
| 39 | private static final int BYTE_2 = 2; |
| 40 | private static final int BYTE_2_MASK = 0xFF0000; |
| 41 | private static final int BYTE_2_SHIFT = 16; |
| 42 | |
| 43 | private static final int BYTE_3 = 3; |
| 44 | private static final long BYTE_3_MASK = 0xFF000000L; |
| 45 | private static final int BYTE_3_SHIFT = 24; |
| 46 | |
Sebastian Bazley | 8fa12b8 | 2009-03-13 19:41:42 +0000 | [diff] [blame] | 47 | private final long value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 48 | |
Sebastian Bazley | f1d63a6 | 2009-03-28 03:18:42 +0000 | [diff] [blame] | 49 | /** Central File Header Signature */ |
Stefan Bodewig | 008ca94 | 2009-03-26 22:29:59 +0000 | [diff] [blame] | 50 | public static final ZipLong CFH_SIG = new ZipLong(0X02014B50L); |
Sebastian Bazley | f1d63a6 | 2009-03-28 03:18:42 +0000 | [diff] [blame] | 51 | |
| 52 | /** Local File Header Signature */ |
Stefan Bodewig | 008ca94 | 2009-03-26 22:29:59 +0000 | [diff] [blame] | 53 | public static final ZipLong LFH_SIG = new ZipLong(0X04034B50L); |
| 54 | |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 55 | /** |
Stefan Bodewig | 92d8c57 | 2013-01-01 10:51:42 +0000 | [diff] [blame] | 56 | * Data Descriptor signature. |
| 57 | * |
| 58 | * <p>Actually, PKWARE uses this as marker for split/spanned |
| 59 | * archives and other archivers have started to use it as Data |
| 60 | * Descriptor signature (as well).</p> |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 61 | * @since 1.1 |
Stefan Bodewig | d7ea9ff | 2010-03-12 15:35:44 +0000 | [diff] [blame] | 62 | */ |
| 63 | public static final ZipLong DD_SIG = new ZipLong(0X08074B50L); |
| 64 | |
| 65 | /** |
Stefan Bodewig | cb1fdba | 2011-07-25 16:31:10 +0000 | [diff] [blame] | 66 | * Value stored in size and similar fields if ZIP64 extensions are |
| 67 | * used. |
Gary D. Gregory | 2bd0dd4 | 2012-04-01 13:02:39 +0000 | [diff] [blame] | 68 | * @since 1.3 |
Stefan Bodewig | cb1fdba | 2011-07-25 16:31:10 +0000 | [diff] [blame] | 69 | */ |
Stefan Bodewig | fadf636 | 2011-07-29 04:01:05 +0000 | [diff] [blame] | 70 | static final ZipLong ZIP64_MAGIC = new ZipLong(ZipConstants.ZIP64_MAGIC); |
Stefan Bodewig | cb1fdba | 2011-07-25 16:31:10 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
Stefan Bodewig | e10ce2c | 2013-01-01 10:57:23 +0000 | [diff] [blame] | 73 | * Marks ZIP archives that were supposed to be split or spanned |
| 74 | * but only needed a single segment in then end (so are actually |
| 75 | * neither split nor spanned). |
| 76 | * |
| 77 | * <p>This is the "PK00" prefix found in some archives.</p> |
| 78 | * @since 1.5 |
| 79 | */ |
| 80 | public static final ZipLong SINGLE_SEGMENT_SPLIT_MARKER = |
| 81 | new ZipLong(0X30304B50L); |
| 82 | |
| 83 | /** |
Stefan Bodewig | 45e51c2 | 2013-12-22 07:03:43 +0000 | [diff] [blame^] | 84 | * Archive extra data record signature. |
Stefan Bodewig | 7181d54 | 2013-01-22 12:45:24 +0000 | [diff] [blame] | 85 | * @since 1.5 |
| 86 | */ |
| 87 | public static final ZipLong AED_SIG = new ZipLong(0X08064B50L); |
| 88 | |
| 89 | /** |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 90 | * Create instance from a number. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 91 | * @param value the long to store as a ZipLong |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 92 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 93 | public ZipLong(long value) { |
| 94 | this.value = value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Create instance from bytes. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 99 | * @param bytes the bytes to store as a ZipLong |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 100 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 101 | public ZipLong (byte[] bytes) { |
| 102 | this(bytes, 0); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Create instance from the four bytes starting at offset. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 107 | * @param bytes the bytes to store as a ZipLong |
| 108 | * @param offset the offset to start |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 109 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 110 | public ZipLong (byte[] bytes, int offset) { |
| 111 | value = ZipLong.getValue(bytes, offset); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 115 | * Get value as four bytes in big endian byte order. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 116 | * @return value as four bytes in big endian order |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 117 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 118 | public byte[] getBytes() { |
| 119 | return ZipLong.getBytes(value); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get value as Java long. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 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); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 141 | return result; |
| 142 | } |
| 143 | |
| 144 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 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 |
Stefan Bodewig | ec44557 | 2011-07-21 08:38:24 +0000 | [diff] [blame] | 148 | * @return the corresponding Java long value |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 149 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 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 |
Stefan Bodewig | ec44557 | 2011-07-21 08:38:24 +0000 | [diff] [blame] | 161 | * @return the corresponding Java long value |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 162 | */ |
| 163 | public static long getValue(byte[] bytes) { |
| 164 | return getValue(bytes, 0); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Override to make two instances with same value equal. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 169 | * @param o an object to compare |
| 170 | * @return true if the objects are equal |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 171 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 172 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 173 | public boolean equals(Object o) { |
| 174 | if (o == null || !(o instanceof ZipLong)) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 177 | return value == ((ZipLong) o).getValue(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Override to make two instances with same value equal. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 182 | * @return the value stored in the ZipLong |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 183 | */ |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 184 | @Override |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 185 | public int hashCode() { |
| 186 | return (int) value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 187 | } |
Stefan Bodewig | 5e5804c | 2009-02-05 12:45:23 +0000 | [diff] [blame] | 188 | |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 189 | @Override |
Stefan Bodewig | 5e5804c | 2009-02-05 12:45:23 +0000 | [diff] [blame] | 190 | public Object clone() { |
| 191 | try { |
Sebastian Bazley | 5ccdd3f | 2009-03-04 00:29:29 +0000 | [diff] [blame] | 192 | return super.clone(); |
Stefan Bodewig | 5e5804c | 2009-02-05 12:45:23 +0000 | [diff] [blame] | 193 | } catch (CloneNotSupportedException cnfe) { |
| 194 | // impossible |
| 195 | throw new RuntimeException(cnfe); |
| 196 | } |
| 197 | } |
Stefan Bodewig | 60e2dec | 2011-07-21 12:11:06 +0000 | [diff] [blame] | 198 | |
Stefan Bodewig | e53e88a | 2011-07-25 13:28:31 +0000 | [diff] [blame] | 199 | @Override |
Stefan Bodewig | 60e2dec | 2011-07-21 12:11:06 +0000 | [diff] [blame] | 200 | public String toString() { |
| 201 | return "ZipLong value: " + value; |
| 202 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 203 | } |