2 * Copyright (c) 2017-present, Facebook, Inc.
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)
24 #define XXH_NAMESPACE ZSTD_
27 #include "pool.h" // use zstd thread pool for demo
29 #include "zstd_seekable.h"
31 static void* malloc_orDie(size_t size)
33 void* const buff = malloc(size);
34 if (buff) return buff;
40 static FILE* fopen_orDie(const char *filename, const char *instruction)
42 FILE* const inFile = fopen(filename, instruction);
43 if (inFile) return inFile;
49 static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
51 size_t const readSize = fread(buffer, 1, sizeToRead, file);
52 if (readSize == sizeToRead) return readSize; /* good */
53 if (feof(file)) return readSize; /* good, reached end of file */
59 static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
61 size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
62 if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
68 static size_t fclose_orDie(FILE* file)
70 if (!fclose(file)) return 0;
76 static void fseek_orDie(FILE* file, long int offset, int origin)
78 if (!fseek(file, offset, origin)) {
79 if (!fflush(file)) return;
86 static long int ftell_orDie(FILE* file)
88 long int off = ftell(file);
89 if (off != -1) return off;
103 int compressionLevel;
107 static void compressFrame(void* opaque)
109 struct job* job = opaque;
111 job->checksum = XXH64(job->src, job->srcSize, 0);
113 size_t ret = ZSTD_compress(job->dst, job->dstSize, job->src, job->srcSize, job->compressionLevel);
114 if (ZSTD_isError(ret)) {
115 fprintf(stderr, "ZSTD_compress() error : %s \n", ZSTD_getErrorName(ret));
123 static void compressFile_orDie(const char* fname, const char* outName, int cLevel, unsigned frameSize, int nbThreads)
125 POOL_ctx* pool = POOL_create(nbThreads, nbThreads);
126 if (pool == NULL) { fprintf(stderr, "POOL_create() error \n"); exit(9); }
128 FILE* const fin = fopen_orDie(fname, "rb");
129 FILE* const fout = fopen_orDie(outName, "wb");
131 if (ZSTD_compressBound(frameSize) > 0xFFFFFFFFU) { fprintf(stderr, "Frame size too large \n"); exit(10); }
132 unsigned dstSize = ZSTD_compressBound(frameSize);
135 fseek_orDie(fin, 0, SEEK_END);
136 long int length = ftell_orDie(fin);
137 fseek_orDie(fin, 0, SEEK_SET);
139 size_t numFrames = (length + frameSize - 1) / frameSize;
141 struct job* jobs = malloc_orDie(sizeof(struct job) * numFrames);
144 for(i = 0; i < numFrames; i++) {
145 void* in = malloc_orDie(frameSize);
146 void* out = malloc_orDie(dstSize);
148 size_t inSize = fread_orDie(in, frameSize, fin);
151 jobs[i].srcSize = inSize;
153 jobs[i].dstSize = dstSize;
154 jobs[i].compressionLevel = cLevel;
156 POOL_add(pool, compressFrame, &jobs[i]);
159 ZSTD_frameLog* fl = ZSTD_seekable_createFrameLog(1);
160 if (fl == NULL) { fprintf(stderr, "ZSTD_seekable_createFrameLog() failed \n"); exit(11); }
161 for (i = 0; i < numFrames; i++) {
162 while (!jobs[i].done) SLEEP(5); /* wake up every 5 milliseconds to check */
163 fwrite_orDie(jobs[i].dst, jobs[i].dstSize, fout);
164 free((void*)jobs[i].src);
167 size_t ret = ZSTD_seekable_logFrame(fl, jobs[i].dstSize, jobs[i].srcSize, jobs[i].checksum);
168 if (ZSTD_isError(ret)) { fprintf(stderr, "ZSTD_seekable_logFrame() error : %s \n", ZSTD_getErrorName(ret)); }
171 { unsigned char seekTableBuff[1024];
172 ZSTD_outBuffer out = {seekTableBuff, 1024, 0};
173 while (ZSTD_seekable_writeSeekTable(fl, &out) != 0) {
174 fwrite_orDie(seekTableBuff, out.pos, fout);
177 fwrite_orDie(seekTableBuff, out.pos, fout);
180 ZSTD_seekable_freeFrameLog(fl);
186 static const char* createOutFilename_orDie(const char* filename)
188 size_t const inL = strlen(filename);
189 size_t const outL = inL + 5;
190 void* outSpace = malloc_orDie(outL);
191 memset(outSpace, 0, outL);
192 strcat(outSpace, filename);
193 strcat(outSpace, ".zst");
194 return (const char*)outSpace;
197 int main(int argc, const char** argv) {
198 const char* const exeName = argv[0];
200 printf("wrong arguments\n");
202 printf("%s FILE FRAME_SIZE NB_THREADS\n", exeName);
206 { const char* const inFileName = argv[1];
207 unsigned const frameSize = (unsigned)atoi(argv[2]);
208 int const nbThreads = atoi(argv[3]);
210 const char* const outFileName = createOutFilename_orDie(inFileName);
211 compressFile_orDie(inFileName, outFileName, 5, frameSize, nbThreads);