refactor of harness, for clarity (#1974)

following #1953 (false positive, due to heuristic confusion)
This commit is contained in:
Yann Collet 2020-01-24 20:16:28 -08:00 committed by GitHub
parent 5c769e33f8
commit 5bcd6448b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -21,108 +21,93 @@ 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)
static size_t read_file(const char *path, u8 **ptr) // Error message then exit
#define ERR_OUT(...) { fprintf(stderr, __VA_ARGS__); exit(1); }
typedef struct {
u8* address;
size_t size;
} buffer_s;
static void freeBuffer(buffer_s b) { free(b.address); }
static buffer_s read_file(const char *path)
{ {
FILE* const f = fopen(path, "rb"); FILE* const f = fopen(path, "rb");
if (!f) { if (!f) ERR_OUT("failed to open file %s \n", path);
fprintf(stderr, "failed to open file %s \n", path);
exit(1);
}
fseek(f, 0L, SEEK_END); fseek(f, 0L, SEEK_END);
size_t const size = (size_t)ftell(f); size_t const size = (size_t)ftell(f);
rewind(f); rewind(f);
*ptr = malloc(size); void* const ptr = malloc(size);
if (!ptr) { if (!ptr) ERR_OUT("failed to allocate memory to hold %s \n", path);
fprintf(stderr, "failed to allocate memory to hold %s \n", path);
exit(1);
}
size_t const read = fread(*ptr, 1, size, f); size_t const read = fread(ptr, 1, size, f);
if (read != size) { /* must read everything in one pass */ if (read != size) ERR_OUT("error while reading file %s \n", path);
fprintf(stderr, "error while reading file %s \n", path);
exit(1);
}
fclose(f); fclose(f);
buffer_s const b = { ptr, size };
return read; return b;
} }
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) { if (!f) ERR_OUT("failed to open file %s \n", path);
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) {
written += fwrite(ptr+written, 1, size, f); written += fwrite(ptr+written, 1, size, f);
if (ferror(f)) { if (ferror(f)) ERR_OUT("error while writing file %s\n", path);
fprintf(stderr, "error while writing file %s\n", path); }
exit(1);
} }
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", ERR_OUT("usage: %s <file.zst> <out_path> [dictionary] \n", argv[0]);
argv[0]);
return 1; buffer_s const input = read_file(argv[1]);
}
u8* input; buffer_s dict = { NULL, 0 };
size_t const input_size = read_file(argv[1], &input);
u8* dict = NULL;
size_t dict_size = 0;
if (argc >= 4) { if (argc >= 4) {
dict_size = read_file(argv[3], &dict); dict = read_file(argv[3]);
} }
size_t out_capacity = ZSTD_get_decompressed_size(input, input_size); size_t out_capacity = ZSTD_get_decompressed_size(input.address, input.size);
if (out_capacity == (size_t)-1) { if (out_capacity == (size_t)-1) {
out_capacity = 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)out_capacity); MAX_COMPRESSION_RATIO, (unsigned)out_capacity);
} }
if (out_capacity > MAX_OUTPUT_SIZE) { if (out_capacity > MAX_OUTPUT_SIZE)
fprintf(stderr, ERR_OUT("Required output size too large for this implementation \n");
"Required output size too large for this implementation \n");
return 1;
}
u8* const output = malloc(out_capacity); u8* const output = malloc(out_capacity);
if (!output) { if (!output) ERR_OUT("failed to allocate memory \n");
fprintf(stderr, "failed to allocate memory \n");
return 1;
}
dictionary_t* const parsed_dict = create_dictionary(); dictionary_t* const parsed_dict = create_dictionary();
if (dict) { if (dict.size) {
parse_dictionary(parsed_dict, dict, dict_size); parse_dictionary(parsed_dict, dict.address, dict.size);
} }
size_t const decompressed_size = size_t const decompressed_size =
ZSTD_decompress_with_dict(output, out_capacity, ZSTD_decompress_with_dict(output, out_capacity,
input, input_size, input.address, input.size,
parsed_dict); parsed_dict);
free_dictionary(parsed_dict); free_dictionary(parsed_dict);
write_file(argv[2], output, decompressed_size); write_file(argv[2], output, decompressed_size);
free(input); freeBuffer(input);
freeBuffer(dict);
free(output); free(output);
free(dict);
return 0; return 0;
} }