]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/zstd/enzstd.c
Raised copyright years
[tofuproxy.git] / cmd / zstd / enzstd.c
1 /*
2 enzstd -- .warc.zst compressor
3 Copyright (C) 2021-2022 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 #ifdef __FreeBSD__
26 #include "capsicum.c.in"
27 #endif // __FreeBSD__
28
29 int
30 main(int argc, char **argv)
31 {
32 #ifdef __FreeBSD__
33     capsicum_start();
34 #endif // __FreeBSD__
35     ZSTD_CCtx *ctx = ZSTD_createCCtx();
36     if (ctx == NULL) {
37         fputs("can not initialize ZSTD_createCCtx\n", stderr);
38         return EXIT_FAILURE;
39     }
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));
43         return EXIT_FAILURE;
44     }
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));
48         return EXIT_FAILURE;
49     }
50
51     char *src      = NULL;
52     char *dst      = NULL;
53     int rc         = EXIT_FAILURE;
54     size_t srcSize = 8;
55     src            = malloc(srcSize);
56     if (src == NULL) {
57         fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
58         goto Exit;
59     }
60     size_t dstSize     = 0;
61     size_t srcWantSize = 0;
62     size_t dstWantSize = 0;
63     size_t n           = 0;
64     for (;;) {
65         n = fread(src, 1, 8, stdin);
66         if (n < 8) {
67             if (feof(stdin)) {
68                 break;
69             }
70             perror("can not fread(stdin)");
71             goto Exit;
72         }
73         srcWantSize = (size_t)be64dec(src);
74         if (srcWantSize > srcSize) {
75             free(src);
76             srcSize = srcWantSize;
77             src     = malloc(srcSize);
78             if (src == NULL) {
79                 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
80                 goto Exit;
81             }
82         }
83         n = fread(src, 1, srcWantSize, stdin);
84         if (n < srcWantSize) {
85             fputs("insufficient data fed\n", stderr);
86             goto Exit;
87         }
88         dstWantSize = ZSTD_compressBound(srcWantSize);
89         if (dstWantSize > dstSize) {
90             free(dst);
91             dstSize = dstWantSize;
92             dst     = malloc(dstSize);
93             if (dst == NULL) {
94                 fprintf(stderr, "can not allocate memory: %zu\n", dstSize);
95                 goto Exit;
96             }
97         }
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));
101             goto Exit;
102         }
103         n = fwrite(dst, 1, zCode, stdout);
104         if (n < zCode) {
105             perror("can not fwrite(stdout)");
106             goto Exit;
107         }
108     }
109     rc = EXIT_SUCCESS;
110
111 Exit:
112     free(dst);
113     free(src);
114     ZSTD_freeCCtx(ctx);
115     return rc;
116 }