From cc45da2b2bfed0ae67ab39a4f72a0df89a22ae7f Mon Sep 17 00:00:00 2001 From: alex Date: Tue, 12 Apr 2022 18:18:35 -0700 Subject: [PATCH] ... --- inc/piece.h | 2 ++ inc/torrent.h | 4 ++++ src/torrent/free.c | 2 ++ src/torrent/init.c | 1 + src/torrent/piece.c | 39 +++++++++++++++++++++------------------ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/inc/piece.h b/inc/piece.h index de80352..3cce33c 100644 --- a/inc/piece.h +++ b/inc/piece.h @@ -14,6 +14,8 @@ struct piece { #include +int piece_init(struct piece**); +void piece_free(struct piece*); struct piece* piece_load(const struct torrent*,size_t); #endif diff --git a/inc/torrent.h b/inc/torrent.h index ee1c66f..7623920 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -9,6 +9,7 @@ #include #include #include +#include #include struct torrent_files { @@ -26,6 +27,9 @@ struct torrent { struct tree *tree; struct torrent_files files; + struct piece **pieces; + size_t pieces_size; + uint8_t infohash[crypto_hash_sha256_BYTES]; int watch_fd; diff --git a/src/torrent/free.c b/src/torrent/free.c index abf6493..8be6e51 100644 --- a/src/torrent/free.c +++ b/src/torrent/free.c @@ -13,6 +13,8 @@ void torrent_free(struct torrent *p) { hashmap_free(p->files.paths); hashmap_free(p->files.roots); + if(p->pieces!=NULL) { free(p->pieces); } + if(p->tree!=NULL) { tree_free(p->tree); } free(p); diff --git a/src/torrent/init.c b/src/torrent/init.c index 692c180..443cf14 100644 --- a/src/torrent/init.c +++ b/src/torrent/init.c @@ -16,6 +16,7 @@ int torrent_init(struct torrent **torrent_p, unsigned long long piece_length) { (*torrent_p)->root = NULL; (*torrent_p)->name = NULL; (*torrent_p)->feed_url = NULL; + (*torrent_p)->pieces = NULL; (*torrent_p)->piece_length = piece_length; memset((*torrent_p)->infohash,0,crypto_hash_sha256_BYTES); diff --git a/src/torrent/piece.c b/src/torrent/piece.c index 9e59559..f5a7747 100644 --- a/src/torrent/piece.c +++ b/src/torrent/piece.c @@ -2,6 +2,7 @@ static int piece_layer_sort(const void*, const void*); static int torrent_bencode_piece_layers(FILE*,struct file*); +static int torrent_piece_layers(struct torrent*); #define PIECE_LAYER_BENCODED_LENGTH 2+1+32+2+1+32 @@ -29,10 +30,10 @@ static int torrent_bencode_piece_layers(FILE *fp, struct file *file_p) { return 1; } -int torrent_file_piece_layers(FILE *fp, struct torrent *p) { - const unsigned char **hashes; - size_t index, pieces; +static int torrent_piece_layers(struct torrent *p) { struct file *file_p; + struct block *block_p; + size_t index, pieces; pieces = 0; for(size_t i=0;ifiles.roots->size;i++) { @@ -42,38 +43,40 @@ int torrent_file_piece_layers(FILE *fp, struct torrent *p) { } } - hashes = malloc(sizeof(const unsigned char*)*pieces); - if(NULL==hashes) { return -1; } + // sort hashes first + qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layers_sort); + + torrent->pieces = malloc(sizeof(struct piece)*pieces); + if(NULL==torrent->pieces) { return -1; } index = 0; for(size_t i=0;ifiles.roots->size;i++) { file_p = (struct file*)p->files.roots->map[i]; if(file_p!=NULL) { if(file_p->size>p->piece_length) { - hashes[index] = file_p->root->hash; - index++; + block_p = file_p->piece_layers; + while(block_p!=NULL) { + torrent->pieces[index] = file_p->root->hash; + index++; + } } } } - qsort(hashes,pieces,sizeof(const unsigned char*),&piece_layer_sort); + return -1; +} - for(size_t i=0;ifiles.roots,hashes[i],crypto_hash_sha256_BYTES); - if(NULL==file_p) { goto clean; } +int torrent_file_piece_layers(FILE *fp, struct torrent *p) { + if(torrent_piece_layers(p)<0) { return -1; } - if(torrent_bencode_piece_layers(fp,file_p)<0) { goto clean; } + for(size_t i=0;ipieces_size;i++) { + if(torrent_bencode_piece_layers(fp,file_p)<0) { return -1; } } - free(hashes); - return 1; -clean: - free(hashes); - return -1; } -static int piece_layer_sort(const void *p1, const void *p2) { +static int piece_layers_sort(const void *p1, const void *p2) { if(NULL==p1) { return 1; } if(NULL==p2) { return -1; } -- 2.30.2