1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2019 Conrad Meyer <[email protected]>
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 __FBSDID("$FreeBSD$");
30
31 #include <err.h>
32 #include <limits.h>
33 #include <stddef.h>
34 #include <stdint.h>
35
36 #include <zstd.h>
37
38 #include "mkuzip.h"
39 #include "mkuz_blk.h"
40 #include "mkuz_zstd.h"
41
42 size_t
mkuz_zstd_cbound(size_t blksz)43 mkuz_zstd_cbound(size_t blksz)
44 {
45 return (ZSTD_compressBound(blksz));
46 }
47
48 void *
mkuz_zstd_init(int * comp_level)49 mkuz_zstd_init(int *comp_level)
50 {
51 ZSTD_CCtx *cctx;
52 size_t rc;
53
54 /* Default chosen for near-parity with mkuzip zlib default. */
55 if (*comp_level == USE_DEFAULT_LEVEL)
56 *comp_level = 9;
57 if (*comp_level < ZSTD_minCLevel() || *comp_level == 0 ||
58 *comp_level > ZSTD_maxCLevel())
59 errx(1, "provided compression level %d is invalid",
60 *comp_level);
61
62 cctx = ZSTD_createCCtx();
63 if (cctx == NULL)
64 errx(1, "could not allocate Zstd context");
65
66 rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel,
67 *comp_level);
68 if (ZSTD_isError(rc))
69 errx(1, "Could not set zstd compression level %d: %s",
70 *comp_level, ZSTD_getErrorName(rc));
71
72 rc = ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1);
73 if (ZSTD_isError(rc))
74 errx(1, "Could not enable zstd checksum: %s",
75 ZSTD_getErrorName(rc));
76
77 return (cctx);
78 }
79
80 void
mkuz_zstd_compress(void * p,const struct mkuz_blk * iblk,struct mkuz_blk * oblk)81 mkuz_zstd_compress(void *p, const struct mkuz_blk *iblk, struct mkuz_blk *oblk)
82 {
83 ZSTD_CCtx *cctx;
84 size_t rc;
85
86 cctx = p;
87
88 rc = ZSTD_compress2(cctx, oblk->data, oblk->alen, iblk->data,
89 iblk->info.len);
90 if (ZSTD_isError(rc))
91 errx(1, "could not compress data: ZSTD_compress2: %s",
92 ZSTD_getErrorName(rc));
93
94 oblk->info.len = rc;
95 }
96