xref: /freebsd-src/sys/contrib/zstd/examples/simple_compression.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 <string.h>    // strlen, strcat, memset
142b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
152b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
162b9c00cbSConrad Meyer 
compress_orDie(const char * fname,const char * oname)172b9c00cbSConrad Meyer static void compress_orDie(const char* fname, const char* oname)
182b9c00cbSConrad Meyer {
192b9c00cbSConrad Meyer     size_t fSize;
202b9c00cbSConrad Meyer     void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
212b9c00cbSConrad Meyer     size_t const cBuffSize = ZSTD_compressBound(fSize);
222b9c00cbSConrad Meyer     void* const cBuff = malloc_orDie(cBuffSize);
232b9c00cbSConrad Meyer 
242b9c00cbSConrad Meyer     /* Compress.
252b9c00cbSConrad Meyer      * If you are doing many compressions, you may want to reuse the context.
262b9c00cbSConrad Meyer      * See the multiple_simple_compression.c example.
272b9c00cbSConrad Meyer      */
282b9c00cbSConrad Meyer     size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
292b9c00cbSConrad Meyer     CHECK_ZSTD(cSize);
302b9c00cbSConrad Meyer 
312b9c00cbSConrad Meyer     saveFile_orDie(oname, cBuff, cSize);
322b9c00cbSConrad Meyer 
332b9c00cbSConrad Meyer     /* success */
342b9c00cbSConrad Meyer     printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
352b9c00cbSConrad Meyer 
362b9c00cbSConrad Meyer     free(fBuff);
372b9c00cbSConrad Meyer     free(cBuff);
382b9c00cbSConrad Meyer }
392b9c00cbSConrad Meyer 
createOutFilename_orDie(const char * filename)402b9c00cbSConrad Meyer static char* createOutFilename_orDie(const char* filename)
412b9c00cbSConrad Meyer {
422b9c00cbSConrad Meyer     size_t const inL = strlen(filename);
432b9c00cbSConrad Meyer     size_t const outL = inL + 5;
442b9c00cbSConrad Meyer     void* const outSpace = malloc_orDie(outL);
452b9c00cbSConrad Meyer     memset(outSpace, 0, outL);
462b9c00cbSConrad Meyer     strcat(outSpace, filename);
472b9c00cbSConrad Meyer     strcat(outSpace, ".zst");
482b9c00cbSConrad Meyer     return (char*)outSpace;
492b9c00cbSConrad Meyer }
502b9c00cbSConrad Meyer 
main(int argc,const char ** argv)512b9c00cbSConrad Meyer int main(int argc, const char** argv)
522b9c00cbSConrad Meyer {
532b9c00cbSConrad Meyer     const char* const exeName = argv[0];
542b9c00cbSConrad Meyer 
552b9c00cbSConrad Meyer     if (argc!=2) {
562b9c00cbSConrad Meyer         printf("wrong arguments\n");
572b9c00cbSConrad Meyer         printf("usage:\n");
582b9c00cbSConrad Meyer         printf("%s FILE\n", exeName);
592b9c00cbSConrad Meyer         return 1;
602b9c00cbSConrad Meyer     }
612b9c00cbSConrad Meyer 
622b9c00cbSConrad Meyer     const char* const inFilename = argv[1];
632b9c00cbSConrad Meyer 
642b9c00cbSConrad Meyer     char* const outFilename = createOutFilename_orDie(inFilename);
652b9c00cbSConrad Meyer     compress_orDie(inFilename, outFilename);
662b9c00cbSConrad Meyer     free(outFilename);
672b9c00cbSConrad Meyer     return 0;
682b9c00cbSConrad Meyer }
69