1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2008 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2000-2009 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, |
… | |
… | |
87 | #define expect_true(expr) expect ((expr) != 0, 1) |
87 | #define expect_true(expr) expect ((expr) != 0, 1) |
88 | |
88 | |
89 | /* |
89 | /* |
90 | * compressed format |
90 | * compressed format |
91 | * |
91 | * |
92 | * 000LLLLL <L+1> ; literal |
92 | * 000LLLLL <L+1> ; literal, L+1=1..33 octets |
93 | * LLLooooo oooooooo ; backref L |
93 | * LLLooooo oooooooo ; backref L+1=1..7 octets, o+1=1..4096 offset |
94 | * 111ooooo LLLLLLLL oooooooo ; backref L+7 |
94 | * 111ooooo LLLLLLLL oooooooo ; backref L+8 octets, o+1=1..4096 offset |
95 | * |
95 | * |
96 | */ |
96 | */ |
97 | |
97 | |
98 | unsigned int |
98 | unsigned int |
99 | lzf_compress (const void *const in_data, unsigned int in_len, |
99 | lzf_compress (const void *const in_data, unsigned int in_len, |
… | |
… | |
168 | /* match found at *ref++ */ |
168 | /* match found at *ref++ */ |
169 | unsigned int len = 2; |
169 | unsigned int len = 2; |
170 | unsigned int maxlen = in_end - ip - len; |
170 | unsigned int maxlen = in_end - ip - len; |
171 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
171 | maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; |
172 | |
172 | |
|
|
173 | if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */ |
|
|
174 | if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */ |
|
|
175 | return 0; |
|
|
176 | |
173 | op [- lit - 1] = lit - 1; /* stop run */ |
177 | op [- lit - 1] = lit - 1; /* stop run */ |
174 | op -= !lit; /* undo run if length is zero */ |
178 | op -= !lit; /* undo run if length is zero */ |
175 | |
|
|
176 | if (expect_false (op + 3 + 1 >= out_end)) |
|
|
177 | return 0; |
|
|
178 | |
179 | |
179 | for (;;) |
180 | for (;;) |
180 | { |
181 | { |
181 | if (expect_true (maxlen > 16)) |
182 | if (expect_true (maxlen > 16)) |
182 | { |
183 | { |
… | |
… | |
220 | *op++ = (off >> 8) + ( 7 << 5); |
221 | *op++ = (off >> 8) + ( 7 << 5); |
221 | *op++ = len - 7; |
222 | *op++ = len - 7; |
222 | } |
223 | } |
223 | |
224 | |
224 | *op++ = off; |
225 | *op++ = off; |
|
|
226 | lit = 0; op++; /* start run */ |
225 | |
227 | |
|
|
228 | ip += len + 1; |
|
|
229 | |
226 | if (expect_true (ip + len < in_end - 2)) |
230 | if (expect_false (ip >= in_end - 2)) |
227 | { |
231 | break; |
|
|
232 | |
228 | #if ULTRA_FAST || VERY_FAST |
233 | #if ULTRA_FAST || VERY_FAST |
229 | ip += len; |
234 | --ip; |
230 | # if VERY_FAST && !ULTRA_FAST |
235 | # if VERY_FAST && !ULTRA_FAST |
231 | --ip; |
236 | --ip; |
232 | # endif |
237 | # endif |
233 | hval = FRST (ip); |
238 | hval = FRST (ip); |
234 | |
239 | |
|
|
240 | hval = NEXT (hval, ip); |
|
|
241 | htab[IDX (hval)] = ip; |
|
|
242 | ip++; |
|
|
243 | |
|
|
244 | # if VERY_FAST && !ULTRA_FAST |
|
|
245 | hval = NEXT (hval, ip); |
|
|
246 | htab[IDX (hval)] = ip; |
|
|
247 | ip++; |
|
|
248 | # endif |
|
|
249 | #else |
|
|
250 | ip -= len + 1; |
|
|
251 | |
|
|
252 | do |
|
|
253 | { |
235 | hval = NEXT (hval, ip); |
254 | hval = NEXT (hval, ip); |
236 | htab[IDX (hval)] = ip; |
255 | htab[IDX (hval)] = ip; |
237 | ip++; |
256 | ip++; |
238 | |
|
|
239 | # if VERY_FAST && !ULTRA_FAST |
|
|
240 | hval = NEXT (hval, ip); |
|
|
241 | htab[IDX (hval)] = ip; |
|
|
242 | ip++; |
|
|
243 | # endif |
|
|
244 | #else |
|
|
245 | do |
|
|
246 | { |
|
|
247 | hval = NEXT (hval, ip); |
|
|
248 | htab[IDX (hval)] = ip; |
|
|
249 | ip++; |
|
|
250 | } |
257 | } |
251 | while (len--); |
258 | while (len--); |
252 | #endif |
259 | #endif |
253 | } |
|
|
254 | |
|
|
255 | lit = 0; op++; /* start run */ |
|
|
256 | } |
260 | } |
257 | else |
261 | else |
258 | { |
262 | { |
259 | /* one more literal byte we must copy */ |
263 | /* one more literal byte we must copy */ |
260 | if (expect_false (op >= out_end)) |
264 | if (expect_false (op >= out_end)) |