]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/enzstd/enzstd.c
Various refactoring
[tofuproxy.git] / cmd / enzstd / enzstd.c
1 /*
2 enzstd -- .warc.zst compressor
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
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.
8
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.
13
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/>.
16 */
17
18 #include <inttypes.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <sys/endian.h>
22
23 #include <zstd.h>
24
25 int
26 main(int argc, char **argv)
27 {
28     ZSTD_CCtx *ctx = ZSTD_createCCtx();
29     if (ctx == NULL) {
30         fputs("can not initialize ZSTD_createCCtx\n", stderr);
31         return EXIT_FAILURE;
32     };
33     size_t zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 1);
34     if (ZSTD_isError(zCode)) {
35         fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
36         return EXIT_FAILURE;
37     };
38     zCode = ZSTD_CCtx_setParameter(ctx, ZSTD_c_compressionLevel, ZSTD_maxCLevel());
39     if (ZSTD_isError(zCode)) {
40         fprintf(stderr, "can not setParameter: %s\n", ZSTD_getErrorName(zCode));
41         return EXIT_FAILURE;
42     };
43
44     size_t srcSize = 8;
45     uint8_t *src   = malloc(srcSize);
46     if (src == NULL) {
47         fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
48         return EXIT_FAILURE;
49     };
50     size_t dstSize     = 0;
51     uint8_t *dst       = NULL;
52     size_t srcWantSize = 0;
53     size_t dstWantSize = 0;
54     size_t n           = 0;
55     for (;;) {
56         n = fread(src, 1, 8, stdin);
57         if (n < 8) {
58             if (feof(stdin)) {
59                 break;
60             };
61             perror("can not fread(stdin)");
62             return EXIT_FAILURE;
63         };
64         srcWantSize = (size_t)be64dec(src);
65         if (srcWantSize > srcSize) {
66             free(src);
67             srcSize = srcWantSize;
68             src     = malloc(srcSize);
69             if (src == NULL) {
70                 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
71                 return EXIT_FAILURE;
72             };
73         };
74         n = fread(src, 1, srcWantSize, stdin);
75         if (n < srcWantSize) {
76             fputs("insufficient data fed\n", stderr);
77             return EXIT_FAILURE;
78         };
79         dstWantSize = ZSTD_compressBound(srcWantSize);
80         if (dstWantSize > dstSize) {
81             free(dst);
82             dstSize = dstWantSize;
83             dst     = malloc(dstSize);
84             if (dst == NULL) {
85                 fprintf(stderr, "can not allocate memory: %zu\n", dstSize);
86                 return EXIT_FAILURE;
87             };
88         };
89         zCode = ZSTD_compress2(ctx, dst, dstSize, src, srcWantSize);
90         if (ZSTD_isError(zCode)) {
91             fprintf(stderr, "can not compress: %s\n", ZSTD_getErrorName(zCode));
92             return EXIT_FAILURE;
93         };
94         n = fwrite(dst, 1, zCode, stdout);
95         if (n < zCode) {
96             perror("can not fwrite(stdout)");
97             return EXIT_FAILURE;
98         };
99     };
100     free(dst);
101     free(src);
102     ZSTD_freeCCtx(ctx);
103     return EXIT_SUCCESS;
104 };