1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.archivers.zip;
20
21 import static org.apache.commons.compress.archivers.zip.ZipConstants.SHORT;
22 import static org.apache.commons.compress.archivers.zip.ZipConstants.WORD;
23
24 import java.nio.charset.Charset;
25 import java.util.zip.CRC32;
26 import java.util.zip.ZipException;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62 public class AsiExtraField implements ZipExtraField, UnixStat, Cloneable {
63
64 static final ZipShort HEADER_ID = new ZipShort(0x756E);
65 private static final int MIN_SIZE = WORD + SHORT + WORD + SHORT + SHORT;
66
67
68
69
70 private int mode;
71
72
73
74 private int uid;
75
76
77
78 private int gid;
79
80
81
82
83
84
85
86 private String link = "";
87
88
89
90 private boolean dirFlag;
91
92
93
94
95 private CRC32 crc = new CRC32();
96
97
98 public AsiExtraField() {
99 }
100
101 @Override
102 public Object clone() {
103 try {
104 final AsiExtraField cloned = (AsiExtraField) super.clone();
105 cloned.crc = new CRC32();
106 return cloned;
107 } catch (final CloneNotSupportedException cnfe) {
108
109 throw new UnsupportedOperationException(cnfe);
110 }
111 }
112
113
114
115
116
117
118 @Override
119 public byte[] getCentralDirectoryData() {
120 return getLocalFileDataData();
121 }
122
123
124
125
126
127
128 @Override
129 public ZipShort getCentralDirectoryLength() {
130 return getLocalFileDataLength();
131 }
132
133
134
135
136
137
138 public int getGroupId() {
139 return gid;
140 }
141
142
143
144
145
146
147 @Override
148 public ZipShort getHeaderId() {
149 return HEADER_ID;
150 }
151
152
153
154
155
156
157 public String getLinkedFile() {
158 return link;
159 }
160
161
162
163
164
165
166 @Override
167 public byte[] getLocalFileDataData() {
168
169 final byte[] data = new byte[getLocalFileDataLength().getValue() - WORD];
170 System.arraycopy(ZipShort.getBytes(getMode()), 0, data, 0, 2);
171
172 final byte[] linkArray = getLinkedFile().getBytes(Charset.defaultCharset());
173
174 System.arraycopy(ZipLong.getBytes(linkArray.length), 0, data, 2, WORD);
175
176 System.arraycopy(ZipShort.getBytes(getUserId()), 0, data, 6, 2);
177 System.arraycopy(ZipShort.getBytes(getGroupId()), 0, data, 8, 2);
178
179 System.arraycopy(linkArray, 0, data, 10, linkArray.length);
180
181
182 crc.reset();
183 crc.update(data);
184 final long checksum = crc.getValue();
185
186 final byte[] result = new byte[data.length + WORD];
187 System.arraycopy(ZipLong.getBytes(checksum), 0, result, 0, WORD);
188 System.arraycopy(data, 0, result, WORD, data.length);
189 return result;
190 }
191
192
193
194
195
196
197 @Override
198 public ZipShort getLocalFileDataLength() {
199
200 return new ZipShort(WORD
201 + 2
202 + WORD
203 + 2
204 + 2
205 + getLinkedFile().getBytes(Charset.defaultCharset()).length);
206
207
208 }
209
210
211
212
213
214
215 public int getMode() {
216 return mode;
217 }
218
219
220
221
222
223
224
225 protected int getMode(final int mode) {
226 int type = FILE_FLAG;
227 if (isLink()) {
228 type = LINK_FLAG;
229 } else if (isDirectory()) {
230 type = DIR_FLAG;
231 }
232 return type | mode & PERM_MASK;
233 }
234
235
236
237
238
239
240 public int getUserId() {
241 return uid;
242 }
243
244
245
246
247
248
249 public boolean isDirectory() {
250 return dirFlag && !isLink();
251 }
252
253
254
255
256
257
258 public boolean isLink() {
259 return !getLinkedFile().isEmpty();
260 }
261
262
263
264
265 @Override
266 public void parseFromCentralDirectoryData(final byte[] buffer, final int offset, final int length) throws ZipException {
267 parseFromLocalFileData(buffer, offset, length);
268 }
269
270
271
272
273
274
275
276
277
278 @Override
279 public void parseFromLocalFileData(final byte[] data, final int offset, final int length) throws ZipException {
280 if (length < MIN_SIZE) {
281 throw new ZipException("The length is too short, only " + length + " bytes, expected at least " + MIN_SIZE);
282 }
283
284 final long givenChecksum = ZipLong.getValue(data, offset);
285 final byte[] tmp = new byte[length - WORD];
286 System.arraycopy(data, offset + WORD, tmp, 0, length - WORD);
287 crc.reset();
288 crc.update(tmp);
289 final long realChecksum = crc.getValue();
290 if (givenChecksum != realChecksum) {
291 throw new ZipException("Bad CRC checksum, expected " + Long.toHexString(givenChecksum) + " instead of " + Long.toHexString(realChecksum));
292 }
293
294 final int newMode = ZipShort.getValue(tmp, 0);
295
296 final int linkArrayLength = (int) ZipLong.getValue(tmp, 2);
297 if (linkArrayLength < 0 || linkArrayLength > tmp.length - 10) {
298 throw new ZipException("Bad symbolic link name length " + linkArrayLength + " in ASI extra field");
299 }
300 uid = ZipShort.getValue(tmp, 6);
301 gid = ZipShort.getValue(tmp, 8);
302 if (linkArrayLength == 0) {
303 link = "";
304 } else {
305 final byte[] linkArray = new byte[linkArrayLength];
306 System.arraycopy(tmp, 10, linkArray, 0, linkArrayLength);
307 link = new String(linkArray, Charset.defaultCharset());
308 }
309
310 setDirectory((newMode & DIR_FLAG) != 0);
311 setMode(newMode);
312 }
313
314
315
316
317
318
319 public void setDirectory(final boolean dirFlag) {
320 this.dirFlag = dirFlag;
321 mode = getMode(mode);
322 }
323
324
325
326
327
328
329 public void setGroupId(final int gid) {
330 this.gid = gid;
331 }
332
333
334
335
336
337
338 public void setLinkedFile(final String name) {
339 link = name;
340 mode = getMode(mode);
341 }
342
343
344
345
346
347
348 public void setMode(final int mode) {
349 this.mode = getMode(mode);
350 }
351
352
353
354
355
356
357 public void setUserId(final int uid) {
358 this.uid = uid;
359 }
360 }