2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de>
6 * Redistribution and use in source and binary forms, with or without modifica-
7 * tion, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
18 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
20 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
24 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
25 * OF THE POSSIBILITY OF SUCH DAMAGE.
27 * Alternatively, the contents of this file may be used under the terms of
28 * the GNU General Public License ("GPL") version 2 or any later version,
29 * in which case the provisions of the GPL are applicable instead of
30 * the above. If you wish to allow the use of your version of this file
31 * only under the terms of the GPL and not to allow others to use your
32 * version of this file under the BSD license, indicate your decision
33 * by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL. If you do not delete the
35 * provisions above, a recipient may use your version of this file under
36 * either the BSD or the GPL.
41 #define HSIZE (1 << (HLOG))
44 * don't play with this unless you benchmark!
45 * decompression is not dependent on the hash function
46 * the hashing function might seem strange, just believe me
50 # define FRST(p) (((p[0]) << 8) | p[1])
51 # define NEXT(v,p) (((v) << 8) | p[2])
53 # define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
55 # define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
57 # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
61 * IDX works because it is very similar to a multiplicative hash, e.g.
62 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
63 * the latter is also quite fast on newer CPUs, and compresses similarly.
65 * the next one is also quite good, albeit slow ;)
66 * (int)(cos(h & 0xffffff) * 1e6)
70 /* original lzv-like hash function, much worse and thus slower */
71 # define FRST(p) (p[0] << 5) ^ p[1]
72 # define NEXT(v,p) ((v) << 5) ^ p[2]
73 # define IDX(h) ((h) & (HSIZE - 1))
76 #define MAX_LIT (1 << 5)
77 #define MAX_OFF (1 << 13)
78 #define MAX_REF ((1 << 8) + (1 << 3))
81 # define expect(expr,value) __builtin_expect ((expr),(value))
82 # define inline inline
84 # define expect(expr,value) (expr)
85 # define inline static
88 #define expect_false(expr) expect ((expr) != 0, 0)
89 #define expect_true(expr) expect ((expr) != 0, 1)
94 * 000LLLLL <L+1> ; literal
95 * LLLooooo oooooooo ; backref L
96 * 111ooooo LLLLLLLL oooooooo ; backref L+7
101 lzf_compress (const void *const in_data, unsigned int in_len,
102 void *out_data, unsigned int out_len
112 const u8 *ip = (const u8 *)in_data;
113 u8 *op = (u8 *)out_data;
114 const u8 *in_end = ip + in_len;
115 u8 *out_end = op + out_len;
118 /* off requires a type wide enough to hold a general pointer difference.
119 * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
120 * works for differences within a single object). We also assume that no
121 * no bit pattern traps. Since the only platform that is both non-POSIX
122 * and fails to support both assumptions is windows 64 bit, we make a
123 * special workaround for it.
125 #if defined (WIN32) && defined (_M_X64)
126 unsigned _int64 off; /* workaround for missing POSIX compliance */
133 if (!in_len || !out_len)
137 memset (htab, 0, sizeof (htab));
139 for (hslot = htab; hslot < htab + HSIZE; hslot++)
144 lit = 0; op++; /* start run */
147 while (ip < in_end - 2)
149 hval = NEXT (hval, ip);
150 hslot = htab + IDX (hval);
151 ref = *hslot; *hslot = ip;
155 && ref < ip /* the next test will actually take care of this, but this is faster */
157 && (off = ip - ref - 1) < MAX_OFF
159 && ref > (const u8 *)in_data
165 && *(const u16 *)ref == *(const u16 *)ip
170 /* match found at *ref++ */
171 unsigned int len = 2;
172 unsigned int maxlen = in_end - ip - len;
173 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
175 if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
176 if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
179 op [- lit - 1] = lit - 1; /* stop run */
180 op -= !lit; /* undo run if length is zero */
184 if (expect_true (maxlen > 16))
186 len++; if (ref [len] != ip [len]) break;
187 len++; if (ref [len] != ip [len]) break;
188 len++; if (ref [len] != ip [len]) break;
189 len++; if (ref [len] != ip [len]) break;
191 len++; if (ref [len] != ip [len]) break;
192 len++; if (ref [len] != ip [len]) break;
193 len++; if (ref [len] != ip [len]) break;
194 len++; if (ref [len] != ip [len]) break;
196 len++; if (ref [len] != ip [len]) break;
197 len++; if (ref [len] != ip [len]) break;
198 len++; if (ref [len] != ip [len]) break;
199 len++; if (ref [len] != ip [len]) break;
201 len++; if (ref [len] != ip [len]) break;
202 len++; if (ref [len] != ip [len]) break;
203 len++; if (ref [len] != ip [len]) break;
204 len++; if (ref [len] != ip [len]) break;
209 while (len < maxlen && ref[len] == ip[len]);
214 len -= 2; /* len is now #octets - 1 */
219 *op++ = (off >> 8) + (len << 5);
223 *op++ = (off >> 8) + ( 7 << 5);
228 lit = 0; op++; /* start run */
232 if (expect_false (ip >= in_end - 2))
235 #if ULTRA_FAST || VERY_FAST
237 # if VERY_FAST && !ULTRA_FAST
242 hval = NEXT (hval, ip);
243 htab[IDX (hval)] = ip;
246 # if VERY_FAST && !ULTRA_FAST
247 hval = NEXT (hval, ip);
248 htab[IDX (hval)] = ip;
256 hval = NEXT (hval, ip);
257 htab[IDX (hval)] = ip;
265 /* one more literal byte we must copy */
266 if (expect_false (op >= out_end))
269 lit++; *op++ = *ip++;
271 if (expect_false (lit == MAX_LIT))
273 op [- lit - 1] = lit - 1; /* stop run */
274 lit = 0; op++; /* start run */
279 if (op + 3 > out_end) /* at most 3 bytes can be missing here */
284 lit++; *op++ = *ip++;
286 if (expect_false (lit == MAX_LIT))
288 op [- lit - 1] = lit - 1; /* stop run */
289 lit = 0; op++; /* start run */
293 op [- lit - 1] = lit - 1; /* end run */
294 op -= !lit; /* undo run if length is zero */
296 return op - (u8 *)out_data;
300 # define SET_ERRNO(n)
303 # define SET_ERRNO(n) errno = (n)
306 #if (__i386 || __amd64) && __GNUC__ >= 3
307 # define lzf_movsb(dst, src, len) \
309 : "=D" (dst), "=S" (src), "=c" (len) \
310 : "0" (dst), "1" (src), "2" (len));
314 lzf_decompress (const void *const in_data, unsigned int in_len,
315 void *out_data, unsigned int out_len)
317 u8 const *ip = (const u8 *)in_data;
318 u8 *op = (u8 *)out_data;
319 u8 const *const in_end = ip + in_len;
320 u8 *const out_end = op + out_len;
324 unsigned int ctrl = *ip++;
326 if (ctrl < (1 << 5)) /* literal run */
330 if (op + ctrl > out_end)
337 if (ip + ctrl > in_end)
345 lzf_movsb (op, ip, ctrl);
352 else /* back reference */
354 unsigned int len = ctrl >> 5;
356 u8 *ref = op - ((ctrl & 0x1f) << 8) - 1;
379 if (op + len + 2 > out_end)
385 if (ref < (u8 *)out_data)
393 lzf_movsb (op, ref, len);
406 return op - (u8 *)out_data;