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 main(int argc, char **argv)
33 ZSTD_DCtx *ctx = ZSTD_createDCtx();
35 fputs("can not initialize ZSTD_DCtx\n", stderr);
38 FILE *fdOff = fdopen(3, "wb");
39 int rc = EXIT_FAILURE;
40 uint8_t *bufIn = NULL;
41 uint8_t *bufOut = NULL;
42 const size_t bufInSize = ZSTD_DStreamInSize();
43 bufIn = malloc(bufInSize);
45 fputs("no memory\n", stderr);
48 const size_t bufOutSize = ZSTD_DStreamOutSize();
49 bufOut = malloc(bufOutSize);
51 fputs("no memory\n", stderr);
55 unsigned long long bufSize = 0;
57 ZSTD_inBuffer bIn = {bufIn, 0, 0};
58 ZSTD_outBuffer bOut = {bufOut, 0, 0};
61 bool lastBlock = false;
65 size_t offsetPrev = 0;
69 n = fread(bufIn, 1, bufInSize, stdin);
74 perror("can not fread(FILE)");
78 if (n >= 8 && le32dec(bufIn) == 0x184D2A5D) {
80 size_t dictSize = (size_t)le32dec(bufIn + 4);
81 uint8_t *dict = malloc(dictSize);
83 fprintf(stderr, "insufficient memory for dictionary: %zu\n", dictSize);
86 const size_t alreadyRead = n - 8;
87 memcpy(dict, bufIn + 8, alreadyRead);
89 n = fread(dict + alreadyRead, 1, dictSize - alreadyRead, stdin);
90 if (n != dictSize - alreadyRead) {
91 perror("can not read dictionary data");
95 offset = dictSize + 8;
98 fprintf(fdOff, "%zu\t0\n", offset);
100 uint32_t hdr = le32dec(dict);
102 case ZSTD_MAGIC_DICTIONARY:
103 zCode = ZSTD_DCtx_loadDictionary(ctx, dict, dictSize);
105 if ((zCode != 0) && (ZSTD_isError(zCode))) {
108 "can not load dictionary: %s\n",
109 ZSTD_getErrorName(zCode));
114 case ZSTD_MAGICNUMBER:
115 bufSize = ZSTD_getFrameContentSize(dict, dictSize);
117 case ZSTD_CONTENTSIZE_UNKNOWN:
118 case ZSTD_CONTENTSIZE_ERROR:
119 fprintf(stderr, "can not determine dictionary's size\n");
123 uint8_t *buf = malloc(bufSize);
126 stderr, "insufficient memory for dictionary: %llu\n", bufSize);
130 zCode = ZSTD_decompress(buf, bufSize, dict, dictSize);
132 if (ZSTD_isError(zCode)) {
135 "can not decompress dictionary: %s\n",
136 ZSTD_getErrorName(zCode));
140 zCode = ZSTD_DCtx_loadDictionary(ctx, buf, zCode);
142 if ((zCode != 0) && (ZSTD_isError(zCode))) {
145 "can not load dictionary: %s\n",
146 ZSTD_getErrorName(zCode));
152 fprintf(stderr, "unknown dictionary header\n");
160 while (bIn.pos < bIn.size) {
161 bOut.size = bufOutSize;
163 zCode = ZSTD_decompressStream(ctx, &bOut, &bIn);
164 if ((zCode != 0) && (ZSTD_isError(zCode))) {
165 fprintf(stderr, "can not decompress: %s\n", ZSTD_getErrorName(zCode));
168 n = fwrite(bufOut, 1, bOut.pos, stdout);
170 perror("can not fwrite(stdout)");
177 fprintf(fdOff, "%zu\t%zu\n", offset - offsetPrev, written);
179 offsetPrev = offset + bIn.pos;
190 fputs("empty input\n", stderr);
194 fprintf(stderr, "unfinished decompression: %s\n", ZSTD_getErrorName(zCode));
200 if (bufOut != NULL) {
207 if ((fdOff != NULL) && (fclose(fdOff) != 0)) {
208 perror("can not fclose(4)");