1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2005 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de> |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
4 | * Redistribution and use in source and binary forms, with or without modifica- |
5 | * tion, are permitted provided that the following conditions are met: |
5 | * tion, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
7 | * 1. Redistributions of source code must retain the above copyright notice, |
8 | * this list of conditions and the following disclaimer. |
8 | * this list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
10 | * 2. Redistributions in binary form must reproduce the above copyright |
11 | * notice, this list of conditions and the following disclaimer in the |
11 | * notice, this list of conditions and the following disclaimer in the |
12 | * documentation and/or other materials provided with the distribution. |
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 | * |
13 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED |
14 | * 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- |
15 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER- |
19 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
16 | * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO |
20 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
17 | * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE- |
… | |
… | |
44 | * don't play with this unless you benchmark! |
41 | * don't play with this unless you benchmark! |
45 | * decompression is not dependent on the hash function |
42 | * decompression is not dependent on the hash function |
46 | * the hashing function might seem strange, just believe me |
43 | * the hashing function might seem strange, just believe me |
47 | * it works ;) |
44 | * it works ;) |
48 | */ |
45 | */ |
|
|
46 | #ifndef FRST |
49 | #define FRST(p) (((p[0]) << 8) | p[1]) |
47 | # define FRST(p) (((p[0]) << 8) | p[1]) |
50 | #define NEXT(v,p) (((v) << 8) | p[2]) |
48 | # define NEXT(v,p) (((v) << 8) | p[2]) |
51 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
49 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
|
|
50 | /*# define IDX(h) ((ip[0] * 121 ^ ip[1] * 33 ^ ip[2] * 1) & (HSIZE-1))*/ |
|
|
51 | #endif |
52 | /* |
52 | /* |
53 | * IDX works because it is very similar to a multiplicative hash, e.g. |
53 | * IDX works because it is very similar to a multiplicative hash, e.g. |
54 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
54 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
55 | * the latter is also quite fast on newer CPUs, and sligthly better |
55 | * the latter is also quite fast on newer CPUs, and compresses similarly. |
56 | * |
56 | * |
57 | * the next one is also quite good, albeit slow ;) |
57 | * the next one is also quite good, albeit slow ;) |
58 | * (int)(cos(h & 0xffffff) * 1e6) |
58 | * (int)(cos(h & 0xffffff) * 1e6) |
59 | */ |
59 | */ |
60 | |
60 | |
… | |
… | |
65 | # define IDX(h) ((h) & (HSIZE - 1)) |
65 | # define IDX(h) ((h) & (HSIZE - 1)) |
66 | #endif |
66 | #endif |
67 | |
67 | |
68 | #define MAX_LIT (1 << 5) |
68 | #define MAX_LIT (1 << 5) |
69 | #define MAX_OFF (1 << 13) |
69 | #define MAX_OFF (1 << 13) |
70 | #define MAX_REF ((1 << 8) + (1 << 3)) |
70 | #define MAX_REF ((1 << 8) + (1 << 3)) |
|
|
71 | |
|
|
72 | #if (__i386 || __amd64) && __GNUC__ >= 3 |
|
|
73 | # define lzf_movsb(dst, src, len) \ |
|
|
74 | asm ("rep movsb" \ |
|
|
75 | : "=D" (dst), "=S" (src), "=c" (len) \ |
|
|
76 | : "0" (dst), "1" (src), "2" (len)); |
|
|
77 | #endif |
71 | |
78 | |
72 | /* |
79 | /* |
73 | * compressed format |
80 | * compressed format |
74 | * |
81 | * |
75 | * 000LLLLL <L+1> ; literal |
82 | * 000LLLLL <L+1> ; literal |
76 | * LLLOOOOO oooooooo ; backref L |
83 | * LLLooooo oooooooo ; backref L |
77 | * 111OOOOO LLLLLLLL oooooooo ; backref L+7 |
84 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 |
78 | * |
85 | * |
79 | */ |
86 | */ |
80 | |
87 | |
81 | unsigned int |
88 | unsigned int |
82 | lzf_compress (const void *const in_data, unsigned int in_len, |
89 | lzf_compress (const void *const in_data, unsigned int in_len, |
83 | void *out_data, unsigned int out_len |
90 | void *out_data, unsigned int out_len |
84 | #if LZF_STATE_ARG |
91 | #if LZF_STATE_ARG |
85 | , LZF_STATE *htab |
92 | , LZF_STATE htab |
86 | #endif |
93 | #endif |
87 | ) |
94 | ) |
88 | { |
95 | { |
89 | #if !LZF_STATE_ARG |
96 | #if !LZF_STATE_ARG |
90 | LZF_STATE htab; |
97 | LZF_STATE htab; |
… | |
… | |
99 | unsigned int hval = FRST (ip); |
106 | unsigned int hval = FRST (ip); |
100 | unsigned long off; |
107 | unsigned long off; |
101 | int lit = 0; |
108 | int lit = 0; |
102 | |
109 | |
103 | #if INIT_HTAB |
110 | #if INIT_HTAB |
104 | # if USE_MEMCPY |
|
|
105 | memset (htab, 0, sizeof (htab)); |
111 | memset (htab, 0, sizeof (htab)); |
106 | # else |
112 | # if 0 |
107 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
113 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
108 | *hslot++ = ip; |
114 | *hslot++ = ip; |
109 | # endif |
115 | # endif |
110 | #endif |
116 | #endif |
111 | |
117 | |
112 | for (;;) |
118 | for (;;) |
113 | { |
119 | { |
… | |
… | |
209 | { |
215 | { |
210 | if (op + 1 + MAX_LIT >= out_end) |
216 | if (op + 1 + MAX_LIT >= out_end) |
211 | return 0; |
217 | return 0; |
212 | |
218 | |
213 | *op++ = MAX_LIT - 1; |
219 | *op++ = MAX_LIT - 1; |
214 | #if USE_MEMCPY |
220 | |
215 | memcpy (op, ip - MAX_LIT, MAX_LIT); |
221 | #ifdef lzf_movsb |
216 | op += MAX_LIT; |
222 | ip -= lit; |
217 | lit = 0; |
223 | lzf_movsb (op, ip, lit); |
218 | #else |
224 | #else |
219 | lit = -lit; |
225 | lit = -lit; |
220 | do |
226 | do |
221 | *op++ = ip[lit]; |
227 | *op++ = ip[lit]; |
222 | while (++lit); |
228 | while (++lit); |
… | |
… | |
228 | { |
234 | { |
229 | if (op + lit + 1 >= out_end) |
235 | if (op + lit + 1 >= out_end) |
230 | return 0; |
236 | return 0; |
231 | |
237 | |
232 | *op++ = lit - 1; |
238 | *op++ = lit - 1; |
|
|
239 | #ifdef lzf_movsb |
|
|
240 | ip -= lit; |
|
|
241 | lzf_movsb (op, ip, lit); |
|
|
242 | #else |
233 | lit = -lit; |
243 | lit = -lit; |
234 | do |
244 | do |
235 | *op++ = ip[lit]; |
245 | *op++ = ip[lit]; |
236 | while (++lit); |
246 | while (++lit); |
|
|
247 | #endif |
237 | } |
248 | } |
238 | |
249 | |
239 | return op - (u8 *) out_data; |
250 | return op - (u8 *) out_data; |
240 | } |
251 | } |
|
|
252 | |