2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
40 #include "mkuz_zstd.h"
43 mkuz_zstd_cbound(size_t blksz)
45 return (ZSTD_compressBound(blksz));
49 mkuz_zstd_init(int *comp_level)
54 /* Default chosen for near-parity with mkuzip zlib default. */
55 if (*comp_level == USE_DEFAULT_LEVEL)
57 if (*comp_level < ZSTD_minCLevel() || *comp_level == 0 ||
58 *comp_level > ZSTD_maxCLevel())
59 errx(1, "provided compression level %d is invalid",
62 cctx = ZSTD_createCCtx();
64 errx(1, "could not allocate Zstd context");
66 rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel,
69 errx(1, "Could not set zstd compression level %d: %s",
70 *comp_level, ZSTD_getErrorName(rc));
72 rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
74 errx(1, "Could not enable zstd checksum: %s",
75 ZSTD_getErrorName(rc));
81 mkuz_zstd_compress(void *p, const struct mkuz_blk *iblk, struct mkuz_blk *oblk)
88 rc = ZSTD_compress2(cctx, oblk->data, oblk->alen, iblk->data,
91 errx(1, "could not compress data: ZSTD_compress2: %s",
92 ZSTD_getErrorName(rc));