From 5cf217dc4bcfc6d10fc15afa768ad50e403fd67e Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 2 Mar 2022 17:29:47 -0800 Subject: [PATCH] ... --- inc/file.h | 2 +- src/file.c | 19 +++++++++++-------- test/unit/file.tests.c | 36 +++++++++++++++++++++++------------- 3 files changed, 35 insertions(+), 22 deletions(-) diff --git a/inc/file.h b/inc/file.h index b700191..c67b1a8 100644 --- a/inc/file.h +++ b/inc/file.h @@ -17,8 +17,8 @@ struct file { char *path; struct block *blocks; - struct block *root; struct block *piece_layers; + struct block *root; uint64_t size; }; diff --git a/src/file.c b/src/file.c index 9ec06d8..af38d76 100644 --- a/src/file.c +++ b/src/file.c @@ -40,7 +40,8 @@ ssize_t file_bencode(struct file *p, uint8_t *buf, size_t len) { i = bencode_string(pieces_root_string,sizeof(pieces_root_string)-1,buf,len); if(advance(i,&buf,&len)<0) { return -1; } - i = bencode_string((uint8_t*)p->root,crypto_hash_sha256_BYTES,buf,len); + if(p->root==NULL) { return -1; } + i = bencode_string((uint8_t*)p->root->hash,crypto_hash_sha256_BYTES,buf,len); if(advance(i,&buf,&len)<0) { return -1; } i = bencode_dict_end(buf,buf_len); @@ -122,7 +123,7 @@ int file_hash(struct file *file_p, int piece_length) { if(hash(data,len,next->hash,crypto_hash_sha256_BYTES)<0) { goto clean; } if(NULL==p) { - file_p->piece_layers = next; + file_p->blocks = next; p = next; } else { p->next = next; @@ -132,12 +133,12 @@ int file_hash(struct file *file_p, int piece_length) { fclose(fp); - if(p->next!=NULL) { // i.e. more than 1 block - if(block_pad(p)<0) { goto clean; } + if(file_p->blocks->next!=NULL) { // i.e. more than 1 block + if(block_pad(file_p->blocks)<0) { return -1; } } if(file_piece_layers(file_p,piece_length)<0) { return -1; } - if(file_merkle_root(file_p)<0) { goto clean; } + if(file_merkle_root(file_p)<0) { return -1; } return 1; clean: @@ -146,7 +147,8 @@ clean: } static int file_merkle_root(struct file *file_p) { - return -1; + if(block_merkle_root(file_p->piece_layers,&(file_p->root))<0) { return -1; } + return 1; } static int file_piece_layers(struct file *file_p, int piece_length) { @@ -155,6 +157,7 @@ static int file_piece_layers(struct file *file_p, int piece_length) { ratio = piece_length / BLOCK_SIZE; + file_p->piece_layers = file_p->blocks; p = file_p->blocks; while(ratio>1) { if(block_merkle_layer(p,&(file_p->piece_layers))<0) { return -1; } @@ -182,9 +185,9 @@ int file_init(struct file **p, const char *path) { (*p)->name = strdup(b); - (*p)->root = NULL; - (*p)->piece_layers = NULL; (*p)->blocks = NULL; + (*p)->piece_layers = NULL; + (*p)->root = NULL; (*p)->size = 0; diff --git a/test/unit/file.tests.c b/test/unit/file.tests.c index 14e51e9..7136983 100644 --- a/test/unit/file.tests.c +++ b/test/unit/file.tests.c @@ -47,7 +47,8 @@ static void file_bencode_basic_test() { uint8_t expected[] = "d0:d6:lengthi1024e11:pieces root32:00000000000000000000000000000000ee"; assert(file_init(&p,TEST_FILE_1)==1); - memset(p->root,48,crypto_hash_sha256_BYTES); + assert(1==block_init(&(p->root))); + memset(p->root->hash,48,crypto_hash_sha256_BYTES); p->size = 1024; assert(69==file_bencode(p,buf,100)); @@ -70,32 +71,40 @@ static void file_hash_basic_test() { 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->piece_layers)); + assert(memcmp(p->root->hash,expected1,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(p->blocks==p->piece_layers); + assert(p->blocks==p->root); 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->piece_layers)); + assert(memcmp(p->root->hash,expected2,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(p->blocks==p->piece_layers); + assert(p->blocks==p->root); 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->piece_layers)); + assert(memcmp(p->root->hash,expected3,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(p->blocks==p->piece_layers); + assert(p->blocks==p->root); 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->piece_layers)); + assert(memcmp(p->root->hash,expected4,crypto_hash_sha256_BYTES)==0); + assert(1==block_length(p->blocks)); + assert(p->blocks==p->piece_layers); + assert(p->blocks==p->root); assert(51==p->size); file_free(p); @@ -109,7 +118,7 @@ static void file_hash_large_file_test() { assert(file_init(&p,TEST_FILE_1)==1); assert(file_hash(p,16384)==1); - assert(memcmp(p->root,expected,crypto_hash_sha256_BYTES)==0); + assert(memcmp(p->root->hash,expected,crypto_hash_sha256_BYTES)==0); assert(10001==block_length(p->piece_layers)); assert((10000*16384+21)==p->size); @@ -154,7 +163,6 @@ static void file_hash_random_file_test() { 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)); @@ -164,9 +172,11 @@ 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(NULL==p->piece_layers); + assert(NULL==p->root); + assert(0==p->size); file_free(p); -- 2.39.5