2 * LZ4 - Fast LZ compression algorithm
3 * Copyright (C) 2011-2012, Yann Collet.
4 * BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * You can contact the author at :
30 * - LZ4 homepage : http://fastcompression.blogspot.com/p/lz4.html
31 * - LZ4 source repository : http://code.google.com/p/lz4/
33 * Changed for kernel use by:
34 * Chanho Min <chanho.min@lge.com>
37 #include <linux/log2.h>
38 #include <linux/module.h>
39 #include <linux/kernel.h>
40 #include <linux/lz4.h>
41 #include <asm/unaligned.h>
44 #define LZ4_HASH_VALUE(p, _table) \
45 __HASH_VALUE(p, MEMORY_USAGE - ilog2(sizeof(_table[0])))
47 struct lz4_hash_table {
48 const u8 *(*add)(const struct lz4_hash_table, const u8 *);
53 #if __SIZEOF_POINTER__ == 4
54 static inline const u8 *hash_table_add32(const struct lz4_hash_table hash,
57 const u8 **table = hash.ctx;
59 swap(table[LZ4_HASH_VALUE(ip, table)], ip);
63 static inline const u8 *hash_table_add32(const struct lz4_hash_table hash,
66 u32 *table = hash.ctx;
67 size_t offset = ip - hash.base;
69 swap(table[LZ4_HASH_VALUE(ip, table)], offset);
70 return hash.base + offset;
74 static inline const u8 *hash_table_add16(const struct lz4_hash_table hash,
77 u16 *table = hash.ctx;
78 size_t offset = ip - hash.base;
80 swap(table[LZ4_HASH_VALUE(ip, table)], offset);
81 return hash.base + offset;
84 static inline const u8 *try_match(const struct lz4_hash_table hash,
87 const u8 *ref = hash.add(hash, ip);
89 return ref >= ip - MAX_DISTANCE &&
90 A32(ref) == A32(ip) ? ref : NULL;
93 static inline const u8 *find_match(const struct lz4_hash_table hash,
94 const u8 **ip, const u8 *anchor,
95 const u8 *start, const u8 *end)
98 int findmatchattempts = (1U << SKIPSTRENGTH) + 3;
99 const u8 *next_ip = *ip, *ref;
103 next_ip += findmatchattempts++ >> SKIPSTRENGTH;
105 if (unlikely(next_ip > end))
107 } while (!(ref = try_match(hash, *ip)));
110 while (*ip > anchor &&
112 unlikely((*ip)[-1] == ref[-1])) {
123 * Compress 'isize' bytes from 'source' into an output buffer 'dest' of
124 * maximum size 'maxOutputSize'. * If it cannot achieve it, compression
125 * will stop, and result of the function will be zero.
126 * return : the number of bytes written in buffer 'dest', or 0 if the
129 static inline int lz4_compressctx(const struct lz4_hash_table hash,
130 const u8 *src, size_t src_len,
131 u8 *dst, size_t *dst_len)
134 const u8 *anchor = ip, *ref;
135 const u8 *const iend = ip + src_len;
136 const u8 *const mflimit = iend - MFLIMIT;
137 const u8 *const matchlimit = iend - LASTLITERALS;
138 size_t maxoutputsize = *dst_len;
140 u8 *const oend = op + maxoutputsize;
145 if (src_len < MINLENGTH)
148 memset(hash.ctx, 0, LZ4_MEM_COMPRESS);
153 /* Starting a literal: */
155 ref = find_match(hash, &ip, anchor, src, mflimit);
160 * We found a match; @ip now points to the match and @ref points
161 * to the prior part of the input we matched with. Everything up
162 * to @anchor has been encoded; the range from @anchor to @ip
163 * didn't match and now has to be encoded as a literal:
165 length = ip - anchor;
168 /* check output limit */
169 if (unlikely(op + length + (2 + 1 + LASTLITERALS) +
170 (length >> 8) > oend))
173 *token = encode_length(&op, length) << ML_BITS;
176 MEMCPY_ADVANCE_CHUNKED(op, anchor, length);
178 /* Encode matches: */
181 PUT_LE16_ADVANCE(op, ip - ref);
183 /* MINMATCH bytes already matched from find_match(): */
187 length = common_length(ip, ref, matchlimit);
189 /* Check output limit */
190 if (unlikely(op + (1 + LASTLITERALS) +
191 (length >> 8) > oend))
196 *token += encode_length(&op, length);
198 /* Test end of chunk */
205 hash.add(hash, ip - 2);
207 /* Test next position */
208 ref = try_match(hash, ip);
218 /* Encode Last Literals */
219 length = iend - anchor;
220 if ((op - dst) + length + 1 +
221 ((length + 255 - RUN_MASK) / 255) > (u32)maxoutputsize)
225 *token = encode_length(&op, length) << ML_BITS;
226 MEMCPY_ADVANCE(op, anchor, iend - anchor);
233 __attribute__((flatten))
234 int lz4_compress(const unsigned char *src, size_t src_len,
235 unsigned char *dst, size_t *dst_len, void *wrkmem)
237 if (src_len < LZ4_64KLIMIT) {
238 const struct lz4_hash_table hash = {
239 .add = hash_table_add16,
244 return lz4_compressctx(hash, src, src_len, dst, dst_len);
246 const struct lz4_hash_table hash = {
247 .add = hash_table_add32,
252 return lz4_compressctx(hash, src, src_len, dst, dst_len);
255 EXPORT_SYMBOL(lz4_compress);
257 MODULE_LICENSE("Dual BSD/GPL");
258 MODULE_DESCRIPTION("LZ4 compressor");