| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 43 |
|
| 44 |
java.util.zip.CRC32 crc32 = new java.util.zip.CRC32(); |
| 45 |
crc32.update('\0'); |
| 46 |
CheckedInputStream inChecked = new CheckedInputStream(in, crc32); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 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 |
} |