2 unzstd -- .warc.zst decompressor
3 Copyright (C) 2021 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));
128 case ZSTD_MAGICNUMBER:
129 bufSize = ZSTD_getFrameContentSize(dict, dictSize);
131 case ZSTD_CONTENTSIZE_UNKNOWN:
132 case ZSTD_CONTENTSIZE_ERROR:
133 fprintf(stderr, "can not determine dictionary's size\n");
137 char *buf = malloc(bufSize);
140 stderr, "insufficient memory for dictionary: %llu\n", bufSize);
144 zCode = ZSTD_decompress(buf, bufSize, dict, dictSize);
146 if (ZSTD_isError(zCode)) {
149 "can not decompress dictionary: %s\n",
150 ZSTD_getErrorName(zCode));
154 zCode = ZSTD_DCtx_loadDictionary(ctx, buf, zCode);
156 if ((zCode != 0) && (ZSTD_isError(zCode))) {
159 "can not load dictionary: %s\n",
160 ZSTD_getErrorName(zCode));
166 fprintf(stderr, "unknown dictionary header\n");
174 while (bIn.pos < bIn.size) {
175 bOut.size = bufOutSize;
177 zCode = ZSTD_decompressStream(ctx, &bOut, &bIn);
178 if ((zCode != 0) && (ZSTD_isError(zCode))) {
179 fprintf(stderr, "can not decompress: %s\n", ZSTD_getErrorName(zCode));
182 n = fwrite(bufOut, 1, bOut.pos, stdout);
184 perror("can not fwrite(stdout)");
191 fprintf(fdOff, "%zu\t%zu\n", offset - offsetPrev, written);
193 offsetPrev = offset + bIn.pos;
204 fputs("empty input\n", stderr);
208 fprintf(stderr, "unfinished decompression: %s\n", ZSTD_getErrorName(zCode));
214 if (bufOut != NULL) {
221 if ((fdOff != NULL) && (fclose(fdOff) != 0)) {
222 perror("can not fclose(4)");