quicklz.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. // Fast data compression library
  2. // Copyright (C) 2006-2011 Lasse Mikkel Reinhold
  3. // lar@quicklz.com
  4. //
  5. // QuickLZ can be used for free under the GPL 1, 2 or 3 license (where anything
  6. // released into public must be open source) or under a commercial license if such
  7. // has been acquired (see http://www.quicklz.com/order.html). The commercial license
  8. // does not cover derived or ported versions created by third parties under GPL.
  9. // 1.5.0 final
  10. #include "quicklz.h"
  11. #if QLZ_VERSION_MAJOR != 1 || QLZ_VERSION_MINOR != 5 || QLZ_VERSION_REVISION != 0
  12. #error quicklz.c and quicklz.h have different versions
  13. #endif
  14. #if (defined(__X86__) || defined(__i386__) || defined(i386) || defined(_M_IX86) || defined(__386__) || defined(__x86_64__) || defined(_M_X64))
  15. #define X86X64
  16. #endif
  17. #define MINOFFSET 2
  18. #define UNCONDITIONAL_MATCHLEN 6
  19. #define UNCOMPRESSED_END 4
  20. #define CWORD_LEN 4
  21. #if QLZ_COMPRESSION_LEVEL == 1 && defined QLZ_PTR_64 && QLZ_STREAMING_BUFFER == 0
  22. #define OFFSET_BASE source
  23. #define CAST (ui32)(size_t)
  24. #else
  25. #define OFFSET_BASE 0
  26. #define CAST
  27. #endif
  28. int qlz_get_setting(int setting)
  29. {
  30. switch (setting)
  31. {
  32. case 0: return QLZ_COMPRESSION_LEVEL;
  33. case 1: return sizeof(qlz_state_compress);
  34. case 2: return sizeof(qlz_state_decompress);
  35. case 3: return QLZ_STREAMING_BUFFER;
  36. #ifdef QLZ_MEMORY_SAFE
  37. case 6: return 1;
  38. #else
  39. case 6: return 0;
  40. #endif
  41. case 7: return QLZ_VERSION_MAJOR;
  42. case 8: return QLZ_VERSION_MINOR;
  43. case 9: return QLZ_VERSION_REVISION;
  44. }
  45. return -1;
  46. }
  47. #if QLZ_COMPRESSION_LEVEL == 1
  48. static int same(const unsigned char *src, size_t n)
  49. {
  50. while(n > 0 && *(src + n) == *src)
  51. n--;
  52. return n == 0 ? 1 : 0;
  53. }
  54. #endif
  55. static void reset_table_compress(qlz_state_compress *state)
  56. {
  57. int i;
  58. for(i = 0; i < QLZ_HASH_VALUES; i++)
  59. {
  60. #if QLZ_COMPRESSION_LEVEL == 1
  61. state->hash[i].offset = 0;
  62. #else
  63. state->hash_counter[i] = 0;
  64. #endif
  65. }
  66. }
  67. static void reset_table_decompress(qlz_state_decompress *state)
  68. {
  69. int i;
  70. (void)state;
  71. (void)i;
  72. #if QLZ_COMPRESSION_LEVEL == 2
  73. for(i = 0; i < QLZ_HASH_VALUES; i++)
  74. {
  75. state->hash_counter[i] = 0;
  76. }
  77. #endif
  78. }
  79. static __inline ui32 hash_func(ui32 i)
  80. {
  81. #if QLZ_COMPRESSION_LEVEL == 2
  82. return ((i >> 9) ^ (i >> 13) ^ i) & (QLZ_HASH_VALUES - 1);
  83. #else
  84. return ((i >> 12) ^ i) & (QLZ_HASH_VALUES - 1);
  85. #endif
  86. }
  87. static __inline ui32 fast_read(void const *src, ui32 bytes)
  88. {
  89. #ifndef X86X64
  90. unsigned char *p = (unsigned char*)src;
  91. switch (bytes)
  92. {
  93. case 4:
  94. return(*p | *(p + 1) << 8 | *(p + 2) << 16 | *(p + 3) << 24);
  95. case 3:
  96. return(*p | *(p + 1) << 8 | *(p + 2) << 16);
  97. case 2:
  98. return(*p | *(p + 1) << 8);
  99. case 1:
  100. return(*p);
  101. }
  102. return 0;
  103. #else
  104. if (bytes >= 1 && bytes <= 4)
  105. return *((ui32*)src);
  106. else
  107. return 0;
  108. #endif
  109. }
  110. static __inline ui32 hashat(const unsigned char *src)
  111. {
  112. ui32 fetch, hash;
  113. fetch = fast_read(src, 3);
  114. hash = hash_func(fetch);
  115. return hash;
  116. }
  117. static __inline void fast_write(ui32 f, void *dst, size_t bytes)
  118. {
  119. #ifndef X86X64
  120. unsigned char *p = (unsigned char*)dst;
  121. switch (bytes)
  122. {
  123. case 4:
  124. *p = (unsigned char)f;
  125. *(p + 1) = (unsigned char)(f >> 8);
  126. *(p + 2) = (unsigned char)(f >> 16);
  127. *(p + 3) = (unsigned char)(f >> 24);
  128. return;
  129. case 3:
  130. *p = (unsigned char)f;
  131. *(p + 1) = (unsigned char)(f >> 8);
  132. *(p + 2) = (unsigned char)(f >> 16);
  133. return;
  134. case 2:
  135. *p = (unsigned char)f;
  136. *(p + 1) = (unsigned char)(f >> 8);
  137. return;
  138. case 1:
  139. *p = (unsigned char)f;
  140. return;
  141. }
  142. #else
  143. switch (bytes)
  144. {
  145. case 4:
  146. *((ui32*)dst) = f;
  147. return;
  148. case 3:
  149. *((ui32*)dst) = f;
  150. return;
  151. case 2:
  152. *((ui16 *)dst) = (ui16)f;
  153. return;
  154. case 1:
  155. *((unsigned char*)dst) = (unsigned char)f;
  156. return;
  157. }
  158. #endif
  159. }
  160. size_t qlz_size_decompressed(const char *source)
  161. {
  162. ui32 n, r;
  163. n = (((*source) & 2) == 2) ? 4 : 1;
  164. r = fast_read(source + 1 + n, n);
  165. r = r & (0xffffffff >> ((4 - n)*8));
  166. return r;
  167. }
  168. size_t qlz_size_compressed(const char *source)
  169. {
  170. ui32 n, r;
  171. n = (((*source) & 2) == 2) ? 4 : 1;
  172. r = fast_read(source + 1, n);
  173. r = r & (0xffffffff >> ((4 - n)*8));
  174. return r;
  175. }
  176. size_t qlz_size_header(const char *source)
  177. {
  178. size_t n = 2*((((*source) & 2) == 2) ? 4 : 1) + 1;
  179. return n;
  180. }
  181. static __inline void memcpy_up(unsigned char *dst, const unsigned char *src, ui32 n)
  182. {
  183. // Caution if modifying memcpy_up! Overlap of dst and src must be special handled.
  184. #ifndef X86X64
  185. unsigned char *end = dst + n;
  186. while(dst < end)
  187. {
  188. *dst = *src;
  189. dst++;
  190. src++;
  191. }
  192. #else
  193. ui32 f = 0;
  194. do
  195. {
  196. *(ui32 *)(dst + f) = *(ui32 *)(src + f);
  197. f += MINOFFSET + 1;
  198. }
  199. while (f < n);
  200. #endif
  201. }
  202. static __inline void update_hash(qlz_state_decompress *state, const unsigned char *s)
  203. {
  204. #if QLZ_COMPRESSION_LEVEL == 1
  205. ui32 hash;
  206. hash = hashat(s);
  207. state->hash[hash].offset = s;
  208. state->hash_counter[hash] = 1;
  209. #elif QLZ_COMPRESSION_LEVEL == 2
  210. ui32 hash;
  211. unsigned char c;
  212. hash = hashat(s);
  213. c = state->hash_counter[hash];
  214. state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = s;
  215. c++;
  216. state->hash_counter[hash] = c;
  217. #endif
  218. (void)state;
  219. (void)s;
  220. }
  221. #if QLZ_COMPRESSION_LEVEL <= 2
  222. static void update_hash_upto(qlz_state_decompress *state, unsigned char **lh, const unsigned char *max)
  223. {
  224. while(*lh < max)
  225. {
  226. (*lh)++;
  227. update_hash(state, *lh);
  228. }
  229. }
  230. #endif
  231. static size_t qlz_compress_core(const unsigned char *source, unsigned char *destination, size_t size, qlz_state_compress *state)
  232. {
  233. const unsigned char *last_byte = source + size - 1;
  234. const unsigned char *src = source;
  235. unsigned char *cword_ptr = destination;
  236. unsigned char *dst = destination + CWORD_LEN;
  237. ui32 cword_val = 1U << 31;
  238. const unsigned char *last_matchstart = last_byte - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END;
  239. ui32 fetch = 0;
  240. unsigned int lits = 0;
  241. (void) lits;
  242. if(src <= last_matchstart)
  243. fetch = fast_read(src, 3);
  244. while(src <= last_matchstart)
  245. {
  246. if ((cword_val & 1) == 1)
  247. {
  248. // store uncompressed if compression ratio is too low
  249. if (src > source + (size >> 1) && dst - destination > src - source - ((src - source) >> 5))
  250. return 0;
  251. fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN);
  252. cword_ptr = dst;
  253. dst += CWORD_LEN;
  254. cword_val = 1U << 31;
  255. fetch = fast_read(src, 3);
  256. }
  257. #if QLZ_COMPRESSION_LEVEL == 1
  258. {
  259. const unsigned char *o;
  260. ui32 hash, cached;
  261. hash = hash_func(fetch);
  262. cached = fetch ^ state->hash[hash].cache;
  263. state->hash[hash].cache = fetch;
  264. o = state->hash[hash].offset + OFFSET_BASE;
  265. state->hash[hash].offset = CAST(src - OFFSET_BASE);
  266. #ifdef X86X64
  267. if ((cached & 0xffffff) == 0 && o != OFFSET_BASE && (src - o > MINOFFSET || (src == o + 1 && lits >= 3 && src > source + 3 && same(src - 3, 6))))
  268. {
  269. if(cached != 0)
  270. {
  271. #else
  272. if (cached == 0 && o != OFFSET_BASE && (src - o > MINOFFSET || (src == o + 1 && lits >= 3 && src > source + 3 && same(src - 3, 6))))
  273. {
  274. if (*(o + 3) != *(src + 3))
  275. {
  276. #endif
  277. hash <<= 4;
  278. cword_val = (cword_val >> 1) | (1U << 31);
  279. fast_write((3 - 2) | hash, dst, 2);
  280. src += 3;
  281. dst += 2;
  282. }
  283. else
  284. {
  285. const unsigned char *old_src = src;
  286. size_t matchlen;
  287. hash <<= 4;
  288. cword_val = (cword_val >> 1) | (1U << 31);
  289. src += 4;
  290. if(*(o + (src - old_src)) == *src)
  291. {
  292. src++;
  293. if(*(o + (src - old_src)) == *src)
  294. {
  295. size_t q = last_byte - UNCOMPRESSED_END - (src - 5) + 1;
  296. size_t remaining = q > 255 ? 255 : q;
  297. src++;
  298. while(*(o + (src - old_src)) == *src && (size_t)(src - old_src) < remaining)
  299. src++;
  300. }
  301. }
  302. matchlen = src - old_src;
  303. if (matchlen < 18)
  304. {
  305. fast_write((ui32)(matchlen - 2) | hash, dst, 2);
  306. dst += 2;
  307. }
  308. else
  309. {
  310. fast_write((ui32)(matchlen << 16) | hash, dst, 3);
  311. dst += 3;
  312. }
  313. }
  314. fetch = fast_read(src, 3);
  315. lits = 0;
  316. }
  317. else
  318. {
  319. lits++;
  320. *dst = *src;
  321. src++;
  322. dst++;
  323. cword_val = (cword_val >> 1);
  324. #ifdef X86X64
  325. fetch = fast_read(src, 3);
  326. #else
  327. fetch = (fetch >> 8 & 0xffff) | (*(src + 2) << 16);
  328. #endif
  329. }
  330. }
  331. #elif QLZ_COMPRESSION_LEVEL >= 2
  332. {
  333. const unsigned char *o, *offset2;
  334. ui32 hash, matchlen, k, m, best_k = 0;
  335. unsigned char c;
  336. size_t remaining = (last_byte - UNCOMPRESSED_END - src + 1) > 255 ? 255 : (last_byte - UNCOMPRESSED_END - src + 1);
  337. (void)best_k;
  338. //hash = hashat(src);
  339. fetch = fast_read(src, 3);
  340. hash = hash_func(fetch);
  341. c = state->hash_counter[hash];
  342. offset2 = state->hash[hash].offset[0];
  343. if(offset2 < src - MINOFFSET && c > 0 && ((fast_read(offset2, 3) ^ fetch) & 0xffffff) == 0)
  344. {
  345. matchlen = 3;
  346. if(*(offset2 + matchlen) == *(src + matchlen))
  347. {
  348. matchlen = 4;
  349. while(*(offset2 + matchlen) == *(src + matchlen) && matchlen < remaining)
  350. matchlen++;
  351. }
  352. }
  353. else
  354. matchlen = 0;
  355. for(k = 1; k < QLZ_POINTERS && c > k; k++)
  356. {
  357. o = state->hash[hash].offset[k];
  358. #if QLZ_COMPRESSION_LEVEL == 3
  359. if(((fast_read(o, 3) ^ fetch) & 0xffffff) == 0 && o < src - MINOFFSET)
  360. #elif QLZ_COMPRESSION_LEVEL == 2
  361. if(*(src + matchlen) == *(o + matchlen) && ((fast_read(o, 3) ^ fetch) & 0xffffff) == 0 && o < src - MINOFFSET)
  362. #endif
  363. {
  364. m = 3;
  365. while(*(o + m) == *(src + m) && m < remaining)
  366. m++;
  367. #if QLZ_COMPRESSION_LEVEL == 3
  368. if ((m > matchlen) || (m == matchlen && o > offset2))
  369. #elif QLZ_COMPRESSION_LEVEL == 2
  370. if (m > matchlen)
  371. #endif
  372. {
  373. offset2 = o;
  374. matchlen = m;
  375. best_k = k;
  376. }
  377. }
  378. }
  379. o = offset2;
  380. state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = src;
  381. c++;
  382. state->hash_counter[hash] = c;
  383. #if QLZ_COMPRESSION_LEVEL == 3
  384. if(matchlen > 2 && src - o < 131071)
  385. {
  386. ui32 u;
  387. size_t offset = src - o;
  388. for(u = 1; u < matchlen; u++)
  389. {
  390. hash = hashat(src + u);
  391. c = state->hash_counter[hash]++;
  392. state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = src + u;
  393. }
  394. cword_val = (cword_val >> 1) | (1U << 31);
  395. src += matchlen;
  396. if(matchlen == 3 && offset <= 63)
  397. {
  398. *dst = (unsigned char)(offset << 2);
  399. dst++;
  400. }
  401. else if (matchlen == 3 && offset <= 16383)
  402. {
  403. ui32 f = (ui32)((offset << 2) | 1);
  404. fast_write(f, dst, 2);
  405. dst += 2;
  406. }
  407. else if (matchlen <= 18 && offset <= 1023)
  408. {
  409. ui32 f = ((matchlen - 3) << 2) | ((ui32)offset << 6) | 2;
  410. fast_write(f, dst, 2);
  411. dst += 2;
  412. }
  413. else if(matchlen <= 33)
  414. {
  415. ui32 f = ((matchlen - 2) << 2) | ((ui32)offset << 7) | 3;
  416. fast_write(f, dst, 3);
  417. dst += 3;
  418. }
  419. else
  420. {
  421. ui32 f = ((matchlen - 3) << 7) | ((ui32)offset << 15) | 3;
  422. fast_write(f, dst, 4);
  423. dst += 4;
  424. }
  425. }
  426. else
  427. {
  428. *dst = *src;
  429. src++;
  430. dst++;
  431. cword_val = (cword_val >> 1);
  432. }
  433. #elif QLZ_COMPRESSION_LEVEL == 2
  434. if(matchlen > 2)
  435. {
  436. cword_val = (cword_val >> 1) | (1U << 31);
  437. src += matchlen;
  438. if (matchlen < 10)
  439. {
  440. ui32 f = best_k | ((matchlen - 2) << 2) | (hash << 5);
  441. fast_write(f, dst, 2);
  442. dst += 2;
  443. }
  444. else
  445. {
  446. ui32 f = best_k | (matchlen << 16) | (hash << 5);
  447. fast_write(f, dst, 3);
  448. dst += 3;
  449. }
  450. }
  451. else
  452. {
  453. *dst = *src;
  454. src++;
  455. dst++;
  456. cword_val = (cword_val >> 1);
  457. }
  458. #endif
  459. }
  460. #endif
  461. }
  462. while (src <= last_byte)
  463. {
  464. if ((cword_val & 1) == 1)
  465. {
  466. fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN);
  467. cword_ptr = dst;
  468. dst += CWORD_LEN;
  469. cword_val = 1U << 31;
  470. }
  471. #if QLZ_COMPRESSION_LEVEL < 3
  472. if (src <= last_byte - 3)
  473. {
  474. #if QLZ_COMPRESSION_LEVEL == 1
  475. ui32 hash, fetch;
  476. fetch = fast_read(src, 3);
  477. hash = hash_func(fetch);
  478. state->hash[hash].offset = CAST(src - OFFSET_BASE);
  479. state->hash[hash].cache = fetch;
  480. #elif QLZ_COMPRESSION_LEVEL == 2
  481. ui32 hash;
  482. unsigned char c;
  483. hash = hashat(src);
  484. c = state->hash_counter[hash];
  485. state->hash[hash].offset[c & (QLZ_POINTERS - 1)] = src;
  486. c++;
  487. state->hash_counter[hash] = c;
  488. #endif
  489. }
  490. #endif
  491. *dst = *src;
  492. src++;
  493. dst++;
  494. cword_val = (cword_val >> 1);
  495. }
  496. while((cword_val & 1) != 1)
  497. cword_val = (cword_val >> 1);
  498. fast_write((cword_val >> 1) | (1U << 31), cword_ptr, CWORD_LEN);
  499. // min. size must be 9 bytes so that the qlz_size functions can take 9 bytes as argument
  500. return dst - destination < 9 ? 9 : dst - destination;
  501. }
  502. static size_t qlz_decompress_core(const unsigned char *source, unsigned char *destination, size_t size, qlz_state_decompress *state, const unsigned char *history)
  503. {
  504. const unsigned char *src = source + qlz_size_header((const char *)source);
  505. unsigned char *dst = destination;
  506. const unsigned char *last_destination_byte = destination + size - 1;
  507. ui32 cword_val = 1;
  508. const unsigned char *last_matchstart = last_destination_byte - UNCONDITIONAL_MATCHLEN - UNCOMPRESSED_END;
  509. unsigned char *last_hashed = destination - 1;
  510. const unsigned char *last_source_byte = source + qlz_size_compressed((const char *)source) - 1;
  511. static const ui32 bitlut[16] = {4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0};
  512. (void) last_source_byte;
  513. (void) last_hashed;
  514. (void) state;
  515. (void) history;
  516. for(;;)
  517. {
  518. ui32 fetch;
  519. if (cword_val == 1)
  520. {
  521. #ifdef QLZ_MEMORY_SAFE
  522. if(src + CWORD_LEN - 1 > last_source_byte)
  523. return 0;
  524. #endif
  525. cword_val = fast_read(src, CWORD_LEN);
  526. src += CWORD_LEN;
  527. }
  528. #ifdef QLZ_MEMORY_SAFE
  529. if(src + 4 - 1 > last_source_byte)
  530. return 0;
  531. #endif
  532. fetch = fast_read(src, 4);
  533. if ((cword_val & 1) == 1)
  534. {
  535. ui32 matchlen;
  536. const unsigned char *offset2;
  537. #if QLZ_COMPRESSION_LEVEL == 1
  538. ui32 hash;
  539. cword_val = cword_val >> 1;
  540. hash = (fetch >> 4) & 0xfff;
  541. offset2 = (const unsigned char *)(size_t)state->hash[hash].offset;
  542. if((fetch & 0xf) != 0)
  543. {
  544. matchlen = (fetch & 0xf) + 2;
  545. src += 2;
  546. }
  547. else
  548. {
  549. matchlen = *(src + 2);
  550. src += 3;
  551. }
  552. #elif QLZ_COMPRESSION_LEVEL == 2
  553. ui32 hash;
  554. unsigned char c;
  555. cword_val = cword_val >> 1;
  556. hash = (fetch >> 5) & 0x7ff;
  557. c = (unsigned char)(fetch & 0x3);
  558. offset2 = state->hash[hash].offset[c];
  559. if((fetch & (28)) != 0)
  560. {
  561. matchlen = ((fetch >> 2) & 0x7) + 2;
  562. src += 2;
  563. }
  564. else
  565. {
  566. matchlen = *(src + 2);
  567. src += 3;
  568. }
  569. #elif QLZ_COMPRESSION_LEVEL == 3
  570. ui32 offset;
  571. cword_val = cword_val >> 1;
  572. if ((fetch & 3) == 0)
  573. {
  574. offset = (fetch & 0xff) >> 2;
  575. matchlen = 3;
  576. src++;
  577. }
  578. else if ((fetch & 2) == 0)
  579. {
  580. offset = (fetch & 0xffff) >> 2;
  581. matchlen = 3;
  582. src += 2;
  583. }
  584. else if ((fetch & 1) == 0)
  585. {
  586. offset = (fetch & 0xffff) >> 6;
  587. matchlen = ((fetch >> 2) & 15) + 3;
  588. src += 2;
  589. }
  590. else if ((fetch & 127) != 3)
  591. {
  592. offset = (fetch >> 7) & 0x1ffff;
  593. matchlen = ((fetch >> 2) & 0x1f) + 2;
  594. src += 3;
  595. }
  596. else
  597. {
  598. offset = (fetch >> 15);
  599. matchlen = ((fetch >> 7) & 255) + 3;
  600. src += 4;
  601. }
  602. offset2 = dst - offset;
  603. #endif
  604. #ifdef QLZ_MEMORY_SAFE
  605. if(offset2 < history || offset2 > dst - MINOFFSET - 1)
  606. return 0;
  607. if(matchlen > (ui32)(last_destination_byte - dst - UNCOMPRESSED_END + 1))
  608. return 0;
  609. #endif
  610. memcpy_up(dst, offset2, matchlen);
  611. dst += matchlen;
  612. #if QLZ_COMPRESSION_LEVEL <= 2
  613. update_hash_upto(state, &last_hashed, dst - matchlen);
  614. last_hashed = dst - 1;
  615. #endif
  616. }
  617. else
  618. {
  619. if (dst < last_matchstart)
  620. {
  621. unsigned int n = bitlut[cword_val & 0xf];
  622. #ifdef X86X64
  623. *(ui32 *)dst = *(ui32 *)src;
  624. #else
  625. memcpy_up(dst, src, 4);
  626. #endif
  627. cword_val = cword_val >> n;
  628. dst += n;
  629. src += n;
  630. #if QLZ_COMPRESSION_LEVEL <= 2
  631. update_hash_upto(state, &last_hashed, dst - 3);
  632. #endif
  633. }
  634. else
  635. {
  636. while(dst <= last_destination_byte)
  637. {
  638. if (cword_val == 1)
  639. {
  640. src += CWORD_LEN;
  641. cword_val = 1U << 31;
  642. }
  643. #ifdef QLZ_MEMORY_SAFE
  644. if(src >= last_source_byte + 1)
  645. return 0;
  646. #endif
  647. *dst = *src;
  648. dst++;
  649. src++;
  650. cword_val = cword_val >> 1;
  651. }
  652. #if QLZ_COMPRESSION_LEVEL <= 2
  653. update_hash_upto(state, &last_hashed, last_destination_byte - 3); // todo, use constant
  654. #endif
  655. return size;
  656. }
  657. }
  658. }
  659. }
  660. size_t qlz_compress(const void *source, char *destination, size_t size, qlz_state_compress *state)
  661. {
  662. size_t r;
  663. ui32 compressed;
  664. size_t base;
  665. if(size == 0 || size > 0xffffffff - 400)
  666. return 0;
  667. if(size < 216)
  668. base = 3;
  669. else
  670. base = 9;
  671. #if QLZ_STREAMING_BUFFER > 0
  672. if (state->stream_counter + size - 1 >= QLZ_STREAMING_BUFFER)
  673. #endif
  674. {
  675. reset_table_compress(state);
  676. r = base + qlz_compress_core((const unsigned char *)source, (unsigned char*)destination + base, size, state);
  677. #if QLZ_STREAMING_BUFFER > 0
  678. reset_table_compress(state);
  679. #endif
  680. if(r == base)
  681. {
  682. memcpy(destination + base, source, size);
  683. r = size + base;
  684. compressed = 0;
  685. }
  686. else
  687. {
  688. compressed = 1;
  689. }
  690. state->stream_counter = 0;
  691. }
  692. #if QLZ_STREAMING_BUFFER > 0
  693. else
  694. {
  695. unsigned char *src = state->stream_buffer + state->stream_counter;
  696. memcpy(src, source, size);
  697. r = base + qlz_compress_core(src, (unsigned char*)destination + base, size, state);
  698. if(r == base)
  699. {
  700. memcpy(destination + base, src, size);
  701. r = size + base;
  702. compressed = 0;
  703. reset_table_compress(state);
  704. }
  705. else
  706. {
  707. compressed = 1;
  708. }
  709. state->stream_counter += size;
  710. }
  711. #endif
  712. if(base == 3)
  713. {
  714. *destination = (unsigned char)(0 | compressed);
  715. *(destination + 1) = (unsigned char)r;
  716. *(destination + 2) = (unsigned char)size;
  717. }
  718. else
  719. {
  720. *destination = (unsigned char)(2 | compressed);
  721. fast_write((ui32)r, destination + 1, 4);
  722. fast_write((ui32)size, destination + 5, 4);
  723. }
  724. *destination |= (QLZ_COMPRESSION_LEVEL << 2);
  725. *destination |= (1 << 6);
  726. *destination |= ((QLZ_STREAMING_BUFFER == 0 ? 0 : (QLZ_STREAMING_BUFFER == 100000 ? 1 : (QLZ_STREAMING_BUFFER == 1000000 ? 2 : 3))) << 4);
  727. // 76543210
  728. // 01SSLLHC
  729. return r;
  730. }
  731. size_t qlz_decompress(const char *source, void *destination, qlz_state_decompress *state)
  732. {
  733. size_t dsiz = qlz_size_decompressed(source);
  734. #if QLZ_STREAMING_BUFFER > 0
  735. if (state->stream_counter + qlz_size_decompressed(source) - 1 >= QLZ_STREAMING_BUFFER)
  736. #endif
  737. {
  738. if((*source & 1) == 1)
  739. {
  740. reset_table_decompress(state);
  741. dsiz = qlz_decompress_core((const unsigned char *)source, (unsigned char *)destination, dsiz, state, (const unsigned char *)destination);
  742. }
  743. else
  744. {
  745. memcpy(destination, source + qlz_size_header(source), dsiz);
  746. }
  747. state->stream_counter = 0;
  748. reset_table_decompress(state);
  749. }
  750. #if QLZ_STREAMING_BUFFER > 0
  751. else
  752. {
  753. unsigned char *dst = state->stream_buffer + state->stream_counter;
  754. if((*source & 1) == 1)
  755. {
  756. dsiz = qlz_decompress_core((const unsigned char *)source, dst, dsiz, state, (const unsigned char *)state->stream_buffer);
  757. }
  758. else
  759. {
  760. memcpy(dst, source + qlz_size_header(source), dsiz);
  761. reset_table_decompress(state);
  762. }
  763. memcpy(destination, dst, dsiz);
  764. state->stream_counter += dsiz;
  765. }
  766. #endif
  767. return dsiz;
  768. }