Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.commons.compress.archivers.zip; |
| 20 | |
| 21 | import java.util.zip.CRC32; |
| 22 | import java.util.zip.ZipException; |
| 23 | |
| 24 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 25 | * Adds Unix file permission and UID/GID fields as well as symbolic |
| 26 | * link handling. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 27 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 28 | * <p>This class uses the ASi extra field in the format: |
| 29 | * <pre> |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 30 | * Value Size Description |
| 31 | * ----- ---- ----------- |
| 32 | * (Unix3) 0x756e Short tag for this extra block type |
| 33 | * TSize Short total data size for this block |
| 34 | * CRC Long CRC-32 of the remaining data |
| 35 | * Mode Short file permissions |
| 36 | * SizDev Long symlink'd size OR major/minor dev num |
| 37 | * UID Short user ID |
| 38 | * GID Short group ID |
| 39 | * (var.) variable symbolic link filename |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 40 | * </pre> |
| 41 | * taken from appnote.iz (Info-ZIP note, 981119) found at <a |
| 42 | * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p> |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 43 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 44 | * |
| 45 | * <p>Short is two bytes and Long is four bytes in big endian byte and |
| 46 | * word order, device numbers are currently not supported.</p> |
Sebastian Bazley | 99870ef | 2009-03-28 00:04:36 +0000 | [diff] [blame] | 47 | * @NotThreadSafe |
Stefan Bodewig | 673fd9b | 2009-04-15 09:38:47 +0000 | [diff] [blame] | 48 | * |
| 49 | * <p>Since the documentation this class is based upon doesn't mention |
| 50 | * the character encoding of the file name at all, it is assumed that |
| 51 | * it uses the current platform's default encoding.</p> |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 52 | */ |
| 53 | public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable { |
| 54 | |
| 55 | private static final ZipShort HEADER_ID = new ZipShort(0x756E); |
| 56 | private static final int WORD = 4; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Standard Unix stat(2) file mode. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 59 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 60 | private int mode = 0; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 61 | /** |
| 62 | * User ID. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 63 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 64 | private int uid = 0; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 65 | /** |
| 66 | * Group ID. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 67 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 68 | private int gid = 0; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 69 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 70 | * File this entry points to, if it is a symbolic link. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 71 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 72 | * <p>empty string - if entry is not a symbolic link.</p> |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 73 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 74 | private String link = ""; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 75 | /** |
| 76 | * Is this an entry for a directory? |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 77 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 78 | private boolean dirFlag = false; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 79 | |
| 80 | /** |
| 81 | * Instance used to calculate checksums. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 82 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 83 | private CRC32 crc = new CRC32(); |
| 84 | |
| 85 | /** Constructor for AsiExtraField. */ |
| 86 | public AsiExtraField() { |
| 87 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 88 | |
| 89 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 90 | * The Header-ID. |
| 91 | * @return the value for the header id for this extrafield |
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 ZipShort getHeaderId() { |
| 94 | return HEADER_ID; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Length of the extra field in the local file data - without |
| 99 | * Header-ID or length specifier. |
| 100 | * @return a <code>ZipShort</code> for the length of the data of this extra field |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 101 | */ |
| 102 | public ZipShort getLocalFileDataLength() { |
| 103 | return new ZipShort(WORD // CRC |
| 104 | + 2 // Mode |
| 105 | + WORD // SizDev |
| 106 | + 2 // UID |
| 107 | + 2 // GID |
Stefan Bodewig | 673fd9b | 2009-04-15 09:38:47 +0000 | [diff] [blame] | 108 | + getLinkedFile().getBytes().length); |
Sebastian Bazley | 325f002 | 2009-04-15 11:23:48 +0000 | [diff] [blame] | 109 | // Uses default charset - see class Javadoc |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Delegate to local file data. |
| 114 | * @return the centralDirectory length |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 115 | */ |
| 116 | public ZipShort getCentralDirectoryLength() { |
| 117 | return getLocalFileDataLength(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * The actual data to put into local file data - without Header-ID |
| 122 | * or length specifier. |
| 123 | * @return get the data |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 124 | */ |
| 125 | public byte[] getLocalFileDataData() { |
| 126 | // CRC will be added later |
| 127 | byte[] data = new byte[getLocalFileDataLength().getValue() - WORD]; |
| 128 | System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2); |
| 129 | |
Sebastian Bazley | 325f002 | 2009-04-15 11:23:48 +0000 | [diff] [blame] | 130 | byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 131 | // CheckStyle:MagicNumber OFF |
| 132 | System.arraycopy(ZipLong.getBytes(linkArray.length), |
| 133 | 0, data, 2, WORD); |
| 134 | |
| 135 | System.arraycopy(ZipShort.getBytes(getUserId()), |
| 136 | 0, data, 6, 2); |
| 137 | System.arraycopy(ZipShort.getBytes(getGroupId()), |
| 138 | 0, data, 8, 2); |
| 139 | |
| 140 | System.arraycopy(linkArray, 0, data, 10, linkArray.length); |
| 141 | // CheckStyle:MagicNumber ON |
| 142 | |
| 143 | crc.reset(); |
| 144 | crc.update(data); |
| 145 | long checksum = crc.getValue(); |
| 146 | |
| 147 | byte[] result = new byte[data.length + WORD]; |
| 148 | System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD); |
| 149 | System.arraycopy(data, 0, result, WORD, data.length); |
| 150 | return result; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Delegate to local file data. |
| 155 | * @return the local file data |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 156 | */ |
| 157 | public byte[] getCentralDirectoryData() { |
| 158 | return getLocalFileDataData(); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Set the user id. |
| 163 | * @param uid the user id |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 164 | */ |
| 165 | public void setUserId(int uid) { |
| 166 | this.uid = uid; |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the user id. |
| 171 | * @return the user id |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 172 | */ |
| 173 | public int getUserId() { |
| 174 | return uid; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Set the group id. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 179 | * @param gid the group id |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 180 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 181 | public void setGroupId(int gid) { |
| 182 | this.gid = gid; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Get the group id. |
| 187 | * @return the group id |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 188 | */ |
| 189 | public int getGroupId() { |
| 190 | return gid; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Indicate that this entry is a symbolic link to the given filename. |
| 195 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 196 | * @param name Name of the file this entry links to, empty String |
| 197 | * if it is not a symbolic link. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 198 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 199 | public void setLinkedFile(String name) { |
| 200 | link = name; |
| 201 | mode = getMode(mode); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Name of linked file |
| 206 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 207 | * @return name of the file this entry links to if it is a |
| 208 | * symbolic link, the empty string otherwise. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 209 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 210 | public String getLinkedFile() { |
| 211 | return link; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Is this entry a symbolic link? |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 216 | * @return true if this is a symbolic link |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 217 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 218 | public boolean isLink() { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 219 | return getLinkedFile().length() != 0; |
| 220 | } |
| 221 | |
| 222 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 223 | * File mode of this file. |
| 224 | * @param mode the file mode |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 225 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 226 | public void setMode(int mode) { |
| 227 | this.mode = getMode(mode); |
| 228 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 229 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 230 | /** |
| 231 | * File mode of this file. |
| 232 | * @return the file mode |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 233 | */ |
| 234 | public int getMode() { |
| 235 | return mode; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Indicate whether this entry is a directory. |
| 240 | * @param dirFlag if true, this entry is a directory |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 241 | */ |
| 242 | public void setDirectory(boolean dirFlag) { |
| 243 | this.dirFlag = dirFlag; |
| 244 | mode = getMode(mode); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Is this entry a directory? |
| 249 | * @return true if this entry is a directory |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 250 | */ |
| 251 | public boolean isDirectory() { |
| 252 | return dirFlag && !isLink(); |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Populate data from this array as if it was in local file data. |
| 257 | * @param data an array of bytes |
| 258 | * @param offset the start offset |
| 259 | * @param length the number of bytes in the array from offset |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 260 | * @throws ZipException on error |
| 261 | */ |
| 262 | public void parseFromLocalFileData(byte[] data, int offset, int length) |
| 263 | throws ZipException { |
| 264 | |
| 265 | long givenChecksum = ZipLong.getValue(data, offset); |
| 266 | byte[] tmp = new byte[length - WORD]; |
| 267 | System.arraycopy(data, offset + WORD, tmp, 0, length - WORD); |
| 268 | crc.reset(); |
| 269 | crc.update(tmp); |
| 270 | long realChecksum = crc.getValue(); |
| 271 | if (givenChecksum != realChecksum) { |
| 272 | throw new ZipException("bad CRC checksum " |
| 273 | + Long.toHexString(givenChecksum) |
| 274 | + " instead of " |
| 275 | + Long.toHexString(realChecksum)); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 278 | int newMode = ZipShort.getValue(tmp, 0); |
| 279 | // CheckStyle:MagicNumber OFF |
| 280 | byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)]; |
| 281 | uid = ZipShort.getValue(tmp, 6); |
| 282 | gid = ZipShort.getValue(tmp, 8); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 283 | |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 284 | if (linkArray.length == 0) { |
| 285 | link = ""; |
| 286 | } else { |
| 287 | System.arraycopy(tmp, 10, linkArray, 0, linkArray.length); |
Sebastian Bazley | 325f002 | 2009-04-15 11:23:48 +0000 | [diff] [blame] | 288 | link = new String(linkArray); // Uses default charset - see class Javadoc |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 289 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 290 | // CheckStyle:MagicNumber ON |
| 291 | setDirectory((newMode & DIR_FLAG) != 0); |
| 292 | setMode(newMode); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | /** |
Stefan Bodewig | dfbdfee | 2009-03-03 12:42:28 +0000 | [diff] [blame] | 296 | * Doesn't do anything special since this class always uses the |
| 297 | * same data in central directory and local file data. |
| 298 | */ |
| 299 | public void parseFromCentralDirectoryData(byte[] buffer, int offset, |
| 300 | int length) |
| 301 | throws ZipException { |
| 302 | parseFromLocalFileData(buffer, offset, length); |
| 303 | } |
| 304 | |
| 305 | /** |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 306 | * Get the file mode for given permissions with the correct file type. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 307 | * @param mode the mode |
| 308 | * @return the type with the mode |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 309 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 310 | protected int getMode(int mode) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 311 | int type = FILE_FLAG; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 312 | if (isLink()) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 313 | type = LINK_FLAG; |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 314 | } else if (isDirectory()) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 315 | type = DIR_FLAG; |
| 316 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 317 | return type | (mode & PERM_MASK); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 318 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame] | 319 | |
Sebastian Bazley | fb06e3a | 2011-08-15 15:10:46 +0000 | [diff] [blame] | 320 | @Override |
Stefan Bodewig | 5e5804c | 2009-02-05 12:45:23 +0000 | [diff] [blame] | 321 | public Object clone() { |
| 322 | try { |
| 323 | AsiExtraField cloned = (AsiExtraField) super.clone(); |
| 324 | cloned.crc = new CRC32(); |
| 325 | return cloned; |
| 326 | } catch (CloneNotSupportedException cnfe) { |
| 327 | // impossible |
| 328 | throw new RuntimeException(cnfe); |
| 329 | } |
| 330 | } |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 331 | } |