xref: /freebsd-src/sys/contrib/zstd/examples/multiple_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 /* The objective of this example is to show of to compress multiple successive files
132b9c00cbSConrad Meyer *  while preserving memory management.
142b9c00cbSConrad Meyer *  All structures and buffers will be created only once,
152b9c00cbSConrad Meyer *  and shared across all compression operations */
162b9c00cbSConrad Meyer 
172b9c00cbSConrad Meyer #include <stdio.h>     // printf
182b9c00cbSConrad Meyer #include <stdlib.h>    // free
192b9c00cbSConrad Meyer #include <string.h>    // memset, strcat
202b9c00cbSConrad Meyer #include <zstd.h>      // presumes zstd library is installed
212b9c00cbSConrad Meyer #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
222b9c00cbSConrad Meyer 
232b9c00cbSConrad Meyer typedef struct {
242b9c00cbSConrad Meyer     void* buffIn;
252b9c00cbSConrad Meyer     void* buffOut;
262b9c00cbSConrad Meyer     size_t buffInSize;
272b9c00cbSConrad Meyer     size_t buffOutSize;
282b9c00cbSConrad Meyer     ZSTD_CCtx* cctx;
292b9c00cbSConrad Meyer } resources;
302b9c00cbSConrad Meyer 
createResources_orDie(int cLevel)312b9c00cbSConrad Meyer static resources createResources_orDie(int cLevel)
322b9c00cbSConrad Meyer {
332b9c00cbSConrad Meyer     resources ress;
342b9c00cbSConrad Meyer     ress.buffInSize = ZSTD_CStreamInSize();   /* can always read one full block */
352b9c00cbSConrad Meyer     ress.buffOutSize= ZSTD_CStreamOutSize();  /* can always flush a full block */
362b9c00cbSConrad Meyer     ress.buffIn = malloc_orDie(ress.buffInSize);
372b9c00cbSConrad Meyer     ress.buffOut= malloc_orDie(ress.buffOutSize);
382b9c00cbSConrad Meyer     ress.cctx = ZSTD_createCCtx();
392b9c00cbSConrad Meyer     CHECK(ress.cctx != NULL, "ZSTD_createCCtx() failed!");
402b9c00cbSConrad Meyer 
412b9c00cbSConrad Meyer     /* Set any compression parameters you want here.
422b9c00cbSConrad Meyer      * They will persist for every compression operation.
432b9c00cbSConrad Meyer      * Here we set the compression level, and enable the checksum.
442b9c00cbSConrad Meyer      */
452b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, cLevel) );
462b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_checksumFlag, 1) );
472b9c00cbSConrad Meyer     return ress;
482b9c00cbSConrad Meyer }
492b9c00cbSConrad Meyer 
freeResources(resources ress)502b9c00cbSConrad Meyer static void freeResources(resources ress)
512b9c00cbSConrad Meyer {
522b9c00cbSConrad Meyer     ZSTD_freeCCtx(ress.cctx);
532b9c00cbSConrad Meyer     free(ress.buffIn);
542b9c00cbSConrad Meyer     free(ress.buffOut);
552b9c00cbSConrad Meyer }
562b9c00cbSConrad Meyer 
compressFile_orDie(resources ress,const char * fname,const char * outName)572b9c00cbSConrad Meyer static void compressFile_orDie(resources ress, const char* fname, const char* outName)
582b9c00cbSConrad Meyer {
592b9c00cbSConrad Meyer     // Open the input and output files.
602b9c00cbSConrad Meyer     FILE* const fin  = fopen_orDie(fname, "rb");
612b9c00cbSConrad Meyer     FILE* const fout = fopen_orDie(outName, "wb");
622b9c00cbSConrad Meyer 
632b9c00cbSConrad Meyer     /* Reset the context to a clean state to start a new compression operation.
642b9c00cbSConrad Meyer      * The parameters are sticky, so we keep the compression level and extra
652b9c00cbSConrad Meyer      * parameters that we set in createResources_orDie().
662b9c00cbSConrad Meyer      */
672b9c00cbSConrad Meyer     CHECK_ZSTD( ZSTD_CCtx_reset(ress.cctx, ZSTD_reset_session_only) );
682b9c00cbSConrad Meyer 
692b9c00cbSConrad Meyer     size_t const toRead = ress.buffInSize;
702b9c00cbSConrad Meyer     size_t read;
712b9c00cbSConrad Meyer     while ( (read = fread_orDie(ress.buffIn, toRead, fin)) ) {
722b9c00cbSConrad Meyer         /* This loop is the same as streaming_compression.c.
732b9c00cbSConrad Meyer          * See that file for detailed comments.
742b9c00cbSConrad Meyer          */
752b9c00cbSConrad Meyer         int const lastChunk = (read < toRead);
762b9c00cbSConrad Meyer         ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
772b9c00cbSConrad Meyer 
782b9c00cbSConrad Meyer         ZSTD_inBuffer input = { ress.buffIn, read, 0 };
792b9c00cbSConrad Meyer         int finished;
802b9c00cbSConrad Meyer         do {
812b9c00cbSConrad Meyer             ZSTD_outBuffer output = { ress.buffOut, ress.buffOutSize, 0 };
822b9c00cbSConrad Meyer             size_t const remaining = ZSTD_compressStream2(ress.cctx, &output, &input, mode);
832b9c00cbSConrad Meyer             CHECK_ZSTD(remaining);
842b9c00cbSConrad Meyer             fwrite_orDie(ress.buffOut, output.pos, fout);
852b9c00cbSConrad Meyer             finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
862b9c00cbSConrad Meyer         } while (!finished);
872b9c00cbSConrad Meyer         CHECK(input.pos == input.size,
882b9c00cbSConrad Meyer               "Impossible: zstd only returns 0 when the input is completely consumed!");
892b9c00cbSConrad Meyer     }
902b9c00cbSConrad Meyer 
912b9c00cbSConrad Meyer     fclose_orDie(fout);
922b9c00cbSConrad Meyer     fclose_orDie(fin);
932b9c00cbSConrad Meyer }
942b9c00cbSConrad Meyer 
main(int argc,const char ** argv)952b9c00cbSConrad Meyer int main(int argc, const char** argv)
962b9c00cbSConrad Meyer {
972b9c00cbSConrad Meyer     const char* const exeName = argv[0];
982b9c00cbSConrad Meyer 
992b9c00cbSConrad Meyer     if (argc<2) {
1002b9c00cbSConrad Meyer         printf("wrong arguments\n");
1012b9c00cbSConrad Meyer         printf("usage:\n");
1022b9c00cbSConrad Meyer         printf("%s FILE(s)\n", exeName);
1032b9c00cbSConrad Meyer         return 1;
1042b9c00cbSConrad Meyer     }
1052b9c00cbSConrad Meyer 
1062b9c00cbSConrad Meyer     int const cLevel = 7;
1072b9c00cbSConrad Meyer     resources const ress = createResources_orDie(cLevel);
1082b9c00cbSConrad Meyer     void* ofnBuffer = NULL;
1092b9c00cbSConrad Meyer     size_t ofnbSize = 0;
1102b9c00cbSConrad Meyer 
1112b9c00cbSConrad Meyer     int argNb;
1122b9c00cbSConrad Meyer     for (argNb = 1; argNb < argc; argNb++) {
1132b9c00cbSConrad Meyer         const char* const ifn = argv[argNb];
1142b9c00cbSConrad Meyer         size_t const ifnSize = strlen(ifn);
1152b9c00cbSConrad Meyer         size_t const ofnSize = ifnSize + 5;
1162b9c00cbSConrad Meyer         if (ofnbSize <= ofnSize) {
1172b9c00cbSConrad Meyer             ofnbSize = ofnSize + 16;
1182b9c00cbSConrad Meyer             free(ofnBuffer);
1192b9c00cbSConrad Meyer             ofnBuffer = malloc_orDie(ofnbSize);
1202b9c00cbSConrad Meyer         }
1212b9c00cbSConrad Meyer         memset(ofnBuffer, 0, ofnSize);
1222b9c00cbSConrad Meyer         strcat(ofnBuffer, ifn);
1232b9c00cbSConrad Meyer         strcat(ofnBuffer, ".zst");
1242b9c00cbSConrad Meyer         compressFile_orDie(ress, ifn, ofnBuffer);
1252b9c00cbSConrad Meyer     }
1262b9c00cbSConrad Meyer 
1272b9c00cbSConrad Meyer     freeResources(ress);
1282b9c00cbSConrad Meyer     free(ofnBuffer);
1292b9c00cbSConrad Meyer 
1302b9c00cbSConrad Meyer     printf("compressed %i files \n", argc-1);
1312b9c00cbSConrad Meyer 
1322b9c00cbSConrad Meyer     return 0;
1332b9c00cbSConrad Meyer }
134