1
0
mirror of https://github.com/facebook/zstd.git synced 2025-07-30 22:23:13 +03:00

minor refactoring of harness

This commit is contained in:
Yann Collet
2019-10-17 14:11:54 -07:00
parent bfd829f254
commit b062b6fb2d

View File

@ -21,10 +21,6 @@ typedef unsigned char u8;
// Protect against allocating too much memory for output // Protect against allocating too much memory for output
#define MAX_OUTPUT_SIZE ((size_t)1024 * 1024 * 1024) #define MAX_OUTPUT_SIZE ((size_t)1024 * 1024 * 1024)
u8 *input;
u8 *output;
u8 *dict;
static size_t read_file(const char *path, u8 **ptr) static size_t read_file(const char *path, u8 **ptr)
{ {
FILE* const f = fopen(path, "rb"); FILE* const f = fopen(path, "rb");
@ -61,6 +57,10 @@ static size_t read_file(const char *path, u8 **ptr)
static void write_file(const char *path, const u8 *ptr, size_t size) static void write_file(const char *path, const u8 *ptr, size_t size)
{ {
FILE* const f = fopen(path, "wb"); FILE* const f = fopen(path, "wb");
if (!f) {
fprintf(stderr, "failed to open file %s \n", path);
exit(1);
}
size_t written = 0; size_t written = 0;
while (written < size) { while (written < size) {
@ -73,7 +73,8 @@ static void write_file(const char *path, const u8 *ptr, size_t size)
fclose(f); fclose(f);
} }
int main(int argc, char **argv) { int main(int argc, char **argv)
{
if (argc < 3) { if (argc < 3) {
fprintf(stderr, "usage: %s <file.zst> <out_path> [dictionary] \n", fprintf(stderr, "usage: %s <file.zst> <out_path> [dictionary] \n",
argv[0]); argv[0]);
@ -81,27 +82,31 @@ int main(int argc, char **argv) {
return 1; return 1;
} }
u8* input;
size_t const input_size = read_file(argv[1], &input); size_t const input_size = read_file(argv[1], &input);
u8* dict;
size_t dict_size = 0; size_t dict_size = 0;
if (argc >= 4) { if (argc >= 4) {
dict_size = read_file(argv[3], &dict); dict_size = read_file(argv[3], &dict);
} }
size_t decompressed_size = ZSTD_get_decompressed_size(input, input_size); size_t out_capacity = ZSTD_get_decompressed_size(input, input_size);
if (decompressed_size == (size_t)-1) { if (out_capacity == (size_t)-1) {
decompressed_size = MAX_COMPRESSION_RATIO * input_size; out_capacity = MAX_COMPRESSION_RATIO * input_size;
fprintf(stderr, "WARNING: Compressed data does not contain " fprintf(stderr, "WARNING: Compressed data does not contain "
"decompressed size, going to assume the compression " "decompressed size, going to assume the compression "
"ratio is at most %d (decompressed size of at most " "ratio is at most %d (decompressed size of at most "
"%u) \n", "%u) \n",
MAX_COMPRESSION_RATIO, (unsigned)decompressed_size); MAX_COMPRESSION_RATIO, (unsigned)out_capacity);
} }
if (decompressed_size > MAX_OUTPUT_SIZE) { if (out_capacity > MAX_OUTPUT_SIZE) {
fprintf(stderr, fprintf(stderr,
"Required output size too large for this implementation \n"); "Required output size too large for this implementation \n");
return 1; return 1;
} }
output = malloc(decompressed_size);
u8* const output = malloc(out_capacity);
if (!output) { if (!output) {
fprintf(stderr, "failed to allocate memory \n"); fprintf(stderr, "failed to allocate memory \n");
return 1; return 1;
@ -111,17 +116,17 @@ int main(int argc, char **argv) {
if (dict) { if (dict) {
parse_dictionary(parsed_dict, dict, dict_size); parse_dictionary(parsed_dict, dict, dict_size);
} }
size_t decompressed = size_t const decompressed_size =
ZSTD_decompress_with_dict(output, decompressed_size, ZSTD_decompress_with_dict(output, out_capacity,
input, input_size, parsed_dict); input, input_size,
parsed_dict);
free_dictionary(parsed_dict); free_dictionary(parsed_dict);
write_file(argv[2], output, decompressed); write_file(argv[2], output, decompressed_size);
free(input); free(input);
free(output); free(output);
free(dict); free(dict);
input = output = dict = NULL;
return 0; return 0;
} }