free(p->path);
}
+ if(p->blocks!=NULL) {
+ block_free(p->blocks);
+ }
+
free(p);
}
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");
}
while(1) {
- if(block_init(&start)<0) { goto clean; }
- p = start;
+ start = NULL;
+ p = NULL;
for(size_t i=0;i<blocks_per_piece;i++) {
size_t len = fread(data,sizeof(uint8_t),BLOCK_SIZE,fp);
if((len<=BLOCK_SIZE)&&(ferror(fp)!=0)) {
goto clean;
}
- if(len==0) { break; }
+ if(len==0) { goto done; }
file_p->size += 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);
}
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; }
#include<file.h>
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));
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);
}