dictionary_compression.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) Yann Collet, Facebook, Inc.
  3. * All rights reserved.
  4. *
  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).
  8. * You may select, at your option, one of the above-listed licenses.
  9. */
  10. #include <stdio.h> // printf
  11. #include <stdlib.h> // free
  12. #include <string.h> // memset, strcat
  13. #include <zstd.h> // presumes zstd library is installed
  14. #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
  15. /* createDict() :
  16. `dictFileName` is supposed to have been created using `zstd --train` */
  17. static ZSTD_CDict* createCDict_orDie(const char* dictFileName, int cLevel)
  18. {
  19. size_t dictSize;
  20. printf("loading dictionary %s \n", dictFileName);
  21. void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
  22. ZSTD_CDict* const cdict = ZSTD_createCDict(dictBuffer, dictSize, cLevel);
  23. CHECK(cdict != NULL, "ZSTD_createCDict() failed!");
  24. free(dictBuffer);
  25. return cdict;
  26. }
  27. static void compress(const char* fname, const char* oname, const ZSTD_CDict* cdict)
  28. {
  29. size_t fSize;
  30. void* const fBuff = mallocAndLoadFile_orDie(fname, &fSize);
  31. size_t const cBuffSize = ZSTD_compressBound(fSize);
  32. void* const cBuff = malloc_orDie(cBuffSize);
  33. /* Compress using the dictionary.
  34. * This function writes the dictionary id, and content size into the header.
  35. * But, it doesn't use a checksum. You can control these options using the
  36. * advanced API: ZSTD_CCtx_setParameter(), ZSTD_CCtx_refCDict(),
  37. * and ZSTD_compress2().
  38. */
  39. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  40. CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
  41. size_t const cSize = ZSTD_compress_usingCDict(cctx, cBuff, cBuffSize, fBuff, fSize, cdict);
  42. CHECK_ZSTD(cSize);
  43. saveFile_orDie(oname, cBuff, cSize);
  44. /* success */
  45. printf("%25s : %6u -> %7u - %s \n", fname, (unsigned)fSize, (unsigned)cSize, oname);
  46. ZSTD_freeCCtx(cctx); /* never fails */
  47. free(fBuff);
  48. free(cBuff);
  49. }
  50. static char* createOutFilename_orDie(const char* filename)
  51. {
  52. size_t const inL = strlen(filename);
  53. size_t const outL = inL + 5;
  54. void* outSpace = malloc_orDie(outL);
  55. memset(outSpace, 0, outL);
  56. strcat(outSpace, filename);
  57. strcat(outSpace, ".zst");
  58. return (char*)outSpace;
  59. }
  60. int main(int argc, const char** argv)
  61. {
  62. const char* const exeName = argv[0];
  63. int const cLevel = 3;
  64. if (argc<3) {
  65. fprintf(stderr, "wrong arguments\n");
  66. fprintf(stderr, "usage:\n");
  67. fprintf(stderr, "%s [FILES] dictionary\n", exeName);
  68. return 1;
  69. }
  70. /* load dictionary only once */
  71. const char* const dictName = argv[argc-1];
  72. ZSTD_CDict* const dictPtr = createCDict_orDie(dictName, cLevel);
  73. int u;
  74. for (u=1; u<argc-1; u++) {
  75. const char* inFilename = argv[u];
  76. char* const outFilename = createOutFilename_orDie(inFilename);
  77. compress(inFilename, outFilename, dictPtr);
  78. free(outFilename);
  79. }
  80. ZSTD_freeCDict(dictPtr);
  81. printf("All %u files compressed. \n", argc-2);
  82. return 0;
  83. }