From 148eec92611367a6fc0f688377a74f89968bf7e7 Mon Sep 17 00:00:00 2001 From: Yunchih Chen Date: Thu, 30 Aug 2018 22:05:26 +0800 Subject: Checksum in a simple way Otherwise, cksum value mysteriously differs ... --- bin/nfextract.c | 3 ++- lib/collect.c | 3 +++ lib/common.c | 12 ++---------- lib/extract.c | 7 +++++-- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/bin/nfextract.c b/bin/nfextract.c index 44e7d1a..31e4605 100644 --- a/bin/nfextract.c +++ b/bin/nfextract.c @@ -92,6 +92,7 @@ static void extract_all(const char *storage_dir) { WARN(1, "Storage trunk file index " "out of predefined range: %s", ep->d_name); + return; } else { trunk_files[index] = strdup(ep->d_name); if (index > max_index) @@ -102,7 +103,7 @@ static void extract_all(const char *storage_dir) { closedir(dp); - for (i = 0; i < max_index; ++i) { + for (i = 0; i <= max_index; ++i) { if (trunk_files[i]) extract_each(storage_dir, trunk_files[i]); free(trunk_files[i]); diff --git a/lib/collect.c b/lib/collect.c index ee37510..c69c9a9 100644 --- a/lib/collect.c +++ b/lib/collect.c @@ -197,6 +197,9 @@ void *nfl_collect_worker(void *targs) { // write checksum nf->header->cksum = nfl_header_cksum(nf->header); + debug("Recv worker #%u: calculated checksum: %x", + nf->header->id, + nf->header->cksum); // spawn commit thread nfl_commit(nf); diff --git a/lib/common.c b/lib/common.c index 4e64c9e..c81dfdb 100644 --- a/lib/common.c +++ b/lib/common.c @@ -72,16 +72,8 @@ uint32_t nfl_get_filesize(FILE *f) { } uint32_t nfl_header_cksum(nfl_header_t *header) { -#define H(s) (0x9e3779b9 + (s << 6) + (s >> 1)) - register uint64_t s = 3784672181; - s ^= H(header->id); - s ^= H(header->max_n_entries); - s ^= H(header->n_entries); - s ^= H(header->raw_size); - s ^= H(header->compression_opt); - s ^= H(header->start_time); - s ^= H(header->end_time); - return s & UINT_MAX; + /* simply use a magic number for integrity check */ + return 0x9e37a9b9; } void nfl_cal_trunk(uint32_t total_size, uint32_t *trunk_cnt, diff --git a/lib/extract.c b/lib/extract.c index 21d51d0..ea10582 100644 --- a/lib/extract.c +++ b/lib/extract.c @@ -11,8 +11,11 @@ static int nfl_extract_zstd(FILE *f, nfl_state_t *state); static int nfl_extract_lz4(FILE *f, nfl_state_t *state); static int nfl_verify_header(nfl_header_t *header) { - if (header->cksum != nfl_header_cksum(header)) + if (header->cksum != nfl_header_cksum(header)) { + debug("Header checksum mismatch: expected: 0x%x, got: 0x%x", + header->cksum, nfl_header_cksum(header)); return -1; + } if (header->id > MAX_TRUNK_ID) return -1; @@ -90,7 +93,7 @@ int nfl_extract_worker(const char *filename, nfl_state_t *state) { // Check header validity WARN_RETURN(ferror(f), "%s", strerror(errno)); - WARN_RETURN(!got || nfl_verify_header(h) < 0, + WARN_RETURN(got == 0 || nfl_verify_header(h) < 0, "File %s has corrupted header.", filename); // Read body -- cgit v1.2.3