xref: /freebsd-src/sys/contrib/zstd/examples/streaming_compression.c (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
12b9c00cbSConrad Meyer /*
2*5ff13fbcSAllan Jude  * Copyright (c) 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>     // printf
132b9c00cbSConrad Meyer #include <stdlib.h>    // free
142b9c00cbSConrad Meyer #include <string.h>    // memset, strcat, strlen
152b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
162b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
172b9c00cbSConrad Meyer 
compressFile_orDie(const char * fname,const char * outName,int cLevel,int nbThreads)18*5ff13fbcSAllan Jude static void compressFile_orDie(const char* fname, const char* outName, int cLevel,
19*5ff13fbcSAllan Jude                                int nbThreads)
202b9c00cbSConrad Meyer {
21*5ff13fbcSAllan Jude     fprintf (stderr, "Starting compression of %s with level %d, using %d threads\n",
22*5ff13fbcSAllan Jude              fname, cLevel, nbThreads);
23*5ff13fbcSAllan Jude 
242b9c00cbSConrad Meyer     /* Open the input and output files. */
252b9c00cbSConrad Meyer     FILE* const fin  = fopen_orDie(fname, "rb");
262b9c00cbSConrad Meyer     FILE* const fout = fopen_orDie(outName, "wb");
272b9c00cbSConrad Meyer     /* Create the input and output buffers.
282b9c00cbSConrad Meyer      * They may be any size, but we recommend using these functions to size them.
292b9c00cbSConrad Meyer      * Performance will only suffer significantly for very tiny buffers.
302b9c00cbSConrad Meyer      */
312b9c00cbSConrad Meyer     size_t const buffInSize = ZSTD_CStreamInSize();
322b9c00cbSConrad Meyer     void*  const buffIn  = malloc_orDie(buffInSize);
332b9c00cbSConrad Meyer     size_t const buffOutSize = ZSTD_CStreamOutSize();
342b9c00cbSConrad Meyer     void*  const buffOut = malloc_orDie(buffOutSize);
352b9c00cbSConrad Meyer 
362b9c00cbSConrad Meyer     /* Create the context. */
372b9c00cbSConrad Meyer     ZSTD_CCtx* const cctx = ZSTD_createCCtx();
382b9c00cbSConrad Meyer     CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
392b9c00cbSConrad Meyer 
402b9c00cbSConrad Meyer     /* Set any parameters you want.
412b9c00cbSConrad Meyer      * Here we set the compression level, and enable the checksum.
422b9c00cbSConrad Meyer      */
432b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
442b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
45*5ff13fbcSAllan Jude     ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);
462b9c00cbSConrad Meyer 
472b9c00cbSConrad Meyer     /* This loop read from the input file, compresses that entire chunk,
482b9c00cbSConrad Meyer      * and writes all output produced to the output file.
492b9c00cbSConrad Meyer      */
502b9c00cbSConrad Meyer     size_t const toRead = buffInSize;
519cbefe25SConrad Meyer     for (;;) {
529cbefe25SConrad Meyer         size_t read = fread_orDie(buffIn, toRead, fin);
532b9c00cbSConrad Meyer         /* Select the flush mode.
542b9c00cbSConrad Meyer          * If the read may not be finished (read == toRead) we use
552b9c00cbSConrad Meyer          * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
562b9c00cbSConrad Meyer          * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
572b9c00cbSConrad Meyer          * since it knows it is compressing the entire source in one pass.
582b9c00cbSConrad Meyer          */
592b9c00cbSConrad Meyer         int const lastChunk = (read < toRead);
602b9c00cbSConrad Meyer         ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
612b9c00cbSConrad Meyer         /* Set the input buffer to what we just read.
622b9c00cbSConrad Meyer          * We compress until the input buffer is empty, each time flushing the
632b9c00cbSConrad Meyer          * output.
642b9c00cbSConrad Meyer          */
652b9c00cbSConrad Meyer         ZSTD_inBuffer input = { buffIn, read, 0 };
662b9c00cbSConrad Meyer         int finished;
672b9c00cbSConrad Meyer         do {
682b9c00cbSConrad Meyer             /* Compress into the output buffer and write all of the output to
692b9c00cbSConrad Meyer              * the file so we can reuse the buffer next iteration.
702b9c00cbSConrad Meyer              */
712b9c00cbSConrad Meyer             ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
722b9c00cbSConrad Meyer             size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
732b9c00cbSConrad Meyer             CHECK_ZSTD(remaining);
742b9c00cbSConrad Meyer             fwrite_orDie(buffOut, output.pos, fout);
752b9c00cbSConrad Meyer             /* If we're on the last chunk we're finished when zstd returns 0,
762b9c00cbSConrad Meyer              * which means its consumed all the input AND finished the frame.
772b9c00cbSConrad Meyer              * Otherwise, we're finished when we've consumed all the input.
782b9c00cbSConrad Meyer              */
792b9c00cbSConrad Meyer             finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
802b9c00cbSConrad Meyer         } while (!finished);
812b9c00cbSConrad Meyer         CHECK(input.pos == input.size,
822b9c00cbSConrad Meyer               "Impossible: zstd only returns 0 when the input is completely consumed!");
839cbefe25SConrad Meyer 
849cbefe25SConrad Meyer         if (lastChunk) {
859cbefe25SConrad Meyer             break;
869cbefe25SConrad Meyer         }
872b9c00cbSConrad Meyer     }
882b9c00cbSConrad Meyer 
892b9c00cbSConrad Meyer     ZSTD_freeCCtx(cctx);
902b9c00cbSConrad Meyer     fclose_orDie(fout);
912b9c00cbSConrad Meyer     fclose_orDie(fin);
922b9c00cbSConrad Meyer     free(buffIn);
932b9c00cbSConrad Meyer     free(buffOut);
942b9c00cbSConrad Meyer }
952b9c00cbSConrad Meyer 
962b9c00cbSConrad Meyer 
createOutFilename_orDie(const char * filename)972b9c00cbSConrad Meyer static char* createOutFilename_orDie(const char* filename)
982b9c00cbSConrad Meyer {
992b9c00cbSConrad Meyer     size_t const inL = strlen(filename);
1002b9c00cbSConrad Meyer     size_t const outL = inL + 5;
1012b9c00cbSConrad Meyer     void* const outSpace = malloc_orDie(outL);
1022b9c00cbSConrad Meyer     memset(outSpace, 0, outL);
1032b9c00cbSConrad Meyer     strcat(outSpace, filename);
1042b9c00cbSConrad Meyer     strcat(outSpace, ".zst");
1052b9c00cbSConrad Meyer     return (char*)outSpace;
1062b9c00cbSConrad Meyer }
1072b9c00cbSConrad Meyer 
main(int argc,const char ** argv)1082b9c00cbSConrad Meyer int main(int argc, const char** argv)
1092b9c00cbSConrad Meyer {
1102b9c00cbSConrad Meyer     const char* const exeName = argv[0];
1112b9c00cbSConrad Meyer 
112*5ff13fbcSAllan Jude     if (argc < 2) {
1132b9c00cbSConrad Meyer         printf("wrong arguments\n");
1142b9c00cbSConrad Meyer         printf("usage:\n");
115*5ff13fbcSAllan Jude         printf("%s FILE [LEVEL] [THREADS]\n", exeName);
1162b9c00cbSConrad Meyer         return 1;
1172b9c00cbSConrad Meyer     }
1182b9c00cbSConrad Meyer 
119*5ff13fbcSAllan Jude     int cLevel = 1;
120*5ff13fbcSAllan Jude     int nbThreads = 4;
121*5ff13fbcSAllan Jude 
122*5ff13fbcSAllan Jude     if (argc >= 3) {
123*5ff13fbcSAllan Jude       cLevel = atoi (argv[2]);
124*5ff13fbcSAllan Jude       CHECK(cLevel != 0, "can't parse LEVEL!");
125*5ff13fbcSAllan Jude     }
126*5ff13fbcSAllan Jude 
127*5ff13fbcSAllan Jude     if (argc >= 4) {
128*5ff13fbcSAllan Jude       nbThreads = atoi (argv[3]);
129*5ff13fbcSAllan Jude       CHECK(nbThreads != 0, "can't parse THREADS!");
130*5ff13fbcSAllan Jude     }
131*5ff13fbcSAllan Jude 
1322b9c00cbSConrad Meyer     const char* const inFilename = argv[1];
1332b9c00cbSConrad Meyer 
1342b9c00cbSConrad Meyer     char* const outFilename = createOutFilename_orDie(inFilename);
135*5ff13fbcSAllan Jude     compressFile_orDie(inFilename, outFilename, cLevel, nbThreads);
1362b9c00cbSConrad Meyer 
1372b9c00cbSConrad Meyer     free(outFilename);   /* not strictly required, since program execution stops there,
138*5ff13fbcSAllan Jude                           * but some static analyzer may complain otherwise */
1392b9c00cbSConrad Meyer     return 0;
1402b9c00cbSConrad Meyer }
141