2
* Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de>
4
* Redistribution and use in source and binary forms, with or without modifica-
5
* tion, are permitted provided that the following conditions are met:
7
* 1. Redistributions of source code must retain the above copyright notice,
8
* this list of conditions and the following disclaimer.
10
* 2. Redistributions in binary form must reproduce the above copyright
11
* notice, this list of conditions and the following disclaimer in the
12
* documentation and/or other materials provided with the distribution.
14
* 3. The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19
* CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
20
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21
* CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25
* ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26
* OF THE POSSIBILITY OF SUCH DAMAGE.
28
* Alternatively, the contents of this file may be used under the terms of
29
* the GNU General Public License version 2 (the "GPL"), in which case the
30
* provisions of the GPL are applicable instead of the above. If you wish to
31
* allow the use of your version of this file only under the terms of the
32
* GPL and not to allow others to use your version of this file under the
33
* BSD license, indicate your decision by deleting the provisions above and
34
* replace them with the notice and other provisions required by the GPL. If
35
* you do not delete the provisions above, a recipient may use your version
36
* of this file under either the BSD or the GPL.
43
#ifdef USE_UINTPTR_T /* bird: use intptr_t for pointer acrobatics on 64-bit windows. */
44
# if defined(_MSC_VER)
51
#define HSIZE (1 << (HLOG))
54
* don't play with this unless you benchmark!
55
* decompression is not dependent on the hash function
56
* the hashing function might seem strange, just believe me
60
# define FRST(p) (((p[0]) << 8) | p[1])
61
# define NEXT(v,p) (((v) << 8) | p[2])
62
# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
65
* IDX works because it is very similar to a multiplicative hash, e.g.
66
* ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
67
* the latter is also quite fast on newer CPUs, and sligthly better
69
* the next one is also quite good, albeit slow ;)
70
* (int)(cos(h & 0xffffff) * 1e6)
74
/* original lzv-like hash function, much worse and thus slower */
75
# define FRST(p) (p[0] << 5) ^ p[1]
76
# define NEXT(v,p) ((v) << 5) ^ p[2]
77
# define IDX(h) ((h) & (HSIZE - 1))
80
#define MAX_LIT (1 << 5)
81
#define MAX_OFF (1 << 13)
82
#define MAX_REF ((1 << 8) + (1 << 3))
87
* 000LLLLL <L+1> ; literal
88
* LLLooooo oooooooo ; backref L
89
* 111ooooo LLLLLLLL oooooooo ; backref L+7
94
lzf_compress (const void *const in_data, unsigned int in_len,
95
void *out_data, unsigned int out_len
105
const u8 *ip = (const u8 *)in_data;
106
u8 *op = (u8 *)out_data;
107
const u8 *in_end = ip + in_len;
108
u8 *out_end = op + out_len;
111
unsigned int hval = FRST (ip);
112
#ifdef USE_UINTPTR_T /* bird: use intptr_t for pointer acrobatics on 64-bit windows. */
121
memset (htab, 0, sizeof (htab));
123
for (hslot = htab; hslot < htab + HSIZE; hslot++)
132
hval = NEXT (hval, ip);
133
hslot = htab + IDX (hval);
134
ref = *hslot; *hslot = ip;
137
#if INIT_HTAB && !USE_MEMCPY
138
&& ref < ip /* the next test will actually take care of this, but this is faster */
140
&& (off = ip - ref - 1) < MAX_OFF
142
&& ref > (u8 *)in_data
148
&& *(u16 *)ref == *(u16 *)ip
153
/* match found at *ref++ */
154
unsigned int len = 2;
155
unsigned int maxlen = in_end - ip - len;
156
maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
158
if (op + lit + 1 + 3 >= out_end)
163
while (len < maxlen && ref[len] == ip[len]);
179
*op++ = (off >> 8) + (len << 5);
183
*op++ = (off >> 8) + ( 7 << 5);
189
#if ULTRA_FAST || VERY_FAST
191
#if VERY_FAST && !ULTRA_FAST
196
hval = NEXT (hval, ip);
197
htab[IDX (hval)] = ip;
200
#if VERY_FAST && !ULTRA_FAST
201
hval = NEXT (hval, ip);
202
htab[IDX (hval)] = ip;
208
hval = NEXT (hval, ip);
209
htab[IDX (hval)] = ip;
217
else if (ip == in_end)
220
/* one more literal byte we must copy */
226
if (op + 1 + MAX_LIT >= out_end)
231
memcpy (op, ip - MAX_LIT, MAX_LIT);
245
if (op + lit + 1 >= out_end)
255
return op - (u8 *) out_data;