src/block.c \
src/default.c \
src/file.c \
+ src/fs/concat.c \
+ src/fs/dir.c \
+ src/fs/file.c \
+ src/fs/filter.c \
src/hash.c \
src/hashmap.c \
src/init.c \
src/torrent.c \
src/tree.c \
src/usage.c \
- src/util/concat.c \
- src/util/dir.c \
- src/util/file.c \
- src/util/filter.c \
- src/util/pow.c \
src/watch.c
seederd_SOURCES += \
inc/block.h \
inc/default.h \
inc/file.h \
+ inc/fs.h \
inc/hash.h \
inc/hashmap.h \
inc/init.h \
inc/torrent.h \
inc/tree.h \
inc/usage.h \
- inc/util.h \
inc/watch.h
SUBDIRS = . test/unit
int block_init(struct block**);
size_t block_length(struct block*);
int block_merkle_root(struct block*);
+int block_pad(struct block*);
#endif
#include<block.h>
+static unsigned long next_power_2(unsigned long);
+
int block_append_blank(struct block *p) {
if(NULL==p) { return -1; }
return 1;
}
+
+int block_pad(struct block *p) {
+ if(NULL==p) { return -1; }
+
+ size_t i = 1;
+ while(p->next!=NULL) {
+ i++;
+ p = p->next;
+ }
+
+ size_t pad = next_power_2(i) - i;
+ while(pad>0) {
+ if(block_init(&(p->next))<0) { return -1; }
+ memset(p->next->hash,0,crypto_hash_sha256_BYTES);
+ p = p->next;
+ pad--;
+ }
+
+ return 1;
+}
+
+static 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;
+}
// if the file is smaller than one piece then the block hashes
// should be padded to the next power of two instead of the next
// piece boundary
- size_t leaves_required = next_power_2(blocks);
- while(leaves_required>0) {
- if(block_append_blank(start)<0) { goto clean; }
- leaves_required--;
- }
+ if(block_pad(start)<0) { goto clean; }
}
if(block_merkle_root(start)<0) { goto clean; }
fclose(fp);
if(block_duplicate(&start,file_p->blocks)<0) { return -1; }
+ if(block_pad(start)<0) { return -1; }
if(block_merkle_root(start)<0) { return -1; }
memcpy(file_p->root,start->hash,crypto_hash_sha256_BYTES);
block_free(start);
+++ /dev/null
-#include<util.h>
-
-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;
-}
-DNDEBUG
endif
-check_PROGRAMS = bencode.tests block.tests file.tests hash.tests hashmap.tests torrent.tests tree.tests util.filter.tests
+check_PROGRAMS = bencode.tests block.tests file.tests hash.tests hashmap.tests torrent.tests tree.tests fs.filter.tests
TESTS = $(check_PROGRAMS)
if ENABLE_MEMCHECK
file.tests.c \
$(top_srcdir)/src/block.c \
$(top_srcdir)/src/file.c \
- $(top_srcdir)/src/hash.c \
- $(top_srcdir)/src/util/pow.c
+ $(top_srcdir)/src/hash.c
hash_tests_SOURCES = \
$(common_SOURCES) \
$(top_srcdir)/src/hash.c \
$(top_srcdir)/src/hashmap.c \
$(top_srcdir)/src/torrent.c \
- $(top_srcdir)/src/tree.c \
- $(top_srcdir)/src/util/pow.c
+ $(top_srcdir)/src/tree.c
tree_tests_SOURCES = \
$(common_SOURCES) \
$(top_srcdir)/src/block.c \
$(top_srcdir)/src/file.c \
$(top_srcdir)/src/hash.c \
- $(top_srcdir)/src/tree.c \
- $(top_srcdir)/src/util/pow.c
+ $(top_srcdir)/src/tree.c
-util_filter_tests_SOURCES = \
+fs_filter_tests_SOURCES = \
$(common_SOURCES) \
- util.filter.tests.c \
- $(top_srcdir)/src/util/filter.c
+ fs.filter.tests.c \
+ $(top_srcdir)/src/fs/filter.c
#include<block.h>
int main();
-static void block_append_blank_basic_test();
static void block_duplicate_basic_test();
static void block_init_basic_test();
static void block_length_basic_test();
static void block_merkle_root_basic_test();
+static void block_pad_basic_test();
int main() {
setup_env();
block_init_basic_test();
- block_append_blank_basic_test();
block_duplicate_basic_test();
block_length_basic_test();
block_merkle_root_basic_test();
+ block_pad_basic_test();
clean_env();
return EXIT_SUCCESS;
}
-static void block_append_blank_basic_test() {
- struct block *root, *p;
- unsigned char expected[crypto_hash_sha256_BYTES] = {0};
-
- assert(block_init(&p)==1);
- memset(p->hash,10,crypto_hash_sha256_BYTES);
-
- root = p;
-
- assert(block_length(root)==1);
- assert(block_append_blank(p)==1);
- assert(p->next!=NULL);
- p = p->next;
-
- assert(block_length(root)==2);
- assert(memcmp(p->hash,expected,crypto_hash_sha256_BYTES)==0);
-
- block_free(root);
-}
-
static void block_duplicate_basic_test() {
struct block *root, *root2, *p, *p2;
block_free(root);
}
+
+static void block_pad_basic_test() {
+ struct block *p;
+
+ assert(block_init(&p)==1);
+ assert(block_pad(p)==1);
+ assert(block_length(p)==1);
+
+ assert(block_init(&(p->next))==1);
+ assert(block_pad(p)==1);
+ assert(block_length(p)==2);
+
+ assert(block_init(&(p->next->next))==1);
+ assert(block_pad(p)==1);
+ assert(block_length(p)==4);
+
+ block_free(p);
+}
struct file *p;
FILE *fp;
unsigned char buf[16384];
- unsigned char expected[crypto_hash_sha256_BYTES] = {0};
+ unsigned char expected[crypto_hash_sha256_BYTES] = {58,71,55,213,6,94,65,97,113,64,176,175,139,67,229,164,13,18,213,164,166,129,202,162,224,45,9,227,191,155,144,144};
fp = fopen(TEST_FILE_1,"a");
assert(fp!=NULL);
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(10000==block_length(p->blocks));
+ assert(10001==block_length(p->blocks));
assert((10000*16384+21)==p->size);
file_free(p);
assert(file_init(&p,TEST_FILE_5)==1);
assert(file_hash(p,16384)==1);
- assert(1==block_length(p->blocks));
- assert(24==p->size);
+ assert(15625==block_length(p->blocks));
+ assert(256000000==p->size);
file_free(p);
}