1 | /* |
1 | /* |
2 | * Copyright (c) 2000-2007 Marc Alexander Lehmann <schmorp@schmorp.de> |
2 | * Copyright (c) 2000-2008 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, |
… | |
… | |
111 | u8 *op = (u8 *)out_data; |
111 | u8 *op = (u8 *)out_data; |
112 | const u8 *in_end = ip + in_len; |
112 | const u8 *in_end = ip + in_len; |
113 | u8 *out_end = op + out_len; |
113 | u8 *out_end = op + out_len; |
114 | const u8 *ref; |
114 | const u8 *ref; |
115 | |
115 | |
|
|
116 | /* off requires a type wide enough to hold a general pointer difference. |
|
|
117 | * ISO C doesn't have that (size_t might not be enough and ptrdiff_t only |
|
|
118 | * works for differences within a single object). We also assume that no |
|
|
119 | * no bit pattern traps. Since the only platform that is both non-POSIX |
|
|
120 | * and fails to support both assumptions is windows 64 bit, we make a |
|
|
121 | * special workaround for it. |
|
|
122 | */ |
|
|
123 | #if defined (WIN32) && defined (_M_X64) |
|
|
124 | unsigned _int64 off; /* workaround for missing POSIX compliance */ |
|
|
125 | #else |
|
|
126 | unsigned long off; |
|
|
127 | #endif |
116 | unsigned int hval; |
128 | unsigned int hval; |
117 | unsigned long off; |
|
|
118 | int lit; |
129 | int lit; |
119 | |
130 | |
120 | if (!in_len || !out_len) |
131 | if (!in_len || !out_len) |
121 | return 0; |
132 | return 0; |
122 | |
133 | |
… | |
… | |
136 | hval = NEXT (hval, ip); |
147 | hval = NEXT (hval, ip); |
137 | hslot = htab + IDX (hval); |
148 | hslot = htab + IDX (hval); |
138 | ref = *hslot; *hslot = ip; |
149 | ref = *hslot; *hslot = ip; |
139 | |
150 | |
140 | if (1 |
151 | if (1 |
141 | #if INIT_HTAB && !USE_MEMCPY |
152 | #if INIT_HTAB |
142 | && 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 */ |
143 | #endif |
154 | #endif |
144 | && (off = ip - ref - 1) < MAX_OFF |
155 | && (off = ip - ref - 1) < MAX_OFF |
145 | && ip + 4 < in_end |
156 | && ip + 4 < in_end |
146 | && ref > (u8 *)in_data |
157 | && ref > (u8 *)in_data |
… | |
… | |
195 | while (len < maxlen && ref[len] == ip[len]); |
206 | while (len < maxlen && ref[len] == ip[len]); |
196 | |
207 | |
197 | break; |
208 | break; |
198 | } |
209 | } |
199 | |
210 | |
200 | len -= 2; |
211 | len -= 2; /* len is now #octets - 1 */ |
201 | ip++; |
212 | ip++; |
202 | |
213 | |
203 | if (len < 7) |
214 | if (len < 7) |
204 | { |
215 | { |
205 | *op++ = (off >> 8) + (len << 5); |
216 | *op++ = (off >> 8) + (len << 5); |
… | |
… | |
209 | *op++ = (off >> 8) + ( 7 << 5); |
220 | *op++ = (off >> 8) + ( 7 << 5); |
210 | *op++ = len - 7; |
221 | *op++ = len - 7; |
211 | } |
222 | } |
212 | |
223 | |
213 | *op++ = off; |
224 | *op++ = off; |
|
|
225 | lit = 0; op++; /* start run */ |
|
|
226 | |
|
|
227 | ip += len + 1; |
|
|
228 | |
|
|
229 | if (expect_false (ip >= in_end - 2)) |
|
|
230 | break; |
214 | |
231 | |
215 | #if ULTRA_FAST || VERY_FAST |
232 | #if ULTRA_FAST || VERY_FAST |
216 | ip += len; |
|
|
217 | #if VERY_FAST && !ULTRA_FAST |
|
|
218 | --ip; |
233 | --ip; |
|
|
234 | # if VERY_FAST && !ULTRA_FAST |
|
|
235 | --ip; |
219 | #endif |
236 | # endif |
220 | hval = FRST (ip); |
237 | hval = FRST (ip); |
221 | |
238 | |
222 | hval = NEXT (hval, ip); |
239 | hval = NEXT (hval, ip); |
223 | htab[IDX (hval)] = ip; |
240 | htab[IDX (hval)] = ip; |
224 | ip++; |
241 | ip++; |
225 | |
242 | |
226 | #if VERY_FAST && !ULTRA_FAST |
243 | # if VERY_FAST && !ULTRA_FAST |
227 | hval = NEXT (hval, ip); |
244 | hval = NEXT (hval, ip); |
228 | htab[IDX (hval)] = ip; |
245 | htab[IDX (hval)] = ip; |
229 | ip++; |
246 | ip++; |
230 | #endif |
247 | # endif |
231 | #else |
248 | #else |
|
|
249 | ip -= len + 1; |
|
|
250 | |
232 | do |
251 | do |
233 | { |
252 | { |
234 | hval = NEXT (hval, ip); |
253 | hval = NEXT (hval, ip); |
235 | htab[IDX (hval)] = ip; |
254 | htab[IDX (hval)] = ip; |
236 | ip++; |
255 | ip++; |
237 | } |
256 | } |
238 | while (len--); |
257 | while (len--); |
239 | #endif |
258 | #endif |
240 | |
|
|
241 | lit = 0; op++; /* start run */ |
|
|
242 | } |
259 | } |
243 | else |
260 | else |
244 | { |
261 | { |
245 | /* one more literal byte we must copy */ |
262 | /* one more literal byte we must copy */ |
246 | if (expect_false (op >= out_end)) |
263 | if (expect_false (op >= out_end)) |
… | |
… | |
254 | lit = 0; op++; /* start run */ |
271 | lit = 0; op++; /* start run */ |
255 | } |
272 | } |
256 | } |
273 | } |
257 | } |
274 | } |
258 | |
275 | |
259 | if (op + 2 > out_end) /* at most 2 bytes can be missing here */ |
276 | if (op + 3 > out_end) /* at most 3 bytes can be missing here */ |
260 | return 0; |
277 | return 0; |
261 | |
278 | |
262 | while (ip < in_end) |
279 | while (ip < in_end) |
263 | { |
280 | { |
264 | lit++; *op++ = *ip++; |
281 | lit++; *op++ = *ip++; |
|
|
282 | |
|
|
283 | if (expect_false (lit == MAX_LIT)) |
|
|
284 | { |
|
|
285 | op [- lit - 1] = lit - 1; /* stop run */ |
|
|
286 | lit = 0; op++; /* start run */ |
|
|
287 | } |
265 | } |
288 | } |
266 | |
289 | |
267 | op [- lit - 1] = lit - 1; /* end run */ |
290 | op [- lit - 1] = lit - 1; /* end run */ |
268 | op -= !lit; /* undo run if length is zero */ |
291 | op -= !lit; /* undo run if length is zero */ |
269 | |
292 | |