fastlz.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. FastLZ - lightning-fast lossless compression library
  3. Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
  4. Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
  5. Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. */
  22. #if !defined(FASTLZ__COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR)
  23. /*
  24. * Always check for bound when decompressing.
  25. * Generally it is best to leave it defined.
  26. */
  27. #define FASTLZ_SAFE
  28. /*
  29. * Give hints to the compiler for branch prediction optimization.
  30. */
  31. #if defined(__GNUC__) && (__GNUC__ > 2)
  32. #define FASTLZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
  33. #define FASTLZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
  34. #else
  35. #define FASTLZ_EXPECT_CONDITIONAL(c) (c)
  36. #define FASTLZ_UNEXPECT_CONDITIONAL(c) (c)
  37. #endif
  38. /*
  39. * Use inlined functions for supported systems.
  40. */
  41. #if defined(__GNUC__) || defined(__DMC__) || defined(__POCC__) || defined(__WATCOMC__) || defined(__SUNPRO_C)
  42. #define FASTLZ_INLINE inline
  43. #elif defined(__BORLANDC__) || defined(_MSC_VER) || defined(__LCC__)
  44. #define FASTLZ_INLINE __inline
  45. #else
  46. #define FASTLZ_INLINE
  47. #endif
  48. /*
  49. * Prevent accessing more than 8-bit at once, except on x86 architectures.
  50. */
  51. #if !defined(FASTLZ_STRICT_ALIGN)
  52. #define FASTLZ_STRICT_ALIGN
  53. #if defined(__i386__) || defined(__386) /* GNU C, Sun Studio */
  54. #undef FASTLZ_STRICT_ALIGN
  55. #elif defined(__i486__) || defined(__i586__) || defined(__i686__) /* GNU C */
  56. #undef FASTLZ_STRICT_ALIGN
  57. #elif defined(_M_IX86) /* Intel, MSVC */
  58. #undef FASTLZ_STRICT_ALIGN
  59. #elif defined(__386)
  60. #undef FASTLZ_STRICT_ALIGN
  61. #elif defined(_X86_) /* MinGW */
  62. #undef FASTLZ_STRICT_ALIGN
  63. #elif defined(__I86__) /* Digital Mars */
  64. #undef FASTLZ_STRICT_ALIGN
  65. #endif
  66. #endif
  67. /*
  68. * FIXME: use preprocessor magic to set this on different platforms!
  69. */
  70. typedef unsigned char flzuint8;
  71. typedef unsigned short flzuint16;
  72. typedef unsigned int flzuint32;
  73. /* prototypes */
  74. int fastlz_compress(const void* input, int length, void* output);
  75. int fastlz_compress_level(int level, const void* input, int length, void* output);
  76. int fastlz_decompress(const void* input, int length, void* output, int maxout);
  77. #define MAX_COPY 32
  78. #define MAX_LEN 264 /* 256 + 8 */
  79. #define MAX_DISTANCE 8192
  80. #if !defined(FASTLZ_STRICT_ALIGN)
  81. #define FASTLZ_READU16(p) *((const flzuint16*)(p))
  82. #else
  83. #define FASTLZ_READU16(p) ((p)[0] | (p)[1]<<8)
  84. #endif
  85. #define HASH_LOG 13
  86. #define HASH_SIZE (1<< HASH_LOG)
  87. #define HASH_MASK (HASH_SIZE-1)
  88. #define HASH_FUNCTION(v,p) { v = FASTLZ_READU16(p); v ^= FASTLZ_READU16(p+1)^(v>>(16-HASH_LOG));v &= HASH_MASK; }
  89. #undef FASTLZ_LEVEL
  90. #define FASTLZ_LEVEL 1
  91. #undef FASTLZ_COMPRESSOR
  92. #undef FASTLZ_DECOMPRESSOR
  93. #define FASTLZ_COMPRESSOR fastlz1_compress
  94. #define FASTLZ_DECOMPRESSOR fastlz1_decompress
  95. static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
  96. static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
  97. #include "fastlz.c"
  98. #undef FASTLZ_LEVEL
  99. #define FASTLZ_LEVEL 2
  100. #undef MAX_DISTANCE
  101. #define MAX_DISTANCE 8191
  102. #define MAX_FARDISTANCE (65535+MAX_DISTANCE-1)
  103. #undef FASTLZ_COMPRESSOR
  104. #undef FASTLZ_DECOMPRESSOR
  105. #define FASTLZ_COMPRESSOR fastlz2_compress
  106. #define FASTLZ_DECOMPRESSOR fastlz2_decompress
  107. static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output);
  108. static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout);
  109. #include "fastlz.c"
  110. int fastlz_compress(const void* input, int length, void* output)
  111. {
  112. /* for short block, choose fastlz1 */
  113. if(length < 65536)
  114. return fastlz1_compress(input, length, output);
  115. /* else... */
  116. return fastlz2_compress(input, length, output);
  117. }
  118. int fastlz_decompress(const void* input, int length, void* output, int maxout)
  119. {
  120. /* magic identifier for compression level */
  121. int level = ((*(const flzuint8*)input) >> 5) + 1;
  122. if(level == 1)
  123. return fastlz1_decompress(input, length, output, maxout);
  124. if(level == 2)
  125. return fastlz2_decompress(input, length, output, maxout);
  126. /* unknown level, trigger error */
  127. return 0;
  128. }
  129. int fastlz_compress_level(int level, const void* input, int length, void* output)
  130. {
  131. if(level == 1)
  132. return fastlz1_compress(input, length, output);
  133. if(level == 2)
  134. return fastlz2_compress(input, length, output);
  135. return 0;
  136. }
  137. #else /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */
  138. static FASTLZ_INLINE int FASTLZ_COMPRESSOR(const void* input, int length, void* output)
  139. {
  140. const flzuint8* ip = (const flzuint8*) input;
  141. const flzuint8* ip_bound = ip + length - 2;
  142. const flzuint8* ip_limit = ip + length - 12;
  143. flzuint8* op = (flzuint8*) output;
  144. const flzuint8* htab[HASH_SIZE];
  145. const flzuint8** hslot;
  146. flzuint32 hval;
  147. flzuint32 copy;
  148. /* sanity check */
  149. if(FASTLZ_UNEXPECT_CONDITIONAL(length < 4))
  150. {
  151. if(length)
  152. {
  153. /* create literal copy only */
  154. *op++ = length-1;
  155. ip_bound++;
  156. while(ip <= ip_bound)
  157. *op++ = *ip++;
  158. return length+1;
  159. }
  160. else
  161. return 0;
  162. }
  163. /* initializes hash table */
  164. for (hslot = htab; hslot < htab + HASH_SIZE; hslot++)
  165. *hslot = ip;
  166. /* we start with literal copy */
  167. copy = 2;
  168. *op++ = MAX_COPY-1;
  169. *op++ = *ip++;
  170. *op++ = *ip++;
  171. /* main loop */
  172. while(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
  173. {
  174. const flzuint8* ref;
  175. flzuint32 distance;
  176. /* minimum match length */
  177. flzuint32 len = 3;
  178. /* comparison starting-point */
  179. const flzuint8* anchor = ip;
  180. /* check for a run */
  181. #if FASTLZ_LEVEL==2
  182. if(ip[0] == ip[-1] && FASTLZ_READU16(ip-1)==FASTLZ_READU16(ip+1))
  183. {
  184. distance = 1;
  185. ip += 3;
  186. ref = anchor - 1 + 3;
  187. goto match;
  188. }
  189. #endif
  190. /* find potential match */
  191. HASH_FUNCTION(hval,ip);
  192. hslot = htab + hval;
  193. ref = htab[hval];
  194. /* calculate distance to the match */
  195. distance = (int)(anchor - ref);
  196. /* update hash table */
  197. *hslot = anchor;
  198. /* is this a match? check the first 3 bytes */
  199. if(distance==0 ||
  200. #if FASTLZ_LEVEL==1
  201. (distance >= MAX_DISTANCE) ||
  202. #else
  203. (distance >= MAX_FARDISTANCE) ||
  204. #endif
  205. *ref++ != *ip++ || *ref++!=*ip++ || *ref++!=*ip++)
  206. goto literal;
  207. #if FASTLZ_LEVEL==2
  208. /* far, needs at least 5-byte match */
  209. if(distance >= MAX_DISTANCE)
  210. {
  211. if(*ip++ != *ref++ || *ip++!= *ref++)
  212. goto literal;
  213. len += 2;
  214. }
  215. match:
  216. #endif
  217. /* last matched byte */
  218. ip = anchor + len;
  219. /* distance is biased */
  220. distance--;
  221. if(!distance)
  222. {
  223. /* zero distance means a run */
  224. flzuint8 x = ip[-1];
  225. while(ip < ip_bound)
  226. if(*ref++ != x) break; else ip++;
  227. }
  228. else
  229. for(;;)
  230. {
  231. /* safe because the outer check against ip limit */
  232. if(*ref++ != *ip++) break;
  233. if(*ref++ != *ip++) break;
  234. if(*ref++ != *ip++) break;
  235. if(*ref++ != *ip++) break;
  236. if(*ref++ != *ip++) break;
  237. if(*ref++ != *ip++) break;
  238. if(*ref++ != *ip++) break;
  239. if(*ref++ != *ip++) break;
  240. while(ip < ip_bound)
  241. if(*ref++ != *ip++) break;
  242. break;
  243. }
  244. /* if we have copied something, adjust the copy count */
  245. if(copy)
  246. /* copy is biased, '0' means 1 byte copy */
  247. *(op-copy-1) = copy-1;
  248. else
  249. /* back, to overwrite the copy count */
  250. op--;
  251. /* reset literal counter */
  252. copy = 0;
  253. /* length is biased, '1' means a match of 3 bytes */
  254. ip -= 3;
  255. len = (int)(ip - anchor);
  256. /* encode the match */
  257. #if FASTLZ_LEVEL==2
  258. if(distance < MAX_DISTANCE)
  259. {
  260. if(len < 7)
  261. {
  262. *op++ = (len << 5) + (distance >> 8);
  263. *op++ = (distance & 255);
  264. }
  265. else
  266. {
  267. *op++ = (7 << 5) + (distance >> 8);
  268. for(len-=7; len >= 255; len-= 255)
  269. *op++ = 255;
  270. *op++ = len;
  271. *op++ = (distance & 255);
  272. }
  273. }
  274. else
  275. {
  276. /* far away, but not yet in the another galaxy... */
  277. if(len < 7)
  278. {
  279. distance -= MAX_DISTANCE;
  280. *op++ = (len << 5) + 31;
  281. *op++ = 255;
  282. *op++ = distance >> 8;
  283. *op++ = distance & 255;
  284. }
  285. else
  286. {
  287. distance -= MAX_DISTANCE;
  288. *op++ = (7 << 5) + 31;
  289. for(len-=7; len >= 255; len-= 255)
  290. *op++ = 255;
  291. *op++ = len;
  292. *op++ = 255;
  293. *op++ = distance >> 8;
  294. *op++ = distance & 255;
  295. }
  296. }
  297. #else
  298. if(FASTLZ_UNEXPECT_CONDITIONAL(len > MAX_LEN-2))
  299. while(len > MAX_LEN-2)
  300. {
  301. *op++ = (7 << 5) + (distance >> 8);
  302. *op++ = MAX_LEN - 2 - 7 -2;
  303. *op++ = (distance & 255);
  304. len -= MAX_LEN-2;
  305. }
  306. if(len < 7)
  307. {
  308. *op++ = (len << 5) + (distance >> 8);
  309. *op++ = (distance & 255);
  310. }
  311. else
  312. {
  313. *op++ = (7 << 5) + (distance >> 8);
  314. *op++ = len - 7;
  315. *op++ = (distance & 255);
  316. }
  317. #endif
  318. /* update the hash at match boundary */
  319. HASH_FUNCTION(hval,ip);
  320. htab[hval] = ip++;
  321. HASH_FUNCTION(hval,ip);
  322. htab[hval] = ip++;
  323. /* assuming literal copy */
  324. *op++ = MAX_COPY-1;
  325. continue;
  326. literal:
  327. *op++ = *anchor++;
  328. ip = anchor;
  329. copy++;
  330. if(FASTLZ_UNEXPECT_CONDITIONAL(copy == MAX_COPY))
  331. {
  332. copy = 0;
  333. *op++ = MAX_COPY-1;
  334. }
  335. }
  336. /* left-over as literal copy */
  337. ip_bound++;
  338. while(ip <= ip_bound)
  339. {
  340. *op++ = *ip++;
  341. copy++;
  342. if(copy == MAX_COPY)
  343. {
  344. copy = 0;
  345. *op++ = MAX_COPY-1;
  346. }
  347. }
  348. /* if we have copied something, adjust the copy length */
  349. if(copy)
  350. *(op-copy-1) = copy-1;
  351. else
  352. op--;
  353. #if FASTLZ_LEVEL==2
  354. /* marker for fastlz2 */
  355. *(flzuint8*)output |= (1 << 5);
  356. #endif
  357. return (int)(op - (flzuint8*)output);
  358. }
  359. static FASTLZ_INLINE int FASTLZ_DECOMPRESSOR(const void* input, int length, void* output, int maxout)
  360. {
  361. const flzuint8* ip = (const flzuint8*) input;
  362. const flzuint8* ip_limit = ip + length;
  363. flzuint8* op = (flzuint8*) output;
  364. flzuint8* op_limit = op + maxout;
  365. flzuint32 ctrl = (*ip++) & 31;
  366. int loop = 1;
  367. do
  368. {
  369. const flzuint8* ref = op;
  370. flzuint32 len = ctrl >> 5;
  371. flzuint32 ofs = (ctrl & 31) << 8;
  372. if(ctrl >= 32)
  373. {
  374. #if FASTLZ_LEVEL==2
  375. flzuint8 code;
  376. #endif
  377. len--;
  378. ref -= ofs;
  379. if (len == 7-1)
  380. #if FASTLZ_LEVEL==1
  381. len += *ip++;
  382. ref -= *ip++;
  383. #else
  384. do
  385. {
  386. code = *ip++;
  387. len += code;
  388. } while (code==255);
  389. code = *ip++;
  390. ref -= code;
  391. /* match from 16-bit distance */
  392. if(FASTLZ_UNEXPECT_CONDITIONAL(code==255))
  393. if(FASTLZ_EXPECT_CONDITIONAL(ofs==(31 << 8)))
  394. {
  395. ofs = (*ip++) << 8;
  396. ofs += *ip++;
  397. ref = op - ofs - MAX_DISTANCE;
  398. }
  399. #endif
  400. #ifdef FASTLZ_SAFE
  401. if (FASTLZ_UNEXPECT_CONDITIONAL(op + len + 3 > op_limit))
  402. return 0;
  403. if (FASTLZ_UNEXPECT_CONDITIONAL(ref-1 < (flzuint8 *)output))
  404. return 0;
  405. #endif
  406. if(FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit))
  407. ctrl = *ip++;
  408. else
  409. loop = 0;
  410. if(ref == op)
  411. {
  412. /* optimize copy for a run */
  413. flzuint8 b = ref[-1];
  414. *op++ = b;
  415. *op++ = b;
  416. *op++ = b;
  417. for(; len; --len)
  418. *op++ = b;
  419. }
  420. else
  421. {
  422. #if !defined(FASTLZ_STRICT_ALIGN)
  423. const flzuint16* p;
  424. flzuint16* q;
  425. #endif
  426. /* copy from reference */
  427. ref--;
  428. *op++ = *ref++;
  429. *op++ = *ref++;
  430. *op++ = *ref++;
  431. #if !defined(FASTLZ_STRICT_ALIGN)
  432. /* copy a byte, so that now it's word aligned */
  433. if(len & 1)
  434. {
  435. *op++ = *ref++;
  436. len--;
  437. }
  438. /* copy 16-bit at once */
  439. q = (flzuint16*) op;
  440. op += len;
  441. p = (const flzuint16*) ref;
  442. for(len>>=1; len > 4; len-=4)
  443. {
  444. *q++ = *p++;
  445. *q++ = *p++;
  446. *q++ = *p++;
  447. *q++ = *p++;
  448. }
  449. for(; len; --len)
  450. *q++ = *p++;
  451. #else
  452. for(; len; --len)
  453. *op++ = *ref++;
  454. #endif
  455. }
  456. }
  457. else
  458. {
  459. ctrl++;
  460. #ifdef FASTLZ_SAFE
  461. if (FASTLZ_UNEXPECT_CONDITIONAL(op + ctrl > op_limit))
  462. return 0;
  463. if (FASTLZ_UNEXPECT_CONDITIONAL(ip + ctrl > ip_limit))
  464. return 0;
  465. #endif
  466. *op++ = *ip++;
  467. for(--ctrl; ctrl; ctrl--)
  468. *op++ = *ip++;
  469. loop = (int)FASTLZ_EXPECT_CONDITIONAL(ip < ip_limit);
  470. if(loop)
  471. ctrl = *ip++;
  472. }
  473. }
  474. while(FASTLZ_EXPECT_CONDITIONAL(loop));
  475. return (int)(op - (flzuint8*)output);
  476. }
  477. #endif /* !defined(FASTLZ_COMPRESSOR) && !defined(FASTLZ_DECOMPRESSOR) */