fastlz.c 14 KB

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