filegen.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*********************************************************************
  2. Blosc - Blocked Shuffling and Compression Library
  3. Generator data file for Blosc forward and backward tests.
  4. Creation date: 2018-02-16
  5. Author: Elvis Stansvik, Francesc Alted <francesc@blosc.org>
  6. See LICENSES/BLOSC.txt for details about copyright and rights to use.
  7. **********************************************************************/
  8. #include <stdio.h>
  9. #include <blosc.h>
  10. #include <string.h>
  11. #if defined(_WIN32) && !defined(__MINGW32__)
  12. #include <windows.h>
  13. /* stdint.h only available in VS2010 (VC++ 16.0) and newer */
  14. #if defined(_MSC_VER) && _MSC_VER < 1600
  15. #include "win32/stdint-windows.h"
  16. #else
  17. #include <stdint.h>
  18. #endif
  19. #else
  20. #include <stdint.h>
  21. #endif /* _WIN32 */
  22. #ifdef __HAIKU__
  23. /* int32_t declared here */
  24. #include <stdint.h>
  25. #endif
  26. #define SIZE (1000 * 1000)
  27. int main(int argc, char *argv[]) {
  28. static int32_t data[SIZE];
  29. static int32_t data_out[SIZE];
  30. static int32_t data_dest[SIZE];
  31. size_t isize = SIZE * sizeof(int32_t);
  32. size_t osize = SIZE * sizeof(int32_t);
  33. int dsize = SIZE * sizeof(int32_t);
  34. int csize;
  35. long fsize;
  36. int i;
  37. FILE *f;
  38. /* Register the filter with the library */
  39. printf("Blosc version info: %s (%s)\n", BLOSC_VERSION_STRING, BLOSC_VERSION_DATE);
  40. /* Initialize the Blosc compressor */
  41. blosc_init();
  42. /* Use the argv[2] compressor. The supported ones are "blosclz",
  43. "lz4", "lz4hc", "snappy", "zlib" and "zstd"*/
  44. blosc_set_compressor(argv[2]);
  45. if (strcmp(argv[1], "compress") == 0) {
  46. for (i = 0; i < SIZE; i++) {
  47. data[i] = i;
  48. }
  49. /* Compress with clevel=9 and shuffle active */
  50. csize = blosc_compress(9, 1, sizeof(int32_t), isize, data, data_out, osize);
  51. if (csize == 0) {
  52. printf("Buffer is uncompressible. Giving up.\n");
  53. return 1;
  54. } else if (csize < 0) {
  55. printf("Compression error. Error code: %d\n", csize);
  56. return csize;
  57. }
  58. printf("Compression: %d -> %d (%.1fx)\n", (int) isize, csize, (1. * isize) / csize);
  59. /* Write data_out to argv[3] */
  60. f = fopen(argv[3], "wb+");
  61. if (fwrite(data_out, 1, (size_t) csize, f) == SIZE) {
  62. printf("Wrote %s\n", argv[3]);
  63. } else {
  64. printf("Write failed");
  65. }
  66. } else {
  67. /* Read from argv[2] into data_out. */
  68. f = fopen(argv[2], "rb");
  69. fseek(f, 0, SEEK_END);
  70. fsize = ftell(f);
  71. fseek(f, 0, SEEK_SET);
  72. if (fread(data_out, 1, (size_t) fsize, f) == fsize) {
  73. printf("Checking %s\n", argv[2]);
  74. } else {
  75. printf("Read failed");
  76. }
  77. /* Decompress */
  78. dsize = blosc_decompress(data_out, data_dest, (size_t) dsize);
  79. if (dsize < 0) {
  80. printf("Decompression error. Error code: %d\n", dsize);
  81. return dsize;
  82. }
  83. printf("Decompression succesful!\n");
  84. }
  85. /* After using it, destroy the Blosc environment */
  86. blosc_destroy();
  87. return 0;
  88. }