package org.apache.commons.compress.compressors.lz4;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.utils.BoundedInputStream;
import org.apache.commons.compress.utils.ByteUtils;
import org.apache.commons.compress.utils.ChecksumCalculatingInputStream;
import org.apache.commons.compress.utils.CountingInputStream;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.compress.utils.InputStreamStatistics;
public class FramedLZ4CompressorInputStream extends CompressorInputStream
implements InputStreamStatistics {
static final byte[] LZ4_SIGNATURE = new byte[] {
4, 0x22, 0x4d, 0x18
};
private static final byte[] SKIPPABLE_FRAME_TRAILER = new byte[] {
0x2a, 0x4d, 0x18
};
private static final byte SKIPPABLE_FRAME_PREFIX_BYTE_MASK = 0x50;
static final int VERSION_MASK = 0xC0;
static final int SUPPORTED_VERSION = 0x40;
static final int BLOCK_INDEPENDENCE_MASK = 0x20;
static final int BLOCK_CHECKSUM_MASK = 0x10;
static final int CONTENT_SIZE_MASK = 0x08;
static final int CONTENT_CHECKSUM_MASK = 0x04;
static final int BLOCK_MAX_SIZE_MASK = 0x70;
static final int UNCOMPRESSED_FLAG_MASK = 0x80000000;
private final byte[] oneByte = new byte[1];
private final ByteUtils.ByteSupplier supplier = new ByteUtils.ByteSupplier() {
@Override
public int getAsByte() throws IOException {
return readOneByte();
}
};
private final CountingInputStream inputStream;
private final boolean decompressConcatenated;
private boolean expectBlockChecksum;
private boolean expectBlockDependency;
private boolean expectContentSize;
private boolean expectContentChecksum;
private InputStream currentBlock;
private boolean endReached, inUncompressed;
private final XXHash32 contentHash = new XXHash32();
private final XXHash32 blockHash = new XXHash32();
private byte[] blockDependencyBuffer;
public FramedLZ4CompressorInputStream(InputStream in) throws IOException {
this(in, false);
}
public FramedLZ4CompressorInputStream(InputStream in, boolean decompressConcatenated) throws IOException {
this.inputStream = new CountingInputStream(in);
this.decompressConcatenated = decompressConcatenated;
init(true);
}
@Override
public int read() throws IOException {
return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] & 0xFF;
}
@Override
public void close() throws IOException {
try {
if (currentBlock != null) {
currentBlock.close();
currentBlock = null;
}
} finally {
inputStream.close();
}
}
@Override
public int read(final byte[] b, final int off, final int len) throws IOException {
if (len == 0) {
return 0;
}
if (endReached) {
return -1;
}
int r = readOnce(b, off, len);
if (r == -1) {
nextBlock();
if (!endReached) {
r = readOnce(b, off, len);
}
}
if (r != -1) {
if (expectBlockDependency) {
appendToBlockDependencyBuffer(b, off, r);
}
if (expectContentChecksum) {
contentHash.update(b, off, r);
}
}
return r;
}
@Override
public long getCompressedCount() {
return inputStream.getBytesRead();
}
private void init(boolean firstFrame) throws IOException {
if (readSignature(firstFrame)) {
readFrameDescriptor();
nextBlock();
}
}
private boolean readSignature(boolean firstFrame) throws IOException {
String garbageMessage = firstFrame ? "Not a LZ4 frame stream" : "LZ4 frame stream followed by garbage";
final byte[] b = new byte[4];
int read = IOUtils.readFully(inputStream, b);
count(read);
if (0 == read && !firstFrame) {
endReached = true;
return false;
}
if (4 != read) {
throw new IOException(garbageMessage);
}
read = skipSkippableFrame(b);
if (0 == read && !firstFrame) {
endReached = true;
return false;
}
if (4 != read || !matches(b, 4)) {
throw new IOException(garbageMessage);
}
return true;
}
private void readFrameDescriptor() throws IOException {
int flags = readOneByte();
if (flags == -1) {
throw new IOException("Premature end of stream while reading frame flags");
}
contentHash.update(flags);
if ((flags & VERSION_MASK) != SUPPORTED_VERSION) {
throw new IOException("Unsupported version " + (flags >> 6));
}
expectBlockDependency = (flags & BLOCK_INDEPENDENCE_MASK) == 0;
if (expectBlockDependency) {
if (blockDependencyBuffer == null) {
blockDependencyBuffer = new byte[BlockLZ4CompressorInputStream.WINDOW_SIZE];
}
} else {
blockDependencyBuffer = null;
}
expectBlockChecksum = (flags & BLOCK_CHECKSUM_MASK) != 0;
expectContentSize = (flags & CONTENT_SIZE_MASK) != 0;
expectContentChecksum = (flags & CONTENT_CHECKSUM_MASK) != 0;
int bdByte = readOneByte();
if (bdByte == -1) {
throw new IOException("Premature end of stream while reading frame BD byte");
}
contentHash.update(bdByte);
if (expectContentSize) {
byte[] contentSize = new byte[8];
int skipped = IOUtils.readFully(inputStream, contentSize);
count(skipped);
if (8 != skipped) {
throw new IOException("Premature end of stream while reading content size");
}
contentHash.update(contentSize, 0, contentSize.length);
}
int headerHash = readOneByte();
if (headerHash == -1) {
throw new IOException("Premature end of stream while reading frame header checksum");
}
int expectedHash = (int) ((contentHash.getValue() >> 8) & 0xff);
contentHash.reset();
if (headerHash != expectedHash) {
throw new IOException("Frame header checksum mismatch");
}
}
private void nextBlock() throws IOException {
maybeFinishCurrentBlock();
long len = ByteUtils.fromLittleEndian(supplier, 4);
boolean uncompressed = (len & UNCOMPRESSED_FLAG_MASK) != 0;
int realLen = (int) (len & (~UNCOMPRESSED_FLAG_MASK));
if (realLen < 0) {
throw new IOException("Found illegal block with negative size");
}
if (realLen == 0) {
verifyContentChecksum();
if (!decompressConcatenated) {
endReached = true;
} else {
init(false);
}
return;
}
InputStream capped = new BoundedInputStream(inputStream, realLen);
if (expectBlockChecksum) {
capped = new ChecksumCalculatingInputStream(blockHash, capped);
}
if (uncompressed) {
inUncompressed = true;
currentBlock = capped;
} else {
inUncompressed = false;
BlockLZ4CompressorInputStream s = new BlockLZ4CompressorInputStream(capped);
if (expectBlockDependency) {
s.prefill(blockDependencyBuffer);
}
currentBlock = s;
}
}
private void maybeFinishCurrentBlock() throws IOException {
if (currentBlock != null) {
currentBlock.close();
currentBlock = null;
if (expectBlockChecksum) {
verifyChecksum(blockHash, "block");
blockHash.reset();
}
}
}
private void verifyContentChecksum() throws IOException {
if (expectContentChecksum) {
verifyChecksum(contentHash, "content");
}
contentHash.reset();
}
private void verifyChecksum(XXHash32 hash, String kind) throws IOException {
byte[] checksum = new byte[4];
int read = IOUtils.readFully(inputStream, checksum);
count(read);
if (4 != read) {
throw new IOException("Premature end of stream while reading " + kind + " checksum");
}
long expectedHash = hash.getValue();
if (expectedHash != ByteUtils.fromLittleEndian(checksum)) {
throw new IOException(kind + " checksum mismatch.");
}
}
private int readOneByte() throws IOException {
final int b = inputStream.read();
if (b != -1) {
count(1);
return b & 0xFF;
}
return -1;
}
private int readOnce(byte[] b, int off, int len) throws IOException {
if (inUncompressed) {
int cnt = currentBlock.read(b, off, len);
count(cnt);
return cnt;
}
BlockLZ4CompressorInputStream l = (BlockLZ4CompressorInputStream) currentBlock;
long before = l.getBytesRead();
int cnt = currentBlock.read(b, off, len);
count(l.getBytesRead() - before);
return cnt;
}
private static boolean isSkippableFrameSignature(byte[] b) {
if ((b[0] & SKIPPABLE_FRAME_PREFIX_BYTE_MASK) != SKIPPABLE_FRAME_PREFIX_BYTE_MASK) {
return false;
}
for (int i = 1; i < 4; i++) {
if (b[i] != SKIPPABLE_FRAME_TRAILER[i - 1]) {
return false;
}
}
return true;
}
private int skipSkippableFrame(byte[] b) throws IOException {
int read = 4;
while (read == 4 && isSkippableFrameSignature(b)) {
final long len = ByteUtils.fromLittleEndian(supplier, 4);
if (len < 0) {
throw new IOException("Found illegal skippable frame with negative size");
}
long skipped = IOUtils.skip(inputStream, len);
count(skipped);
if (len != skipped) {
throw new IOException("Premature end of stream while skipping frame");
}
read = IOUtils.readFully(inputStream, b);
count(read);
}
return read;
}
private void appendToBlockDependencyBuffer(final byte[] b, final int off, int len) {
len = Math.min(len, blockDependencyBuffer.length);
if (len > 0) {
int keep = blockDependencyBuffer.length - len;
if (keep > 0) {
System.arraycopy(blockDependencyBuffer, len, blockDependencyBuffer, 0, keep);
}
System.arraycopy(b, off, blockDependencyBuffer, keep, len);
}
}
public static boolean matches(final byte[] signature, final int length) {
if (length < LZ4_SIGNATURE.length) {
return false;
}
byte[] shortenedSig = signature;
if (signature.length > LZ4_SIGNATURE.length) {
shortenedSig = new byte[LZ4_SIGNATURE.length];
System.arraycopy(signature, 0, shortenedSig, 0, LZ4_SIGNATURE.length);
}
return Arrays.equals(shortenedSig, LZ4_SIGNATURE);
}
}