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.ar;
20
21 import java.io.EOFException;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.nio.charset.StandardCharsets;
25 import java.util.Arrays;
26 import java.util.regex.Pattern;
27
28 import org.apache.commons.compress.archivers.ArchiveInputStream;
29 import org.apache.commons.compress.utils.ArchiveUtils;
30 import org.apache.commons.compress.utils.IOUtils;
31 import org.apache.commons.compress.utils.ParsingUtils;
32
33
34
35
36
37
38 public class ArArchiveInputStream extends ArchiveInputStream<ArArchiveEntry> {
39
40
41 private static final int NAME_OFFSET = 0;
42 private static final int NAME_LEN = 16;
43 private static final int LAST_MODIFIED_OFFSET = NAME_LEN;
44
45 private static final int LAST_MODIFIED_LEN = 12;
46
47 private static final int USER_ID_OFFSET = LAST_MODIFIED_OFFSET + LAST_MODIFIED_LEN;
48
49 private static final int USER_ID_LEN = 6;
50
51 private static final int GROUP_ID_OFFSET = USER_ID_OFFSET + USER_ID_LEN;
52 private static final int GROUP_ID_LEN = 6;
53 private static final int FILE_MODE_OFFSET = GROUP_ID_OFFSET + GROUP_ID_LEN;
54 private static final int FILE_MODE_LEN = 8;
55 private static final int LENGTH_OFFSET = FILE_MODE_OFFSET + FILE_MODE_LEN;
56 private static final int LENGTH_LEN = 10;
57 static final String BSD_LONGNAME_PREFIX = "#1/";
58 private static final int BSD_LONGNAME_PREFIX_LEN = BSD_LONGNAME_PREFIX.length();
59 private static final Pattern BSD_LONGNAME_PATTERN = Pattern.compile("^" + BSD_LONGNAME_PREFIX + "\\d+");
60 private static final String GNU_STRING_TABLE_NAME = "//";
61 private static final Pattern GNU_LONGNAME_PATTERN = Pattern.compile("^/\\d+");
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85 private static boolean isBSDLongName(final String name) {
86 return name != null && BSD_LONGNAME_PATTERN.matcher(name).matches();
87 }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 private static boolean isGNUStringTable(final String name) {
104 return GNU_STRING_TABLE_NAME.equals(name);
105 }
106
107
108
109
110
111
112
113
114 public static boolean matches(final byte[] signature, final int length) {
115
116
117 return length >= 8 && signature[0] == 0x21 && signature[1] == 0x3c && signature[2] == 0x61 && signature[3] == 0x72 && signature[4] == 0x63
118 && signature[5] == 0x68 && signature[6] == 0x3e && signature[7] == 0x0a;
119 }
120
121 private long offset;
122
123 private boolean closed;
124
125
126
127
128 private ArArchiveEntry currentEntry;
129
130
131 private byte[] namebuffer;
132
133
134
135
136 private long entryOffset = -1;
137
138
139 private final byte[] metaData = new byte[NAME_LEN + LAST_MODIFIED_LEN + USER_ID_LEN + GROUP_ID_LEN + FILE_MODE_LEN + LENGTH_LEN];
140
141
142
143
144
145
146 public ArArchiveInputStream(final InputStream inputStream) {
147 super(inputStream, StandardCharsets.US_ASCII.name());
148 }
149
150 private int asInt(final byte[] byteArray, final int offset, final int len) throws IOException {
151 return asInt(byteArray, offset, len, 10, false);
152 }
153
154 private int asInt(final byte[] byteArray, final int offset, final int len, final boolean treatBlankAsZero) throws IOException {
155 return asInt(byteArray, offset, len, 10, treatBlankAsZero);
156 }
157
158 private int asInt(final byte[] byteArray, final int offset, final int len, final int base) throws IOException {
159 return asInt(byteArray, offset, len, base, false);
160 }
161
162 private int asInt(final byte[] byteArray, final int offset, final int len, final int base, final boolean treatBlankAsZero) throws IOException {
163 final String string = ArchiveUtils.toAsciiString(byteArray, offset, len).trim();
164 if (string.isEmpty() && treatBlankAsZero) {
165 return 0;
166 }
167 return ParsingUtils.parseIntValue(string, base);
168 }
169
170 private long asLong(final byte[] byteArray, final int offset, final int len) throws IOException {
171 return ParsingUtils.parseLongValue(ArchiveUtils.toAsciiString(byteArray, offset, len).trim());
172 }
173
174
175
176
177
178
179 @Override
180 public void close() throws IOException {
181 if (!closed) {
182 closed = true;
183 in.close();
184 }
185 currentEntry = null;
186 }
187
188
189
190
191
192
193
194
195 private String getBSDLongName(final String bsdLongName) throws IOException {
196 final int nameLen = ParsingUtils.parseIntValue(bsdLongName.substring(BSD_LONGNAME_PREFIX_LEN));
197 final byte[] name = IOUtils.readRange(in, nameLen);
198 final int read = name.length;
199 trackReadBytes(read);
200 if (read != nameLen) {
201 throw new EOFException();
202 }
203 return ArchiveUtils.toAsciiString(name);
204 }
205
206
207
208
209
210
211
212
213 private String getExtendedName(final int offset) throws IOException {
214 if (namebuffer == null) {
215 throw new IOException("Cannot process GNU long file name as no // record was found");
216 }
217 for (int i = offset; i < namebuffer.length; i++) {
218 if (namebuffer[i] == '\012' || namebuffer[i] == 0) {
219
220 if (i == 0) {
221 break;
222 }
223 if (namebuffer[i - 1] == '/') {
224 i--;
225 }
226
227
228 if (i - offset > 0) {
229 return ArchiveUtils.toAsciiString(namebuffer, offset, i - offset);
230 }
231 break;
232 }
233 }
234 throw new IOException("Failed to read entry: " + offset);
235 }
236
237
238
239
240
241
242
243
244 @Deprecated
245 public ArArchiveEntry getNextArEntry() throws IOException {
246 if (currentEntry != null) {
247 final long entryEnd = entryOffset + currentEntry.getLength();
248 final long skipped = org.apache.commons.io.IOUtils.skip(in, entryEnd - offset);
249 trackReadBytes(skipped);
250 currentEntry = null;
251 }
252
253 if (offset == 0) {
254 final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.HEADER);
255 final byte[] realized = IOUtils.readRange(in, expected.length);
256 final int read = realized.length;
257 trackReadBytes(read);
258 if (read != expected.length) {
259 throw new IOException("Failed to read header. Occurred at byte: " + getBytesRead());
260 }
261 if (!Arrays.equals(expected, realized)) {
262 throw new IOException("Invalid header " + ArchiveUtils.toAsciiString(realized));
263 }
264 }
265
266 if (offset % 2 != 0) {
267 if (in.read() < 0) {
268
269 return null;
270 }
271 trackReadBytes(1);
272 }
273
274 {
275 final int read = IOUtils.readFully(in, metaData);
276 trackReadBytes(read);
277 if (read == 0) {
278 return null;
279 }
280 if (read < metaData.length) {
281 throw new IOException("Truncated ar archive");
282 }
283 }
284
285 {
286 final byte[] expected = ArchiveUtils.toAsciiBytes(ArArchiveEntry.TRAILER);
287 final byte[] realized = IOUtils.readRange(in, expected.length);
288 final int read = realized.length;
289 trackReadBytes(read);
290 if (read != expected.length) {
291 throw new IOException("Failed to read entry trailer. Occurred at byte: " + getBytesRead());
292 }
293 if (!Arrays.equals(expected, realized)) {
294 throw new IOException("Invalid entry trailer. not read the content? Occurred at byte: " + getBytesRead());
295 }
296 }
297
298 entryOffset = offset;
299
300
301
302
303 String temp = ArchiveUtils.toAsciiString(metaData, NAME_OFFSET, NAME_LEN).trim();
304 if (isGNUStringTable(temp)) {
305 currentEntry = readGNUStringTable(metaData, LENGTH_OFFSET, LENGTH_LEN);
306 return getNextArEntry();
307 }
308
309 long len;
310 try {
311 len = asLong(metaData, LENGTH_OFFSET, LENGTH_LEN);
312 } catch (final NumberFormatException ex) {
313 throw new IOException("Broken archive, unable to parse ar_size field as a number", ex);
314 }
315 if (temp.endsWith("/")) {
316 temp = temp.substring(0, temp.length() - 1);
317 } else if (isGNULongName(temp)) {
318 final int off = ParsingUtils.parseIntValue(temp.substring(1));
319 temp = getExtendedName(off);
320 } else if (isBSDLongName(temp)) {
321 temp = getBSDLongName(temp);
322
323
324
325 final int nameLen = temp.length();
326 len -= nameLen;
327 entryOffset += nameLen;
328 }
329
330 if (len < 0) {
331 throw new IOException("broken archive, entry with negative size");
332 }
333
334 try {
335 currentEntry = new ArArchiveEntry(temp, len, asInt(metaData, USER_ID_OFFSET, USER_ID_LEN, true),
336 asInt(metaData, GROUP_ID_OFFSET, GROUP_ID_LEN, true), asInt(metaData, FILE_MODE_OFFSET, FILE_MODE_LEN, 8),
337 asLong(metaData, LAST_MODIFIED_OFFSET, LAST_MODIFIED_LEN));
338 return currentEntry;
339 } catch (final NumberFormatException ex) {
340 throw new IOException("Broken archive, unable to parse entry metadata fields as numbers", ex);
341 }
342 }
343
344
345
346
347
348
349 @Override
350 public ArArchiveEntry getNextEntry() throws IOException {
351 return getNextArEntry();
352 }
353
354
355
356
357
358
359 private boolean isGNULongName(final String name) {
360 return name != null && GNU_LONGNAME_PATTERN.matcher(name).matches();
361 }
362
363
364
365
366
367
368 @Override
369 public int read(final byte[] b, final int off, final int len) throws IOException {
370 if (len == 0) {
371 return 0;
372 }
373 if (currentEntry == null) {
374 throw new IllegalStateException("No current ar entry");
375 }
376 final long entryEnd = entryOffset + currentEntry.getLength();
377 if (len < 0 || offset >= entryEnd) {
378 return -1;
379 }
380 final int toRead = (int) Math.min(len, entryEnd - offset);
381 final int ret = this.in.read(b, off, toRead);
382 trackReadBytes(ret);
383 return ret;
384 }
385
386
387
388
389
390
391 private ArArchiveEntry readGNUStringTable(final byte[] length, final int offset, final int len) throws IOException {
392 int bufflen;
393 try {
394 bufflen = asInt(length, offset, len);
395 } catch (final NumberFormatException ex) {
396 throw new IOException("Broken archive, unable to parse GNU string table length field as a number", ex);
397 }
398
399 namebuffer = IOUtils.readRange(in, bufflen);
400 final int read = namebuffer.length;
401 trackReadBytes(read);
402 if (read != bufflen) {
403 throw new IOException("Failed to read complete // record: expected=" + bufflen + " read=" + read);
404 }
405 return new ArArchiveEntry(GNU_STRING_TABLE_NAME, bufflen);
406 }
407
408 private void trackReadBytes(final long read) {
409 count(read);
410 if (read > 0) {
411 offset += read;
412 }
413 }
414 }