]> Sergey Matveev's repositories - tofuproxy.git/blob - cmd/zstd/unzstd.c
Raised copyright years
[tofuproxy.git] / cmd / zstd / unzstd.c
1 /*
2 unzstd -- .warc.zst decompressor
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 // https://iipc.github.io/warc-specifications/specifications/warc-zstd/
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <stdbool.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/endian.h>
27
28 #include <zstd.h>
29
30 #ifdef __FreeBSD__
31 #include "capsicum.c.in"
32 #include <capsicum_helpers.h>
33 #include <err.h>
34 #include <sysexits.h>
35 #endif // __FreeBSD__
36
37 int
38 main(int argc, char **argv)
39 {
40     FILE *fdOff = fdopen(3, "wb");
41 #ifdef __FreeBSD__
42     if ((fdOff != NULL) && (caph_limit_stream(3, CAPH_WRITE)) != 0) {
43         errx(EX_OSERR, "can not caph_limit_stream(3)");
44     }
45     capsicum_start();
46 #endif // __FreeBSD__
47
48     ZSTD_DCtx *ctx = ZSTD_createDCtx();
49     if (ctx == NULL) {
50         fputs("can not initialize ZSTD_DCtx\n", stderr);
51         return 1;
52     }
53     int rc                 = EXIT_FAILURE;
54     char *bufIn            = NULL;
55     char *bufOut           = NULL;
56     const size_t bufInSize = ZSTD_DStreamInSize();
57     bufIn                  = malloc(bufInSize);
58     if (bufIn == NULL) {
59         fputs("no memory\n", stderr);
60         goto Exit;
61     }
62     const size_t bufOutSize = ZSTD_DStreamOutSize();
63     bufOut                  = malloc(bufOutSize);
64     if (bufOut == NULL) {
65         fputs("no memory\n", stderr);
66         goto Exit;
67     }
68
69     unsigned long long bufSize = 0;
70
71     ZSTD_inBuffer bIn   = {bufIn, 0, 0};
72     ZSTD_outBuffer bOut = {bufOut, 0, 0};
73
74     bool isEmpty      = true;
75     bool lastBlock    = false;
76     size_t n          = 0;
77     size_t written    = 0;
78     size_t offset     = 0;
79     size_t offsetPrev = 0;
80     size_t zCode      = 0;
81 ReadAgain:
82     for (;;) {
83         n = fread(bufIn, 1, bufInSize, stdin);
84         if (n != bufInSize) {
85             if (feof(stdin)) {
86                 lastBlock = true;
87             } else {
88                 perror("can not fread(FILE)");
89                 goto Exit;
90             }
91         }
92         if (n >= 8 && le32dec(bufIn) == 0x184D2A5D) {
93             // dictionary frame
94             size_t dictSize = (size_t)le32dec(bufIn + 4);
95             char *dict      = malloc(dictSize);
96             if (dict == NULL) {
97                 fprintf(stderr, "insufficient memory for dictionary: %zu\n", dictSize);
98                 goto Exit;
99             }
100             const size_t alreadyRead = n - 8;
101             memcpy(dict, bufIn + 8, alreadyRead);
102             errno = 0;
103             n     = fread(dict + alreadyRead, 1, dictSize - alreadyRead, stdin);
104             if (n != dictSize - alreadyRead) {
105                 perror("can not read dictionary data");
106                 free(dict);
107                 goto Exit;
108             }
109             offset     = dictSize + 8;
110             offsetPrev = offset;
111             if (fdOff != NULL) {
112                 fprintf(fdOff, "%zu\t0\n", offset);
113             }
114             uint32_t hdr = le32dec(dict);
115             switch (hdr) {
116             case ZSTD_MAGIC_DICTIONARY:
117                 zCode = ZSTD_DCtx_loadDictionary(ctx, dict, dictSize);
118                 free(dict);
119                 if ((zCode != 0) && (ZSTD_isError(zCode))) {
120                     fprintf(
121                         stderr,
122                         "can not load dictionary: %s\n",
123                         ZSTD_getErrorName(zCode));
124                     goto Exit;
125                 }
126                 goto ReadAgain;
127             case ZSTD_MAGICNUMBER:
128                 bufSize = ZSTD_getFrameContentSize(dict, dictSize);
129                 switch (bufSize) {
130                 case ZSTD_CONTENTSIZE_UNKNOWN:
131                 case ZSTD_CONTENTSIZE_ERROR:
132                     fprintf(stderr, "can not determine dictionary's size\n");
133                     free(dict);
134                     goto Exit;
135                 }
136                 char *buf = malloc(bufSize);
137                 if (buf == NULL) {
138                     fprintf(
139                         stderr, "insufficient memory for dictionary: %llu\n", bufSize);
140                     free(dict);
141                     goto Exit;
142                 }
143                 zCode = ZSTD_decompress(buf, bufSize, dict, dictSize);
144                 free(dict);
145                 if (ZSTD_isError(zCode)) {
146                     fprintf(
147                         stderr,
148                         "can not decompress dictionary: %s\n",
149                         ZSTD_getErrorName(zCode));
150                     free(buf);
151                     goto Exit;
152                 }
153                 zCode = ZSTD_DCtx_loadDictionary(ctx, buf, zCode);
154                 free(buf);
155                 if ((zCode != 0) && (ZSTD_isError(zCode))) {
156                     fprintf(
157                         stderr,
158                         "can not load dictionary: %s\n",
159                         ZSTD_getErrorName(zCode));
160                     goto Exit;
161                 }
162                 goto ReadAgain;
163             default:
164                 fprintf(stderr, "unknown dictionary header\n");
165                 free(dict);
166                 goto Exit;
167             }
168         }
169         isEmpty  = false;
170         bIn.size = n;
171         bIn.pos  = 0;
172         while (bIn.pos < bIn.size) {
173             bOut.size = bufOutSize;
174             bOut.pos  = 0;
175             zCode     = ZSTD_decompressStream(ctx, &bOut, &bIn);
176             if ((zCode != 0) && (ZSTD_isError(zCode))) {
177                 fprintf(stderr, "can not decompress: %s\n", ZSTD_getErrorName(zCode));
178                 goto Exit;
179             }
180             n = fwrite(bufOut, 1, bOut.pos, stdout);
181             if (n != bOut.pos) {
182                 perror("can not fwrite(stdout)");
183                 goto Exit;
184             }
185             written += n;
186             if (zCode == 0) {
187                 offset += bIn.pos;
188                 if (fdOff != NULL) {
189                     fprintf(fdOff, "%zu\t%zu\n", offset - offsetPrev, written);
190                 }
191                 offsetPrev = offset + bIn.pos;
192                 written    = 0;
193             }
194         }
195         if (lastBlock) {
196             break;
197         }
198         offset += bIn.pos;
199     }
200
201     if (isEmpty) {
202         fputs("empty input\n", stderr);
203         goto Exit;
204     }
205     if (zCode != 0) {
206         fprintf(stderr, "unfinished decompression: %s\n", ZSTD_getErrorName(zCode));
207         goto Exit;
208     }
209     rc = EXIT_SUCCESS;
210
211 Exit:
212     if (bufOut != NULL) {
213         free(bufOut);
214     }
215     if (bufIn != NULL) {
216         free(bufIn);
217     }
218     ZSTD_freeDCtx(ctx);
219     if ((fdOff != NULL) && (fclose(fdOff) != 0)) {
220         perror("can not fclose(4)");
221         return EXIT_FAILURE;
222     }
223     return rc;
224 }