...
authoralex <[email protected]>
Sat, 11 Dec 2021 01:23:56 +0000 (17:23 -0800)
committeralex <[email protected]>
Sat, 11 Dec 2021 01:23:56 +0000 (17:23 -0800)
inc/torrent.h
src/torrent/file.c
src/torrent/piece.c

index ad8aafc5d635e134143cd0c620a66226a9a0fcc3..24c67ab0cad0a6853ac59ce269c4fdeff1884dc2 100644 (file)
@@ -33,6 +33,7 @@ int torrent_add(struct torrent*,struct file*);
 int torrent_file(struct torrent*);
 ssize_t torrent_file_info(struct torrent*,uint8_t**);
 char *torrent_file_path(unsigned char*,size_t);
+int torrent_file_piece_layers(FILE*,struct torrent*);
 void torrent_free(struct torrent*);
 int torrent_init(struct torrent**,const char*,const char*,unsigned long long);
 char *torrent_magnet(struct torrent*);
index 3666f2b0f86a92a3217f48f0e7b6fc5e4d306c83..c0ed1457e741ba65318952b258a136bb5d988fca 100644 (file)
@@ -2,7 +2,6 @@
 
 static int piece_layer_sort(const void*,const void*);
 static int torrent_file_announce(FILE*,struct torrent*); 
-static int torrent_file_piece_layers(FILE*,struct torrent*);
 static int torrent_file_write_end(FILE*);
 static int torrent_file_write_info(FILE*,uint8_t*,size_t);
 static int torrent_file_write_piece_layers(FILE*,struct torrent*);
@@ -98,56 +97,6 @@ char *torrent_file_path(unsigned char *infohash, size_t len) {
        return path;
 }
 
-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.roots->size;i++) {
-               file_p = (struct file*)p->files.roots->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.roots->size;i++) {
-               file_p = (struct file*)p->files.roots->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) {
        uint8_t c;
        ssize_t i;
index ffa062e1dec67ff2514cf4aef5b195148f3c4936..94725f74996a6ae66b9b02215758b0cbec2c5825 100644 (file)
@@ -1,5 +1,72 @@
 #include<torrent.h>
 
-int torrent_bencode_piece_layers() {
+ssize_t torrent_bencode_piece_layers(struct torrent *torrent_p, unsigned char *hash) {
+       struct file *file_p;
+       struct block *p;
+       size_t buf_len, len;
+       ssize_t i;
+       uint8_t *buf;
+       
+       file_p = hashmap_find(torrent_p->files.roots,hash,crypto_hash_sha256_BYTES)
+       if(NULL==file_p) { return -1; }
+
+       // more lenght
+
+       buf = malloc(sizeof(uint8_t)*len);
+       if(NULL==buf) { return -1; }
+
+       buf_len = len;
+
+       if((i = bencode_string(file_p->root,crypto_hash_sha256_BYTES,buf,len))<0) { goto clean; }
+       buf += i;
+       len -= i;
+
+       p = file_p->piece_layers;
+       while(
+
        return -1;
 }
+
+int torrent_file_piece_layers(FILE *fp, struct torrent *p) {
+       unsigned char **hashes;
+       size_t index, pieces;
+       struct file *file_p;
+       struct block **piece_layers, *block_p;
+
+       pieces = 0;
+       for(size_t i=0;i<p->files.roots->size;i++) {
+               file_p = (struct file*)p->files.roots->map[i];
+               if(file_p!=NULL) { pieces++; }
+       }
+
+       hashes = malloc(sizeof(unsigned char*));
+       if(NULL==hashes) {
+               return -1;
+       }
+
+       index = 0;
+       for(size_t i=0;i<p->files.roots->size;i++) {
+               file_p = (struct file*)p->files.roots->map[i];
+               if(file_p!=NULL) {
+                       hashes[index] = file_p->root;
+               }
+       }
+
+       qsort(hashes,pieces,sizeof(unsigned char*),&piece_layer_sort);
+
+       // NOT DONE
+       for(size_t i=0;i<pieces;i++) {
+               file_p = (struct file*)p->files.roots->map[i];
+
+       return 1;
+//clean:
+//     free(piece_layers);
+//     return -1;
+}
+
+static int piece_layer_sort(const void *p1, const void *p2) {
+       if(NULL==a) { return 1; }
+       if(NULL==b) { return -1; }
+
+       return memcmp(p1,p2,crypto_hash_sha256_BYTES);
+}