quicklz.c 24 KB

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