1 | /*
|
---|
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de>
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without modifica-
|
---|
5 | * tion, are permitted provided that the following conditions are met:
|
---|
6 | *
|
---|
7 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
8 | * this list of conditions and the following disclaimer.
|
---|
9 | *
|
---|
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.
|
---|
13 | *
|
---|
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.
|
---|
16 | *
|
---|
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.
|
---|
27 | *
|
---|
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.
|
---|
37 | */
|
---|
38 |
|
---|
39 | #include "lzfP.h"
|
---|
40 | #ifdef VBOX
|
---|
41 | #include "lzf.h"
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #define HSIZE (1 << (HLOG))
|
---|
45 |
|
---|
46 | /*
|
---|
47 | * don't play with this unless you benchmark!
|
---|
48 | * decompression is not dependent on the hash function
|
---|
49 | * the hashing function might seem strange, just believe me
|
---|
50 | * it works ;)
|
---|
51 | */
|
---|
52 | #ifndef FRST
|
---|
53 | # define FRST(p) (((p[0]) << 8) | p[1])
|
---|
54 | # define NEXT(v,p) (((v) << 8) | p[2])
|
---|
55 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
|
---|
56 | #endif
|
---|
57 | /*
|
---|
58 | * IDX works because it is very similar to a multiplicative hash, e.g.
|
---|
59 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
|
---|
60 | * the latter is also quite fast on newer CPUs, and sligthly better
|
---|
61 | *
|
---|
62 | * the next one is also quite good, albeit slow ;)
|
---|
63 | * (int)(cos(h & 0xffffff) * 1e6)
|
---|
64 | */
|
---|
65 |
|
---|
66 | #if 0
|
---|
67 | /* original lzv-like hash function, much worse and thus slower */
|
---|
68 | # define FRST(p) (p[0] << 5) ^ p[1]
|
---|
69 | # define NEXT(v,p) ((v) << 5) ^ p[2]
|
---|
70 | # define IDX(h) ((h) & (HSIZE - 1))
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #define MAX_LIT (1 << 5)
|
---|
74 | #define MAX_OFF (1 << 13)
|
---|
75 | #define MAX_REF ((1 << 8) + (1 << 3))
|
---|
76 |
|
---|
77 | /*
|
---|
78 | * compressed format
|
---|
79 | *
|
---|
80 | * 000LLLLL <L+1> ; literal
|
---|
81 | * LLLooooo oooooooo ; backref L
|
---|
82 | * 111ooooo LLLLLLLL oooooooo ; backref L+7
|
---|
83 | *
|
---|
84 | */
|
---|
85 |
|
---|
86 | unsigned int
|
---|
87 | lzf_compress (const void *const in_data, unsigned int in_len,
|
---|
88 | void *out_data, unsigned int out_len
|
---|
89 | #if LZF_STATE_ARG
|
---|
90 | , LZF_STATE *htab
|
---|
91 | #endif
|
---|
92 | )
|
---|
93 | {
|
---|
94 | #if !LZF_STATE_ARG
|
---|
95 | LZF_STATE htab;
|
---|
96 | #endif
|
---|
97 | const u8 **hslot;
|
---|
98 | const u8 *ip = (const u8 *)in_data;
|
---|
99 | u8 *op = (u8 *)out_data;
|
---|
100 | const u8 *in_end = ip + in_len;
|
---|
101 | u8 *out_end = op + out_len;
|
---|
102 | const u8 *ref;
|
---|
103 |
|
---|
104 | unsigned int hval = FRST (ip);
|
---|
105 | unsigned long off;
|
---|
106 | int lit = 0;
|
---|
107 |
|
---|
108 | #if INIT_HTAB
|
---|
109 | # if USE_MEMCPY
|
---|
110 | memset (htab, 0, sizeof (htab));
|
---|
111 | # else
|
---|
112 | for (hslot = htab; hslot < htab + HSIZE; hslot++)
|
---|
113 | *hslot++ = ip;
|
---|
114 | # endif
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | for (;;)
|
---|
118 | {
|
---|
119 | if (ip < in_end - 2)
|
---|
120 | {
|
---|
121 | hval = NEXT (hval, ip);
|
---|
122 | hslot = htab + IDX (hval);
|
---|
123 | ref = *hslot; *hslot = ip;
|
---|
124 |
|
---|
125 | if (1
|
---|
126 | #if INIT_HTAB && !USE_MEMCPY
|
---|
127 | && ref < ip /* the next test will actually take care of this, but this is faster */
|
---|
128 | #endif
|
---|
129 | && (off = ip - ref - 1) < MAX_OFF
|
---|
130 | && ip + 4 < in_end
|
---|
131 | && ref > (u8 *)in_data
|
---|
132 | #if STRICT_ALIGN
|
---|
133 | && ref[0] == ip[0]
|
---|
134 | && ref[1] == ip[1]
|
---|
135 | && ref[2] == ip[2]
|
---|
136 | #else
|
---|
137 | && *(u16 *)ref == *(u16 *)ip
|
---|
138 | && ref[2] == ip[2]
|
---|
139 | #endif
|
---|
140 | )
|
---|
141 | {
|
---|
142 | /* match found at *ref++ */
|
---|
143 | unsigned int len = 2;
|
---|
144 | unsigned int maxlen = in_end - ip - len;
|
---|
145 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
|
---|
146 |
|
---|
147 | if (op + lit + 1 + 3 >= out_end)
|
---|
148 | return 0;
|
---|
149 |
|
---|
150 | do
|
---|
151 | len++;
|
---|
152 | while (len < maxlen && ref[len] == ip[len]);
|
---|
153 |
|
---|
154 | if (lit)
|
---|
155 | {
|
---|
156 | *op++ = lit - 1;
|
---|
157 | lit = -lit;
|
---|
158 | do
|
---|
159 | *op++ = ip[lit];
|
---|
160 | while (++lit);
|
---|
161 | }
|
---|
162 |
|
---|
163 | len -= 2;
|
---|
164 | ip++;
|
---|
165 |
|
---|
166 | if (len < 7)
|
---|
167 | {
|
---|
168 | *op++ = (off >> 8) + (len << 5);
|
---|
169 | }
|
---|
170 | else
|
---|
171 | {
|
---|
172 | *op++ = (off >> 8) + ( 7 << 5);
|
---|
173 | *op++ = len - 7;
|
---|
174 | }
|
---|
175 |
|
---|
176 | *op++ = off;
|
---|
177 |
|
---|
178 | #if ULTRA_FAST || VERY_FAST
|
---|
179 | ip += len;
|
---|
180 | #if VERY_FAST && !ULTRA_FAST
|
---|
181 | --ip;
|
---|
182 | #endif
|
---|
183 | hval = FRST (ip);
|
---|
184 |
|
---|
185 | hval = NEXT (hval, ip);
|
---|
186 | htab[IDX (hval)] = ip;
|
---|
187 | ip++;
|
---|
188 |
|
---|
189 | #if VERY_FAST && !ULTRA_FAST
|
---|
190 | hval = NEXT (hval, ip);
|
---|
191 | htab[IDX (hval)] = ip;
|
---|
192 | ip++;
|
---|
193 | #endif
|
---|
194 | #else
|
---|
195 | do
|
---|
196 | {
|
---|
197 | hval = NEXT (hval, ip);
|
---|
198 | htab[IDX (hval)] = ip;
|
---|
199 | ip++;
|
---|
200 | }
|
---|
201 | while (len--);
|
---|
202 | #endif
|
---|
203 | continue;
|
---|
204 | }
|
---|
205 | }
|
---|
206 | else if (ip == in_end)
|
---|
207 | break;
|
---|
208 |
|
---|
209 | /* one more literal byte we must copy */
|
---|
210 | lit++;
|
---|
211 | ip++;
|
---|
212 |
|
---|
213 | if (lit == MAX_LIT)
|
---|
214 | {
|
---|
215 | if (op + 1 + MAX_LIT >= out_end)
|
---|
216 | return 0;
|
---|
217 |
|
---|
218 | *op++ = MAX_LIT - 1;
|
---|
219 | #if USE_MEMCPY
|
---|
220 | memcpy (op, ip - MAX_LIT, MAX_LIT);
|
---|
221 | op += MAX_LIT;
|
---|
222 | lit = 0;
|
---|
223 | #else
|
---|
224 | lit = -lit;
|
---|
225 | do
|
---|
226 | *op++ = ip[lit];
|
---|
227 | while (++lit);
|
---|
228 | #endif
|
---|
229 | }
|
---|
230 | }
|
---|
231 |
|
---|
232 | if (lit)
|
---|
233 | {
|
---|
234 | if (op + lit + 1 >= out_end)
|
---|
235 | return 0;
|
---|
236 |
|
---|
237 | *op++ = lit - 1;
|
---|
238 | lit = -lit;
|
---|
239 | do
|
---|
240 | *op++ = ip[lit];
|
---|
241 | while (++lit);
|
---|
242 | }
|
---|
243 |
|
---|
244 | return op - (u8 *) out_data;
|
---|
245 | }
|
---|