VirtualBox

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

Last change on this file since 44080 was 23582, checked in by vboxsync, 15 years ago

liblzf-3.4: Fixed build misconfig on windows (just use uintptr_t everwhere).

File size: 8.8 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# include <iprt/types.h> /* need uintptr_t. */
41#endif
42
43#define HSIZE (1 << (HLOG))
44
45/*
46 * don't play with this unless you benchmark!
47 * decompression is not dependent on the hash function
48 * the hashing function might seem strange, just believe me
49 * it works ;)
50 */
51#ifndef FRST
52# define FRST(p) (((p[0]) << 8) | p[1])
53# define NEXT(v,p) (((v) << 8) | p[2])
54# if ULTRA_FAST
55# define IDX(h) ((( h >> (3*8 - HLOG)) - h ) & (HSIZE - 1))
56# elif VERY_FAST
57# define IDX(h) ((( h >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
58# else
59# define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1))
60# endif
61#endif
62/*
63 * IDX works because it is very similar to a multiplicative hash, e.g.
64 * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1))
65 * the latter is also quite fast on newer CPUs, and compresses similarly.
66 *
67 * the next one is also quite good, albeit slow ;)
68 * (int)(cos(h & 0xffffff) * 1e6)
69 */
70
71#if 0
72/* original lzv-like hash function, much worse and thus slower */
73# define FRST(p) (p[0] << 5) ^ p[1]
74# define NEXT(v,p) ((v) << 5) ^ p[2]
75# define IDX(h) ((h) & (HSIZE - 1))
76#endif
77
78#define MAX_LIT (1 << 5)
79#define MAX_OFF (1 << 13)
80#define MAX_REF ((1 << 8) + (1 << 3))
81
82#if __GNUC__ >= 3
83# define expect(expr,value) __builtin_expect ((expr),(value))
84# define inline inline
85#else
86# define expect(expr,value) (expr)
87# define inline static
88#endif
89
90#define expect_false(expr) expect ((expr) != 0, 0)
91#define expect_true(expr) expect ((expr) != 0, 1)
92
93/*
94 * compressed format
95 *
96 * 000LLLLL <L+1> ; literal
97 * LLLooooo oooooooo ; backref L
98 * 111ooooo LLLLLLLL oooooooo ; backref L+7
99 *
100 */
101
102unsigned int
103lzf_compress (const void *const in_data, unsigned int in_len,
104 void *out_data, unsigned int out_len
105#if LZF_STATE_ARG
106 , LZF_STATE htab
107#endif
108 )
109{
110#if !LZF_STATE_ARG
111 LZF_STATE htab;
112#endif
113 const u8 **hslot;
114 const u8 *ip = (const u8 *)in_data;
115 u8 *op = (u8 *)out_data;
116 const u8 *in_end = ip + in_len;
117 u8 *out_end = op + out_len;
118 const u8 *ref;
119
120 /* off requires a type wide enough to hold a general pointer difference.
121 * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only
122 * works for differences within a single object). We also assume that no
123 * no bit pattern traps. Since the only platform that is both non-POSIX
124 * and fails to support both assumptions is windows 64 bit, we make a
125 * special workaround for it.
126 */
127#ifdef VBOX
128 uintptr_t off;
129#else
130# error "Build config error."
131#if defined (WIN32) && defined (_M_X64)
132 unsigned _int64 off; /* workaround for missing POSIX compliance */
133#else
134 unsigned long off;
135#endif
136#endif
137 unsigned int hval;
138 int lit;
139
140 if (!in_len || !out_len)
141 return 0;
142
143#if INIT_HTAB
144 memset (htab, 0, sizeof (htab));
145# if 0
146 for (hslot = htab; hslot < htab + HSIZE; hslot++)
147 *hslot++ = ip;
148# endif
149#endif
150
151 lit = 0; op++; /* start run */
152
153 hval = FRST (ip);
154 while (ip < in_end - 2)
155 {
156 hval = NEXT (hval, ip);
157 hslot = htab + IDX (hval);
158 ref = *hslot; *hslot = ip;
159
160 if (1
161#if INIT_HTAB
162 && ref < ip /* the next test will actually take care of this, but this is faster */
163#endif
164 && (off = ip - ref - 1) < MAX_OFF
165 && ip + 4 < in_end
166 && ref > (u8 *)in_data
167#if STRICT_ALIGN
168 && ref[0] == ip[0]
169 && ref[1] == ip[1]
170 && ref[2] == ip[2]
171#else
172 && *(u16 *)ref == *(u16 *)ip
173 && ref[2] == ip[2]
174#endif
175 )
176 {
177 /* match found at *ref++ */
178 unsigned int len = 2;
179 unsigned int maxlen = in_end - ip - len;
180 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
181
182 op [- lit - 1] = lit - 1; /* stop run */
183 op -= !lit; /* undo run if length is zero */
184
185 if (expect_false (op + 3 + 1 >= out_end))
186 return 0;
187
188 for (;;)
189 {
190 if (expect_true (maxlen > 16))
191 {
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 len++; if (ref [len] != ip [len]) break;
196
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 len++; if (ref [len] != ip [len]) break;
201
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 len++; if (ref [len] != ip [len]) break;
206
207 len++; if (ref [len] != ip [len]) break;
208 len++; if (ref [len] != ip [len]) break;
209 len++; if (ref [len] != ip [len]) break;
210 len++; if (ref [len] != ip [len]) break;
211 }
212
213 do
214 len++;
215 while (len < maxlen && ref[len] == ip[len]);
216
217 break;
218 }
219
220 len -= 2; /* len is now #octets - 1 */
221 ip++;
222
223 if (len < 7)
224 {
225 *op++ = (off >> 8) + (len << 5);
226 }
227 else
228 {
229 *op++ = (off >> 8) + ( 7 << 5);
230 *op++ = len - 7;
231 }
232
233 *op++ = off;
234 lit = 0; op++; /* start run */
235
236 ip += len + 1;
237
238 if (expect_false (ip >= in_end - 2))
239 break;
240
241#if ULTRA_FAST || VERY_FAST
242 --ip;
243# if VERY_FAST && !ULTRA_FAST
244 --ip;
245# endif
246 hval = FRST (ip);
247
248 hval = NEXT (hval, ip);
249 htab[IDX (hval)] = ip;
250 ip++;
251
252# if VERY_FAST && !ULTRA_FAST
253 hval = NEXT (hval, ip);
254 htab[IDX (hval)] = ip;
255 ip++;
256# endif
257#else
258 ip -= len + 1;
259
260 do
261 {
262 hval = NEXT (hval, ip);
263 htab[IDX (hval)] = ip;
264 ip++;
265 }
266 while (len--);
267#endif
268 }
269 else
270 {
271 /* one more literal byte we must copy */
272 if (expect_false (op >= out_end))
273 return 0;
274
275 lit++; *op++ = *ip++;
276
277 if (expect_false (lit == MAX_LIT))
278 {
279 op [- lit - 1] = lit - 1; /* stop run */
280 lit = 0; op++; /* start run */
281 }
282 }
283 }
284
285 if (op + 3 > out_end) /* at most 3 bytes can be missing here */
286 return 0;
287
288 while (ip < in_end)
289 {
290 lit++; *op++ = *ip++;
291
292 if (expect_false (lit == MAX_LIT))
293 {
294 op [- lit - 1] = lit - 1; /* stop run */
295 lit = 0; op++; /* start run */
296 }
297 }
298
299 op [- lit - 1] = lit - 1; /* end run */
300 op -= !lit; /* undo run if length is zero */
301
302 return op - (u8 *)out_data;
303}
304
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