Path: blob/main/sys/contrib/zstd/examples/simple_decompression.c
48249 views
/*1* Copyright (c) Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/910#include <stdio.h> // printf11#include <stdlib.h> // free12#include <zstd.h> // presumes zstd library is installed13#include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()1415static void decompress(const char* fname)16{17size_t cSize;18void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);19/* Read the content size from the frame header. For simplicity we require20* that it is always present. By default, zstd will write the content size21* in the header when it is known. If you can't guarantee that the frame22* content size is always written into the header, either use streaming23* decompression, or ZSTD_decompressBound().24*/25unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);26CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);27CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);2829void* const rBuff = malloc_orDie((size_t)rSize);3031/* Decompress.32* If you are doing many decompressions, you may want to reuse the context33* and use ZSTD_decompressDCtx(). If you want to set advanced parameters,34* use ZSTD_DCtx_setParameter().35*/36size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);37CHECK_ZSTD(dSize);38/* When zstd knows the content size, it will error if it doesn't match. */39CHECK(dSize == rSize, "Impossible because zstd will check this condition!");4041/* success */42printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);4344free(rBuff);45free(cBuff);46}4748int main(int argc, const char** argv)49{50const char* const exeName = argv[0];5152if (argc!=2) {53printf("wrong arguments\n");54printf("usage:\n");55printf("%s FILE\n", exeName);56return 1;57}5859decompress(argv[1]);6061printf("%s correctly decoded (in memory). \n", argv[1]);6263return 0;64}656667