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.tar;
20
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.UncheckedIOException;
24 import java.math.BigInteger;
25 import java.nio.ByteBuffer;
26 import java.nio.charset.Charset;
27 import java.nio.charset.StandardCharsets;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.commons.compress.archivers.zip.ZipEncoding;
35 import org.apache.commons.compress.archivers.zip.ZipEncodingHelper;
36 import org.apache.commons.compress.utils.IOUtils;
37 import org.apache.commons.compress.utils.ParsingUtils;
38 import org.apache.commons.io.output.ByteArrayOutputStream;
39
40
41
42
43
44
45
46 public class TarUtils {
47
48 private static final int BYTE_MASK = 255;
49
50 static final ZipEncoding DEFAULT_ENCODING = ZipEncodingHelper.getZipEncoding(Charset.defaultCharset());
51
52
53
54
55 static final ZipEncoding FALLBACK_ENCODING = new ZipEncoding() {
56
57 @Override
58 public boolean canEncode(final String name) {
59 return true;
60 }
61
62 @Override
63 public String decode(final byte[] buffer) {
64 final int length = buffer.length;
65 final StringBuilder result = new StringBuilder(length);
66 for (final byte b : buffer) {
67 if (b == 0) {
68 break;
69 }
70 result.append((char) (b & 0xFF));
71 }
72 return result.toString();
73 }
74
75 @Override
76 public ByteBuffer encode(final String name) {
77 final int length = name.length();
78 final byte[] buf = new byte[length];
79
80 for (int i = 0; i < length; ++i) {
81 buf[i] = (byte) name.charAt(i);
82 }
83 return ByteBuffer.wrap(buf);
84 }
85 };
86
87
88
89
90
91
92
93 public static long computeCheckSum(final byte[] buf) {
94 long sum = 0;
95 for (final byte element : buf) {
96 sum += BYTE_MASK & element;
97 }
98 return sum;
99 }
100
101
102 private static String exceptionMessage(final byte[] buffer, final int offset, final int length, final int current, final byte currentByte) {
103
104
105
106
107
108
109
110 String string = new String(buffer, offset, length, Charset.defaultCharset());
111
112 string = string.replace("\0", "{NUL}");
113 return "Invalid byte " + currentByte + " at offset " + (current - offset) + " in '" + string + "' len=" + length;
114 }
115
116 private static void formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative) {
117 final BigInteger val = BigInteger.valueOf(value);
118 final byte[] b = val.toByteArray();
119 final int len = b.length;
120 if (len > length - 1) {
121 throw new IllegalArgumentException("Value " + value + " is too large for " + length + " byte field.");
122 }
123 final int off = offset + length - len;
124 System.arraycopy(b, 0, buf, off, len);
125 final byte fill = (byte) (negative ? 0xff : 0);
126 for (int i = offset + 1; i < off; i++) {
127 buf[i] = fill;
128 }
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144 public static int formatCheckSumOctalBytes(final long value, final byte[] buf, final int offset, final int length) {
145 int idx = length - 2;
146 formatUnsignedOctalString(value, buf, offset, idx);
147 buf[offset + idx++] = 0;
148 buf[offset + idx] = (byte) ' ';
149 return offset + length;
150 }
151
152 private static void formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative) {
153 final int bits = (length - 1) * 8;
154 final long max = 1L << bits;
155 long val = Math.abs(value);
156 if (val < 0 || val >= max) {
157 throw new IllegalArgumentException("Value " + value + " is too large for " + length + " byte field.");
158 }
159 if (negative) {
160 val ^= max - 1;
161 val++;
162 val |= 0xffL << bits;
163 }
164 for (int i = offset + length - 1; i >= offset; i--) {
165 buf[i] = (byte) val;
166 val >>= 8;
167 }
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public static int formatLongOctalBytes(final long value, final byte[] buf, final int offset, final int length) {
183 final int idx = length - 1;
184 formatUnsignedOctalString(value, buf, offset, idx);
185 buf[offset + idx] = (byte) ' ';
186 return offset + length;
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 public static int formatLongOctalOrBinaryBytes(final long value, final byte[] buf, final int offset, final int length) {
203
204 final long maxAsOctalChar = length == TarConstants.UIDLEN ? TarConstants.MAXID : TarConstants.MAXSIZE;
205 final boolean negative = value < 0;
206 if (!negative && value <= maxAsOctalChar) {
207 return formatLongOctalBytes(value, buf, offset, length);
208 }
209 if (length < 9) {
210 formatLongBinary(value, buf, offset, length, negative);
211 } else {
212 formatBigIntegerBinary(value, buf, offset, length, negative);
213 }
214 buf[offset] = (byte) (negative ? 0xff : 0x80);
215 return offset + length;
216 }
217
218
219
220
221
222
223
224
225
226
227
228 public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length) {
229 try {
230 return formatNameBytes(name, buf, offset, length, DEFAULT_ENCODING);
231 } catch (final IOException ex) {
232 try {
233 return formatNameBytes(name, buf, offset, length, FALLBACK_ENCODING);
234 } catch (final IOException ex2) {
235
236 throw new UncheckedIOException(ex2);
237 }
238 }
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254 public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length, final ZipEncoding encoding) throws IOException {
255 int len = name.length();
256 ByteBuffer b = encoding.encode(name);
257 while (b.limit() > length && len > 0) {
258 b = encoding.encode(name.substring(0, --len));
259 }
260 final int limit = b.limit() - b.position();
261 System.arraycopy(b.array(), b.arrayOffset(), buf, offset, limit);
262
263 for (int i = limit; i < length; ++i) {
264 buf[offset + i] = 0;
265 }
266 return offset + length;
267 }
268
269
270
271
272
273
274
275
276
277
278
279
280
281 public static int formatOctalBytes(final long value, final byte[] buf, final int offset, final int length) {
282 int idx = length - 2;
283 formatUnsignedOctalString(value, buf, offset, idx);
284 buf[offset + idx++] = (byte) ' ';
285 buf[offset + idx] = 0;
286 return offset + length;
287 }
288
289
290
291
292
293
294
295
296
297
298 public static void formatUnsignedOctalString(final long value, final byte[] buffer, final int offset, final int length) {
299 int remaining = length;
300 remaining--;
301 if (value == 0) {
302 buffer[offset + remaining--] = (byte) '0';
303 } else {
304 long val = value;
305 for (; remaining >= 0 && val != 0; --remaining) {
306
307 buffer[offset + remaining] = (byte) ((byte) '0' + (byte) (val & 7));
308 val = val >>> 3;
309
310 }
311 if (val != 0) {
312 throw new IllegalArgumentException(value + "=" + Long.toOctalString(value) + " will not fit in octal number buffer of length " + length);
313 }
314 }
315
316 for (; remaining >= 0; --remaining) {
317 buffer[offset + remaining] = (byte) '0';
318 }
319 }
320
321 private static long parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative) {
322 final byte[] remainder = new byte[length - 1];
323 System.arraycopy(buffer, offset + 1, remainder, 0, length - 1);
324 BigInteger val = new BigInteger(remainder);
325 if (negative) {
326
327 val = val.add(BigInteger.valueOf(-1)).not();
328 }
329 if (val.bitLength() > 63) {
330 throw new IllegalArgumentException("At offset " + offset + ", " + length + " byte binary number exceeds maximum signed long value");
331 }
332 return negative ? -val.longValue() : val.longValue();
333 }
334
335 private static long parseBinaryLong(final byte[] buffer, final int offset, final int length, final boolean negative) {
336 if (length >= 9) {
337 throw new IllegalArgumentException("At offset " + offset + ", " + length + " byte binary number exceeds maximum signed long value");
338 }
339 long val = 0;
340 for (int i = 1; i < length; i++) {
341 val = (val << 8) + (buffer[offset + i] & 0xff);
342 }
343 if (negative) {
344
345 val--;
346 val ^= (long) Math.pow(2.0, (length - 1) * 8.0) - 1;
347 }
348 return negative ? -val : val;
349 }
350
351
352
353
354
355
356
357
358
359 public static boolean parseBoolean(final byte[] buffer, final int offset) {
360 return buffer[offset] == 1;
361 }
362
363
364
365
366
367
368
369
370
371
372 protected static List<TarArchiveStructSparse> parseFromPAX01SparseHeaders(final String sparseMap) throws IOException {
373 final List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>();
374 final String[] sparseHeaderStrings = sparseMap.split(",");
375 if (sparseHeaderStrings.length % 2 == 1) {
376 throw new IOException("Corrupted TAR archive. Bad format in GNU.sparse.map PAX Header");
377 }
378 for (int i = 0; i < sparseHeaderStrings.length; i += 2) {
379 final long sparseOffset = ParsingUtils.parseLongValue(sparseHeaderStrings[i]);
380 if (sparseOffset < 0) {
381 throw new IOException("Corrupted TAR archive. Sparse struct offset contains negative value");
382 }
383 final long sparseNumbytes = ParsingUtils.parseLongValue(sparseHeaderStrings[i + 1]);
384 if (sparseNumbytes < 0) {
385 throw new IOException("Corrupted TAR archive. Sparse struct numbytes contains negative value");
386 }
387 sparseHeaders.add(new TarArchiveStructSparse(sparseOffset, sparseNumbytes));
388 }
389 return Collections.unmodifiableList(sparseHeaders);
390 }
391
392
393
394
395
396
397
398
399
400 public static String parseName(final byte[] buffer, final int offset, final int length) {
401 try {
402 return parseName(buffer, offset, length, DEFAULT_ENCODING);
403 } catch (final IOException ex) {
404 try {
405 return parseName(buffer, offset, length, FALLBACK_ENCODING);
406 } catch (final IOException ex2) {
407
408 throw new UncheckedIOException(ex2);
409 }
410 }
411 }
412
413
414
415
416
417
418
419
420
421
422
423
424 public static String parseName(final byte[] buffer, final int offset, final int length, final ZipEncoding encoding) throws IOException {
425 int len = 0;
426 for (int i = offset; len < length && buffer[i] != 0; i++) {
427 len++;
428 }
429 if (len > 0) {
430 final byte[] b = new byte[len];
431 System.arraycopy(buffer, offset, b, 0, len);
432 return encoding.decode(b);
433 }
434 return "";
435 }
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458 public static long parseOctal(final byte[] buffer, final int offset, final int length) {
459 long result = 0;
460 int end = offset + length;
461 int start = offset;
462 if (length < 2) {
463 throw new IllegalArgumentException("Length " + length + " must be at least 2");
464 }
465 if (buffer[start] == 0) {
466 return 0L;
467 }
468
469 while (start < end) {
470 if (buffer[start] != ' ') {
471 break;
472 }
473 start++;
474 }
475
476
477
478
479 byte trailer = buffer[end - 1];
480 while (start < end && (trailer == 0 || trailer == ' ')) {
481 end--;
482 trailer = buffer[end - 1];
483 }
484 for (; start < end; start++) {
485 final byte currentByte = buffer[start];
486
487 if (currentByte < '0' || currentByte > '7') {
488 throw new IllegalArgumentException(exceptionMessage(buffer, offset, length, start, currentByte));
489 }
490 result = (result << 3) + (currentByte - '0');
491
492 }
493 return result;
494 }
495
496
497
498
499
500
501
502
503
504
505
506
507
508 public static long parseOctalOrBinary(final byte[] buffer, final int offset, final int length) {
509 if ((buffer[offset] & 0x80) == 0) {
510 return parseOctal(buffer, offset, length);
511 }
512 final boolean negative = buffer[offset] == (byte) 0xff;
513 if (length < 9) {
514 return parseBinaryLong(buffer, offset, length, negative);
515 }
516 return parseBinaryBigInteger(buffer, offset, length, negative);
517 }
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534 @Deprecated
535 protected static List<TarArchiveStructSparse> parsePAX01SparseHeaders(final String sparseMap) {
536 try {
537 return parseFromPAX01SparseHeaders(sparseMap);
538 } catch (final IOException ex) {
539 throw new UncheckedIOException(ex.getMessage(), ex);
540 }
541 }
542
543
544
545
546
547
548
549
550
551
552
553 protected static List<TarArchiveStructSparse> parsePAX1XSparseHeaders(final InputStream inputStream, final int recordSize) throws IOException {
554
555 final List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>();
556 long bytesRead = 0;
557 long[] readResult = readLineOfNumberForPax1X(inputStream);
558 long sparseHeadersCount = readResult[0];
559 if (sparseHeadersCount < 0) {
560
561 throw new IOException("Corrupted TAR archive. Negative value in sparse headers block");
562 }
563 bytesRead += readResult[1];
564 while (sparseHeadersCount-- > 0) {
565 readResult = readLineOfNumberForPax1X(inputStream);
566 final long sparseOffset = readResult[0];
567 if (sparseOffset < 0) {
568 throw new IOException("Corrupted TAR archive. Sparse header block offset contains negative value");
569 }
570 bytesRead += readResult[1];
571
572 readResult = readLineOfNumberForPax1X(inputStream);
573 final long sparseNumbytes = readResult[0];
574 if (sparseNumbytes < 0) {
575 throw new IOException("Corrupted TAR archive. Sparse header block numbytes contains negative value");
576 }
577 bytesRead += readResult[1];
578 sparseHeaders.add(new TarArchiveStructSparse(sparseOffset, sparseNumbytes));
579 }
580
581 final long bytesToSkip = recordSize - bytesRead % recordSize;
582 org.apache.commons.io.IOUtils.skip(inputStream, bytesToSkip);
583 return sparseHeaders;
584 }
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611 @Deprecated
612 protected static Map<String, String> parsePaxHeaders(final InputStream inputStream, final List<TarArchiveStructSparse> sparseHeaders,
613 final Map<String, String> globalPaxHeaders) throws IOException {
614 return parsePaxHeaders(inputStream, sparseHeaders, globalPaxHeaders, -1);
615 }
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643 protected static Map<String, String> parsePaxHeaders(final InputStream inputStream, final List<TarArchiveStructSparse> sparseHeaders,
644 final Map<String, String> globalPaxHeaders, final long headerSize) throws IOException {
645 final Map<String, String> headers = new HashMap<>(globalPaxHeaders);
646 Long offset = null;
647
648 int totalRead = 0;
649 while (true) {
650 int ch;
651 int len = 0;
652 int read = 0;
653 while ((ch = inputStream.read()) != -1) {
654 read++;
655 totalRead++;
656 if (ch == '\n') {
657 break;
658 }
659 if (ch == ' ') {
660
661 final ByteArrayOutputStream coll = new ByteArrayOutputStream();
662 while ((ch = inputStream.read()) != -1) {
663 read++;
664 totalRead++;
665 if (totalRead < 0 || headerSize >= 0 && totalRead >= headerSize) {
666 break;
667 }
668 if (ch == '=') {
669 final String keyword = coll.toString(StandardCharsets.UTF_8);
670
671 final int restLen = len - read;
672 if (restLen <= 1) {
673 headers.remove(keyword);
674 } else if (headerSize >= 0 && restLen > headerSize - totalRead) {
675 throw new IOException("Paxheader value size " + restLen + " exceeds size of header record");
676 } else {
677 final byte[] rest = IOUtils.readRange(inputStream, restLen);
678 final int got = rest.length;
679 if (got != restLen) {
680 throw new IOException("Failed to read Paxheader. Expected " + restLen + " bytes, read " + got);
681 }
682 totalRead += restLen;
683
684 if (rest[restLen - 1] != '\n') {
685 throw new IOException("Failed to read Paxheader." + "Value should end with a newline");
686 }
687 final String value = new String(rest, 0, restLen - 1, StandardCharsets.UTF_8);
688 headers.put(keyword, value);
689
690
691 if (keyword.equals(TarGnuSparseKeys.OFFSET)) {
692 if (offset != null) {
693
694 sparseHeaders.add(new TarArchiveStructSparse(offset, 0));
695 }
696 try {
697 offset = Long.valueOf(value);
698 } catch (final NumberFormatException ex) {
699 throw new IOException("Failed to read Paxheader." + TarGnuSparseKeys.OFFSET + " contains a non-numeric value");
700 }
701 if (offset < 0) {
702 throw new IOException("Failed to read Paxheader." + TarGnuSparseKeys.OFFSET + " contains negative value");
703 }
704 }
705
706
707 if (keyword.equals(TarGnuSparseKeys.NUMBYTES)) {
708 if (offset == null) {
709 throw new IOException(
710 "Failed to read Paxheader." + TarGnuSparseKeys.OFFSET + " is expected before GNU.sparse.numbytes shows up.");
711 }
712 final long numbytes = ParsingUtils.parseLongValue(value);
713 if (numbytes < 0) {
714 throw new IOException("Failed to read Paxheader." + TarGnuSparseKeys.NUMBYTES + " contains negative value");
715 }
716 sparseHeaders.add(new TarArchiveStructSparse(offset, numbytes));
717 offset = null;
718 }
719 }
720 break;
721 }
722 coll.write((byte) ch);
723 }
724 break;
725 }
726
727 if (ch < '0' || ch > '9') {
728 throw new IOException("Failed to read Paxheader. Encountered a non-number while reading length");
729 }
730 len *= 10;
731 len += ch - '0';
732 }
733 if (ch == -1) {
734 break;
735 }
736 }
737 if (offset != null) {
738
739 sparseHeaders.add(new TarArchiveStructSparse(offset, 0));
740 }
741 return headers;
742 }
743
744
745
746
747
748
749
750
751
752 public static TarArchiveStructSparse parseSparse(final byte[] buffer, final int offset) {
753 final long sparseOffset = parseOctalOrBinary(buffer, offset, TarConstants.SPARSE_OFFSET_LEN);
754 final long sparseNumbytes = parseOctalOrBinary(buffer, offset + TarConstants.SPARSE_OFFSET_LEN, TarConstants.SPARSE_NUMBYTES_LEN);
755 return new TarArchiveStructSparse(sparseOffset, sparseNumbytes);
756 }
757
758
759
760
761
762
763
764
765
766 private static long[] readLineOfNumberForPax1X(final InputStream inputStream) throws IOException {
767 int number;
768 long result = 0;
769 long bytesRead = 0;
770 while ((number = inputStream.read()) != '\n') {
771 bytesRead += 1;
772 if (number == -1) {
773 throw new IOException("Unexpected EOF when reading parse information of 1.X PAX format");
774 }
775 if (number < '0' || number > '9') {
776 throw new IOException("Corrupted TAR archive. Non-numeric value in sparse headers block");
777 }
778 result = result * 10 + (number - '0');
779 }
780 bytesRead += 1;
781 return new long[] { result, bytesRead };
782 }
783
784
785
786
787 static List<TarArchiveStructSparse> readSparseStructs(final byte[] buffer, final int offset, final int entries) throws IOException {
788 final List<TarArchiveStructSparse> sparseHeaders = new ArrayList<>();
789 for (int i = 0; i < entries; i++) {
790 try {
791 final TarArchiveStructSparse sparseHeader = parseSparse(buffer,
792 offset + i * (TarConstants.SPARSE_OFFSET_LEN + TarConstants.SPARSE_NUMBYTES_LEN));
793 if (sparseHeader.getOffset() < 0) {
794 throw new IOException("Corrupted TAR archive, sparse entry with negative offset");
795 }
796 if (sparseHeader.getNumbytes() < 0) {
797 throw new IOException("Corrupted TAR archive, sparse entry with negative numbytes");
798 }
799 sparseHeaders.add(sparseHeader);
800 } catch (final IllegalArgumentException ex) {
801
802 throw new IOException("Corrupted TAR archive, sparse entry is invalid", ex);
803 }
804 }
805 return Collections.unmodifiableList(sparseHeaders);
806 }
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824 public static boolean verifyCheckSum(final byte[] header) {
825 final long storedSum = parseOctal(header, TarConstants.CHKSUM_OFFSET, TarConstants.CHKSUMLEN);
826 long unsignedSum = 0;
827 long signedSum = 0;
828 for (int i = 0; i < header.length; i++) {
829 byte b = header[i];
830 if (TarConstants.CHKSUM_OFFSET <= i && i < TarConstants.CHKSUM_OFFSET + TarConstants.CHKSUMLEN) {
831 b = ' ';
832 }
833 unsignedSum += 0xff & b;
834 signedSum += b;
835 }
836 return storedSum == unsignedSum || storedSum == signedSum;
837 }
838
839
840 private TarUtils() {
841 }
842
843 }