1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2003 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)) |
59 | |
74 | |
60 | /* |
75 | /* |
61 | * compressed format |
76 | * compressed format |
62 | * |
77 | * |
63 | * 000LLLLL <L+1> ; literal |
78 | * 000LLLLL <L+1> ; literal |
64 | * LLLOOOOO oooooooo ; backref L |
79 | * LLLooooo oooooooo ; backref L |
65 | * 111OOOOO LLLLLLLL oooooooo ; backref L+7 |
80 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 |
66 | * |
81 | * |
67 | */ |
82 | */ |
68 | |
83 | |
69 | unsigned int |
84 | unsigned int |
70 | lzf_compress (const void *const in_data, unsigned int in_len, |
85 | lzf_compress (const void *const in_data, unsigned int in_len, |
71 | void *out_data, unsigned int out_len |
86 | void *out_data, unsigned int out_len |
72 | #if LZF_STATE_ARG |
87 | #if LZF_STATE_ARG |
73 | , LZF_STATE *htab |
88 | , LZF_STATE htab |
74 | #endif |
89 | #endif |
75 | ) |
90 | ) |
76 | { |
91 | { |
77 | #if !LZF_STATE_ARG |
92 | #if !LZF_STATE_ARG |
78 | LZF_STATE htab; |
93 | LZF_STATE htab; |
… | |
… | |
125 | /* match found at *ref++ */ |
140 | /* match found at *ref++ */ |
126 | unsigned int len = 2; |
141 | unsigned int len = 2; |
127 | unsigned int maxlen = in_end - ip - len; |
142 | unsigned int maxlen = in_end - ip - len; |
128 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
143 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
129 | |
144 | |
|
|
145 | if (op + lit + 1 + 3 >= out_end) |
|
|
146 | return 0; |
|
|
147 | |
130 | do |
148 | do |
131 | len++; |
149 | len++; |
132 | while (len < maxlen && ref[len] == ip[len]); |
150 | while (len < maxlen && ref[len] == ip[len]); |
133 | |
|
|
134 | if (op + lit + 1 + 3 >= out_end) |
|
|
135 | return 0; |
|
|
136 | |
151 | |
137 | if (lit) |
152 | if (lit) |
138 | { |
153 | { |
139 | *op++ = lit - 1; |
154 | *op++ = lit - 1; |
140 | lit = -lit; |
155 | lit = -lit; |
… | |
… | |
156 | *op++ = len - 7; |
171 | *op++ = len - 7; |
157 | } |
172 | } |
158 | |
173 | |
159 | *op++ = off; |
174 | *op++ = off; |
160 | |
175 | |
161 | #if ULTRA_FAST |
176 | #if ULTRA_FAST || VERY_FAST |
162 | ip += len; |
177 | ip += len; |
|
|
178 | #if VERY_FAST && !ULTRA_FAST |
|
|
179 | --ip; |
|
|
180 | #endif |
163 | hval = FRST (ip); |
181 | hval = FRST (ip); |
|
|
182 | |
164 | hval = NEXT (hval, ip); |
183 | hval = NEXT (hval, ip); |
165 | htab[IDX (hval)] = ip; |
184 | htab[IDX (hval)] = ip; |
166 | ip++; |
185 | ip++; |
|
|
186 | |
|
|
187 | #if VERY_FAST && !ULTRA_FAST |
|
|
188 | hval = NEXT (hval, ip); |
|
|
189 | htab[IDX (hval)] = ip; |
|
|
190 | ip++; |
|
|
191 | #endif |
167 | #else |
192 | #else |
168 | do |
193 | do |
169 | { |
194 | { |
170 | hval = NEXT (hval, ip); |
195 | hval = NEXT (hval, ip); |
171 | htab[IDX (hval)] = ip; |
196 | htab[IDX (hval)] = ip; |