xref: /freebsd-src/sys/contrib/zstd/examples/streaming_decompression.c (revision 9cbefe25d46756f342c7dd3d174d2d1103808f21)
12b9c00cbSConrad Meyer /*
22b9c00cbSConrad Meyer  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
32b9c00cbSConrad Meyer  * All rights reserved.
42b9c00cbSConrad Meyer  *
52b9c00cbSConrad Meyer  * This source code is licensed under both the BSD-style license (found in the
62b9c00cbSConrad Meyer  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
72b9c00cbSConrad Meyer  * in the COPYING file in the root directory of this source tree).
82b9c00cbSConrad Meyer  * You may select, at your option, one of the above-listed licenses.
92b9c00cbSConrad Meyer  */
102b9c00cbSConrad Meyer 
112b9c00cbSConrad Meyer 
122b9c00cbSConrad Meyer #include <stdio.h>     // fprintf
132b9c00cbSConrad Meyer #include <stdlib.h>    // free
142b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
152b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
162b9c00cbSConrad Meyer 
172b9c00cbSConrad Meyer static void decompressFile_orDie(const char* fname)
182b9c00cbSConrad Meyer {
192b9c00cbSConrad Meyer     FILE* const fin  = fopen_orDie(fname, "rb");
202b9c00cbSConrad Meyer     size_t const buffInSize = ZSTD_DStreamInSize();
212b9c00cbSConrad Meyer     void*  const buffIn  = malloc_orDie(buffInSize);
222b9c00cbSConrad Meyer     FILE* const fout = stdout;
232b9c00cbSConrad Meyer     size_t const buffOutSize = ZSTD_DStreamOutSize();  /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
242b9c00cbSConrad Meyer     void*  const buffOut = malloc_orDie(buffOutSize);
252b9c00cbSConrad Meyer 
262b9c00cbSConrad Meyer     ZSTD_DCtx* const dctx = ZSTD_createDCtx();
272b9c00cbSConrad Meyer     CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
282b9c00cbSConrad Meyer 
292b9c00cbSConrad Meyer     /* This loop assumes that the input file is one or more concatenated zstd
302b9c00cbSConrad Meyer      * streams. This example won't work if there is trailing non-zstd data at
312b9c00cbSConrad Meyer      * the end, but streaming decompression in general handles this case.
322b9c00cbSConrad Meyer      * ZSTD_decompressStream() returns 0 exactly when the frame is completed,
332b9c00cbSConrad Meyer      * and doesn't consume input after the frame.
342b9c00cbSConrad Meyer      */
352b9c00cbSConrad Meyer     size_t const toRead = buffInSize;
362b9c00cbSConrad Meyer     size_t read;
37*9cbefe25SConrad Meyer     size_t lastRet = 0;
38*9cbefe25SConrad Meyer     int isEmpty = 1;
392b9c00cbSConrad Meyer     while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
40*9cbefe25SConrad Meyer         isEmpty = 0;
412b9c00cbSConrad Meyer         ZSTD_inBuffer input = { buffIn, read, 0 };
422b9c00cbSConrad Meyer         /* Given a valid frame, zstd won't consume the last byte of the frame
432b9c00cbSConrad Meyer          * until it has flushed all of the decompressed data of the frame.
442b9c00cbSConrad Meyer          * Therefore, instead of checking if the return code is 0, we can
452b9c00cbSConrad Meyer          * decompress just check if input.pos < input.size.
462b9c00cbSConrad Meyer          */
472b9c00cbSConrad Meyer         while (input.pos < input.size) {
482b9c00cbSConrad Meyer             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
492b9c00cbSConrad Meyer             /* The return code is zero if the frame is complete, but there may
502b9c00cbSConrad Meyer              * be multiple frames concatenated together. Zstd will automatically
512b9c00cbSConrad Meyer              * reset the context when a frame is complete. Still, calling
522b9c00cbSConrad Meyer              * ZSTD_DCtx_reset() can be useful to reset the context to a clean
532b9c00cbSConrad Meyer              * state, for instance if the last decompression call returned an
542b9c00cbSConrad Meyer              * error.
552b9c00cbSConrad Meyer              */
562b9c00cbSConrad Meyer             size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
572b9c00cbSConrad Meyer             CHECK_ZSTD(ret);
582b9c00cbSConrad Meyer             fwrite_orDie(buffOut, output.pos, fout);
59*9cbefe25SConrad Meyer             lastRet = ret;
602b9c00cbSConrad Meyer         }
612b9c00cbSConrad Meyer     }
622b9c00cbSConrad Meyer 
63*9cbefe25SConrad Meyer     if (isEmpty) {
64*9cbefe25SConrad Meyer         fprintf(stderr, "input is empty\n");
65*9cbefe25SConrad Meyer         exit(1);
66*9cbefe25SConrad Meyer     }
67*9cbefe25SConrad Meyer 
68*9cbefe25SConrad Meyer     if (lastRet != 0) {
69*9cbefe25SConrad Meyer         /* The last return value from ZSTD_decompressStream did not end on a
70*9cbefe25SConrad Meyer          * frame, but we reached the end of the file! We assume this is an
71*9cbefe25SConrad Meyer          * error, and the input was truncated.
72*9cbefe25SConrad Meyer          */
73*9cbefe25SConrad Meyer         fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
74*9cbefe25SConrad Meyer         exit(1);
75*9cbefe25SConrad Meyer     }
76*9cbefe25SConrad Meyer 
772b9c00cbSConrad Meyer     ZSTD_freeDCtx(dctx);
782b9c00cbSConrad Meyer     fclose_orDie(fin);
792b9c00cbSConrad Meyer     fclose_orDie(fout);
802b9c00cbSConrad Meyer     free(buffIn);
812b9c00cbSConrad Meyer     free(buffOut);
822b9c00cbSConrad Meyer }
832b9c00cbSConrad Meyer 
842b9c00cbSConrad Meyer 
852b9c00cbSConrad Meyer int main(int argc, const char** argv)
862b9c00cbSConrad Meyer {
872b9c00cbSConrad Meyer     const char* const exeName = argv[0];
882b9c00cbSConrad Meyer 
892b9c00cbSConrad Meyer     if (argc!=2) {
902b9c00cbSConrad Meyer         fprintf(stderr, "wrong arguments\n");
912b9c00cbSConrad Meyer         fprintf(stderr, "usage:\n");
922b9c00cbSConrad Meyer         fprintf(stderr, "%s FILE\n", exeName);
932b9c00cbSConrad Meyer         return 1;
942b9c00cbSConrad Meyer     }
952b9c00cbSConrad Meyer 
962b9c00cbSConrad Meyer     const char* const inFilename = argv[1];
972b9c00cbSConrad Meyer 
982b9c00cbSConrad Meyer     decompressFile_orDie(inFilename);
992b9c00cbSConrad Meyer     return 0;
1002b9c00cbSConrad Meyer }
101