Debian Codegraph @

compression/liblzf/lzf_c.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2000-2008 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 /*MAB: added this include to silence compiler */
38 #include "lzf.h"
39
40 #include "lzfP.h"
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
101 unsigned int
102 lzf_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 /*MAB fix bug, it is __int64 rather than _int64 */
128 unsigned __int64 off; /* workaround for missing POSIX compliance */
129 #else
130 unsigned long off;
131 #endif
132 unsigned int hval;
133 int lit;
134
135 if (!in_len || !out_len)
136 return 0;
137
138 #if INIT_HTAB
139 memset (htab, 0, sizeof (htab));
140 # if 0
141 for (hslot = htab; hslot < htab + HSIZE; hslot++)
142 *hslot++ = ip;
143 # endif
144 #endif
145
146 lit = 0; op++; /* start run */
147
148 hval = (unsigned)(FRST (ip)); /*MAB silence warning with cast*/
149 while (ip < in_end - 2)
150 {
151 hval = NEXT (hval, ip);
152 hslot = htab + IDX (hval);
153 ref = *hslot; *hslot = ip;
154
155 if (
156 /*MAB: Remove 1, it was a constant, to silence compiler */
157 #if INIT_HTAB
158 ref < ip /* the next test will actually take care of this, but this is faster */
159 &&
160 #endif
161 (off = (unsigned long)(ip - ref - 1)) < MAX_OFF /*MAB silence warning with cast */
162 && ip + 4 < in_end
163 && ref > (u8 *)in_data
164 #if STRICT_ALIGN
165 && ref[0] == ip[0]
166 && ref[1] == ip[1]
167 && ref[2] == ip[2]
168 #else
169 && *(u16 *)ref == *(u16 *)ip
170 && ref[2] == ip[2]
171 #endif
172 )
173 {
174 /* match found at *ref++ */
175 unsigned int len = 2;
176 unsigned int maxlen = (unsigned)(in_end - ip - (int)len); /* MAB silence warning with casting */
177 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
178
179 if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
180 if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
181 return 0;
182
183 op [- lit - 1] = (u8) (lit - 1); /* stop run */ /*MAB: Casting to u8 to silence compiler */
184 op -= !lit; /* undo run if length is zero */
185
186 for (;;)
187 {
188 if (expect_true (maxlen > 16))
189 {
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 len++; if (ref [len] != ip [len]) break;
194
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 len++; if (ref [len] != ip [len]) break;
199
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 len++; if (ref [len] != ip [len]) break;
204
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 len++; if (ref [len] != ip [len]) break;
209 }
210
211 do
212 len++;
213 while (len < maxlen && ref[len] == ip[len]);
214
215 break;
216 }
217
218 len -= 2; /* len is now #octets - 1 */
219 ip++;
220
221 if (len < 7)
222 {
223 *op++ = (u8) ((off >> 8) + (len << 5)); /*MAB: Casting to u8 to silence compiler */
224 }
225 else
226 {
227 *op++ = (u8) ((off >> 8) + ( 7 << 5)); /*MAB: Casting to u8 to silence compiler */
228 *op++ = (u8) (len - 7); /*MAB: Casting to u8 to silence compiler */
229 }
230
231 *op++ = (u8) off; /*MAB: Casting to u8 to silence compiler */
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 = (unsigned)(FRST (ip)); /*MAB silence warning with cast*/
245
246 hval = NEXT (hval, ip);
247 htab[IDX (hval)] = ip;
248 ip++;
249
250 # if VERY_FAST && !ULTRA_FAST
251 hval = NEXT (hval, ip);
252 htab[IDX (hval)] = ip;
253 ip++;
254 # endif
255 #else
256 ip -= len + 1;
257
258 do
259 {
260 hval = NEXT (hval, ip);
261 htab[IDX (hval)] = ip;
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] = (u8) (lit - 1); /* stop run */ /*MAB: Casting to u8 to silence compiler */
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] = (u8) (lit - 1); /* stop run */ /*MAB: Casting to u8 to silence compiler */
293 lit = 0; op++; /* start run */
294 }
295 }
296
297 op [- lit - 1] = (u8) (lit - 1); /* end run */ /*MAB: Casting to u8 to silence compiler */
298 op -= !lit; /* undo run if length is zero */
299
300 return (unsigned)(op - (u8 *)out_data); /*MAB: Casting to unsigned to silence compiler */
301 }
302