1 // enzstd -- .warc.zst compressor
2 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, version 3 of the License.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 // GNU General Public License for more details.
13 // You should have received a copy of the GNU General Public License
14 // along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "capsicum.c.in"
27 main(int argc, char **argv)
32 ZSTD_CCtx *ctx = ZSTD_createCCtx();
34 fputs("can not initialize ZSTD_createCCtx\n", stderr);
37 size_t zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 1);
38 if (ZSTD_isError(zCode)) {
39 fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
42 zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel());
43 if (ZSTD_isError(zCode)) {
44 fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
48 unsigned char *src = NULL;
49 unsigned char *dst = NULL;
50 int rc = EXIT_FAILURE;
52 src = malloc(srcSize);
54 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
58 size_t srcWantSize = 0;
59 size_t dstWantSize = 0;
62 n = fread(src, 1, 8, stdin);
67 perror("can not fread(stdin)");
70 srcWantSize = ((uint64_t)(src[0]) << 56) | ((uint64_t)(src[1]) << 48) |
71 ((uint64_t)(src[2]) << 40) | ((uint64_t)(src[3]) << 32) |
72 ((uint64_t)(src[4]) << 24) | ((uint64_t)(src[5]) << 16) |
73 ((uint64_t)(src[6]) << 8) | (uint64_t)(src[7]);
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)");