Debian Codegraph @

src/external/sinfl.h

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