fastlz.c 15 KB

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