2 enzstd -- .warc.zst compressor
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/endian.h>
26 #include "capsicum.c.in"
30 main(int argc, char **argv)
35 ZSTD_CCtx *ctx = ZSTD_createCCtx();
37 fputs("can not initialize ZSTD_createCCtx\n", stderr);
40 size_t zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 1);
41 if (ZSTD_isError(zCode)) {
42 fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
45 zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel());
46 if (ZSTD_isError(zCode)) {
47 fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
53 int rc = EXIT_FAILURE;
55 src = malloc(srcSize);
57 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
61 size_t srcWantSize = 0;
62 size_t dstWantSize = 0;
65 n = fread(src, 1, 8, stdin);
70 perror("can not fread(stdin)");
73 srcWantSize = (size_t)be64dec(src);
74 if (srcWantSize > srcSize) {
76 srcSize = srcWantSize;
77 src = malloc(srcSize);
79 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
83 n = fread(src, 1, srcWantSize, stdin);
84 if (n < srcWantSize) {
85 fputs("insufficient data fed\n", stderr);
88 dstWantSize = ZSTD_compressBound(srcWantSize);
89 if (dstWantSize > dstSize) {
91 dstSize = dstWantSize;
92 dst = malloc(dstSize);
94 fprintf(stderr, "can not allocate memory: %zu\n", dstSize);
98 zCode = ZSTD_compress2(ctx, dst, dstSize, src, srcWantSize);
99 if (ZSTD_isError(zCode)) {
100 fprintf(stderr, "can not compress: %s\n", ZSTD_getErrorName(zCode));
103 n = fwrite(dst, 1, zCode, stdout);
105 perror("can not fwrite(stdout)");