Debian Codegraph @

src/main/java/org/apache/tools/zip/AsiExtraField.java

raw
1 /*
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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 *
17 */
18
19 package org.apache.tools.zip;
20
21 import java.util.zip.CRC32;
22 import java.util.zip.ZipException;
23
24 /**
25 * Adds Unix file permission and UID/GID fields as well as symbolic
26 * link handling.
27 *
28 * <p>This class uses the ASi extra field in the format:
29 * <pre>
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
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>
43
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>
47 *
48 */
49 public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
50
51 private static final ZipShort HEADER_ID = new ZipShort(0x756E);
52 private static final int WORD = 4;
53 /**
54 * Standard Unix stat(2) file mode.
55 *
56 * @since 1.1
57 */
58 private int mode = 0;
59 /**
60 * User ID.
61 *
62 * @since 1.1
63 */
64 private int uid = 0;
65 /**
66 * Group ID.
67 *
68 * @since 1.1
69 */
70 private int gid = 0;
71 /**
72 * File this entry points to, if it is a symbolic link.
73 *
74 * <p>empty string - if entry is not a symbolic link.</p>
75 *
76 * @since 1.1
77 */
78 private String link = "";
79 /**
80 * Is this an entry for a directory?
81 *
82 * @since 1.1
83 */
84 private boolean dirFlag = false;
85
86 /**
87 * Instance used to calculate checksums.
88 *
89 * @since 1.1
90 */
91 private CRC32 crc = new CRC32();
92
93 /** Constructor for AsiExtraField. */
94 public AsiExtraField() {
95 }
96
97 /**
98 * The Header-ID.
99 * @return the value for the header id for this extrafield
100 * @since 1.1
101 */
102 public ZipShort getHeaderId() {
103 return HEADER_ID;
104 }
105
106 /**
107 * Length of the extra field in the local file data - without
108 * Header-ID or length specifier.
109 * @return a <code>ZipShort</code> for the length of the data of this extra field
110 * @since 1.1
111 */
112 public ZipShort getLocalFileDataLength() {
113 return new ZipShort(WORD // CRC
114 + 2 // Mode
115 + WORD // SizDev
116 + 2 // UID
117 + 2 // GID
118 + getLinkedFile().getBytes().length);
119 }
120
121 /**
122 * Delegate to local file data.
123 * @return the centralDirectory length
124 * @since 1.1
125 */
126 public ZipShort getCentralDirectoryLength() {
127 return getLocalFileDataLength();
128 }
129
130 /**
131 * The actual data to put into local file data - without Header-ID
132 * or length specifier.
133 * @return get the data
134 * @since 1.1
135 */
136 public byte[] getLocalFileDataData() {
137 // CRC will be added later
138 byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
139 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
140
141 byte[] linkArray = getLinkedFile().getBytes();
142 // CheckStyle:MagicNumber OFF
143 System.arraycopy(ZipLong.getBytes(linkArray.length),
144 0, data, 2, WORD);
145
146 System.arraycopy(ZipShort.getBytes(getUserId()),
147 0, data, 6, 2);
148 System.arraycopy(ZipShort.getBytes(getGroupId()),
149 0, data, 8, 2);
150
151 System.arraycopy(linkArray, 0, data, 10, linkArray.length);
152 // CheckStyle:MagicNumber ON
153
154 crc.reset();
155 crc.update(data);
156 long checksum = crc.getValue();
157
158 byte[] result = new byte[data.length + WORD];
159 System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
160 System.arraycopy(data, 0, result, WORD, data.length);
161 return result;
162 }
163
164 /**
165 * Delegate to local file data.
166 * @return the local file data
167 * @since 1.1
168 */
169 public byte[] getCentralDirectoryData() {
170 return getLocalFileDataData();
171 }
172
173 /**
174 * Set the user id.
175 * @param uid the user id
176 * @since 1.1
177 */
178 public void setUserId(int uid) {
179 this.uid = uid;
180 }
181
182 /**
183 * Get the user id.
184 * @return the user id
185 * @since 1.1
186 */
187 public int getUserId() {
188 return uid;
189 }
190
191 /**
192 * Set the group id.
193 * @param gid the group id
194 * @since 1.1
195 */
196 public void setGroupId(int gid) {
197 this.gid = gid;
198 }
199
200 /**
201 * Get the group id.
202 * @return the group id
203 * @since 1.1
204 */
205 public int getGroupId() {
206 return gid;
207 }
208
209 /**
210 * Indicate that this entry is a symbolic link to the given filename.
211 *
212 * @param name Name of the file this entry links to, empty String
213 * if it is not a symbolic link.
214 *
215 * @since 1.1
216 */
217 public void setLinkedFile(String name) {
218 link = name;
219 mode = getMode(mode);
220 }
221
222 /**
223 * Name of linked file
224 *
225 * @return name of the file this entry links to if it is a
226 * symbolic link, the empty string otherwise.
227 *
228 * @since 1.1
229 */
230 public String getLinkedFile() {
231 return link;
232 }
233
234 /**
235 * Is this entry a symbolic link?
236 * @return true if this is a symbolic link
237 * @since 1.1
238 */
239 public boolean isLink() {
240 return getLinkedFile().length() != 0;
241 }
242
243 /**
244 * File mode of this file.
245 * @param mode the file mode
246 * @since 1.1
247 */
248 public void setMode(int mode) {
249 this.mode = getMode(mode);
250 }
251
252 /**
253 * File mode of this file.
254 * @return the file mode
255 * @since 1.1
256 */
257 public int getMode() {
258 return mode;
259 }
260
261 /**
262 * Indicate whether this entry is a directory.
263 * @param dirFlag if true, this entry is a directory
264 * @since 1.1
265 */
266 public void setDirectory(boolean dirFlag) {
267 this.dirFlag = dirFlag;
268 mode = getMode(mode);
269 }
270
271 /**
272 * Is this entry a directory?
273 * @return true if this entry is a directory
274 * @since 1.1
275 */
276 public boolean isDirectory() {
277 return dirFlag && !isLink();
278 }
279
280 /**
281 * Populate data from this array as if it was in local file data.
282 * @param data an array of bytes
283 * @param offset the start offset
284 * @param length the number of bytes in the array from offset
285 * @since 1.1
286 * @throws ZipException on error
287 */
288 public void parseFromLocalFileData(byte[] data, int offset, int length)
289 throws ZipException {
290
291 long givenChecksum = ZipLong.getValue(data, offset);
292 byte[] tmp = new byte[length - WORD];
293 System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
294 crc.reset();
295 crc.update(tmp);
296 long realChecksum = crc.getValue();
297 if (givenChecksum != realChecksum) {
298 throw new ZipException("bad CRC checksum "
299 + Long.toHexString(givenChecksum)
300 + " instead of "
301 + Long.toHexString(realChecksum));
302 }
303
304 int newMode = ZipShort.getValue(tmp, 0);
305 // CheckStyle:MagicNumber OFF
306 byte[] linkArray = new byte[(int) ZipLong.getValue(tmp, 2)];
307 uid = ZipShort.getValue(tmp, 6);
308 gid = ZipShort.getValue(tmp, 8);
309
310 if (linkArray.length == 0) {
311 link = "";
312 } else {
313 System.arraycopy(tmp, 10, linkArray, 0, linkArray.length);
314 link = new String(linkArray);
315 }
316 // CheckStyle:MagicNumber ON
317 setDirectory((newMode & DIR_FLAG) != 0);
318 setMode(newMode);
319 }
320
321 /**
322 * Get the file mode for given permissions with the correct file type.
323 * @param mode the mode
324 * @return the type with the mode
325 * @since 1.1
326 */
327 protected int getMode(int mode) {
328 int type = FILE_FLAG;
329 if (isLink()) {
330 type = LINK_FLAG;
331 } else if (isDirectory()) {
332 type = DIR_FLAG;
333 }
334 return type | (mode & PERM_MASK);
335 }
336
337 public Object clone() {
338 try {
339 AsiExtraField cloned = (AsiExtraField) super.clone();
340 cloned.crc = new CRC32();
341 return cloned;
342 } catch (CloneNotSupportedException cnfe) {
343 // impossible
344 throw new RuntimeException(cnfe);
345 }
346 }
347 }