tHave bekstr() return a bencoding structure - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 30c885bf1a9870d5d0d983ad719511eaccd052d4
 (DIR) parent a9b0348be67fa7baacce53c098b659e7e24b0268
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Sun, 22 Oct 2017 08:56:34 +0200
       
       Have bekstr() return a bencoding structure
       
       Diffstat:
         M be.c                                |      19 ++++++++++---------
         M be.h                                |       2 +-
         M libeech.c                           |       8 ++++----
       
       3 files changed, 15 insertions(+), 14 deletions(-)
       ---
 (DIR) diff --git a/be.c b/be.c
       t@@ -311,22 +311,23 @@ bekint(struct be *b, char *k, size_t kl)
                return n;
        }
        
       -char *
       -bekstr(struct be *b, char *k, size_t kl)
       +int
       +bekstr(struct be *b, char *k, size_t kl, struct be *s)
        {
                char *sp;
                size_t vl;
                struct be v;
       -        static char s[LINE_MAX];
        
       -        memset(s, 0, LINE_MAX);
                if (bekv(b, k, kl, &v) < 0)
       -                return NULL;
       +                return -1;
                if (bestr(&v, &sp, &vl) < 0)
       -                return NULL;
       +                return -1;
        
       -        memcpy(s, sp, MIN(vl, LINE_MAX));
       -        s[MIN(vl, LINE_MAX - 1)] = 0;
       +        if (s) {
       +                s->start = v.start;
       +                s->off = sp;
       +                s->end = sp + vl;
       +        }
        
       -        return s;
       +        return 0;
        }
 (DIR) diff --git a/be.h b/be.h
       t@@ -25,6 +25,6 @@ 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);
       +int bekstr(struct be *, char *, size_t, struct be *);
        
        #endif
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -65,12 +65,12 @@ torrentsize(struct torrent *t)
        static int
        loadtracker(struct torrent *t)
        {
       -        char *sp;
       +        struct be v;
        
       -        sp = bekstr(&t->be, "announce", 8);
       -        if (!sp)
       +        if (bekstr(&t->be, "announce", 8, &v) < 0)
                        return -1;
       -        memcpy(t->tr, sp, strlen(sp));
       +
       +        memcpy(t->tr, v.off, v.end - v.off);
                return 0;
        }