xref: /freebsd-src/sys/contrib/zstd/examples/simple_decompression.c (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
12b9c00cbSConrad Meyer /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
32b9c00cbSConrad Meyer  * All rights reserved.
42b9c00cbSConrad Meyer  *
52b9c00cbSConrad Meyer  * This source code is licensed under both the BSD-style license (found in the
62b9c00cbSConrad Meyer  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
72b9c00cbSConrad Meyer  * in the COPYING file in the root directory of this source tree).
82b9c00cbSConrad Meyer  * You may select, at your option, one of the above-listed licenses.
92b9c00cbSConrad Meyer  */
102b9c00cbSConrad Meyer 
112b9c00cbSConrad Meyer #include <stdio.h>     // printf
122b9c00cbSConrad Meyer #include <stdlib.h>    // free
132b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
142b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
152b9c00cbSConrad Meyer 
decompress(const char * fname)162b9c00cbSConrad Meyer static void decompress(const char* fname)
172b9c00cbSConrad Meyer {
182b9c00cbSConrad Meyer     size_t cSize;
192b9c00cbSConrad Meyer     void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
202b9c00cbSConrad Meyer     /* Read the content size from the frame header. For simplicity we require
212b9c00cbSConrad Meyer      * that it is always present. By default, zstd will write the content size
222b9c00cbSConrad Meyer      * in the header when it is known. If you can't guarantee that the frame
232b9c00cbSConrad Meyer      * content size is always written into the header, either use streaming
242b9c00cbSConrad Meyer      * decompression, or ZSTD_decompressBound().
252b9c00cbSConrad Meyer      */
262b9c00cbSConrad Meyer     unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
272b9c00cbSConrad Meyer     CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
282b9c00cbSConrad Meyer     CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
292b9c00cbSConrad Meyer 
302b9c00cbSConrad Meyer     void* const rBuff = malloc_orDie((size_t)rSize);
312b9c00cbSConrad Meyer 
322b9c00cbSConrad Meyer     /* Decompress.
332b9c00cbSConrad Meyer      * If you are doing many decompressions, you may want to reuse the context
342b9c00cbSConrad Meyer      * and use ZSTD_decompressDCtx(). If you want to set advanced parameters,
352b9c00cbSConrad Meyer      * use ZSTD_DCtx_setParameter().
362b9c00cbSConrad Meyer      */
372b9c00cbSConrad Meyer     size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);
382b9c00cbSConrad Meyer     CHECK_ZSTD(dSize);
392b9c00cbSConrad Meyer     /* When zstd knows the content size, it will error if it doesn't match. */
402b9c00cbSConrad Meyer     CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
412b9c00cbSConrad Meyer 
422b9c00cbSConrad Meyer     /* success */
432b9c00cbSConrad Meyer     printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
442b9c00cbSConrad Meyer 
452b9c00cbSConrad Meyer     free(rBuff);
462b9c00cbSConrad Meyer     free(cBuff);
472b9c00cbSConrad Meyer }
482b9c00cbSConrad Meyer 
main(int argc,const char ** argv)492b9c00cbSConrad Meyer int main(int argc, const char** argv)
502b9c00cbSConrad Meyer {
512b9c00cbSConrad Meyer     const char* const exeName = argv[0];
522b9c00cbSConrad Meyer 
532b9c00cbSConrad Meyer     if (argc!=2) {
542b9c00cbSConrad Meyer         printf("wrong arguments\n");
552b9c00cbSConrad Meyer         printf("usage:\n");
562b9c00cbSConrad Meyer         printf("%s FILE\n", exeName);
572b9c00cbSConrad Meyer         return 1;
582b9c00cbSConrad Meyer     }
592b9c00cbSConrad Meyer 
602b9c00cbSConrad Meyer     decompress(argv[1]);
612b9c00cbSConrad Meyer 
622b9c00cbSConrad Meyer     printf("%s correctly decoded (in memory). \n", argv[1]);
632b9c00cbSConrad Meyer 
642b9c00cbSConrad Meyer     return 0;
652b9c00cbSConrad Meyer }
66