X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=cmd%2Fzstd%2Funzstd.c;h=b7b311488e7a63f136b09a888d1c365dea30ecbd;hb=decb4bfb406665dde1233e9936fee3edfe3e81da;hp=dda5aa4afcb872486e9a95c7fa94d9090b03b597;hpb=d7426fe5729581c19bd51e08b963eae83c94d1fa;p=tofuproxy.git diff --git a/cmd/zstd/unzstd.c b/cmd/zstd/unzstd.c index dda5aa4..b7b3114 100644 --- a/cmd/zstd/unzstd.c +++ b/cmd/zstd/unzstd.c @@ -1,6 +1,6 @@ /* unzstd -- .warc.zst decompressor -Copyright (C) 2021 Sergey Matveev +Copyright (C) 2021-2022 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -41,7 +41,7 @@ main(int argc, char **argv) #ifdef __FreeBSD__ if ((fdOff != NULL) && (caph_limit_stream(3, CAPH_WRITE)) != 0) { errx(EX_OSERR, "can not caph_limit_stream(3)"); - }; + } capsicum_start(); #endif // __FreeBSD__ @@ -49,7 +49,7 @@ main(int argc, char **argv) if (ctx == NULL) { fputs("can not initialize ZSTD_DCtx\n", stderr); return 1; - }; + } int rc = EXIT_FAILURE; char *bufIn = NULL; char *bufOut = NULL; @@ -58,13 +58,13 @@ main(int argc, char **argv) if (bufIn == NULL) { fputs("no memory\n", stderr); goto Exit; - }; + } const size_t bufOutSize = ZSTD_DStreamOutSize(); bufOut = malloc(bufOutSize); if (bufOut == NULL) { fputs("no memory\n", stderr); goto Exit; - }; + } unsigned long long bufSize = 0; @@ -87,8 +87,8 @@ ReadAgain: } else { perror("can not fread(FILE)"); goto Exit; - }; - }; + } + } if (n >= 8 && le32dec(bufIn) == 0x184D2A5D) { // dictionary frame size_t dictSize = (size_t)le32dec(bufIn + 4); @@ -96,7 +96,7 @@ ReadAgain: if (dict == NULL) { fprintf(stderr, "insufficient memory for dictionary: %zu\n", dictSize); goto Exit; - }; + } const size_t alreadyRead = n - 8; memcpy(dict, bufIn + 8, alreadyRead); errno = 0; @@ -105,12 +105,12 @@ ReadAgain: perror("can not read dictionary data"); free(dict); goto Exit; - }; + } offset = dictSize + 8; offsetPrev = offset; if (fdOff != NULL) { fprintf(fdOff, "%zu\t0\n", offset); - }; + } uint32_t hdr = le32dec(dict); switch (hdr) { case ZSTD_MAGIC_DICTIONARY: @@ -122,7 +122,7 @@ ReadAgain: "can not load dictionary: %s\n", ZSTD_getErrorName(zCode)); goto Exit; - }; + } goto ReadAgain; case ZSTD_MAGICNUMBER: bufSize = ZSTD_getFrameContentSize(dict, dictSize); @@ -132,14 +132,14 @@ ReadAgain: fprintf(stderr, "can not determine dictionary's size\n"); free(dict); goto Exit; - }; + } char *buf = malloc(bufSize); if (buf == NULL) { fprintf( stderr, "insufficient memory for dictionary: %llu\n", bufSize); free(dict); goto Exit; - }; + } zCode = ZSTD_decompress(buf, bufSize, dict, dictSize); free(dict); if (ZSTD_isError(zCode)) { @@ -149,7 +149,7 @@ ReadAgain: ZSTD_getErrorName(zCode)); free(buf); goto Exit; - }; + } zCode = ZSTD_DCtx_loadDictionary(ctx, buf, zCode); free(buf); if ((zCode != 0) && (ZSTD_isError(zCode))) { @@ -158,14 +158,14 @@ ReadAgain: "can not load dictionary: %s\n", ZSTD_getErrorName(zCode)); goto Exit; - }; + } goto ReadAgain; default: fprintf(stderr, "unknown dictionary header\n"); free(dict); goto Exit; - }; - }; + } + } isEmpty = false; bIn.size = n; bIn.pos = 0; @@ -176,49 +176,49 @@ ReadAgain: if ((zCode != 0) && (ZSTD_isError(zCode))) { fprintf(stderr, "can not decompress: %s\n", ZSTD_getErrorName(zCode)); goto Exit; - }; + } n = fwrite(bufOut, 1, bOut.pos, stdout); if (n != bOut.pos) { perror("can not fwrite(stdout)"); goto Exit; - }; + } written += n; if (zCode == 0) { offset += bIn.pos; if (fdOff != NULL) { fprintf(fdOff, "%zu\t%zu\n", offset - offsetPrev, written); - }; + } offsetPrev = offset + bIn.pos; written = 0; - }; - }; + } + } if (lastBlock) { break; - }; + } offset += bIn.pos; - }; + } if (isEmpty) { fputs("empty input\n", stderr); goto Exit; - }; + } if (zCode != 0) { fprintf(stderr, "unfinished decompression: %s\n", ZSTD_getErrorName(zCode)); goto Exit; - }; + } rc = EXIT_SUCCESS; Exit: if (bufOut != NULL) { free(bufOut); - }; + } if (bufIn != NULL) { free(bufIn); - }; + } ZSTD_freeDCtx(ctx); if ((fdOff != NULL) && (fclose(fdOff) != 0)) { perror("can not fclose(4)"); return EXIT_FAILURE; - }; + } return rc; -}; +}