xref: /aosp_15_r20/external/zstd/examples/multiple_streaming_compression.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 /* The objective of this example is to show of to compress multiple successive files
13*01826a49SYabin Cui *  while preserving memory management.
14*01826a49SYabin Cui *  All structures and buffers will be created only once,
15*01826a49SYabin Cui *  and shared across all compression operations */
16*01826a49SYabin Cui 
17*01826a49SYabin Cui #include <stdio.h>     // printf
18*01826a49SYabin Cui #include <stdlib.h>    // free
19*01826a49SYabin Cui #include <string.h>    // memset, strcat
20*01826a49SYabin Cui #include <zstd.h>      // presumes zstd library is installed
21*01826a49SYabin Cui #include "common.h"    // Helper functions, CHECK(), and CHECK_ZSTD()
22*01826a49SYabin Cui 
23*01826a49SYabin Cui typedef struct {
24*01826a49SYabin Cui     void* buffIn;
25*01826a49SYabin Cui     void* buffOut;
26*01826a49SYabin Cui     size_t buffInSize;
27*01826a49SYabin Cui     size_t buffOutSize;
28*01826a49SYabin Cui     ZSTD_CCtx* cctx;
29*01826a49SYabin Cui } resources;
30*01826a49SYabin Cui 
createResources_orDie(int cLevel)31*01826a49SYabin Cui static resources createResources_orDie(int cLevel)
32*01826a49SYabin Cui {
33*01826a49SYabin Cui     resources ress;
34*01826a49SYabin Cui     ress.buffInSize = ZSTD_CStreamInSize();   /* can always read one full block */
35*01826a49SYabin Cui     ress.buffOutSize= ZSTD_CStreamOutSize();  /* can always flush a full block */
36*01826a49SYabin Cui     ress.buffIn = malloc_orDie(ress.buffInSize);
37*01826a49SYabin Cui     ress.buffOut= malloc_orDie(ress.buffOutSize);
38*01826a49SYabin Cui     ress.cctx = ZSTD_createCCtx();
39*01826a49SYabin Cui     CHECK(ress.cctx != NULL, "ZSTD_createCCtx() failed!");
40*01826a49SYabin Cui 
41*01826a49SYabin Cui     /* Set any compression parameters you want here.
42*01826a49SYabin Cui      * They will persist for every compression operation.
43*01826a49SYabin Cui      * Here we set the compression level, and enable the checksum.
44*01826a49SYabin Cui      */
45*01826a49SYabin Cui     CHECK_ZSTD( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_compressionLevel, cLevel) );
46*01826a49SYabin Cui     CHECK_ZSTD( ZSTD_CCtx_setParameter(ress.cctx, ZSTD_c_checksumFlag, 1) );
47*01826a49SYabin Cui     return ress;
48*01826a49SYabin Cui }
49*01826a49SYabin Cui 
freeResources(resources ress)50*01826a49SYabin Cui static void freeResources(resources ress)
51*01826a49SYabin Cui {
52*01826a49SYabin Cui     ZSTD_freeCCtx(ress.cctx);
53*01826a49SYabin Cui     free(ress.buffIn);
54*01826a49SYabin Cui     free(ress.buffOut);
55*01826a49SYabin Cui }
56*01826a49SYabin Cui 
compressFile_orDie(resources ress,const char * fname,const char * outName)57*01826a49SYabin Cui static void compressFile_orDie(resources ress, const char* fname, const char* outName)
58*01826a49SYabin Cui {
59*01826a49SYabin Cui     // Open the input and output files.
60*01826a49SYabin Cui     FILE* const fin  = fopen_orDie(fname, "rb");
61*01826a49SYabin Cui     FILE* const fout = fopen_orDie(outName, "wb");
62*01826a49SYabin Cui 
63*01826a49SYabin Cui     /* Reset the context to a clean state to start a new compression operation.
64*01826a49SYabin Cui      * The parameters are sticky, so we keep the compression level and extra
65*01826a49SYabin Cui      * parameters that we set in createResources_orDie().
66*01826a49SYabin Cui      */
67*01826a49SYabin Cui     CHECK_ZSTD( ZSTD_CCtx_reset(ress.cctx, ZSTD_reset_session_only) );
68*01826a49SYabin Cui 
69*01826a49SYabin Cui     size_t const toRead = ress.buffInSize;
70*01826a49SYabin Cui     size_t read;
71*01826a49SYabin Cui     while ( (read = fread_orDie(ress.buffIn, toRead, fin)) ) {
72*01826a49SYabin Cui         /* This loop is the same as streaming_compression.c.
73*01826a49SYabin Cui          * See that file for detailed comments.
74*01826a49SYabin Cui          */
75*01826a49SYabin Cui         int const lastChunk = (read < toRead);
76*01826a49SYabin Cui         ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
77*01826a49SYabin Cui 
78*01826a49SYabin Cui         ZSTD_inBuffer input = { ress.buffIn, read, 0 };
79*01826a49SYabin Cui         int finished;
80*01826a49SYabin Cui         do {
81*01826a49SYabin Cui             ZSTD_outBuffer output = { ress.buffOut, ress.buffOutSize, 0 };
82*01826a49SYabin Cui             size_t const remaining = ZSTD_compressStream2(ress.cctx, &output, &input, mode);
83*01826a49SYabin Cui             CHECK_ZSTD(remaining);
84*01826a49SYabin Cui             fwrite_orDie(ress.buffOut, output.pos, fout);
85*01826a49SYabin Cui             finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
86*01826a49SYabin Cui         } while (!finished);
87*01826a49SYabin Cui         CHECK(input.pos == input.size,
88*01826a49SYabin Cui               "Impossible: zstd only returns 0 when the input is completely consumed!");
89*01826a49SYabin Cui     }
90*01826a49SYabin Cui 
91*01826a49SYabin Cui     fclose_orDie(fout);
92*01826a49SYabin Cui     fclose_orDie(fin);
93*01826a49SYabin Cui }
94*01826a49SYabin Cui 
main(int argc,const char ** argv)95*01826a49SYabin Cui int main(int argc, const char** argv)
96*01826a49SYabin Cui {
97*01826a49SYabin Cui     const char* const exeName = argv[0];
98*01826a49SYabin Cui 
99*01826a49SYabin Cui     if (argc<2) {
100*01826a49SYabin Cui         printf("wrong arguments\n");
101*01826a49SYabin Cui         printf("usage:\n");
102*01826a49SYabin Cui         printf("%s FILE(s)\n", exeName);
103*01826a49SYabin Cui         return 1;
104*01826a49SYabin Cui     }
105*01826a49SYabin Cui 
106*01826a49SYabin Cui     int const cLevel = 7;
107*01826a49SYabin Cui     resources const ress = createResources_orDie(cLevel);
108*01826a49SYabin Cui     void* ofnBuffer = NULL;
109*01826a49SYabin Cui     size_t ofnbSize = 0;
110*01826a49SYabin Cui 
111*01826a49SYabin Cui     int argNb;
112*01826a49SYabin Cui     for (argNb = 1; argNb < argc; argNb++) {
113*01826a49SYabin Cui         const char* const ifn = argv[argNb];
114*01826a49SYabin Cui         size_t const ifnSize = strlen(ifn);
115*01826a49SYabin Cui         size_t const ofnSize = ifnSize + 5;
116*01826a49SYabin Cui         if (ofnbSize <= ofnSize) {
117*01826a49SYabin Cui             ofnbSize = ofnSize + 16;
118*01826a49SYabin Cui             free(ofnBuffer);
119*01826a49SYabin Cui             ofnBuffer = malloc_orDie(ofnbSize);
120*01826a49SYabin Cui         }
121*01826a49SYabin Cui         memset(ofnBuffer, 0, ofnSize);
122*01826a49SYabin Cui         strcat(ofnBuffer, ifn);
123*01826a49SYabin Cui         strcat(ofnBuffer, ".zst");
124*01826a49SYabin Cui         compressFile_orDie(ress, ifn, ofnBuffer);
125*01826a49SYabin Cui     }
126*01826a49SYabin Cui 
127*01826a49SYabin Cui     freeResources(ress);
128*01826a49SYabin Cui     free(ofnBuffer);
129*01826a49SYabin Cui 
130*01826a49SYabin Cui     printf("compressed %i files \n", argc-1);
131*01826a49SYabin Cui 
132*01826a49SYabin Cui     return 0;
133*01826a49SYabin Cui }
134