harness.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2017-2021, 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>
  11. #include <stdlib.h>
  12. #include "zstd_decompress.h"
  13. typedef unsigned char u8;
  14. // If the data doesn't have decompressed size with it, fallback on assuming the
  15. // compression ratio is at most 16
  16. #define MAX_COMPRESSION_RATIO (16)
  17. // Protect against allocating too much memory for output
  18. #define MAX_OUTPUT_SIZE ((size_t)1024 * 1024 * 1024)
  19. // Error message then exit
  20. #define ERR_OUT(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
  21. typedef struct {
  22. u8* address;
  23. size_t size;
  24. } buffer_s;
  25. static void freeBuffer(buffer_s b) { free(b.address); }
  26. static buffer_s read_file(const char *path)
  27. {
  28. FILE* const f = fopen(path, "rb");
  29. if (!f) ERR_OUT("failed to open file %s \n", path);
  30. fseek(f, 0L, SEEK_END);
  31. size_t const size = (size_t)ftell(f);
  32. rewind(f);
  33. void* const ptr = malloc(size);
  34. if (!ptr) ERR_OUT("failed to allocate memory to hold %s \n", path);
  35. size_t const read = fread(ptr, 1, size, f);
  36. if (read != size) ERR_OUT("error while reading file %s \n", path);
  37. fclose(f);
  38. buffer_s const b = { ptr, size };
  39. return b;
  40. }
  41. static void write_file(const char* path, const u8* ptr, size_t size)
  42. {
  43. FILE* const f = fopen(path, "wb");
  44. if (!f) ERR_OUT("failed to open file %s \n", path);
  45. size_t written = 0;
  46. while (written < size) {
  47. written += fwrite(ptr+written, 1, size, f);
  48. if (ferror(f)) ERR_OUT("error while writing file %s\n", path);
  49. }
  50. fclose(f);
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. if (argc < 3)
  55. ERR_OUT("usage: %s <file.zst> <out_path> [dictionary] \n", argv[0]);
  56. buffer_s const input = read_file(argv[1]);
  57. buffer_s dict = { NULL, 0 };
  58. if (argc >= 4) {
  59. dict = read_file(argv[3]);
  60. }
  61. size_t out_capacity = ZSTD_get_decompressed_size(input.address, input.size);
  62. if (out_capacity == (size_t)-1) {
  63. out_capacity = MAX_COMPRESSION_RATIO * input.size;
  64. fprintf(stderr, "WARNING: Compressed data does not contain "
  65. "decompressed size, going to assume the compression "
  66. "ratio is at most %d (decompressed size of at most "
  67. "%u) \n",
  68. MAX_COMPRESSION_RATIO, (unsigned)out_capacity);
  69. }
  70. if (out_capacity > MAX_OUTPUT_SIZE)
  71. ERR_OUT("Required output size too large for this implementation \n");
  72. u8* const output = malloc(out_capacity);
  73. if (!output) ERR_OUT("failed to allocate memory \n");
  74. dictionary_t* const parsed_dict = create_dictionary();
  75. if (dict.size) {
  76. #if defined (ZDEC_NO_DICTIONARY)
  77. printf("dict.size = %zu \n", dict.size);
  78. ERR_OUT("no dictionary support \n");
  79. #else
  80. parse_dictionary(parsed_dict, dict.address, dict.size);
  81. #endif
  82. }
  83. size_t const decompressed_size =
  84. ZSTD_decompress_with_dict(output, out_capacity,
  85. input.address, input.size,
  86. parsed_dict);
  87. free_dictionary(parsed_dict);
  88. write_file(argv[2], output, decompressed_size);
  89. freeBuffer(input);
  90. freeBuffer(dict);
  91. free(output);
  92. return 0;
  93. }