2 unzstd -- .warc.zst decompressor
3 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
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.
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.
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/>.
18 // https://iipc.github.io/warc-specifications/specifications/warc-zstd/
26 #include <sys/endian.h>
31 #include "capsicum.c.in"
32 #include <capsicum_helpers.h>
38 main(int argc, char **argv)
40 FILE *fdOff = fdopen(3, "wb");
42 if ((fdOff != NULL) && (caph_limit_stream(3, CAPH_WRITE)) != 0) {
43 errx(EX_OSERR, "can not caph_limit_stream(3)");
48 ZSTD_DCtx *ctx = ZSTD_createDCtx();
50 fputs("can not initialize ZSTD_DCtx\n", stderr);
53 int rc = EXIT_FAILURE;
56 const size_t bufInSize = ZSTD_DStreamInSize();
57 bufIn = malloc(bufInSize);
59 fputs("no memory\n", stderr);
62 const size_t bufOutSize = ZSTD_DStreamOutSize();
63 bufOut = malloc(bufOutSize);
65 fputs("no memory\n", stderr);
69 unsigned long long bufSize = 0;
71 ZSTD_inBuffer bIn = {bufIn, 0, 0};
72 ZSTD_outBuffer bOut = {bufOut, 0, 0};
75 bool lastBlock = false;
79 size_t offsetPrev = 0;
83 n = fread(bufIn, 1, bufInSize, stdin);
88 perror("can not fread(FILE)");
92 if (n >= 8 && le32dec(bufIn) == 0x184D2A5D) {
94 size_t dictSize = (size_t)le32dec(bufIn + 4);
95 char *dict = malloc(dictSize);
97 fprintf(stderr, "insufficient memory for dictionary: %zu\n", dictSize);
100 const size_t alreadyRead = n - 8;
101 memcpy(dict, bufIn + 8, alreadyRead);
103 n = fread(dict + alreadyRead, 1, dictSize - alreadyRead, stdin);
104 if (n != dictSize - alreadyRead) {
105 perror("can not read dictionary data");
109 offset = dictSize + 8;
112 fprintf(fdOff, "%zu\t0\n", offset);
114 uint32_t hdr = le32dec(dict);
116 case ZSTD_MAGIC_DICTIONARY:
117 zCode = ZSTD_DCtx_loadDictionary(ctx, dict, dictSize);
119 if ((zCode != 0) && (ZSTD_isError(zCode))) {
122 "can not load dictionary: %s\n",
123 ZSTD_getErrorName(zCode));
127 case ZSTD_MAGICNUMBER:
128 bufSize = ZSTD_getFrameContentSize(dict, dictSize);
130 case ZSTD_CONTENTSIZE_UNKNOWN:
131 case ZSTD_CONTENTSIZE_ERROR:
132 fprintf(stderr, "can not determine dictionary's size\n");
136 char *buf = malloc(bufSize);
139 stderr, "insufficient memory for dictionary: %llu\n", bufSize);
143 zCode = ZSTD_decompress(buf, bufSize, dict, dictSize);
145 if (ZSTD_isError(zCode)) {
148 "can not decompress dictionary: %s\n",
149 ZSTD_getErrorName(zCode));
153 zCode = ZSTD_DCtx_loadDictionary(ctx, buf, zCode);
155 if ((zCode != 0) && (ZSTD_isError(zCode))) {
158 "can not load dictionary: %s\n",
159 ZSTD_getErrorName(zCode));
164 fprintf(stderr, "unknown dictionary header\n");
172 while (bIn.pos < bIn.size) {
173 bOut.size = bufOutSize;
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));
180 n = fwrite(bufOut, 1, bOut.pos, stdout);
182 perror("can not fwrite(stdout)");
189 fprintf(fdOff, "%zu\t%zu\n", offset - offsetPrev, written);
191 offsetPrev = offset + bIn.pos;
202 fputs("empty input\n", stderr);
206 fprintf(stderr, "unfinished decompression: %s\n", ZSTD_getErrorName(zCode));
212 if (bufOut != NULL) {
219 if ((fdOff != NULL) && (fclose(fdOff) != 0)) {
220 perror("can not fclose(4)");