sinfl.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. # Small Deflate
  3. `sdefl` is a small bare bone lossless compression library in ANSI C (ISO C90)
  4. which implements the Deflate (RFC 1951) compressed data format specification standard.
  5. It is mainly tuned to get as much speed and compression ratio from as little code
  6. as needed to keep the implementation as concise as possible.
  7. ## Features
  8. - Portable single header and source file duo written in ANSI C (ISO C90)
  9. - Dual license with either MIT or public domain
  10. - Small implementation
  11. - Deflate: 525 LoC
  12. - Inflate: 500 LoC
  13. - Webassembly:
  14. - Deflate ~3.7 KB (~2.2KB compressed)
  15. - Inflate ~3.6 KB (~2.2KB compressed)
  16. ## Usage:
  17. This file behaves differently depending on what symbols you define
  18. before including it.
  19. Header-File mode:
  20. If you do not define `SINFL_IMPLEMENTATION` before including this file, it
  21. will operate in header only mode. In this mode it declares all used structs
  22. and the API of the library without including the implementation of the library.
  23. Implementation mode:
  24. If you define `SINFL_IMPLEMENTATION` before including this file, it will
  25. compile the implementation. Make sure that you only include
  26. this file implementation in *one* C or C++ file to prevent collisions.
  27. ### Benchmark
  28. | Compressor name | Compression| Decompress.| Compr. size | Ratio |
  29. | ------------------------| -----------| -----------| ----------- | ----- |
  30. | miniz 1.0 -1 | 122 MB/s | 208 MB/s | 48510028 | 48.51 |
  31. | miniz 1.0 -6 | 27 MB/s | 260 MB/s | 36513697 | 36.51 |
  32. | miniz 1.0 -9 | 23 MB/s | 261 MB/s | 36460101 | 36.46 |
  33. | zlib 1.2.11 -1 | 72 MB/s | 307 MB/s | 42298774 | 42.30 |
  34. | zlib 1.2.11 -6 | 24 MB/s | 313 MB/s | 36548921 | 36.55 |
  35. | zlib 1.2.11 -9 | 20 MB/s | 314 MB/s | 36475792 | 36.48 |
  36. | sdefl 1.0 -0 | 127 MB/s | 355 MB/s | 40004116 | 39.88 |
  37. | sdefl 1.0 -1 | 111 MB/s | 413 MB/s | 38940674 | 38.82 |
  38. | sdefl 1.0 -5 | 45 MB/s | 436 MB/s | 36577183 | 36.46 |
  39. | sdefl 1.0 -7 | 38 MB/s | 432 MB/s | 36523781 | 36.41 |
  40. | libdeflate 1.3 -1 | 147 MB/s | 667 MB/s | 39597378 | 39.60 |
  41. | libdeflate 1.3 -6 | 69 MB/s | 689 MB/s | 36648318 | 36.65 |
  42. | libdeflate 1.3 -9 | 13 MB/s | 672 MB/s | 35197141 | 35.20 |
  43. | libdeflate 1.3 -12 | 8.13 MB/s | 670 MB/s | 35100568 | 35.10 |
  44. ### Compression
  45. Results on the [Silesia compression corpus](http://sun.aei.polsl.pl/~sdeor/index.php?page=silesia):
  46. | File | Original | `sdefl 0` | `sdefl 5` | `sdefl 7` |
  47. | --------| -----------| -------------| ---------- | ------------|
  48. | dickens | 10.192.446 | 4,260,187 | 3,845,261 | 3,833,657 |
  49. | mozilla | 51.220.480 | 20,774,706 | 19,607,009 | 19,565,867 |
  50. | mr | 9.970.564 | 3,860,531 | 3,673,460 | 3,665,627 |
  51. | nci | 33.553.445 | 4,030,283 | 3,094,526 | 3,006,075 |
  52. | ooffice | 6.152.192 | 3,320,063 | 3,186,373 | 3,183,815 |
  53. | osdb | 10.085.684 | 3,919,646 | 3,649,510 | 3,649,477 |
  54. | reymont | 6.627.202 | 2,263,378 | 1,857,588 | 1,827,237 |
  55. | samba | 21.606.400 | 6,121,797 | 5,462,670 | 5,450,762 |
  56. | sao | 7.251.944 | 5,612,421 | 5,485,380 | 5,481,765 |
  57. | webster | 41.458.703 | 13,972,648 | 12,059,432 | 11,991,421 |
  58. | xml | 5.345.280 | 886,620 | 674,009 | 662,141 |
  59. | x-ray | 8.474.240 | 6,304,655 | 6,244,779 | 6,244,779 |
  60. ## License
  61. ```
  62. ------------------------------------------------------------------------------
  63. This software is available under 2 licenses -- choose whichever you prefer.
  64. ------------------------------------------------------------------------------
  65. ALTERNATIVE A - MIT License
  66. Copyright (c) 2020-2023 Micha Mettke
  67. Permission is hereby granted, free of charge, to any person obtaining a copy of
  68. this software and associated documentation files (the "Software"), to deal in
  69. the Software without restriction, including without limitation the rights to
  70. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  71. of the Software, and to permit persons to whom the Software is furnished to do
  72. so, subject to the following conditions:
  73. The above copyright notice and this permission notice shall be included in all
  74. copies or substantial portions of the Software.
  75. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  76. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  77. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  78. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  79. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  80. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  81. SOFTWARE.
  82. ------------------------------------------------------------------------------
  83. ALTERNATIVE B - Public Domain (www.unlicense.org)
  84. This is free and unencumbered software released into the public domain.
  85. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  86. software, either in source code form or as a compiled binary, for any purpose,
  87. commercial or non-commercial, and by any means.
  88. In jurisdictions that recognize copyright laws, the author or authors of this
  89. software dedicate any and all copyright interest in the software to the public
  90. domain. We make this dedication for the benefit of the public at large and to
  91. the detriment of our heirs and successors. We intend this dedication to be an
  92. overt act of relinquishment in perpetuity of all present and future rights to
  93. this software under copyright law.
  94. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  95. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  96. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  97. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  98. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  99. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  100. ------------------------------------------------------------------------------
  101. ```
  102. */
  103. #ifndef SINFL_H_INCLUDED
  104. #define SINFL_H_INCLUDED
  105. #ifdef __cplusplus
  106. extern "C" {
  107. #endif
  108. #define SINFL_PRE_TBL_SIZE 128
  109. #define SINFL_LIT_TBL_SIZE 1334
  110. #define SINFL_OFF_TBL_SIZE 402
  111. struct sinfl {
  112. const unsigned char *bitptr;
  113. unsigned long long bitbuf;
  114. int bitcnt;
  115. unsigned lits[SINFL_LIT_TBL_SIZE];
  116. unsigned dsts[SINFL_OFF_TBL_SIZE];
  117. };
  118. extern int sinflate(void *out, int cap, const void *in, int size);
  119. extern int zsinflate(void *out, int cap, const void *in, int size);
  120. #ifdef __cplusplus
  121. }
  122. #endif
  123. #endif /* SINFL_H_INCLUDED */
  124. #ifdef SINFL_IMPLEMENTATION
  125. #include <string.h> /* memcpy, memset */
  126. #include <assert.h> /* assert */
  127. #if defined(__GNUC__) || defined(__clang__)
  128. #define sinfl_likely(x) __builtin_expect((x),1)
  129. #define sinfl_unlikely(x) __builtin_expect((x),0)
  130. #else
  131. #define sinfl_likely(x) (x)
  132. #define sinfl_unlikely(x) (x)
  133. #endif
  134. #ifndef SINFL_NO_SIMD
  135. #if defined(__x86_64__) || defined(_WIN32) || defined(_WIN64)
  136. #include <emmintrin.h>
  137. #define sinfl_char16 __m128i
  138. #define sinfl_char16_ld(p) _mm_loadu_si128((const __m128i *)(void*)(p))
  139. #define sinfl_char16_str(d,v) _mm_storeu_si128((__m128i*)(void*)(d), v)
  140. #define sinfl_char16_char(c) _mm_set1_epi8(c)
  141. #elif defined(__arm__) || defined(__aarch64__)
  142. #include <arm_neon.h>
  143. #define sinfl_char16 uint8x16_t
  144. #define sinfl_char16_ld(p) vld1q_u8((const unsigned char*)(p))
  145. #define sinfl_char16_str(d,v) vst1q_u8((unsigned char*)(d), v)
  146. #define sinfl_char16_char(c) vdupq_n_u8(c)
  147. #else
  148. #define SINFL_NO_SIMD
  149. #endif
  150. #endif
  151. static int
  152. sinfl_bsr(unsigned n) {
  153. #ifdef _MSC_VER
  154. _BitScanReverse(&n, n);
  155. return n;
  156. #elif defined(__GNUC__) || defined(__clang__)
  157. return 31 - __builtin_clz(n);
  158. #endif
  159. }
  160. static unsigned long long
  161. sinfl_read64(const void *p) {
  162. unsigned long long n;
  163. memcpy(&n, p, 8);
  164. return n;
  165. }
  166. static void
  167. sinfl_copy64(unsigned char **dst, unsigned char **src) {
  168. unsigned long long n;
  169. memcpy(&n, *src, 8);
  170. memcpy(*dst, &n, 8);
  171. *dst += 8, *src += 8;
  172. }
  173. static unsigned char*
  174. sinfl_write64(unsigned char *dst, unsigned long long w) {
  175. memcpy(dst, &w, 8);
  176. return dst + 8;
  177. }
  178. #ifndef SINFL_NO_SIMD
  179. static unsigned char*
  180. sinfl_write128(unsigned char *dst, sinfl_char16 w) {
  181. sinfl_char16_str(dst, w);
  182. return dst + 8;
  183. }
  184. static void
  185. sinfl_copy128(unsigned char **dst, unsigned char **src) {
  186. sinfl_char16 n = sinfl_char16_ld(*src);
  187. sinfl_char16_str(*dst, n);
  188. *dst += 16, *src += 16;
  189. }
  190. #endif
  191. static void
  192. sinfl_refill(struct sinfl *s) {
  193. s->bitbuf |= sinfl_read64(s->bitptr) << s->bitcnt;
  194. s->bitptr += (63 - s->bitcnt) >> 3;
  195. s->bitcnt |= 56; /* bitcount in range [56,63] */
  196. }
  197. static int
  198. sinfl_peek(struct sinfl *s, int cnt) {
  199. assert(cnt >= 0 && cnt <= 56);
  200. assert(cnt <= s->bitcnt);
  201. return s->bitbuf & ((1ull << cnt) - 1);
  202. }
  203. static void
  204. sinfl_eat(struct sinfl *s, int cnt) {
  205. assert(cnt <= s->bitcnt);
  206. s->bitbuf >>= cnt;
  207. s->bitcnt -= cnt;
  208. }
  209. static int
  210. sinfl__get(struct sinfl *s, int cnt) {
  211. int res = sinfl_peek(s, cnt);
  212. sinfl_eat(s, cnt);
  213. return res;
  214. }
  215. static int
  216. sinfl_get(struct sinfl *s, int cnt) {
  217. sinfl_refill(s);
  218. return sinfl__get(s, cnt);
  219. }
  220. struct sinfl_gen {
  221. int len;
  222. int cnt;
  223. int word;
  224. short* sorted;
  225. };
  226. static int
  227. sinfl_build_tbl(struct sinfl_gen *gen, unsigned *tbl, int tbl_bits,
  228. const int *cnt) {
  229. int tbl_end = 0;
  230. while (!(gen->cnt = cnt[gen->len])) {
  231. ++gen->len;
  232. }
  233. tbl_end = 1 << gen->len;
  234. while (gen->len <= tbl_bits) {
  235. do {unsigned bit = 0;
  236. tbl[gen->word] = (*gen->sorted++ << 16) | gen->len;
  237. if (gen->word == tbl_end - 1) {
  238. for (; gen->len < tbl_bits; gen->len++) {
  239. memcpy(&tbl[tbl_end], tbl, (size_t)tbl_end * sizeof(tbl[0]));
  240. tbl_end <<= 1;
  241. }
  242. return 1;
  243. }
  244. bit = 1 << sinfl_bsr((unsigned)(gen->word ^ (tbl_end - 1)));
  245. gen->word &= bit - 1;
  246. gen->word |= bit;
  247. } while (--gen->cnt);
  248. do {
  249. if (++gen->len <= tbl_bits) {
  250. memcpy(&tbl[tbl_end], tbl, (size_t)tbl_end * sizeof(tbl[0]));
  251. tbl_end <<= 1;
  252. }
  253. } while (!(gen->cnt = cnt[gen->len]));
  254. }
  255. return 0;
  256. }
  257. static void
  258. sinfl_build_subtbl(struct sinfl_gen *gen, unsigned *tbl, int tbl_bits,
  259. const int *cnt) {
  260. int sub_bits = 0;
  261. int sub_start = 0;
  262. int sub_prefix = -1;
  263. int tbl_end = 1 << tbl_bits;
  264. while (1) {
  265. unsigned entry;
  266. int bit, stride, i;
  267. /* start new sub-table */
  268. if ((gen->word & ((1 << tbl_bits)-1)) != sub_prefix) {
  269. int used = 0;
  270. sub_prefix = gen->word & ((1 << tbl_bits)-1);
  271. sub_start = tbl_end;
  272. sub_bits = gen->len - tbl_bits;
  273. used = gen->cnt;
  274. while (used < (1 << sub_bits)) {
  275. sub_bits++;
  276. used = (used << 1) + cnt[tbl_bits + sub_bits];
  277. }
  278. tbl_end = sub_start + (1 << sub_bits);
  279. tbl[sub_prefix] = (sub_start << 16) | 0x10 | (sub_bits & 0xf);
  280. }
  281. /* fill sub-table */
  282. entry = (*gen->sorted << 16) | ((gen->len - tbl_bits) & 0xf);
  283. gen->sorted++;
  284. i = sub_start + (gen->word >> tbl_bits);
  285. stride = 1 << (gen->len - tbl_bits);
  286. do {
  287. tbl[i] = entry;
  288. i += stride;
  289. } while (i < tbl_end);
  290. if (gen->word == (1 << gen->len)-1) {
  291. return;
  292. }
  293. bit = 1 << sinfl_bsr(gen->word ^ ((1 << gen->len) - 1));
  294. gen->word &= bit - 1;
  295. gen->word |= bit;
  296. gen->cnt--;
  297. while (!gen->cnt) {
  298. gen->cnt = cnt[++gen->len];
  299. }
  300. }
  301. }
  302. static void
  303. sinfl_build(unsigned *tbl, unsigned char *lens, int tbl_bits, int maxlen,
  304. int symcnt) {
  305. int i, used = 0;
  306. short sort[288];
  307. int cnt[16] = {0}, off[16]= {0};
  308. struct sinfl_gen gen = {0};
  309. gen.sorted = sort;
  310. gen.len = 1;
  311. for (i = 0; i < symcnt; ++i)
  312. cnt[lens[i]]++;
  313. off[1] = cnt[0];
  314. for (i = 1; i < maxlen; ++i) {
  315. off[i + 1] = off[i] + cnt[i];
  316. used = (used << 1) + cnt[i];
  317. }
  318. used = (used << 1) + cnt[i];
  319. for (i = 0; i < symcnt; ++i)
  320. gen.sorted[off[lens[i]]++] = (short)i;
  321. gen.sorted += off[0];
  322. if (used < (1 << maxlen)){
  323. for (i = 0; i < 1 << tbl_bits; ++i)
  324. tbl[i] = (0 << 16u) | 1;
  325. return;
  326. }
  327. if (!sinfl_build_tbl(&gen, tbl, tbl_bits, cnt)){
  328. sinfl_build_subtbl(&gen, tbl, tbl_bits, cnt);
  329. }
  330. }
  331. static int
  332. sinfl_decode(struct sinfl *s, const unsigned *tbl, int bit_len) {
  333. int idx = sinfl_peek(s, bit_len);
  334. unsigned key = tbl[idx];
  335. if (key & 0x10) {
  336. /* sub-table lookup */
  337. int len = key & 0x0f;
  338. sinfl_eat(s, bit_len);
  339. idx = sinfl_peek(s, len);
  340. key = tbl[((key >> 16) & 0xffff) + (unsigned)idx];
  341. }
  342. sinfl_eat(s, key & 0x0f);
  343. return (key >> 16) & 0x0fff;
  344. }
  345. static int
  346. sinfl_decompress(unsigned char *out, int cap, const unsigned char *in, int size) {
  347. static const unsigned char order[] = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
  348. static const short dbase[30+2] = {1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  349. 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577};
  350. static const unsigned char dbits[30+2] = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,
  351. 10,10,11,11,12,12,13,13,0,0};
  352. static const short lbase[29+2] = {3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,
  353. 43,51,59,67,83,99,115,131,163,195,227,258,0,0};
  354. static const unsigned char lbits[29+2] = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,
  355. 4,4,4,5,5,5,5,0,0,0};
  356. const unsigned char *oe = out + cap;
  357. const unsigned char *e = in + size, *o = out;
  358. enum sinfl_states {hdr,stored,fixed,dyn,blk};
  359. enum sinfl_states state = hdr;
  360. struct sinfl s = {0};
  361. int last = 0;
  362. s.bitptr = in;
  363. while (1) {
  364. switch (state) {
  365. case hdr: {
  366. /* block header */
  367. int type = 0;
  368. sinfl_refill(&s);
  369. last = sinfl__get(&s,1);
  370. type = sinfl__get(&s,2);
  371. switch (type) {default: return (int)(out-o);
  372. case 0x00: state = stored; break;
  373. case 0x01: state = fixed; break;
  374. case 0x02: state = dyn; break;}
  375. } break;
  376. case stored: {
  377. /* uncompressed block */
  378. unsigned len, nlen;
  379. sinfl__get(&s,s.bitcnt & 7);
  380. len = (unsigned short)sinfl__get(&s,16);
  381. nlen = (unsigned short)sinfl__get(&s,16);
  382. s.bitptr -= s.bitcnt / 8;
  383. s.bitbuf = s.bitcnt = 0;
  384. if ((unsigned short)len != (unsigned short)~nlen)
  385. return (int)(out-o);
  386. if (len > (e - s.bitptr) || !len)
  387. return (int)(out-o);
  388. memcpy(out, s.bitptr, (size_t)len);
  389. s.bitptr += len, out += len;
  390. if (last) return (int)(out-o);
  391. state = hdr;
  392. } break;
  393. case fixed: {
  394. /* fixed huffman codes */
  395. int n; unsigned char lens[288+32];
  396. for (n = 0; n <= 143; n++) lens[n] = 8;
  397. for (n = 144; n <= 255; n++) lens[n] = 9;
  398. for (n = 256; n <= 279; n++) lens[n] = 7;
  399. for (n = 280; n <= 287; n++) lens[n] = 8;
  400. for (n = 0; n < 32; n++) lens[288+n] = 5;
  401. /* build lit/dist tables */
  402. sinfl_build(s.lits, lens, 10, 15, 288);
  403. sinfl_build(s.dsts, lens + 288, 8, 15, 32);
  404. state = blk;
  405. } break;
  406. case dyn: {
  407. /* dynamic huffman codes */
  408. int n, i;
  409. unsigned hlens[SINFL_PRE_TBL_SIZE];
  410. unsigned char nlens[19] = {0}, lens[288+32];
  411. sinfl_refill(&s);
  412. {int nlit = 257 + sinfl__get(&s,5);
  413. int ndist = 1 + sinfl__get(&s,5);
  414. int nlen = 4 + sinfl__get(&s,4);
  415. for (n = 0; n < nlen; n++)
  416. nlens[order[n]] = (unsigned char)sinfl_get(&s,3);
  417. sinfl_build(hlens, nlens, 7, 7, 19);
  418. /* decode code lengths */
  419. for (n = 0; n < nlit + ndist;) {
  420. int sym = 0;
  421. sinfl_refill(&s);
  422. sym = sinfl_decode(&s, hlens, 7);
  423. switch (sym) {default: lens[n++] = (unsigned char)sym; break;
  424. case 16: for (i=3+sinfl_get(&s,2);i;i--,n++) lens[n]=lens[n-1]; break;
  425. case 17: for (i=3+sinfl_get(&s,3);i;i--,n++) lens[n]=0; break;
  426. case 18: for (i=11+sinfl_get(&s,7);i;i--,n++) lens[n]=0; break;}
  427. }
  428. /* build lit/dist tables */
  429. sinfl_build(s.lits, lens, 10, 15, nlit);
  430. sinfl_build(s.dsts, lens + nlit, 8, 15, ndist);
  431. state = blk;}
  432. } break;
  433. case blk: {
  434. /* decompress block */
  435. while (1) {
  436. int sym;
  437. sinfl_refill(&s);
  438. sym = sinfl_decode(&s, s.lits, 10);
  439. if (sym < 256) {
  440. /* literal */
  441. if (sinfl_unlikely(out >= oe)) {
  442. return (int)(out-o);
  443. }
  444. *out++ = (unsigned char)sym;
  445. sym = sinfl_decode(&s, s.lits, 10);
  446. if (sym < 256) {
  447. *out++ = (unsigned char)sym;
  448. continue;
  449. }
  450. }
  451. if (sinfl_unlikely(sym == 256)) {
  452. /* end of block */
  453. if (last) return (int)(out-o);
  454. state = hdr;
  455. break;
  456. }
  457. /* match */
  458. if (sym >= 286) {
  459. /* length codes 286 and 287 must not appear in compressed data */
  460. return (int)(out-o);
  461. }
  462. sym -= 257;
  463. {int len = sinfl__get(&s, lbits[sym]) + lbase[sym];
  464. int dsym = sinfl_decode(&s, s.dsts, 8);
  465. int offs = sinfl__get(&s, dbits[dsym]) + dbase[dsym];
  466. unsigned char *dst = out, *src = out - offs;
  467. if (sinfl_unlikely(offs > (int)(out-o))) {
  468. return (int)(out-o);
  469. }
  470. out = out + len;
  471. #ifndef SINFL_NO_SIMD
  472. if (sinfl_likely(oe - out >= 16 * 3)) {
  473. if (offs >= 16) {
  474. /* simd copy match */
  475. sinfl_copy128(&dst, &src);
  476. sinfl_copy128(&dst, &src);
  477. do sinfl_copy128(&dst, &src);
  478. while (dst < out);
  479. } else if (offs >= 8) {
  480. /* word copy match */
  481. sinfl_copy64(&dst, &src);
  482. sinfl_copy64(&dst, &src);
  483. do sinfl_copy64(&dst, &src);
  484. while (dst < out);
  485. } else if (offs == 1) {
  486. /* rle match copying */
  487. sinfl_char16 w = sinfl_char16_char(src[0]);
  488. dst = sinfl_write128(dst, w);
  489. dst = sinfl_write128(dst, w);
  490. do dst = sinfl_write128(dst, w);
  491. while (dst < out);
  492. } else {
  493. /* byte copy match */
  494. *dst++ = *src++;
  495. *dst++ = *src++;
  496. do *dst++ = *src++;
  497. while (dst < out);
  498. }
  499. }
  500. #else
  501. if (sinfl_likely(oe - out >= 3 * 8 - 3)) {
  502. if (offs >= 8) {
  503. /* word copy match */
  504. sinfl_copy64(&dst, &src);
  505. sinfl_copy64(&dst, &src);
  506. do sinfl_copy64(&dst, &src);
  507. while (dst < out);
  508. } else if (offs == 1) {
  509. /* rle match copying */
  510. unsigned int c = src[0];
  511. unsigned int hw = (c << 24u) | (c << 16u) | (c << 8u) | (unsigned)c;
  512. unsigned long long w = (unsigned long long)hw << 32llu | hw;
  513. dst = sinfl_write64(dst, w);
  514. dst = sinfl_write64(dst, w);
  515. do dst = sinfl_write64(dst, w);
  516. while (dst < out);
  517. } else {
  518. /* byte copy match */
  519. *dst++ = *src++;
  520. *dst++ = *src++;
  521. do *dst++ = *src++;
  522. while (dst < out);
  523. }
  524. }
  525. #endif
  526. else {
  527. *dst++ = *src++;
  528. *dst++ = *src++;
  529. do *dst++ = *src++;
  530. while (dst < out);
  531. }}
  532. }
  533. } break;}
  534. }
  535. return (int)(out-o);
  536. }
  537. extern int
  538. sinflate(void *out, int cap, const void *in, int size) {
  539. return sinfl_decompress((unsigned char*)out, cap, (const unsigned char*)in, size);
  540. }
  541. static unsigned
  542. sinfl_adler32(unsigned adler32, const unsigned char *in, int in_len) {
  543. const unsigned ADLER_MOD = 65521;
  544. unsigned s1 = adler32 & 0xffff;
  545. unsigned s2 = adler32 >> 16;
  546. unsigned blk_len, i;
  547. blk_len = in_len % 5552;
  548. while (in_len) {
  549. for (i=0; i + 7 < blk_len; i += 8) {
  550. s1 += in[0]; s2 += s1;
  551. s1 += in[1]; s2 += s1;
  552. s1 += in[2]; s2 += s1;
  553. s1 += in[3]; s2 += s1;
  554. s1 += in[4]; s2 += s1;
  555. s1 += in[5]; s2 += s1;
  556. s1 += in[6]; s2 += s1;
  557. s1 += in[7]; s2 += s1;
  558. in += 8;
  559. }
  560. for (; i < blk_len; ++i)
  561. s1 += *in++, s2 += s1;
  562. s1 %= ADLER_MOD; s2 %= ADLER_MOD;
  563. in_len -= blk_len;
  564. blk_len = 5552;
  565. } return (unsigned)(s2 << 16) + (unsigned)s1;
  566. }
  567. extern int
  568. zsinflate(void *out, int cap, const void *mem, int size) {
  569. const unsigned char *in = (const unsigned char*)mem;
  570. if (size >= 6) {
  571. const unsigned char *eob = in + size - 4;
  572. int n = sinfl_decompress((unsigned char*)out, cap, in + 2u, size);
  573. unsigned a = sinfl_adler32(1u, (unsigned char*)out, n);
  574. unsigned h = eob[0] << 24 | eob[1] << 16 | eob[2] << 8 | eob[3] << 0;
  575. return a == h ? n : -1;
  576. } else {
  577. return -1;
  578. }
  579. }
  580. #endif