seekable_compression.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (c) 2017-present, 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. */
  9. #include <stdlib.h> // malloc, free, exit, atoi
  10. #include <stdio.h> // fprintf, perror, feof, fopen, etc.
  11. #include <string.h> // strlen, memset, strcat
  12. #define ZSTD_STATIC_LINKING_ONLY
  13. #include <zstd.h> // presumes zstd library is installed
  14. #include "zstd_seekable.h"
  15. static void* malloc_orDie(size_t size)
  16. {
  17. void* const buff = malloc(size);
  18. if (buff) return buff;
  19. /* error */
  20. perror("malloc:");
  21. exit(1);
  22. }
  23. static FILE* fopen_orDie(const char *filename, const char *instruction)
  24. {
  25. FILE* const inFile = fopen(filename, instruction);
  26. if (inFile) return inFile;
  27. /* error */
  28. perror(filename);
  29. exit(3);
  30. }
  31. static size_t fread_orDie(void* buffer, size_t sizeToRead, FILE* file)
  32. {
  33. size_t const readSize = fread(buffer, 1, sizeToRead, file);
  34. if (readSize == sizeToRead) return readSize; /* good */
  35. if (feof(file)) return readSize; /* good, reached end of file */
  36. /* error */
  37. perror("fread");
  38. exit(4);
  39. }
  40. static size_t fwrite_orDie(const void* buffer, size_t sizeToWrite, FILE* file)
  41. {
  42. size_t const writtenSize = fwrite(buffer, 1, sizeToWrite, file);
  43. if (writtenSize == sizeToWrite) return sizeToWrite; /* good */
  44. /* error */
  45. perror("fwrite");
  46. exit(5);
  47. }
  48. static size_t fclose_orDie(FILE* file)
  49. {
  50. if (!fclose(file)) return 0;
  51. /* error */
  52. perror("fclose");
  53. exit(6);
  54. }
  55. static void compressFile_orDie(const char* fname, const char* outName, int cLevel, unsigned frameSize)
  56. {
  57. FILE* const fin = fopen_orDie(fname, "rb");
  58. FILE* const fout = fopen_orDie(outName, "wb");
  59. size_t const buffInSize = ZSTD_CStreamInSize(); /* can always read one full block */
  60. void* const buffIn = malloc_orDie(buffInSize);
  61. size_t const buffOutSize = ZSTD_CStreamOutSize(); /* can always flush a full block */
  62. void* const buffOut = malloc_orDie(buffOutSize);
  63. ZSTD_seekable_CStream* const cstream = ZSTD_seekable_createCStream();
  64. if (cstream==NULL) { fprintf(stderr, "ZSTD_seekable_createCStream() error \n"); exit(10); }
  65. size_t const initResult = ZSTD_seekable_initCStream(cstream, cLevel, 1, frameSize);
  66. if (ZSTD_isError(initResult)) { fprintf(stderr, "ZSTD_seekable_initCStream() error : %s \n", ZSTD_getErrorName(initResult)); exit(11); }
  67. size_t read, toRead = buffInSize;
  68. while( (read = fread_orDie(buffIn, toRead, fin)) ) {
  69. ZSTD_inBuffer input = { buffIn, read, 0 };
  70. while (input.pos < input.size) {
  71. ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
  72. toRead = ZSTD_seekable_compressStream(cstream, &output , &input); /* toRead is guaranteed to be <= ZSTD_CStreamInSize() */
  73. if (ZSTD_isError(toRead)) { fprintf(stderr, "ZSTD_seekable_compressStream() error : %s \n", ZSTD_getErrorName(toRead)); exit(12); }
  74. if (toRead > buffInSize) toRead = buffInSize; /* Safely handle case when `buffInSize` is manually changed to a value < ZSTD_CStreamInSize()*/
  75. fwrite_orDie(buffOut, output.pos, fout);
  76. }
  77. }
  78. while (1) {
  79. ZSTD_outBuffer output = { buffOut, buffOutSize, 0 };
  80. size_t const remainingToFlush = ZSTD_seekable_endStream(cstream, &output); /* close stream */
  81. if (ZSTD_isError(remainingToFlush)) { fprintf(stderr, "ZSTD_seekable_endStream() error : %s \n", ZSTD_getErrorName(remainingToFlush)); exit(13); }
  82. fwrite_orDie(buffOut, output.pos, fout);
  83. if (!remainingToFlush) break;
  84. }
  85. ZSTD_seekable_freeCStream(cstream);
  86. fclose_orDie(fout);
  87. fclose_orDie(fin);
  88. free(buffIn);
  89. free(buffOut);
  90. }
  91. static char* createOutFilename_orDie(const char* filename)
  92. {
  93. size_t const inL = strlen(filename);
  94. size_t const outL = inL + 5;
  95. void* outSpace = malloc_orDie(outL);
  96. memset(outSpace, 0, outL);
  97. strcat(outSpace, filename);
  98. strcat(outSpace, ".zst");
  99. return (char*)outSpace;
  100. }
  101. int main(int argc, const char** argv) {
  102. const char* const exeName = argv[0];
  103. if (argc!=3) {
  104. printf("wrong arguments\n");
  105. printf("usage:\n");
  106. printf("%s FILE FRAME_SIZE\n", exeName);
  107. return 1;
  108. }
  109. { const char* const inFileName = argv[1];
  110. unsigned const frameSize = (unsigned)atoi(argv[2]);
  111. char* const outFileName = createOutFilename_orDie(inFileName);
  112. compressFile_orDie(inFileName, outFileName, 5, frameSize);
  113. free(outFileName);
  114. }
  115. return 0;
  116. }