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