]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Thu, 16 Dec 2021 01:22:41 +0000 (17:22 -0800)
committeralex <[email protected]>
Thu, 16 Dec 2021 01:22:41 +0000 (17:22 -0800)
src/tree.c

index 6503b082ff3fd4e71f97eff7ac80c8bfe6684a67..b66058573b59420a060ed6e104e23dac2775e2f5 100644 (file)
@@ -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) {