libeech

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

commit f63985b044c1830c68418b81dfd33bb0d81fb08d
parent 286c8e057c6773a0179200a7d964e4fcb4133010
Author: z3bra <contactatz3bradotorg>
Date:   Thu Oct 19 16:33:50 +0200

Simplify interger search for bencoded data

Diffstat:
be.c | 14++++++++++++++
be.h | 1+
libeech.c | 16+++++++---------
3 files changed, 22 insertions(+), 9 deletions(-)
diff --git a/be.c b/be.c @@ -297,6 +297,20 @@ bepath(struct be *b, char **p, size_t l) return 0; } +long +bekint(struct be *b, char *k, size_t kl) +{ + long n; + struct be v; + + if (bekv(b, k, kl, &v) < 0) + return -1; + if (beint(&v, &n) < 0) + return -1; + + return n; +} + char * bekstr(struct be *b, char *k, size_t kl) { diff --git a/be.h b/be.h @@ -24,6 +24,7 @@ int bedictnext(struct be *, char **, size_t *, struct be *); char betype(struct be *); int bekv(struct be *, char *, size_t, struct be *); int bepath(struct be *, char **, size_t); +long bekint(struct be *, char *, size_t); char * bekstr(struct be *, char *, size_t); #endif diff --git a/libeech.c b/libeech.c @@ -58,22 +58,20 @@ torrentsize(struct torrent *t) { int i; long l, sz = 0; - struct be info, f, v; + struct be info, file; 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) + if (!bekv(&info, "files", 5, &file)) { + for (i = 0; !belistnext(&file) && !belistover(&file); i++) { + l = bekint(&file, "length", 6); + if (l < 0) return -1; sz += l; } } else { - if (bekv(&info, "length", 6, &v) < 0) - return -1; - if (beint(&v, &sz) < 0) + sz = bekint(&info, "length", 6); + if (sz < 0) return -1; }