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