1*2b9c00cbSConrad Meyer /* 2*2b9c00cbSConrad Meyer * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. 3*2b9c00cbSConrad Meyer * All rights reserved. 4*2b9c00cbSConrad Meyer * 5*2b9c00cbSConrad Meyer * This source code is licensed under both the BSD-style license (found in the 6*2b9c00cbSConrad Meyer * LICENSE file in the root directory of this source tree) and the GPLv2 (found 7*2b9c00cbSConrad Meyer * in the COPYING file in the root directory of this source tree). 8*2b9c00cbSConrad Meyer * You may select, at your option, one of the above-listed licenses. 9*2b9c00cbSConrad Meyer */ 10*2b9c00cbSConrad Meyer 11*2b9c00cbSConrad Meyer #include <stdio.h> // printf 12*2b9c00cbSConrad Meyer #include <stdlib.h> // free 13*2b9c00cbSConrad Meyer #include <zstd.h> // presumes zstd library is installed 14*2b9c00cbSConrad Meyer #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD() 15*2b9c00cbSConrad Meyer 16*2b9c00cbSConrad Meyer static void decompress(const char* fname) 17*2b9c00cbSConrad Meyer { 18*2b9c00cbSConrad Meyer size_t cSize; 19*2b9c00cbSConrad Meyer void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize); 20*2b9c00cbSConrad Meyer /* Read the content size from the frame header. For simplicity we require 21*2b9c00cbSConrad Meyer * that it is always present. By default, zstd will write the content size 22*2b9c00cbSConrad Meyer * in the header when it is known. If you can't guarantee that the frame 23*2b9c00cbSConrad Meyer * content size is always written into the header, either use streaming 24*2b9c00cbSConrad Meyer * decompression, or ZSTD_decompressBound(). 25*2b9c00cbSConrad Meyer */ 26*2b9c00cbSConrad Meyer unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize); 27*2b9c00cbSConrad Meyer CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname); 28*2b9c00cbSConrad Meyer CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname); 29*2b9c00cbSConrad Meyer 30*2b9c00cbSConrad Meyer void* const rBuff = malloc_orDie((size_t)rSize); 31*2b9c00cbSConrad Meyer 32*2b9c00cbSConrad Meyer /* Decompress. 33*2b9c00cbSConrad Meyer * If you are doing many decompressions, you may want to reuse the context 34*2b9c00cbSConrad Meyer * and use ZSTD_decompressDCtx(). If you want to set advanced parameters, 35*2b9c00cbSConrad Meyer * use ZSTD_DCtx_setParameter(). 36*2b9c00cbSConrad Meyer */ 37*2b9c00cbSConrad Meyer size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize); 38*2b9c00cbSConrad Meyer CHECK_ZSTD(dSize); 39*2b9c00cbSConrad Meyer /* When zstd knows the content size, it will error if it doesn't match. */ 40*2b9c00cbSConrad Meyer CHECK(dSize == rSize, "Impossible because zstd will check this condition!"); 41*2b9c00cbSConrad Meyer 42*2b9c00cbSConrad Meyer /* success */ 43*2b9c00cbSConrad Meyer printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize); 44*2b9c00cbSConrad Meyer 45*2b9c00cbSConrad Meyer free(rBuff); 46*2b9c00cbSConrad Meyer free(cBuff); 47*2b9c00cbSConrad Meyer } 48*2b9c00cbSConrad Meyer 49*2b9c00cbSConrad Meyer int main(int argc, const char** argv) 50*2b9c00cbSConrad Meyer { 51*2b9c00cbSConrad Meyer const char* const exeName = argv[0]; 52*2b9c00cbSConrad Meyer 53*2b9c00cbSConrad Meyer if (argc!=2) { 54*2b9c00cbSConrad Meyer printf("wrong arguments\n"); 55*2b9c00cbSConrad Meyer printf("usage:\n"); 56*2b9c00cbSConrad Meyer printf("%s FILE\n", exeName); 57*2b9c00cbSConrad Meyer return 1; 58*2b9c00cbSConrad Meyer } 59*2b9c00cbSConrad Meyer 60*2b9c00cbSConrad Meyer decompress(argv[1]); 61*2b9c00cbSConrad Meyer 62*2b9c00cbSConrad Meyer printf("%s correctly decoded (in memory). \n", argv[1]); 63*2b9c00cbSConrad Meyer 64*2b9c00cbSConrad Meyer return 0; 65*2b9c00cbSConrad Meyer } 66