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