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, |
… | |
… | |
44 | * don't play with this unless you benchmark! |
44 | * don't play with this unless you benchmark! |
45 | * decompression is not dependent on the hash function |
45 | * decompression is not dependent on the hash function |
46 | * the hashing function might seem strange, just believe me |
46 | * the hashing function might seem strange, just believe me |
47 | * it works ;) |
47 | * it works ;) |
48 | */ |
48 | */ |
|
|
49 | #ifndef FRST |
49 | #define FRST(p) (((p[0]) << 8) + p[1]) |
50 | # define FRST(p) (((p[0]) << 8) | p[1]) |
50 | #define NEXT(v,p) (((v) << 8) + p[2]) |
51 | # define NEXT(v,p) (((v) << 8) | p[2]) |
51 | #define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
52 | # define IDX(h) ((((h ^ (h << 5)) >> (3*8 - HLOG)) - h*5) & (HSIZE - 1)) |
|
|
53 | #endif |
52 | /* |
54 | /* |
53 | * IDX works because it is very similar to a multiplicative hash, e.g. |
55 | * IDX works because it is very similar to a multiplicative hash, e.g. |
54 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
56 | * ((h * 57321 >> (3*8 - HLOG)) & (HSIZE - 1)) |
55 | * the latter is also quite fast on newer CPUs, and sligthly better |
57 | * the latter is also quite fast on newer CPUs, and sligthly better |
56 | * |
58 | * |
… | |
… | |
71 | |
73 | |
72 | /* |
74 | /* |
73 | * compressed format |
75 | * compressed format |
74 | * |
76 | * |
75 | * 000LLLLL <L+1> ; literal |
77 | * 000LLLLL <L+1> ; literal |
76 | * LLLOOOOO oooooooo ; backref L |
78 | * LLLooooo oooooooo ; backref L |
77 | * 111OOOOO LLLLLLLL oooooooo ; backref L+7 |
79 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 |
78 | * |
80 | * |
79 | */ |
81 | */ |
80 | |
82 | |
81 | unsigned int |
83 | unsigned int |
82 | lzf_compress (const void *const in_data, unsigned int in_len, |
84 | lzf_compress (const void *const in_data, unsigned int in_len, |
… | |
… | |
137 | /* match found at *ref++ */ |
139 | /* match found at *ref++ */ |
138 | unsigned int len = 2; |
140 | unsigned int len = 2; |
139 | unsigned int maxlen = in_end - ip - len; |
141 | unsigned int maxlen = in_end - ip - len; |
140 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
142 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
141 | |
143 | |
|
|
144 | if (op + lit + 1 + 3 >= out_end) |
|
|
145 | return 0; |
|
|
146 | |
142 | do |
147 | do |
143 | len++; |
148 | len++; |
144 | while (len < maxlen && ref[len] == ip[len]); |
149 | while (len < maxlen && ref[len] == ip[len]); |
145 | |
|
|
146 | if (op + lit + 1 + 3 >= out_end) |
|
|
147 | return 0; |
|
|
148 | |
150 | |
149 | if (lit) |
151 | if (lit) |
150 | { |
152 | { |
151 | *op++ = lit - 1; |
153 | *op++ = lit - 1; |
152 | lit = -lit; |
154 | lit = -lit; |
… | |
… | |
168 | *op++ = len - 7; |
170 | *op++ = len - 7; |
169 | } |
171 | } |
170 | |
172 | |
171 | *op++ = off; |
173 | *op++ = off; |
172 | |
174 | |
173 | #if ULTRA_FAST |
175 | #if ULTRA_FAST || VERY_FAST |
174 | ip += len; |
176 | ip += len; |
|
|
177 | #if VERY_FAST && !ULTRA_FAST |
|
|
178 | --ip; |
|
|
179 | #endif |
175 | hval = FRST (ip); |
180 | hval = FRST (ip); |
|
|
181 | |
176 | hval = NEXT (hval, ip); |
182 | hval = NEXT (hval, ip); |
177 | htab[IDX (hval)] = ip; |
183 | htab[IDX (hval)] = ip; |
178 | ip++; |
184 | ip++; |
|
|
185 | |
|
|
186 | #if VERY_FAST && !ULTRA_FAST |
|
|
187 | hval = NEXT (hval, ip); |
|
|
188 | htab[IDX (hval)] = ip; |
|
|
189 | ip++; |
|
|
190 | #endif |
179 | #else |
191 | #else |
180 | do |
192 | do |
181 | { |
193 | { |
182 | hval = NEXT (hval, ip); |
194 | hval = NEXT (hval, ip); |
183 | htab[IDX (hval)] = ip; |
195 | htab[IDX (hval)] = ip; |