tSimplify interger search for bencoded data - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit f63985b044c1830c68418b81dfd33bb0d81fb08d
 (DIR) parent 286c8e057c6773a0179200a7d964e4fcb4133010
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Thu, 19 Oct 2017 16:33:50 +0200
       
       Simplify interger search for bencoded data
       
       Diffstat:
         M be.c                                |      14 ++++++++++++++
         M be.h                                |       1 +
         M libeech.c                           |      16 +++++++---------
       
       3 files changed, 22 insertions(+), 9 deletions(-)
       ---
 (DIR) diff --git a/be.c b/be.c
       t@@ -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)
        {
 (DIR) diff --git a/be.h b/be.h
       t@@ -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
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -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;
                }