Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. /*
  2. * Copyright (c) 2016-present, 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.  
  11.  
  12. #include <stdlib.h> // malloc, free, exit
  13. #include <stdio.h> // fprintf, perror, feof, fopen, etc.
  14. #include <string.h> // strlen, memset, strcat
  15. # define ZSTD_STATIC_LINKING_ONLY
  16. #include <zstd.h> // presumes zstd library is installed
  17. #include "utils.h"
  18.  
  19. static void compressFile_orDie(const char* fname, const char* outName, int cLevel)
  20. {
  21. FILE* const fin = fopen_orDie(fname, "rb");
  22. FILE* const fout = fopen_orDie(outName, "wb");
  23. size_t const buffInSize = ZSTD_CStreamInSize(); /* can always read one full block */
  24. void* const buffIn = malloc_orDie(buffInSize);
  25. size_t const buffOutSize = ZSTD_CStreamOutSize(); /* can always flush a full block */
  26. void* const buffOut = malloc_orDie(buffOutSize);
  27.  
  28. ZSTD_CStream* const cstream = ZSTD_createCStream();
  29. if (cstream==NULL) { fprintf(stderr, "ZSTD_createCStream() error \n"); exit(10); }
  30.  
  31. // MY CODE START
  32. #ifdef ZSTD_MULTITHREAD
  33. assert(!ZSTD_isError(ZSTD_CCtx_setParameter(cstream, ZSTD_c_nbWorkers, 2)));
  34. #endif
  35.  
  36. // size_t const initResult = ZSTD_initCStream(cstream, cLevel);
  37. // if (ZSTD_isError(initResult)) {
  38. // fprintf(stderr, "ZSTD_initCStream() error : %s \n",
  39. // ZSTD_getErrorName(initResult));
  40. // exit(11);
  41. // }
  42. // MY CODE END
  43.  
  44. size_t read, toRead = buffInSize;
  45. while( (read = fread_orDie(buffIn, toRead, fin)) ) {
  46. ZSTD_inBuffer input = { buffIn, read, 0 };
  47. while (input.pos < input.size) {
  48. ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
  49. toRead = ZSTD_compressStream(cstream, &output , &input); /* toRead is guaranteed to be <= ZSTD_CStreamInSize() */
  50. if (ZSTD_isError(toRead)) {
  51. fprintf(stderr, "ZSTD_compressStream() error : %s \n",
  52. ZSTD_getErrorName(toRead));
  53. exit(12);
  54. }
  55. if (toRead > buffInSize) toRead = buffInSize; /* Safely handle case when `buffInSize` is manually changed to a value < ZSTD_CStreamInSize()*/
  56. fwrite_orDie(buffOut, output.pos, fout);
  57. }
  58. }
  59.  
  60. ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
  61. size_t const remainingToFlush = ZSTD_endStream(cstream, &output); /* close frame */
  62. if (remainingToFlush) { fprintf(stderr, "not fully flushed"); exit(13); }
  63. fwrite_orDie(buffOut, output.pos, fout);
  64.  
  65. ZSTD_freeCStream(cstream);
  66. fclose_orDie(fout);
  67. fclose_orDie(fin); free(buffIn);
  68. free(buffOut);
  69. }
  70.  
  71.  
  72. static char* createOutFilename_orDie(const char* filename)
  73. {
  74. size_t const inL = strlen(filename);
  75. size_t const outL = inL + 5;
  76. void* const outSpace = malloc_orDie(outL);
  77. memset(outSpace, 0, outL);
  78. strcat(outSpace, filename);
  79. strcat(outSpace, ".zst");
  80. return (char*)outSpace;
  81. }
  82.  
  83. int main(int argc, const char** argv)
  84. {
  85. const char* const exeName = argv[0];
  86.  
  87. if (argc!=2) {
  88. printf("wrong arguments\n");
  89. printf("usage:\n");
  90. printf("%s FILE\n", exeName);
  91. return 1;
  92. }
  93.  
  94. const char* const inFilename = argv[1];
  95.  
  96. char* const outFilename = createOutFilename_orDie(inFilename);
  97. compressFile_orDie(inFilename, outFilename, 1);
  98.  
  99. free(outFilename); /* not strictly required, since program execution stops there,
  100. * but some static analyzer main complain otherwise */
  101. return 0;
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement