commit 88318ab79460dbf2c045e987868159a04180f237
parent 70d86f8202e8ed1ee16fddef234dc3cebf19e2e5
Author: nolash <dev@holbrook.no>
Date: Tue, 14 Sep 2021 16:39:07 +0200
Correctness ok for file hashre
Diffstat:
2 files changed, 29 insertions(+), 5 deletions(-)
diff --git a/src/file.c b/src/file.c
@@ -19,6 +19,7 @@ int filehash_sum(filehash_t *fctx) {
int i;
int blocks;
long long blocks_span_length;
+ int trailer = 0;
int remain_length;
char *target;
@@ -28,14 +29,32 @@ int filehash_sum(filehash_t *fctx) {
target = fctx->buf + _SWARM_WORD_SIZE;
+ trailer = 0;
+
for (i = 0; i < SWARM_LEVELS; i++) {
if (fctx->ptr[i] == target) {
return fctx->length;
}
l = fctx->ptr[i] - fctx->ptr[i+1];
blocks = (l / _SWARM_WORD_SIZE);
- blocks_span_length = blocks * fctx->spans[i];
- blocks_span_length -= SWARM_BLOCK_SIZE - (SWARM_BLOCK_SIZE - remain_length);
+
+ if (blocks == 1) {
+ trailer = 1;
+ fctx->ptr[i+1] = fctx->ptr[i];
+ continue;
+ }
+
+ blocks_span_length = 0;
+
+ if (trailer == 1) {
+ blocks--;
+ blocks_span_length += SWARM_BLOCK_SIZE;
+ }
+
+ blocks_span_length += blocks * fctx->spans[i];
+ if (remain_length > 0) {
+ blocks_span_length -= SWARM_BLOCK_SIZE - remain_length;
+ }
bmt_init(bctx, fctx->ptr[i+1], l, (long long)(blocks_span_length));
r = bmt_sum(bctx);
diff --git a/test/check_file.c b/test/check_file.c
@@ -81,12 +81,14 @@ START_TEST(check_file_vectors) {
SWARM_BLOCK_SIZE,
SWARM_BLOCK_SIZE * 2,
SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE,
+ SWARM_BLOCK_SIZE * _SWARM_BATCH_SIZE + _SWARM_WORD_SIZE - 1,
};
char *vectors[] = {
"c10090961e7682a10890c334d759a28426647141213abda93b096b892824d2ef",
"29a5fb121ce96194ba8b7b823a1f9c6af87e1791f824940a53b5a7efe3f790d9",
"3047d841077898c26bbe6be652a2ec590a5d9bd7cd45d290ea42511b48753c09",
+ "e5c76afa931e33ac94bce2e754b1bb6407d07f738f67856783d93934ca8fc576",
};
filehash_init(&fh);
@@ -100,7 +102,10 @@ START_TEST(check_file_vectors) {
whole = lengths[i] / SWARM_BLOCK_SIZE;
part = lengths[i] % SWARM_BLOCK_SIZE;
- writes = whole + (int)((part - 1)/SWARM_BLOCK_SIZE);
+ writes = whole;
+ if (part > 0) {
+ writes++;
+ }
for (int j = 0; j < writes; j++) {
if (j < whole) {
@@ -109,9 +114,9 @@ START_TEST(check_file_vectors) {
l = part;
}
r = block_generate(&bg, buf, l);
- ck_assert_int_eq(r, SWARM_BLOCK_SIZE);
+ ck_assert_int_eq(r, l);
r = filehash_write(&fh, buf, l);
- ck_assert_int_eq(r, SWARM_BLOCK_SIZE);
+ ck_assert_int_eq(r, l);
}
r = filehash_sum(&fh);