ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/liblzf/lzf_c.c
(Generate patch)

Comparing liblzf/lzf_c.c (file contents):
Revision 1.32 by root, Fri May 9 12:42:50 2008 UTC vs.
Revision 1.41 by root, Tue Jun 1 02:30:06 2010 UTC

1/* 1/*
2 * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de> 2 * Copyright (c) 2000-2010 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,
38 38
39#define HSIZE (1 << (HLOG)) 39#define HSIZE (1 << (HLOG))
40 40
41/* 41/*
42 * don't play with this unless you benchmark! 42 * don't play with this unless you benchmark!
43 * decompression is not dependent on the hash function 43 * the data format is not dependent on the hash function.
44 * the hashing function might seem strange, just believe me 44 * the hash function might seem strange, just believe me,
45 * it works ;) 45 * it works ;)
46 */ 46 */
47#ifndef FRST 47#ifndef FRST
48# define FRST(p) (((p[0]) << 8) | p[1]) 48# define FRST(p) (((p[0]) << 8) | p[1])
49# define NEXT(v,p) (((v) << 8) | p[2]) 49# define NEXT(v,p) (((v) << 8) | p[2])
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
98unsigned int 98unsigned int
99lzf_compress (const void *const in_data, unsigned int in_len, 99lzf_compress (const void *const in_data, unsigned int in_len,
153 && ref < ip /* the next test will actually take care of this, but this is faster */ 153 && ref < ip /* the next test will actually take care of this, but this is faster */
154#endif 154#endif
155 && (off = ip - ref - 1) < MAX_OFF 155 && (off = ip - ref - 1) < MAX_OFF
156 && ip + 4 < in_end 156 && ip + 4 < in_end
157 && ref > (u8 *)in_data 157 && ref > (u8 *)in_data
158 && ref[2] == ip[2]
158#if STRICT_ALIGN 159#if STRICT_ALIGN
159 && ref[0] == ip[0] 160 && (ref[1] << 8) | ref[0] == (ip[1] << 8) | ip[0]
160 && ref[1] == ip[1]
161 && ref[2] == ip[2]
162#else 161#else
163 && *(u16 *)ref == *(u16 *)ip 162 && *(u16 *)ref == *(u16 *)ip
164 && ref[2] == ip[2]
165#endif 163#endif
166 ) 164 )
167 { 165 {
168 /* match found at *ref++ */ 166 /* match found at *ref++ */
169 unsigned int len = 2; 167 unsigned int len = 2;
170 unsigned int maxlen = in_end - ip - len; 168 unsigned int maxlen = in_end - ip - len;
171 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen; 169 maxlen = maxlen > MAX_REF ? MAX_REF : maxlen;
172 170
171 if (expect_false (op + 3 + 1 >= out_end)) /* first a faster conservative test */
172 if (op - !lit + 3 + 1 >= out_end) /* second the exact but rare test */
173 return 0;
174
173 op [- lit - 1] = lit - 1; /* stop run */ 175 op [- lit - 1] = lit - 1; /* stop run */
174 op -= !lit; /* undo run if length is zero */ 176 op -= !lit; /* undo run if length is zero */
175
176 if (expect_false (op + 3 + 1 >= out_end))
177 return 0;
178 177
179 for (;;) 178 for (;;)
180 { 179 {
181 if (expect_true (maxlen > 16)) 180 if (expect_true (maxlen > 16))
182 { 181 {
206 while (len < maxlen && ref[len] == ip[len]); 205 while (len < maxlen && ref[len] == ip[len]);
207 206
208 break; 207 break;
209 } 208 }
210 209
211 len -= 2; 210 len -= 2; /* len is now #octets - 1 */
212 ip++; 211 ip++;
213 212
214 if (len < 7) 213 if (len < 7)
215 { 214 {
216 *op++ = (off >> 8) + (len << 5); 215 *op++ = (off >> 8) + (len << 5);
220 *op++ = (off >> 8) + ( 7 << 5); 219 *op++ = (off >> 8) + ( 7 << 5);
221 *op++ = len - 7; 220 *op++ = len - 7;
222 } 221 }
223 222
224 *op++ = off; 223 *op++ = off;
224 lit = 0; op++; /* start run */
225
226 ip += len + 1;
227
228 if (expect_false (ip >= in_end - 2))
229 break;
225 230
226#if ULTRA_FAST || VERY_FAST 231#if ULTRA_FAST || VERY_FAST
227 ip += len;
228#if VERY_FAST && !ULTRA_FAST
229 --ip; 232 --ip;
233# if VERY_FAST && !ULTRA_FAST
234 --ip;
230#endif 235# endif
231 hval = FRST (ip); 236 hval = FRST (ip);
232 237
233 hval = NEXT (hval, ip); 238 hval = NEXT (hval, ip);
234 htab[IDX (hval)] = ip; 239 htab[IDX (hval)] = ip;
235 ip++; 240 ip++;
236 241
237#if VERY_FAST && !ULTRA_FAST 242# if VERY_FAST && !ULTRA_FAST
238 hval = NEXT (hval, ip); 243 hval = NEXT (hval, ip);
239 htab[IDX (hval)] = ip; 244 htab[IDX (hval)] = ip;
240 ip++; 245 ip++;
241#endif 246# endif
242#else 247#else
248 ip -= len + 1;
249
243 do 250 do
244 { 251 {
245 hval = NEXT (hval, ip); 252 hval = NEXT (hval, ip);
246 htab[IDX (hval)] = ip; 253 htab[IDX (hval)] = ip;
247 ip++; 254 ip++;
248 } 255 }
249 while (len--); 256 while (len--);
250#endif 257#endif
251
252 lit = 0; op++; /* start run */
253 } 258 }
254 else 259 else
255 { 260 {
256 /* one more literal byte we must copy */ 261 /* one more literal byte we must copy */
257 if (expect_false (op >= out_end)) 262 if (expect_false (op >= out_end))

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines