001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.compress.compressors.lz4;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.util.Arrays;
024
025import org.apache.commons.compress.compressors.CompressorInputStream;
026import org.apache.commons.compress.utils.BoundedInputStream;
027import org.apache.commons.compress.utils.ByteUtils;
028import org.apache.commons.compress.utils.ChecksumCalculatingInputStream;
029import org.apache.commons.compress.utils.CountingInputStream;
030import org.apache.commons.compress.utils.IOUtils;
031import org.apache.commons.compress.utils.InputStreamStatistics;
032
033/**
034 * CompressorInputStream for the LZ4 frame format.
035 *
036 * <p>Based on the "spec" in the version "1.5.1 (31/03/2015)"</p>
037 *
038 * @see <a href="http://lz4.github.io/lz4/lz4_Frame_format.html">LZ4 Frame Format Description</a>
039 * @since 1.14
040 * @NotThreadSafe
041 */
042public class FramedLZ4CompressorInputStream extends CompressorInputStream
043    implements InputStreamStatistics {
044
045    // used by FramedLZ4CompressorOutputStream as well
046    static final byte[] LZ4_SIGNATURE = new byte[] { //NOSONAR
047        4, 0x22, 0x4d, 0x18
048    };
049    private static final byte[] SKIPPABLE_FRAME_TRAILER = new byte[] {
050        0x2a, 0x4d, 0x18
051    };
052    private static final byte SKIPPABLE_FRAME_PREFIX_BYTE_MASK = 0x50;
053
054    static final int VERSION_MASK = 0xC0;
055    static final int SUPPORTED_VERSION = 0x40;
056    static final int BLOCK_INDEPENDENCE_MASK = 0x20;
057    static final int BLOCK_CHECKSUM_MASK = 0x10;
058    static final int CONTENT_SIZE_MASK = 0x08;
059    static final int CONTENT_CHECKSUM_MASK = 0x04;
060    static final int BLOCK_MAX_SIZE_MASK = 0x70;
061    static final int UNCOMPRESSED_FLAG_MASK = 0x80000000;
062
063    // used in no-arg read method
064    private final byte[] oneByte = new byte[1];
065
066    private final ByteUtils.ByteSupplier supplier = new ByteUtils.ByteSupplier() {
067        @Override
068        public int getAsByte() throws IOException {
069            return readOneByte();
070        }
071    };
072
073    private final CountingInputStream in;
074    private final boolean decompressConcatenated;
075
076    private boolean expectBlockChecksum;
077    private boolean expectBlockDependency;
078    private boolean expectContentSize;
079    private boolean expectContentChecksum;
080
081    private InputStream currentBlock;
082    private boolean endReached, inUncompressed;
083
084    // used for frame header checksum and content checksum, if present
085    private final XXHash32 contentHash = new XXHash32();
086
087    // used for block checksum, if present
088    private final XXHash32 blockHash = new XXHash32();
089
090    // only created if the frame doesn't set the block independence flag
091    private byte[] blockDependencyBuffer;
092
093    /**
094     * Creates a new input stream that decompresses streams compressed
095     * using the LZ4 frame format and stops after decompressing the
096     * first frame.
097     * @param in  the InputStream from which to read the compressed data
098     * @throws IOException if reading fails
099     */
100    public FramedLZ4CompressorInputStream(InputStream in) throws IOException {
101        this(in, false);
102    }
103
104    /**
105     * Creates a new input stream that decompresses streams compressed
106     * using the LZ4 frame format.
107     * @param in  the InputStream from which to read the compressed data
108     * @param decompressConcatenated if true, decompress until the end
109     *          of the input; if false, stop after the first LZ4 frame
110     *          and leave the input position to point to the next byte
111     *          after the frame stream
112     * @throws IOException if reading fails
113     */
114    public FramedLZ4CompressorInputStream(InputStream in, boolean decompressConcatenated) throws IOException {
115        this.in = new CountingInputStream(in);
116        this.decompressConcatenated = decompressConcatenated;
117        init(true);
118    }
119
120    /** {@inheritDoc} */
121    @Override
122    public int read() throws IOException {
123        return read(oneByte, 0, 1) == -1 ? -1 : oneByte[0] & 0xFF;
124    }
125
126    /** {@inheritDoc} */
127    @Override
128    public void close() throws IOException {
129        try {
130            if (currentBlock != null) {
131                currentBlock.close();
132                currentBlock = null;
133            }
134        } finally {
135            in.close();
136        }
137    }
138
139    /** {@inheritDoc} */
140    @Override
141    public int read(final byte[] b, final int off, final int len) throws IOException {
142        if (endReached) {
143            return -1;
144        }
145        int r = readOnce(b, off, len);
146        if (r == -1) {
147            nextBlock();
148            if (!endReached) {
149                r = readOnce(b, off, len);
150            }
151        }
152        if (r != -1) {
153            if (expectBlockDependency) {
154                appendToBlockDependencyBuffer(b, off, r);
155            }
156            if (expectContentChecksum) {
157                contentHash.update(b, off, r);
158            }
159        }
160        return r;
161    }
162
163    /**
164     * @since 1.17
165     */
166    @Override
167    public long getCompressedCount() {
168        return in.getBytesRead();
169    }
170
171    private void init(boolean firstFrame) throws IOException {
172        if (readSignature(firstFrame)) {
173            readFrameDescriptor();
174            nextBlock();
175        }
176    }
177
178    private boolean readSignature(boolean firstFrame) throws IOException {
179        String garbageMessage = firstFrame ? "Not a LZ4 frame stream" : "LZ4 frame stream followed by garbage";
180        final byte[] b = new byte[4];
181        int read = IOUtils.readFully(in, b);
182        count(read);
183        if (0 == read && !firstFrame) {
184            // good LZ4 frame and nothing after it
185            endReached = true;
186            return false;
187        }
188        if (4 != read) {
189            throw new IOException(garbageMessage);
190        }
191
192        read = skipSkippableFrame(b);
193        if (0 == read && !firstFrame) {
194            // good LZ4 frame with only some skippable frames after it
195            endReached = true;
196            return false;
197        }
198        if (4 != read || !matches(b, 4)) {
199            throw new IOException(garbageMessage);
200        }
201        return true;
202    }
203
204    private void readFrameDescriptor() throws IOException {
205        int flags = readOneByte();
206        if (flags == -1) {
207            throw new IOException("Premature end of stream while reading frame flags");
208        }
209        contentHash.update(flags);
210        if ((flags & VERSION_MASK) != SUPPORTED_VERSION) {
211            throw new IOException("Unsupported version " + (flags >> 6));
212        }
213        expectBlockDependency = (flags & BLOCK_INDEPENDENCE_MASK) == 0;
214        if (expectBlockDependency) {
215            if (blockDependencyBuffer == null) {
216                blockDependencyBuffer = new byte[BlockLZ4CompressorInputStream.WINDOW_SIZE];
217            }
218        } else {
219            blockDependencyBuffer = null;
220        }
221        expectBlockChecksum = (flags & BLOCK_CHECKSUM_MASK) != 0;
222        expectContentSize = (flags & CONTENT_SIZE_MASK) != 0;
223        expectContentChecksum = (flags & CONTENT_CHECKSUM_MASK) != 0;
224        int bdByte = readOneByte();
225        if (bdByte == -1) { // max size is irrelevant for this implementation
226            throw new IOException("Premature end of stream while reading frame BD byte");
227        }
228        contentHash.update(bdByte);
229        if (expectContentSize) { // for now we don't care, contains the uncompressed size
230            byte[] contentSize = new byte[8];
231            int skipped = IOUtils.readFully(in, contentSize);
232            count(skipped);
233            if (8 != skipped) {
234                throw new IOException("Premature end of stream while reading content size");
235            }
236            contentHash.update(contentSize, 0, contentSize.length);
237        }
238        int headerHash = readOneByte();
239        if (headerHash == -1) { // partial hash of header.
240            throw new IOException("Premature end of stream while reading frame header checksum");
241        }
242        int expectedHash = (int) ((contentHash.getValue() >> 8) & 0xff);
243        contentHash.reset();
244        if (headerHash != expectedHash) {
245            throw new IOException("frame header checksum mismatch.");
246        }
247    }
248
249    private void nextBlock() throws IOException {
250        maybeFinishCurrentBlock();
251        long len = ByteUtils.fromLittleEndian(supplier, 4);
252        boolean uncompressed = (len & UNCOMPRESSED_FLAG_MASK) != 0;
253        int realLen = (int) (len & (~UNCOMPRESSED_FLAG_MASK));
254        if (realLen == 0) {
255            verifyContentChecksum();
256            if (!decompressConcatenated) {
257                endReached = true;
258            } else {
259                init(false);
260            }
261            return;
262        }
263        InputStream capped = new BoundedInputStream(in, realLen);
264        if (expectBlockChecksum) {
265            capped = new ChecksumCalculatingInputStream(blockHash, capped);
266        }
267        if (uncompressed) {
268            inUncompressed = true;
269            currentBlock = capped;
270        } else {
271            inUncompressed = false;
272            BlockLZ4CompressorInputStream s = new BlockLZ4CompressorInputStream(capped);
273            if (expectBlockDependency) {
274                s.prefill(blockDependencyBuffer);
275            }
276            currentBlock = s;
277        }
278    }
279
280    private void maybeFinishCurrentBlock() throws IOException {
281        if (currentBlock != null) {
282            currentBlock.close();
283            currentBlock = null;
284            if (expectBlockChecksum) {
285                verifyChecksum(blockHash, "block");
286                blockHash.reset();
287            }
288        }
289    }
290
291    private void verifyContentChecksum() throws IOException {
292        if (expectContentChecksum) {
293            verifyChecksum(contentHash, "content");
294        }
295        contentHash.reset();
296    }
297
298    private void verifyChecksum(XXHash32 hash, String kind) throws IOException {
299        byte[] checksum = new byte[4];
300        int read = IOUtils.readFully(in, checksum);
301        count(read);
302        if (4 != read) {
303            throw new IOException("Premature end of stream while reading " + kind + " checksum");
304        }
305        long expectedHash = hash.getValue();
306        if (expectedHash != ByteUtils.fromLittleEndian(checksum)) {
307            throw new IOException(kind + " checksum mismatch.");
308        }
309    }
310
311    private int readOneByte() throws IOException {
312        final int b = in.read();
313        if (b != -1) {
314            count(1);
315            return b & 0xFF;
316        }
317        return -1;
318    }
319
320    private int readOnce(byte[] b, int off, int len) throws IOException {
321        if (inUncompressed) {
322            int cnt = currentBlock.read(b, off, len);
323            count(cnt);
324            return cnt;
325        }
326        BlockLZ4CompressorInputStream l = (BlockLZ4CompressorInputStream) currentBlock;
327        long before = l.getBytesRead();
328        int cnt = currentBlock.read(b, off, len);
329        count(l.getBytesRead() - before);
330        return cnt;
331    }
332
333    private static boolean isSkippableFrameSignature(byte[] b) {
334        if ((b[0] & SKIPPABLE_FRAME_PREFIX_BYTE_MASK) != SKIPPABLE_FRAME_PREFIX_BYTE_MASK) {
335            return false;
336        }
337        for (int i = 1; i < 4; i++) {
338            if (b[i] != SKIPPABLE_FRAME_TRAILER[i - 1]) {
339                return false;
340            }
341        }
342        return true;
343    }
344
345    /**
346     * Skips over the contents of a skippable frame as well as
347     * skippable frames following it.
348     *
349     * <p>It then tries to read four more bytes which are supposed to
350     * hold an LZ4 signature and returns the number of bytes read
351     * while storing the bytes in the given array.</p>
352     */
353    private int skipSkippableFrame(byte[] b) throws IOException {
354        int read = 4;
355        while (read == 4 && isSkippableFrameSignature(b)) {
356            long len = ByteUtils.fromLittleEndian(supplier, 4);
357            long skipped = IOUtils.skip(in, len);
358            count(skipped);
359            if (len != skipped) {
360                throw new IOException("Premature end of stream while skipping frame");
361            }
362            read = IOUtils.readFully(in, b);
363            count(read);
364        }
365        return read;
366    }
367
368    private void appendToBlockDependencyBuffer(final byte[] b, final int off, int len) {
369        len = Math.min(len, blockDependencyBuffer.length);
370        if (len > 0) {
371            int keep = blockDependencyBuffer.length - len;
372            if (keep > 0) {
373                // move last keep bytes towards the start of the buffer
374                System.arraycopy(blockDependencyBuffer, len, blockDependencyBuffer, 0, keep);
375            }
376            // append new data
377            System.arraycopy(b, off, blockDependencyBuffer, keep, len);
378        }
379    }
380
381    /**
382     * Checks if the signature matches what is expected for a .lz4 file.
383     *
384     * <p>.lz4 files start with a four byte signature.</p>
385     *
386     * @param signature the bytes to check
387     * @param length    the number of bytes to check
388     * @return          true if this is a .sz stream, false otherwise
389     */
390    public static boolean matches(final byte[] signature, final int length) {
391
392        if (length < LZ4_SIGNATURE.length) {
393            return false;
394        }
395
396        byte[] shortenedSig = signature;
397        if (signature.length > LZ4_SIGNATURE.length) {
398            shortenedSig = new byte[LZ4_SIGNATURE.length];
399            System.arraycopy(signature, 0, shortenedSig, 0, LZ4_SIGNATURE.length);
400        }
401
402        return Arrays.equals(shortenedSig, LZ4_SIGNATURE);
403    }
404}