]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/zstd/enzstd.c
Use Capsicum if available
[tofuproxy.git] / cmd / zstd / 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 #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     int rc         = EXIT_FAILURE;
52     size_t srcSize = 8;
53     uint8_t *src   = malloc(srcSize);
54     if (src == NULL) {
55         fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
56         goto Exit;
57     };
58     size_t dstSize     = 0;
59     uint8_t *dst       = NULL;
60     size_t srcWantSize = 0;
61     size_t dstWantSize = 0;
62     size_t n           = 0;
63     for (;;) {
64         n = fread(src, 1, 8, stdin);
65         if (n < 8) {
66             if (feof(stdin)) {
67                 break;
68             };
69             perror("can not fread(stdin)");
70             goto Exit;
71         };
72         srcWantSize = (size_t)be64dec(src);
73         if (srcWantSize > srcSize) {
74             free(src);
75             srcSize = srcWantSize;
76             src     = malloc(srcSize);
77             if (src == NULL) {
78                 fprintf(stderr, "can not allocate memory: %zu\n", srcSize);
79                 goto Exit;
80             };
81         };
82         n = fread(src, 1, srcWantSize, stdin);
83         if (n < srcWantSize) {
84             fputs("insufficient data fed\n", stderr);
85             goto Exit;
86         };
87         dstWantSize = ZSTD_compressBound(srcWantSize);
88         if (dstWantSize > dstSize) {
89             free(dst);
90             dstSize = dstWantSize;
91             dst     = malloc(dstSize);
92             if (dst == NULL) {
93                 fprintf(stderr, "can not allocate memory: %zu\n", dstSize);
94                 goto Exit;
95             };
96         };
97         zCode = ZSTD_compress2(ctx, dst, dstSize, src, srcWantSize);
98         if (ZSTD_isError(zCode)) {
99             fprintf(stderr, "can not compress: %s\n", ZSTD_getErrorName(zCode));
100             goto Exit;
101         };
102         n = fwrite(dst, 1, zCode, stdout);
103         if (n < zCode) {
104             perror("can not fwrite(stdout)");
105             goto Exit;
106         };
107     };
108     rc = EXIT_SUCCESS;
109
110 Exit:
111     free(dst);
112     free(src);
113     ZSTD_freeCCtx(ctx);
114     return rc;
115 };