compress-utils 0.12.1

Unified interface to zstd, brotli, zlib, gzip, bzip2, lz4, xz/lzma and snappy compression, over a single C core.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/*
 * deflate_backend.h — shared zlib/gzip implementation for compress-utils.
 *
 * zlib and gzip are the same DEFLATE codec (the zlib library) differing only
 * in the wrapper written around the compressed data:
 *   - zlib wrapper (RFC 1950): windowBits 15
 *   - gzip wrapper (RFC 1952): windowBits 15 + 16 = 31
 *
 * The library behind this is zlib-ng in its zlib-compatible mode, so the calls
 * below are the classic zlib API. Its output differs from madler zlib at every
 * level (other match finders) but is the same on every CPU and SIMD path, and
 * every standard decoder reads it.
 *
 * Neither wire format carries the decompressed size (gzip's ISIZE trailer is
 * mod 2^32 and needs the whole stream), so decompress_size_hint always returns
 * CU_ERR_SIZE_UNKNOWN and the bindings fall back to streaming.
 *
 * Data after the end of the compressed stream depends on the wrapper:
 *   - gzip: another member (`cat a.gz b.gz`, pigz), decoded and appended as
 *     the gzip CLI does. Zero padding between or after members is skipped.
 *   - zlib: an error.
 *   - raw DEFLATE: ignored, since a raw stream cut from a container can be
 *     followed by the container's own bytes.
 * Single-stream mode (CU_DSTREAM_SINGLE) stops at the end of the first
 * stream/member for all three, keeping everything after it (including gzip
 * zero padding) as unused.
 *
 * This header is internal and included by exactly two .c files.
 */

#ifndef CU_DEFLATE_BACKEND_H
#define CU_DEFLATE_BACKEND_H

#include "algorithm_registry.h"
#include "compress_utils.h"
#include "utils/levels.h"
#include "utils/single_stream.h"

#include <zlib.h>

#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>

/* windowBits selecting the wrapper the zlib library writes/reads. */
#define CU_DFL_ZLIB_WBITS 15
#define CU_DFL_GZIP_WBITS 31

/* At the end of a gzip member, skip zero padding; returns whether another
 * member follows in strm's input. */
static int dfl_next_gzip_member(z_stream* strm) {
    while (strm->avail_in > 0 && *strm->next_in == 0) {
        strm->next_in++;
        strm->avail_in--;
    }
    return strm->avail_in > 0;
}

/* zlib/gzip/deflate: user 1..10 → native 1..7, 9, 9, 9.
 *
 * zlib-ng's levels are different algorithms from zlib's: 1 is the "quick"
 * strategy (fixed Huffman codes: about twice as fast, roughly 20% larger
 * output), 2 is "fast", 3-6 "medium", 7-9 "slow" (lazy matching). Native 8 is
 * skipped: it produces the same output as 7 within 0.1% and is slower. So user
 * levels 8-10 all run native 9, the highest effort, which is the same
 * algorithm and ratio as madler zlib's level 9. On text, native 9 comes out
 * about 0.5% larger than 7 (an upstream quirk); on binary data it is about 2%
 * smaller. */
static int dfl_native_level(int user_level) {
    int level = cu_clamp_level(user_level, 1, 10);
    return level <= 7 ? level : 9;
}

static cu_status_t dfl_map_error(int code, cu_status_t fallback) {
    if (code == Z_OK || code == Z_STREAM_END) return CU_OK;
    cu_set_last_errorf("zlib: code %d (%s)", code, zError(code));
    return fallback;
}

/* ============================================================================
 * One-shot
 * ============================================================================ */

static size_t dfl_compress_bound(size_t in_len, int window_bits) {
    uLong bound = compressBound((uLong)in_len);
    /* compressBound accounts for the zlib wrapper; the gzip wrapper is larger
     * (10-byte header + 8-byte trailer). Add margin so the bound stays a true
     * upper bound for gzip output too. */
    if (window_bits > 15) bound += 18;
    return bound;
}

static cu_status_t dfl_compress(
    const uint8_t* in, size_t in_len,
    uint8_t* out, size_t* out_len,
    int level, int window_bits
) {
    size_t needed = dfl_compress_bound(in_len, window_bits);
    if (*out_len < needed) {
        *out_len = needed;
        return CU_ERR_BUF_TOO_SMALL;
    }
    z_stream strm;
    memset(&strm, 0, sizeof(strm));
    /* deflateInit2 with windowBits 15 / memLevel 8 / default strategy produces
     * output identical to compress2(); windowBits 31 adds the gzip wrapper. */
    int r = deflateInit2(&strm, dfl_native_level(level), Z_DEFLATED,
                         window_bits, 8, Z_DEFAULT_STRATEGY);
    if (r != Z_OK) return dfl_map_error(r, CU_ERR_COMPRESSION);

    strm.next_in   = (Bytef*)in;
    strm.avail_in  = (uInt)in_len;
    strm.next_out  = out;
    strm.avail_out = (uInt)*out_len;

    r = deflate(&strm, Z_FINISH);
    if (r != Z_STREAM_END) {
        deflateEnd(&strm);
        return dfl_map_error(r, CU_ERR_COMPRESSION);
    }
    *out_len = (size_t)strm.total_out;
    deflateEnd(&strm);
    return CU_OK;
}

static cu_status_t dfl_decompress(
    const uint8_t* in, size_t in_len,
    uint8_t* out, size_t* out_len,
    int window_bits
) {
    if (in_len == 0) {
        cu_set_last_error("zlib: empty input");
        return CU_ERR_TRUNCATED;
    }
    z_stream strm;
    memset(&strm, 0, sizeof(strm));
    int r = inflateInit2(&strm, window_bits);
    if (r != Z_OK) return dfl_map_error(r, CU_ERR_DECOMPRESSION);

    strm.next_in   = (Bytef*)in;
    strm.avail_in  = (uInt)in_len;
    strm.next_out  = out;
    strm.avail_out = (uInt)*out_len;

    size_t cap_limit = cu_get_max_decompressed_size();
    cu_status_t ret;

    r = inflate(&strm, Z_FINISH);
    int trailing = 0;
    while (r == Z_STREAM_END && strm.avail_in > 0) {
        if (window_bits == CU_DFL_ZLIB_WBITS) {
            trailing = 1;
            break;
        }
        if (window_bits != CU_DFL_GZIP_WBITS || !dfl_next_gzip_member(&strm)) break;
        r = inflateReset(&strm);
        if (r == Z_OK) r = inflate(&strm, Z_FINISH);
    }
    /* inflateReset zeroes total_out, so measure from the output pointer. */
    size_t produced = (size_t)(strm.next_out - out);

    if (trailing) {
        cu_set_last_error("zlib: data after end of stream");
        ret = CU_ERR_DECOMPRESSION;
    } else if (r == Z_STREAM_END) {
        if (cap_limit > 0 && produced > cap_limit) {
            cu_set_last_errorf("zlib: decompressed size %zu exceeds cap %zu",
                               produced, cap_limit);
            ret = CU_ERR_SIZE_LIMIT;
        } else {
            *out_len = produced;
            ret = CU_OK;
        }
    } else if ((r == Z_BUF_ERROR || r == Z_OK) && strm.avail_out == 0) {
        /* Output exhausted; the wire format doesn't encode the size. */
        cu_set_last_error("zlib: output buffer too small (size not encoded in stream)");
        ret = CU_ERR_SIZE_UNKNOWN;
    } else if (r == Z_BUF_ERROR || r == Z_OK) {
        /* The output had room: the input ended before the stream did. */
        cu_set_last_error("zlib: truncated input");
        ret = CU_ERR_TRUNCATED;
    } else if (r == Z_DATA_ERROR) {
        cu_set_last_error("zlib: corrupted data");
        ret = CU_ERR_DECOMPRESSION;
    } else {
        ret = dfl_map_error(r, CU_ERR_DECOMPRESSION);
    }
    inflateEnd(&strm);
    return ret;
}

static cu_status_t dfl_decompress_size_hint(
    const uint8_t* in, size_t in_len, size_t* out_size
) {
    (void)in; (void)in_len; (void)out_size;
    /* zlib carries no size; gzip's ISIZE is mod 2^32 and needs the whole
     * stream — not a reliable cheap probe, so report unknown for both. */
    return CU_ERR_SIZE_UNKNOWN;
}

/* ============================================================================
 * Streaming (shared: state carries the z_stream, so pumps are wrapper-agnostic)
 * ============================================================================ */

typedef struct {
    z_stream strm;
    uint8_t* pending;      /* unconsumed input; live bytes are [pending_off, pending_len) */
    size_t   pending_off;  /* decompression only; compression keeps it 0 */
    size_t   pending_len;
    size_t   pending_cap;
    int      finishing;
    int      stream_end;
    int      kind;  /* 0 = compress, 1 = decompress */
    int      window_bits;
    int      single;       /* CU_DSTREAM_SINGLE: stop at the first stream end */
    cu_unused_t unused;    /* single: input received after the end */
} dfl_stream_state_t;

/* Appends to the live pending bytes, first moving them to the front. Draining
 * only advances pending_off, so each byte is moved at most once per append
 * rather than once per output page. */
static cu_status_t dfl_pending_append(dfl_stream_state_t* st, const uint8_t* src, size_t n) {
    if (n == 0) return CU_OK;
    if (st->pending_off > 0) {
        st->pending_len -= st->pending_off;
        memmove(st->pending, st->pending + st->pending_off, st->pending_len);
        st->pending_off = 0;
    }
    size_t need = st->pending_len + n;
    if (need > st->pending_cap) {
        size_t new_cap = st->pending_cap ? st->pending_cap * 2 : 16 * 1024;
        while (new_cap < need) new_cap *= 2;
        uint8_t* p = realloc(st->pending, new_cap);
        if (!p) {
            cu_set_last_error("zlib: out of memory");
            return CU_ERR_OOM;
        }
        st->pending = p;
        st->pending_cap = new_cap;
    }
    memcpy(st->pending + st->pending_len, src, n);
    st->pending_len += n;
    return CU_OK;
}

/* ---- Streaming compression ---- */

static cu_status_t dfl_cstream_create(int level, int window_bits, void** out_state) {
    dfl_stream_state_t* st = calloc(1, sizeof(*st));
    if (!st) { cu_set_last_error("zlib: oom"); return CU_ERR_OOM; }
    st->kind = 0;
    int r = deflateInit2(&st->strm, dfl_native_level(level), Z_DEFLATED,
                         window_bits, 8, Z_DEFAULT_STRATEGY);
    if (r != Z_OK) {
        cu_status_t s = dfl_map_error(r, CU_ERR_COMPRESSION);
        free(st);
        return s;
    }
    *out_state = st;
    return CU_OK;
}

static cu_status_t dfl_cstream_pump(dfl_stream_state_t* st, int flush_flag,
                                    uint8_t* out, size_t* out_len) {
    size_t cap = *out_len;
    size_t written = 0;
    int z_ret = Z_OK;

    /* Deflate pending input first. Without a flush this also runs with no
     * input, so a drain call collects output deflate still holds. */
    if (st->pending_len > 0 || flush_flag == Z_NO_FLUSH) {
        st->strm.next_in  = st->pending;
        st->strm.avail_in = (uInt)st->pending_len;
        st->strm.next_out  = out + written;
        st->strm.avail_out = (uInt)(cap - written);
        z_ret = deflate(&st->strm, Z_NO_FLUSH);
        size_t produced = (cap - written) - st->strm.avail_out;
        written += produced;
        if (z_ret != Z_OK && z_ret != Z_BUF_ERROR) {
            *out_len = written;
            return dfl_map_error(z_ret, CU_ERR_COMPRESSION);
        }
        size_t consumed = st->pending_len - st->strm.avail_in;
        if (st->strm.avail_in > 0 || st->strm.avail_out == 0) {
            /* Output filled: input may remain, and deflate may hold output. */
            if (st->strm.avail_in > 0) {
                memmove(st->pending, st->pending + consumed, st->strm.avail_in);
            }
            st->pending_len = st->strm.avail_in;
            *out_len = written;
            return CU_ERR_BUF_TOO_SMALL;
        }
        st->pending_len = 0;
    }

    /* No new input here — caller threaded any new input through pending. */
    if (flush_flag != Z_NO_FLUSH) {
        for (;;) {
            st->strm.next_in  = NULL;
            st->strm.avail_in = 0;
            st->strm.next_out  = out + written;
            st->strm.avail_out = (uInt)(cap - written);
            z_ret = deflate(&st->strm, flush_flag);
            size_t produced = (cap - written) - st->strm.avail_out;
            written += produced;
            if (z_ret == Z_STREAM_END) {
                st->stream_end = 1;
                *out_len = written;
                return CU_OK;
            }
            if (z_ret != Z_OK && z_ret != Z_BUF_ERROR) {
                *out_len = written;
                return dfl_map_error(z_ret, CU_ERR_COMPRESSION);
            }
            if (st->strm.avail_out == 0) {
                /* Out of room: more to write, unless a flush just filled the
                 * buffer exactly. Asking again would append a second flush
                 * marker, and with a buffer under 7 bytes the flush would
                 * never end (see deflate() in zlib.h), so ask what is left. */
                unsigned pending = 0;
                int bits = 0;
                if (flush_flag == Z_FINISH || z_ret != Z_OK ||
                    deflatePending(&st->strm, &pending, &bits) != Z_OK ||
                    pending > 0 || bits > 0) {
                    *out_len = written;
                    return CU_ERR_BUF_TOO_SMALL;
                }
            }
            /* Z_OK / Z_BUF_ERROR with room remaining (or a flush that ended
             * exactly at the end of the buffer) means deflate is done
             * producing for this flush_flag. */
            break;
        }
    }

    *out_len = written;
    return CU_OK;
}

static cu_status_t dfl_cstream_write(
    void* state, const uint8_t* in, size_t in_len,
    uint8_t* out, size_t* out_len
) {
    dfl_stream_state_t* st = (dfl_stream_state_t*)state;
    if (st->finishing) {
        cu_set_last_error("zlib: write after finish");
        return CU_ERR_STREAM_STATE;
    }
    cu_status_t s = dfl_pending_append(st, in, in_len);
    if (s != CU_OK) return s;
    return dfl_cstream_pump(st, Z_NO_FLUSH, out, out_len);
}

static cu_status_t dfl_cstream_finish(
    void* state, uint8_t* out, size_t* out_len
) {
    dfl_stream_state_t* st = (dfl_stream_state_t*)state;
    st->finishing = 1;
    if (st->stream_end) {
        *out_len = 0;
        return CU_OK;
    }
    return dfl_cstream_pump(st, Z_FINISH, out, out_len);
}

/* Z_SYNC_FLUSH: byte-aligns the output and ends with an empty stored block,
 * so everything written so far can be inflated; the stream stays open. */
static cu_status_t dfl_cstream_flush(
    void* state, uint8_t* out, size_t* out_len
) {
    dfl_stream_state_t* st = (dfl_stream_state_t*)state;
    if (st->finishing) {
        cu_set_last_error("zlib: flush after finish");
        return CU_ERR_STREAM_STATE;
    }
    return dfl_cstream_pump(st, Z_SYNC_FLUSH, out, out_len);
}

static void dfl_stream_destroy(void* state);  /* fwd — shared by both dirs */

/* ---- Streaming decompression ---- */

static cu_status_t dfl_dstream_create(int window_bits, void** out_state) {
    dfl_stream_state_t* st = calloc(1, sizeof(*st));
    if (!st) { cu_set_last_error("zlib: oom"); return CU_ERR_OOM; }
    st->kind = 1;
    st->window_bits = window_bits;
    int r = inflateInit2(&st->strm, window_bits);
    if (r != Z_OK) {
        cu_status_t s = dfl_map_error(r, CU_ERR_DECOMPRESSION);
        free(st);
        return s;
    }
    *out_state = st;
    return CU_OK;
}

static cu_status_t dfl_dstream_create_single(int window_bits, void** out_state) {
    cu_status_t s = dfl_dstream_create(window_bits, out_state);
    if (s == CU_OK) ((dfl_stream_state_t*)*out_state)->single = 1;
    return s;
}

/* Inflates `in` into `out`; returns the unconsumed input length in *left.
 * A full `out` returns BUF_TOO_SMALL even when all input was consumed, since
 * inflate may still hold output; the drain call then runs inflate again.
 * zlib counts in uInt, so input and output are fed in pieces of at most
 * UINT_MAX bytes. In single mode it stops at the end of the stream, leaving
 * the rest in *left; inflate reports Z_STREAM_END only once all output is
 * written. */
static cu_status_t dfl_inflate(dfl_stream_state_t* st, const uint8_t* in, size_t in_len,
                               uint8_t* out, size_t* out_len, size_t* left) {
    size_t cap = *out_len > UINT_MAX ? UINT_MAX : *out_len;
    size_t rest = in_len;
    st->strm.next_in   = (Bytef*)in;
    st->strm.avail_in  = 0;
    st->strm.next_out  = out;
    st->strm.avail_out = (uInt)cap;
    cu_status_t s = CU_OK;

    for (;;) {
        if (st->strm.avail_in == 0 && rest > 0) {
            st->strm.avail_in = rest > UINT_MAX ? UINT_MAX : (uInt)rest;
            rest -= st->strm.avail_in;
        }
        if (st->stream_end) {
            if (st->single) break;
            if (st->strm.avail_in == 0) break;
            if (st->window_bits == CU_DFL_ZLIB_WBITS) {
                cu_set_last_error("zlib: data after end of stream");
                s = CU_ERR_DECOMPRESSION;
                break;
            }
            if (st->window_bits != CU_DFL_GZIP_WBITS) {
                st->strm.avail_in = 0;  /* raw DEFLATE: not ours */
                rest = 0;
                break;
            }
            if (!dfl_next_gzip_member(&st->strm)) continue;  /* refill, or end */
            int r = inflateReset(&st->strm);
            if (r != Z_OK) { s = dfl_map_error(r, CU_ERR_DECOMPRESSION); break; }
            st->stream_end = 0;
        }
        int z_ret = inflate(&st->strm, Z_NO_FLUSH);
        if (z_ret == Z_STREAM_END) {
            st->stream_end = 1;
            continue;
        }
        if (z_ret != Z_OK && z_ret != Z_BUF_ERROR) {
            s = dfl_map_error(z_ret, CU_ERR_DECOMPRESSION);
            break;
        }
        if (st->strm.avail_out == 0) { s = CU_ERR_BUF_TOO_SMALL; break; }
        if (st->strm.avail_in == 0 && rest == 0) break;  /* needs more input */
        /* No progress despite input and output room: never expected from
         * zlib, but fail rather than drop the input or spin. */
        if (z_ret == Z_BUF_ERROR && st->strm.avail_in > 0) {
            s = dfl_map_error(z_ret, CU_ERR_DECOMPRESSION);
            break;
        }
    }

    *out_len = cap - st->strm.avail_out;
    *left = st->strm.avail_in + rest;
    return s;
}

/* Decodes retained input first, then `in`; keeps whatever is left unconsumed
 * when the output fills. */
static cu_status_t dfl_dstream_pump(dfl_stream_state_t* st, const uint8_t* in, size_t in_len,
                                    uint8_t* out, size_t* out_len) {
    size_t left = 0;
    cu_status_t s;
    if (st->pending_off < st->pending_len) {
        s = dfl_pending_append(st, in, in_len);
        if (s != CU_OK) return s;
        size_t live = st->pending_len - st->pending_off;
        s = dfl_inflate(st, st->pending + st->pending_off, live, out, out_len, &left);
        if (s == CU_ERR_BUF_TOO_SMALL) {
            st->pending_off += live - left;
            return s;
        }
        if (s == CU_OK && st->single && st->stream_end) {
            s = cu_unused_append(&st->unused, st->pending + st->pending_len - left, left);
        }
        st->pending_off = st->pending_len = 0;
        return s;
    }
    s = dfl_inflate(st, in, in_len, out, out_len, &left);
    if (s == CU_ERR_BUF_TOO_SMALL && left > 0) {
        cu_status_t a = dfl_pending_append(st, in + (in_len - left), left);
        if (a != CU_OK) return a;
    } else if (s == CU_OK && st->single && st->stream_end && left > 0) {
        s = cu_unused_append(&st->unused, in + (in_len - left), left);
    }
    return s;
}

static cu_status_t dfl_dstream_write(
    void* state, const uint8_t* in, size_t in_len,
    uint8_t* out, size_t* out_len
) {
    dfl_stream_state_t* st = (dfl_stream_state_t*)state;
    /* Single mode past the end: inflate wrote all output before reporting it. */
    if (st->single && st->stream_end) {
        *out_len = 0;
        return CU_OK;
    }
    if (st->stream_end && in_len > 0 && st->window_bits == CU_DFL_ZLIB_WBITS) {
        cu_set_last_error("zlib: data after end of stream");
        return CU_ERR_DECOMPRESSION;
    }
    return dfl_dstream_pump(st, in, in_len, out, out_len);
}

static cu_status_t dfl_dstream_finish(
    void* state, uint8_t* out, size_t* out_len
) {
    dfl_stream_state_t* st = (dfl_stream_state_t*)state;
    cu_status_t s = dfl_dstream_pump(st, NULL, 0, out, out_len);
    if (s != CU_OK) return s;
    if (!st->stream_end) {
        cu_set_last_error("zlib: truncated stream at finish");
        return CU_ERR_TRUNCATED;
    }
    return CU_OK;
}

/* ---- Shared teardown (both directions) ---- */

static void dfl_stream_destroy(void* state) {
    dfl_stream_state_t* st = (dfl_stream_state_t*)state;
    if (!st) return;
    if (st->kind == 0) {
        deflateEnd(&st->strm);
    } else {
        inflateEnd(&st->strm);
    }
    free(st->pending);
    cu_unused_free(&st->unused);
    free(st);
}

static int dfl_dstream_end(const void* state, const uint8_t** unused, size_t* unused_len) {
    const dfl_stream_state_t* st = (const dfl_stream_state_t*)state;
    if (!st->single || !st->stream_end) return 0;
    cu_unused_get(&st->unused, unused, unused_len);
    return 1;
}

#endif  /* CU_DEFLATE_BACKEND_H */