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) {