@$(MAKE) -C $(PRGDIR) $@ > $(VOID)
@$(MAKE) -C $(TESTDIR) $@ > $(VOID)
@$(MAKE) -C $(ZWRAPDIR) $@ > $(VOID)
- @rm -f zstd
+ @$(RM) zstd
@echo Cleaning completed
ifneq (,$(filter $(HOST_OS),MSYS POSIX))
cmaketest:
cmake --version
- rm -rf projects/cmake/build
+ $(RM) -r projects/cmake/build
mkdir projects/cmake/build
cd projects/cmake/build ; cmake -DPREFIX:STRING=~/install_test_dir $(CMAKE_PARAMS) .. ; $(MAKE) install ; $(MAKE) uninstall
test: all
cp README.md tmp
+ cp Makefile tmp2
@echo starting simple compression
./simple_compression tmp
./simple_decompression tmp.zst
./streaming_compression tmp
./streaming_decompression tmp.zst > /dev/null
@echo starting dictionary compression
- ./dictionary_compression tmp README.md
- ./dictionary_decompression tmp.zst README.md
+ ./dictionary_compression tmp2 tmp README.md
+ ./dictionary_decompression tmp2.zst tmp.zst README.md
@echo tests completed
/* createDict() :
`dictFileName` is supposed to have been created using `zstd --train` */
-static ZSTD_CDict* createCDict_orDie(const char* dictFileName)
+static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
{
size_t dictSize;
printf("loading dictionary %s \n", dictFileName);
void* const dictBuffer = loadFile_orDie(dictFileName, &dictSize);
- ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, 3);
+ ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
if (!cdict) {
fprintf(stderr, "ZSTD_createCDict error \n");
exit(7);
void* const cBuff = malloc_orDie(cBuffSize);
ZSTD_CCtx* const cctx = ZSTD_createCCtx();
+ if (cctx==NULL) { fprintf(stderr, "ZSTD_createCCtx() error \n"); exit(10); }
size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
if (ZSTD_isError(cSize)) {
fprintf(stderr, "error compressing %s : %s \n", fname, ZSTD_getErrorName(cSize));
/* success */
printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
- ZSTD_freeCCtx(cctx);
+ ZSTD_freeCCtx(cctx); /* never fails */
free(fBuff);
free(cBuff);
}
int main(int argc, const char** argv)
{
const char* const exeName = argv[0];
+ int const cLevel = 3;
if (argc<3) {
fprintf(stderr, "wrong arguments\n");
/* load dictionary only once */
const char* const dictName = argv[argc-1];
- ZSTD_CDict* const dictPtr = createCDict_orDie(dictName);
+ ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel);
int u;
for (u=1; u<argc-1; u++) {
fprintf(stderr, "%s : original size unknown \n", fname);
exit(6);
}
- void* const rBuff = malloc_orDie(rSize);
+ void* const rBuff = malloc_orDie((size_t)rSize);
ZSTD_DCtx* const dctx = ZSTD_createDCtx();
+ if (dctx==NULL) { fprintf(stderr, "ZSTD_createDCtx() error \n"); exit(10); }
size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
-
if (dSize != rSize) {
fprintf(stderr, "error decoding %s : %s \n", fname, ZSTD_getErrorName(dSize));
exit(7);
fprintf(stderr, "fread: %s : %s \n", fileName, strerror(errno));
exit(4);
}
- fclose(inFile);
+ fclose(inFile); /* can't fail, read only */
*size = buffSize;
return buffer;
}
{
size_t const inL = strlen(filename);
size_t const outL = inL + 5;
- void* outSpace = malloc_orDie(outL);
+ void* const outSpace = malloc_orDie(outL);
memset(outSpace, 0, outL);
strcat(outSpace, filename);
strcat(outSpace, ".zst");
printf("fread: %s : %s \n", fileName, strerror(errno));
exit(4);
}
- fclose(inFile);
+ fclose(inFile); /* can't fail (read only) */
*size = buffSize;
return buffer;
}
printf("%s : original size unknown \n", fname);
exit(5);
}
- void* const rBuff = malloc_X(rSize);
+ void* const rBuff = malloc_X((size_t)rSize);
size_t const dSize = ZSTD_decompress(rBuff, rSize, cBuff, cSize);
void* const buffIn = malloc_orDie(buffInSize);
size_t const buffOutSize = ZSTD_CStreamOutSize();;
void* const buffOut = malloc_orDie(buffOutSize);
- size_t read, toRead = buffInSize;
ZSTD_CStream* const cstream = ZSTD_createCStream();
if (cstream==NULL) { fprintf(stderr, "ZSTD_createCStream() error \n"); exit(10); }
size_t const initResult = ZSTD_initCStream(cstream, cLevel);
if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_initCStream() error \n"); exit(11); }
+ size_t read, toRead = buffInSize;
while( (read = fread_orDie(buffIn, toRead, fin)) ) {
ZSTD_inBuffer input = { buffIn, read, 0 };
while (input.pos < input.size) {
}
ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
- size_t const remainingToFlush = ZSTD_endStream(cstream, &output);
+ size_t const remainingToFlush = ZSTD_endStream(cstream, &output); /* close frame */
if (remainingToFlush) { fprintf(stderr, "not fully flushed"); exit(12); }
fwrite_orDie(buffOut, output.pos, fout);
+ ZSTD_freeCStream(cstream);
fclose_orDie(fout);
fclose_orDie(fin);
free(buffIn);
exit(5);
}
-
static size_t fclose_orDie(FILE* file)
{
if (!fclose(file)) return 0;
}
}
+ ZSTD_freeDStream(dstream);
fclose_orDie(fin);
fclose_orDie(fout);
free(buffIn);