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)) |
… | |
… | |
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 | */ |
39 | #define FRST(p) (((p[0]) << 8) + p[1]) |
49 | #define FRST(p) (((p[0]) << 8) + p[1]) |
40 | #define NEXT(v,p) (((v) << 8) + p[2]) |
50 | #define NEXT(v,p) (((v) << 8) + p[2]) |
41 | #define IDX(h) ((((h ^ (h << 4)) >> (3*8 - HLOG)) + h*3) & (HSIZE - 1)) |
51 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
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 sligthly better |
|
|
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)) |
… | |
… | |
66 | * |
78 | * |
67 | */ |
79 | */ |
68 | |
80 | |
69 | unsigned int |
81 | unsigned int |
70 | lzf_compress (const void *const in_data, unsigned int in_len, |
82 | lzf_compress (const void *const in_data, unsigned int in_len, |
71 | void *out_data, unsigned int out_len) |
83 | void *out_data, unsigned int out_len |
|
|
84 | #if LZF_STATE_ARG |
|
|
85 | , LZF_STATE *htab |
|
|
86 | #endif |
|
|
87 | ) |
72 | { |
88 | { |
73 | const u8 *htab[HSIZE]; |
89 | #if !LZF_STATE_ARG |
|
|
90 | LZF_STATE htab; |
|
|
91 | #endif |
|
|
92 | const u8 **hslot; |
74 | const u8 *ip = in_data; |
93 | const u8 *ip = (const u8 *)in_data; |
75 | u8 *op = out_data; |
94 | u8 *op = (u8 *)out_data; |
76 | const u8 *in_end = ip + in_len; |
95 | const u8 *in_end = ip + in_len; |
77 | u8 *out_end = op + out_len; |
96 | u8 *out_end = op + out_len; |
78 | const u8 *ref; |
97 | const u8 *ref; |
79 | |
98 | |
80 | unsigned int hval = FRST (ip); |
99 | unsigned int hval = FRST (ip); |
81 | unsigned int off; |
100 | unsigned long off; |
82 | int lit = 0; |
101 | int lit = 0; |
83 | |
102 | |
84 | #if INIT_HTAB |
103 | #if INIT_HTAB |
85 | # if USE_MEMCPY |
104 | # if USE_MEMCPY |
86 | memset (htab, 0, sizeof (htab)); |
105 | memset (htab, 0, sizeof (htab)); |
87 | # else |
106 | # else |
88 | for (off = 0; off < HSIZE; off++) |
107 | for (hslot = htab; hslot < htab + HSIZE; hslot++) |
89 | htab[off] = ip; |
108 | *hslot++ = ip; |
90 | # endif |
109 | # endif |
91 | #endif |
110 | #endif |
92 | |
111 | |
93 | do |
112 | for (;;) |
94 | { |
113 | { |
|
|
114 | if (ip < in_end - 2) |
|
|
115 | { |
95 | hval = NEXT (hval, ip); |
116 | hval = NEXT (hval, ip); |
96 | off = IDX (hval); |
117 | hslot = htab + IDX (hval); |
97 | ref = htab[off]; |
118 | ref = *hslot; *hslot = ip; |
98 | htab[off] = ip; |
|
|
99 | |
119 | |
100 | if (1 |
120 | if (1 |
101 | #if INIT_HTAB && !USE_MEMCPY |
121 | #if INIT_HTAB && !USE_MEMCPY |
102 | && ref < ip /* the next test will actually take care of this, but it is faster */ |
122 | && ref < ip /* the next test will actually take care of this, but this is faster */ |
103 | #endif |
123 | #endif |
104 | && (off = ip - ref - 1) < MAX_OFF |
124 | && (off = ip - ref - 1) < MAX_OFF |
105 | && ip + 4 < in_end |
125 | && ip + 4 < in_end |
106 | && ref > (u8 *)in_data |
126 | && ref > (u8 *)in_data |
107 | #if STRICT_ALIGN |
127 | #if STRICT_ALIGN |
108 | && ref[0] == ip[0] |
128 | && ref[0] == ip[0] |
109 | && ref[1] == ip[1] |
129 | && ref[1] == ip[1] |
110 | && ref[2] == ip[2] |
130 | && ref[2] == ip[2] |
111 | #else |
131 | #else |
112 | && *(u16 *)ref == *(u16 *)ip |
132 | && *(u16 *)ref == *(u16 *)ip |
113 | && ref[2] == ip[2] |
133 | && ref[2] == ip[2] |
114 | #endif |
134 | #endif |
115 | ) |
135 | ) |
116 | { |
136 | { |
117 | /* match found at *ref++ */ |
137 | /* match found at *ref++ */ |
118 | unsigned int len = 2; |
138 | unsigned int len = 2; |
119 | unsigned int maxlen = in_end - ip - len; |
139 | unsigned int maxlen = in_end - ip - len; |
120 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
140 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
121 | |
141 | |
122 | do |
142 | do |
123 | len++; |
143 | len++; |
124 | while (len < maxlen && ref[len] == ip[len]); |
144 | while (len < maxlen && ref[len] == ip[len]); |
125 | |
145 | |
126 | if (op + lit + 1 + 3 >= out_end) |
146 | if (op + lit + 1 + 3 >= out_end) |
127 | return 0; |
147 | return 0; |
128 | |
148 | |
129 | if (lit) |
149 | if (lit) |
130 | { |
150 | { |
131 | *op++ = lit - 1; |
151 | *op++ = lit - 1; |
132 | lit = -lit; |
152 | lit = -lit; |
133 | do |
153 | do |
134 | *op++ = ip[lit]; |
154 | *op++ = ip[lit]; |
135 | while (++lit); |
155 | while (++lit); |
136 | } |
156 | } |
137 | |
157 | |
138 | len -= 2; |
158 | len -= 2; |
139 | ip++; |
159 | ip++; |
140 | |
160 | |
141 | if (len < 7) |
161 | if (len < 7) |
142 | { |
162 | { |
143 | *op++ = (off >> 8) + (len << 5); |
163 | *op++ = (off >> 8) + (len << 5); |
144 | } |
164 | } |
145 | else |
165 | else |
146 | { |
166 | { |
147 | *op++ = (off >> 8) + ( 7 << 5); |
167 | *op++ = (off >> 8) + ( 7 << 5); |
148 | *op++ = len - 7; |
168 | *op++ = len - 7; |
149 | } |
169 | } |
150 | |
170 | |
151 | *op++ = off; |
171 | *op++ = off; |
152 | |
172 | |
153 | #if ULTRA_FAST |
173 | #if ULTRA_FAST |
154 | ip += len; |
174 | ip += len; |
155 | hval = FRST (ip); |
175 | hval = FRST (ip); |
156 | hval = NEXT (hval, ip); |
176 | hval = NEXT (hval, ip); |
157 | htab[IDX (hval)] = ip; |
177 | htab[IDX (hval)] = ip; |
158 | ip++; |
178 | ip++; |
159 | #else |
179 | #else |
160 | do |
180 | do |
161 | { |
181 | { |
162 | hval = NEXT (hval, ip); |
182 | hval = NEXT (hval, ip); |
163 | htab[IDX (hval)] = ip; |
183 | htab[IDX (hval)] = ip; |
|
|
184 | ip++; |
|
|
185 | } |
|
|
186 | while (len--); |
|
|
187 | #endif |
|
|
188 | continue; |
|
|
189 | } |
|
|
190 | } |
|
|
191 | else if (ip == in_end) |
|
|
192 | break; |
|
|
193 | |
|
|
194 | /* one more literal byte we must copy */ |
|
|
195 | lit++; |
164 | ip++; |
196 | ip++; |
165 | } |
|
|
166 | while (len--); |
|
|
167 | #endif |
|
|
168 | } |
|
|
169 | else |
|
|
170 | { |
|
|
171 | /* one more literal byte we must copy */ |
|
|
172 | lit++; |
|
|
173 | ip++; |
|
|
174 | |
197 | |
175 | if (lit == MAX_LIT) |
198 | if (lit == MAX_LIT) |
176 | { |
199 | { |
177 | if (op + 1 + MAX_LIT >= out_end) |
200 | if (op + 1 + MAX_LIT >= out_end) |
178 | return 0; |
201 | return 0; |
179 | |
202 | |
180 | *op++ = MAX_LIT - 1; |
203 | *op++ = MAX_LIT - 1; |
181 | #if USE_MEMCPY |
204 | #if USE_MEMCPY |
182 | memcpy (op, ip - MAX_LIT, MAX_LIT); |
205 | memcpy (op, ip - MAX_LIT, MAX_LIT); |
183 | op += MAX_LIT; |
206 | op += MAX_LIT; |
184 | lit = 0; |
207 | lit = 0; |
185 | #else |
208 | #else |
186 | lit = -lit; |
209 | lit = -lit; |
187 | do |
210 | do |
188 | *op++ = ip[lit]; |
211 | *op++ = ip[lit]; |
189 | while (++lit); |
212 | while (++lit); |
190 | #endif |
213 | #endif |
191 | } |
214 | } |
192 | } |
|
|
193 | } |
215 | } |
194 | while (ip < in_end); |
|
|
195 | |
216 | |
196 | if (lit) |
217 | if (lit) |
197 | { |
218 | { |
198 | if (op + lit + 1 >= out_end) |
219 | if (op + lit + 1 >= out_end) |
199 | return 0; |
220 | return 0; |