streaming_decompression.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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> // fprintf
  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. static void decompressFile_orDie(const char* fname)
  15. {
  16. FILE* const fin = fopen_orDie(fname, "rb");
  17. size_t const buffInSize = ZSTD_DStreamInSize();
  18. void* const buffIn = malloc_orDie(buffInSize);
  19. FILE* const fout = stdout;
  20. size_t const buffOutSize = ZSTD_DStreamOutSize(); /* Guarantee to successfully flush at least one complete compressed block in all circumstances. */
  21. void* const buffOut = malloc_orDie(buffOutSize);
  22. ZSTD_DCtx* const dctx = ZSTD_createDCtx();
  23. CHECK(dctx != NULL, "ZSTD_createDCtx() failed!");
  24. /* This loop assumes that the input file is one or more concatenated zstd
  25. * streams. This example won't work if there is trailing non-zstd data at
  26. * the end, but streaming decompression in general handles this case.
  27. * ZSTD_decompressStream() returns 0 exactly when the frame is completed,
  28. * and doesn't consume input after the frame.
  29. */
  30. size_t const toRead = buffInSize;
  31. size_t read;
  32. size_t lastRet = 0;
  33. int isEmpty = 1;
  34. while ( (read = fread_orDie(buffIn, toRead, fin)) ) {
  35. isEmpty = 0;
  36. ZSTD_inBuffer input = { buffIn, read, 0 };
  37. /* Given a valid frame, zstd won't consume the last byte of the frame
  38. * until it has flushed all of the decompressed data of the frame.
  39. * Therefore, instead of checking if the return code is 0, we can
  40. * decompress just check if input.pos < input.size.
  41. */
  42. while (input.pos < input.size) {
  43. ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
  44. /* The return code is zero if the frame is complete, but there may
  45. * be multiple frames concatenated together. Zstd will automatically
  46. * reset the context when a frame is complete. Still, calling
  47. * ZSTD_DCtx_reset() can be useful to reset the context to a clean
  48. * state, for instance if the last decompression call returned an
  49. * error.
  50. */
  51. size_t const ret = ZSTD_decompressStream(dctx, &output , &input);
  52. CHECK_ZSTD(ret);
  53. fwrite_orDie(buffOut, output.pos, fout);
  54. lastRet = ret;
  55. }
  56. }
  57. if (isEmpty) {
  58. fprintf(stderr, "input is empty\n");
  59. exit(1);
  60. }
  61. if (lastRet != 0) {
  62. /* The last return value from ZSTD_decompressStream did not end on a
  63. * frame, but we reached the end of the file! We assume this is an
  64. * error, and the input was truncated.
  65. */
  66. fprintf(stderr, "EOF before end of stream: %zu\n", lastRet);
  67. exit(1);
  68. }
  69. ZSTD_freeDCtx(dctx);
  70. fclose_orDie(fin);
  71. fclose_orDie(fout);
  72. free(buffIn);
  73. free(buffOut);
  74. }
  75. int main(int argc, const char** argv)
  76. {
  77. const char* const exeName = argv[0];
  78. if (argc!=2) {
  79. fprintf(stderr, "wrong arguments\n");
  80. fprintf(stderr, "usage:\n");
  81. fprintf(stderr, "%s FILE\n", exeName);
  82. return 1;
  83. }
  84. const char* const inFilename = argv[1];
  85. decompressFile_orDie(inFilename);
  86. return 0;
  87. }