1 | /* |
1 | /* |
2 | * Copyright (c) 2000 Marc Alexander Lehmann <pcg@goof.com> |
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- |
… | |
… | |
22 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
23 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
20 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
24 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH- |
21 | * 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 |
22 | * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
26 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
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 version 2 (the "GPL"), in which case the |
|
|
27 | * provisions of the GPL are applicable instead of the above. If you wish to |
|
|
28 | * allow the use of your version of this file only under the terms of the |
|
|
29 | * GPL and not to allow others to use your version of this file under the |
|
|
30 | * BSD license, indicate your decision by deleting the provisions above and |
|
|
31 | * replace them with the notice and other provisions required by the GPL. If |
|
|
32 | * you do not delete the provisions above, a recipient may use your version |
|
|
33 | * of this file under either the BSD or the GPL. |
27 | */ |
34 | */ |
28 | |
35 | |
29 | #include "lzfP.h" |
36 | #include "lzfP.h" |
30 | |
37 | |
31 | #define HSIZE (1 << (HLOG)) |
38 | #define HSIZE (1 << (HLOG)) |
… | |
… | |
34 | * don't play with this unless you benchmark! |
41 | * don't play with this unless you benchmark! |
35 | * decompression is not dependent on the hash function |
42 | * decompression is not dependent on the hash function |
36 | * the hashing function might seem strange, just believe me |
43 | * the hashing function might seem strange, just believe me |
37 | * it works ;) |
44 | * it works ;) |
38 | */ |
45 | */ |
|
|
46 | #ifndef FRST |
39 | #define FRST(p) (((p[0]) << 8) + p[1]) |
47 | # define FRST(p) (((p[0]) << 8) | p[1]) |
40 | #define NEXT(v,p) (((v) << 8) + p[2]) |
48 | # define NEXT(v,p) (((v) << 8) | p[2]) |
41 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) + h*3) & (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 |
42 | /* |
52 | /* |
43 | * 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. |
44 | * (h * 57321 >> (3*8 - HLOG)) |
54 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
|
|
55 | * the latter is also quite fast on newer CPUs, and compresses similarly. |
|
|
56 | * |
45 | * the next one is also quite good, albeit slow ;) |
57 | * the next one is also quite good, albeit slow ;) |
46 | * (int)(cos(h & 0xffffff) * 1e6) |
58 | * (int)(cos(h & 0xffffff) * 1e6) |
47 | */ |
59 | */ |
48 | |
60 | |
49 | #if 0 |
61 | #if 0 |
50 | /* original lzv-like hash function */ |
62 | /* original lzv-like hash function, much worse and thus slower */ |
51 | # define FRST(p) (p[0] << 5) ^ p[1] |
63 | # define FRST(p) (p[0] << 5) ^ p[1] |
52 | # define NEXT(v,p) ((v) << 5) ^ p[2] |
64 | # define NEXT(v,p) ((v) << 5) ^ p[2] |
53 | # define IDX(h) ((h) & (HSIZE - 1)) |
65 | # define IDX(h) ((h) & (HSIZE - 1)) |
54 | #endif |
66 | #endif |
55 | |
67 | |
56 | #define MAX_LIT (1 << 5) |
68 | #define MAX_LIT (1 << 5) |
57 | #define MAX_OFF (1 << 13) |
69 | #define MAX_OFF (1 << 13) |
58 | #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 |
59 | |
78 | |
60 | /* |
79 | /* |
61 | * compressed format |
80 | * compressed format |
62 | * |
81 | * |
63 | * 000LLLLL <L+1> ; literal |
82 | * 000LLLLL <L+1> ; literal |
64 | * LLLOOOOO oooooooo ; backref L |
83 | * LLLooooo oooooooo ; backref L |
65 | * 111OOOOO LLLLLLLL oooooooo ; backref L+7 |
84 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 |
66 | * |
85 | * |
67 | */ |
86 | */ |
68 | |
87 | |
69 | unsigned int |
88 | unsigned int |
70 | lzf_compress (const void *const in_data, unsigned int in_len, |
89 | lzf_compress (const void *const in_data, unsigned int in_len, |
71 | void *out_data, unsigned int out_len) |
90 | void *out_data, unsigned int out_len |
|
|
91 | #if LZF_STATE_ARG |
|
|
92 | , LZF_STATE htab |
|
|
93 | #endif |
|
|
94 | ) |
72 | { |
95 | { |
73 | const u8 *htab[HSIZE]; |
96 | #if !LZF_STATE_ARG |
|
|
97 | LZF_STATE htab; |
|
|
98 | #endif |
74 | const u8 **hslot; |
99 | const u8 **hslot; |
75 | const u8 *ip = in_data; |
100 | const u8 *ip = (const u8 *)in_data; |
76 | u8 *op = out_data; |
101 | u8 *op = (u8 *)out_data; |
77 | const u8 *in_end = ip + in_len; |
102 | const u8 *in_end = ip + in_len; |
78 | u8 *out_end = op + out_len; |
103 | u8 *out_end = op + out_len; |
79 | const u8 *ref; |
104 | const u8 *ref; |
80 | |
105 | |
81 | unsigned int hval = FRST (ip); |
106 | unsigned int hval = FRST (ip); |
82 | unsigned long off; |
107 | unsigned long off; |
83 | int lit = 0; |
108 | int lit = 0; |
84 | |
109 | |
85 | #if INIT_HTAB |
110 | #if INIT_HTAB |
86 | # if USE_MEMCPY |
|
|
87 | memset (htab, 0, sizeof (htab)); |
111 | memset (htab, 0, sizeof (htab)); |
88 | # else |
112 | # if 0 |
89 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
113 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
90 | *hslot++ = ip; |
114 | *hslot++ = ip; |
91 | # endif |
115 | # endif |
92 | #endif |
116 | #endif |
93 | |
117 | |
94 | do |
118 | for (;;) |
95 | { |
119 | { |
|
|
120 | if (ip < in_end - 2) |
|
|
121 | { |
96 | hval = NEXT (hval, ip); |
122 | hval = NEXT (hval, ip); |
97 | hslot = htab + IDX (hval); |
123 | hslot = htab + IDX (hval); |
98 | ref = *hslot; *hslot = ip; |
124 | ref = *hslot; *hslot = ip; |
99 | |
125 | |
100 | if (1 |
126 | if (1 |
101 | #if INIT_HTAB && !USE_MEMCPY |
127 | #if INIT_HTAB && !USE_MEMCPY |
102 | && ref < ip /* the next test will actually take care of this, but it is faster */ |
128 | && ref < ip /* the next test will actually take care of this, but this is faster */ |
103 | #endif |
129 | #endif |
104 | && (off = ip - ref - 1) < MAX_OFF |
130 | && (off = ip - ref - 1) < MAX_OFF |
105 | && ip + 4 < in_end |
131 | && ip + 4 < in_end |
106 | && ref > (u8 *)in_data |
132 | && ref > (u8 *)in_data |
107 | #if STRICT_ALIGN |
133 | #if STRICT_ALIGN |
108 | && ref[0] == ip[0] |
134 | && ref[0] == ip[0] |
109 | && ref[1] == ip[1] |
135 | && ref[1] == ip[1] |
110 | && ref[2] == ip[2] |
136 | && ref[2] == ip[2] |
111 | #else |
137 | #else |
112 | && *(u16 *)ref == *(u16 *)ip |
138 | && *(u16 *)ref == *(u16 *)ip |
113 | && ref[2] == ip[2] |
139 | && ref[2] == ip[2] |
114 | #endif |
140 | #endif |
115 | ) |
141 | ) |
116 | { |
142 | { |
117 | /* match found at *ref++ */ |
143 | /* match found at *ref++ */ |
118 | unsigned int len = 2; |
144 | unsigned int len = 2; |
119 | unsigned int maxlen = in_end - ip - len; |
145 | unsigned int maxlen = in_end - ip - len; |
120 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
146 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
121 | |
147 | |
122 | do |
148 | if (op + lit + 1 + 3 >= out_end) |
123 | len++; |
149 | return 0; |
|
|
150 | |
|
|
151 | do |
|
|
152 | len++; |
124 | while (len < maxlen && ref[len] == ip[len]); |
153 | while (len < maxlen && ref[len] == ip[len]); |
125 | |
154 | |
126 | if (op + lit + 1 + 3 >= out_end) |
155 | if (lit) |
127 | return 0; |
156 | { |
128 | |
|
|
129 | if (lit) |
|
|
130 | { |
|
|
131 | *op++ = lit - 1; |
157 | *op++ = lit - 1; |
132 | lit = -lit; |
158 | lit = -lit; |
133 | do |
159 | do |
134 | *op++ = ip[lit]; |
160 | *op++ = ip[lit]; |
135 | while (++lit); |
161 | while (++lit); |
136 | } |
162 | } |
137 | |
163 | |
138 | len -= 2; |
164 | len -= 2; |
139 | ip++; |
165 | ip++; |
140 | |
166 | |
141 | if (len < 7) |
167 | if (len < 7) |
142 | { |
168 | { |
143 | *op++ = (off >> 8) + (len << 5); |
169 | *op++ = (off >> 8) + (len << 5); |
144 | } |
170 | } |
145 | else |
171 | else |
146 | { |
172 | { |
147 | *op++ = (off >> 8) + ( 7 << 5); |
173 | *op++ = (off >> 8) + ( 7 << 5); |
148 | *op++ = len - 7; |
174 | *op++ = len - 7; |
149 | } |
175 | } |
150 | |
176 | |
151 | *op++ = off; |
177 | *op++ = off; |
152 | |
178 | |
153 | #if ULTRA_FAST |
179 | #if ULTRA_FAST || VERY_FAST |
154 | ip += len; |
180 | ip += len; |
|
|
181 | #if VERY_FAST && !ULTRA_FAST |
|
|
182 | --ip; |
|
|
183 | #endif |
155 | hval = FRST (ip); |
184 | hval = FRST (ip); |
|
|
185 | |
156 | hval = NEXT (hval, ip); |
186 | hval = NEXT (hval, ip); |
157 | htab[IDX (hval)] = ip; |
187 | htab[IDX (hval)] = ip; |
158 | ip++; |
188 | ip++; |
159 | #else |
189 | |
160 | do |
190 | #if VERY_FAST && !ULTRA_FAST |
161 | { |
|
|
162 | hval = NEXT (hval, ip); |
191 | hval = NEXT (hval, ip); |
163 | htab[IDX (hval)] = ip; |
192 | htab[IDX (hval)] = ip; |
|
|
193 | ip++; |
|
|
194 | #endif |
|
|
195 | #else |
|
|
196 | do |
|
|
197 | { |
|
|
198 | hval = NEXT (hval, ip); |
|
|
199 | htab[IDX (hval)] = ip; |
|
|
200 | ip++; |
|
|
201 | } |
|
|
202 | while (len--); |
|
|
203 | #endif |
|
|
204 | continue; |
|
|
205 | } |
|
|
206 | } |
|
|
207 | else if (ip == in_end) |
|
|
208 | break; |
|
|
209 | |
|
|
210 | /* one more literal byte we must copy */ |
|
|
211 | lit++; |
164 | ip++; |
212 | ip++; |
165 | } |
|
|
166 | while (len--); |
|
|
167 | #endif |
|
|
168 | } |
|
|
169 | else |
|
|
170 | { |
|
|
171 | /* one more literal byte we must copy */ |
|
|
172 | lit++; |
|
|
173 | ip++; |
|
|
174 | |
213 | |
175 | if (lit == MAX_LIT) |
214 | if (lit == MAX_LIT) |
176 | { |
215 | { |
177 | if (op + 1 + MAX_LIT >= out_end) |
216 | if (op + 1 + MAX_LIT >= out_end) |
178 | return 0; |
217 | return 0; |
179 | |
218 | |
180 | *op++ = MAX_LIT - 1; |
219 | *op++ = MAX_LIT - 1; |
181 | #if USE_MEMCPY |
220 | |
182 | memcpy (op, ip - MAX_LIT, MAX_LIT); |
221 | #ifdef lzf_movsb |
183 | op += MAX_LIT; |
222 | ip -= lit; |
184 | lit = 0; |
223 | lzf_movsb (op, ip, lit); |
185 | #else |
224 | #else |
186 | lit = -lit; |
225 | lit = -lit; |
187 | do |
226 | do |
188 | *op++ = ip[lit]; |
227 | *op++ = ip[lit]; |
189 | while (++lit); |
228 | while (++lit); |
190 | #endif |
229 | #endif |
191 | } |
230 | } |
192 | } |
|
|
193 | } |
231 | } |
194 | while (ip < in_end); |
|
|
195 | |
232 | |
196 | if (lit) |
233 | if (lit) |
197 | { |
234 | { |
198 | if (op + lit + 1 >= out_end) |
235 | if (op + lit + 1 >= out_end) |
199 | return 0; |
236 | return 0; |
200 | |
237 | |
201 | *op++ = lit - 1; |
238 | *op++ = lit - 1; |
|
|
239 | #ifdef lzf_movsb |
|
|
240 | ip -= lit; |
|
|
241 | lzf_movsb (op, ip, lit); |
|
|
242 | #else |
202 | lit = -lit; |
243 | lit = -lit; |
203 | do |
244 | do |
204 | *op++ = ip[lit]; |
245 | *op++ = ip[lit]; |
205 | while (++lit); |
246 | while (++lit); |
|
|
247 | #endif |
206 | } |
248 | } |
207 | |
249 | |
208 | return op - (u8 *) out_data; |
250 | return op - (u8 *) out_data; |
209 | } |
251 | } |
|
|
252 | |