libeech

BitTorrent library
git clone git://z3bra.org/libeech.git
Log | Files | Refs | README | LICENSE

commit ba84409915193ec48b1becfaf434e3a1ba780628
parent 1f684681b6e996a18378c7567a1fa0b1fe903a45
Author: z3bra <contactatz3bradotorg>
Date:   Thu Oct 19 00:13:33 +0200

Function to retrieve torrent size

Diffstat:
libeech.c | 29+++++++++++++++++++++++++++++
1 file changed, 29 insertions(+), 0 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -18,6 +18,7 @@ /* helpers to retrieve attributes from metadata */ static char * peerid(); static unsigned char * infohash(struct torrent *); +static long torrentsize(struct torrent *); /* Initialization of the torrent struct */ static int loadfile(struct torrent *, char *); @@ -59,6 +60,34 @@ infohash(struct torrent *t) return hash; } +/* Calculate full torrent size */ +static long +torrentsize(struct torrent *t) +{ + int i; + long l, sz = 0; + struct be info, f, v; + + if (bekv(&t->be, "info", 4, &info) < 0) + return -1; + if (!bekv(&info, "files", 5, &f)) { + for (i = 0; !belistnext(&f) && !belistover(&f); i++) { + if (bekv(&f, "length", 6, &v) < 0) + return -1; + if (beint(&v, &l) < 0) + return -1; + sz += l; + } + } else { + if (bekv(&info, "length", 6, &v) < 0) + return -1; + if (beint(&v, &sz) < 0) + return -1; + } + + return sz; +} + /* Read torrent file into a memory buffer */ static int loadfile(struct torrent *t, char *path)