xref: /freebsd-src/sys/contrib/zstd/examples/simple_compression.c (revision 2b9c00cb6bd9392645dc8afca59cf57c42df4e2d)
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 <string.h>    // strlen, strcat, memset
14*2b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
15*2b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
16*2b9c00cbSConrad Meyer 
17*2b9c00cbSConrad Meyer static void compress_orDie(const char* fname, const char* oname)
18*2b9c00cbSConrad Meyer {
19*2b9c00cbSConrad Meyer     size_t fSize;
20*2b9c00cbSConrad Meyer     void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
21*2b9c00cbSConrad Meyer     size_t const cBuffSize = ZSTD_compressBound(fSize);
22*2b9c00cbSConrad Meyer     void* const cBuff = malloc_orDie(cBuffSize);
23*2b9c00cbSConrad Meyer 
24*2b9c00cbSConrad Meyer     /* Compress.
25*2b9c00cbSConrad Meyer      * If you are doing many compressions, you may want to reuse the context.
26*2b9c00cbSConrad Meyer      * See the multiple_simple_compression.c example.
27*2b9c00cbSConrad Meyer      */
28*2b9c00cbSConrad Meyer     size_t const cSize = ZSTD_compress(cBuff, cBuffSize, fBuff, fSize, 1);
29*2b9c00cbSConrad Meyer     CHECK_ZSTD(cSize);
30*2b9c00cbSConrad Meyer 
31*2b9c00cbSConrad Meyer     saveFile_orDie(oname, cBuff, cSize);
32*2b9c00cbSConrad Meyer 
33*2b9c00cbSConrad Meyer     /* success */
34*2b9c00cbSConrad Meyer     printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
35*2b9c00cbSConrad Meyer 
36*2b9c00cbSConrad Meyer     free(fBuff);
37*2b9c00cbSConrad Meyer     free(cBuff);
38*2b9c00cbSConrad Meyer }
39*2b9c00cbSConrad Meyer 
40*2b9c00cbSConrad Meyer static char* createOutFilename_orDie(const char* filename)
41*2b9c00cbSConrad Meyer {
42*2b9c00cbSConrad Meyer     size_t const inL = strlen(filename);
43*2b9c00cbSConrad Meyer     size_t const outL = inL + 5;
44*2b9c00cbSConrad Meyer     void* const outSpace = malloc_orDie(outL);
45*2b9c00cbSConrad Meyer     memset(outSpace, 0, outL);
46*2b9c00cbSConrad Meyer     strcat(outSpace, filename);
47*2b9c00cbSConrad Meyer     strcat(outSpace, ".zst");
48*2b9c00cbSConrad Meyer     return (char*)outSpace;
49*2b9c00cbSConrad Meyer }
50*2b9c00cbSConrad Meyer 
51*2b9c00cbSConrad Meyer int main(int argc, const char** argv)
52*2b9c00cbSConrad Meyer {
53*2b9c00cbSConrad Meyer     const char* const exeName = argv[0];
54*2b9c00cbSConrad Meyer 
55*2b9c00cbSConrad Meyer     if (argc!=2) {
56*2b9c00cbSConrad Meyer         printf("wrong arguments\n");
57*2b9c00cbSConrad Meyer         printf("usage:\n");
58*2b9c00cbSConrad Meyer         printf("%s FILE\n", exeName);
59*2b9c00cbSConrad Meyer         return 1;
60*2b9c00cbSConrad Meyer     }
61*2b9c00cbSConrad Meyer 
62*2b9c00cbSConrad Meyer     const char* const inFilename = argv[1];
63*2b9c00cbSConrad Meyer 
64*2b9c00cbSConrad Meyer     char* const outFilename = createOutFilename_orDie(inFilename);
65*2b9c00cbSConrad Meyer     compress_orDie(inFilename, outFilename);
66*2b9c00cbSConrad Meyer     free(outFilename);
67*2b9c00cbSConrad Meyer     return 0;
68*2b9c00cbSConrad Meyer }
69