From: alex Date: Thu, 16 Dec 2021 01:22:41 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=515fcffa7fff263fb5a78c56736304de848ae77a;p=seeder ... --- diff --git a/src/tree.c b/src/tree.c index 6503b08..b660585 100644 --- a/src/tree.c +++ b/src/tree.c @@ -151,8 +151,48 @@ ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t len) { return buf_len - len; } -size_t tree_bencode_length(struct tree *p) { - return 0; +/* + * Bencoded file entry: + * { + * "": { + * length: [LENGTH], + * pieces root: [ROOT] + * } + * } + * + * equals: d0:d5:lengthi[LENGTH]e11:pieces root32:[ROOT]ee + */ + +#define BENCODE_FILE_LENGTH_CONSTANT 13+18+2+crypto_hash_sha256_BYTES + +size_t tree_bencode_length(struct tree *tree) { + struct tree_entry *p; + size_t i, len; + + i = 1; // { + + p = tree->entries; + while(p!=NULL) { + len = strlen(p->name); + + i += bencode_size_int(len); + i++; // : + i += len; + + i++; // { + if(p->children!=NULL) { + i += tree_bencode_length(p->children); + } else { + i += BENCODE_FILE_LENGTH_CONSTANT; + i += bencode_size_int(p->file->size); + } + + i++; // } + } + + i++; // } + + return i; } void tree_entry_free(struct tree_entry *p) {