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);
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;
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:
}
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) {
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; }
(*p)->name = strdup(b);
- (*p)->root = NULL;
- (*p)->piece_layers = NULL;
(*p)->blocks = NULL;
+ (*p)->piece_layers = NULL;
+ (*p)->root = NULL;
(*p)->size = 0;
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));
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);
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);
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));
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);