| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
#define FASTLZ_SAFE |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#if defined(__GNUC__) && (__GNUC__ > 2) |
| 39 |
#define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1)) |
| 40 |
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0)) |
| 41 |
#else |
| 42 |
#define FASTLZ_EXPECT_CONDITIONAL(c) (c) |
| 43 |
#define FASTLZ_UNEXPECT_CONDITIONAL(c) (c) |
| 44 |
#endif |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
#if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C) |
| 50 |
#define FASTLZ_INLINE inline |
| 51 |
#elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__) |
| 52 |
#define FASTLZ_INLINE __inline |
| 53 |
#else |
| 54 |
#define FASTLZ_INLINE |
| 55 |
#endif |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#if !defined(FASTLZ_STRICT_ALIGN) |
| 61 |
#define FASTLZ_STRICT_ALIGN |
| 62 |
#if defined(__i386__) || defined(__386) |
| 63 |
#undef FASTLZ_STRICT_ALIGN |
| 64 |
#elif defined(__i486__) || defined(__i586__) || defined(__i686__) |
| 65 |
#undef FASTLZ_STRICT_ALIGN |
| 66 |
#elif defined(_M_IX86) |
| 67 |
#undef FASTLZ_STRICT_ALIGN |
| 68 |
#elif defined(__386) |
| 69 |
#undef FASTLZ_STRICT_ALIGN |
| 70 |
#elif defined(_X86_) |
| 71 |
#undef FASTLZ_STRICT_ALIGN |
| 72 |
#elif defined(__I86__) |
| 73 |
#undef FASTLZ_STRICT_ALIGN |
| 74 |
#endif |
| 75 |
#endif |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
typedef unsigned char flzuint8; |
| 81 |
typedef unsigned short flzuint16; |
| 82 |
typedef unsigned int flzuint32; |
| 83 |
|
| 84 |
|
| 85 |
#if defined(_MSC_VER) |
| 86 |
#pragma warning(disable: 4244) |
| 87 |
#endif |
| 88 |
|
| 89 |
|
| 90 |
int fastlz_compress(const void* input, int length, void* output); |
| 91 |
int fastlz_compress_level(int level, const void* input, int length, void* output); |
| 92 |
int fastlz_decompress(const void* input, int length, void* output, int maxout); |
| 93 |
|
| 94 |
#define MAX_COPY 32 |
| 95 |
#define MAX_LEN 264 |
| 96 |
#define MAX_DISTANCE 8192 |
| 97 |
|
| 98 |
#if !defined(FASTLZ_STRICT_ALIGN) |
| 99 |
#define FASTLZ_READU16(p) *((const flzuint16*)(p)) |
| 100 |
#else |
| 101 |
#define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8) |
| 102 |
#endif |
| 103 |
|
| 104 |
#define HASH_LOG 13 |
| 105 |
#define HASH_SIZE (1<< HASH_LOG) |
| 106 |
#define HASH_MASK (HASH_SIZE-1) |
| 107 |
#define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; } |
| 108 |
|
| 109 |
#undef FASTLZ_LEVEL |
| 110 |
#define FASTLZ_LEVEL 1 |
| 111 |
|
| 112 |
#undef FASTLZ_COMPRESSOR |
| 113 |
#undef FASTLZ_DECOMPRESSOR |
| 114 |
#define FASTLZ_COMPRESSOR fastlz1_compress |
| 115 |
#define FASTLZ_DECOMPRESSOR fastlz1_decompress |
| 116 |
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output); |
| 117 |
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout); |
| 118 |
#include "fastlz.c" |
| 119 |
|
| 120 |
#undef FASTLZ_LEVEL |
| 121 |
#define FASTLZ_LEVEL 2 |
| 122 |
|
| 123 |
#undef MAX_DISTANCE |
| 124 |
#define MAX_DISTANCE 8191 |
| 125 |
#define MAX_FARDISTANCE (65535+MAX_DISTANCE-1) |
| 126 |
|
| 127 |
#undef FASTLZ_COMPRESSOR |
| 128 |
#undef FASTLZ_DECOMPRESSOR |
| 129 |
#define FASTLZ_COMPRESSOR fastlz2_compress |
| 130 |
#define FASTLZ_DECOMPRESSOR fastlz2_decompress |
| 131 |
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output); |
| 132 |
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout); |
| 133 |
#include "fastlz.c" |
| 134 |
|
| 135 |
int fastlz_compress(const void* input, int length, void* output) |
| 136 |
{ |
| 137 |
|
| 138 |
if(length < 65536) |
| 139 |
return fastlz1_compress(input, length, output); |
| 140 |
|
| 141 |
|
| 142 |
return fastlz2_compress(input, length, output); |
| 143 |
} |
| 144 |
|
| 145 |
int fastlz_decompress(const void* input, int length, void* output, int maxout) |
| 146 |
{ |
| 147 |
|
| 148 |
int level = ((*(const flzuint8*)input) >> 5) + 1; |
| 149 |
|
| 150 |
if(level == 1) |
| 151 |
return fastlz1_decompress(input, length, output, maxout); |
| 152 |
if(level == 2) |
| 153 |
return fastlz2_decompress(input, length, output, maxout); |
| 154 |
|
| 155 |
|
| 156 |
return 0; |
| 157 |
} |
| 158 |
|
| 159 |
int fastlz_compress_level(int level, const void* input, int length, void* output) |
| 160 |
{ |
| 161 |
if(level == 1) |
| 162 |
return fastlz1_compress(input, length, output); |
| 163 |
if(level == 2) |
| 164 |
return fastlz2_compress(input, length, output); |
| 165 |
|
| 166 |
return 0; |
| 167 |
} |
| 168 |
|
| 169 |
#else |
| 170 |
|
| 171 |
static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output) |
| 172 |
{ |
| 173 |
const flzuint8* ip = (const flzuint8*) input; |
| 174 |
const flzuint8* ip_bound = ip + length - 2; |
| 175 |
const flzuint8* ip_limit = ip + length - 12; |
| 176 |
flzuint8* op = (flzuint8*) output; |
| 177 |
|
| 178 |
const flzuint8* htab[HASH_SIZE]; |
| 179 |
const flzuint8** hslot; |
| 180 |
flzuint32 hval; |
| 181 |
|
| 182 |
flzuint32 copy; |
| 183 |
|
| 184 |
|
| 185 |
if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4)) |
| 186 |
{ |
| 187 |
if(length) |
| 188 |
{ |
| 189 |
|
| 190 |
*op++ = length-1; |
| 191 |
ip_bound++; |
| 192 |
while(ip <= ip_bound) |
| 193 |
*op++ = *ip++; |
| 194 |
return length+1; |
| 195 |
} |
| 196 |
else |
| 197 |
return 0; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
for (hslot = htab; hslot < htab + HASH_SIZE; hslot++) |
| 202 |
*hslot = ip; |
| 203 |
|
| 204 |
|
| 205 |
copy = 2; |
| 206 |
*op++ = MAX_COPY-1; |
| 207 |
*op++ = *ip++; |
| 208 |
*op++ = *ip++; |
| 209 |
|
| 210 |
|
| 211 |
while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) |
| 212 |
{ |
| 213 |
const flzuint8* ref; |
| 214 |
flzuint32 distance; |
| 215 |
|
| 216 |
|
| 217 |
flzuint32 len = 3; |
| 218 |
|
| 219 |
|
| 220 |
const flzuint8* anchor = ip; |
| 221 |
|
| 222 |
|
| 223 |
#if FASTLZ_LEVEL==2 |
| 224 |
if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1)) |
| 225 |
{ |
| 226 |
distance = 1; |
| 227 |
ip += 3; |
| 228 |
ref = anchor - 1 + 3; |
| 229 |
goto match; |
| 230 |
} |
| 231 |
#endif |
| 232 |
|
| 233 |
|
| 234 |
HASH_FUNCTION(hval,ip); |
| 235 |
hslot = htab + hval; |
| 236 |
ref = htab[hval]; |
| 237 |
|
| 238 |
|
| 239 |
distance = anchor - ref; |
| 240 |
|
| 241 |
|
| 242 |
*hslot = anchor; |
| 243 |
|
| 244 |
|
| 245 |
if(distance==0 || |
| 246 |
#if FASTLZ_LEVEL==1 |
| 247 |
(distance >= MAX_DISTANCE) || |
| 248 |
#else |
| 249 |
(distance >= MAX_FARDISTANCE) || |
| 250 |
#endif |
| 251 |
*ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++) |
| 252 |
goto literal; |
| 253 |
|
| 254 |
#if FASTLZ_LEVEL==2 |
| 255 |
|
| 256 |
if(distance >= MAX_DISTANCE) |
| 257 |
{ |
| 258 |
if(*ip++ != *ref++ || *ip++!= *ref++) |
| 259 |
goto literal; |
| 260 |
len += 2; |
| 261 |
} |
| 262 |
|
| 263 |
match: |
| 264 |
#endif |
| 265 |
|
| 266 |
|
| 267 |
ip = anchor + len; |
| 268 |
|
| 269 |
|
| 270 |
distance--; |
| 271 |
|
| 272 |
if(!distance) |
| 273 |
{ |
| 274 |
|
| 275 |
flzuint8 x = ip[-1]; |
| 276 |
while(ip < ip_bound) |
| 277 |
if(*ref++ != x) break; else ip++; |
| 278 |
} |
| 279 |
else |
| 280 |
for(;;) |
| 281 |
{ |
| 282 |
|
| 283 |
if(*ref++ != *ip++) break; |
| 284 |
if(*ref++ != *ip++) break; |
| 285 |
if(*ref++ != *ip++) break; |
| 286 |
if(*ref++ != *ip++) break; |
| 287 |
if(*ref++ != *ip++) break; |
| 288 |
if(*ref++ != *ip++) break; |
| 289 |
if(*ref++ != *ip++) break; |
| 290 |
if(*ref++ != *ip++) break; |
| 291 |
while(ip < ip_bound) |
| 292 |
if(*ref++ != *ip++) break; |
| 293 |
break; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
if(copy) |
| 298 |
|
| 299 |
*(op-copy-1) = copy-1; |
| 300 |
else |
| 301 |
|
| 302 |
op--; |
| 303 |
|
| 304 |
|
| 305 |
copy = 0; |
| 306 |
|
| 307 |
|
| 308 |
ip -= 3; |
| 309 |
len = ip - anchor; |
| 310 |
|
| 311 |
|
| 312 |
#if FASTLZ_LEVEL==2 |
| 313 |
if(distance < MAX_DISTANCE) |
| 314 |
{ |
| 315 |
if(len < 7) |
| 316 |
{ |
| 317 |
*op++ = (len << 5) + (distance >> 8); |
| 318 |
*op++ = (distance & 255); |
| 319 |
} |
| 320 |
else |
| 321 |
{ |
| 322 |
*op++ = (7 << 5) + (distance >> 8); |
| 323 |
for(len-=7; len >= 255; len-= 255) |
| 324 |
*op++ = 255; |
| 325 |
*op++ = len; |
| 326 |
*op++ = (distance & 255); |
| 327 |
} |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
|
| 332 |
if(len < 7) |
| 333 |
{ |
| 334 |
distance -= MAX_DISTANCE; |
| 335 |
*op++ = (len << 5) + 31; |
| 336 |
*op++ = 255; |
| 337 |
*op++ = distance >> 8; |
| 338 |
*op++ = distance & 255; |
| 339 |
} |
| 340 |
else |
| 341 |
{ |
| 342 |
distance -= MAX_DISTANCE; |
| 343 |
*op++ = (7 << 5) + 31; |
| 344 |
for(len-=7; len >= 255; len-= 255) |
| 345 |
*op++ = 255; |
| 346 |
*op++ = len; |
| 347 |
*op++ = 255; |
| 348 |
*op++ = distance >> 8; |
| 349 |
*op++ = distance & 255; |
| 350 |
} |
| 351 |
} |
| 352 |
#else |
| 353 |
|
| 354 |
if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2)) |
| 355 |
while(len > MAX_LEN-2) |
| 356 |
{ |
| 357 |
*op++ = (7 << 5) + (distance >> 8); |
| 358 |
*op++ = MAX_LEN - 2 - 7 -2; |
| 359 |
*op++ = (distance & 255); |
| 360 |
len -= MAX_LEN-2; |
| 361 |
} |
| 362 |
|
| 363 |
if(len < 7) |
| 364 |
{ |
| 365 |
*op++ = (len << 5) + (distance >> 8); |
| 366 |
*op++ = (distance & 255); |
| 367 |
} |
| 368 |
else |
| 369 |
{ |
| 370 |
*op++ = (7 << 5) + (distance >> 8); |
| 371 |
*op++ = len - 7; |
| 372 |
*op++ = (distance & 255); |
| 373 |
} |
| 374 |
#endif |
| 375 |
|
| 376 |
|
| 377 |
HASH_FUNCTION(hval,ip); |
| 378 |
htab[hval] = ip++; |
| 379 |
HASH_FUNCTION(hval,ip); |
| 380 |
htab[hval] = ip++; |
| 381 |
|
| 382 |
|
| 383 |
*op++ = MAX_COPY-1; |
| 384 |
|
| 385 |
continue; |
| 386 |
|
| 387 |
literal: |
| 388 |
*op++ = *anchor++; |
| 389 |
ip = anchor; |
| 390 |
copy++; |
| 391 |
if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY)) |
| 392 |
{ |
| 393 |
copy = 0; |
| 394 |
*op++ = MAX_COPY-1; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
ip_bound++; |
| 400 |
while(ip <= ip_bound) |
| 401 |
{ |
| 402 |
*op++ = *ip++; |
| 403 |
copy++; |
| 404 |
if(copy == MAX_COPY) |
| 405 |
{ |
| 406 |
copy = 0; |
| 407 |
*op++ = MAX_COPY-1; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
if(copy) |
| 413 |
*(op-copy-1) = copy-1; |
| 414 |
else |
| 415 |
op--; |
| 416 |
|
| 417 |
#if FASTLZ_LEVEL==2 |
| 418 |
|
| 419 |
*(flzuint8*)output |= (1 << 5); |
| 420 |
#endif |
| 421 |
|
| 422 |
return op - (flzuint8*)output; |
| 423 |
} |
| 424 |
|
| 425 |
static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout) |
| 426 |
{ |
| 427 |
const flzuint8* ip = (const flzuint8*) input; |
| 428 |
const flzuint8* ip_limit = ip + length; |
| 429 |
flzuint8* op = (flzuint8*) output; |
| 430 |
flzuint8* op_limit = op + maxout; |
| 431 |
flzuint32 ctrl = (*ip++) & 31; |
| 432 |
int loop = 1; |
| 433 |
|
| 434 |
do |
| 435 |
{ |
| 436 |
const flzuint8* ref = op; |
| 437 |
flzuint32 len = ctrl >> 5; |
| 438 |
flzuint32 ofs = (ctrl & 31) << 8; |
| 439 |
|
| 440 |
if(ctrl >= 32) |
| 441 |
{ |
| 442 |
#if FASTLZ_LEVEL==2 |
| 443 |
flzuint8 code; |
| 444 |
#endif |
| 445 |
len--; |
| 446 |
ref -= ofs; |
| 447 |
if (len == 7-1) |
| 448 |
#if FASTLZ_LEVEL==1 |
| 449 |
len += *ip++; |
| 450 |
ref -= *ip++; |
| 451 |
#else |
| 452 |
do |
| 453 |
{ |
| 454 |
code = *ip++; |
| 455 |
len += code; |
| 456 |
} while (code==255); |
| 457 |
code = *ip++; |
| 458 |
ref -= code; |
| 459 |
|
| 460 |
|
| 461 |
if(FASTLZ_UNEXPECT_CONDITIONAL(code==255)) |
| 462 |
if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8))) |
| 463 |
{ |
| 464 |
ofs = (*ip++) << 8; |
| 465 |
ofs += *ip++; |
| 466 |
ref = op - ofs - MAX_DISTANCE; |
| 467 |
} |
| 468 |
#endif |
| 469 |
|
| 470 |
#ifdef FASTLZ_SAFE |
| 471 |
if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit)) |
| 472 |
return 0; |
| 473 |
|
| 474 |
if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output)) |
| 475 |
return 0; |
| 476 |
#endif |
| 477 |
|
| 478 |
if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit)) |
| 479 |
ctrl = *ip++; |
| 480 |
else |
| 481 |
loop = 0; |
| 482 |
|
| 483 |
if(ref == op) |
| 484 |
{ |
| 485 |
|
| 486 |
flzuint8 b = ref[-1]; |
| 487 |
*op++ = b; |
| 488 |
*op++ = b; |
| 489 |
*op++ = b; |
| 490 |
for(; len; --len) |
| 491 |
*op++ = b; |
| 492 |
} |
| 493 |
else |
| 494 |
{ |
| 495 |
#if !defined(FASTLZ_STRICT_ALIGN) |
| 496 |
const flzuint16* p; |
| 497 |
flzuint16* q; |
| 498 |
#endif |
| 499 |
|
| 500 |
ref--; |
| 501 |
*op++ = *ref++; |
| 502 |
*op++ = *ref++; |
| 503 |
*op++ = *ref++; |
| 504 |
|
| 505 |
#if !defined(FASTLZ_STRICT_ALIGN) |
| 506 |
|
| 507 |
if(len & 1) |
| 508 |
{ |
| 509 |
*op++ = *ref++; |
| 510 |
len--; |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
q = (flzuint16*) op; |
| 515 |
op += len; |
| 516 |
p = (const flzuint16*) ref; |
| 517 |
for(len>>=1; len > 4; len-=4) |
| 518 |
{ |
| 519 |
*q++ = *p++; |
| 520 |
*q++ = *p++; |
| 521 |
*q++ = *p++; |
| 522 |
*q++ = *p++; |
| 523 |
} |
| 524 |
for(; len; --len) |
| 525 |
*q++ = *p++; |
| 526 |
#else |
| 527 |
for(; len; --len) |
| 528 |
*op++ = *ref++; |
| 529 |
#endif |
| 530 |
} |
| 531 |
} |
| 532 |
else |
| 533 |
{ |
| 534 |
ctrl++; |
| 535 |
#ifdef FASTLZ_SAFE |
| 536 |
if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit)) |
| 537 |
return 0; |
| 538 |
if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit)) |
| 539 |
return 0; |
| 540 |
#endif |
| 541 |
|
| 542 |
*op++ = *ip++; |
| 543 |
for(--ctrl; ctrl; ctrl--) |
| 544 |
*op++ = *ip++; |
| 545 |
|
| 546 |
loop = FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit); |
| 547 |
if(loop) |
| 548 |
ctrl = *ip++; |
| 549 |
} |
| 550 |
} |
| 551 |
while(FASTLZ_EXPECT_CONDITIONAL(loop)); |
| 552 |
|
| 553 |
return op - (flzuint8*)output; |
| 554 |
} |
| 555 |
|
| 556 |
#endif |