xref: /freebsd-src/sys/contrib/zstd/examples/streaming_decompression.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>     // fprintf
13*2b9c00cbSConrad Meyer #include <stdlib.h>    // free
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 decompressFile_orDie(const char* fname)
18*2b9c00cbSConrad Meyer {
19*2b9c00cbSConrad Meyer     FILE* const fin  = fopen_orDie(fname, "rb");
20*2b9c00cbSConrad Meyer     size_t const buffInSize = ZSTD_DStreamInSize();
21*2b9c00cbSConrad Meyer     void*  const buffIn  = malloc_orDie(buffInSize);
22*2b9c00cbSConrad Meyer     FILE* const fout = stdout;
23*2b9c00cbSConrad Meyer     size_t const buffOutSize = ZSTD_DStreamOutSize();  /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
24*2b9c00cbSConrad Meyer     void*  const buffOut = malloc_orDie(buffOutSize);
25*2b9c00cbSConrad Meyer 
26*2b9c00cbSConrad Meyer     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
27*2b9c00cbSConrad Meyer     CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
28*2b9c00cbSConrad Meyer 
29*2b9c00cbSConrad Meyer     /* This loop assumes that the input file is one or more concatenated zstd
30*2b9c00cbSConrad Meyer      * streams. This example won't work if there is trailing non-zstd data at
31*2b9c00cbSConrad Meyer      * the end, but streaming decompression in general handles this case.
32*2b9c00cbSConrad Meyer      * ZSTD_decompressStream() returns 0 exactly when the frame is completed,
33*2b9c00cbSConrad Meyer      * and doesn't consume input after the frame.
34*2b9c00cbSConrad Meyer      */
35*2b9c00cbSConrad Meyer     size_t const toRead = buffInSize;
36*2b9c00cbSConrad Meyer     size_t read;
37*2b9c00cbSConrad Meyer     while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
38*2b9c00cbSConrad Meyer         ZSTD_inBuffer input = { buffIn, read, 0 };
39*2b9c00cbSConrad Meyer         /* Given a valid frame, zstd won't consume the last byte of the frame
40*2b9c00cbSConrad Meyer          * until it has flushed all of the decompressed data of the frame.
41*2b9c00cbSConrad Meyer          * Therefore, instead of checking if the return code is 0, we can
42*2b9c00cbSConrad Meyer          * decompress just check if input.pos < input.size.
43*2b9c00cbSConrad Meyer          */
44*2b9c00cbSConrad Meyer         while (input.pos < input.size) {
45*2b9c00cbSConrad Meyer             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
46*2b9c00cbSConrad Meyer             /* The return code is zero if the frame is complete, but there may
47*2b9c00cbSConrad Meyer              * be multiple frames concatenated together. Zstd will automatically
48*2b9c00cbSConrad Meyer              * reset the context when a frame is complete. Still, calling
49*2b9c00cbSConrad Meyer              * ZSTD_DCtx_reset() can be useful to reset the context to a clean
50*2b9c00cbSConrad Meyer              * state, for instance if the last decompression call returned an
51*2b9c00cbSConrad Meyer              * error.
52*2b9c00cbSConrad Meyer              */
53*2b9c00cbSConrad Meyer             size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
54*2b9c00cbSConrad Meyer             CHECK_ZSTD(ret);
55*2b9c00cbSConrad Meyer             fwrite_orDie(buffOut, output.pos, fout);
56*2b9c00cbSConrad Meyer         }
57*2b9c00cbSConrad Meyer     }
58*2b9c00cbSConrad Meyer 
59*2b9c00cbSConrad Meyer     ZSTD_freeDCtx(dctx);
60*2b9c00cbSConrad Meyer     fclose_orDie(fin);
61*2b9c00cbSConrad Meyer     fclose_orDie(fout);
62*2b9c00cbSConrad Meyer     free(buffIn);
63*2b9c00cbSConrad Meyer     free(buffOut);
64*2b9c00cbSConrad Meyer }
65*2b9c00cbSConrad Meyer 
66*2b9c00cbSConrad Meyer 
67*2b9c00cbSConrad Meyer int main(int argc, const char** argv)
68*2b9c00cbSConrad Meyer {
69*2b9c00cbSConrad Meyer     const char* const exeName = argv[0];
70*2b9c00cbSConrad Meyer 
71*2b9c00cbSConrad Meyer     if (argc!=2) {
72*2b9c00cbSConrad Meyer         fprintf(stderr, "wrong arguments\n");
73*2b9c00cbSConrad Meyer         fprintf(stderr, "usage:\n");
74*2b9c00cbSConrad Meyer         fprintf(stderr, "%s FILE\n", exeName);
75*2b9c00cbSConrad Meyer         return 1;
76*2b9c00cbSConrad Meyer     }
77*2b9c00cbSConrad Meyer 
78*2b9c00cbSConrad Meyer     const char* const inFilename = argv[1];
79*2b9c00cbSConrad Meyer 
80*2b9c00cbSConrad Meyer     decompressFile_orDie(inFilename);
81*2b9c00cbSConrad Meyer     return 0;
82*2b9c00cbSConrad Meyer }
83