dictionary_decompression.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 <zstd.h> // presumes zstd library is installed
  13. #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
  14. /* createDict() :
  15. `dictFileName` is supposed to have been created using `zstd --train` */
  16. static ZSTD_DDict* createDict_orDie(const char* dictFileName)
  17. {
  18. size_t dictSize;
  19. printf("loading dictionary %s \n", dictFileName);
  20. void* const dictBuffer = mallocAndLoadFile_orDie(dictFileName, &dictSize);
  21. ZSTD_DDict* const ddict = ZSTD_createDDict(dictBuffer, dictSize);
  22. CHECK(ddict != NULL, "ZSTD_createDDict() failed!");
  23. free(dictBuffer);
  24. return ddict;
  25. }
  26. static void decompress(const char* fname, const ZSTD_DDict* ddict)
  27. {
  28. size_t cSize;
  29. void* const cBuff = mallocAndLoadFile_orDie(fname, &cSize);
  30. /* Read the content size from the frame header. For simplicity we require
  31. * that it is always present. By default, zstd will write the content size
  32. * in the header when it is known. If you can't guarantee that the frame
  33. * content size is always written into the header, either use streaming
  34. * decompression, or ZSTD_decompressBound().
  35. */
  36. unsigned long long const rSize = ZSTD_getFrameContentSize(cBuff, cSize);
  37. CHECK(rSize != ZSTD_CONTENTSIZE_ERROR, "%s: not compressed by zstd!", fname);
  38. CHECK(rSize != ZSTD_CONTENTSIZE_UNKNOWN, "%s: original size unknown!", fname);
  39. void* const rBuff = malloc_orDie((size_t)rSize);
  40. /* Check that the dictionary ID matches.
  41. * If a non-zstd dictionary is used, then both will be zero.
  42. * By default zstd always writes the dictionary ID into the frame.
  43. * Zstd will check if there is a dictionary ID mismatch as well.
  44. */
  45. unsigned const expectedDictID = ZSTD_getDictID_fromDDict(ddict);
  46. unsigned const actualDictID = ZSTD_getDictID_fromFrame(cBuff, cSize);
  47. CHECK(actualDictID == expectedDictID,
  48. "DictID mismatch: expected %u got %u",
  49. expectedDictID,
  50. actualDictID);
  51. /* Decompress using the dictionary.
  52. * If you need to control the decompression parameters, then use the
  53. * advanced API: ZSTD_DCtx_setParameter(), ZSTD_DCtx_refDDict(), and
  54. * ZSTD_decompressDCtx().
  55. */
  56. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  57. CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
  58. size_t const dSize = ZSTD_decompress_usingDDict(dctx, rBuff, rSize, cBuff, cSize, ddict);
  59. CHECK_ZSTD(dSize);
  60. /* When zstd knows the content size, it will error if it doesn't match. */
  61. CHECK(dSize == rSize, "Impossible because zstd will check this condition!");
  62. /* success */
  63. printf("%25s : %6u -> %7u \n", fname, (unsigned)cSize, (unsigned)rSize);
  64. ZSTD_freeDCtx(dctx);
  65. free(rBuff);
  66. free(cBuff);
  67. }
  68. int main(int argc, const char** argv)
  69. {
  70. const char* const exeName = argv[0];
  71. if (argc<3) {
  72. printf("wrong arguments\n");
  73. printf("usage:\n");
  74. printf("%s [FILES] dictionary\n", exeName);
  75. return 1;
  76. }
  77. /* load dictionary only once */
  78. const char* const dictName = argv[argc-1];
  79. ZSTD_DDict* const dictPtr = createDict_orDie(dictName);
  80. int u;
  81. for (u=1; u<argc-1; u++) decompress(argv[u], dictPtr);
  82. ZSTD_freeDDict(dictPtr);
  83. printf("All %u files correctly decoded (in memory) \n", argc-2);
  84. return 0;
  85. }