xref: /netbsd-src/external/bsd/zstd/dist/examples/streaming_decompression.c (revision 3117ece4fc4a4ca4489ba793710b60b0d26bab6c)
1*3117ece4Schristos /*
2*3117ece4Schristos  * Copyright (c) Meta Platforms, Inc. and affiliates.
3*3117ece4Schristos  * All rights reserved.
4*3117ece4Schristos  *
5*3117ece4Schristos  * This source code is licensed under both the BSD-style license (found in the
6*3117ece4Schristos  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7*3117ece4Schristos  * in the COPYING file in the root directory of this source tree).
8*3117ece4Schristos  * You may select, at your option, one of the above-listed licenses.
9*3117ece4Schristos  */
10*3117ece4Schristos 
11*3117ece4Schristos 
12*3117ece4Schristos #include <stdio.h>     // fprintf
13*3117ece4Schristos #include <stdlib.h>    // free
14*3117ece4Schristos #include <zstd.h>      // presumes zstd library is installed
15*3117ece4Schristos #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
16*3117ece4Schristos 
17*3117ece4Schristos static void decompressFile_orDie(const char* fname)
18*3117ece4Schristos {
19*3117ece4Schristos     FILE* const fin  = fopen_orDie(fname, "rb");
20*3117ece4Schristos     size_t const buffInSize = ZSTD_DStreamInSize();
21*3117ece4Schristos     void*  const buffIn  = malloc_orDie(buffInSize);
22*3117ece4Schristos     FILE* const fout = stdout;
23*3117ece4Schristos     size_t const buffOutSize = ZSTD_DStreamOutSize();  /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
24*3117ece4Schristos     void*  const buffOut = malloc_orDie(buffOutSize);
25*3117ece4Schristos 
26*3117ece4Schristos     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
27*3117ece4Schristos     CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
28*3117ece4Schristos 
29*3117ece4Schristos     /* This loop assumes that the input file is one or more concatenated zstd
30*3117ece4Schristos      * streams. This example won't work if there is trailing non-zstd data at
31*3117ece4Schristos      * the end, but streaming decompression in general handles this case.
32*3117ece4Schristos      * ZSTD_decompressStream() returns 0 exactly when the frame is completed,
33*3117ece4Schristos      * and doesn't consume input after the frame.
34*3117ece4Schristos      */
35*3117ece4Schristos     size_t const toRead = buffInSize;
36*3117ece4Schristos     size_t read;
37*3117ece4Schristos     size_t lastRet = 0;
38*3117ece4Schristos     int isEmpty = 1;
39*3117ece4Schristos     while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
40*3117ece4Schristos         isEmpty = 0;
41*3117ece4Schristos         ZSTD_inBuffer input = { buffIn, read, 0 };
42*3117ece4Schristos         /* Given a valid frame, zstd won't consume the last byte of the frame
43*3117ece4Schristos          * until it has flushed all of the decompressed data of the frame.
44*3117ece4Schristos          * Therefore, instead of checking if the return code is 0, we can
45*3117ece4Schristos          * decompress just check if input.pos < input.size.
46*3117ece4Schristos          */
47*3117ece4Schristos         while (input.pos < input.size) {
48*3117ece4Schristos             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
49*3117ece4Schristos             /* The return code is zero if the frame is complete, but there may
50*3117ece4Schristos              * be multiple frames concatenated together. Zstd will automatically
51*3117ece4Schristos              * reset the context when a frame is complete. Still, calling
52*3117ece4Schristos              * ZSTD_DCtx_reset() can be useful to reset the context to a clean
53*3117ece4Schristos              * state, for instance if the last decompression call returned an
54*3117ece4Schristos              * error.
55*3117ece4Schristos              */
56*3117ece4Schristos             size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
57*3117ece4Schristos             CHECK_ZSTD(ret);
58*3117ece4Schristos             fwrite_orDie(buffOut, output.pos, fout);
59*3117ece4Schristos             lastRet = ret;
60*3117ece4Schristos         }
61*3117ece4Schristos     }
62*3117ece4Schristos 
63*3117ece4Schristos     if (isEmpty) {
64*3117ece4Schristos         fprintf(stderr, "input is empty\n");
65*3117ece4Schristos         exit(1);
66*3117ece4Schristos     }
67*3117ece4Schristos 
68*3117ece4Schristos     if (lastRet != 0) {
69*3117ece4Schristos         /* The last return value from ZSTD_decompressStream did not end on a
70*3117ece4Schristos          * frame, but we reached the end of the file! We assume this is an
71*3117ece4Schristos          * error, and the input was truncated.
72*3117ece4Schristos          */
73*3117ece4Schristos         fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
74*3117ece4Schristos         exit(1);
75*3117ece4Schristos     }
76*3117ece4Schristos 
77*3117ece4Schristos     ZSTD_freeDCtx(dctx);
78*3117ece4Schristos     fclose_orDie(fin);
79*3117ece4Schristos     fclose_orDie(fout);
80*3117ece4Schristos     free(buffIn);
81*3117ece4Schristos     free(buffOut);
82*3117ece4Schristos }
83*3117ece4Schristos 
84*3117ece4Schristos 
85*3117ece4Schristos int main(int argc, const char** argv)
86*3117ece4Schristos {
87*3117ece4Schristos     const char* const exeName = argv[0];
88*3117ece4Schristos 
89*3117ece4Schristos     if (argc!=2) {
90*3117ece4Schristos         fprintf(stderr, "wrong arguments\n");
91*3117ece4Schristos         fprintf(stderr, "usage:\n");
92*3117ece4Schristos         fprintf(stderr, "%s FILE\n", exeName);
93*3117ece4Schristos         return 1;
94*3117ece4Schristos     }
95*3117ece4Schristos 
96*3117ece4Schristos     const char* const inFilename = argv[1];
97*3117ece4Schristos 
98*3117ece4Schristos     decompressFile_orDie(inFilename);
99*3117ece4Schristos     return 0;
100*3117ece4Schristos }
101