VirtualBox

source: vbox/trunk/src/libs/liblzf-1.51/lzf_c.c@ 4394

Last change on this file since 4394 was 3620, checked in by vboxsync, 17 years ago

I hope this is it...

  • Property svn:eol-style set to native
File size: 7.0 KB
Line 
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#ifdef USE_UINTPTR_T /* bird: use intptr_t for pointer acrobatics on 64-bit windows. */
44# if defined(_MSC_VER)
45# include <crtdefs.h>
46# else
47# include <stdint.h>
48# endif
49#endif
50
51#define HSIZE (1 << (HLOG))
52
53/*
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
57 * it works ;)
58 */
59#ifndef FRST
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))
63#endif
64/*
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
68 *
69 * the next one is also quite good, albeit slow ;)
70 * (int)(cos(h & 0xffffff) * 1e6)
71 */
72
73#if 0
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))
78#endif
79
80#define MAX_LIT (1 << 5)
81#define MAX_OFF (1 << 13)
82#define MAX_REF ((1 << 8) + (1 << 3))
83
84/*
85 * compressed format
86 *
87 * 000LLLLL <L+1> ; literal
88 * LLLooooo oooooooo ; backref L
89 * 111ooooo LLLLLLLL oooooooo ; backref L+7
90 *
91 */
92
93unsigned int
94lzf_compress (const void *const in_data, unsigned int in_len,
95 void *out_data, unsigned int out_len
96#if LZF_STATE_ARG
97 , LZF_STATE *htab
98#endif
99 )
100{
101#if !LZF_STATE_ARG
102 LZF_STATE htab;
103#endif
104 const u8 **hslot;
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;
109 const u8 *ref;
110
111 unsigned int hval = FRST (ip);
112#ifdef USE_UINTPTR_T /* bird: use intptr_t for pointer acrobatics on 64-bit windows. */
113 uintptr_t off;
114#else
115 unsigned long off;
116#endif
117 int lit = 0;
118
119#if INIT_HTAB
120# if USE_MEMCPY
121 memset (htab, 0, sizeof (htab));
122# else
123 for (hslot = htab; hslot < htab + HSIZE; hslot++)
124 *hslot++ = ip;
125# endif
126#endif
127
128 for (;;)
129 {
130 if (ip < in_end - 2)
131 {
132 hval = NEXT (hval, ip);
133 hslot = htab + IDX (hval);
134 ref = *hslot; *hslot = ip;
135
136 if (1
137#if INIT_HTAB && !USE_MEMCPY
138 && ref < ip /* the next test will actually take care of this, but this is faster */
139#endif
140 && (off = ip - ref - 1) < MAX_OFF
141 && ip + 4 < in_end
142 && ref > (u8 *)in_data
143#if STRICT_ALIGN
144 && ref[0] == ip[0]
145 && ref[1] == ip[1]
146 && ref[2] == ip[2]
147#else
148 && *(u16 *)ref == *(u16 *)ip
149 && ref[2] == ip[2]
150#endif
151 )
152 {
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;
157
158 if (op + lit + 1 + 3 >= out_end)
159 return 0;
160
161 do
162 len++;
163 while (len < maxlen && ref[len] == ip[len]);
164
165 if (lit)
166 {
167 *op++ = lit - 1;
168 lit = -lit;
169 do
170 *op++ = ip[lit];
171 while (++lit);
172 }
173
174 len -= 2;
175 ip++;
176
177 if (len < 7)
178 {
179 *op++ = (off >> 8) + (len << 5);
180 }
181 else
182 {
183 *op++ = (off >> 8) + ( 7 << 5);
184 *op++ = len - 7;
185 }
186
187 *op++ = off;
188
189#if ULTRA_FAST || VERY_FAST
190 ip += len;
191#if VERY_FAST && !ULTRA_FAST
192 --ip;
193#endif
194 hval = FRST (ip);
195
196 hval = NEXT (hval, ip);
197 htab[IDX (hval)] = ip;
198 ip++;
199
200#if VERY_FAST && !ULTRA_FAST
201 hval = NEXT (hval, ip);
202 htab[IDX (hval)] = ip;
203 ip++;
204#endif
205#else
206 do
207 {
208 hval = NEXT (hval, ip);
209 htab[IDX (hval)] = ip;
210 ip++;
211 }
212 while (len--);
213#endif
214 continue;
215 }
216 }
217 else if (ip == in_end)
218 break;
219
220 /* one more literal byte we must copy */
221 lit++;
222 ip++;
223
224 if (lit == MAX_LIT)
225 {
226 if (op + 1 + MAX_LIT >= out_end)
227 return 0;
228
229 *op++ = MAX_LIT - 1;
230#if USE_MEMCPY
231 memcpy (op, ip - MAX_LIT, MAX_LIT);
232 op += MAX_LIT;
233 lit = 0;
234#else
235 lit = -lit;
236 do
237 *op++ = ip[lit];
238 while (++lit);
239#endif
240 }
241 }
242
243 if (lit)
244 {
245 if (op + lit + 1 >= out_end)
246 return 0;
247
248 *op++ = lit - 1;
249 lit = -lit;
250 do
251 *op++ = ip[lit];
252 while (++lit);
253 }
254
255 return op - (u8 *) out_data;
256}
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