2 * Copyright (c) Meta Platforms, Inc. and affiliates.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #include <stdio.h> // printf
12 #include <stdlib.h> // free
13 #include <string.h> // memcpy, strlen
14 #include <zstd.h> // presumes zstd library is installed
15 #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
26 * allocate memory for buffers big enough to compress all files
27 * as well as memory for output file name (ofn)
29 static resources createResources_orDie(int argc, const char** argv, char **ofn, size_t* ofnBufferLen)
31 size_t maxFilenameLength=0;
32 size_t maxFileSize = 0;
35 for (argNb = 1; argNb < argc; argNb++) {
36 const char* const filename = argv[argNb];
37 size_t const filenameLength = strlen(filename);
38 size_t const fileSize = fsize_orDie(filename);
40 if (filenameLength > maxFilenameLength) maxFilenameLength = filenameLength;
41 if (fileSize > maxFileSize) maxFileSize = fileSize;
45 ress.fBufferSize = maxFileSize;
46 ress.cBufferSize = ZSTD_compressBound(maxFileSize);
48 *ofnBufferLen = maxFilenameLength + 5;
49 *ofn = (char*)malloc_orDie(*ofnBufferLen);
50 ress.fBuffer = malloc_orDie(ress.fBufferSize);
51 ress.cBuffer = malloc_orDie(ress.cBufferSize);
52 ress.cctx = ZSTD_createCCtx();
53 CHECK(ress.cctx != NULL, "ZSTD_createCCtx() failed!");
57 static void freeResources(resources ress, char *outFilename)
61 ZSTD_freeCCtx(ress.cctx); /* never fails */
65 /* compress with pre-allocated context (ZSTD_CCtx) and input/output buffers*/
66 static void compressFile_orDie(resources ress, const char* fname, const char* oname)
68 size_t fSize = loadFile_orDie(fname, ress.fBuffer, ress.fBufferSize);
70 /* Compress using the context.
71 * If you need more control over parameters, use the advanced API:
72 * ZSTD_CCtx_setParameter(), and ZSTD_compress2().
74 size_t const cSize = ZSTD_compressCCtx(ress.cctx, ress.cBuffer, ress.cBufferSize, ress.fBuffer, fSize, 1);
77 saveFile_orDie(oname, ress.cBuffer, cSize);
80 printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
83 int main(int argc, const char** argv)
85 const char* const exeName = argv[0];
88 printf("wrong arguments\n");
90 printf("%s FILE(s)\n", exeName);
94 /* memory allocation for outFilename and resources */
96 size_t outFilenameBufferLen;
97 resources const ress = createResources_orDie(argc, argv, &outFilename, &outFilenameBufferLen);
99 /* compress files with shared context, input and output buffers */
101 for (argNb = 1; argNb < argc; argNb++) {
102 const char* const inFilename = argv[argNb];
103 size_t const inFilenameLen = strlen(inFilename);
104 CHECK(inFilenameLen + 5 <= outFilenameBufferLen, "File name too long!");
105 memcpy(outFilename, inFilename, inFilenameLen);
106 memcpy(outFilename+inFilenameLen, ".zst", 5);
107 compressFile_orDie(ress, inFilename, outFilename);
111 freeResources(ress,outFilename);
113 printf("compressed %i files \n", argc-1);