From: alex Date: Sat, 4 Dec 2021 08:25:01 +0000 (-0800) Subject: ... X-Git-Url: http://git.infiniteadaptability.org/?a=commitdiff_plain;h=bd279fff3900c53ca160206f920932a75f6e518d;p=seeder ... --- diff --git a/src/bencode/encode.c b/src/bencode/encode.c index 8ba8ffc..727e608 100644 --- a/src/bencode/encode.c +++ b/src/bencode/encode.c @@ -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) { diff --git a/src/torrent.c b/src/torrent.c index e23aa1f..db20559 100644 --- a/src/torrent.c +++ b/src/torrent.c @@ -1,5 +1,6 @@ #include +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;ifiles->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;ifiles->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) {