From 515fcffa7fff263fb5a78c56736304de848ae77a Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 15 Dec 2021 17:22:41 -0800 Subject: [PATCH] ... --- src/tree.c | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) 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) { -- 2.39.5