]> infiniteadaptability.org Git - seeder/commitdiff
...
authoralex <[email protected]>
Sat, 4 Dec 2021 08:25:01 +0000 (00:25 -0800)
committeralex <[email protected]>
Sat, 4 Dec 2021 08:25:01 +0000 (00:25 -0800)
src/bencode/encode.c
src/torrent.c

index 8ba8ffc14d2fd632072d3b4f77773fbfb5d033f1..727e6086825bc6be3fdaf76ac9454bd62b6d4bd4 100644 (file)
@@ -70,9 +70,9 @@ ssize_t bencode_string(const uint8_t *str, size_t len, uint8_t *output, size_t o
 
 
 static size_t size_int(int i) {
-       i = abs(i);
-       size_t j = 0;
+       size_t j = (i<0)?1:0;
        
+       i = abs(i);
        if(0==i) { return 1; }
 
        while(i>0) {
index e23aa1fd899be10dad405ede3f689169a50cb27c..db20559f5d2de38ca92b7955aa066503e6f63327 100644 (file)
@@ -1,5 +1,6 @@
 #include<torrent.h>
 
+static int piece_layer_sort(const void*,const void*);
 static int torrent_add_file(struct torrent*,struct file*);
 static int torrent_file_announce(FILE*,struct torrent*); 
 static char *torrent_file_path(unsigned char*,size_t);
@@ -176,7 +177,53 @@ static char *torrent_file_path(unsigned char *infohash, size_t len) {
 }
 
 static int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
+       size_t index, pieces;
+       struct file *file_p;
+       struct block **piece_layers, *block_p;
+
+       pieces = 0;
+       for(size_t i=0;i<p->files->size;i++) {
+               file_p = (struct file*)p->files->map[i];
+               if(file_p!=NULL) {
+                       pieces += block_length(file_p->piece_layers);
+               }
+       }
+
+       piece_layers = malloc(sizeof(struct block*)*pieces);
+       if(NULL==piece_layers) {
+               return -1;
+       }
+
+       index = 0;
+       for(size_t i=0;i<p->files->size;i++) {
+               file_p = (struct file*)p->files->map[i];
+               if(file_p!=NULL) {
+                       block_p = file_p->piece_layers;
+                       while(block_p!=NULL) {
+                               piece_layers[index] = block_p;
+                               index++;
+                               block_p = block_p->next;
+                       }
+               }
+       }
+
+       qsort(piece_layers,pieces,sizeof(struct block*),&piece_layer_sort);
+
        return -1;
+//clean:
+//     free(piece_layers);
+//     return -1;
+}
+
+static int piece_layer_sort(const void *p1, const void *p2) {
+       struct block *a, *b;
+       a = (struct block*)p1;
+       b = (struct block*)p2;
+
+       if(a==NULL) { return 1; }
+       if(b==NULL) { return -1; }
+
+       return memcmp(a->hash,b->hash,crypto_hash_sha256_BYTES);
 }
 
 static int torrent_file_write_end(FILE *fp) {