Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 394bdd7 | 2017-08-29 09:24:11 -0700 | [diff] [blame] | 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). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 9 | **/ |
| 10 | |
| 11 | /* This example deals with Dictionary compression, |
| 12 | * its counterpart is `examples/dictionary_decompression.c` . |
| 13 | * These examples presume that a dictionary already exists. |
| 14 | * The main method to create a dictionary is `zstd --train`, |
| 15 | * look at the CLI documentation for details. |
| 16 | * Another possible method is to employ dictionary training API, |
| 17 | * published in `lib/zdict.h` . |
| 18 | **/ |
| 19 | |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 20 | #include <stdio.h> // printf |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 21 | #include <stdlib.h> // free |
| 22 | #include <string.h> // memset, strcat |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 23 | #include <zstd.h> // presumes zstd library is installed |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 24 | #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD() |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 25 | |
| 26 | /* createDict() : |
Elliott Hughes | 44aba64 | 2023-09-12 20:18:59 +0000 | [diff] [blame] | 27 | ** `dictFileName` is supposed already created using `zstd --train` */ |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 28 | static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel) |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 29 | { |
| 30 | size_t dictSize; |
| 31 | printf("loading dictionary %s \n", dictFileName); |
Yi Jin | bc4dc60 | 2018-12-17 16:54:55 -0800 | [diff] [blame] | 32 | void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize); |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 33 | ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 34 | CHECK(cdict != NULL, "ZSTD_createCDict() failed!"); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 35 | free(dictBuffer); |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 36 | return cdict; |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | |
| 40 | static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict) |
| 41 | { |
| 42 | size_t fSize; |
Yi Jin | bc4dc60 | 2018-12-17 16:54:55 -0800 | [diff] [blame] | 43 | void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 44 | size_t const cBuffSize = ZSTD_compressBound(fSize); |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 45 | void* const cBuff = malloc_orDie(cBuffSize); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 46 | |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 47 | /* Compress using the dictionary. |
| 48 | * This function writes the dictionary id, and content size into the header. |
| 49 | * But, it doesn't use a checksum. You can control these options using the |
| 50 | * advanced API: ZSTD_CCtx_setParameter(), ZSTD_CCtx_refCDict(), |
| 51 | * and ZSTD_compress2(). |
| 52 | */ |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 53 | ZSTD_CCtx* const cctx = ZSTD_createCCtx(); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 54 | CHECK(cctx != NULL, "ZSTD_createCCtx() failed!"); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 55 | size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict); |
Nick Terrell | 1d0c170 | 2019-04-05 18:11:17 -0700 | [diff] [blame] | 56 | CHECK_ZSTD(cSize); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 57 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 58 | saveFile_orDie(oname, cBuff, cSize); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 59 | |
| 60 | /* success */ |
| 61 | printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname); |
| 62 | |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 63 | ZSTD_freeCCtx(cctx); /* never fails */ |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 64 | free(fBuff); |
| 65 | free(cBuff); |
| 66 | } |
| 67 | |
| 68 | |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 69 | static char* createOutFilename_orDie(const char* filename) |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 70 | { |
| 71 | size_t const inL = strlen(filename); |
| 72 | size_t const outL = inL + 5; |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 73 | void* outSpace = malloc_orDie(outL); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 74 | memset(outSpace, 0, outL); |
| 75 | strcat(outSpace, filename); |
| 76 | strcat(outSpace, ".zst"); |
| 77 | return (char*)outSpace; |
| 78 | } |
| 79 | |
| 80 | int main(int argc, const char** argv) |
| 81 | { |
| 82 | const char* const exeName = argv[0]; |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 83 | int const cLevel = 3; |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 84 | |
| 85 | if (argc<3) { |
| 86 | fprintf(stderr, "wrong arguments\n"); |
| 87 | fprintf(stderr, "usage:\n"); |
| 88 | fprintf(stderr, "%s [FILES] dictionary\n", exeName); |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | /* load dictionary only once */ |
| 93 | const char* const dictName = argv[argc-1]; |
Yann Collet | a266464 | 2016-09-09 19:33:56 +0200 | [diff] [blame] | 94 | ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 95 | |
| 96 | int u; |
| 97 | for (u=1; u<argc-1; u++) { |
| 98 | const char* inFilename = argv[u]; |
Yann Collet | 373d4f9 | 2016-08-01 17:36:11 +0200 | [diff] [blame] | 99 | char* const outFilename = createOutFilename_orDie(inFilename); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 100 | compress(inFilename, outFilename, dictPtr); |
| 101 | free(outFilename); |
| 102 | } |
| 103 | |
Yann Collet | 0b2d682 | 2016-08-01 17:39:06 +0200 | [diff] [blame] | 104 | ZSTD_freeCDict(dictPtr); |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 105 | printf("All %u files compressed. \n", argc-2); |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 106 | return 0; |
Yann Collet | cadd7cd | 2016-07-15 18:52:37 +0200 | [diff] [blame] | 107 | } |