streaming_compression.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, strlen
  13. #include <zstd.h> // presumes zstd library is installed
  14. #include "common.h" // Helper functions, CHECK(), and CHECK_ZSTD()
  15. static void compressFile_orDie(const char* fname, const char* outName, int cLevel,
  16. int nbThreads)
  17. {
  18. fprintf (stderr, "Starting compression of %s with level %d, using %d threads\n",
  19. fname, cLevel, nbThreads);
  20. /* Open the input and output files. */
  21. FILE* const fin = fopen_orDie(fname, "rb");
  22. FILE* const fout = fopen_orDie(outName, "wb");
  23. /* Create the input and output buffers.
  24. * They may be any size, but we recommend using these functions to size them.
  25. * Performance will only suffer significantly for very tiny buffers.
  26. */
  27. size_t const buffInSize = ZSTD_CStreamInSize();
  28. void* const buffIn = malloc_orDie(buffInSize);
  29. size_t const buffOutSize = ZSTD_CStreamOutSize();
  30. void* const buffOut = malloc_orDie(buffOutSize);
  31. /* Create the context. */
  32. ZSTD_CCtx* const cctx = ZSTD_createCCtx();
  33. CHECK(cctx != NULL, "ZSTD_createCCtx() failed!");
  34. /* Set any parameters you want.
  35. * Here we set the compression level, and enable the checksum.
  36. */
  37. CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, cLevel) );
  38. CHECK_ZSTD( ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1) );
  39. ZSTD_CCtx_setParameter(cctx, ZSTD_c_nbWorkers, nbThreads);
  40. /* This loop read from the input file, compresses that entire chunk,
  41. * and writes all output produced to the output file.
  42. */
  43. size_t const toRead = buffInSize;
  44. for (;;) {
  45. size_t read = fread_orDie(buffIn, toRead, fin);
  46. /* Select the flush mode.
  47. * If the read may not be finished (read == toRead) we use
  48. * ZSTD_e_continue. If this is the last chunk, we use ZSTD_e_end.
  49. * Zstd optimizes the case where the first flush mode is ZSTD_e_end,
  50. * since it knows it is compressing the entire source in one pass.
  51. */
  52. int const lastChunk = (read < toRead);
  53. ZSTD_EndDirective const mode = lastChunk ? ZSTD_e_end : ZSTD_e_continue;
  54. /* Set the input buffer to what we just read.
  55. * We compress until the input buffer is empty, each time flushing the
  56. * output.
  57. */
  58. ZSTD_inBuffer input = { buffIn, read, 0 };
  59. int finished;
  60. do {
  61. /* Compress into the output buffer and write all of the output to
  62. * the file so we can reuse the buffer next iteration.
  63. */
  64. ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
  65. size_t const remaining = ZSTD_compressStream2(cctx, &output , &input, mode);
  66. CHECK_ZSTD(remaining);
  67. fwrite_orDie(buffOut, output.pos, fout);
  68. /* If we're on the last chunk we're finished when zstd returns 0,
  69. * which means its consumed all the input AND finished the frame.
  70. * Otherwise, we're finished when we've consumed all the input.
  71. */
  72. finished = lastChunk ? (remaining == 0) : (input.pos == input.size);
  73. } while (!finished);
  74. CHECK(input.pos == input.size,
  75. "Impossible: zstd only returns 0 when the input is completely consumed!");
  76. if (lastChunk) {
  77. break;
  78. }
  79. }
  80. ZSTD_freeCCtx(cctx);
  81. fclose_orDie(fout);
  82. fclose_orDie(fin);
  83. free(buffIn);
  84. free(buffOut);
  85. }
  86. static char* createOutFilename_orDie(const char* filename)
  87. {
  88. size_t const inL = strlen(filename);
  89. size_t const outL = inL + 5;
  90. void* const outSpace = malloc_orDie(outL);
  91. memset(outSpace, 0, outL);
  92. strcat(outSpace, filename);
  93. strcat(outSpace, ".zst");
  94. return (char*)outSpace;
  95. }
  96. int main(int argc, const char** argv)
  97. {
  98. const char* const exeName = argv[0];
  99. if (argc < 2) {
  100. printf("wrong arguments\n");
  101. printf("usage:\n");
  102. printf("%s FILE [LEVEL] [THREADS]\n", exeName);
  103. return 1;
  104. }
  105. int cLevel = 1;
  106. int nbThreads = 4;
  107. if (argc >= 3) {
  108. cLevel = atoi (argv[2]);
  109. CHECK(cLevel != 0, "can't parse LEVEL!");
  110. }
  111. if (argc >= 4) {
  112. nbThreads = atoi (argv[3]);
  113. CHECK(nbThreads != 0, "can't parse THREADS!");
  114. }
  115. const char* const inFilename = argv[1];
  116. char* const outFilename = createOutFilename_orDie(inFilename);
  117. compressFile_orDie(inFilename, outFilename, cLevel, nbThreads);
  118. free(outFilename); /* not strictly required, since program execution stops there,
  119. * but some static analyzer may complain otherwise */
  120. return 0;
  121. }