xref: /freebsd/usr.bin/mkuzip/mkuz_zstd.c (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2019 Conrad Meyer <cem@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #include <err.h>
30 #include <limits.h>
31 #include <stddef.h>
32 #include <stdint.h>
33 
34 #include <zstd.h>
35 
36 #include "mkuzip.h"
37 #include "mkuz_blk.h"
38 #include "mkuz_zstd.h"
39 
40 size_t
41 mkuz_zstd_cbound(size_t blksz)
42 {
43 	return (ZSTD_compressBound(blksz));
44 }
45 
46 void *
47 mkuz_zstd_init(int *comp_level)
48 {
49 	ZSTD_CCtx *cctx;
50 	size_t rc;
51 
52 	/* Default chosen for near-parity with mkuzip zlib default. */
53 	if (*comp_level == USE_DEFAULT_LEVEL)
54 		*comp_level = 9;
55 	if (*comp_level < ZSTD_minCLevel() || *comp_level == 0 ||
56 	    *comp_level > ZSTD_maxCLevel())
57 		errx(1, "provided compression level %d is invalid",
58 		    *comp_level);
59 
60 	cctx = ZSTD_createCCtx();
61 	if (cctx == NULL)
62 		errx(1, "could not allocate Zstd context");
63 
64 	rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel,
65 	    *comp_level);
66 	if (ZSTD_isError(rc))
67 		errx(1, "Could not set zstd compression level %d: %s",
68 		    *comp_level, ZSTD_getErrorName(rc));
69 
70 	rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
71 	if (ZSTD_isError(rc))
72 		errx(1, "Could not enable zstd checksum: %s",
73 		    ZSTD_getErrorName(rc));
74 
75 	return (cctx);
76 }
77 
78 void
79 mkuz_zstd_compress(void *p, const struct mkuz_blk *iblk, struct mkuz_blk *oblk)
80 {
81 	ZSTD_CCtx *cctx;
82 	size_t rc;
83 
84 	cctx = p;
85 
86 	rc = ZSTD_compress2(cctx, oblk->data, oblk->alen, iblk->data,
87 	    iblk->info.len);
88 	if (ZSTD_isError(rc))
89 		errx(1, "could not compress data: ZSTD_compress2: %s",
90 		    ZSTD_getErrorName(rc));
91 
92 	oblk->info.len = rc;
93 }
94