From 51a4cd4edd4c276ddf00997b5d8e9f38e4191222 Mon Sep 17 00:00:00 2001 From: alex Date: Fri, 10 Dec 2021 17:23:56 -0800 Subject: [PATCH] ... --- inc/torrent.h | 1 + src/torrent/file.c | 51 --------------------------------- src/torrent/piece.c | 69 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 52 deletions(-) diff --git a/inc/torrent.h b/inc/torrent.h index ad8aafc..24c67ab 100644 --- a/inc/torrent.h +++ b/inc/torrent.h @@ -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*); diff --git a/src/torrent/file.c b/src/torrent/file.c index 3666f2b..c0ed145 100644 --- a/src/torrent/file.c +++ b/src/torrent/file.c @@ -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;ifiles.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;ifiles.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; diff --git a/src/torrent/piece.c b/src/torrent/piece.c index ffa062e..94725f7 100644 --- a/src/torrent/piece.c +++ b/src/torrent/piece.c @@ -1,5 +1,72 @@ #include -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;ifiles.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;ifiles.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;ifiles.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); +} -- 2.30.2