1604e653cem/*-
2604e653cem * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3604e653cem *
4604e653cem * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
5604e653cem *
6604e653cem * Redistribution and use in source and binary forms, with or without
7604e653cem * modification, are permitted provided that the following conditions
8604e653cem * are met:
9604e653cem * 1. Redistributions of source code must retain the above copyright
10604e653cem *    notice, this list of conditions and the following disclaimer.
11604e653cem * 2. Redistributions in binary form must reproduce the above copyright
12604e653cem *    notice, this list of conditions and the following disclaimer in the
13604e653cem *    documentation and/or other materials provided with the distribution.
14604e653cem *
15604e653cem * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16604e653cem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17604e653cem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18604e653cem * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19604e653cem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20604e653cem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21604e653cem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22604e653cem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23604e653cem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24604e653cem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25604e653cem * SUCH DAMAGE.
26604e653cem */
27604e653cem
28604e653cem#include <sys/cdefs.h>
29604e653cem__FBSDID("$FreeBSD$");
30604e653cem
31604e653cem#include <err.h>
32604e653cem#include <limits.h>
33604e653cem#include <stddef.h>
34604e653cem#include <stdint.h>
35604e653cem
36604e653cem#include <zstd.h>
37604e653cem
38604e653cem#include "mkuzip.h"
39604e653cem#include "mkuz_blk.h"
40604e653cem#include "mkuz_zstd.h"
41604e653cem
42604e653cemsize_t
43604e653cemmkuz_zstd_cbound(size_t blksz)
44604e653cem{
45604e653cem	return (ZSTD_compressBound(blksz));
46604e653cem}
47604e653cem
48604e653cemvoid *
49604e653cemmkuz_zstd_init(int *comp_level)
50604e653cem{
51604e653cem	ZSTD_CCtx *cctx;
52604e653cem	size_t rc;
53604e653cem
54604e653cem	/* Default chosen for near-parity with mkuzip zlib default. */
55604e653cem	if (*comp_level == USE_DEFAULT_LEVEL)
56604e653cem		*comp_level = 9;
57604e653cem	if (*comp_level < ZSTD_minCLevel() || *comp_level == 0 ||
58604e653cem	    *comp_level > ZSTD_maxCLevel())
59604e653cem		errx(1, "provided compression level %d is invalid",
60604e653cem		    *comp_level);
61604e653cem
62604e653cem	cctx = ZSTD_createCCtx();
63604e653cem	if (cctx == NULL)
64604e653cem		errx(1, "could not allocate Zstd context");
65604e653cem
66604e653cem	rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel,
67604e653cem	    *comp_level);
68604e653cem	if (ZSTD_isError(rc))
69604e653cem		errx(1, "Could not set zstd compression level %d: %s",
70604e653cem		    *comp_level, ZSTD_getErrorName(rc));
71604e653cem
72604e653cem	rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
73604e653cem	if (ZSTD_isError(rc))
74604e653cem		errx(1, "Could not enable zstd checksum: %s",
75604e653cem		    ZSTD_getErrorName(rc));
76604e653cem
77604e653cem	return (cctx);
78604e653cem}
79604e653cem
80604e653cemvoid
81604e653cemmkuz_zstd_compress(void *p, const struct mkuz_blk *iblk, struct mkuz_blk *oblk)
82604e653cem{
83604e653cem	ZSTD_CCtx *cctx;
84604e653cem	size_t rc;
85604e653cem
86604e653cem	cctx = p;
87604e653cem
88604e653cem	rc = ZSTD_compress2(cctx, oblk->data, oblk->alen, iblk->data,
89604e653cem	    iblk->info.len);
90604e653cem	if (ZSTD_isError(rc))
91604e653cem		errx(1, "could not compress data: ZSTD_compress2: %s",
92604e653cem		    ZSTD_getErrorName(rc));
93604e653cem
94604e653cem	oblk->info.len = rc;
95604e653cem}
96