fastlz.c 13 KB

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