Debian Codegraph @

src/org/tukaani/xz/index/IndexHash.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4
5 package org.tukaani.xz.index;
6
7 import java.io.InputStream;
8 import java.io.DataInputStream;
9 import java.io.IOException;
10 import java.nio.ByteBuffer;
11 import java.util.Arrays;
12 import java.util.zip.CheckedInputStream;
13 import org.tukaani.xz.common.DecoderUtil;
14 import org.tukaani.xz.XZIOException;
15 import org.tukaani.xz.CorruptedInputException;
16
17 public class IndexHash extends IndexBase {
18 private org.tukaani.xz.check.Check hash;
19
20 public IndexHash() {
21 super(new CorruptedInputException());
22
23 try {
24 hash = new org.tukaani.xz.check.SHA256();
25 } catch (java.security.NoSuchAlgorithmException e) {
26 hash = new org.tukaani.xz.check.CRC32();
27 }
28 }
29
30 @Override
31 public void add(long unpaddedSize, long uncompressedSize)
32 throws XZIOException {
33 super.add(unpaddedSize, uncompressedSize);
34
35 ByteBuffer buf = ByteBuffer.allocate(2 * 8);
36 buf.putLong(unpaddedSize);
37 buf.putLong(uncompressedSize);
38 hash.update(buf.array());
39 }
40
41 public void validate(InputStream in) throws IOException {
42 // Index Indicator (0x00) has already been read by BlockInputStream
43 // so add 0x00 to the CRC32 here.
44 java.util.zip.CRC32 crc32 = new java.util.zip.CRC32();
45 crc32.update('\0');
46 CheckedInputStream inChecked = new CheckedInputStream(in, crc32);
47
48 // Get and validate the Number of Records field.
49 // If Block Header Size was corrupt and became Index Indicator,
50 // this error would actually be about corrupt Block Header.
51 // This is why the error message mentions both possibilities.
52 long storedRecordCount = DecoderUtil.decodeVLI(inChecked);
53 if (storedRecordCount != recordCount)
54 throw new CorruptedInputException(
55 "XZ Block Header or the start of XZ Index is corrupt");
56
57 // Decode and hash the Index field and compare it to
58 // the hash value calculated from the decoded Blocks.
59 IndexHash stored = new IndexHash();
60 for (long i = 0; i < recordCount; ++i) {
61 long unpaddedSize = DecoderUtil.decodeVLI(inChecked);
62 long uncompressedSize = DecoderUtil.decodeVLI(inChecked);
63
64 try {
65 stored.add(unpaddedSize, uncompressedSize);
66 } catch (XZIOException e) {
67 throw new CorruptedInputException("XZ Index is corrupt");
68 }
69
70 if (stored.blocksSum > blocksSum
71 || stored.uncompressedSum > uncompressedSum
72 || stored.indexListSize > indexListSize)
73 throw new CorruptedInputException("XZ Index is corrupt");
74 }
75
76 if (stored.blocksSum != blocksSum
77 || stored.uncompressedSum != uncompressedSum
78 || stored.indexListSize != indexListSize
79 || !Arrays.equals(stored.hash.finish(), hash.finish()))
80 throw new CorruptedInputException("XZ Index is corrupt");
81
82 // Index Padding
83 DataInputStream inData = new DataInputStream(inChecked);
84 for (int i = getIndexPaddingSize(); i > 0; --i)
85 if (inData.readUnsignedByte() != 0x00)
86 throw new CorruptedInputException("XZ Index is corrupt");
87
88 // CRC32
89 long value = crc32.getValue();
90 for (int i = 0; i < 4; ++i)
91 if (((value >>> (i * 8)) & 0xFF) != inData.readUnsignedByte())
92 throw new CorruptedInputException("XZ Index is corrupt");
93 }
94 }