2 enzstd -- .warc.zst compressor
3 Copyright (C) 2021-2023 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/>.
25 #include "capsicum.c.in"
29 main(int argc, char **argv)
34 ZSTD_CCtx *ctx = ZSTD_createCCtx();
36 fputs("can not initialize ZSTD_createCCtx\n", stderr);
39 size_t zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 1);
40 if (ZSTD_isError(zCode)) {
41 fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
44 zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel());
45 if (ZSTD_isError(zCode)) {
46 fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
52 int rc = EXIT_FAILURE;
54 src = malloc(srcSize);
56 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
60 size_t srcWantSize = 0;
61 size_t dstWantSize = 0;
64 n = fread(src, 1, 8, stdin);
69 perror("can not fread(stdin)");
72 srcWantSize = ((uint64_t)(src[0]) << 56) | ((uint64_t)(src[1]) << 48) |
73 ((uint64_t)(src[2]) << 40) | ((uint64_t)(src[3]) << 32) |
74 ((uint64_t)(src[4]) << 24) | ((uint64_t)(src[5]) << 16) |
75 ((uint64_t)(src[6]) << 8) | (uint64_t)(src[7]);
76 if (srcWantSize > srcSize) {
78 srcSize = srcWantSize;
79 src = malloc(srcSize);
81 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
85 n = fread(src, 1, srcWantSize, stdin);
86 if (n < srcWantSize) {
87 fputs("insufficient data fed\n", stderr);
90 dstWantSize = ZSTD_compressBound(srcWantSize);
91 if (dstWantSize > dstSize) {
93 dstSize = dstWantSize;
94 dst = malloc(dstSize);
96 fprintf(stderr, "can not allocate memory: %zu\n", dstSize);
100 zCode = ZSTD_compress2(ctx, dst, dstSize, src, srcWantSize);
101 if (ZSTD_isError(zCode)) {
102 fprintf(stderr, "can not compress: %s\n", ZSTD_getErrorName(zCode));
105 n = fwrite(dst, 1, zCode, stdout);
107 perror("can not fwrite(stdout)");