VirtualBox

source: vbox/trunk/src/libs/liblzf-3.6/lzf_c.c@ 100762

Last change on this file since 100762 was 96429, checked in by vboxsync, 2 years ago

src/libs: Switch to liblzf-3.6, bugref:8515

  • Property svn:eol-style set to native
File size: 9.0 KB
Line 
1/*
2 * Copyright (c) 2000-2010 Marc Alexander Lehmann <[email protected]>
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 * the data format is not dependent on the hash function.
48 * the hash 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, L+1=1..33 octets
97 * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset
98 * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset
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 *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#ifdef VBOX
127 uintptr_t off;
128#else
129# error "Build config error."
130#if defined (WIN32) && defined (_M_X64)
131 unsigned _int64 off; /* workaround for missing POSIX compliance */
132#else
133 unsigned long off;
134#endif
135#endif
136 unsigned int hval;
137 int lit;
138
139 if (!in_len || !out_len)
140 return 0;
141
142#if INIT_HTAB
143 memset (htab, 0, sizeof (htab));
144#endif
145
146 lit = 0; op++; /* start run */
147
148 hval = FRST (ip);
149 while (ip < in_end - 2)
150 {
151 LZF_HSLOT *hslot;
152
153 hval = NEXT (hval, ip);
154 hslot = htab + IDX (hval);
155 ref = *hslot + LZF_HSLOT_BIAS; *hslot = ip - LZF_HSLOT_BIAS;
156
157 if (1
158#if INIT_HTAB
159 && ref < ip /* the next test will actually take care of this, but this is faster */
160#endif
161 && (off = ip - ref - 1) < MAX_OFF
162 && ref > (u8 *)in_data
163 && ref[2] == ip[2]
164#if STRICT_ALIGN
165 && ((ref[1] << 8) | ref[0]) == ((ip[1] << 8) | ip[0])
166#else
167 && *(u16 *)ref == *(u16 *)ip
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 if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
177 if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
178 return 0;
179
180 op [- lit - 1] = lit - 1; /* stop run */
181 op -= !lit; /* undo run if length is zero */
182
183 for (;;)
184 {
185 if (expect_true (maxlen > 16))
186 {
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 len++; if (ref [len] != ip [len]) break;
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
208 do
209 len++;
210 while (len < maxlen && ref[len] == ip[len]);
211
212 break;
213 }
214
215 len -= 2; /* len is now #octets - 1 */
216 ip++;
217
218 if (len < 7)
219 {
220 *op++ = (off >> 8) + (len << 5);
221 }
222 else
223 {
224 *op++ = (off >> 8) + ( 7 << 5);
225 *op++ = len - 7;
226 }
227
228 *op++ = off;
229
230 lit = 0; op++; /* start run */
231
232 ip += len + 1;
233
234 if (expect_false (ip >= in_end - 2))
235 break;
236
237#if ULTRA_FAST || VERY_FAST
238 --ip;
239# if VERY_FAST && !ULTRA_FAST
240 --ip;
241# endif
242 hval = FRST (ip);
243
244 hval = NEXT (hval, ip);
245 htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
246 ip++;
247
248# if VERY_FAST && !ULTRA_FAST
249 hval = NEXT (hval, ip);
250 htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
251 ip++;
252# endif
253#else
254 ip -= len + 1;
255
256 do
257 {
258 hval = NEXT (hval, ip);
259 htab[IDX (hval)] = ip - LZF_HSLOT_BIAS;
260 ip++;
261 }
262 while (len--);
263#endif
264 }
265 else
266 {
267 /* one more literal byte we must copy */
268 if (expect_false (op >= out_end))
269 return 0;
270
271 lit++; *op++ = *ip++;
272
273 if (expect_false (lit == MAX_LIT))
274 {
275 op [- lit - 1] = lit - 1; /* stop run */
276 lit = 0; op++; /* start run */
277 }
278 }
279 }
280
281 if (op + 3 > out_end) /* at most 3 bytes can be missing here */
282 return 0;
283
284 while (ip < in_end)
285 {
286 lit++; *op++ = *ip++;
287
288 if (expect_false (lit == MAX_LIT))
289 {
290 op [- lit - 1] = lit - 1; /* stop run */
291 lit = 0; op++; /* start run */
292 }
293 }
294
295 op [- lit - 1] = lit - 1; /* end run */
296 op -= !lit; /* undo run if length is zero */
297
298 return op - (u8 *)out_data;
299}
300
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