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).
10 #include <stdlib.h> // malloc, free, exit, atoi
11 #include <stdio.h> // fprintf, perror, feof, fopen, etc.
12 #include <string.h> // strlen, memset, strcat
13 #define ZSTD_STATIC_LINKING_ONLY
14 #include <zstd.h> // presumes zstd library is installed
16 #include "../zstd_seekable.h"
18 static void* malloc_orDie(size_t size)
20 void* const buff = malloc(size);
21 if (buff) return buff;
27 static FILE* fopen_orDie(const char *filename, const char *instruction)
29 FILE* const inFile = fopen(filename, instruction);
30 if (inFile) return inFile;
36 static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
38 size_t const readSize = fread(buffer, 1, sizeToRead, file);
39 if (readSize == sizeToRead) return readSize; /* good */
40 if (feof(file)) return readSize; /* good, reached end of file */
46 static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
48 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
49 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
55 static size_t fclose_orDie(FILE* file)
57 if (!fclose(file)) return 0;
63 static void compressFile_orDie(const char* fname, const char* outName, int cLevel, unsigned frameSize)
65 FILE* const fin = fopen_orDie(fname, "rb");
66 FILE* const fout = fopen_orDie(outName, "wb");
67 size_t const buffInSize = ZSTD_CStreamInSize(); /* can always read one full block */
68 void* const buffIn = malloc_orDie(buffInSize);
69 size_t const buffOutSize = ZSTD_CStreamOutSize(); /* can always flush a full block */
70 void* const buffOut = malloc_orDie(buffOutSize);
72 ZSTD_seekable_CStream* const cstream = ZSTD_seekable_createCStream();
73 if (cstream==NULL) { fprintf(stderr, "ZSTD_seekable_createCStream() error \n"); exit(10); }
74 size_t const initResult = ZSTD_seekable_initCStream(cstream, cLevel, 1, frameSize);
75 if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_seekable_initCStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); }
77 size_t read, toRead = buffInSize;
78 while( (read = fread_orDie(buffIn, toRead, fin)) ) {
79 ZSTD_inBuffer input = { buffIn, read, 0 };
80 while (input.pos < input.size) {
81 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
82 toRead = ZSTD_seekable_compressStream(cstream, &output , &input); /* toRead is guaranteed to be <= ZSTD_CStreamInSize() */
83 if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_seekable_compressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); }
84 if (toRead > buffInSize) toRead = buffInSize; /* Safely handle case when `buffInSize` is manually changed to a value < ZSTD_CStreamInSize()*/
85 fwrite_orDie(buffOut, output.pos, fout);
90 ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
91 size_t const remainingToFlush = ZSTD_seekable_endStream(cstream, &output); /* close stream */
92 if (ZSTD_isError(remainingToFlush)) { fprintf(stderr, "ZSTD_seekable_endStream() error : %s \n", ZSTD_getErrorName(remainingToFlush)); exit(13); }
93 fwrite_orDie(buffOut, output.pos, fout);
94 if (!remainingToFlush) break;
97 ZSTD_seekable_freeCStream(cstream);
104 static char* createOutFilename_orDie(const char* filename)
106 size_t const inL = strlen(filename);
107 size_t const outL = inL + 5;
108 void* outSpace = malloc_orDie(outL);
109 memset(outSpace, 0, outL);
110 strcat(outSpace, filename);
111 strcat(outSpace, ".zst");
112 return (char*)outSpace;
115 #define CLEVEL_DEFAULT 5
116 int main(int argc, const char** argv)
118 const char* const exeName = argv[0];
119 if (argc<3 || argc>4) {
120 printf("wrong arguments \n");
122 printf("%s FILE FRAME_SIZE [LEVEL] \n", exeName);
126 { const char* const inFileName = argv[1];
127 unsigned const frameSize = (unsigned)atoi(argv[2]);
128 int const cLevel = (argc==4) ? atoi(argv[3]) : CLEVEL_DEFAULT;
130 char* const outFileName = createOutFilename_orDie(inFileName);
131 compressFile_orDie(inFileName, outFileName, cLevel, frameSize);