1 // SPDX-License-Identifier: GPL-2.0 OR BSD-2-Clause
3 * linux/drivers/staging/erofs/unzip_lz4.c
5 * Copyright (C) 2018 HUAWEI, Inc.
6 * http://www.huawei.com/
7 * Created by Gao Xiang <gaoxiang25@huawei.com>
9 * Original code taken from 'linux/lib/lz4/lz4_decompress.c'
13 * LZ4 - Fast LZ compression algorithm
14 * Copyright (C) 2011 - 2016, Yann Collet.
15 * BSD 2 - Clause License (http://www.opensource.org/licenses/bsd - license.php)
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
19 * * Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * * Redistributions in binary form must reproduce the above
22 * copyright notice, this list of conditions and the following disclaimer
23 * in the documentation and/or other materials provided with the
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * You can contact the author at :
37 * - LZ4 homepage : http://www.lz4.org
38 * - LZ4 source repository : https://github.com/lz4/lz4
40 * Changed for kernel usage by:
41 * Sven Schmidt <4sschmid@informatik.uni-hamburg.de>
44 #include <asm/unaligned.h>
48 * no public solution to solve our requirement yet.
49 * see: <required buffer size for LZ4_decompress_safe_partial>
50 * https://groups.google.com/forum/#!topic/lz4c/_3kkz5N6n00
52 static FORCE_INLINE int customized_lz4_decompress_safe_partial(
53 const void * const source,
59 const BYTE *ip = (const BYTE *) source;
60 const BYTE * const iend = ip + inputSize;
62 BYTE *op = (BYTE *) dest;
63 BYTE * const oend = op + outputSize;
66 static const unsigned int dec32table[] = { 0, 1, 2, 1, 4, 4, 4, 4 };
67 static const int dec64table[] = { 0, 0, 0, -1, 0, 1, 2, 3 };
69 /* Empty output buffer */
70 if (unlikely(outputSize == 0))
71 return ((inputSize == 1) && (*ip == 0)) ? 0 : -1;
73 /* Main Loop : decode sequences */
79 /* get literal length */
80 unsigned int const token = *ip++;
82 length = token>>ML_BITS;
84 if (length == RUN_MASK) {
90 } while ((ip < iend - RUN_MASK) & (s == 255));
92 if (unlikely((size_t)(op + length) < (size_t)(op))) {
93 /* overflow detection */
96 if (unlikely((size_t)(ip + length) < (size_t)(ip))) {
97 /* overflow detection */
104 if ((cpy > oend - WILDCOPYLENGTH) ||
105 (ip + length > iend - (2 + 1 + LASTLITERALS))) {
107 memcpy(op, ip, length = oend - op);
112 if (unlikely(ip + length > iend)) {
115 * read attempt beyond
116 * end of input buffer
121 memcpy(op, ip, length);
127 /* Necessarily EOF, due to parsing restrictions */
130 LZ4_wildCopy(op, ip, cpy);
136 offset = LZ4_readLE16(ip);
140 if (unlikely(match < (const BYTE *)dest)) {
141 /* Error : offset outside buffers */
145 /* get matchlength */
146 length = token & ML_MASK;
147 if (length == ML_MASK) {
153 if (ip > iend - LASTLITERALS)
159 if (unlikely((size_t)(op + length) < (size_t)op)) {
160 /* overflow detection */
167 /* copy match within block */
170 if (unlikely(cpy >= oend - WILDCOPYLENGTH)) {
179 /* costs ~1%; silence an msan warning when offset == 0 */
180 LZ4_write32(op, (U32)offset);
182 if (unlikely(offset < 8)) {
183 const int dec64 = dec64table[offset];
189 match += dec32table[offset];
190 memcpy(op + 4, match, 4);
193 LZ4_copy8(op, match);
199 if (unlikely(cpy > oend - 12)) {
200 BYTE * const oCopyLimit = oend - (WILDCOPYLENGTH - 1);
202 if (op < oCopyLimit) {
203 LZ4_wildCopy(op, match, oCopyLimit);
204 match += oCopyLimit - op;
211 LZ4_copy8(op, match);
214 LZ4_wildCopy(op + 8, match + 8, cpy);
217 op = cpy; /* correction */
219 DBG_BUGON((void *)ip - source > inputSize);
220 DBG_BUGON((void *)op - dest > outputSize);
222 /* Nb of output bytes decoded */
223 return (int) ((void *)op - dest);
225 /* Overflow error detected */
230 int z_erofs_unzip_lz4(void *in, void *out, size_t inlen, size_t outlen)
232 int ret = customized_lz4_decompress_safe_partial(in,
239 * LZ4_decompress_safe will return an error code
240 * (< 0) if decompression failed
242 errln("%s, failed to decompress, in[%p, %zu] outlen[%p, %zu]",
243 __func__, in, inlen, out, outlen);
245 print_hex_dump(KERN_DEBUG, "raw data [in]: ", DUMP_PREFIX_OFFSET,
246 16, 1, in, inlen, true);
247 print_hex_dump(KERN_DEBUG, "raw data [out]: ", DUMP_PREFIX_OFFSET,
248 16, 1, out, outlen, true);