001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.commons.compress.archivers.zip; 020 021import java.util.zip.CRC32; 022import java.util.zip.ZipException; 023 024/** 025 * Adds Unix file permission and UID/GID fields as well as symbolic 026 * link handling. 027 * 028 * <p>This class uses the ASi extra field in the format:</p> 029 * <pre> 030 * Value Size Description 031 * ----- ---- ----------- 032 * (Unix3) 0x756e Short tag for this extra block type 033 * TSize Short total data size for this block 034 * CRC Long CRC-32 of the remaining data 035 * Mode Short file permissions 036 * SizDev Long symlink'd size OR major/minor dev num 037 * UID Short user ID 038 * GID Short group ID 039 * (var.) variable symbolic link filename 040 * </pre> 041 * <p>taken from appnote.iz (Info-ZIP note, 981119) found at <a 042 * href="ftp://ftp.uu.net/pub/archiving/zip/doc/">ftp://ftp.uu.net/pub/archiving/zip/doc/</a></p> 043 * 044 * <p>Short is two bytes and Long is four bytes in big endian byte and 045 * word order, device numbers are currently not supported.</p> 046 * @NotThreadSafe 047 * 048 * <p>Since the documentation this class is based upon doesn't mention 049 * the character encoding of the file name at all, it is assumed that 050 * it uses the current platform's default encoding.</p> 051 */ 052public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable { 053 054 private static final ZipShort HEADER_ID = new ZipShort(0x756E); 055 private static final int WORD = 4; 056 /** 057 * Standard Unix stat(2) file mode. 058 */ 059 private int mode = 0; 060 /** 061 * User ID. 062 */ 063 private int uid = 0; 064 /** 065 * Group ID. 066 */ 067 private int gid = 0; 068 /** 069 * File this entry points to, if it is a symbolic link. 070 * 071 * <p>empty string - if entry is not a symbolic link.</p> 072 */ 073 private String link = ""; 074 /** 075 * Is this an entry for a directory? 076 */ 077 private boolean dirFlag = false; 078 079 /** 080 * Instance used to calculate checksums. 081 */ 082 private CRC32 crc = new CRC32(); 083 084 /** Constructor for AsiExtraField. */ 085 public AsiExtraField() { 086 } 087 088 /** 089 * The Header-ID. 090 * @return the value for the header id for this extrafield 091 */ 092 public ZipShort getHeaderId() { 093 return HEADER_ID; 094 } 095 096 /** 097 * Length of the extra field in the local file data - without 098 * Header-ID or length specifier. 099 * @return a <code>ZipShort</code> for the length of the data of this extra field 100 */ 101 public ZipShort getLocalFileDataLength() { 102 return new ZipShort(WORD // CRC 103 + 2 // Mode 104 + WORD // SizDev 105 + 2 // UID 106 + 2 // GID 107 + getLinkedFile().getBytes().length); 108 // Uses default charset - see class Javadoc 109 } 110 111 /** 112 * Delegate to local file data. 113 * @return the centralDirectory length 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 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 129 byte[] linkArray = getLinkedFile().getBytes(); // Uses default charset - see class Javadoc 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 155 */ 156 public byte[] getCentralDirectoryData() { 157 return getLocalFileDataData(); 158 } 159 160 /** 161 * Set the user id. 162 * @param uid the user id 163 */ 164 public void setUserId(int uid) { 165 this.uid = uid; 166 } 167 168 /** 169 * Get the user id. 170 * @return the user id 171 */ 172 public int getUserId() { 173 return uid; 174 } 175 176 /** 177 * Set the group id. 178 * @param gid the group id 179 */ 180 public void setGroupId(int gid) { 181 this.gid = gid; 182 } 183 184 /** 185 * Get the group id. 186 * @return the group id 187 */ 188 public int getGroupId() { 189 return gid; 190 } 191 192 /** 193 * Indicate that this entry is a symbolic link to the given filename. 194 * 195 * @param name Name of the file this entry links to, empty String 196 * if it is not a symbolic link. 197 */ 198 public void setLinkedFile(String name) { 199 link = name; 200 mode = getMode(mode); 201 } 202 203 /** 204 * Name of linked file 205 * 206 * @return name of the file this entry links to if it is a 207 * symbolic link, the empty string otherwise. 208 */ 209 public String getLinkedFile() { 210 return link; 211 } 212 213 /** 214 * Is this entry a symbolic link? 215 * @return true if this is a symbolic link 216 */ 217 public boolean isLink() { 218 return getLinkedFile().length() != 0; 219 } 220 221 /** 222 * File mode of this file. 223 * @param mode the file mode 224 */ 225 public void setMode(int mode) { 226 this.mode = getMode(mode); 227 } 228 229 /** 230 * File mode of this file. 231 * @return the file mode 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 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 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 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)); 275 } 276 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); 282 283 if (linkArray.length == 0) { 284 link = ""; 285 } else { 286 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length); 287 link = new String(linkArray); // Uses default charset - see class Javadoc 288 } 289 // CheckStyle:MagicNumber ON 290 setDirectory((newMode & DIR_FLAG) != 0); 291 setMode(newMode); 292 } 293 294 /** 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 /** 305 * Get the file mode for given permissions with the correct file type. 306 * @param mode the mode 307 * @return the type with the mode 308 */ 309 protected int getMode(int mode) { 310 int type = FILE_FLAG; 311 if (isLink()) { 312 type = LINK_FLAG; 313 } else if (isDirectory()) { 314 type = DIR_FLAG; 315 } 316 return type | (mode & PERM_MASK); 317 } 318 319 @Override 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 } 330}