]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Fri, 3 Dec 2021 01:02:35 +0000 (17:02 -0800)
committeralex <[email protected]>
Fri, 3 Dec 2021 01:02:35 +0000 (17:02 -0800)
inc/tree.h
src/bencode/encode.c
src/tree.c
test/unit/Makefile.am
test/unit/bencode.tests.c

index 10e3dd43bc4ae0066916f001ab07154239e17041..b2893bb3f5d8f24f3ca3a67018ba3560c441af9b 100644 (file)
@@ -3,6 +3,7 @@
 
 #include<stdlib.h>
 
+#include<bencode.h>
 #include<file.h>
 
 struct tree_entry {
index b21c6b8d6a7928362ccff674abd61462c43abfe4..01fc8c8ff00912044d965c22aa5f8411af56bf5f 100644 (file)
@@ -55,28 +55,26 @@ ssize_t bencode_list_start(uint8_t *output, size_t output_size) {
 
 ssize_t bencode_string(const uint8_t *str, size_t len, uint8_t *output, size_t output_size) {
        size_t i;
-       int ret;
        
        if(NULL==str) { return -1; }
-       if(len<=0) { return -1; }
        if(NULL==output) { return -1; }
-       if(len>output_size) { return -1; }
 
        i = size_int(len);
-       i++;
+       i += 2; // account for ':' and '\0'
        i += len;
 
-       if((ret = snprintf((char*)output,output_size,"%lu:%s",len,(const char*)str))<0) { return -1; }
-
-       if(ret!=i) { return -1; }
+       if(i>output_size) { return -1; }
 
-       return i;
+       return snprintf((char*)output,i,"%lu:%s",len,(const char*)str);
 }
 
 
 static size_t size_int(int i) {
        i = abs(i);
        size_t j = 0;
+       
+       if(0==i) { return 1; }
+
        while(i>0) {
                i /= 10;
                j++;
index 23bcaace9f130bdc838f4b76291b341efcda1e98..1b3207affc83652e39887d5ad2996c1818c93a20 100644 (file)
@@ -112,9 +112,16 @@ static int tree_add_file(struct tree *root, struct file *to_add) {
 
 ssize_t tree_bencode(struct tree *tree, uint8_t *buf, size_t buf_len) {
        struct tree_entry *p;
+       ssize_t i;
 
        p = tree->entries;
        while(p!=NULL) {
+               i = bencode_string((const uint8_t*)p->name,strlen(p->name),buf,buf_len);
+               if(i<0) { return -1; }
+
+               buf += i;
+               buf_len -= i;
+
                if(p->children!=NULL) {
                        // write directory
                        // recurse into subdirectory
index 17e239c7b66afd2e865ad5fd772ee9757fa8a6ed..a1ae89d483330cad5ceab00ec4a22bd45a988db7 100644 (file)
@@ -76,6 +76,7 @@ torrent_tests_SOURCES = \
 tree_tests_SOURCES = \
        $(common_SOURCES) \
        tree.tests.c \
+       $(top_srcdir)/src/bencode/encode.c \
        $(top_srcdir)/src/block.c \
        $(top_srcdir)/src/file.c \
        $(top_srcdir)/src/hash.c \
index 48e8efb861393fcc74bc01e27d2b455d68a86058..25a4320748b73ad0555e5b4eaae0a1435d1c7ee8 100644 (file)
@@ -142,10 +142,12 @@ static void bencode_string_basic_test() {
 
        uint8_t str1[] = "testlkajslfkdjasdfl test string";
        assert(bencode_string(NULL,31,buf,1024)==-1);
-       assert(bencode_string(str1,0,buf,1024)==-1);
        assert(bencode_string(str1,31,NULL,1024)==-1);
        assert(bencode_string(str1,31,buf,0)==-1);
-       assert(bencode_string(str1,31,buf,1024)==34);
+       
+       assert(bencode_string(str1,0,buf,1024)==2);
+       assert(memcmp("0:",buf,2)==0);
 
+       assert(bencode_string(str1,31,buf,1024)==34);
        assert(memcmp("31:testlkajslfkdjasdfl test string",buf,34)==0);
 }