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