xref: /freebsd-src/sys/contrib/zstd/examples/streaming_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 
12*2b9c00cbSConrad Meyer #include <stdio.h>     // printf
13*2b9c00cbSConrad Meyer #include <stdlib.h>    // free
14*2b9c00cbSConrad Meyer #include <string.h>    // memset, strcat, strlen
15*2b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
16*2b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
17*2b9c00cbSConrad Meyer 
18*2b9c00cbSConrad Meyer 
19*2b9c00cbSConrad Meyer static void compressFile_orDie(const char* fname, const char* outName, int cLevel)
20*2b9c00cbSConrad Meyer {
21*2b9c00cbSConrad Meyer     /* Open the input and output files. */
22*2b9c00cbSConrad Meyer     FILE* const fin  = fopen_orDie(fname, "rb");
23*2b9c00cbSConrad Meyer     FILE* const fout = fopen_orDie(outName, "wb");
24*2b9c00cbSConrad Meyer     /* Create the input and output buffers.
25*2b9c00cbSConrad Meyer      * They may be any size, but we recommend using these functions to size them.
26*2b9c00cbSConrad Meyer      * Performance will only suffer significantly for very tiny buffers.
27*2b9c00cbSConrad Meyer      */
28*2b9c00cbSConrad Meyer     size_t const buffInSize = ZSTD_CStreamInSize();
29*2b9c00cbSConrad Meyer     void*  const buffIn  = malloc_orDie(buffInSize);
30*2b9c00cbSConrad Meyer     size_t const buffOutSize = ZSTD_CStreamOutSize();
31*2b9c00cbSConrad Meyer     void*  const buffOut = malloc_orDie(buffOutSize);
32*2b9c00cbSConrad Meyer 
33*2b9c00cbSConrad Meyer     /* Create the context. */
34*2b9c00cbSConrad Meyer     ZSTD_CCtx* const cctx = ZSTD_createCCtx();
35*2b9c00cbSConrad Meyer     CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
36*2b9c00cbSConrad Meyer 
37*2b9c00cbSConrad Meyer     /* Set any parameters you want.
38*2b9c00cbSConrad Meyer      * Here we set the compression level, and enable the checksum.
39*2b9c00cbSConrad Meyer      */
40*2b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
41*2b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
42*2b9c00cbSConrad Meyer 
43*2b9c00cbSConrad Meyer     /* This loop read from the input file, compresses that entire chunk,
44*2b9c00cbSConrad Meyer      * and writes all output produced to the output file.
45*2b9c00cbSConrad Meyer      */
46*2b9c00cbSConrad Meyer     size_t const toRead = buffInSize;
47*2b9c00cbSConrad Meyer     size_t read;
48*2b9c00cbSConrad Meyer     while ((read = fread_orDie(buffIn, toRead, fin))) {
49*2b9c00cbSConrad Meyer         /* Select the flush mode.
50*2b9c00cbSConrad Meyer          * If the read may not be finished (read == toRead) we use
51*2b9c00cbSConrad Meyer          * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
52*2b9c00cbSConrad Meyer          * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
53*2b9c00cbSConrad Meyer          * since it knows it is compressing the entire source in one pass.
54*2b9c00cbSConrad Meyer          */
55*2b9c00cbSConrad Meyer         int const lastChunk = (read < toRead);
56*2b9c00cbSConrad Meyer         ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
57*2b9c00cbSConrad Meyer         /* Set the input buffer to what we just read.
58*2b9c00cbSConrad Meyer          * We compress until the input buffer is empty, each time flushing the
59*2b9c00cbSConrad Meyer          * output.
60*2b9c00cbSConrad Meyer          */
61*2b9c00cbSConrad Meyer         ZSTD_inBuffer input = { buffIn, read, 0 };
62*2b9c00cbSConrad Meyer         int finished;
63*2b9c00cbSConrad Meyer         do {
64*2b9c00cbSConrad Meyer             /* Compress into the output buffer and write all of the output to
65*2b9c00cbSConrad Meyer              * the file so we can reuse the buffer next iteration.
66*2b9c00cbSConrad Meyer              */
67*2b9c00cbSConrad Meyer             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
68*2b9c00cbSConrad Meyer             size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
69*2b9c00cbSConrad Meyer             CHECK_ZSTD(remaining);
70*2b9c00cbSConrad Meyer             fwrite_orDie(buffOut, output.pos, fout);
71*2b9c00cbSConrad Meyer             /* If we're on the last chunk we're finished when zstd returns 0,
72*2b9c00cbSConrad Meyer              * which means its consumed all the input AND finished the frame.
73*2b9c00cbSConrad Meyer              * Otherwise, we're finished when we've consumed all the input.
74*2b9c00cbSConrad Meyer              */
75*2b9c00cbSConrad Meyer             finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
76*2b9c00cbSConrad Meyer         } while (!finished);
77*2b9c00cbSConrad Meyer         CHECK(input.pos == input.size,
78*2b9c00cbSConrad Meyer               "Impossible: zstd only returns 0 when the input is completely consumed!");
79*2b9c00cbSConrad Meyer     }
80*2b9c00cbSConrad Meyer 
81*2b9c00cbSConrad Meyer     ZSTD_freeCCtx(cctx);
82*2b9c00cbSConrad Meyer     fclose_orDie(fout);
83*2b9c00cbSConrad Meyer     fclose_orDie(fin);
84*2b9c00cbSConrad Meyer     free(buffIn);
85*2b9c00cbSConrad Meyer     free(buffOut);
86*2b9c00cbSConrad Meyer }
87*2b9c00cbSConrad Meyer 
88*2b9c00cbSConrad Meyer 
89*2b9c00cbSConrad Meyer static char* createOutFilename_orDie(const char* filename)
90*2b9c00cbSConrad Meyer {
91*2b9c00cbSConrad Meyer     size_t const inL = strlen(filename);
92*2b9c00cbSConrad Meyer     size_t const outL = inL + 5;
93*2b9c00cbSConrad Meyer     void* const outSpace = malloc_orDie(outL);
94*2b9c00cbSConrad Meyer     memset(outSpace, 0, outL);
95*2b9c00cbSConrad Meyer     strcat(outSpace, filename);
96*2b9c00cbSConrad Meyer     strcat(outSpace, ".zst");
97*2b9c00cbSConrad Meyer     return (char*)outSpace;
98*2b9c00cbSConrad Meyer }
99*2b9c00cbSConrad Meyer 
100*2b9c00cbSConrad Meyer int main(int argc, const char** argv)
101*2b9c00cbSConrad Meyer {
102*2b9c00cbSConrad Meyer     const char* const exeName = argv[0];
103*2b9c00cbSConrad Meyer 
104*2b9c00cbSConrad Meyer     if (argc!=2) {
105*2b9c00cbSConrad Meyer         printf("wrong arguments\n");
106*2b9c00cbSConrad Meyer         printf("usage:\n");
107*2b9c00cbSConrad Meyer         printf("%s FILE\n", exeName);
108*2b9c00cbSConrad Meyer         return 1;
109*2b9c00cbSConrad Meyer     }
110*2b9c00cbSConrad Meyer 
111*2b9c00cbSConrad Meyer     const char* const inFilename = argv[1];
112*2b9c00cbSConrad Meyer 
113*2b9c00cbSConrad Meyer     char* const outFilename = createOutFilename_orDie(inFilename);
114*2b9c00cbSConrad Meyer     compressFile_orDie(inFilename, outFilename, 1);
115*2b9c00cbSConrad Meyer 
116*2b9c00cbSConrad Meyer     free(outFilename);   /* not strictly required, since program execution stops there,
117*2b9c00cbSConrad Meyer                           * but some static analyzer main complain otherwise */
118*2b9c00cbSConrad Meyer     return 0;
119*2b9c00cbSConrad Meyer }
120