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*);
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*);
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;
#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);
+}