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