VirtualBox

source: vbox/trunk/src/libs/liblzf-3.4/lzf_c.c@ 17027

Last change on this file since 17027 was 17027, checked in by vboxsync, 16 years ago

liblzf-3.4: exporting (in progress).

File size: 8.7 KB
Line 
1/*
2 * Copyright (c) 2000-2008 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 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
16 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
17 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
18 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
22 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * Alternatively, the contents of this file may be used under the terms of
26 * the GNU General Public License ("GPL") version 2 or any later version,
27 * in which case the provisions of the GPL are applicable instead of
28 * the above. If you wish to allow the use of your version of this file
29 * only under the terms of the GPL and not to allow others to use your
30 * version of this file under the BSD license, indicate your decision
31 * by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL. If you do not delete the
33 * provisions above, a recipient may use your version of this file under
34 * either the BSD or the GPL.
35 */
36
37#include "lzfP.h"
38#ifdef VBOX
39# include "lzf.h" /* need the prototype */
40#endif
41
42#define HSIZE (1 << (HLOG))
43
44/*
45 * don't play with this unless you benchmark!
46 * decompression is not dependent on the hash function
47 * the hashing function might seem strange, just believe me
48 * it works ;)
49 */
50#ifndef FRST
51# define FRST(p) (((p[0]) << 8) | p[1])
52# define NEXT(v,p) (((v) << 8) | p[2])
53# if ULTRA_FAST
54# define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
55# elif VERY_FAST
56# define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
57# else
58# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
59# endif
60#endif
61/*
62 * IDX works because it is very similar to a multiplicative hash, e.g.
63 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
64 * the latter is also quite fast on newer CPUs, and compresses similarly.
65 *
66 * the next one is also quite good, albeit slow ;)
67 * (int)(cos(h & 0xffffff) * 1e6)
68 */
69
70#if 0
71/* original lzv-like hash function, much worse and thus slower */
72# define FRST(p) (p[0] << 5) ^ p[1]
73# define NEXT(v,p) ((v) << 5) ^ p[2]
74# define IDX(h) ((h) & (HSIZE - 1))
75#endif
76
77#define MAX_LIT (1 << 5)
78#define MAX_OFF (1 << 13)
79#define MAX_REF ((1 << 8) + (1 << 3))
80
81#if __GNUC__ >= 3
82# define expect(expr,value) __builtin_expect ((expr),(value))
83# define inline inline
84#else
85# define expect(expr,value) (expr)
86# define inline static
87#endif
88
89#define expect_false(expr) expect ((expr) != 0, 0)
90#define expect_true(expr) expect ((expr) != 0, 1)
91
92/*
93 * compressed format
94 *
95 * 000LLLLL <L+1> ; literal
96 * LLLooooo oooooooo ; backref L
97 * 111ooooo LLLLLLLL oooooooo ; backref L+7
98 *
99 */
100
101unsigned int
102lzf_compress (const void *const in_data, unsigned int in_len,
103 void *out_data, unsigned int out_len
104#if LZF_STATE_ARG
105 , LZF_STATE htab
106#endif
107 )
108{
109#if !LZF_STATE_ARG
110 LZF_STATE htab;
111#endif
112 const u8 **hslot;
113 const u8 *ip = (const u8 *)in_data;
114 u8 *op = (u8 *)out_data;
115 const u8 *in_end = ip + in_len;
116 u8 *out_end = op + out_len;
117 const u8 *ref;
118
119 /* off requires a type wide enough to hold a general pointer difference.
120 * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
121 * works for differences within a single object). We also assume that no
122 * no bit pattern traps. Since the only platform that is both non-POSIX
123 * and fails to support both assumptions is windows 64 bit, we make a
124 * special workaround for it.
125 */
126#if defined (WIN32) && defined (_M_X64)
127 unsigned _int64 off; /* workaround for missing POSIX compliance */
128#else
129 unsigned long off;
130#endif
131 unsigned int hval;
132 int lit;
133
134 if (!in_len || !out_len)
135 return 0;
136
137#if INIT_HTAB
138 memset (htab, 0, sizeof (htab));
139# if 0
140 for (hslot = htab; hslot < htab + HSIZE; hslot++)
141 *hslot++ = ip;
142# endif
143#endif
144
145 lit = 0; op++; /* start run */
146
147 hval = FRST (ip);
148 while (ip < in_end - 2)
149 {
150 hval = NEXT (hval, ip);
151 hslot = htab + IDX (hval);
152 ref = *hslot; *hslot = ip;
153
154 if (1
155#if INIT_HTAB
156 && ref < ip /* the next test will actually take care of this, but this is faster */
157#endif
158 && (off = ip - ref - 1) < MAX_OFF
159 && ip + 4 < in_end
160 && ref > (u8 *)in_data
161#if STRICT_ALIGN
162 && ref[0] == ip[0]
163 && ref[1] == ip[1]
164 && ref[2] == ip[2]
165#else
166 && *(u16 *)ref == *(u16 *)ip
167 && ref[2] == ip[2]
168#endif
169 )
170 {
171 /* match found at *ref++ */
172 unsigned int len = 2;
173 unsigned int maxlen = in_end - ip - len;
174 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
175
176 op [- lit - 1] = lit - 1; /* stop run */
177 op -= !lit; /* undo run if length is zero */
178
179 if (expect_false (op + 3 + 1 >= out_end))
180 return 0;
181
182 for (;;)
183 {
184 if (expect_true (maxlen > 16))
185 {
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;
190
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;
195
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;
200
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;
205 }
206
207 do
208 len++;
209 while (len < maxlen && ref[len] == ip[len]);
210
211 break;
212 }
213
214 len -= 2; /* len is now #octets - 1 */
215 ip++;
216
217 if (len < 7)
218 {
219 *op++ = (off >> 8) + (len << 5);
220 }
221 else
222 {
223 *op++ = (off >> 8) + ( 7 << 5);
224 *op++ = len - 7;
225 }
226
227 *op++ = off;
228 lit = 0; op++; /* start run */
229
230 ip += len + 1;
231
232 if (expect_false (ip >= in_end - 2))
233 break;
234
235#if ULTRA_FAST || VERY_FAST
236 --ip;
237# if VERY_FAST && !ULTRA_FAST
238 --ip;
239# endif
240 hval = FRST (ip);
241
242 hval = NEXT (hval, ip);
243 htab[IDX (hval)] = ip;
244 ip++;
245
246# if VERY_FAST && !ULTRA_FAST
247 hval = NEXT (hval, ip);
248 htab[IDX (hval)] = ip;
249 ip++;
250# endif
251#else
252 ip -= len + 1;
253
254 do
255 {
256 hval = NEXT (hval, ip);
257 htab[IDX (hval)] = ip;
258 ip++;
259 }
260 while (len--);
261#endif
262 }
263 else
264 {
265 /* one more literal byte we must copy */
266 if (expect_false (op >= out_end))
267 return 0;
268
269 lit++; *op++ = *ip++;
270
271 if (expect_false (lit == MAX_LIT))
272 {
273 op [- lit - 1] = lit - 1; /* stop run */
274 lit = 0; op++; /* start run */
275 }
276 }
277 }
278
279 if (op + 3 > out_end) /* at most 3 bytes can be missing here */
280 return 0;
281
282 while (ip < in_end)
283 {
284 lit++; *op++ = *ip++;
285
286 if (expect_false (lit == MAX_LIT))
287 {
288 op [- lit - 1] = lit - 1; /* stop run */
289 lit = 0; op++; /* start run */
290 }
291 }
292
293 op [- lit - 1] = lit - 1; /* end run */
294 op -= !lit; /* undo run if length is zero */
295
296 return op - (u8 *)out_data;
297}
298
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette