From 9ac1664448e711701aadc4683bbf1bd9ad2d7c10 Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 2 Nov 2021 15:04:22 -0700 Subject: [PATCH] ... --- inc/util.h | 2 +- src/block.c | 1 + src/file.c | 33 ++++++++++++++++++-------- src/util/pow.c | 12 ++++++++-- test/unit/block.tests.c | 4 ++-- test/unit/file.tests.c | 51 +++++++++++++++++++++++++++++++++++++++++ test/unit/tree.tests.c | 2 +- 7 files changed, 90 insertions(+), 15 deletions(-) diff --git a/inc/util.h b/inc/util.h index ebaf9cd..2329adb 100644 --- a/inc/util.h +++ b/inc/util.h @@ -12,6 +12,6 @@ int file_filter_all(const char*); int file_filter_ignore_dotfiles(const char*); int is_directory(const char*); int is_file(const char*); -size_t next_power_2(size_t); +unsigned long next_power_2(unsigned long); #endif diff --git a/src/block.c b/src/block.c index facc715..f1b5159 100644 --- a/src/block.c +++ b/src/block.c @@ -84,6 +84,7 @@ int block_merkle_root(struct block *root) { if(NULL==root) { return -1; } size_t len = block_length(root); + if(1==len) { return 0; } if((len&(len-1))!=0) { return -1; } p = root; diff --git a/src/file.c b/src/file.c index fe63072..8bcbc0d 100644 --- a/src/file.c +++ b/src/file.c @@ -11,6 +11,10 @@ void file_free(struct file *p) { free(p->path); } + if(p->blocks!=NULL) { + block_free(p->blocks); + } + free(p); } @@ -20,6 +24,11 @@ int file_hash(struct file *file_p, int piece_length) { FILE *fp; int blocks_per_piece; + if(NULL==file_p) { return -1; } + if((piece_length<16384)||(piece_length&&(piece_length&(piece_length-1)))) { + return -1; + } + blocks_per_piece = piece_length / BLOCK_SIZE; fp = fopen(file_p->path,"rb"); @@ -29,8 +38,8 @@ int file_hash(struct file *file_p, int piece_length) { } while(1) { - if(block_init(&start)<0) { goto clean; } - p = start; + start = NULL; + p = NULL; for(size_t i=0;isize += len; if(block_init(&next)<0) { goto clean; } if(hash(data,len,next->hash,crypto_hash_sha256_BYTES)<0) { goto clean; } - p->next = next; - p = p->next; + if(NULL==p) { + start = next; + p = next; + } else { + p->next = next; + p = p->next; + } } size_t blocks = block_length(start); @@ -64,16 +78,17 @@ int file_hash(struct file *file_p, int piece_length) { } if(block_merkle_root(start)<0) { goto clean; } - + if(NULL==file_p->blocks) { file_p->blocks = start; end = start; + } else { + end->next = start; + end = end->next; } - - end->next = start; - end = end->next; } +done: fclose(fp); if(block_duplicate(&start,file_p->blocks)<0) { return -1; } diff --git a/src/util/pow.c b/src/util/pow.c index aba6520..b3db5da 100644 --- a/src/util/pow.c +++ b/src/util/pow.c @@ -1,5 +1,13 @@ #include -size_t next_power_2(size_t i) { - return 0; +unsigned long next_power_2(unsigned long i) { + i--; + i |= i >> 1; + i |= i >> 2; + i |= i >> 4; + i |= i >> 8; + i |= i >> 16; + i++; + + return i; } diff --git a/test/unit/block.tests.c b/test/unit/block.tests.c index 9c33de5..22a61c8 100644 --- a/test/unit/block.tests.c +++ b/test/unit/block.tests.c @@ -12,9 +12,9 @@ static void block_merkle_root_basic_test(); int main() { setup_env(); + block_init_basic_test(); block_append_blank_basic_test(); block_duplicate_basic_test(); - block_init_basic_test(); block_length_basic_test(); block_merkle_root_basic_test(); @@ -137,7 +137,7 @@ static void block_merkle_root_basic_test() { assert(block_init(&root)==1); memset(root->hash,0,crypto_hash_sha256_BYTES); - assert(block_merkle_root(root)==-1); + assert(block_merkle_root(root)==0); assert(block_init(&(root->next))==1); memset(root->next->hash,1,crypto_hash_sha256_BYTES); diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c index 0e0a31b..7be295e 100644 --- a/test/unit/file.tests.c +++ b/test/unit/file.tests.c @@ -3,20 +3,67 @@ #include int main(); +static void file_hash_basic_test(); static void file_init_basic_test(); int main() { setup_env(); file_init_basic_test(); + file_hash_basic_test(); clean_env(); return EXIT_SUCCESS; } +static void file_hash_basic_test() { + struct file *p; + unsigned char expected1[crypto_hash_sha256_BYTES] = {174,73,190,37,189,244,236,113,18,122,255,20,5,229,77,151,187,72,27,75,203,32,25,154,81,249,19,29,118,7,228,229}; + unsigned char expected2[crypto_hash_sha256_BYTES] = {71,126,141,85,81,21,1,209,253,63,10,198,227,135,44,112,211,128,113,240,243,4,185,72,5,139,121,89,41,155,245,5}; + unsigned char expected3[crypto_hash_sha256_BYTES] = {47,41,56,87,56,3,117,236,236,192,175,52,172,206,68,105,215,102,47,147,47,213,216,92,13,12,117,223,28,179,169,157}; + unsigned char expected4[crypto_hash_sha256_BYTES] = {13,185,47,130,186,141,40,43,108,4,196,12,148,165,132,182,128,76,83,36,20,191,31,59,218,88,213,132,228,13,139,159}; + + assert(file_init(&p,TEST_FILE_1)==1); + + assert(file_hash(NULL,16384)==-1); + assert(file_hash(p,1384)==-1); + + assert(file_hash(p,16384)==1); + assert(memcmp(p->root,expected1,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(21==p->size); + + file_free(p); + + assert(file_init(&p,TEST_FILE_2)==1); + assert(file_hash(p,16384)==1); + assert(memcmp(p->root,expected2,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(26==p->size); + + file_free(p); + + assert(file_init(&p,TEST_FILE_3)==1); + assert(file_hash(p,16384)==1); + assert(memcmp(p->root,expected3,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(24==p->size); + + file_free(p); + + assert(file_init(&p,TEST_FILE_4)==1); + assert(file_hash(p,16384)==1); + assert(memcmp(p->root,expected4,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(51==p->size); + + file_free(p); +} + static void file_init_basic_test() { struct file *p; + unsigned char expected[crypto_hash_sha256_BYTES]; assert(-1==file_init(NULL,NULL)); assert(-1==file_init(&p,NULL)); @@ -26,6 +73,10 @@ static void file_init_basic_test() { assert(strcmp(p->name,"test")==0); assert(strcmp(p->path,"testdir/test")==0); + memset(expected,0,crypto_hash_sha256_BYTES); + assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0); + assert(NULL==p->blocks); + assert(0==p->size); file_free(p); } diff --git a/test/unit/tree.tests.c b/test/unit/tree.tests.c index c644c40..dfe2e52 100644 --- a/test/unit/tree.tests.c +++ b/test/unit/tree.tests.c @@ -12,9 +12,9 @@ static void tree_init_basic_test(); int main() { setup_env(); + tree_init_basic_test(); tree_add_basic_test(); tree_add_extended_test(); - tree_init_basic_test(); clean_env(); -- 2.30.2