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
15 #include <zstd_errors.h>
16 #if defined(WIN32) || defined(_WIN32)
18 # define SLEEP(x) Sleep(x)
21 # define SLEEP(x) usleep(x * 1000)
26 #include "pool.h" // use zstd thread pool for demo
28 #include "../zstd_seekable.h"
30 static void* malloc_orDie(size_t size)
32 void* const buff = malloc(size);
33 if (buff) return buff;
39 static FILE* fopen_orDie(const char *filename, const char *instruction)
41 FILE* const inFile = fopen(filename, instruction);
42 if (inFile) return inFile;
48 static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
50 size_t const readSize = fread(buffer, 1, sizeToRead, file);
51 if (readSize == sizeToRead) return readSize; /* good */
52 if (feof(file)) return readSize; /* good, reached end of file */
58 static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
60 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
61 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
67 static size_t fclose_orDie(FILE* file)
69 if (!fclose(file)) return 0;
75 static void fseek_orDie(FILE* file, long int offset, int origin)
77 if (!fseek(file, offset, origin)) {
78 if (!fflush(file)) return;
85 static long int ftell_orDie(FILE* file)
87 long int off = ftell(file);
88 if (off != -1) return off;
102 int compressionLevel;
106 static void compressFrame(void* opaque)
108 struct job* job = opaque;
110 job->checksum = XXH64(job->src, job->srcSize, 0);
112 size_t ret = ZSTD_compress(job->dst, job->dstSize, job->src, job->srcSize, job->compressionLevel);
113 if (ZSTD_isError(ret)) {
114 fprintf(stderr, "ZSTD_compress() error : %s \n", ZSTD_getErrorName(ret));
122 static void compressFile_orDie(const char* fname, const char* outName, int cLevel, unsigned frameSize, int nbThreads)
124 POOL_ctx* pool = POOL_create(nbThreads, nbThreads);
125 if (pool == NULL) { fprintf(stderr, "POOL_create() error \n"); exit(9); }
127 FILE* const fin = fopen_orDie(fname, "rb");
128 FILE* const fout = fopen_orDie(outName, "wb");
130 if (ZSTD_compressBound(frameSize) > 0xFFFFFFFFU) { fprintf(stderr, "Frame size too large \n"); exit(10); }
131 unsigned dstSize = ZSTD_compressBound(frameSize);
134 fseek_orDie(fin, 0, SEEK_END);
135 long int length = ftell_orDie(fin);
136 fseek_orDie(fin, 0, SEEK_SET);
138 size_t numFrames = (length + frameSize - 1) / frameSize;
140 struct job* jobs = malloc_orDie(sizeof(struct job) * numFrames);
143 for(i = 0; i < numFrames; i++) {
144 void* in = malloc_orDie(frameSize);
145 void* out = malloc_orDie(dstSize);
147 size_t inSize = fread_orDie(in, frameSize, fin);
150 jobs[i].srcSize = inSize;
152 jobs[i].dstSize = dstSize;
153 jobs[i].compressionLevel = cLevel;
155 POOL_add(pool, compressFrame, &jobs[i]);
158 ZSTD_frameLog* fl = ZSTD_seekable_createFrameLog(1);
159 if (fl == NULL) { fprintf(stderr, "ZSTD_seekable_createFrameLog() failed \n"); exit(11); }
160 for (i = 0; i < numFrames; i++) {
161 while (!jobs[i].done) SLEEP(5); /* wake up every 5 milliseconds to check */
162 fwrite_orDie(jobs[i].dst, jobs[i].dstSize, fout);
163 free((void*)jobs[i].src);
166 size_t ret = ZSTD_seekable_logFrame(fl, jobs[i].dstSize, jobs[i].srcSize, jobs[i].checksum);
167 if (ZSTD_isError(ret)) { fprintf(stderr, "ZSTD_seekable_logFrame() error : %s \n", ZSTD_getErrorName(ret)); }
170 { unsigned char seekTableBuff[1024];
171 ZSTD_outBuffer out = {seekTableBuff, 1024, 0};
172 while (ZSTD_seekable_writeSeekTable(fl, &out) != 0) {
173 fwrite_orDie(seekTableBuff, out.pos, fout);
176 fwrite_orDie(seekTableBuff, out.pos, fout);
179 ZSTD_seekable_freeFrameLog(fl);
185 static const char* createOutFilename_orDie(const char* filename)
187 size_t const inL = strlen(filename);
188 size_t const outL = inL + 5;
189 void* outSpace = malloc_orDie(outL);
190 memset(outSpace, 0, outL);
191 strcat(outSpace, filename);
192 strcat(outSpace, ".zst");
193 return (const char*)outSpace;
196 int main(int argc, const char** argv) {
197 const char* const exeName = argv[0];
199 printf("wrong arguments\n");
201 printf("%s FILE FRAME_SIZE NB_THREADS\n", exeName);
205 { const char* const inFileName = argv[1];
206 unsigned const frameSize = (unsigned)atoi(argv[2]);
207 int const nbThreads = atoi(argv[3]);
209 const char* const outFileName = createOutFilename_orDie(inFileName);
210 compressFile_orDie(inFileName, outFileName, 5, frameSize, nbThreads);