minor: DIFF determination

use gdiff on SunOS
This commit is contained in:
Yann Collet
2019-10-17 14:03:20 -07:00
parent 1a18f1484f
commit bfd829f254
2 changed files with 31 additions and 21 deletions
+11 -3
View File
@@ -7,8 +7,15 @@
# in the COPYING file in the root directory of this source tree). # in the COPYING file in the root directory of this source tree).
# ################################################################ # ################################################################
ZSTD ?= zstd # requires zstd installation on local system ZSTD ?= zstd # note: requires zstd installation on local system
UNAME?= $(shell uname)
ifeq ($(UNAME), SunOS)
DIFF ?= gdiff
else
DIFF ?= diff DIFF ?= diff
endif
HARNESS_FILES=*.c HARNESS_FILES=*.c
MULTITHREAD_LDFLAGS = -pthread MULTITHREAD_LDFLAGS = -pthread
@@ -30,7 +37,7 @@ harness: $(HARNESS_FILES)
clean: clean:
@$(RM) harness @$(RM) harness
@$(RM) -rf harness.dSYM @$(RM) -rf harness.dSYM # MacOS specific
test: harness test: harness
# #
@@ -46,7 +53,8 @@ test: harness
# note : files are presented multiple for training, to reach minimum threshold # note : files are presented multiple for training, to reach minimum threshold
@$(ZSTD) --train harness.c zstd_decompress.c zstd_decompress.h README.md \ @$(ZSTD) --train harness.c zstd_decompress.c zstd_decompress.h README.md \
harness.c zstd_decompress.c zstd_decompress.h README.md \ harness.c zstd_decompress.c zstd_decompress.h README.md \
harness.c zstd_decompress.c zstd_decompress.h README.md harness.c zstd_decompress.c zstd_decompress.h README.md \
-o dictionary
@$(ZSTD) -f README.md -D dictionary -o tmp.zst @$(ZSTD) -f README.md -D dictionary -o tmp.zst
@./harness tmp.zst tmp dictionary @./harness tmp.zst tmp dictionary
@$(DIFF) -s tmp README.md @$(DIFF) -s tmp README.md
+20 -18
View File
@@ -25,28 +25,29 @@ u8 *input;
u8 *output; u8 *output;
u8 *dict; u8 *dict;
size_t read_file(const char *path, u8 **ptr) { static size_t read_file(const char *path, u8 **ptr)
FILE *f = fopen(path, "rb"); {
FILE* const f = fopen(path, "rb");
if (!f) { if (!f) {
fprintf(stderr, "failed to open file %s\n", path); fprintf(stderr, "failed to open file %s \n", path);
exit(1); exit(1);
} }
fseek(f, 0L, SEEK_END); fseek(f, 0L, SEEK_END);
size_t size = (size_t)ftell(f); size_t const size = (size_t)ftell(f);
rewind(f); rewind(f);
*ptr = malloc(size); *ptr = malloc(size);
if (!ptr) { if (!ptr) {
fprintf(stderr, "failed to allocate memory to hold %s\n", path); fprintf(stderr, "failed to allocate memory to hold %s \n", path);
exit(1); exit(1);
} }
size_t pos = 0; size_t pos = 0;
while (!feof(f)) { while (!feof(f)) {
size_t read = fread(&(*ptr)[pos], 1, size, f); size_t const read = fread(*ptr + pos, 1, size, f);
if (ferror(f)) { if (ferror(f)) {
fprintf(stderr, "error while reading file %s\n", path); fprintf(stderr, "error while reading file %s \n", path);
exit(1); exit(1);
} }
pos += read; pos += read;
@@ -57,30 +58,30 @@ size_t read_file(const char *path, u8 **ptr) {
return pos; return pos;
} }
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 *f = fopen(path, "wb"); {
FILE* const f = fopen(path, "wb");
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)) {
fprintf(stderr, "error while writing file %s\n", path); fprintf(stderr, "error while writing file %s\n", path);
exit(1); 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", fprintf(stderr, "usage: %s <file.zst> <out_path> [dictionary] \n",
argv[0]); argv[0]);
return 1; return 1;
} }
size_t input_size = read_file(argv[1], &input); size_t const input_size = read_file(argv[1], &input);
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);
@@ -92,17 +93,17 @@ int main(int argc, char **argv) {
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 "
"%zu)\n", "%u) \n",
MAX_COMPRESSION_RATIO, decompressed_size); MAX_COMPRESSION_RATIO, (unsigned)decompressed_size);
} }
if (decompressed_size > MAX_OUTPUT_SIZE) { if (decompressed_size > 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); output = malloc(decompressed_size);
if (!output) { if (!output) {
fprintf(stderr, "failed to allocate memory\n"); fprintf(stderr, "failed to allocate memory \n");
return 1; return 1;
} }
@@ -122,4 +123,5 @@ int main(int argc, char **argv) {
free(output); free(output);
free(dict); free(dict);
input = output = dict = NULL; input = output = dict = NULL;
return 0;
} }